Util.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Copyright (C) Alibaba Cloud Computing
  4. * All rights reserved
  5. */
  6. class Aliyun_Log_Util {
  7. /**
  8. * Get the local machine ip address.
  9. *
  10. * @return string
  11. */
  12. public static function getLocalIp() {
  13. try { // if exec can be used
  14. $preg = "/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";
  15. if ( PATH_SEPARATOR==':' ) { // linux
  16. //exec("ifconfig", $out, $stats);
  17. if (!empty($out)) {
  18. if (isset($out[1]) && strstr($out[1], 'addr:')) {
  19. $tmpArray = explode(":", $out[1]);
  20. $tmpIp = explode(" ", $tmpArray[1]);
  21. if (preg_match($preg, trim($tmpIp[0])))
  22. return trim($tmpIp[0]);
  23. }
  24. }
  25. } else { // windows PATH_SEPARATOR==';'
  26. exec("ipconfig", $out, $stats);
  27. if (!empty($out)) {
  28. foreach ($out AS $row) {
  29. if (strstr($row, "IP") && strstr($row, ":") && !strstr($row, "IPv6")) {
  30. $tmpIp = explode(":", $row);
  31. if (preg_match($preg, trim($tmpIp[1])))
  32. return trim($tmpIp[1]);
  33. }
  34. }
  35. }
  36. }
  37. } catch ( Exception $ex ){
  38. }
  39. if (isset($_ENV["HOSTNAME"]))
  40. $MachineName = $_ENV["HOSTNAME"];
  41. else if (isset($_ENV["COMPUTERNAME"]))
  42. $MachineName = $_ENV["COMPUTERNAME"];
  43. else
  44. $MachineName = "";
  45. if ($MachineName!="")
  46. return $MachineName;
  47. return '127.0.0.1';
  48. }
  49. /**
  50. * If $gonten is raw IP address, return true.
  51. *
  52. * @return bool
  53. */
  54. public static function isIp($gonten){
  55. $ip = explode(".", $gonten);
  56. for($i=0;$i<count($ip);++$i)
  57. if($ip[$i]>255)
  58. return 0;
  59. return preg_match("/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $gonten);
  60. }
  61. /**
  62. * Calculate string $value MD5.
  63. *
  64. * @return string
  65. */
  66. public static function calMD5($value) {
  67. return strtoupper(md5($value));
  68. }
  69. /**
  70. * Calculate string $content hmacSHA1 with secret key $key.
  71. *
  72. * @return string
  73. */
  74. public static function hmacSHA1($content, $key) {
  75. $signature = hash_hmac("sha1", $content, $key, true);
  76. return base64_encode($signature);
  77. }
  78. /**
  79. * Change $logGroup to bytes.
  80. *
  81. * @return string
  82. */
  83. public static function toBytes($logGroup) {
  84. $mem = fopen("php://memory", "rwb");
  85. $logGroup->write($mem);
  86. rewind($mem);
  87. $bytes="";
  88. if(feof($mem)===false){
  89. $bytes = fread($mem, 10*1024*1024);
  90. }
  91. fclose($mem);
  92. return $bytes;
  93. //$mem = fopen("php://memory", "wb");
  94. /* $fiveMBs = 5*1024*1024;
  95. $mem = fopen("php://temp/maxmemory:$fiveMBs", 'rwb');
  96. $logGroup->write($mem);
  97. // rewind($mem);
  98. // fclose($mem);
  99. //d://logGroup.pdoc
  100. // $mem = fopen("php://memory", "rb");
  101. // $mem = fopen("php://temp/maxmemory:$fiveMBs", 'r+');
  102. $bytes;
  103. while(!feof($mem))
  104. $bytes = fread($mem, 10*1024*1024);
  105. fclose($mem);
  106. //test
  107. if($bytes===false)echo "fread fail";
  108. return $bytes;*/
  109. }
  110. /**
  111. * Get url encode.
  112. *
  113. * @return string
  114. */
  115. public static function urlEncodeValue($value) {
  116. return urlencode ( $value );
  117. }
  118. /**
  119. * Get url encode.
  120. *
  121. * @return string
  122. */
  123. public static function urlEncode($params) {
  124. ksort ( $params );
  125. $url = "";
  126. $first = true;
  127. foreach ( $params as $key => $value ) {
  128. $val = Aliyun_Log_Util::urlEncodeValue ( $value );
  129. if ($first) {
  130. $first = false;
  131. $url = "$key=$val";
  132. } else
  133. $url .= "&$key=$val";
  134. }
  135. return $url;
  136. }
  137. /**
  138. * Get canonicalizedLOGHeaders string as defined.
  139. *
  140. * @return string
  141. */
  142. public static function canonicalizedLOGHeaders($header) {
  143. ksort ( $header );
  144. $content = '';
  145. $first = true;
  146. foreach ( $header as $key => $value )
  147. if (strpos ( $key, "x-log-" ) === 0 || strpos ( $key, "x-acs-" ) === 0) { // x-log- header
  148. if ($first) {
  149. $content .= $key . ':' . $value;
  150. $first = false;
  151. } else
  152. $content .= "\n" . $key . ':' . $value;
  153. }
  154. return $content;
  155. }
  156. /**
  157. * Get canonicalizedResource string as defined.
  158. *
  159. * @return string
  160. */
  161. public static function canonicalizedResource($resource, $params) {
  162. if ($params) {
  163. ksort ( $params );
  164. $urlString = "";
  165. $first = true;
  166. foreach ( $params as $key => $value ) {
  167. if ($first) {
  168. $first = false;
  169. $urlString = "$key=$value";
  170. } else
  171. $urlString .= "&$key=$value";
  172. }
  173. return $resource . '?' . $urlString;
  174. }
  175. return $resource;
  176. }
  177. /**
  178. * Get request authorization string as defined.
  179. *
  180. * @return string
  181. */
  182. public static function getRequestAuthorization($method, $resource, $key,$stsToken, $params, $headers) {
  183. if (! $key)
  184. return '';
  185. $content = $method . "\n";
  186. if (isset ( $headers ['Content-MD5'] ))
  187. $content .= $headers ['Content-MD5'];
  188. $content .= "\n";
  189. if (isset ( $headers ['Content-Type'] ))
  190. $content .= $headers ['Content-Type'];
  191. $content .= "\n";
  192. $content .= $headers ['Date'] . "\n";
  193. $content .= Aliyun_Log_Util::canonicalizedLOGHeaders ( $headers ) . "\n";
  194. $content .= Aliyun_Log_Util::canonicalizedResource ( $resource, $params );
  195. return Aliyun_Log_Util::hmacSHA1 ( $content, $key );
  196. }
  197. }