forked from snail007/microphp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MicroPHP.php
9842 lines (9508 loc) · 279 KB
/
MicroPHP.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
/*
* Copyright 2015 狂奔的蜗牛.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* MicroPHP
*
* An open source application development framework for PHP 5.2.0 or newer
*
* @package MicroPHP
* @author 狂奔的蜗牛
* @email 672308444@163.com
* @copyright Copyright (c) 2013 - 2015, 狂奔的蜗牛, Inc.
* @link http://git.oschina.net/snail/microphp
* @since Version 2.3.2
* @createdtime 2015-05-15 15:49:19
*/
if (!function_exists('args')) {
function args($key = null) {
return MpInput::parameters($key);
}
}
if (!function_exists('xss_clean')) {
function xss_clean($val) {
return MpInput::xss_clean($val);
}
}
foreach (array('set_cookie'=>'setCookie', 'set_cookie_raw'=>'setCookieRaw') as $func=>$true) {
if (!function_exists($func)) {
eval('function ' . $func . '($key, $value, $life = null, $path = "/", $domian = null, $http_only = false) {
return MpLoader::' . $true . '($key, $value, $life, $path, $domian, $http_only);
}');
}
}
foreach (array('server', 'session') as $func) {
if (!function_exists($func)) {
eval('function ' . $func . '($key = null, $default = null) {
return MpInput::' . $func . '($key, $default);
}');
}
}
foreach (array('get_rule', 'post_rule', 'get_post_rule', 'post_get_rule') as $func) {
if (!function_exists($func)) {
eval('function ' . $func . '($rule, $key, $default = null) {
return MpInput::' . $func . '($rule, $key, $default);
}');
}
}
foreach (array('get', 'post', 'cookie', 'cookie_raw', 'get_post', 'post_get') as $func) {
if (!function_exists($func)) {
if ($func == 'cookie_raw') {
$func = 'cookiRaw';
}
eval('function ' . $func . '($key = null, $default = null, $xss_clean = false) {
return MpInput::' . $func . '($key, $default, $xss_clean);
}');
}
}
foreach (array('get_int', 'post_int', 'get_post_int', 'post_get_int',
'get_time', 'post_time', 'get_post_time', 'post_get_time',
'get_date', 'post_date', 'get_post_date', 'post_get_date',
'get_datetime', 'post_datetime', 'get_post_datetime', 'post_get_datetime') as $func) {
if (!function_exists($func)) {
eval('function ' . $func . '($key, $min = null, $max = null, $default = null) {
return MpInput::' . $func . '($key, $min, $max, $default);
}');
}
}
if (!function_exists('dump')) {
/**
* 打印变量内容,参数和var_dump一样
* @param type $arg
* @param type $_
*/
function dump($arg, $_ = null) {
$args = func_get_args();
if (MpInput::isCli()) {
call_user_func_array('var_dump', $args);
} else {
echo '<pre>';
call_user_func_array('var_dump', $args);
echo '</pre>';
}
}
}
if (!function_exists('table')) {
/**
* 实例化一个表模型
* @param string $table_name 不带表前缀的表名称
* @param CI_DB_active_record $db 使用的数据库连接对象,默认留空是当前数据库连接
* @return MpTableModel
*/
function table($table_name, $db = null) {
return MpTableModel::M($table_name, $db);
}
}
if (!function_exists('url')) {
/**
* 生成url链接<br>
* 使用示例:<br>
* url(),<br>
* url('welcome.index'),<br>
* url('welcome.index','aa','bb'),<br>
* url('welcome.index',array('a'=>'bb','b'=>'ccc'),'dd','ee'),<br>
* url('welcome.index','dd','ee',array('a'=>'bb')),<br>
* url('welcome.index',array('a'=>'bb','b'=>'ccc')),<br>
* url('','aa','bb'),<br>
* url('',array('a'=>'bb','b'=>'ccc'),'dd','ee'),<br>
* url('',array('a'=>'bb','b'=>'ccc')),<br>
* 另外可以在第一个参数开始加上:<br>
* #和?用来控制url中显示入口文件名称和使用相对路经<br>
* 默认不显示入口文件名称,使用绝对路经<br>
* 使用示例:<br>
* url('#welcome.index'),<br>
* url('?welcome.index'),<br>
* url('#?welcome.index'),<br>
* url('?#welcome.index'),<br>
* @return string
*/
function url() {
$action = null;
$argc = func_num_args();
if ($argc > 0) {
$action = func_get_arg(0);
}
$args = array();
$get_str_arr = array();
if ($argc > 1) {
for ($i = 1; $i < $argc; $i++) {
if (is_array($arg = func_get_arg($i))) {
foreach ($arg as $k => $v) {
$get_str_arr[] = $k . '=' . urlencode($v);
}
} else {
$args[] = $arg;
}
}
}
if (!systemInfo('url_rewrite')) {
$self_name = stripos($action, '#') === 0 || stripos($action, '#') === 1 ? pathinfo(MpInput::server('php_self'), PATHINFO_BASENAME) : '';
$app_start = '?';
$get_start = '&';
} else {
$self_name = '';
$app_start = '';
$get_start = '?';
}
//是否使用相对路经检查
$path = (stripos($action, '?') === 0 || stripos($action, '?') === 1 ? '' : urlPath() . '/' );
$action = ltrim($action, '#?');
$url_app = $path . $self_name .
(empty($args) && empty($get_str_arr) && empty($action) ? '' : $app_start) .
($action . (empty($args) || empty($action) ? '' : '/' ) . implode('/', $args)) .
(empty($get_str_arr) ? '' : $get_start . implode('&', $get_str_arr));
return str_replace('?&', '?', $url_app);
}
}
if (!function_exists('urlPath')) {
/**
* 获取入口文件所在目录url路径。
* 只能在web访问时使用,在命令行下面会抛出异常。
* @param type $subpath 子路径或者文件路径,如果非空就会被附加在入口文件所在目录的后面
* @return type
* @throws Exception
*/
function urlPath($subpath = null) {
if (MpInput::isCli()) {
throw new Exception('function urlPath() can not be used in cli mode');
} else {
$old_path = getcwd();
$root = str_replace(array("/", "\\"), '/', MpInput::server('DOCUMENT_ROOT'));
chdir($root);
$root = getcwd();
$root = str_replace(array("/", "\\"), '/', $root);
chdir($old_path);
$path = path($subpath);
return str_replace($root, '', $path);
}
}
}
if (!function_exists('path')) {
/**
* 获取入口文件所在目录绝对路径。
* @param type $subpath 子路径或者文件路径,如果非空就会被附加在入口文件所在目录的绝对路径后面
* @return type
*/
function path($subpath = null) {
$path = str_replace(array("/", "\\"), '/', realpath('.') . ($subpath ? '/' . trim($subpath, '/\\') : ''));
return truepath($path);
}
}
/**
* 获取系统配置信息,也就是MpLoader::$system里面的信息
* @param type $key MpLoader::$system的键
* @return null
*/
if (!function_exists('systemInfo')) {
function systemInfo($key = NULL) {
if (is_null($key)) {
return MpLoader::$system;
} elseif (isset(MpLoader::$system[$key])) {
return MpLoader::$system[$key];
} else {
return null;
}
}
}
/**
* 获取系统数据库配置信息
* @param type $group 数据库组名称,即MpLoader::$system['db']的键.
* 为null时返回默认的配置组,即MpLoader::$system['db']['active_group']指定的组。
* @param type $key 配置组的键,指定了key可以获取指定组的键对应的值
* @return null
*/
if (!function_exists('dbInfo')) {
function dbInfo($group = NULL, $key = NULL) {
if (is_null($group)) {
$system= systemInfo();
$cfg = $system['db'][$system['db']['active_group']];
if (is_null($key)) {
return $cfg;
} else {
return isset($cfg[$key]) ? $cfg[$key] : null;
}
} elseif (isset($system['db'][$group])) {
$cfg = $system['db'][$group];
if (is_null($key)) {
return $cfg;
} else {
return isset($cfg[$key]) ? $cfg[$key] : null;
}
} else {
return null;
}
}
}
if (!function_exists('sessionStart')) {
function sessionStart() {
if (!isset($_SESSION)) {
session_start();
}
}
}
if (!function_exists('getInstance')) {
function &getInstance() {
return WoniuController::getInstance();
}
}
if (!function_exists('trigger404')) {
function trigger404($msg = '<h1>Not Found</h1>') {
$system = systemInfo();
if (!headers_sent()) {
header('HTTP/1.1 404 NotFound');
}
if (!empty($system['error_page_404']) && file_exists($system['error_page_404'])) {
include $system['error_page_404'];
} else {
echo $msg;
}
exit();
}
}
if (!function_exists('truepath')) {
/**
* This function is to replace PHP's extremely buggy realpath().
* @param string The original path, can be relative etc.
* @return string The resolved path, it might not exist.
*/
function truepath($path) {
//是linux系统么?
$unipath = PATH_SEPARATOR == ':';
//检测一下是否是相对路径,windows下面没有:,linux下面没有/开头
//如果是相对路径就加上当前工作目录前缀
if (strpos($path, ':') === false && strlen($path) && $path{0} != '/') {
$path = realpath('.') . DIRECTORY_SEPARATOR . $path;
}
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
$absolutes = array();
foreach ($parts as $part) {
if ('.' == $part)
continue;
if ('..' == $part) {
array_pop($absolutes);
} else {
$absolutes[] = $part;
}
}
//如果是linux这里会导致linux开头的/丢失
$path = implode(DIRECTORY_SEPARATOR, $absolutes);
//如果是linux,修复系统前缀
$path = $unipath ? (strlen($path) && $path{0} != '/' ? '/' . $path : $path) : $path;
//最后统一分隔符为/,windows兼容/
$path = str_replace(array('/', '\\'), '/', $path);
return $path;
}
}
if (!function_exists('convertPath')) {
function convertPath($path) {
return str_replace(array("\\", "/"), '/', $path);
}
}
if (!function_exists('trigger500')) {
function trigger500($msg = '<h1>Server Error</h1>') {
$system = systemInfo();
if (!headers_sent()) {
header('HTTP/1.1 500 Server Error');
}
if (!empty($system['error_page_50x']) && file_exists($system['error_page_50x'])) {
include $system['error_page_50x'];
} else {
echo $msg;
}
exit();
}
}
if (!function_exists('woniu_exception_handler')) {
function woniu_exception_handler($exception) {
$errno = $exception->getCode();
$errfile = pathinfo($exception->getFile(), PATHINFO_FILENAME);
$errline = $exception->getLine();
$errstr = $exception->getMessage();
$system =systemInfo();
if ($system['log_error']) {
$handle = $system['log_error_handle']['exception'];
if (!empty($handle)) {
if (is_array($handle)) {
$class = key($handle);
$method = $handle[$class];
$rclass_obj = new ReflectionClass($class);
$rclass_obj = $rclass_obj->newInstanceArgs();
if (method_exists($rclass_obj, $method)) {
$rclass_obj->{$method}($errno, $errstr, $errfile, $errline, get_strace());
}
} else {
if (function_exists($handle)) {
$handle($errno, $errstr, $errfile, $errline, get_strace());
}
}
}
}
if ($system['debug']) {
//@ob_clean();
trigger500('<pre>' . format_error($errno, $errstr, $errfile, $errline) . '</pre>');
}
exit;
}
}
if (!function_exists('woniu_error_handler')) {
/**
* 非致命错误处理函数。
* 该函数会接受所有类型的错误,应该过滤掉致命错误
* @param type $errno
* @param type $errstr
* @param type $errfile
* @param type $errline
* @return type
*/
function woniu_error_handler($errno, $errstr, $errfile, $errline) {
if (!error_reporting()) {
return;
}
$fatal_err = array(E_ERROR, E_USER_ERROR, E_COMPILE_ERROR, E_CORE_ERROR, E_PARSE, E_RECOVERABLE_ERROR);
if (in_array($errno, $fatal_err)) {
return true;
}
$system =systemInfo();
if ($system['log_error']) {
$handle = $system['log_error_handle']['error'];
if (!empty($handle)) {
if (is_array($handle)) {
$class = key($handle);
$method = $handle[$class];
$rclass_obj = new ReflectionClass($class);
$rclass_obj = $rclass_obj->newInstanceArgs();
if (method_exists($rclass_obj, $method)) {
$rclass_obj->{$method}($errno, $errstr, $errfile, $errline, get_strace());
}
} else {
if (function_exists($handle)) {
$handle($errno, $errstr, $errfile, $errline, get_strace());
}
}
}
}
if ($system['debug']) {
//@ob_clean();
echo '<pre>' . format_error($errno, $errstr, $errfile, $errline) . '</pre>';
}
}
}
if (!function_exists('woniu_fatal_handler')) {
/**
* 致命错误处理函数。
* 该函数会接受所有类型的错误,应该只处理致命错误
* @param type $errno
* @param type $errstr
* @param type $errfile
* @param type $errline
* @return type
*/
function woniu_fatal_handler() {
$system = systemInfo();
$errfile = "unknown file";
$errstr = "shutdown";
$errno = E_CORE_ERROR;
$errline = 0;
$error = error_get_last();
$fatal_err = array(E_ERROR, E_USER_ERROR, E_COMPILE_ERROR, E_CORE_ERROR, E_PARSE, E_RECOVERABLE_ERROR);
if ($error !== NULL && isset($error["type"]) && in_array($error["type"], $fatal_err)) {
$errno = $error["type"];
$errfile = $error["file"];
$errline = $error["line"];
$errstr = $error["message"];
if ($system['log_error']) {
$handle = $system['log_error_handle']['error'];
if (!empty($handle)) {
if (is_array($handle)) {
$class = key($handle);
$method = $handle[$class];
$rclass_obj = new ReflectionClass($class);
$rclass_obj = $rclass_obj->newInstanceArgs();
if (method_exists($rclass_obj, $method)) {
$rclass_obj->{$method}($errno, $errstr, $errfile, $errline, get_strace());
}
} else {
if (function_exists($handle)) {
$handle($errno, $errstr, $errfile, $errline, get_strace());
}
}
}
}
if ($system['debug']) {
//@ob_clean();
trigger500('<pre>' . format_error($errno, $errstr, $errfile, $errline) . '</pre>');
}
exit;
}
}
}
if (!function_exists('woniu_db_error_handler')) {
function woniu_db_error_handler($error) {
$msg = '';
if (is_array($error)) {
foreach ($error as $m) {
$msg.=$m . "\n";
}
} else {
$msg = $error;
}
$system = systemInfo();
$woniu_db = systemInfo('db');
if ($system['log_error']) {
$handle = $system['log_error_handle']['db_error'];
if (!empty($handle)) {
if (is_array($handle)) {
$class = key($handle);
$method = $handle[$class];
$rclass_obj = new ReflectionClass($class);
$rclass_obj = $rclass_obj->newInstanceArgs();
if (method_exists($rclass_obj, $method)) {
$rclass_obj->{$method}($msg, get_strace(TRUE));
}
} else {
if (function_exists($handle)) {
$handle($msg, get_strace(TRUE));
}
}
}
}
if ($woniu_db[$woniu_db['active_group']]['db_debug'] && $system['debug']) {
if (!empty($system['error_page_db']) && file_exists($system['error_page_db'])) {
include $system['error_page_db'];
} else {
echo '<pre>' . $msg . get_strace(TRUE) . '</pre>';
}
exit;
}
}
}
if (!function_exists('format_error')) {
function format_error($errno, $errstr, $errfile, $errline) {
$path = truepath(systemInfo('application_folder'));
$path.=empty($path) ? '' : '/';
$array_map = array('0' => 'EXCEPTION', '1' => 'ERROR', '2' => 'WARNING', '4' => 'PARSE', '8' => 'NOTICE', '16' => 'CORE_ERROR', '32' => 'CORE_WARNING', '64' => 'COMPILE_ERROR', '128' => 'COMPILE_WARNING', '256' => 'USER_ERROR', '512' => 'USER_WARNING', '1024' => 'USER_NOTICE', '2048' => 'STRICT', '4096' => 'RECOVERABLE_ERROR', '8192' => 'DEPRECATED', '16384' => 'USER_DEPRECATED');
$trace = get_strace();
$content = '';
$content .= "错误信息:" . nl2br($errstr) . "\n";
$content .= "出错文件:" . str_replace($path, '', $errfile) . "\n";
$content .= "出错行数:{$errline}\n";
$content .= "错误代码:{$errno}\n";
$content .= "错误类型:{$array_map[$errno]}\n";
if (!empty($trace)) {
$content .= "调用信息:{$trace}\n";
}
return $content;
}
}
if (!function_exists('get_strace')) {
function get_strace($is_db = false) {
$trace = debug_backtrace(false);
foreach ($trace as $t) {
if (!in_array($t['function'], array('display_error', 'woniu_db_error_handler', 'woniu_fatal_handler', 'woniu_error_handler', 'woniu_exception_handler'))) {
array_shift($trace);
} else {
array_shift($trace);
break;
}
}
if ($is_db) {
array_shift($trace);
}
array_pop($trace);
array_pop($trace);
$str = '';
$path = truepath(systemInfo('application_folder'));
$path.=empty($path) ? '' : '/';
foreach ($trace as $k => $e) {
$file = !empty($e['file']) ? "File:" . str_replace($path, '', $e['file']) . "\n" : '';
$line = !empty($e['line']) ? " Line:{$e['line']}\n" : '';
$space = (empty($file) && empty($line) ? '' : ' ');
$func = $space . (!empty($e['class']) ? "Function:{$e['class']}{$e['type']}{$e['function']}()\n" : "Function:{$e['function']}()\n");
$str.="\n#{$k} {$file}{$line}{$func}";
}
return $str;
}
}
if (!function_exists('stripslashes_all')) {
function stripslashes_all() {
if (!get_magic_quotes_gpc()) {
return;
}
$strip_list = array('_GET', '_POST', '_COOKIE');
foreach ($strip_list as $val) {
global $$val;
$$val = stripslashes2($$val);
}
}
}
if (!function_exists('stripslashes2')) {
#过滤魔法转义,参数可以是字符串或者数组,支持嵌套数组
function stripslashes2($var) {
if (!get_magic_quotes_gpc()) {
return $var;
}
if (is_array($var)) {
foreach ($var as $key => $val) {
if (is_array($val)) {
$var[$key] = stripslashes2($val);
} else {
$var[$key] = stripslashes($val);
}
}
} elseif (is_string($var)) {
$var = stripslashes($var);
}
return $var;
}
}
if (!function_exists('is_php')) {
function is_php($version = '5.0.0') {
static $_is_php;
$version = (string) $version;
if (!isset($_is_php[$version])) {
$_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
}
return $_is_php[$version];
}
}
if (!function_exists('forceDownload')) {
/**
* 强制下载
* 经过修改,支持中文名称
* Generates headers that force a download to happen
*
* @access public
* @param string filename
* @param mixed the data to be downloaded
* @return void
*/
function forceDownload($filename = '', $data = '') {
if ($filename == '' OR $data == '') {
return FALSE;
}
# Try to determine if the filename includes a file extension.
# We need it in order to set the MIME type
if (FALSE === strpos($filename, '.')) {
return FALSE;
}
# Grab the file extension
$x = explode('.', $filename);
$extension = end($x);
# Load the mime types
$mimes = array('hqx' => 'application/mac-binhex40', 'cpt' => 'application/mac-compactpro', 'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'), 'bin' => 'application/macbinary', 'dms' => 'application/octet-stream', 'lha' => 'application/octet-stream', 'lzh' => 'application/octet-stream', 'exe' => array('application/octet-stream', 'application/x-msdownload'), 'class' => 'application/octet-stream', 'psd' => 'application/x-photoshop', 'so' => 'application/octet-stream', 'sea' => 'application/octet-stream', 'dll' => 'application/octet-stream', 'oda' => 'application/oda', 'pdf' => array('application/pdf', 'application/x-download'), 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'ps' => 'application/postscript', 'smi' => 'application/smil', 'smil' => 'application/smil', 'mif' => 'application/vnd.mif', 'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'), 'ppt' => array('application/powerpoint', 'application/vnd.ms-powerpoint'), 'wbxml' => 'application/wbxml', 'wmlc' => 'application/wmlc', 'dcr' => 'application/x-director', 'dir' => 'application/x-director', 'dxr' => 'application/x-director', 'dvi' => 'application/x-dvi', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip', 'php' => 'application/x-httpd-php', 'php4' => 'application/x-httpd-php', 'php3' => 'application/x-httpd-php', 'phtml' => 'application/x-httpd-php', 'phps' => 'application/x-httpd-php-source', 'js' => 'application/x-javascript', 'swf' => 'application/x-shockwave-flash', 'sit' => 'application/x-stuffit', 'tar' => 'application/x-tar', 'tgz' => array('application/x-tar', 'application/x-gzip-compressed'), 'xhtml' => 'application/xhtml+xml', 'xht' => 'application/xhtml+xml', 'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'), 'mid' => 'audio/midi', 'midi' => 'audio/midi', 'mpga' => 'audio/mpeg', 'mp2' => 'audio/mpeg', 'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'), 'aif' => 'audio/x-aiff', 'aiff' => 'audio/x-aiff', 'aifc' => 'audio/x-aiff', 'ram' => 'audio/x-pn-realaudio', 'rm' => 'audio/x-pn-realaudio', 'rpm' => 'audio/x-pn-realaudio-plugin', 'ra' => 'audio/x-realaudio', 'rv' => 'video/vnd.rn-realvideo', 'wav' => 'audio/x-wav', 'bmp' => 'image/bmp', 'gif' => 'image/gif', 'jpeg' => array('image/jpeg', 'image/pjpeg'), 'jpg' => array('image/jpeg', 'image/pjpeg'), 'jpe' => array('image/jpeg', 'image/pjpeg'), 'png' => array('image/png', 'image/x-png'), 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'css' => 'text/css', 'html' => 'text/html', 'htm' => 'text/html', 'shtml' => 'text/html', 'txt' => 'text/plain', 'text' => 'text/plain', 'log' => array('text/plain', 'text/x-log'), 'rtx' => 'text/richtext', 'rtf' => 'text/rtf', 'xml' => 'text/xml', 'xsl' => 'text/xml', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mpe' => 'video/mpeg', 'qt' => 'video/quicktime', 'mov' => 'video/quicktime', 'avi' => 'video/x-msvideo', 'movie' => 'video/x-sgi-movie', 'doc' => 'application/msword', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'word' => array('application/msword', 'application/octet-stream'), 'xl' => 'application/excel', 'eml' => 'message/rfc822', 'json' => array('application/json', 'text/json'));
# Set a default mime if we can't find it
if (!isset($mimes[$extension])) {
$mime = 'application/octet-stream';
} else {
$mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
}
header('Content-Type: "' . $mime . '"');
$tmpName = $filename;
$filename = '"' . urlencode($tmpName) . '"'; #ie中文文件名支持
if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'firefox') != false) {
$filename = '"' . $tmpName . '"';
}#firefox中文文件名支持
if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'chrome') != false) {
$filename = urlencode($tmpName);
}#Chrome中文文件名支持
header('Content-Disposition: attachment; filename=' . $filename);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: no-cache');
header("Content-Length: " . strlen($data));
exit($data);
}
}
if (!function_exists('getRsCol')) {
/**
* 获取结果集中的一个字段的数组
* @param type $rows
* @param type $col_name
* @return array
*/
function getRsCol($rows, $col_name) {
$ret = array();
foreach ($rows as &$row) {
$ret[] = $row[$col_name];
}
return $ret;
}
}
if (!function_exists('chRsKey')) {
/**
* 改变结果集数组key
* @param type $rs 结果集
* @param type $col 作为结果集key的字段名称
* @return type
*/
function chRsKey($rs, $col) {
$_rs = array();
foreach ($rs as $v) {
$_rs[$v[$col]] = $v;
}
return $_rs;
}
}
if (!function_exists('sortRs')) {
/**
* 按字段对结果集进行排序
* @param type $rows
* @param type $key
* @param type $order
* @return array
*/
function sortRs($rows, $key, $order = 'asc') {
$sort = array();
foreach ($rows as $k => $value) {
$sort[$k] = $value[$key];
}
$order == 'asc' ? asort($sort) : arsort($sort);
$ret = array();
foreach ($sort as $k => $value) {
$ret[] = $rows[$k];
}
return $ret;
}
}
if (!function_exists('mergeRs')) {
/**
* 合并多个结果集,参数是多个:array($rs,$column_name),$column_name是该结果集和其它结果集关联的字段
* 比如:$rs1=array(array('a'=>'1111','b'=>'fasdfas'),array('a'=>'222','b'=>'fasdfas'),array('a'=>'333','b'=>'fasdfas'));
$rs2=array(array('c'=>'1111','r'=>'fasd22fas'),array('c'=>'222','r'=>'fasd22fas'),array('c'=>'333','r'=>'fasdf22as'));
$rs3=array(array('a'=>'1111','e'=>'fasd33fas'),array('a'=>'222','e'=>'fas33dfas'),array('a'=>'333','e'=>'fas33dfas'));
var_dump(mergeRs(array($rs1,'a'),array($rs2,'c'),array($rs3,'a')));
* 上面的例子中三个结果集中的关联字段是$rs1.a=$rs2.c=$rs3.a
* @return array
*/
function mergeRs() {
$argv = func_get_args();
$argc = count($argv);
$ret = array();
foreach ($argv[0][0] as $v) {
$r = $v;
for ($j = 1; $j < $argc; $j++) {
foreach ($argv[$j][0] as $row) {
if ($v[$argv[0][1]] == $row[$argv[$j][1]]) {
$r = array_merge($r, $row);
break;
}
}
}
$ret[] = $r;
}
$allkeys = array();
foreach ($argv as $rs) {
foreach (array_keys($rs[0][0]) as $key) {
$allkeys[] = $key;
}
}
foreach ($ret as &$row) {
foreach ($allkeys as $key) {
if (!isset($row[$key])) {
$row[$key] = null;
}
}
}
return $ret;
}
}
class MP{
}
/* End of file Helper.php */
class WoniuInput {
public static $router;
/**
* 系统最终使用的路由字符串
* @return type
*/
public static function route_query() {
return self::$router['query'];
}
/**
* hmvc模块名称,没有模块就为空
* @return type
*/
public static function module_name() {
return self::$router['module'];
}
/**
* url中方法的路径<br/>
* 比如:<br>
* 1.home.index<br>
* 2.user.home.index ,user是文件夹<br>
* @return type
*/
public static function method_path() {
return self::$router['mpath'];
}
/**
* url中方法名称<br/>
* 比如:<br>
* 1.index<br>
* @return type
*/
public static function method_name() {
return self::$router['m'];
}
/**
* $system配置中方法前缀,比如:do
* @return type
*/
public static function method_prefix() {
return self::$router['prefix'];
}
/**
* url中控制器的路径<br/>
* 比如:<br>
* 1.home<br>
* 2.user.home ,user是文件夹<br>
* @return type
*/
public static function controller_path() {
return self::$router['cpath'];
}
/**
* url中控制器名称<br/>
* 比如:<br>
* 1.home<br>
* @return type
*/
public static function controller_name() {
return self::$router['c'];
}
/**
* url中文件夹名称,没有文件夹返回空<br/>
* 比如:<br/>
* 1.user
*/
public static function folder_name() {
return self::$router['folder'];
}
/**
* 请求的控制器文件绝对路径<br/>
* 比如:/home/www/app/controllers/home.php<br/>
*
*/
public static function controller_file() {
return self::$router['file'];
}
/**
* 请求的控制器类名称<br/>
* 比如:Home
*/
public static function class_name() {
return self::$router['class'];
}
/**
* 请求的控制器方法名称<br/>
* 比如:doIndex
*/
public static function class_method_name() {
return self::$router['method'];
}
/**
* 传递给控制器方法的所有参数的数组,参数为空时返回参数数组<br/>
* 比如:<br/>
* 1.home.index/username/1234,那么返回的参数数组就是:array('username','1234')<br/>
* 2.如果传递了$key,比如$key是1, 那么将返回1234。如果$key是2那么将返回null。<br/>
* @param type $key 参数的索引从0开始,如果传递了索引那么将返回索引对应的参数,不存在的索引将返回null<br/>
* @return null
*/
public static function parameters($key = null) {
if (!is_null($key)) {
if (isset(self::$router['parameters'][$key])) {
return self::$router['parameters'][$key];
} else {
return null;
}
} else {
return self::$router['parameters'];
}
}
private static function get_x_type($rule, $method, $key) {
$val = null;
switch ($method) {
case 'get':
$val = self::get($key);
break;
case 'post':
$val = self::post($key);
break;
case 'get_post':
$val = self::get_post($key);
break;
case 'post_get':
$val = self::post_get($key);
break;
}
if (is_null(MpLoader::checkData($rule, array('check' => $val)))) {
return $val;
} else {
return null;
}
}
private static function get_rule_type($rule, $method, $key, $default = null) {
if (!is_array($rule)) {
$_rule = array($rule => 'err');
} else {
$_rule = array();
foreach ($rule as $r) {
$_rule[$r] = 'err';
}
}
$rule = array('check' => $_rule);
$val = self::get_x_type($rule, $method, $key);
return is_null($val) ? $default : $val;
}
/**
* 根据验证规则和键获取一个值
* @param type $rule 表单验证规则.示例:1.int 2. array('int','range[1,10]')
* @param type $key 键
* @param type $default 默认值。格式错误或者验证不通过,返回默认值。
* @return type
*/
public static function get_rule($rule, $key, $default = null) {
return self::get_rule_type($rule, 'get', $key, $default);
}
/**
* 根据验证规则和键获取一个值
* @param type $rule 表单验证规则.示例:1.int 2. array('int','range[1,10]')
* @param type $key 键
* @param type $default 默认值。格式错误或者验证不通过,返回默认值。
* @return type
*/
public static function post_rule($rule, $key, $default = null) {
return self::get_rule_type($rule, 'post', $key, $default);
}
/**
* 根据验证规则和键获取一个值
* @param type $rule 表单验证规则.示例:1.int 2. array('int','range[1,10]')
* @param type $key 键
* @param type $default 默认值。格式错误或者验证不通过,返回默认值。
* @return type
*/
public static function get_post_rule($rule, $key, $default = null) {
return self::get_rule_type($rule, 'get_post', $key, $default);
}
/**
* 根据验证规则和键获取一个值
* @param type $rule 表单验证规则.示例:1.int 2. array('int','range[1,10]')
* @param type $key 键
* @param type $default 默认值。格式错误或者验证不通过,返回默认值。
* @return type
*/
public static function post_get_rule($rule, $key, $default = null) {
return self::get_rule_type($rule, 'post_get', $key, $default);
}
private static function get_int_type($method, $key, $min = null, $max = null, $default = null) {
$val = self::get_rule_type('int', $method, $key);
$min_okay = is_null($min) || (!is_null($min) && $val >= $min);
$max_okay = is_null($max) || (!is_null($max) && $val <= $max);
return $min_okay && $max_okay && !is_null($val) ? $val : $default;
}
/**
* 获取一个整数
* @param type $key 键
* @param type $min 最小值,为null不限制
* @param type $max 最大值,为null不限制
* @param type $default 默认值。格式错误或者不在范围,返回默认值
* @return type
*/
public static function get_int($key, $min = null, $max = null, $default = null) {
return self::get_int_type('get', $key, $min, $max, $default);
}
/**
* 获取一个整数
* @param type $key 键
* @param type $min 最小值,为null不限制
* @param type $max 最大值,为null不限制
* @param type $default 默认值。格式错误或者不在范围,返回默认值
* @return type
*/
public static function post_int($key, $min = null, $max = null, $default = null) {
return self::get_int_type('post', $key, $min, $max, $default);
}
/**
* 获取一个整数
* @param type $key 键
* @param type $min 最小值,为null不限制
* @param type $max 最大值,为null不限制
* @param type $default 默认值。格式错误或者不在范围,返回默认值
* @return type
*/
public static function get_post_int($key, $min = null, $max = null, $default = null) {
return self::get_int_type('get_post', $key, $min, $max, $default);
}
/**
* 获取一个整数
* @param type $key 键
* @param type $min 最小值,为null不限制
* @param type $max 最大值,为null不限制
* @param type $default 默认值。格式错误或者不在范围,返回默认值
* @return type
*/
public static function post_get_int($key, $min = null, $max = null, $default = null) {
return self::get_int_type('post_get', $key, $min, $max, $default);
}
private static function get_date_type($method, $key, $min = null, $max = null, $default = null) {
$val = self::get_rule_type('date', $method, $key);
$min_okay = is_null($min) || (!is_null($min) && strtotime($val) >= strtotime($min));
$max_okay = is_null($max) || (!is_null($max) && strtotime($val) <= strtotime($max));
return $min_okay && $max_okay && !is_null($val) ? $val : $default;
}
/**
* 获取日期,格式:2012-12-12
* @param type $key 键
* @param type $min 最小日期,格式:2012-12-12。为null不限制
* @param type $max 最大日期,格式:2012-12-12。为null不限制
* @param type $default 默认日期。格式错误或者不在范围,返回默认日期
* @return type
*/
public static function get_date($key, $min = null, $max = null, $default = null) {
return self::get_date_type('get', $key, $min, $max, $default);
}
/**
* 获取日期,格式:2012-12-12
* @param type $key 键
* @param type $min 最小日期,格式:2012-12-12。为null不限制
* @param type $max 最大日期,格式:2012-12-12。为null不限制
* @param type $default 默认日期。格式错误或者不在范围,返回默认日期
* @return type
*/
public static function post_date($key, $min = null, $max = null, $default = null) {
return self::get_date_type('post', $key, $min, $max, $default);
}
/**
* 获取日期,格式:2012-12-12
* @param type $key 键
* @param type $min 最小日期,格式:2012-12-12。为null不限制
* @param type $max 最大日期,格式:2012-12-12。为null不限制
* @param type $default 默认日期。格式错误或者不在范围,返回默认日期