-
Notifications
You must be signed in to change notification settings - Fork 25
/
Functions.php
executable file
·1231 lines (1160 loc) · 36.1 KB
/
Functions.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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// +----------------------------------------------------------------------
// | Buddy Framework
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://buddy.woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <[email protected]>
// +----------------------------------------------------------------------
/**
* get configuration fields
* @param string $name paramname
* @param mixed $value param value
*/
function C($name = null, $value = null) {
static $_config = array();
//if empty get all
if (empty($name)) {
return $_config;
}
//set value first
if (is_string($name)) {
if (!strpos($name, '.')) {
$name = strtolower($name);
if (is_null($value)) {
return isset($_config[$name]) ? $_config[$name] : null;
}
$_config[$name] = $value;
return;
}
//get and set array
$name = explode('.', $name);
$name[0] = strtolower($name[0]);
if (is_null($value)) {
return isset($_config[$name[0]][$name[1]]) ? $_config[$name[0]][$name[1]] : null;
}
$_config[$name[0]][$name[1]] = $value;
return;
}
//array set
if (is_array($name)) {
return $_config = array_merge($_config, array_change_key_case($name));
}
return null; //return null if get the no exist param name
}
/**
* 设置和获取统计数据
* @param string $key 统计的key
* @param int $step 增加多少
*/
function N($key, $step = 0) {
static $_num = array();
if (!isset($_num[$key])) {
$_num[$key] = 0;
}
if (empty($step)) {
return $_num[$key];
} else {
$_num[$key] = $_num[$key] + (int) $step;
}
}
/**
* 统计时间方法
* @param string $start 开始时间
* @param string $end
* @param int $dec
*/
function G($start, $end = '', $dec = 3) {
static $_info = array();
if (!empty($end)) { // 统计时间
if (!isset($_info[$end])) {
$_info[$end] = microtime(TRUE);
}
return number_format(($_info[$end] - $_info[$start]), $dec);
} else { // 记录时间
$_info[$start] = microtime(TRUE);
}
}
/**
* set Language
*
* @param $name string string key
* @param $value string string value
*/
function L($name = null, $value = null) {
static $_lang = array();
if (empty($name)) {
return $_lang;
}
if (is_string($name)) {
$name = strtolower($name);
if (is_null($value)) {
return isset($_lang[$name]) ? $_lang[$name] : $name;
}
$_lang[$name] = $value;
return;
}
if (is_array($name)) {
$_lang = array_merge($_lang, array_change_key_case($name, CASE_LOWER));
}
return;
}
/**
* U function
*
* @param $url string 'index/index'
* @param $params array params
* @param $redirect bool redirect now
*/
function U($url, $params = array(), $redirect = false) {
$depr = DIRECTORY_SEPARATOR;
$isRoute = false;
if (count($params)) {
$path = $url . $depr;
foreach ($params as $key => $value) {
$path .= $key . $depr . $value . $depr;
}
//if is web pub mode then do url route
if (PUB_MODE == 'WEB') {
$routes = C(APP_NAME . '_route_rules');
if (!empty($routes)) {
$paths = explode($depr, $path);
foreach ($routes as $key => $route) {
if (ucfirst($paths[0]) == $route[1] && $paths[1] == $route[2]) {
$path = $depr . $paths[0] . $depr . $paths[3];
$isRoute = true;
break;
}
}
}
}
} else {
$path = $url;
}
if (!$isRoute) {
$path = $depr . $path;
}
if ($redirect) {
redirect($path);
} else {
return $path;
}
}
/**
* redirect to url
* @param string $url string url
* @param int $time int delay time
* @param string $msg show jump message
*/
function redirect($url, $time = 0, $msg = '') {
$url = str_replace(array("\n", "\r"), '', $url);
if (!headers_sent()) {
if (0 === $time) {
header("Location: " . $url);
} else {
header("refresh:{$time};url={$url}");
echo($msg);
}
exit();
} else {
$str = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
if ($time != 0) {
$str .= $msg;
}
exit($str);
}
}
/**
* get action
* get from Action
* @param string $name actionname
*/
function getaction($name) {
$name = ucwords($name);
static $_action = array();
if (isset($_action[$name])) {
return $_action[$name];
}
$cName = $name . 'Action';
$className = ACTION_PATH . DIRECTORY_SEPARATOR . $name . 'Action.php';
require_cache($className);
if (class_exists($cName)) {
$action = new $cName();
$_action[$name] = $action;
return $action;
} else {
//log the error action name
if (class_exists('EmptyAction')) {
$action = new EmptyAction();
$_action[$name] = $action;
return $action;
}
logNotice(__FILE__ . ':' . __FUNCTION__ . ": $nameAction not find!");
}
}
/**
* runaction
* auto new action class then run the action
* @param string $module modulename
* @param string $action actionname
*/
function runaction($module, $action) {
$class = getaction($module);
if ($class) {
if (!method_exists($class, $action)) {
$action = '_empty';
}
return call_user_func(array(&$class, $action));
} else {
return false;
}
}
/**
* Require file optimized
* loadfile
* @param string $filename filename of class
*/
function require_cache($filename) {
static $_importFiles = array();
$filename = realpath($filename);
if (!isset($_importFiles[$filename])) {
if (is_file($filename)) {
require $filename;
$_importFiles[$filename] = true;
} else {
$_importFiles[$filename] = false;
}
}
return $_importFiles[$filename];
}
/**
* mkdirs
* @param array $dirs
* @param int $mode
*/
function mkdirs($dirs, $mode = 0777) {
foreach ($dirs as $dir) {
if (!is_dir($dir))
mkdir($dir, $mode);
}
}
/**
* throw exception
* @todo Exception deal
* @param string $msg message
* @param string $type type
* @param string $code code
*/
function throw_exception($msg, $type = 'Exception', $code = 888) {
logFATAL($msg);
throw new Exception($msg, $code);
//exit($msg);
}
//----------Need to remove--------------------------------------------------------------------
/**
* record the count of the act cache
* @param object $c cache instance
* @param string $key cache key
*/
function countcacheact($c, $key) {
$key.='_c';
if ($c->get($key)) {
if ($c->increment($key, 1) !== false) {
return true;
}
return false;
} else {
if ($c->set($key, 1)) {
return true;
}
return false;
}
}
/**
* create table
* @param mysqli $db mysql instance
* @param string $tablename tablename
*/
function createtable($db, $tablename, $day = 1) {
$tomorrow = date("Ymd", time() + $day * 24 * 3600);
$sql = "create table {$tablename}_{$tomorrow} like {$tablename};";
if (false === $db->query($sql)) {
logFatal('CREATE TABLE ERROR SQL: %s', $sql . ' ' . $db->error);
return false;
}
return true;
}
/**
* send email function
* @TODO need to update this mail
* @param string $title
* @param string $body
* @param mixed $address
* @param int $usleep
*/
function sendMail($title, $body, $address, $usleep = 0, $c = 'GBK') {
$c = C('mailserver');
if (isset($c['smtpserver']) && isset($c['fromemail']) && isset($c['fromname'])) {
$mail = new PHPMailer(TRUE);
$mail->CharSet = $c;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Port = 25;
$mail->Host = $c['smtpserver'];
$mail->From = $c['fromemail'];
$mail->FromName = mb_convert_encoding($c['fromname'], 'GBK', 'UTF-8');
$mail->IsHTML(true);
$mail->Subject = $title;
$mail->MsgHTML($body);
if (is_array($address) && count($address)) {
//TODO:need to be change
foreach ($address as $one) {
$mail->AddAddress($one);
$r = $mail->Send();
if ($r === false) {
UB_LOG_NOTICE('SEND ERROR:%s', $address);
}
if ($usleep > 0) {
usleep($usleep);
}
}
} else {
$mail->AddAddress($address);
$r = $mail->Send();
if ($r === false) {
UB_LOG_NOTICE('SEND ERROR:%s', $address);
}
}
}
}
//----------Need to remove--------------------------------------------------------------------
/**
* debug start
* show time and memory use in this progress
* @param $label debuglable
*/
function debug_start($label = '') {
$GLOBALS[$label]['_beginTime'] = microtime(TRUE);
$GLOBALS[$label]['_beginMem'] = memory_get_usage();
}
/**
* end of the debug start
* debug end and show the time being and memory used
* @param $label debug lable
*/
function debug_end($label = '') {
$GLOBALS[$label]['_endTime'] = microtime(TRUE);
echo 'Process ' . $label . ': Times ' . number_format($GLOBALS[$label]['_endTime'] - $GLOBALS[$label]['_beginTime'], 6) . "s \n";
$GLOBALS[$label]['_endMem'] = memory_get_usage();
echo 'Memories ' . number_format(($GLOBALS[$label]['_endMem'] - $GLOBALS[$label]['_beginMem']) / 1024) . " k \n";
}
/**
* bigid generator
* if u haven't use the autoincrease id ,then use objid() to genearte a unique id.
*/
function objid($length = 1) {
$ivan_len = $length;
$time = explode(' ', microtime());
$id = $time[1] . sprintf('%06u', substr($time[0], 2, 6));
if ($ivan_len > 0) {
$id .= substr(sprintf('%010u', mt_rand()), 0, $ivan_len);
}
return $id;
}
/**
* get instance of a class
* get instance from a class name then cache it
* @param $name class name
* @param $method method of class
* @param $args args in the method
*/
function model_get_instance_of($name, $method = '', $args = array()) {
static $_instance = array();
$identify = empty($args) ? $name . $method : $name . $method . model_to_guid_string($args);
if (!isset($_instance[$identify])) {
if (class_exists($name)) {
$o = new $name();
if (method_exists($o, $method)) {
if (!empty($args)) {
$_instance[$identify] = call_user_func_array(array(&$o, $method), $args);
} else {
$_instance[$identify] = $o->$method();
}
} else {
$_instance[$identify] = $o;
}
} else {
throw_exception('CLASS_NOT_EXIST:' . $name);
}
}
return $_instance[$identify];
}
/**
* build guid
* @param string $mix guid md5
*/
function model_to_guid_string($mix) {
if (is_object($mix) && function_exists('spl_object_hash')) {
return spl_object_hash($mix);
} elseif (is_resource($mix)) {
$mix = get_resource_type($mix) . strval($mix);
} else {
$mix = serialize($mix);
}
return md5($mix);
}
/**
* simple exception setting
* show exception information use echo()
* TODO: need extend
* @param $msg throw message
* @param $type exception type
* @param $code exception code
*/
function model_throw_exception($msg, $type = 'Exception', $code = 888) {
throw_exception($msg, $type, $code);
}
/**
* String name style format
* type
* =0 change java to c
* =1 change c to java
* @param string $name string
* @param integer $type change type
* @return string
*/
function model_parse_name($name, $type = 0) {
if ($type) {
return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
} else {
$name = preg_replace("/[A-Z]/", "_\\0", $name);
return strtolower(trim($name, "_"));
}
}
/**
* Cookie set/get/clear
* 1 cookie: cookie('name')
* 2 clear cookie: cookie(null)
* 3 del prifix cookie: cookie(null,'think_') | prefix
* 4 set cookie: cookie('name','value') | savetime: cookie('name','value',array('expire'=>36000))
* 5 del cookie: cookie('name',null)
* $option prefix,expire,path,domain
* cookie('name','value',array('expire'=>1,'prefix'=>'think_'))
* cookie('name','value','prefix=tp_&expire=10000')
*/
function cookie($name, $value = '', $option = null) {
$config = array(
'prefix' => C('COOKIE_PREFIX'),
'expire' => C('COOKIE_EXPIRE'),
'path' => C('COOKIE_PATH'),
'domain' => C('COOKIE_DOMAIN'),
);
if (!empty($option)) {
if (is_numeric($option)) {
$option = array('expire' => $option);
} elseif (is_string($option)) {
parse_str($option, $option);
}
$config = array_merge($config, array_change_key_case($option));
}
if (is_null($name)) {
if (empty($_COOKIE)) {
return;
}
$prefix = empty($value) ? $config['prefix'] : $value;
if (!empty($prefix)) {
foreach ($_COOKIE as $key => $val) {
if (0 === stripos($key, $prefix)) {
setcookie($key, '', time() - 3600, $config['path'], $config['domain']);
unset($_COOKIE[$key]);
}
}
}
return;
}
$name = $config['prefix'] . $name;
if ('' === $value) {
return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;
} else {
if (is_null($value)) {
setcookie($name, '', time() - 3600, $config['path'], $config['domain']);
unset($_COOKIE[$name]);
} else {
$expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
setcookie($name, $value, $expire, $config['path'], $config['domain']);
$_COOKIE[$name] = $value;
}
}
}
/**
* xml encode
* array to xml
* @param $data array data
* @param $encoding string
* @param $root string
*/
function xml_encode($data, $encoding = 'utf-8', $root = "buddy") {
$xml = '<?xml version="1.0" encoding="' . $encoding . '"?>';
$xml.= '<' . $root . '>';
$xml.= data_to_xml($data);
$xml.= '</' . $root . '>';
return $xml;
}
function data_to_xml($data) {
if (is_object($data)) {
$data = get_object_vars($data);
}
$xml = '';
foreach ($data as $key => $val) {
is_numeric($key) && $key = "item id=\"$key\"";
$xml.="<$key>";
$xml.= ( is_array($val) || is_object($val)) ? data_to_xml($val) : $val;
list($key, ) = explode(' ', $key);
$xml.="</$key>";
}
return $xml;
}
/**
* getip
* get client ip
*/
function getip() {
//@todo: 开发后移除
return '123.112.64.9';
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
$ip = getenv("REMOTE_ADDR");
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
$ip = $_SERVER['REMOTE_ADDR'];
else
$ip = "unknown";
return($ip);
}
/**
* msub string
* @param string $str
* @param int $start
* @param int $length
* @param string $charset
* @param bool $suffix
*/
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) {
if (function_exists("mb_substr")) {
return mb_substr($str, $start, $length, $charset);
} elseif (function_exists('iconv_substr')) {
return iconv_substr($str, $start, $length, $charset);
}
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("", array_slice($match[0], $start, $length));
if ($suffix)
return $slice . "…";
return $slice;
}
/**
* add params to url
* @param string $strUrl
* @param array $arrParamsToAdd array('key'=>'value','key1'=>'value1')
*/
function addParamToUrl($strUrl, $arrParamsToAdd = array()) {
$strQuery = substr(strstr($strUrl, "?"), 1); // string or false
$arrQueryParams = array();
if ($strQuery) {
$strFragment = strstr($strQuery, "#"); // string or false
$strQuery = $strFragment ? substr($strQuery, 0, strlen($strQuery) - strlen($strFragment)) : $strQuery; // delete the fragment from the query
$arrUrlParse["query"] = $strQuery;
parse_str($strQuery, $arrQueryParams);
}
$arrUrlParse = parse_url($strUrl);
if (empty($arrUrlParse["path"]) && empty($strQuery)) {
$arrUrlParse["path"] = '/';
}
$arrQueryParams = array_merge($arrQueryParams, $arrParamsToAdd);
$arrUrlParse['query'] = http_build_query($arrQueryParams);
$url = (isset($arrUrlParse["scheme"]) ? $arrUrlParse["scheme"] . "://" : "") .
(isset($arrUrlParse["user"]) ? $arrUrlParse["user"] . ":" : "") .
(isset($arrUrlParse["pass"]) ? $arrUrlParse["pass"] . "@" : "") .
(isset($arrUrlParse["host"]) ? $arrUrlParse["host"] : "") .
(isset($arrUrlParse["port"]) ? ":" . $arrUrlParse["port"] : "") .
(isset($arrUrlParse["path"]) ? $arrUrlParse["path"] : "") .
(isset($arrUrlParse["query"]) ? "?" . $arrUrlParse["query"] : "") .
(isset($arrUrlParse["fragment"]) ? "#" . $arrUrlParse["fragment"] : "");
return $url;
}
/**
* get root domain
* @param $url url
*/
function getDomain($url) {
$r = parse_url($url);
$domain = $r['host'];
$domainarr = explode('.', $domain);
$count = count($domainarr);
return $domainarr[$count - 2] . '.' . $domainarr[$count - 1];
}
/**
* initLog
* to log log @todo if u will add the filed of log then add it
*/
function initLog() {
//'logid','ip','uid','traceid','method','uri'
//@TODO LOG RECORD IT THE ALL OF
$config = C('log.' . APP_NAME);
if (!empty($config)) {
if (isset($config['info']['ip'])) {
$config['info']['ip'] = getip();
}
if (isset($config['info']['uid'])) {
$config['info']['uid'] = userID();
}
if (isset($config['info']['traceid'])) {
$config['info']['traceid'] = traceID();
}
logInit($config['dir'], $config['file'], $config['level'], $config['info'], $config['flush']);
}
}
/**
* string encode
* @param string $txt string field
* @param string $key encrykey
*/
function strEncode($txt, $key = 'woshimaijiayxq') {
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$nh = rand(0, 64);
$ch = $chars[$nh];
$mdKey = md5($key . $ch);
$mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
$txt = base64_encode($txt);
$tmp = '';
$i = 0;
$j = 0;
$k = 0;
for ($i = 0; $i < strlen($txt); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = ($nh + strpos($chars, $txt[$i]) + ord($mdKey[$k++])) % 64;
$tmp .= $chars[$j];
}
return $ch . $tmp;
}
/**
* string encode
* @param string $txt string field
* @param string $key encrykey
*/
function strDecode($txt, $key = 'woshimaijiayxq') {
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
$ch = $txt[0];
$nh = strpos($chars, $ch);
$mdKey = md5($key . $ch);
$mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
$txt = substr($txt, 1);
$tmp = '';
$i = 0;
$j = 0;
$k = 0;
for ($i = 0; $i < strlen($txt); $i++) {
$k = $k == strlen($mdKey) ? 0 : $k;
$j = strpos($chars, $txt[$i]) - $nh - ord($mdKey[$k++]);
while ($j < 0)
$j+=64;
$tmp .= $chars[$j];
}
return base64_decode($tmp);
}
/**
* get key value from array with key
* @param string $array
* @param unknown_type $idfield
*/
function strin($array, $idfield = 'id', $isstring = false) {
$str = '';
$left = '(';
$right = ')';
if (is_array($array)) {
$str = $isstring ? $str : $str . $left;
foreach ($array as $value) {
if (is_array($value) && isset($value[$idfield])) {
$str .= "'" . $value[$idfield] . "',";
} else {
$str .= "'" . $value . "',";
}
}
return $isstring ? substr($str, 0, -1) : substr($str, 0, -1) . $right;
} else {
return $isstring ? $array : "('$array')";
}
}
//arr to one
function arrToOne($array, $field = 'id') {
$arr = array();
if (is_array($array)) {
foreach ($array as $one) {
if (isset($one[$field])) {
$arr[] = $one[$field];
} elseif (is_string($field)) {
$arr[] = $one;
}
}
}
return $arr;
}
/**
* get item of dataset filed to array
* @param array $array array of dataset
* @param string $idfield
*/
function datasetToArray($array, $idfield = 'id') {
$arr = array();
if (!empty($array)) {
foreach ($array as $value) {
if (isset($value[$idfield])) {
$arr[] = $value[$idfield];
}
}
}
return $arr;
}
/**
* Jump to other url
* @param string $strUrl
*/
function jump($url) {
$url = reMoveXss($url);
//TODO:可以进行扩展
if (!empty($url)) {
header("Location:{$url}");
}
exit();
}
/**
* js go to url function
* @param unknown_type $strUrl
*/
function jsGotoUrl($url) {
$url = reMoveXss($url);
$strHtml = <<<EOF
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="ReFresh" content="0; url={$url}" />
<title>正在处理...</title>
</head>
<body>
<script>
var url="{$url}";
location.href=url;
</script>
</body>
</html>
EOF;
echo $strHtml;
exit();
}
/**
* remove xss code of the string
* @param string $val
*/
function reMoveXss($val) {
$patterns = array(
'<',
'>',
'"',
"'",
);
$replacements = array(
'<',
'>',
'"',
''',
);
$val = str_replace($patterns, $replacements, $val);
return $val;
}
/**
* get all files of dir
* @param string $dir dir path
*/
function allFiles($dir) {
$files = array();
if (is_file($dir)) {
return $dir;
}
$handle = opendir($dir);
if ($handle) {
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$filename = $dir . "/" . $file;
if (is_file($filename)) {
$files[] = $filename;
} else {
$files = array_merge($files, allfile($filename));
}
}
} // end while
closedir($handle);
}
return $files;
}
/**
* generate content to array
* @param string $file file
*/
function getFileContentToArray($file) {
$array = array();
if (is_file($file)) {
$file_handle = fopen($file, "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
$array[] = trim($line);
}
fclose($file_handle);
}
return $array;
}
function posix_getpid_new() {
return DIRECTORY_SEPARATOR == '/' ? posix_getpid() : '8888';
}
/**
* friendly var_dump for explorer
* @param mixed $var var
* @param bool $echo
* @param string $label
* @param bool $strict
*/
function dump($var, $echo = true, $label = null, $strict = true) {
$label = ($label === null) ? '' : rtrim($label) . ' ';
if (!$strict) {
if (ini_get('html_errors')) {
$output = print_r($var, true);
$output = "<pre>" . $label . htmlspecialchars($output, ENT_QUOTES) . "</pre>";
} else {
$output = $label . print_r($var, true);
}
} else {
ob_start();
var_dump($var);
$output = ob_get_clean();
if (!extension_loaded('xdebug')) {
$output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
$output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
}
}
if ($echo) {
echo($output);
return null;
}else
return $output;
}
/**
* count time used
* @param string $key key of the lable
* @param int $step step
* @throws Exception
*/
function T($key, $step = 0) {
static $_num = array();
if (!isset($_num[$key])) {
$_num[$key] = 0;
}
if (empty($step))
return $_num[$key];
else
$_num[$key] = $_num[$key] + (int) $step;
}
/**
* merge ids from 2 array
* default length is 500
* @param array $ids1 array 1
* @param array $ids2 array 2
* @param int $length length of array
*/
function mergeIds($ids1, $ids2, $length = 500) {
if (is_array($ids1) && is_array($ids2)) {
$arr_new = array_merge($ids1, $ids2);
rsort($arr_new);
return implode(',', array_slice($arr_new, 0, $length));
} else {
return false;
}
}
/**
* join id to head
* join id to head with string
* @param bigint $id id
* @param string $ids id sperate by ,
* @param int $length length of the list
*/
function joinIdToHead($id, $ids, $length = 500) {
$ids_arr = array_slice(explode(",", $ids), 0, $length - 1);
return rtrim($id . ',' . implode(',', $ids_arr), ',');
}
/**
* remove id from ids string
* @param bigint $id
* @param unknown_type $ids
*/
function removeIdFromIds($id, $ids) {
$ids = str_replace(',' . strval($id) . ',', ',', ',' . $ids . ',');
return trim($ids, ',');
}
/**
* get next all ids to array
* @param string $ids ids seperate by ,
* @param bigint $id big int of id
* @throws Exception
*/
function subNextAllIdsToArray($ids, $id) {
$ids = ',' . $ids . ',';
$pos = strpos($ids, ',' . strval($id) . ',');
if ($pos) {
$new_ids = substr($ids, 0, $pos);
} else {
$new_ids = $ids;
}
if ($new_ids == '') {
return array();
} else {
return explode(",", trim($new_ids, ','));
}
}
/**
* get length of ids form ids by next id to array
* @param string $ids ids
* @param bigint $id id
* @param int $start from
* @param int $limit limit
*/
function subNextIdsToArray($ids, $id, $start = 0, $length = 50) {
$res_ids = subNextAllIdsToArray($ids, $id);
if (count($res_ids) > $length) {
$res_ids = array_slice($res_ids, $start, $length);
}
return $res_ids;