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