-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcommons.php
296 lines (252 loc) · 4.36 KB
/
commons.php
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/*
*---------------------------------------------------------------
* 常用基础函数模块
*---------------------------------------------------------------
* @copyright (c) 2013
* @author Qiufeng <[email protected]>
* @version 1.0
*/
/**
* 设置回复
*
* @access public
* @param string $key
* @param string $value
* @param array $replys
* @return array
*/
function set_reply($key, $value, &$replys)
{
// key exists if replys
if (array_key_exists($key, $replys))
{
if ( ! is_array($replys[$key]))
{
$replys[$key] = (array)$replys[$key];
}
foreach ($replys[$key] as $k => $v)
{
if ($v == $value)
{
return $replys;
}
}
$replys[$key] = (array)$replys[$key];
array_push($replys[$key], $value);
return $replys;
}
// 直接保存值
$replys[$key] = $value;
return $replys;
}
/**
* 删除带值回复
*
* @access public
* @param string $key
* @param string $value
* @param array $replys
* @return array
*/
function delete_reply($key, $value, &$replys)
{
if ( ! array_key_exists($key, $replys))
{
return $replys;
}
if (is_array($replys[$key]) && count($replys[$key]) >= 2)
{
foreach ($replys[$key] as $k => $v)
{
if ($v == $value)
{
unset($replys[$key][$k]);
return $replys;
}
}
}
if ($replys[$key] == $value)
{
unset($replys[$key]);
}
if (is_array($replys[$key]) && implode("", $replys[$key]) == $value)
{
unset($replys[$key]);
}
return $replys;
}
/**
* 删除回复
*
* @access public
* @param string $key
* @param array $replys
* @return array
*/
function remove_reply($key, &$replys)
{
if ( ! array_key_exists($key, $replys))
{
return $replys;
}
unset($replys[$key]);
return $replys;
}
/**
* 读取文件
*
* @access public
* @param string $file
* @return string
*/
function read_file($file)
{
if ( ! file_exists($file))
{
return FALSE;
}
if (function_exists('file_get_contents'))
{
return file_get_contents($file);
}
if ( ! $fp = @fopen($file, 'rb'))
{
return FALSE;
}
flock($fp, LOCK_SH);
$data = '';
if (filesize($file) > 0)
{
$data =& fread($fp, filesize($file));
}
flock($fp, LOCK_UN);
fclose($fp);
return $data;
}
/**
* 写入文件
*
* @access public
* @param string $path
* @param string $data
* @param string $mode
* @return boolean
*/
function write_file($path, $data, $mode = "wb")
{
if ( ! $fp = @fopen($path, $mode))
{
return FALSE;
}
flock($fp, LOCK_EX);
fwrite($fp, $data);
flock($fp, LOCK_UN);
fclose($fp);
return TRUE;
}
/**
* 对象转数组
*
* @access public
* @param object $obj
* @return array
*/
function obj_to_array($obj)
{
$ret = array();
foreach ($obj as $key =>$value)
{
if (gettype($value) == 'array' || gettype($value) == 'object')
{
$ret[$key] = obj_to_array($value);
}
else
{
$ret[$key] = $value;
}
}
return $ret;
}
function log_message($level = 'error', $message, $php_error = FALSE)
{
if (log_threshold == 0)
{
return;
}
write_log($level, $message, $php_error);
}
/**
* 写入日志文件
* 一般情况下,此方法由log_message函数调用
*
* @access private
* @param string $level
* @param string $msg
* @param boolean $php_error
* @return boolean
*/
function write_log($level = 'error', $msg, $php_error = FALSE)
{
$level = strtoupper($level);
$filepath = temp_dir.'/log-'.date('Y-m-d').'.php';
$message = '';
if ( ! file_exists($filepath))
{
$message .= "WEBQQ DEBUG LOG\n\n";
}
if ( ! $fp = @fopen($filepath, "ab"))
{
return FALSE;
}
$message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date("Y-m-d H:i:s"). ' --->'.$msg."\n";
flock($fp, LOCK_EX);
fwrite($fp, $message);
flock($fp, LOCK_UN);
fclose($fp);
@chmod($filepath, 0666);
return TRUE;
}
/**
* 解析cookie
*
* @access public
* @return array
*/
function parse_cookie()
{
// Netscape HTTP Cookie File
$cookies = file(temp_dir."cookie");
$data = array();
foreach ($cookies as $v)
{
if (preg_match("/(.*\.qq\.com)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\n/U", $v, $p))
{
$data[] = array_slice($p, 1);
}
}
return $data;
}
/**
* 获取cookie
*
* public
* @param array $cookie
* @return array
*/
function get_cookie($cookie = NULL)
{
if ($cookie === NULL)
{
$cookie = parse_cookie();
}
if (is_array($cookie) && count($cookie)<=6)
{
return FALSE;
}
foreach ($cookie as $v)
{
$data[$v[5]] =$v[6];
}
return $data;
}