PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/sop/2.0beta1/ThinkPHP/Common/extend.php

http://iiccms.googlecode.com/
PHP | 573 lines | 379 code | 24 blank | 170 comment | 45 complexity | 470f68c5e9acc92dc5a5b348499bc66d MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, 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. // ?????IP??
  23. function get_client_ip(){
  24. if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  25. $ip = getenv("HTTP_CLIENT_IP");
  26. else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
  27. $ip = getenv("HTTP_X_FORWARDED_FOR");
  28. else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  29. $ip = getenv("REMOTE_ADDR");
  30. else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
  31. $ip = $_SERVER['REMOTE_ADDR'];
  32. else
  33. $ip = "unknown";
  34. return($ip);
  35. }
  36. /**
  37. +----------------------------------------------------------
  38. * ???????????????
  39. +----------------------------------------------------------
  40. * @static
  41. * @access public
  42. +----------------------------------------------------------
  43. * @param string $str ????????
  44. * @param string $start ????
  45. * @param string $length ????
  46. * @param string $charset ????
  47. * @param string $suffix ??????
  48. +----------------------------------------------------------
  49. * @return string
  50. +----------------------------------------------------------
  51. */
  52. function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
  53. {
  54. if(function_exists("mb_substr"))
  55. return mb_substr($str, $start, $length, $charset);
  56. elseif(function_exists('iconv_substr')) {
  57. return iconv_substr($str,$start,$length,$charset);
  58. }
  59. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  60. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  61. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  62. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  63. preg_match_all($re[$charset], $str, $match);
  64. $slice = join("",array_slice($match[0], $start, $length));
  65. if($suffix) return $slice."…";
  66. return $slice;
  67. }
  68. /**
  69. +----------------------------------------------------------
  70. * ???????????????? ????6? ???????
  71. +----------------------------------------------------------
  72. * @param string $len ??
  73. * @param string $type ????
  74. * 0 ?? 1 ?? ?? ??
  75. * @param string $addChars ????
  76. +----------------------------------------------------------
  77. * @return string
  78. +----------------------------------------------------------
  79. */
  80. function rand_string($len=6,$type='',$addChars='') {
  81. $str ='';
  82. switch($type) {
  83. case 0:
  84. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.$addChars;
  85. break;
  86. case 1:
  87. $chars= str_repeat('0123456789',3);
  88. break;
  89. case 2:
  90. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.$addChars;
  91. break;
  92. case 3:
  93. $chars='abcdefghijklmnopqrstuvwxyz'.$addChars;
  94. break;
  95. case 4:
  96. $chars = "????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????".$addChars;
  97. break;
  98. default :
  99. // ????????????oOLl???01???????addChars??
  100. $chars='ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'.$addChars;
  101. break;
  102. }
  103. if($len>10 ) {//?????????????
  104. $chars= $type==1? str_repeat($chars,$len) : str_repeat($chars,5);
  105. }
  106. if($type!=4) {
  107. $chars = str_shuffle($chars);
  108. $str = substr($chars,0,$len);
  109. }else{
  110. // ?????
  111. for($i=0;$i<$len;$i++){
  112. $str.= msubstr($chars, floor(mt_rand(0,mb_strlen($chars,'utf-8')-1)),1);
  113. }
  114. }
  115. return $str;
  116. }
  117. /**
  118. +----------------------------------------------------------
  119. * ??????? ???4???
  120. +----------------------------------------------------------
  121. * @param string $fmode ???
  122. +----------------------------------------------------------
  123. * @return string
  124. +----------------------------------------------------------
  125. */
  126. function build_verify ($length=4,$mode=1) {
  127. return rand_string($length,$mode);
  128. }
  129. /**
  130. +----------------------------------------------------------
  131. * ????? ??????? B K M G T ?????
  132. +----------------------------------------------------------
  133. * @return string
  134. +----------------------------------------------------------
  135. */
  136. function byte_format($size, $dec=2)
  137. {
  138. $a = array("B", "KB", "MB", "GB", "TB", "PB");
  139. $pos = 0;
  140. while ($size >= 1024) {
  141. $size /= 1024;
  142. $pos++;
  143. }
  144. return round($size,$dec)." ".$a[$pos];
  145. }
  146. /**
  147. +----------------------------------------------------------
  148. * ????????UTF8??
  149. +----------------------------------------------------------
  150. * @param string $string ???
  151. +----------------------------------------------------------
  152. * @return Boolean
  153. +----------------------------------------------------------
  154. */
  155. function is_utf8($string)
  156. {
  157. return preg_match('%^(?:
  158. [\x09\x0A\x0D\x20-\x7E] # ASCII
  159. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  160. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  161. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  162. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  163. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  164. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  165. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  166. )*$%xs', $string);
  167. }
  168. /**
  169. +----------------------------------------------------------
  170. * ????
  171. +----------------------------------------------------------
  172. * @param String $str ????????? ?? ???
  173. * @param Boolean $show ????
  174. +----------------------------------------------------------
  175. * @return String
  176. +----------------------------------------------------------
  177. */
  178. function highlight_code($str,$show=false)
  179. {
  180. if(file_exists($str)) {
  181. $str = file_get_contents($str);
  182. }
  183. $str = stripslashes(trim($str));
  184. // The highlight string function encodes and highlights
  185. // brackets so we need them to start raw
  186. $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);
  187. // Replace any existing PHP tags to temporary markers so they don't accidentally
  188. // break the string out of PHP, and thus, thwart the highlighting.
  189. $str = str_replace(array('&lt;?php', '?&gt;', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str);
  190. // The highlight_string function requires that the text be surrounded
  191. // by PHP tags. Since we don't know if A) the submitted text has PHP tags,
  192. // or B) whether the PHP tags enclose the entire string, we will add our
  193. // own PHP tags around the string along with some markers to make replacement easier later
  194. $str = '<?php //tempstart'."\n".$str.'//tempend ?>'; // <?
  195. // All the magic happens here, baby!
  196. $str = highlight_string($str, TRUE);
  197. // Prior to PHP 5, the highlight function used icky font tags
  198. // so we'll replace them with span tags.
  199. if (abs(phpversion()) < 5)
  200. {
  201. $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
  202. $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
  203. }
  204. // Remove our artificially added PHP
  205. $str = preg_replace("#\<code\>.+?//tempstart\<br />\</span\>#is", "<code>\n", $str);
  206. $str = preg_replace("#\<code\>.+?//tempstart\<br />#is", "<code>\n", $str);
  207. $str = preg_replace("#//tempend.+#is", "</span>\n</code>", $str);
  208. // Replace our markers back to PHP tags.
  209. $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('&lt;?php', '?&gt;', '\\'), $str); //<?
  210. $line = explode("<br />", rtrim(ltrim($str,'<code>'),'</code>'));
  211. $result = '<div class="code"><ol>';
  212. foreach($line as $key=>$val) {
  213. $result .= '<li>'.$val.'</li>';
  214. }
  215. $result .= '</ol></div>';
  216. $result = str_replace("\n", "", $result);
  217. if( $show!== false) {
  218. echo($result);
  219. }else {
  220. return $result;
  221. }
  222. }
  223. //?????html
  224. function h($text, $tags = null){
  225. $text = trim($text);
  226. //??????
  227. $text = preg_replace('/<!--?.*-->/','',$text);
  228. //????????
  229. $text = preg_replace('/<\?|\?'.'>/','',$text);
  230. //????js
  231. $text = preg_replace('/<script?.*\/script>/','',$text);
  232. $text = str_replace('[','&#091;',$text);
  233. $text = str_replace(']','&#093;',$text);
  234. $text = str_replace('|','&#124;',$text);
  235. //?????
  236. $text = preg_replace('/\r?\n/','',$text);
  237. //br
  238. $text = preg_replace('/<br(\s\/)?'.'>/i','[br]',$text);
  239. $text = preg_replace('/(\[br\]\s*){10,}/i','[br]',$text);
  240. //????????????on??lang js
  241. while(preg_match('/(<[^><]+)( lang|on|action|background|codebase|dynsrc|lowsrc)[^><]+/i',$text,$mat)){
  242. $text=str_replace($mat[0],$mat[1],$text);
  243. }
  244. while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i',$text,$mat)){
  245. $text=str_replace($mat[0],$mat[1].$mat[3],$text);
  246. }
  247. if(empty($tags)) {
  248. $tags = 'table|td|th|tr|i|b|u|strong|img|p|br|div|strong|em|ul|ol|li|dl|dd|dt|a';
  249. }
  250. //???HTML??
  251. $text = preg_replace('/<('.$tags.')( [^><\[\]]*)>/i','[\1\2]',$text);
  252. //????html
  253. $text = preg_replace('/<\/?(html|head|meta|link|base|basefont|body|bgsound|title|style|script|form|iframe|frame|frameset|applet|id|ilayer|layer|name|script|style|xml)[^><]*>/i','',$text);
  254. //?????html??
  255. while(preg_match('/<([a-z]+)[^><\[\]]*>[^><]*<\/\1>/i',$text,$mat)){
  256. $text=str_replace($mat[0],str_replace('>',']',str_replace('<','[',$mat[0])),$text);
  257. }
  258. //????
  259. while(preg_match('/(\[[^\[\]]*=\s*)(\"|\')([^\2=\[\]]+)\2([^\[\]]*\])/i',$text,$mat)){
  260. $text=str_replace($mat[0],$mat[1].'|'.$mat[3].'|'.$mat[4],$text);
  261. }
  262. //?????????
  263. while(preg_match('/\[[^\[\]]*(\"|\')[^\[\]]*\]/i',$text,$mat)){
  264. $text=str_replace($mat[0],str_replace($mat[1],'',$mat[0]),$text);
  265. }
  266. //?????????? < >
  267. $text = str_replace('<','&lt;',$text);
  268. $text = str_replace('>','&gt;',$text);
  269. $text = str_replace('"','&quot;',$text);
  270. //???
  271. $text = str_replace('[','<',$text);
  272. $text = str_replace(']','>',$text);
  273. $text = str_replace('|','"',$text);
  274. //??????
  275. $text = str_replace(' ',' ',$text);
  276. return $text;
  277. }
  278. function ubb($Text) {
  279. $Text=trim($Text);
  280. //$Text=htmlspecialchars($Text);
  281. $Text=preg_replace("/\\t/is"," ",$Text);
  282. $Text=preg_replace("/\[h1\](.+?)\[\/h1\]/is","<h1>\\1</h1>",$Text);
  283. $Text=preg_replace("/\[h2\](.+?)\[\/h2\]/is","<h2>\\1</h2>",$Text);
  284. $Text=preg_replace("/\[h3\](.+?)\[\/h3\]/is","<h3>\\1</h3>",$Text);
  285. $Text=preg_replace("/\[h4\](.+?)\[\/h4\]/is","<h4>\\1</h4>",$Text);
  286. $Text=preg_replace("/\[h5\](.+?)\[\/h5\]/is","<h5>\\1</h5>",$Text);
  287. $Text=preg_replace("/\[h6\](.+?)\[\/h6\]/is","<h6>\\1</h6>",$Text);
  288. $Text=preg_replace("/\[separator\]/is","",$Text);
  289. $Text=preg_replace("/\[center\](.+?)\[\/center\]/is","<center>\\1</center>",$Text);
  290. $Text=preg_replace("/\[url=http:\/\/([^\[]*)\](.+?)\[\/url\]/is","<a href=\"http://\\1\" target=_blank>\\2</a>",$Text);
  291. $Text=preg_replace("/\[url=([^\[]*)\](.+?)\[\/url\]/is","<a href=\"http://\\1\" target=_blank>\\2</a>",$Text);
  292. $Text=preg_replace("/\[url\]http:\/\/([^\[]*)\[\/url\]/is","<a href=\"http://\\1\" target=_blank>\\1</a>",$Text);
  293. $Text=preg_replace("/\[url\]([^\[]*)\[\/url\]/is","<a href=\"\\1\" target=_blank>\\1</a>",$Text);
  294. $Text=preg_replace("/\[img\](.+?)\[\/img\]/is","<img src=\\1>",$Text);
  295. $Text=preg_replace("/\[color=(.+?)\](.+?)\[\/color\]/is","<font color=\\1>\\2</font>",$Text);
  296. $Text=preg_replace("/\[size=(.+?)\](.+?)\[\/size\]/is","<font size=\\1>\\2</font>",$Text);
  297. $Text=preg_replace("/\[sup\](.+?)\[\/sup\]/is","<sup>\\1</sup>",$Text);
  298. $Text=preg_replace("/\[sub\](.+?)\[\/sub\]/is","<sub>\\1</sub>",$Text);
  299. $Text=preg_replace("/\[pre\](.+?)\[\/pre\]/is","<pre>\\1</pre>",$Text);
  300. $Text=preg_replace("/\[email\](.+?)\[\/email\]/is","<a href='mailto:\\1'>\\1</a>",$Text);
  301. $Text=preg_replace("/\[colorTxt\](.+?)\[\/colorTxt\]/eis","color_txt('\\1')",$Text);
  302. $Text=preg_replace("/\[emot\](.+?)\[\/emot\]/eis","emot('\\1')",$Text);
  303. $Text=preg_replace("/\[i\](.+?)\[\/i\]/is","<i>\\1</i>",$Text);
  304. $Text=preg_replace("/\[u\](.+?)\[\/u\]/is","<u>\\1</u>",$Text);
  305. $Text=preg_replace("/\[b\](.+?)\[\/b\]/is","<b>\\1</b>",$Text);
  306. $Text=preg_replace("/\[quote\](.+?)\[\/quote\]/is"," <div class='quote'><h5>??:</h5><blockquote>\\1</blockquote></div>", $Text);
  307. $Text=preg_replace("/\[code\](.+?)\[\/code\]/eis","highlight_code('\\1')", $Text);
  308. $Text=preg_replace("/\[php\](.+?)\[\/php\]/eis","highlight_code('\\1')", $Text);
  309. $Text=preg_replace("/\[sig\](.+?)\[\/sig\]/is","<div class='sign'>\\1</div>", $Text);
  310. $Text=preg_replace("/\\n/is","<br/>",$Text);
  311. return $Text;
  312. }
  313. // ?????????
  314. function build_count_rand ($number,$length=4,$mode=1) {
  315. if($mode==1 && $length<strlen($number) ) {
  316. //???????????????
  317. return false;
  318. }
  319. $rand = array();
  320. for($i=0; $i<$number; $i++) {
  321. $rand[] = rand_string($length,$mode);
  322. }
  323. $unqiue = array_unique($rand);
  324. if(count($unqiue)==count($rand)) {
  325. return $rand;
  326. }
  327. $count = count($rand)-count($unqiue);
  328. for($i=0; $i<$count*3; $i++) {
  329. $rand[] = rand_string($length,$mode);
  330. }
  331. $rand = array_slice(array_unique ($rand),0,$number);
  332. return $rand;
  333. }
  334. function remove_xss($val) {
  335. // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
  336. // this prevents some character re-spacing such as <java\0script>
  337. // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
  338. $val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);
  339. // straight replacements, the user should never need these since they're normal characters
  340. // this prevents like <IMG SRC=@avascript:alert('XSS')>
  341. $search = 'abcdefghijklmnopqrstuvwxyz';
  342. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  343. $search .= '1234567890!@#$%^&*()';
  344. $search .= '~`";:?+/={}[]-_|\'\\';
  345. for ($i = 0; $i < strlen($search); $i++) {
  346. // ;? matches the ;, which is optional
  347. // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
  348. // @ @ search for the hex values
  349. $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
  350. // @ @ 0{0,7} matches '0' zero to seven times
  351. $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
  352. }
  353. // now the only remaining whitespace attacks are \t, \n, and \r
  354. $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
  355. $ra2 = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
  356. $ra = array_merge($ra1, $ra2);
  357. $found = true; // keep replacing as long as the previous round replaced something
  358. while ($found == true) {
  359. $val_before = $val;
  360. for ($i = 0; $i < sizeof($ra); $i++) {
  361. $pattern = '/';
  362. for ($j = 0; $j < strlen($ra[$i]); $j++) {
  363. if ($j > 0) {
  364. $pattern .= '(';
  365. $pattern .= '(&#[xX]0{0,8}([9ab]);)';
  366. $pattern .= '|';
  367. $pattern .= '|(&#0{0,8}([9|10|13]);)';
  368. $pattern .= ')*';
  369. }
  370. $pattern .= $ra[$i][$j];
  371. }
  372. $pattern .= '/i';
  373. $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
  374. $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
  375. if ($val_before == $val) {
  376. // no replacements were made, so exit the loop
  377. $found = false;
  378. }
  379. }
  380. }
  381. return $val;
  382. }
  383. /**
  384. +----------------------------------------------------------
  385. * ??????????Tree
  386. +----------------------------------------------------------
  387. * @access public
  388. +----------------------------------------------------------
  389. * @param array $list ???????
  390. * @param string $pid parent????
  391. * @param string $level level????
  392. +----------------------------------------------------------
  393. * @return array
  394. +----------------------------------------------------------
  395. */
  396. function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0)
  397. {
  398. // ??Tree
  399. $tree = array();
  400. if(is_array($list)) {
  401. // ???????????
  402. $refer = array();
  403. foreach ($list as $key => $data) {
  404. $refer[$data[$pk]] =& $list[$key];
  405. }
  406. foreach ($list as $key => $data) {
  407. // ??????parent
  408. $parentId = $data[$pid];
  409. if ($root == $parentId) {
  410. $tree[] =& $list[$key];
  411. }else{
  412. if (isset($refer[$parentId])) {
  413. $parent =& $refer[$parentId];
  414. $parent[$child][] =& $list[$key];
  415. }
  416. }
  417. }
  418. }
  419. return $tree;
  420. }
  421. /**
  422. +----------------------------------------------------------
  423. * ??????????
  424. +----------------------------------------------------------
  425. * @access public
  426. +----------------------------------------------------------
  427. * @param array $list ????
  428. * @param string $field ??????
  429. * @param array $sortby ????
  430. * asc???? desc???? nat????
  431. +----------------------------------------------------------
  432. * @return array
  433. +----------------------------------------------------------
  434. */
  435. function list_sort_by($list,$field, $sortby='asc') {
  436. if(is_array($list)){
  437. $refer = $resultSet = array();
  438. foreach ($list as $i => $data)
  439. $refer[$i] = &$data[$field];
  440. switch ($sortby) {
  441. case 'asc': // ????
  442. asort($refer);
  443. break;
  444. case 'desc':// ????
  445. arsort($refer);
  446. break;
  447. case 'nat': // ????
  448. natcasesort($refer);
  449. break;
  450. }
  451. foreach ( $refer as $key=> $val)
  452. $resultSet[] = &$list[$key];
  453. return $resultSet;
  454. }
  455. return false;
  456. }
  457. /**
  458. +----------------------------------------------------------
  459. * ????????
  460. +----------------------------------------------------------
  461. * @access public
  462. +----------------------------------------------------------
  463. * @param array $list ????
  464. * @param mixed $condition ????
  465. * ?? array('name'=>$value) ?? name=$value
  466. +----------------------------------------------------------
  467. * @return array
  468. +----------------------------------------------------------
  469. */
  470. function list_search($list,$condition) {
  471. if(is_string($condition))
  472. parse_str($condition,$condition);
  473. // ???????
  474. $resultSet = array();
  475. foreach ($list as $key=>$data){
  476. $find = false;
  477. foreach ($condition as $field=>$value){
  478. if(isset($data[$field])) {
  479. if(0 === strpos($value,'/')) {
  480. $find = preg_match($value,$data[$field]);
  481. }elseif($data[$field]==$value){
  482. $find = true;
  483. }
  484. }
  485. }
  486. if($find)
  487. $resultSet[] = &$list[$key];
  488. }
  489. return $resultSet;
  490. }
  491. // ??Http????
  492. function send_http_status($status) {
  493. static $_status = array(
  494. // Informational 1xx
  495. 100 => 'Continue',
  496. 101 => 'Switching Protocols',
  497. // Success 2xx
  498. 200 => 'OK',
  499. 201 => 'Created',
  500. 202 => 'Accepted',
  501. 203 => 'Non-Authoritative Information',
  502. 204 => 'No Content',
  503. 205 => 'Reset Content',
  504. 206 => 'Partial Content',
  505. // Redirection 3xx
  506. 300 => 'Multiple Choices',
  507. 301 => 'Moved Permanently',
  508. 302 => 'Moved Temporarily ', // 1.1
  509. 303 => 'See Other',
  510. 304 => 'Not Modified',
  511. 305 => 'Use Proxy',
  512. // 306 is deprecated but reserved
  513. 307 => 'Temporary Redirect',
  514. // Client Error 4xx
  515. 400 => 'Bad Request',
  516. 401 => 'Unauthorized',
  517. 402 => 'Payment Required',
  518. 403 => 'Forbidden',
  519. 404 => 'Not Found',
  520. 405 => 'Method Not Allowed',
  521. 406 => 'Not Acceptable',
  522. 407 => 'Proxy Authentication Required',
  523. 408 => 'Request Timeout',
  524. 409 => 'Conflict',
  525. 410 => 'Gone',
  526. 411 => 'Length Required',
  527. 412 => 'Precondition Failed',
  528. 413 => 'Request Entity Too Large',
  529. 414 => 'Request-URI Too Long',
  530. 415 => 'Unsupported Media Type',
  531. 416 => 'Requested Range Not Satisfiable',
  532. 417 => 'Expectation Failed',
  533. // Server Error 5xx
  534. 500 => 'Internal Server Error',
  535. 501 => 'Not Implemented',
  536. 502 => 'Bad Gateway',
  537. 503 => 'Service Unavailable',
  538. 504 => 'Gateway Timeout',
  539. 505 => 'HTTP Version Not Supported',
  540. 509 => 'Bandwidth Limit Exceeded'
  541. );
  542. if(array_key_exists($code,$_status)) {
  543. header('HTTP/1.1 '.$code.' '.$_status[$code]);
  544. }
  545. }
  546. ?>