function_extend.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. require_once FRAMEWORK_PATH . 'protected/extensions/function_extend.php';
  3. require_once FRAMEWORK_PATH . 'protected/extensions/format_extend.php';
  4. function getAwsSeverIp() {
  5. $output = array();
  6. $return_var = array();
  7. $ret = exec("ifconfig", $output, $return_var);
  8. $ips = [];
  9. foreach ($output as $row) {
  10. $matches = [];
  11. preg_match('/inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/', $row, $matches);
  12. if ($matches) {
  13. $ip = $matches[1];
  14. $ips[$ip] = $ip;
  15. }
  16. }
  17. unset($ips['127.0.0.1']);
  18. return current($ips);
  19. }
  20. /**
  21. * 发送告警信息
  22. */
  23. function alermErrorMsg($msg) {
  24. Tool::err($msg);
  25. if (ENV == ENV_DEV) {
  26. Eos::log("【error】$msg");
  27. return false;
  28. }
  29. $msg = trim($msg);
  30. if (empty($msg)) {
  31. return false;
  32. }
  33. $amc_url = 'http://61.160.36.226/default/recordMsg';
  34. $data = [
  35. "code"=> CODE_UNKNOW_ERROT,
  36. "code_msg"=> $msg,
  37. "app_id"=> 'dw_box',
  38. "server_ip"=> getAwsSeverIp() ?: '127.0.0.1',
  39. "alerm_type"=> 'ERROR',
  40. "time"=> time()
  41. ];
  42. $appKey = 'a67362f15b13cf5df4ac695b39844c84';
  43. $tmpArr = $data;
  44. $tmpArr['app_key'] = $appKey;
  45. sort($tmpArr, SORT_STRING);
  46. $tmpStr = implode($tmpArr);
  47. $signStr = sha1($tmpStr);
  48. $data['sign'] = $signStr;
  49. $header = 'Host: amc-admin.duowan.com';
  50. $ret = (new dwHttp)->post($amc_url, $data, 5, $header);
  51. $ret = json_decode($ret, true);
  52. if ($ret['result']) {
  53. return true;
  54. } else {
  55. var_dump("【error】告警错误:" . $ret['msg']);
  56. return false;
  57. }
  58. }
  59. /**
  60. * 判断是否手机请求
  61. * @return boolean [description]
  62. */
  63. function is_mobile_request() {
  64. if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
  65. return true;
  66. }
  67. // But WP7 is also Windows, with a slightly different characteristic
  68. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') !== false) {
  69. return true;
  70. }
  71. return false;
  72. }
  73. function awsReplaceArrImg(&$arr, $key) {
  74. foreach ($arr as $i => $data) {
  75. $arr[$i][$key] = awsReplaceImg($data[$key]);
  76. }
  77. }
  78. function awsReplaceImg($content) {
  79. $domain = 'image.meechat.me';
  80. $ua = $_SERVER["HTTP_USER_AGENT"];
  81. if (strpos($ua,'MSIE ') === false) {
  82. $scheme = 'https';
  83. } else {
  84. $scheme = 'http';
  85. }
  86. $from = [
  87. '//w2.meechat.me/',
  88. '//w3.meechat.me/',
  89. ];
  90. $to = [
  91. "//{$domain}/ips/w2.meechat.me/",
  92. "//{$domain}/ips/w3.meechat.me/",
  93. ];
  94. $content = str_replace($from, $to, $content);
  95. // 替换为http请求
  96. if ($scheme == 'https') {
  97. $content = str_replace("http://{$domain}/", "{$scheme}://{$domain}/", $content);
  98. }
  99. return $content;
  100. }
  101. function coverReplaceArrImage(&$arr, $key) {
  102. foreach ($arr as $i => $data) {
  103. $arr[$i][$key] = coverReplaceImage($data[$key]);
  104. }
  105. }
  106. function coverReplaceImage($src, $w = 180) {
  107. $url = awsReplaceImg($src);
  108. if ($url != $src) {
  109. if (false !== strpos($url, '?')) {
  110. $url .= '&';
  111. } else {
  112. $url .= '?';
  113. }
  114. $url .= "imageview/0/w/{$w}";
  115. $supportWebp = strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') || $_COOKIE['_webp'];
  116. if ($supportWebp) {
  117. $url .= '/format/webp';
  118. }
  119. }
  120. return $url;
  121. }