PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/code/core/ThinkPHP/Common/functions.php

http://thinksns-2.googlecode.com/
PHP | 831 lines | 610 code | 41 blank | 180 comment | 127 complexity | 22a35919de82fdd3f8562c1c08af94f9 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * Think?????
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Common
  18. * @author liu21st <liu21st@gmail.com>
  19. * @version $Id$
  20. +------------------------------------------------------------------------------
  21. */
  22. // URL?? ?????????
  23. function U($url,$params=array(),$redirect=false,$suffix=true) {
  24. if(0===strpos($url,'/'))
  25. $url = substr($url,1);
  26. if(!strpos($url,'://')) // ??????? ???????
  27. $url = APP_NAME.'://'.$url;
  28. if(stripos($url,'@?')) { // ???????
  29. $url = str_replace('@?','@think?',$url);
  30. }elseif(stripos($url,'@')) { // ???????
  31. $url = $url.MODULE_NAME;
  32. }
  33. // ??URL??
  34. $array = parse_url($url);
  35. $app = isset($array['scheme'])? $array['scheme'] :APP_NAME;
  36. $route = isset($array['user'])?$array['user']:'';
  37. if(defined('GROUP_NAME') && strcasecmp(GROUP_NAME,C('DEFAULT_GROUP')))
  38. $group= GROUP_NAME;
  39. if(isset($array['path'])) {
  40. $action = substr($array['path'],1);
  41. if(!isset($array['host'])) {
  42. // ???????
  43. $module = MODULE_NAME;
  44. }else{// ????
  45. if(strpos($array['host'],'-')) {
  46. list($group,$module) = explode('-',$array['host']);
  47. }else{
  48. $module = $array['host'];
  49. }
  50. }
  51. }else{ // ?????
  52. $module = MODULE_NAME;
  53. $action = $array['host'];
  54. }
  55. if(isset($array['query'])) {
  56. parse_str($array['query'],$query);
  57. $params = array_merge($query,$params);
  58. }
  59. if(C('URL_DISPATCH_ON') && C('URL_MODEL')>0) {
  60. $depr = C('URL_PATHINFO_MODEL')==2?C('URL_PATHINFO_DEPR'):'/';
  61. $str = $depr;
  62. foreach ($params as $var=>$val)
  63. $str .= $var.$depr.$val.$depr;
  64. $str = substr($str,0,-1);
  65. $group = isset($group)?$group.$depr:'';
  66. if(!empty($route)) {
  67. $url = str_replace(APP_NAME,$app,__APP__).'/'.$group.$route.$str;
  68. }else{
  69. $url = str_replace(APP_NAME,$app,__APP__).'/'.$group.$module.$depr.$action.$str;
  70. }
  71. if($suffix && C('URL_HTML_SUFFIX'))
  72. $url .= C('URL_HTML_SUFFIX');
  73. }else{
  74. $params = http_build_query($params);
  75. if(isset($group)) {
  76. $url = str_replace(APP_NAME,$app,__APP__).'?'.C('VAR_GROUP').'='.$group.'&'.C('VAR_MODULE').'='.$module.'&'.C('VAR_ACTION').'='.$action.'&'.$params;
  77. }else{
  78. $url = str_replace(APP_NAME,$app,__APP__).'?'.C('VAR_MODULE').'='.$module.'&'.C('VAR_ACTION').'='.$action.'&'.$params;
  79. }
  80. }
  81. if($redirect)
  82. redirect($url);
  83. else
  84. return $url;
  85. }
  86. /**
  87. +----------------------------------------------------------
  88. * ?????????
  89. * type
  90. * =0 ?Java?????C???
  91. * =1 ?C?????Java???
  92. +----------------------------------------------------------
  93. * @access protected
  94. +----------------------------------------------------------
  95. * @param string $name ???
  96. * @param integer $type ????
  97. +----------------------------------------------------------
  98. * @return string
  99. +----------------------------------------------------------
  100. */
  101. function parse_name($name,$type=0) {
  102. if($type) {
  103. return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
  104. }else{
  105. $name = preg_replace("/[A-Z]/", "_\\0", $name);
  106. return strtolower(trim($name, "_"));
  107. }
  108. }
  109. // ????
  110. function halt($error) {
  111. if(IS_CLI) exit ($error);
  112. $e = array();
  113. if(C('APP_DEBUG')){
  114. //???????????
  115. if(!is_array($error)) {
  116. $trace = debug_backtrace();
  117. $e['message'] = $error;
  118. $e['file'] = $trace[0]['file'];
  119. $e['class'] = $trace[0]['class'];
  120. $e['function'] = $trace[0]['function'];
  121. $e['line'] = $trace[0]['line'];
  122. $traceInfo='';
  123. $time = date("y-m-d H:i:m");
  124. foreach($trace as $t)
  125. {
  126. $traceInfo .= '['.$time.'] '.$t['file'].' ('.$t['line'].') ';
  127. $traceInfo .= $t['class'].$t['type'].$t['function'].'(';
  128. $traceInfo .= implode(', ', $t['args']);
  129. $traceInfo .=")<br/>";
  130. }
  131. $e['trace'] = $traceInfo;
  132. }else {
  133. $e = $error;
  134. }
  135. // ????????
  136. include C('TMPL_EXCEPTION_FILE');
  137. }
  138. else
  139. {
  140. //?????????
  141. $error_page = C('ERROR_PAGE');
  142. if(!empty($error_page)){
  143. redirect($error_page);
  144. }else {
  145. if(C('SHOW_ERROR_MSG'))
  146. $e['message'] = is_array($error)?$error['message']:$error;
  147. else
  148. $e['message'] = C('ERROR_MESSAGE');
  149. // ????????
  150. include C('TMPL_EXCEPTION_FILE');
  151. }
  152. }
  153. exit;
  154. }
  155. // URL???
  156. function redirect($url,$time=0,$msg='')
  157. {
  158. //??URL????
  159. $url = str_replace(array("\n", "\r"), '', $url);
  160. if(empty($msg))
  161. $msg = "????{$time}????????{$url}?";
  162. if (!headers_sent()) {
  163. // redirect
  164. if(0===$time) {
  165. header("Location: ".$url);
  166. }else {
  167. header("refresh:{$time};url={$url}");
  168. echo($msg);
  169. }
  170. exit();
  171. }else {
  172. $str = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
  173. if($time!=0)
  174. $str .= $msg;
  175. exit($str);
  176. }
  177. }
  178. // ???????
  179. function throw_exception($msg,$type='ThinkException',$code=0)
  180. {
  181. if(IS_CLI) exit($msg);
  182. if(class_exists($type,false))
  183. throw new $type($msg,$code,true);
  184. else
  185. halt($msg); // ????????????????
  186. }
  187. // ??????
  188. function debug_start($label='')
  189. {
  190. $GLOBALS[$label]['_beginTime'] = microtime(TRUE);
  191. if ( MEMORY_LIMIT_ON ) $GLOBALS[$label]['_beginMem'] = memory_get_usage();
  192. }
  193. // ?????????????????????
  194. function debug_end($label='')
  195. {
  196. $GLOBALS[$label]['_endTime'] = microtime(TRUE);
  197. echo '<div style="text-align:center;width:100%">Process '.$label.': Times '.number_format($GLOBALS[$label]['_endTime']-$GLOBALS[$label]['_beginTime'],6).'s ';
  198. if ( MEMORY_LIMIT_ON ) {
  199. $GLOBALS[$label]['_endMem'] = memory_get_usage();
  200. echo ' Memories '.number_format(($GLOBALS[$label]['_endMem']-$GLOBALS[$label]['_beginMem'])/1024).' k';
  201. }
  202. echo '</div>';
  203. }
  204. // ??????????
  205. function dump($var, $echo=true,$label=null, $strict=true)
  206. {
  207. $label = ($label===null) ? '' : rtrim($label) . ' ';
  208. if(!$strict) {
  209. if (ini_get('html_errors')) {
  210. $output = print_r($var, true);
  211. $output = "<pre>".$label.htmlspecialchars($output,ENT_QUOTES)."</pre>";
  212. } else {
  213. $output = $label . " : " . print_r($var, true);
  214. }
  215. }else {
  216. ob_start();
  217. var_dump($var);
  218. $output = ob_get_clean();
  219. if(!extension_loaded('xdebug')) {
  220. $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
  221. $output = '<pre>'. $label. htmlspecialchars($output, ENT_QUOTES). '</pre>';
  222. }
  223. }
  224. if ($echo) {
  225. echo($output);
  226. return null;
  227. }else
  228. return $output;
  229. }
  230. // ?????? ??????????
  231. function get_instance_of($name,$method='',$args=array())
  232. {
  233. static $_instance = array();
  234. $identify = empty($args)?$name.$method:$name.$method.to_guid_string($args);
  235. if (!isset($_instance[$identify])) {
  236. if(class_exists($name)){
  237. $o = new $name();
  238. if(method_exists($o,$method)){
  239. if(!empty($args)) {
  240. $_instance[$identify] = call_user_func_array(array(&$o, $method), $args);
  241. }else {
  242. $_instance[$identify] = $o->$method();
  243. }
  244. }
  245. else
  246. $_instance[$identify] = $o;
  247. }
  248. else
  249. halt(L('_CLASS_NOT_EXIST_').':'.$name);
  250. }
  251. return $_instance[$identify];
  252. }
  253. /**
  254. +----------------------------------------------------------
  255. * ??????ThinkPHP?????????model?Action??
  256. * ????????????
  257. +----------------------------------------------------------
  258. * @param string $name ????
  259. +----------------------------------------------------------
  260. * @return void
  261. +----------------------------------------------------------
  262. */
  263. function __autoload($name)
  264. {
  265. // ??????????
  266. if(alias_import($name)) return ;
  267. // ?????????Actioon??Model?
  268. if(substr($name,-5)=="Model") {
  269. require_cache(LIB_PATH.'Model/'.$name.'.class.php');
  270. }elseif(substr($name,-6)=="Action"){
  271. require_cache(LIB_PATH.'Action/'.$name.'.class.php');
  272. }else {
  273. // ????????????????
  274. if(C('APP_AUTOLOAD_PATH')) {
  275. $paths = explode(',',C('APP_AUTOLOAD_PATH'));
  276. foreach ($paths as $path){
  277. if(import($path.$name)) {
  278. // ??????????
  279. return ;
  280. }
  281. }
  282. }
  283. }
  284. return ;
  285. }
  286. // ???require_once
  287. function require_cache($filename)
  288. {
  289. static $_importFiles = array();
  290. $filename = realpath($filename);
  291. if (!isset($_importFiles[$filename])) {
  292. if(file_exists_case($filename)){
  293. require $filename;
  294. $_importFiles[$filename] = true;
  295. }
  296. else
  297. {
  298. $_importFiles[$filename] = false;
  299. }
  300. }
  301. return $_importFiles[$filename];
  302. }
  303. // ????????????
  304. function file_exists_case($filename) {
  305. if(is_file($filename)) {
  306. if(IS_WIN && C('APP_FILE_CASE')) {
  307. if(basename(realpath($filename)) != basename($filename))
  308. return false;
  309. }
  310. return true;
  311. }
  312. return false;
  313. }
  314. /**
  315. +----------------------------------------------------------
  316. * ??????? ?java?Import
  317. * ????????
  318. +----------------------------------------------------------
  319. * @param string $class ?????????
  320. * @param string $baseUrl ????
  321. * @param string $ext ????????
  322. +----------------------------------------------------------
  323. * @return boolen
  324. +----------------------------------------------------------
  325. */
  326. function import($class)
  327. {
  328. static $_file = array();
  329. static $_class = array();
  330. if(isset($_file[$class])) {
  331. return true;
  332. } else if(file_exists($class)){
  333. $_file[$class] = true;
  334. return require_cache($class);
  335. } else {
  336. return alias_import($class);
  337. }
  338. }
  339. /**
  340. +----------------------------------------------------------
  341. * ?????????????
  342. * load('@.Util.Array')
  343. +----------------------------------------------------------
  344. * @param string $name ??????????
  345. * @param string $baseUrl ????
  346. * @param string $ext ????????
  347. +----------------------------------------------------------
  348. * @return void
  349. +----------------------------------------------------------
  350. */
  351. function load($name,$baseUrl='',$ext='.php') {
  352. $name = str_replace(array('.','#'), array('/','.'), $name);
  353. if(empty($baseUrl)) {
  354. if(0 === strpos($name,'@/')) {
  355. //?????????
  356. $baseUrl = APP_PATH.'/Common/';
  357. $name = substr($name,2);
  358. }else{
  359. //??ThinkPHP ?????
  360. $baseUrl = THINK_PATH.'/Common/';
  361. }
  362. }
  363. if(substr($baseUrl, -1) != "/") $baseUrl .= "/";
  364. include $baseUrl . $name . $ext;
  365. }
  366. // ?????????
  367. function alias_import($alias,$classfile='') {
  368. static $_alias = array();
  369. if('' !== $classfile) {
  370. // ??????
  371. $_alias[$alias] = $classfile;
  372. return ;
  373. }
  374. if(is_string($alias)) {
  375. if(isset($_alias[$alias]))
  376. return require_cache($_alias[$alias]);
  377. }elseif(is_array($alias)){
  378. foreach ($alias as $key=>$val)
  379. $_alias[$key] = $val;
  380. return ;
  381. }
  382. return false;
  383. }
  384. /**
  385. +----------------------------------------------------------
  386. * D???????Model
  387. +----------------------------------------------------------
  388. * @param string name Model??
  389. * @param string app Model????
  390. +----------------------------------------------------------
  391. * @return Model
  392. +----------------------------------------------------------
  393. */
  394. function D($name='',$app='')
  395. {
  396. static $_model = array();
  397. if(empty($name)) return new Model;
  398. if(empty($app)) $app = APP_NAME;
  399. if(isset($_model[$app.$name]))
  400. return $_model[$app.$name];
  401. $OriClassName = $name;
  402. $className = $name.'Model';
  403. import(APPS_PATH.'/'.$app.'./Model/'.$className.'.class.php');
  404. if(class_exists($className)) {
  405. $model = new $className();
  406. }else {
  407. $model = new Model($name);
  408. }
  409. $_model[$app.$OriClassName] = $model;
  410. return $model;
  411. }
  412. /**
  413. +----------------------------------------------------------
  414. * M????????????????Model
  415. +----------------------------------------------------------
  416. * @param string name Model??
  417. +----------------------------------------------------------
  418. * @return Model
  419. +----------------------------------------------------------
  420. */
  421. function M($name='',$class='Model') {
  422. static $_model = array();
  423. if(!isset($_model[$name.'_'.$class]))
  424. $_model[$name.'_'.$class] = new $class($name);
  425. return $_model[$name.'_'.$class];
  426. }
  427. /**
  428. +----------------------------------------------------------
  429. * A???????Action
  430. +----------------------------------------------------------
  431. * @param string name Action??
  432. * @param string app Model????
  433. +----------------------------------------------------------
  434. * @return Action
  435. +----------------------------------------------------------
  436. */
  437. function A($name='',$app='')
  438. {
  439. static $_action = array();
  440. if(empty($name)) return false;
  441. if(empty($app)) $app = APP_NAME;
  442. if(isset($_action[$app.$name]))
  443. return $_action[$app.$name];
  444. $OriClassName = $name;
  445. $className = $name.'Action';
  446. import(APPS_PATH.'/'.$app.'./Action/'.$className.'.class.php');
  447. if(class_exists($className)) {
  448. $action = new $className();
  449. $_action[$app.$OriClassName] = $action;
  450. return $action;
  451. }else {
  452. return false;
  453. }
  454. }
  455. // ???????????
  456. function R($module,$action,$app='@') {
  457. $class = A($module,$app);
  458. if($class)
  459. return call_user_func(array(&$class,$action));
  460. else
  461. return false;
  462. }
  463. // ?????????(??????)
  464. function L($name=null,$value=null) {
  465. static $_lang = array();
  466. // ?????????
  467. if(empty($name)) return $_lang;
  468. // ??????(???)
  469. // ????,???????$name
  470. if (is_string($name) )
  471. {
  472. $name = strtoupper($name);
  473. if (is_null($value))
  474. return isset($_lang[$name]) ? $_lang[$name] : $name;
  475. $_lang[$name] = $value;// ????
  476. return;
  477. }
  478. // ????
  479. if (is_array($name))
  480. $_lang = array_merge($_lang,array_change_key_case($name,CASE_UPPER));
  481. return;
  482. }
  483. // ?????
  484. function C($name=null,$value=null)
  485. {
  486. static $_config = array();
  487. // ????????
  488. if(empty($name)) return $_config;
  489. // ???????????
  490. if (is_string($name))
  491. {
  492. if (!strpos($name,'.')) {
  493. $name = strtolower($name);
  494. if (is_null($value))
  495. return isset($_config[$name])? $_config[$name] : null;
  496. $_config[$name] = $value;
  497. return;
  498. }
  499. // ???????????
  500. $name = explode('.',$name);
  501. $name[0] = strtolower($name[0]);
  502. if (is_null($value))
  503. return isset($_config[$name[0]][$name[1]]) ? $_config[$name[0]][$name[1]] : null;
  504. $_config[$name[0]][$name[1]] = $value;
  505. return;
  506. }
  507. // ????
  508. if(is_array($name))
  509. return $_config = array_merge($_config,array_change_key_case($name));
  510. return null;// ??????
  511. }
  512. // ????
  513. function tag($name,$params=array()) {
  514. $tags = C('_tags_.'.$name);
  515. if($tags) {
  516. foreach ($tags as $key=>$call){
  517. if(is_callable($call))
  518. $result = call_user_func_array($call,$params);
  519. }
  520. return $result;
  521. }
  522. return false;
  523. }
  524. // ????Widget
  525. function W($name,$data=array(),$return=false) {
  526. $class = $name.'Widget';
  527. require_cache(LIB_PATH.'Widget/'.$class.'.class.php');
  528. if(!class_exists($class))
  529. throw_exception(L('_CLASS_NOT_EXIST_').':'.$class);
  530. $widget = Think::instance($class);
  531. $content = $widget->render($data);
  532. if($return)
  533. return $content;
  534. else
  535. echo $content;
  536. }
  537. // ?????????
  538. function S($name,$value='',$expire='',$type='') {
  539. static $_cache = array();
  540. import('Cache');
  541. //????????
  542. $cache = Cache::getInstance($type);
  543. if('' !== $value) {
  544. if(is_null($value)) {
  545. // ????
  546. $result = $cache->rm($name);
  547. if($result) unset($_cache[$type.'_'.$name]);
  548. return $result;
  549. }else{
  550. // ????
  551. $cache->set($name,$value,$expire);
  552. $_cache[$type.'_'.$name] = $value;
  553. }
  554. return ;
  555. }
  556. if(isset($_cache[$type.'_'.$name]))
  557. return $_cache[$type.'_'.$name];
  558. // ??????
  559. $value = $cache->get($name);
  560. $_cache[$type.'_'.$name] = $value;
  561. return $value;
  562. }
  563. // ??????????? ???????? ??????
  564. function F($name,$value='',$path=DATA_PATH) {
  565. static $_cache = array();
  566. $filename = $path.$name.'.php';
  567. if('' !== $value) {
  568. if(is_null($value)) {
  569. // ????
  570. return unlink($filename);
  571. }else{
  572. // ????
  573. $dir = dirname($filename);
  574. // ????????
  575. if(!is_dir($dir)) mkdir($dir);
  576. return file_put_contents($filename,"<?php\nreturn ".var_export($value,true).";\n?>");
  577. }
  578. }
  579. if(isset($_cache[$name])) return $_cache[$name];
  580. // ??????
  581. if(is_file($filename)) {
  582. $value = include $filename;
  583. $_cache[$name] = $value;
  584. }else{
  585. $value = false;
  586. }
  587. return $value;
  588. }
  589. // ??PHP?????????????
  590. function to_guid_string($mix)
  591. {
  592. if(is_object($mix) && function_exists('spl_object_hash')) {
  593. return spl_object_hash($mix);
  594. }elseif(is_resource($mix)){
  595. $mix = get_resource_type($mix).strval($mix);
  596. }else{
  597. $mix = serialize($mix);
  598. }
  599. return md5($mix);
  600. }
  601. //[RUNTIME]
  602. // ????
  603. function compile($filename,$runtime=false) {
  604. $content = file_get_contents($filename);
  605. if(true === $runtime)
  606. // ???????
  607. $content = preg_replace('/\/\/\[RUNTIME\](.*?)\/\/\[\/RUNTIME\]/s','',$content);
  608. $content = substr(trim($content),5);
  609. if('?>' == substr($content,-2))
  610. $content = substr($content,0,-2);
  611. return $content;
  612. }
  613. // ???????????
  614. function strip_whitespace($content) {
  615. $stripStr = '';
  616. //??php??
  617. $tokens = token_get_all ($content);
  618. $last_space = false;
  619. for ($i = 0, $j = count ($tokens); $i < $j; $i++)
  620. {
  621. if (is_string ($tokens[$i]))
  622. {
  623. $last_space = false;
  624. $stripStr .= $tokens[$i];
  625. }
  626. else
  627. {
  628. switch ($tokens[$i][0])
  629. {
  630. //????PHP??
  631. case T_COMMENT:
  632. case T_DOC_COMMENT:
  633. break;
  634. //????
  635. case T_WHITESPACE:
  636. if (!$last_space)
  637. {
  638. $stripStr .= ' ';
  639. $last_space = true;
  640. }
  641. break;
  642. default:
  643. $last_space = false;
  644. $stripStr .= $tokens[$i][1];
  645. }
  646. }
  647. }
  648. return $stripStr;
  649. }
  650. // ??????????
  651. function array_define($array) {
  652. $content = '';
  653. foreach($array as $key=>$val) {
  654. $key = strtoupper($key);
  655. if(in_array($key,array('THINK_PATH','APP_NAME','APP_PATH','RUNTIME_PATH','RUNTIME_ALLINONE','THINK_MODE')))
  656. $content .= 'if(!defined(\''.$key.'\')) ';
  657. if(is_int($val) || is_float($val)) {
  658. $content .= "define('".$key."',".$val.");";
  659. }elseif(is_bool($val)) {
  660. $val = ($val)?'true':'false';
  661. $content .= "define('".$key."',".$val.");";
  662. }elseif(is_string($val)) {
  663. $content .= "define('".$key."','".addslashes($val)."');";
  664. }
  665. }
  666. return $content;
  667. }
  668. //[/RUNTIME]
  669. // ??????
  670. function mk_dir($dir, $mode = 0755)
  671. {
  672. if (is_dir($dir) || @mkdir($dir,$mode)) return true;
  673. if (!mk_dir(dirname($dir),$mode)) return false;
  674. return @mkdir($dir,$mode);
  675. }
  676. // ??????? ??????
  677. function auto_charset($fContents,$from,$to){
  678. $from = strtoupper($from)=='UTF8'? 'utf-8':$from;
  679. $to = strtoupper($to)=='UTF8'? 'utf-8':$to;
  680. if( strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents)) ){
  681. //??????????????????
  682. return $fContents;
  683. }
  684. if(is_string($fContents) ) {
  685. if(function_exists('mb_convert_encoding')){
  686. return mb_convert_encoding ($fContents, $to, $from);
  687. }elseif(function_exists('iconv')){
  688. return iconv($from,$to,$fContents);
  689. }else{
  690. return $fContents;
  691. }
  692. }
  693. elseif(is_array($fContents)){
  694. foreach ( $fContents as $key => $val ) {
  695. $_key = auto_charset($key,$from,$to);
  696. $fContents[$_key] = auto_charset($val,$from,$to);
  697. if($key != $_key )
  698. unset($fContents[$key]);
  699. }
  700. return $fContents;
  701. }
  702. else{
  703. return $fContents;
  704. }
  705. }
  706. // xml??
  707. function xml_encode($data,$encoding='utf-8',$root="think") {
  708. $xml = '<?xml version="1.0" encoding="'.$encoding.'"?>';
  709. $xml.= '<'.$root.'>';
  710. $xml.= data_to_xml($data);
  711. $xml.= '</'.$root.'>';
  712. return $xml;
  713. }
  714. function data_to_xml($data) {
  715. if(is_object($data)) {
  716. $data = get_object_vars($data);
  717. }
  718. $xml = '';
  719. foreach($data as $key=>$val) {
  720. is_numeric($key) && $key="item id=\"$key\"";
  721. $xml.="<$key>";
  722. $xml.=(is_array($val)||is_object($val))?data_to_xml($val):$val;
  723. list($key,)=explode(' ',$key);
  724. $xml.="</$key>";
  725. }
  726. return $xml;
  727. }
  728. /**
  729. +----------------------------------------------------------
  730. * Cookie ???????? (???????????) 2009-07-9
  731. +----------------------------------------------------------
  732. * 1 ??cookie: cookie('name')
  733. * 2 ???????????cookie: cookie(null)
  734. * 3 ????????cookie: cookie(null,'think_') | ???????????
  735. * 4 ??cookie: cookie('name','value') | ??????: cookie('name','value',3600)
  736. * 5 ??cookie: cookie('name',null)
  737. +----------------------------------------------------------
  738. * $option ????prefix,expire,path,domain
  739. * ??????:cookie('name','value',array('expire'=>1,'prefix'=>'think_'))
  740. * ??query?????:cookie('name','value','prefix=tp_&expire=10000')
  741. */
  742. function cookie($name,$value='',$option=null)
  743. {
  744. // ????
  745. $config = array(
  746. 'prefix' => C('COOKIE_PREFIX'), // cookie ????
  747. 'expire' => C('COOKIE_EXPIRE'), // cookie ????
  748. 'path' => C('COOKIE_PATH'), // cookie ????
  749. 'domain' => C('COOKIE_DOMAIN'), // cookie ????
  750. );
  751. // ????(???????)
  752. if (!empty($option)) {
  753. if (is_numeric($option))
  754. $option = array('expire'=>$option);
  755. elseif( is_string($option) )
  756. parse_str($option,$option);
  757. array_merge($config,array_change_key_case($option));
  758. }
  759. // ?????????cookie
  760. if (is_null($name)) {
  761. if (empty($_COOKIE)) return;
  762. // ????cookie?????????config???????
  763. $prefix = empty($value)? $config['prefix'] : $value;
  764. if (!empty($prefix))// ??????????????????
  765. {
  766. foreach($_COOKIE as $key=>$val) {
  767. if (0 === stripos($key,$prefix)){
  768. setcookie($_COOKIE[$key],'',time()-3600,$config['path'],$config['domain']);
  769. unset($_COOKIE[$key]);
  770. }
  771. }
  772. }
  773. return;
  774. }
  775. $name = $config['prefix'].$name;
  776. if (''===$value){
  777. return isset($_COOKIE[$name]) ? unserialize($_COOKIE[$name]) : null;// ????Cookie
  778. }else {
  779. if (is_null($value)) {
  780. setcookie($name,'',time()-3600,$config['path'],$config['domain']);
  781. unset($_COOKIE[$name]);// ????cookie
  782. }else {
  783. // ??cookie
  784. $expire = !empty($config['expire'])? time()+ intval($config['expire']):0;
  785. setcookie($name,serialize($value),$expire,$config['path'],$config['domain']);
  786. $_COOKIE[$name] = serialize($value);
  787. }
  788. }
  789. }
  790. ?>