-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.php
More file actions
449 lines (400 loc) · 11.6 KB
/
util.php
File metadata and controls
449 lines (400 loc) · 11.6 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
/**
* Shared utilities
*
* Lots of things TODO
*/
/**
* 設定 Regular Expression 常用 Pattern
*/
define('RE_EMAIL', '/^[0-9a-z._-]+@[0-9a-z_-]+(\.[0-9a-z_-]+)+$/i');
define('RE_DATE', '/^(19|20)[0-9]{2}[\/-][01][0-9][\/-][0-3][0-9]$/');
define('RE_TIME', '/^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/');
define('RE_TS', '/^(19|20)[0-9]{2}[\/-][01][0-9][\/-][0-3][0-9] ([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/');
define('RE_PHONE', '/^[0-9-]+$/');
define('RE_IDNO', '/^[A-Z][0-9]{9}$/');
define('RE_INT', '/^(-?[1-9]|0)[0-9]*$/');
define('RE_NUM', '/^[1-9][0-9]*$/');
define('RE_NUMSTR', '/^[0-9]+$/');
define('RE_01', '/^(0|1)$/');
define('RE_STR', '/.+/');
/**
* 定義意外
*/
class FatalException extends Exception {
public function __construct($message = 'Fatal', $error = -99)
{
parent::__construct($message, $error);
file_put_contents(LOG_PATH . '/fatal.log', date('Y-m-d H:i:s') . "\t$error\t$message\n", FILE_APPEND);
}
}
class SQLException extends Exception {
public function __construct($message = 'SQL', $error = -99)
{
parent::__construct($message, $error);
file_put_contents(LOG_PATH . '/sql.log', date('Y-m-d H:i:s') . "\t$error\t$message\n", FILE_APPEND);
}
}
class NetworkException extends Exception
{
protected $msg;
public function __construct($error = -99, $message = '系統忙碌中請稍後再試')
{
$this->msg = $message;
parent::__construct($message, $error);
file_put_contents(LOG_PATH . '/network.log', date('Y-m-d H:i:s') . "\t$error\t$message\n", FILE_APPEND);
}
public function getMsg()
{
return $this->msg;
}
}
class RequestException extends Exception
{
protected $msg;
public function __construct($error = -99, $message = '系統忙碌中請稍後再試')
{
$this->msg = $message;
parent::__construct($message, -99);
file_put_contents(LOG_PATH . '/req.log', date('Y-m-d H:i:s') . "\t$error\t$message\n", FILE_APPEND);
}
public function getMsg()
{
return $this->msg;
}
}
class ActionException extends Exception
{
protected $msg;
public function __construct($error = -99, $message = '系統忙碌中請稍後再試')
{
$this->msg = $message;
parent::__construct($message, $error);
file_put_contents(LOG_PATH . '/act.log', date('Y-m-d H:i:s') . "\t$error\t$message\n", FILE_APPEND);
}
public function getMsg()
{
return $this->msg;
}
}
class AlertException extends Exception
{
protected $msg;
public function __construct($error = -99, $message = '系統忙碌中請稍後再試')
{
$this->msg = $message;
parent::__construct($message, $error);
file_put_contents(LOG_PATH . 'alert.log', date('Y-m-d H:i:s') . "\t$error\t$message\n", FILE_APPEND);
sendMimeMail('david@richi.com,sam@richi.com,lavenderchang@richi.com,kc@richi.com,regretless@richi.com,yenkuan@richi.com,kaede@richi.com,beiyi@richi.com,jamie.lin@richi.com,powerlilian@richi.com', '紅色警戒!有商品沒送出!!!', $message, $message, 'Richi Inc. <email@richimail.com>');
}
public function getMsg()
{
return $this->msg;
}
}
//從Array取出一個Array
function _ARR($arr, $name, $errno = null, $errmsg = null)
{
if (!is_array($arr)) {
if (isset($errno)) {
throw new ActionException($errno, $errmsg);
}
throw new RequestException(-99, serialize($arr) . ' is not an array');
}
if (!isset($arr[$name])) {
if (isset($errno)) {
throw new ActionException($errno, $errmsg);
}
throw new RequestException(-99, "Undefine variable $name");
}
if (is_array($arr[$name])) {
return $arr[$name];
}
if (isset($errno)) {
throw new ActionException($errno, $errmsg);
}
throw new RequestException(-99, "Pattern not matched $name $pattern");
}
//從Array取出一個值並檢查格式
function _VAL($arr, $name, $pattern = null, $errno = null, $errmsg = null)
{
if (!is_array($arr)) {
if (isset($errno)) {
throw new ActionException($errno, $errmsg);
}
throw new RequestException(-99, serialize($arr) . ' is not an array');
}
if (!isset($arr[$name])) {
if (is_null($pattern)) {
return '';
}
if (isset($errno)) {
throw new ActionException($errno, $errmsg);
}
throw new RequestException(-99, "Undefine variable $name");
}
if (is_null($pattern) || preg_match($pattern, $arr[$name])) {
return $arr[$name];
}
if (isset($errno)) {
throw new ActionException($errno, $errmsg);
}
throw new RequestException(-99, "Pattern not matched $name $pattern");
}
//從Array取出一系列值並檢查格式
function listVal($arr, $map, $errno = null, $errmsg = null)
{
if (!is_array($arr)) {
if (isset($errno)) {
throw new ActionException($errno, $errmsg);
}
throw new RequestException(-99, serialize($arr) . ' is not an array');
}
$out = array();
foreach ($map as $name => $pattern) {
if (!isset($arr[$name])) {
if (is_null($pattern)) {
$out[] = '';
continue;
}
if (isset($errno))
throw new ActionException($errno, $errmsg);
throw new RequestException(-99, "Undefine variable $name");
}
if (is_null($pattern) || preg_match($pattern, $arr[$name])) {
$out[] = $arr[$name];
continue;
}
if (isset($errno)) {
throw new ActionException($errno, $errmsg);
}
throw new RequestException(-99, "Pattern not matched $name $pattern");
}
return $out;
}
//從$_REQUEST取出值並驗證格式
function _REQ($name, $pattern = null, $errno = null, $errmsg = null)
{
if (!isset($_REQUEST[$name]))
{
if (!isset($pattern))
return '';
if (isset($errno))
throw new ActionException($errno, $errmsg);
throw new RequestException(-99, "Undefine variable $name");
}
if (!isset($pattern) || preg_match($pattern, $_REQUEST[$name]))
return $_REQUEST[$name];
if (isset($errno))
throw new ActionException($errno, $errmsg);
throw new RequestException(-99, "Pattern not matched $name $pattern");
}
//從$_GET取出值並驗證格式
function _GET($name, $pattern = null, $errno = null, $errmsg = null)
{
if (!isset($_GET[$name]))
{
if (!isset($pattern))
return '';
if (isset($errno))
throw new ActionException($errno, $errmsg);
throw new RequestException(-99, "Undefine variable $name");
}
if (!isset($pattern) || preg_match($pattern, $_GET[$name]))
return $_GET[$name];
if (isset($errno))
throw new ActionException($errno, $errmsg);
throw new RequestException(-99, "Pattern not matched $name $pattern");
}
//從$_POST取出值並驗證格式
function _POST($name, $pattern = null, $errno = null, $errmsg = null)
{
if (!isset($_POST[$name]))
{
if (!isset($pattern))
return '';
if (isset($errno))
throw new ActionException($errno, $errmsg);
throw new RequestException(-99, "Undefine variable $name");
}
if (!isset($pattern) || preg_match($pattern, $_POST[$name]))
return $_POST[$name];
if (isset($errno))
throw new ActionException($errno, $errmsg);
throw new RequestException(-99, "Pattern not matched $name $pattern");
}
//組成xml
function arr2xml($arr, $root = 'xml')
{
$xml = "<$root>";
foreach ($arr as $key => $value)
{
if (is_numeric($key))
$key = 'item';
if (is_array($value))
$xml .= arr2xml($value, $key);
else
$xml .= "<$key>" . htmlspecialchars($value) . "</$key>";
}
$xml .= "</$root>";
return $xml;
}
//取得XML中某個key的值
function xmlValue($xml, $name, $default = '')
{
if (preg_match("/<$name>([^<]+)</", $xml, $a))
return urldecode($a[1]);
return $default;
}
function mysql_begin()
{
mysql_query('BEGIN');
}
function mysql_commit()
{
mysql_query('COMMIT');
}
function mysql_rollback()
{
mysql_query('ROLLBACK');
}
//寄信
function sendMail($email, $title, $text, $from = 'Richi Inc. <email@richimail.com>')
{
$subject = u2pq($title);
$encoded = base64_encode($text);
$headers = "From: $from\nMIME-Version: 1.0\nContent-Type: text/plain; charset=\"utf-8\"\nContent-Transfer-Encoding: base64";
return mail($email, $subject, $encoded, $headers);
}
//寄信
function sendMimeMail($email, $title, $text, $html, $from = 'Richi Inc. <email@richimail.com>')
{
$subject = u2pq($title);
$html64 = base64_encode($html);
$message = chunk_split(base64_encode($text));
$boundary = "==Multipart_Boundary_" . md5(uniqid());
$boundary2 = "==Multipart_Boundary_" . md5(uniqid());
$headers = "From: $from\nMIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=\"{$boundary}\"";
$message = "This is a multi-part message in MIME format
--{$boundary}
Content-Type: multipart/alternative; boundary=\"{$boundary2}\"
--{$boundary2}
Content-Type: text/plain; charset=\"utf-8\"
Content-Transfer-Encoding: base64
{$message}
--{$boundary2}
Content-Type: text/html; charset=\"utf-8\"
Content-Transfer-Encoding: base64
{$html64}
--{$boundary2}--
--{$boundary}--
";
return mail($email, $subject, $message, $headers);
}
//MIME-Mail的文字編碼
function u2pq($ustr)
{
return "=?utf-8?B?" . base64_encode($ustr) . "?=";
}
function now()
{
return date('Y-m-d H:i:s');
}
function getSerialNo($length = 6, $chars = '3456789ABCDFGHJKLMNPQRSTVWXY')
{
$sn = '';
for ($i = 0; $i < $length; $i ++)
$sn .= substr($chars, rand(0, strlen($chars) - 1), 1);
return $sn;
}
function post($url, $post = "", $useragent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; zh-tw) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8J3")
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
"User-Agent: $useragent",
"Connection: keep-alive"
));
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
//get remote address
function getRemoteAddr()
{
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
return $_SERVER['HTTP_X_FORWARDED_FOR'];
if (isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))
return $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
return $_SERVER['REMOTE_ADDR'];
}
function jsonp($object)
{
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
if(isset($_GET['callback']))
echo $_GET['callback']."(";
echo json_encode($object);
if(isset($_GET['callback']))
echo ");";
exit;
}
/**
* 驗證權限
*
* 這個 function, 僅用來檢查權限,輸入格式為『陣列』或『JSON』,
* 例: 使用者權限設定為 $data = {'account':'1', 'debit':'5', 'topup':'7'}
* 某按鈕或頁面設定為 canDo($data,'account', 'a') ,輸出為false則該使用者無新增權限。
* canDo($data,'account', 'r'),輸出為ture,該使用者有讀取權限
*
* @author sam
*
* @param string $data - 傳遞資料
* @param string $key - 任意定義字串
* @param string $act - 檢查權限等級『 r:讀取(1), a:新增(3), e:編輯(4)』
*
* @return bool
*/
function canDo($data, $key, $act="")
{
//檢查資料格式是否為符和
if(!(is_string($data) || is_array($data))) {
return false;
}
//檢查是否為Array或JSON
if (!is_array($data)) {
$arr = @json_decode($data,true);
if (!is_array($arr)) {
return false;
}
}
else {
$arr = $data;
}
//檢查第一階段權限
if ($act === "") {
return isset($arr[$key]);
}
if (!isset($arr[$key])) {
return false;
}
//驗證細部權限
$permission = $arr[$key];
$chmod['r'] = ($permission%2 === 1);
$chmod['a'] = ($permission === 2 || $permission === 3 || $permission === 6 || $permission === 7);
$chmod['e'] = ($permission >= 4);
return isset($chmod[$act]) ? $chmod[$act] : false;
}
function render($tmpl_file, $context) {
require_once 'Mustache/Autoloader.php';
Mustache_Autoloader::register();
file_exists($tmpl_file) or die;
$tmpl = file_get_contents($tmpl_file);
$m = new Mustache_Engine;
return $m->render($tmpl, $context);
}