PageRenderTime 32ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/classes/func.php

http://swan.googlecode.com/
PHP | 822 lines | 558 code | 128 blank | 136 comment | 120 complexity | ec1a74f9210a23f14b7f2a54b9848a58 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. defined('SW_') or die('Access Error');
  3. /*=====================================================================
  4. * ???????????????????????????????
  5. ======================================================================*/
  6. class SWFunc{
  7. /*=====================================================================
  8. * ????????????0
  9. ======================================================================*/
  10. function checkInt($int){
  11. if(is_numeric($int)){
  12. return intval($int);
  13. }else{
  14. return 0;
  15. }
  16. }
  17. /*=====================================================================
  18. * ?????????????''
  19. ======================================================================*/
  20. function checkString($str){
  21. if($str){
  22. return trim(strval($str));
  23. }else{
  24. return '';
  25. }
  26. }
  27. /*=====================================================================
  28. * ??????????????
  29. * ??????$_GET,$_POST??????????????????
  30. ======================================================================*/
  31. function filterString($str){
  32. if($str){
  33. $str=stripslashes($str);
  34. $str=addslashes($str);
  35. return $str;
  36. }else{
  37. return '';
  38. }
  39. }
  40. /*=====================================================================
  41. * ???????????????????????time()
  42. ======================================================================*/
  43. function checkToTimestamp($str){
  44. return strtotime($str)?strtotime($str):time();
  45. }
  46. /*=====================================================================
  47. * ??html??????????????$length?0?????
  48. ======================================================================*/
  49. function closeHtml($str,$length=0,$plus=''){
  50. $str=self::checkString(kses($str,array()));
  51. if($length>0){
  52. $str=self::sliceStr($str,$length,$plus);
  53. }
  54. return $str;
  55. }
  56. /*=====================================================================
  57. * ?????????html??
  58. * ??????????????
  59. ======================================================================*/
  60. function closeTags($html){
  61. /*?????? < ??????????????HTML???? > ??*/
  62. $html = preg_replace("~<[^<>]+?$~i", "", $html);
  63. /*??????????HTML??*/
  64. #put all opened tags into an array
  65. preg_match_all("#<([a-z]+)( .*[^/])?(?!/)>#iU", $html, $result);
  66. $openedtags = $result[1];
  67. #put all closed tags into an array
  68. preg_match_all("#</([a-z]+)>#iU", $html, $result);
  69. $closedtags = $result[1];
  70. $len_opened = count($openedtags);
  71. # all tags are closed
  72. if (count($closedtags) == $len_opened)
  73. {
  74. return $html;
  75. }
  76. $openedtags = array_reverse($openedtags);
  77. # close tags
  78. for ($i = 0; $i < $len_opened; $i++)
  79. {
  80. if (!in_array($openedtags[$i], $closedtags))
  81. {
  82. $html .= '</' . $openedtags[$i] . '>';
  83. } else
  84. {
  85. unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  86. }
  87. }
  88. return $html;
  89. }
  90. /*=====================================================================
  91. * ??????????html??????????<br />
  92. ======================================================================*/
  93. function transText($str){
  94. $str=self::closeHtml($str);
  95. $str=str_replace("\r\n","\n",$str);
  96. $str=str_replace("\r","\n",$str);
  97. $str=preg_replace("/\n{2,}/i","\n\n", $str);
  98. $str=str_replace("\n",'<br />',$str);
  99. return $str;
  100. }
  101. /*=====================================================================
  102. * UBB????
  103. ======================================================================*/
  104. function transUbb($Text){
  105. $Text=htmlspecialchars($Text);
  106. $Text=stripslashes($Text);
  107. $Text=ereg_replace("\r\n","\n",$Text);
  108. $Text=ereg_replace("\r","\n",$Text);
  109. $Text=nl2br($Text);
  110. $Text=preg_replace("/\\t/is","??",$Text);
  111. //$Text=ereg_replace(" ","&nbsp;",$Text);
  112. $Text=preg_replace("/\[h1\](.+?)\[\/h1\]/is","<h1>\\1</h1>",$Text);
  113. $Text=preg_replace("/\[h2\](.+?)\[\/h2\]/is","<h2>\\1</h2>",$Text);
  114. $Text=preg_replace("/\[h3\](.+?)\[\/h3\]/is","<h3>\\1</h3>",$Text);
  115. $Text=preg_replace("/\[h4\](.+?)\[\/h4\]/is","<h4>\\1</h4>",$Text);
  116. $Text=preg_replace("/\[h5\](.+?)\[\/h5\]/is","<h5>\\1</h5>",$Text);
  117. $Text=preg_replace("/\[h6\](.+?)\[\/h6\]/is","<h6>\\1</h6>",$Text);
  118. $Text=preg_replace("/\[center\](.+?)\[\/center\]/is","<center>\\1</center>",$Text);
  119. $Text=preg_replace("/\[big\](.+?)\[\/big\]/is","<big>\\1</big>",$Text);
  120. $Text=preg_replace("/\[small\](.+?)\[\/small\]/is","<small>\\1</small>",$Text);
  121. $Text=preg_replace("/\[url\](http:\/\/.+?)\[\/url\]/is","<a href=\"\\1\">\\1</a>",$Text);
  122. $Text=preg_replace("/\[url\](.+?)\[\/url\]/is","<a href=\"http://\\1\">http://\\1</a>",$Text);
  123. $Text=preg_replace("/\[url=(http:\/\/.+?)\](.*)\[\/url\]/is","<a href=\"\\1\">\\2</a>",$Text);
  124. $Text=preg_replace("/\[url=(.+?)\](.*)\[\/url\]/is","<a href=\"http://\\1\">\\2</a>",$Text);
  125. $Text=preg_replace("/\[img\](.+?)\[\/img\]/is","<img src=\"\\1\" border=\"0\" />",$Text);
  126. $Text=preg_replace("/\[color=(.+?)\](.+?)\[\/color\]/is","<font color=\"\\1\">\\2</font>",$Text);
  127. $Text=preg_replace("/\[size=(.+?)\](.+?)\[\/size\]/is","<font size=\"\\1\">\\2</font>",$Text);
  128. $Text=preg_replace("/\[sup\](.+?)\[\/sup\]/is","<sup>\\1</sup>",$Text);
  129. $Text=preg_replace("/\[sub\](.+?)\[\/sub\]/is","<sub>\\1</sub>",$Text);
  130. $Text=preg_replace("/\[pre\](.+?)\[\/pre\]/is","<pre>\\1</pre>",$Text);
  131. $Text=preg_replace("/\[email\](.+?)\[\/email\]/is","<a href=\"mailto:\\1\">\\1</a>",$Text);
  132. $Text=preg_replace("/\[i\](.+?)\[\/i\]/is","<i>\\1</i>",$Text);
  133. $Text=preg_replace("/\[b\](.+?)\[\/b\]/is","<b>\\1</b>",$Text);
  134. $Text=preg_replace("/\[quote\](.+?)\[\/quote\]/is","<blockquote><font size=\"1\" face=\"Courier New\">quote:</font><hr>\\1<hr></blockquote>", $Text);
  135. $Text=preg_replace("/\[code\](.+?)\[\/code\]/is","<blockquote><font size=\"1\" face=\"Times New Roman\">code:</font><hr color=\"lightblue\"><i>\\1</i><hr color=\"lightblue\"></blockquote>", $Text);
  136. $Text=preg_replace("/\[sig\](.+?)\[\/sig\]/is","<div style=\"text-align: left; color: darkgreen; margin-left: 5%;\"><br><br>--------------------------<br />\\1<br />--------------------------</div>", $Text);
  137. $Text=ereg_replace("\[hr\]","<hr />",$Text);
  138. return $Text;
  139. }
  140. /*=====================================================================
  141. * ??????????
  142. ======================================================================*/
  143. function strLength($str,$charset='utf-8') {
  144. $n = 0; $p = 0; $c = '';
  145. $len = strlen($str);
  146. if($charset == 'utf-8') {
  147. for($i = 0; $i < $len; $i++) {
  148. $c = ord($str{$i});
  149. if($c > 252) {
  150. $p = 5;
  151. } elseif($c > 248) {
  152. $p = 4;
  153. } elseif($c > 240) {
  154. $p = 3;
  155. } elseif($c > 224) {
  156. $p = 2;
  157. } elseif($c > 192) {
  158. $p = 1;
  159. } else {
  160. $p = 0;
  161. }
  162. $i+=$p;$n++;
  163. }
  164. } else {
  165. for($i = 0; $i < $len; $i++) {
  166. $c = ord($str{$i});
  167. if($c > 127) {
  168. $p = 1;
  169. } else {
  170. $p = 0;
  171. }
  172. $i+=$p;$n++;
  173. }
  174. }
  175. return $n;
  176. }
  177. /*=====================================================================
  178. * ???????????ANSI??
  179. ======================================================================*/
  180. function sliceStr($string, $sublen ,$plus=''){
  181. $start = 0;
  182. $code = 'UTF-8';
  183. if($code == 'UTF-8')
  184. {
  185. $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
  186. preg_match_all($pa, $string, $t_string);
  187. if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen)).$plus;
  188. return join('', array_slice($t_string[0], $start, $sublen));
  189. }
  190. else
  191. {
  192. $start = $start*2;
  193. $sublen = $sublen*2;
  194. $strlen = strlen($string);
  195. $tmpstr = '';
  196. for($i=0; $i< $strlen; $i++)
  197. {
  198. if($i>=$start && $i< ($start+$sublen))
  199. {
  200. if(ord(substr($string, $i, 1))>129)
  201. {
  202. $tmpstr.= substr($string, $i, 2);
  203. }
  204. else
  205. {
  206. $tmpstr.= substr($string, $i, 1);
  207. }
  208. }
  209. if(ord(substr($string, $i, 1))>129) $i++;
  210. }
  211. if(strlen($tmpstr)< $strlen ) $tmpstr.= $plus;
  212. return $tmpstr;
  213. }
  214. }
  215. /*=====================================================================
  216. * ??email?????true?false
  217. ======================================================================*/
  218. function checkEmail($email) {
  219. // First, we check that there's one @ symbol, and that the lengths are right
  220. if (!ereg("[^@]{1,64}@[^@]{1,255}", $email)) {
  221. // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
  222. return false;
  223. }
  224. // Split it into sections to make life easier
  225. $email_array = explode("@", $email);
  226. $local_array = explode(".", $email_array[0]);
  227. for ($i = 0; $i < sizeof($local_array); $i++) {
  228. if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
  229. return false;
  230. }
  231. }
  232. // Check if domain is IP. If not, it should be valid domain name
  233. if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
  234. $domain_array = explode(".", $email_array[1]);
  235. if (sizeof($domain_array) < 2) {
  236. // Not enough parts to domain
  237. return false;
  238. }
  239. for ($i = 0; $i < sizeof($domain_array); $i++) {
  240. if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
  241. return false;
  242. }
  243. }
  244. }
  245. return true;
  246. }
  247. /*=====================================================================
  248. * ??url??????????????
  249. * ?????????????
  250. * ??addSlash??????????url??????
  251. * ??delQuery?????????#???????
  252. ======================================================================*/
  253. function checkUrl($url,$addSlash=false,$delQuery=false) {
  254. $url=self::closeHtml($url);
  255. if($url){
  256. //????????
  257. $url=str_replace('\\','/',$url);
  258. //????
  259. $regex = "((https?)\:\/\/)?"; // SCHEME
  260. $regex .= "([a-z0-9-\.]+)"; // Host or IP
  261. $regex .= "(\:[0-9]{2,5})?"; // Port
  262. $regex .= "((\/([a-z0-9+\$_-]\.?)+)*(\/?))"; // Path
  263. $regex .= "((\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
  264. $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?)"; // Anchor
  265. $matchs=array();
  266. if(preg_match("/^$regex$/i", $url, $matchs)){
  267. //??host????
  268. $host=empty($matchs[3])?'':$matchs[3];
  269. if(!preg_match("/^([a-z0-9-.]*)\.([a-z]{2,3})$/i",$host) && !preg_match("/^localhost$/i",$host) && !preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/",$host)){
  270. $url='';
  271. }else{
  272. //?????http://?https://??
  273. if(empty($matchs[1])) $url='http://'.$url;
  274. //????host???path????????
  275. if($addSlash && empty($matchs[5]) && empty($matchs[9])){
  276. $url.='/';
  277. //????????????????????
  278. }elseif($addSlash && empty($matchs[9]) && empty($matchs[8]) && $matchs[5]!='/' && !preg_match("/\./",$matchs[6])){
  279. $url.='/';
  280. }
  281. //??????
  282. if($delQuery && !empty($matchs[9])){
  283. $url=str_replace($matchs[9],'',$url);
  284. }
  285. }
  286. }else{
  287. $url='';
  288. }
  289. }
  290. return $url;
  291. }
  292. /*=====================================================================
  293. * ???????????????????level
  294. * min?max??????????0????
  295. ======================================================================*/
  296. function checkCharacter($str,$level=1,$min=0,$max=0){
  297. if(!$str) return false;
  298. //????
  299. if($min && self::strLength($str)<$min) return false;
  300. if($max && self::strLength($str)>$max) return false;
  301. //??????????
  302. switch($level){
  303. //???????????????????
  304. case 1:
  305. if(preg_match("/[^\w\x{3400}-\x{4DBF}\x{4E00}-\x{9FAF}]/u",$str)) return false;
  306. break;
  307. //????????
  308. case 2:
  309. if(preg_match("/[^\x20-\x7e]/",$str)) return false;
  310. break;
  311. //??0-9a-zA-Z_??????account??
  312. case 3:
  313. if(preg_match("/[^\d\w\_]/",$str)) return false;
  314. break;
  315. }
  316. return true;
  317. }
  318. /*=====================================================================
  319. * ???????????url?????
  320. * ??/var/www/site/a.php ?? /site/a.php
  321. * ???$path??????????
  322. ======================================================================*/
  323. function getUrlByPath($path){
  324. $url='/'.substr($path,strlen($_SERVER['DOCUMENT_ROOT']));
  325. $url=str_replace('\\','/',$url);
  326. $url=str_replace('//','/',$url);
  327. return $url;
  328. }
  329. /*=====================================================================
  330. * ??????????
  331. * type???????????php??
  332. * ??????/??
  333. ======================================================================*/
  334. function getFiles($dir,$filetype='php') {
  335. $arr=array();
  336. //???????
  337. if (is_dir($dir)){
  338. //??????????
  339. if ($dh = opendir($dir)) {
  340. while (($file = readdir($dh)) !== false) {
  341. if(filetype($dir . $file)!='file') continue;
  342. //??????
  343. $arrTmp=explode('.',$file);
  344. if(count($arrTmp)>1){
  345. $type=$arrTmp[count($arrTmp)-1];
  346. array_pop($arrTmp);
  347. $name=implode('.',$arrTmp);
  348. }else{
  349. $type='';
  350. $name=$arrTmp[0];
  351. }
  352. if(!$filetype || $filetype==$type){
  353. $arr[]=array('name'=>$name,'type'=>$type);
  354. }
  355. }
  356. closedir($dh);
  357. }
  358. }
  359. return $arr;
  360. }
  361. /*=====================================================================
  362. * ???????????
  363. * ??????/??
  364. ======================================================================*/
  365. function getDirs($dir) {
  366. $arr=array();
  367. //???????
  368. if (is_dir($dir)){
  369. //??????????
  370. if ($dh = opendir($dir)) {
  371. while (($file = readdir($dh)) !== false) {
  372. //?????????
  373. if(filetype($dir . $file)!='dir') continue;
  374. //???.??..???
  375. if($file=='.' || $file=='..') continue;
  376. $arr[]=$file;
  377. }
  378. closedir($dh);
  379. }
  380. }
  381. return $arr;
  382. }
  383. /*=====================================================================
  384. * ??????php?????????????"\"?
  385. ======================================================================*/
  386. function stripslashes_deep($value){
  387. return is_array($value)?array_map(array('self','stripslashes_deep'), $value):stripslashes($value);
  388. }
  389. /*=====================================================================
  390. * ?????????stripslashes_deep?????
  391. ======================================================================*/
  392. function addslashes_deep($value){
  393. return is_array($value)?array_map(array('self','addslashes_deep'), $value):addslashes($value);
  394. }
  395. /*=====================================================================
  396. * ?xml????array???
  397. ======================================================================*/
  398. function xml2array($url, $get_attributes = 1, $priority = 'tag')
  399. {
  400. $contents = "";
  401. if (!function_exists('xml_parser_create'))
  402. {
  403. return array ();
  404. }
  405. $parser = xml_parser_create('');
  406. if (!($fp = @ fopen($url, 'rb')))
  407. {
  408. return array ();
  409. }
  410. while (!feof($fp))
  411. {
  412. $contents .= fread($fp, 8192);
  413. }
  414. fclose($fp);
  415. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
  416. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  417. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  418. xml_parse_into_struct($parser, trim($contents), $xml_values);
  419. xml_parser_free($parser);
  420. if (!$xml_values)
  421. return; //Hmm...
  422. $xml_array = array ();
  423. $parents = array ();
  424. $opened_tags = array ();
  425. $arr = array ();
  426. $current = & $xml_array;
  427. $repeated_tag_index = array ();
  428. foreach ($xml_values as $data)
  429. {
  430. unset ($attributes, $value);
  431. extract($data);
  432. $result = array ();
  433. $attributes_data = array ();
  434. if (isset ($value))
  435. {
  436. if ($priority == 'tag')
  437. $result = $value;
  438. else
  439. $result['value'] = $value;
  440. }
  441. if (isset ($attributes) and $get_attributes)
  442. {
  443. foreach ($attributes as $attr => $val)
  444. {
  445. if ($priority == 'tag')
  446. $attributes_data[$attr] = $val;
  447. else
  448. $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  449. }
  450. }
  451. if ($type == "open")
  452. {
  453. $parent[$level -1] = & $current;
  454. if (!is_array($current) or (!in_array($tag, array_keys($current))))
  455. {
  456. $current[$tag] = $result;
  457. if ($attributes_data)
  458. $current[$tag . '_attr'] = $attributes_data;
  459. $repeated_tag_index[$tag . '_' . $level] = 1;
  460. $current = & $current[$tag];
  461. }
  462. else
  463. {
  464. if (isset ($current[$tag][0]))
  465. {
  466. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  467. $repeated_tag_index[$tag . '_' . $level]++;
  468. }
  469. else
  470. {
  471. $current[$tag] = array (
  472. $current[$tag],
  473. $result
  474. );
  475. $repeated_tag_index[$tag . '_' . $level] = 2;
  476. if (isset ($current[$tag . '_attr']))
  477. {
  478. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  479. unset ($current[$tag . '_attr']);
  480. }
  481. }
  482. $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
  483. $current = & $current[$tag][$last_item_index];
  484. }
  485. }
  486. elseif ($type == "complete")
  487. {
  488. if (!isset ($current[$tag]))
  489. {
  490. $current[$tag] = $result;
  491. $repeated_tag_index[$tag . '_' . $level] = 1;
  492. if ($priority == 'tag' and $attributes_data)
  493. $current[$tag . '_attr'] = $attributes_data;
  494. }
  495. else
  496. {
  497. if (isset ($current[$tag][0]) and is_array($current[$tag]))
  498. {
  499. $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
  500. if ($priority == 'tag' and $get_attributes and $attributes_data)
  501. {
  502. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  503. }
  504. $repeated_tag_index[$tag . '_' . $level]++;
  505. }
  506. else
  507. {
  508. $current[$tag] = array (
  509. $current[$tag],
  510. $result
  511. );
  512. $repeated_tag_index[$tag . '_' . $level] = 1;
  513. if ($priority == 'tag' and $get_attributes)
  514. {
  515. if (isset ($current[$tag . '_attr']))
  516. {
  517. $current[$tag]['0_attr'] = $current[$tag . '_attr'];
  518. unset ($current[$tag . '_attr']);
  519. }
  520. if ($attributes_data)
  521. {
  522. $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
  523. }
  524. }
  525. $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
  526. }
  527. }
  528. }
  529. elseif ($type == 'close')
  530. {
  531. $current = & $parent[$level -1];
  532. }
  533. }
  534. return ($xml_array);
  535. }
  536. /*=====================================================================
  537. * ?????????????
  538. * ????????????????????
  539. ======================================================================*/
  540. function obj2array($obj){
  541. if(is_array($obj)){
  542. $arr=array();
  543. foreach($obj as $key=>$value){
  544. $arr[$key]=self::obj2array($value);
  545. }
  546. return $arr;
  547. }elseif(is_object($obj)){
  548. $arr=get_object_vars($obj);
  549. foreach($arr as $key=>$value){
  550. $arr[$key]=self::obj2array($value);
  551. }
  552. return $arr;
  553. }else{
  554. return $obj;
  555. }
  556. }
  557. /*=====================================================================
  558. * ???????IP
  559. ======================================================================*/
  560. function getIp(){
  561. $ip='';
  562. if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  563. $ip = $_SERVER["HTTP_CLIENT_IP"];
  564. }
  565. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  566. $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  567. if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
  568. for ($i = 0; $i < count($ips); $i++) {
  569. if (!eregi ("^(10?172.16?192.168).", $ips[$i])) {
  570. $ip = $ips[$i];
  571. break;
  572. }
  573. }
  574. }
  575. return trim($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  576. }
  577. /*=====================================================================
  578. * ???????IP
  579. ======================================================================*/
  580. function is_internal_ip($ip){
  581. $ip = ip2long($ip);
  582. //??ip????????true
  583. if(!$ip) return true;
  584. $net_a = ip2long('10.255.255.255') >> 24; //A????ip?????
  585. $net_b = ip2long('172.31.255.255') >> 20; //B????ip?????
  586. $net_c = ip2long('192.168.255.255') >> 16; //C????ip?????
  587. return $ip >> 24 === $net_a || $ip >> 20 === $net_b || $ip >> 16 === $net_c;
  588. }
  589. /*=====================================================================
  590. * ???????URL
  591. ======================================================================*/
  592. function is_internal_url($url){
  593. $url=self::checkUrl($url);
  594. if(preg_match("/^((https?)\:\/\/)?([a-z0-9-\.]+)(\:[0-9]{2,5})?\/?/i", $url, $matchs)){
  595. $host=empty($matchs[3])?'':$matchs[3];
  596. //??????????
  597. if(preg_match("/^([a-z0-9-.]*)\.([a-z]{2,3})$/i", $host)) return false;
  598. //??????IP
  599. if(preg_match("/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" .
  600. "(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/", $host)){
  601. //???127???IP
  602. if(!preg_match("/^127\./i", $host) && !self::is_internal_ip($host)) return false;
  603. }
  604. }
  605. return true;
  606. }
  607. /*=====================================================================
  608. * ???????
  609. ======================================================================*/
  610. function randomString($len=8, $pool='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'){
  611. $str='';
  612. for($i=0; $i < $len; $i++){
  613. $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  614. }
  615. return $str;
  616. }
  617. /*=====================================================================
  618. * ??uuid???????
  619. ======================================================================*/
  620. function uuid($key = '', $prefix = ''){
  621. if(!$key) $key = uniqid(rand());
  622. $chars = md5($key);
  623. $uuid = substr($chars,0,8) . '-';
  624. $uuid .= substr($chars,8,4) . '-';
  625. $uuid .= substr($chars,12,4) . '-';
  626. $uuid .= substr($chars,16,4) . '-';
  627. $uuid .= substr($chars,20,12);
  628. return $prefix . $uuid;
  629. }
  630. /*=====================================================================
  631. * ????????????
  632. * -1????1????0???
  633. * ??????v1?v2
  634. * ????"1.0.1 beta2"
  635. ======================================================================*/
  636. function compareVersion($v1,$v2){
  637. $v1=strtolower(trim($v1));
  638. $v2=strtolower(trim($v2));
  639. if($v1==$v2) return 0;
  640. //??????
  641. $regex='([\d\.]+?)(\s+(dev|alpha|beta)(\d+)?)?';
  642. preg_match("/^$regex$/i", $v1, $matchs1);
  643. preg_match("/^$regex$/i", $v2, $matchs2);
  644. //????????????
  645. if(!$matchs1 && !$matchs2){
  646. return 0;
  647. }elseif(!$matchs1 && $matchs2){
  648. return -1;
  649. }elseif($matchs1 && !$matchs2){
  650. return 1;
  651. }
  652. //?????
  653. $main1=explode('.',$matchs1[1]);
  654. $main2=explode('.',$matchs2[1]);
  655. for($i=0;$i<3;$i++){
  656. $main1[$i]=empty($main1[$i])?0:SWFunc::checkInt($main1[$i]);
  657. $main2[$i]=empty($main2[$i])?0:SWFunc::checkInt($main2[$i]);
  658. if($main1[$i]<$main2[$i]) return -1;
  659. if($main1[$i]>$main2[$i]) return 1;
  660. }
  661. //????
  662. $plus1=empty($matchs1[3])?'3':$matchs1[3];
  663. $plus2=empty($matchs2[3])?'3':$matchs2[3];
  664. $plus1=str_replace('dev','0',$plus1);
  665. $plus2=str_replace('dev','0',$plus2);
  666. $plus1=str_replace('alpha','1',$plus1);
  667. $plus2=str_replace('alpha','1',$plus2);
  668. $plus1=str_replace('beta','2',$plus1);
  669. $plus2=str_replace('beta','2',$plus2);
  670. $plus1=SWFunc::checkInt($plus1);
  671. $plus2=SWFunc::checkInt($plus2);
  672. if($plus1<$plus2) return -1;
  673. if($plus1>$plus2) return 1;
  674. //??????
  675. $plus1=empty($matchs1[4])?0:SWFunc::checkInt($matchs1[4]);
  676. $plus2=empty($matchs2[4])?0:SWFunc::checkInt($matchs2[4]);
  677. if($plus1<$plus2) return -1;
  678. if($plus1>$plus2) return 1;
  679. return 0;
  680. }
  681. /*=====================================================================
  682. * ??????timezone??
  683. ======================================================================*/
  684. function timezoneList(){
  685. return array("Africa/Abidjan","Africa/Accra","Africa/Addis_Ababa","Africa/Algiers","Africa/Asmara","Africa/Asmera","Africa/Bamako","Africa/Bangui","Africa/Banjul","Africa/Bissau","Africa/Blantyre","Africa/Brazzaville","Africa/Bujumbura","Africa/Cairo","Africa/Casablanca","Africa/Ceuta","Africa/Conakry","Africa/Dakar","Africa/Dar_es_Salaam","Africa/Djibouti","Africa/Douala","Africa/El_Aaiun","Africa/Freetown","Africa/Gaborone","Africa/Harare","Africa/Johannesburg","Africa/Kampala","Africa/Khartoum","Africa/Kigali","Africa/Kinshasa","Africa/Lagos","Africa/Libreville","Africa/Lome","Africa/Luanda","Africa/Lubumbashi","Africa/Lusaka","Africa/Malabo","Africa/Maputo","Africa/Maseru","Africa/Mbabane","Africa/Mogadishu","Africa/Monrovia","Africa/Nairobi","Africa/Ndjamena","Africa/Niamey","Africa/Nouakchott","Africa/Ouagadougou","Africa/Porto-Novo","Africa/Sao_Tome","Africa/Timbuktu","Africa/Tripoli","Africa/Tunis","Africa/Windhoek","America/Adak","America/Anchorage","America/Anguilla","America/Antigua","America/Araguaina","America/Argentina/Buenos_Aires","America/Argentina/Catamarca","America/Argentina/ComodRivadavia","America/Argentina/Cordoba","America/Argentina/Jujuy","America/Argentina/La_Rioja","America/Argentina/Mendoza","America/Argentina/Rio_Gallegos","America/Argentina/San_Juan","America/Argentina/San_Luis","America/Argentina/Tucuman","America/Argentina/Ushuaia","America/Aruba","America/Asuncion","America/Atikokan","America/Atka","America/Bahia","America/Barbados","America/Belem","America/Belize","America/Blanc-Sablon","America/Boa_Vista","America/Bogota","America/Boise","America/Buenos_Aires","America/Cambridge_Bay","America/Campo_Grande","America/Cancun","America/Caracas","America/Catamarca","America/Cayenne","America/Cayman","America/Chicago","America/Chihuahua","America/Coral_Harbour","America/Cordoba","America/Costa_Rica","America/Cuiaba","America/Curacao","America/Danmarkshavn","America/Dawson","America/Dawson_Creek","America/Denver","America/Detroit","America/Dominica","America/Edmonton","America/Eirunepe","America/El_Salvador","America/Ensenada","America/Fort_Wayne","America/Fortaleza","America/Glace_Bay","America/Godthab","America/Goose_Bay","America/Grand_Turk","America/Grenada","America/Guadeloupe","America/Guatemala","America/Guayaquil","America/Guyana","America/Halifax","America/Havana","America/Hermosillo","America/Indiana/Indianapolis","America/Indiana/Knox","America/Indiana/Marengo","America/Indiana/Petersburg","America/Indiana/Tell_City","America/Indiana/Vevay","America/Indiana/Vincennes","America/Indiana/Winamac","America/Indianapolis","America/Inuvik","America/Iqaluit","America/Jamaica","America/Jujuy","America/Juneau","America/Kentucky/Louisville","America/Kentucky/Monticello","America/Knox_IN","America/La_Paz","America/Lima","America/Los_Angeles","America/Louisville","America/Maceio","America/Managua","America/Manaus","America/Marigot","America/Martinique","America/Mazatlan","America/Mendoza","America/Menominee","America/Merida","America/Mexico_City","America/Miquelon","America/Moncton","America/Monterrey","America/Montevideo","America/Montreal","America/Montserrat","America/Nassau","America/New_York","America/Nipigon","America/Nome","America/Noronha","America/North_Dakota/Center","America/North_Dakota/New_Salem","America/Panama","America/Pangnirtung","America/Paramaribo","America/Phoenix","America/Port-au-Prince","America/Port_of_Spain","America/Porto_Acre","America/Porto_Velho","America/Puerto_Rico","America/Rainy_River","America/Rankin_Inlet","America/Recife","America/Regina","America/Resolute","America/Rio_Branco","America/Rosario","America/Santiago","America/Santo_Domingo","America/Sao_Paulo","America/Scoresbysund","America/Shiprock","America/St_Barthelemy","America/St_Johns","America/St_Kitts","America/St_Lucia","America/St_Thomas","America/St_Vincent","America/Swift_Current","America/Tegucigalpa","America/Thule","America/Thunder_Bay","America/Tijuana","America/Toronto","America/Tortola","America/Vancouver","America/Virgin","America/Whitehorse","America/Winnipeg","America/Yakutat","America/Yellowknife","Antarctica/Casey","Antarctica/Davis","Antarctica/DumontDUrville","Antarctica/Mawson","Antarctica/McMurdo","Antarctica/Palmer","Antarctica/Rothera","Antarctica/South_Pole","Antarctica/Syowa","Antarctica/Vostok","Arctic/Longyearbyen","Asia/Aden","Asia/Almaty","Asia/Amman","Asia/Anadyr","Asia/Aqtau","Asia/Aqtobe","Asia/Ashgabat","Asia/Ashkhabad","Asia/Baghdad","Asia/Bahrain","Asia/Baku","Asia/Bangkok","Asia/Beirut","Asia/Bishkek","Asia/Brunei","Asia/Calcutta","Asia/Choibalsan","Asia/Chongqing","Asia/Chungking","Asia/Colombo","Asia/Dacca","Asia/Damascus","Asia/Dhaka","Asia/Dili","Asia/Dubai","Asia/Dushanbe","Asia/Gaza","Asia/Harbin","Asia/Ho_Chi_Minh","Asia/Hong_Kong","Asia/Hovd","Asia/Irkutsk","Asia/Istanbul","Asia/Jakarta","Asia/Jayapura","Asia/Jerusalem","Asia/Kabul","Asia/Kamchatka","Asia/Karachi","Asia/Kashgar","Asia/Katmandu","Asia/Kolkata","Asia/Krasnoyarsk","Asia/Kuala_Lumpur","Asia/Kuching","Asia/Kuwait","Asia/Macao","Asia/Macau","Asia/Magadan","Asia/Makassar","Asia/Manila","Asia/Muscat","Asia/Nicosia","Asia/Novosibirsk","Asia/Omsk","Asia/Oral","Asia/Phnom_Penh","Asia/Pontianak","Asia/Pyongyang","Asia/Qatar","Asia/Qyzylorda","Asia/Rangoon","Asia/Riyadh","Asia/Saigon","Asia/Sakhalin","Asia/Samarkand","Asia/Seoul","Asia/Shanghai","Asia/Singapore","Asia/Taipei","Asia/Tashkent","Asia/Tbilisi","Asia/Tehran","Asia/Tel_Aviv","Asia/Thimbu","Asia/Thimphu","Asia/Tokyo","Asia/Ujung_Pandang","Asia/Ulaanbaatar","Asia/Ulan_Bator","Asia/Urumqi","Asia/Vientiane","Asia/Vladivostok","Asia/Yakutsk","Asia/Yekaterinburg","Asia/Yerevan","Atlantic/Azores","Atlantic/Bermuda","Atlantic/Canary","Atlantic/Cape_Verde","Atlantic/Faeroe","Atlantic/Faroe","Atlantic/Jan_Mayen","Atlantic/Madeira","Atlantic/Reykjavik","Atlantic/South_Georgia","Atlantic/St_Helena","Atlantic/Stanley","Australia/ACT","Australia/Adelaide","Australia/Brisbane","Australia/Broken_Hill","Australia/Canberra","Australia/Currie","Australia/Darwin","Australia/Eucla","Australia/Hobart","Australia/LHI","Australia/Lindeman","Australia/Lord_Howe","Australia/Melbourne","Australia/North","Australia/NSW","Australia/Perth","Australia/Queensland","Australia/South","Australia/Sydney","Australia/Tasmania","Australia/Victoria","Australia/West","Australia/Yancowinna","Brazil/Acre","Brazil/DeNoronha","Brazil/East","Brazil/West","Canada/Atlantic","Canada/Central","Canada/East-Saskatchewan","Canada/Eastern","Canada/Mountain","Canada/Newfoundland","Canada/Pacific","Canada/Saskatchewan","Canada/Yukon","CET","Chile/Continental","Chile/EasterIsland","CST6CDT","Cuba","EET","Egypt","Eire","EST","EST5EDT","Etc/GMT","Etc/GMT+0","Etc/GMT+1","Etc/GMT+10","Etc/GMT+11","Etc/GMT+12","Etc/GMT+2","Etc/GMT+3","Etc/GMT+4","Etc/GMT+5","Etc/GMT+6","Etc/GMT+7","Etc/GMT+8","Etc/GMT+9","Etc/GMT-0","Etc/GMT-1","Etc/GMT-10","Etc/GMT-11","Etc/GMT-12","Etc/GMT-13","Etc/GMT-14","Etc/GMT-2","Etc/GMT-3","Etc/GMT-4","Etc/GMT-5","Etc/GMT-6","Etc/GMT-7","Etc/GMT-8","Etc/GMT-9","Etc/GMT0","Etc/Greenwich","Etc/UCT","Etc/Universal","Etc/UTC","Etc/Zulu","Europe/Amsterdam","Europe/Andorra","Europe/Athens","Europe/Belfast","Europe/Belgrade","Europe/Berlin","Europe/Bratislava","Europe/Brussels","Europe/Bucharest","Europe/Budapest","Europe/Chisinau","Europe/Copenhagen","Europe/Dublin","Europe/Gibraltar","Europe/Guernsey","Europe/Helsinki","Europe/Isle_of_Man","Europe/Istanbul","Europe/Jersey","Europe/Kaliningrad","Europe/Kiev","Europe/Lisbon","Europe/Ljubljana","Europe/London","Europe/Luxembourg","Europe/Madrid","Europe/Malta","Europe/Mariehamn","Europe/Minsk","Europe/Monaco","Europe/Moscow","Europe/Nicosia","Europe/Oslo","Europe/Paris","Europe/Podgorica","Europe/Prague","Europe/Riga","Europe/Rome","Europe/Samara","Europe/San_Marino","Europe/Sarajevo","Europe/Simferopol","Europe/Skopje","Europe/Sofia","Europe/Stockholm","Europe/Tallinn","Europe/Tirane","Europe/Tiraspol","Europe/Uzhgorod","Europe/Vaduz","Europe/Vatican","Europe/Vienna","Europe/Vilnius","Europe/Volgograd","Europe/Warsaw","Europe/Zagreb","Europe/Zaporozhye","Europe/Zurich","Factory","GB","GB-Eire","GMT","GMT+0","GMT-0","GMT0","Greenwich","Hongkong","HST","Iceland","Indian/Antananarivo","Indian/Chagos","Indian/Christmas","Indian/Cocos","Indian/Comoro","Indian/Kerguelen","Indian/Mahe","Indian/Maldives","Indian/Mauritius","Indian/Mayotte","Indian/Reunion","Iran","Israel","Jamaica","Japan","Kwajalein","Libya","MET","Mexico/BajaNorte","Mexico/BajaSur","Mexico/General","MST","MST7MDT","Navajo","NZ","NZ-CHAT","Pacific/Apia","Pacific/Auckland","Pacific/Chatham","Pacific/Easter","Pacific/Efate","Pacific/Enderbury","Pacific/Fakaofo","Pacific/Fiji","Pacific/Funafuti","Pacific/Galapagos","Pacific/Gambier","Pacific/Guadalcanal","Pacific/Guam","Pacific/Honolulu","Pacific/Johnston","Pacific/Kiritimati","Pacific/Kosrae","Pacific/Kwajalein","Pacific/Majuro","Pacific/Marquesas","Pacific/Midway","Pacific/Nauru","Pacific/Niue","Pacific/Norfolk","Pacific/Noumea","Pacific/Pago_Pago","Pacific/Palau","Pacific/Pitcairn","Pacific/Ponape","Pacific/Port_Moresby","Pacific/Rarotonga","Pacific/Saipan","Pacific/Samoa","Pacific/Tahiti","Pacific/Tarawa","Pacific/Tongatapu","Pacific/Truk","Pacific/Wake","Pacific/Wallis","Pacific/Yap","Poland","Portugal","PRC","PST8PDT","ROC","ROK","Singapore","Turkey","UCT","Universal","US/Alaska","US/Aleutian","US/Arizona","US/Central","US/East-Indiana","US/Eastern","US/Hawaii","US/Indiana-Starke","US/Michigan","US/Mountain","US/Pacific","US/Pacific-New","US/Samoa","UTC","W-SU","WET","Zulu");
  686. }
  687. }
  688. ?>