@@ -29,18 +29,10 @@ function halt($error) {
29
29
$ trace = debug_backtrace ();
30
30
$ e ['message ' ] = $ error ;
31
31
$ e ['file ' ] = $ trace [0 ]['file ' ];
32
- $ e ['class ' ] = isset ($ trace [0 ]['class ' ])?$ trace [0 ]['class ' ]:'' ;
33
- $ e ['function ' ] = isset ($ trace [0 ]['function ' ])?$ trace [0 ]['function ' ]:'' ;
34
32
$ e ['line ' ] = $ trace [0 ]['line ' ];
35
- $ traceInfo = '' ;
36
- $ time = date ('y-m-d H:i:m ' );
37
- foreach ($ trace as $ t ) {
38
- $ traceInfo .= '[ ' . $ time . '] ' . $ t ['file ' ] . ' ( ' . $ t ['line ' ] . ') ' ;
39
- $ traceInfo .= $ t ['class ' ] . $ t ['type ' ] . $ t ['function ' ] . '( ' ;
40
- $ traceInfo .= implode (', ' , $ t ['args ' ]);
41
- $ traceInfo .=')<br/> ' ;
42
- }
43
- $ e ['trace ' ] = $ traceInfo ;
33
+ ob_start ();
34
+ debug_print_backtrace ();
35
+ $ e ['trace ' ] = ob_get_clean ();
44
36
} else {
45
37
$ e = $ error ;
46
38
}
@@ -70,7 +62,7 @@ function halt($error) {
70
62
*/
71
63
function throw_exception ($ msg , $ type ='ThinkException ' , $ code =0 ) {
72
64
if (class_exists ($ type , false ))
73
- throw new $ type ($ msg , $ code, true );
65
+ throw new $ type ($ msg , $ code );
74
66
else
75
67
halt ($ msg ); // 异常类型不存在则输出错误信息字串
76
68
}
@@ -265,7 +257,7 @@ function U($url='',$vars='',$suffix=true,$redirect=false,$domain=false) {
265
257
}
266
258
if (!empty ($ vars )) { // 添加参数
267
259
foreach ($ vars as $ var => $ val ){
268
- if ('' !== trim ($ val )) $ url .= $ depr . $ var . $ depr . $ val ;
260
+ if ('' !== trim ($ val )) $ url .= $ depr . $ var . $ depr . urlencode ( $ val) ;
269
261
}
270
262
}
271
263
if ($ suffix ) {
@@ -295,11 +287,13 @@ function U($url='',$vars='',$suffix=true,$redirect=false,$domain=false) {
295
287
* @param string $name Widget名称
296
288
* @param array $data 传人的参数
297
289
* @param boolean $return 是否返回内容
290
+ * @param string $path Widget所在路径
298
291
* @return void
299
292
*/
300
293
function W ($ name , $ data =array (), $ return =false ) {
301
294
$ class = $ name . 'Widget ' ;
302
- require_cache (BASE_LIB_PATH . 'Widget/ ' . $ class . '.class.php ' );
295
+ $ path = empty ($ path ) ? BASE_LIB_PATH : $ path ;
296
+ require_cache ($ path . 'Widget/ ' . $ class . '.class.php ' );
303
297
if (!class_exists ($ class ))
304
298
throw_exception (L ('_CLASS_NOT_EXIST_ ' ) . ': ' . $ class );
305
299
$ widget = Think::instance ($ class );
@@ -365,13 +359,10 @@ function redirect($url, $time=0, $msg='') {
365
359
}
366
360
}
367
361
368
-
369
362
/**
370
- * 全局缓存设置和读取
363
+ * 缓存管理
371
364
* @param string $name 缓存名称
372
365
* @param mixed $value 缓存值
373
- * @param integer $expire 缓存有效期(秒)
374
- * @param string $type 缓存类型
375
366
* @param array $options 缓存参数
376
367
* @return mixed
377
368
*/
@@ -394,17 +385,19 @@ function S($name,$value='',$options=null) {
394
385
}elseif (is_null ($ value )) { // 删除缓存
395
386
return $ cache ->rm ($ name );
396
387
}else { // 缓存数据
397
- $ expire = is_numeric ($ options )?$ options :NULL ;
388
+ if (is_array ($ options )) {
389
+ $ expire = isset ($ options ['expire ' ])?$ options ['expire ' ]:NULL ;
390
+ }else {
391
+ $ expire = is_numeric ($ options )?$ options :NULL ;
392
+ }
398
393
return $ cache ->set ($ name , $ value , $ expire );
399
394
}
400
395
}
401
-
402
396
// S方法的别名 已经废除 不再建议使用
403
397
function cache ($ name ,$ value ='' ,$ options =null ){
404
398
return S ($ name ,$ value ,$ options );
405
399
}
406
400
407
-
408
401
/**
409
402
* 快速文件数据读取和保存 针对简单类型数据 字符串、数组
410
403
* @param string $name 缓存名称
@@ -495,31 +488,47 @@ function to_guid_string($mix) {
495
488
/**
496
489
* XML编码
497
490
* @param mixed $data 数据
498
- * @param string $encoding 数据编码
499
491
* @param string $root 根节点名
492
+ * @param string $item 数字索引的子节点名
493
+ * @param string $attr 根节点属性
494
+ * @param string $id 数字索引子节点key转换的属性名
495
+ * @param string $encoding 数据编码
500
496
* @return string
501
497
*/
502
- function xml_encode ($ data , $ encoding ='utf-8 ' , $ root ='think ' ) {
503
- $ xml = '<?xml version="1.0" encoding=" ' . $ encoding . '"?> ' ;
504
- $ xml .= '< ' . $ root . '> ' ;
505
- $ xml .= data_to_xml ($ data );
506
- $ xml .= '</ ' . $ root . '> ' ;
498
+ function xml_encode ($ data , $ root ='think ' , $ item ='item ' , $ attr ='' , $ id ='id ' , $ encoding ='utf-8 ' ) {
499
+ if (is_array ($ attr )){
500
+ $ _attr = array ();
501
+ foreach ($ attr as $ key => $ value ) {
502
+ $ _attr [] = "{$ key }= \"{$ value }\"" ;
503
+ }
504
+ $ attr = implode (' ' , $ _attr );
505
+ }
506
+ $ attr = trim ($ attr );
507
+ $ attr = empty ($ attr ) ? '' : " {$ attr }" ;
508
+ $ xml = "<?xml version= \"1.0 \" encoding= \"{$ encoding }\"?> " ;
509
+ $ xml .= "< {$ root }{$ attr }> " ;
510
+ $ xml .= data_to_xml ($ data , $ item , $ id );
511
+ $ xml .= "</ {$ root }> " ;
507
512
return $ xml ;
508
513
}
509
514
510
515
/**
511
516
* 数据XML编码
512
- * @param mixed $data 数据
517
+ * @param mixed $data 数据
518
+ * @param string $item 数字索引时的节点名称
519
+ * @param string $id 数字索引key转换为的属性名
513
520
* @return string
514
521
*/
515
- function data_to_xml ($ data ) {
516
- $ xml = '' ;
522
+ function data_to_xml ($ data, $ item = ' item ' , $ id = ' id ' ) {
523
+ $ xml = $ attr = '' ;
517
524
foreach ($ data as $ key => $ val ) {
518
- is_numeric ($ key ) && $ key = "item id= \"$ key \"" ;
519
- $ xml .= "< $ key> " ;
520
- $ xml .= ( is_array ($ val ) || is_object ($ val )) ? data_to_xml ($ val ) : $ val ;
521
- list ($ key , ) = explode (' ' , $ key );
522
- $ xml .= "</ $ key> " ;
525
+ if (is_numeric ($ key )){
526
+ $ id && $ attr = " {$ id }= \"{$ key }\"" ;
527
+ $ key = $ item ;
528
+ }
529
+ $ xml .= "< {$ key }{$ attr }> " ;
530
+ $ xml .= (is_array ($ val ) || is_object ($ val )) ? data_to_xml ($ val , $ item , $ id ) : $ val ;
531
+ $ xml .= "</ {$ key }> " ;
523
532
}
524
533
return $ xml ;
525
534
}
@@ -711,7 +720,7 @@ function load_ext_file() {
711
720
* @return mixed
712
721
*/
713
722
function get_client_ip ($ type = 0 ) {
714
- $ type = $ type ? 1 : 0 ;
723
+ $ type = $ type ? 1 : 0 ;
715
724
static $ ip = NULL ;
716
725
if ($ ip !== NULL ) return $ ip [$ type ];
717
726
if (isset ($ _SERVER ['HTTP_X_FORWARDED_FOR ' ])) {
@@ -762,4 +771,4 @@ function filter_exp(&$value){
762
771
if (in_array (strtolower ($ value ),array ('exp ' ,'or ' ))){
763
772
$ value .= ' ' ;
764
773
}
765
- }
774
+ }
0 commit comments