PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/code/core/sociax/extend.php

http://thinksns-2.googlecode.com/
PHP | 2071 lines | 1620 code | 93 blank | 358 comment | 141 complexity | 7a79da836c7e9be0ab83a12f6a232bc5 MD5 | raw file
Possible License(s): LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  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. /**
  23. * ?????IP??
  24. */
  25. function get_client_ip(){
  26. if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  27. $ip = getenv("HTTP_CLIENT_IP");
  28. else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
  29. $ip = getenv("HTTP_X_FORWARDED_FOR");
  30. else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  31. $ip = getenv("REMOTE_ADDR");
  32. else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
  33. $ip = $_SERVER['REMOTE_ADDR'];
  34. else
  35. $ip = "unknown";
  36. return($ip);
  37. }
  38. /**
  39. +----------------------------------------------------------
  40. * ???????????????
  41. +----------------------------------------------------------
  42. * @static
  43. * @access public
  44. +----------------------------------------------------------
  45. * @param string $str ????????
  46. * @param string $start ????
  47. * @param string $length ????
  48. * @param string $charset ????
  49. * @param string $suffix ??????
  50. +----------------------------------------------------------
  51. * @return string
  52. +----------------------------------------------------------
  53. */
  54. function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {
  55. if(function_exists("mb_substr"))
  56. $slice = mb_substr($str, $start, $length, $charset);
  57. elseif(function_exists('iconv_substr')) {
  58. $slice = iconv_substr($str,$start,$length,$charset);
  59. }else{
  60. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  61. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  62. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  63. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  64. preg_match_all($re[$charset], $str, $match);
  65. $slice = join("",array_slice($match[0], $start, $length));
  66. }
  67. if($suffix && $str != $slice) return $slice."...";
  68. return $slice;
  69. }
  70. /**
  71. +----------------------------------------------------------
  72. * ???????????????
  73. +----------------------------------------------------------
  74. * @static
  75. * @access public
  76. +----------------------------------------------------------
  77. * @param string $str ????????
  78. * @param string $length ????
  79. * @param string $charset ????
  80. * @param string $suffix ??????
  81. +----------------------------------------------------------
  82. * @return string
  83. +----------------------------------------------------------
  84. */
  85. function mStr($str, $length, $charset="utf-8", $suffix=true){
  86. return msubstr($str, 0, $length, $charset, $suffix);
  87. }
  88. /**
  89. +----------------------------------------------------------
  90. * ???????????????? ????6? ???????
  91. +----------------------------------------------------------
  92. * @param string $len ??
  93. * @param string $type ????
  94. * 0 ?? 1 ?? ?? ??
  95. * @param string $addChars ????
  96. +----------------------------------------------------------
  97. * @return string
  98. +----------------------------------------------------------
  99. */
  100. function rand_string($len=6,$type='',$addChars='') {
  101. $str ='';
  102. switch($type) {
  103. case 0:
  104. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.$addChars;
  105. break;
  106. case 1:
  107. $chars= str_repeat('0123456789',3);
  108. break;
  109. case 2:
  110. $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.$addChars;
  111. break;
  112. case 3:
  113. $chars='abcdefghijklmnopqrstuvwxyz'.$addChars;
  114. break;
  115. default :
  116. // ????????????oOLl???01???????addChars??
  117. $chars='ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'.$addChars;
  118. break;
  119. }
  120. if($len>10 ) {//?????????????
  121. $chars= $type==1? str_repeat($chars,$len) : str_repeat($chars,5);
  122. }
  123. $chars = str_shuffle($chars);
  124. $str = substr($chars,0,$len);
  125. return $str;
  126. }
  127. /**
  128. +----------------------------------------------------------
  129. * ??????? ???4???
  130. +----------------------------------------------------------
  131. * @param string $fmode ???
  132. +----------------------------------------------------------
  133. * @return string
  134. +----------------------------------------------------------
  135. */
  136. function build_verify ($length=4,$mode=1) {
  137. return rand_string($length,$mode);
  138. }
  139. /**
  140. +----------------------------------------------------------
  141. * ????? ??????? B K M G T ?????
  142. +----------------------------------------------------------
  143. * @return string
  144. +----------------------------------------------------------
  145. */
  146. function byte_format($size, $dec=2) {
  147. $a = array("B", "KB", "MB", "GB", "TB", "PB");
  148. $pos = 0;
  149. while ($size >= 1024) {
  150. $size /= 1024;
  151. $pos++;
  152. }
  153. return round($size,$dec)." ".$a[$pos];
  154. }
  155. /**
  156. +----------------------------------------------------------
  157. * ????????UTF8??
  158. +----------------------------------------------------------
  159. * @param string $string ???
  160. +----------------------------------------------------------
  161. * @return Boolean
  162. +----------------------------------------------------------
  163. */
  164. function is_utf8($string) {
  165. return preg_match('%^(?:
  166. [\x09\x0A\x0D\x20-\x7E] # ASCII
  167. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  168. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  169. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  170. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  171. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  172. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  173. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  174. )*$%xs', $string);
  175. }
  176. /**
  177. +----------------------------------------------------------
  178. * ????
  179. +----------------------------------------------------------
  180. * @param String $str ????????? ?? ???
  181. * @param Boolean $show ????
  182. +----------------------------------------------------------
  183. * @return String
  184. +----------------------------------------------------------
  185. */
  186. function highlight_code($str,$show=false) {
  187. if(file_exists($str)) {
  188. $str = file_get_contents($str);
  189. }
  190. $str = stripslashes(trim($str));
  191. // The highlight string function encodes and highlights
  192. // brackets so we need them to start raw
  193. $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);
  194. // Replace any existing PHP tags to temporary markers so they don't accidentally
  195. // break the string out of PHP, and thus, thwart the highlighting.
  196. $str = str_replace(array('&lt;?php', '?&gt;', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str);
  197. // The highlight_string function requires that the text be surrounded
  198. // by PHP tags. Since we don't know if A) the submitted text has PHP tags,
  199. // or B) whether the PHP tags enclose the entire string, we will add our
  200. // own PHP tags around the string along with some markers to make replacement easier later
  201. $str = '<?php //tempstart'."\n".$str.'//tempend ?>'; // <?
  202. // All the magic happens here, baby!
  203. $str = highlight_string($str, TRUE);
  204. // Prior to PHP 5, the highlight function used icky font tags
  205. // so we'll replace them with span tags.
  206. if (abs(phpversion()) < 5)
  207. {
  208. $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
  209. $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
  210. }
  211. // Remove our artificially added PHP
  212. $str = preg_replace("#\<code\>.+?//tempstart\<br />\</span\>#is", "<code>\n", $str);
  213. $str = preg_replace("#\<code\>.+?//tempstart\<br />#is", "<code>\n", $str);
  214. $str = preg_replace("#//tempend.+#is", "</span>\n</code>", $str);
  215. // Replace our markers back to PHP tags.
  216. $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('&lt;?php', '?&gt;', '\\'), $str); //<?
  217. $line = explode("<br />", rtrim(ltrim($str,'<code>'),'</code>'));
  218. $result = '<div class="code"><ol>';
  219. foreach($line as $key=>$val) {
  220. $result .= '<li>'.$val.'</li>';
  221. }
  222. $result .= '</ol></div>';
  223. $result = str_replace("\n", "", $result);
  224. if( $show!== false) {
  225. echo($result);
  226. }else {
  227. return $result;
  228. }
  229. }
  230. /**
  231. * ???????html
  232. * @param string $text ???????
  233. * @param string $type ???string,???:INT,FLOAT,BOOL,WORD,ALNUM,CMD,BASE64,ARRAY,PATH,USERNAME
  234. * @param bool $tagsMethod true?????????????false????????????.
  235. * @param bool $attrMethod ??
  236. * @param array $tags ????????
  237. * @param array $attr ?????????
  238. * @param array $tagsBlack ????????
  239. * @param array $attrBlack ???????????
  240. */
  241. //function h($text,$type,$tagsMethod=true,$attrMethod=true,$xssAuto = 1,$tags=array(),$attr=array(),$tagsBlack=array(),$attrBlack=array()){
  242. // if(!class_exists('Security')){
  243. // vendor('libs.SamLib.filter.Security','','.class.php');
  244. // }
  245. // $inputConfig ['tagsMethod'] = 1; //???????
  246. // $inputConfig ['attrMethod'] = 1; //???????
  247. // $inputConfig ['xssAuto'] = 1 ; //???????
  248. // $inputConfig ['tagsFilter'] = array(); //???????
  249. // $inputConfig ['attrFilter'] = array(); //???????
  250. // $inputConfig ['tagBlacklist'] = array ('applet', 'body', 'bgsound', 'base', 'basefont', 'frame', 'frameset', 'head',
  251. // 'html', 'id', 'iframe', 'ilayer', 'layer', 'link', 'meta', 'name', 'script',
  252. // 'style', 'title', 'xml','vbscript','javascript','input','form','textarea','select','option','button'); //??????
  253. // $inputConfig ['attrBlacklist'] = array ('action', 'codebase', 'dynsrc', 'lowsrc','onclick','onload','onsubmit'); //??????
  254. //
  255. //
  256. //
  257. // $filter = Security::getInstance($type,$inputConfig);
  258. //
  259. // $filter->setTagsMethod($tagsMethod);
  260. // $filter->setAttrMethod($attrMethod);
  261. // $filter->setXss($xssAuto);
  262. // //???.?$tagsMethod=false?????
  263. // !empty($tags) && $filter->setTagsFilter($tags);
  264. // !empty($attr) && $filter->setAttrFilter($attr);
  265. // //???.?$tagsMethod=true????
  266. // !empty($tagsBlack) && $filter->setTagBlacklist($tagsBlack);
  267. // !empty($attrBlack) && $filter->setAttrBlacklist($attrBlack);
  268. // return $filter->inputFilter($text);
  269. //}
  270. //?????html
  271. function h($text, $tags = null){
  272. $text = trim($text);
  273. //??????
  274. $text = preg_replace('/<!--?.*-->/','',$text);
  275. //????????
  276. $text = preg_replace('/<\?|\?'.'>/','',$text);
  277. //????js
  278. $text = preg_replace('/<script?.*\/script>/','',$text);
  279. $text = str_replace('[','&#091;',$text);
  280. $text = str_replace(']','&#093;',$text);
  281. $text = str_replace('|','&#124;',$text);
  282. //?????
  283. $text = preg_replace('/\r?\n/','',$text);
  284. //br
  285. $text = preg_replace('/<br(\s\/)?'.'>/i','[br]',$text);
  286. $text = preg_replace('/(\[br\]\s*){10,}/i','[br]',$text);
  287. //????????????on??lang js
  288. while(preg_match('/(<[^><]+)( lang|on|action|background|codebase|dynsrc|lowsrc)[^><]+/i',$text,$mat)){
  289. $text=str_replace($mat[0],$mat[1],$text);
  290. }
  291. while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i',$text,$mat)){
  292. $text=str_replace($mat[0],$mat[1].$mat[3],$text);
  293. }
  294. if(empty($tags)) {
  295. $tags = 'table|td|th|tr|i|b|u|strong|img|p|br|div|strong|em|ul|ol|li|dl|dd|dt|a';
  296. }
  297. //???HTML??
  298. $text = preg_replace('/<('.$tags.')( [^><\[\]]*)>/i','[\1\2]',$text);
  299. //????html
  300. $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);
  301. //?????html??
  302. while(preg_match('/<([a-z]+)[^><\[\]]*>[^><]*<\/\1>/i',$text,$mat)){
  303. $text=str_replace($mat[0],str_replace('>',']',str_replace('<','[',$mat[0])),$text);
  304. }
  305. //????
  306. while(preg_match('/(\[[^\[\]]*=\s*)(\"|\')([^\2=\[\]]+)\2([^\[\]]*\])/i',$text,$mat)){
  307. $text=str_replace($mat[0],$mat[1].'|'.$mat[3].'|'.$mat[4],$text);
  308. }
  309. //?????????
  310. while(preg_match('/\[[^\[\]]*(\"|\')[^\[\]]*\]/i',$text,$mat)){
  311. $text=str_replace($mat[0],str_replace($mat[1],'',$mat[0]),$text);
  312. }
  313. //?????????? < >
  314. $text = str_replace('<','&lt;',$text);
  315. $text = str_replace('>','&gt;',$text);
  316. $text = str_replace('"','&quot;',$text);
  317. //???
  318. $text = str_replace('[','<',$text);
  319. $text = str_replace(']','>',$text);
  320. $text = str_replace('|','"',$text);
  321. //??????
  322. $text = str_replace(' ',' ',$text);
  323. return $text;
  324. }
  325. //?????
  326. function t($text,$parseBr=false){
  327. //$text = strip_tags($text);
  328. if(!$parseBr){
  329. $text = str_replace(array("\r","\n","\t"),' ',$text);
  330. }else{
  331. $text = nl2br($text);
  332. }
  333. $text = stripslashes($text);
  334. $text = htmlspecialchars($text, ENT_NOQUOTES,'UTF-8 ');
  335. return $text;
  336. }
  337. //??jsescape
  338. function unescape($str) {
  339. $str = rawurldecode($str);
  340. preg_match_all("/(?:%u.{4})|.+/",$str,$r);
  341. $ar = $r[0];
  342. foreach($ar as $k=>$v) {
  343. if(substr($v,0,2) == "%u" && strlen($v) == 6)
  344. $ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,-4)));
  345. }
  346. return join("",$ar);
  347. }
  348. //??UBB
  349. function ubb($Text) {
  350. $Text=trim($Text);
  351. //$Text=htmlspecialchars($Text);
  352. $Text=preg_replace("/\\t/is"," ",$Text);
  353. $Text=preg_replace("/\[h1\](.+?)\[\/h1\]/is","<h1>\\1</h1>",$Text);
  354. $Text=preg_replace("/\[h2\](.+?)\[\/h2\]/is","<h2>\\1</h2>",$Text);
  355. $Text=preg_replace("/\[h3\](.+?)\[\/h3\]/is","<h3>\\1</h3>",$Text);
  356. $Text=preg_replace("/\[h4\](.+?)\[\/h4\]/is","<h4>\\1</h4>",$Text);
  357. $Text=preg_replace("/\[h5\](.+?)\[\/h5\]/is","<h5>\\1</h5>",$Text);
  358. $Text=preg_replace("/\[h6\](.+?)\[\/h6\]/is","<h6>\\1</h6>",$Text);
  359. $Text=preg_replace("/\[separator\]/is","",$Text);
  360. $Text=preg_replace("/\[center\](.+?)\[\/center\]/is","<center>\\1</center>",$Text);
  361. $Text=preg_replace("/\[url=http:\/\/([^\[]*)\](.+?)\[\/url\]/is","<a href=\"http://\\1\" target=_blank>\\2</a>",$Text);
  362. $Text=preg_replace("/\[url=([^\[]*)\](.+?)\[\/url\]/is","<a href=\"http://\\1\" target=_blank>\\2</a>",$Text);
  363. $Text=preg_replace("/\[url\]http:\/\/([^\[]*)\[\/url\]/is","<a href=\"http://\\1\" target=_blank>\\1</a>",$Text);
  364. $Text=preg_replace("/\[url\]([^\[]*)\[\/url\]/is","<a href=\"\\1\" target=_blank>\\1</a>",$Text);
  365. $Text=preg_replace("/\[img\](.+?)\[\/img\]/is","<img src=\\1>",$Text);
  366. $Text=preg_replace("/\[color=(.+?)\](.+?)\[\/color\]/is","<font color=\\1>\\2</font>",$Text);
  367. $Text=preg_replace("/\[size=(.+?)\](.+?)\[\/size\]/is","<font size=\\1>\\2</font>",$Text);
  368. $Text=preg_replace("/\[sup\](.+?)\[\/sup\]/is","<sup>\\1</sup>",$Text);
  369. $Text=preg_replace("/\[sub\](.+?)\[\/sub\]/is","<sub>\\1</sub>",$Text);
  370. $Text=preg_replace("/\[pre\](.+?)\[\/pre\]/is","<pre>\\1</pre>",$Text);
  371. $Text=preg_replace("/\[email\](.+?)\[\/email\]/is","<a href='mailto:\\1'>\\1</a>",$Text);
  372. $Text=preg_replace("/\[colorTxt\](.+?)\[\/colorTxt\]/eis","color_txt('\\1')",$Text);
  373. $Text=preg_replace("/\[emot\](.+?)\[\/emot\]/eis","emot('\\1')",$Text);
  374. $Text=preg_replace("/\[i\](.+?)\[\/i\]/is","<i>\\1</i>",$Text);
  375. $Text=preg_replace("/\[u\](.+?)\[\/u\]/is","<u>\\1</u>",$Text);
  376. $Text=preg_replace("/\[b\](.+?)\[\/b\]/is","<b>\\1</b>",$Text);
  377. $Text=preg_replace("/\[quote\](.+?)\[\/quote\]/is"," <div class='quote'><h5>??:</h5><blockquote>\\1</blockquote></div>", $Text);
  378. $Text=preg_replace("/\[code\](.+?)\[\/code\]/eis","highlight_code('\\1')", $Text);
  379. $Text=preg_replace("/\[php\](.+?)\[\/php\]/eis","highlight_code('\\1')", $Text);
  380. $Text=preg_replace("/\[sig\](.+?)\[\/sig\]/is","<div class='sign'>\\1</div>", $Text);
  381. $Text=preg_replace("/\\n/is","<br/>",$Text);
  382. return $Text;
  383. }
  384. // ?????????
  385. function build_count_rand ($number,$length=4,$mode=1) {
  386. if($mode==1 && $length<strlen($number) ) {
  387. //???????????????
  388. return false;
  389. }
  390. $rand = array();
  391. for($i=0; $i<$number; $i++) {
  392. $rand[] = rand_string($length,$mode);
  393. }
  394. $unqiue = array_unique($rand);
  395. if(count($unqiue)==count($rand)) {
  396. return $rand;
  397. }
  398. $count = count($rand)-count($unqiue);
  399. for($i=0; $i<$count*3; $i++) {
  400. $rand[] = rand_string($length,$mode);
  401. }
  402. $rand = array_slice(array_unique ($rand),0,$number);
  403. return $rand;
  404. }
  405. function remove_xss($val) {
  406. // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
  407. // this prevents some character re-spacing such as <java\0script>
  408. // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
  409. $val = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $val);
  410. // straight replacements, the user should never need these since they're normal characters
  411. // this prevents like <IMG SRC=@avascript:alert('XSS')>
  412. $search = 'abcdefghijklmnopqrstuvwxyz';
  413. $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  414. $search .= '1234567890!@#$%^&*()';
  415. $search .= '~`";:?+/={}[]-_|\'\\';
  416. for ($i = 0; $i < strlen($search); $i++) {
  417. // ;? matches the ;, which is optional
  418. // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
  419. // @ @ search for the hex values
  420. $val = preg_replace('/(&#[xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ;
  421. // @ @ 0{0,7} matches '0' zero to seven times
  422. $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ;
  423. }
  424. // now the only remaining whitespace attacks are \t, \n, and \r
  425. $ra1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
  426. $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');
  427. $ra = array_merge($ra1, $ra2);
  428. $found = true; // keep replacing as long as the previous round replaced something
  429. while ($found == true) {
  430. $val_before = $val;
  431. for ($i = 0; $i < sizeof($ra); $i++) {
  432. $pattern = '/';
  433. for ($j = 0; $j < strlen($ra[$i]); $j++) {
  434. if ($j > 0) {
  435. $pattern .= '(';
  436. $pattern .= '(&#[xX]0{0,8}([9ab]);)';
  437. $pattern .= '|';
  438. $pattern .= '|(&#0{0,8}([9|10|13]);)';
  439. $pattern .= ')*';
  440. }
  441. $pattern .= $ra[$i][$j];
  442. }
  443. $pattern .= '/i';
  444. $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag
  445. $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
  446. if ($val_before == $val) {
  447. // no replacements were made, so exit the loop
  448. $found = false;
  449. }
  450. }
  451. }
  452. return $val;
  453. }
  454. /**
  455. +----------------------------------------------------------
  456. * ??????????Tree
  457. +----------------------------------------------------------
  458. * @access public
  459. +----------------------------------------------------------
  460. * @param array $list ???????
  461. * @param string $pid parent????
  462. * @param string $level level????
  463. +----------------------------------------------------------
  464. * @return array
  465. +----------------------------------------------------------
  466. */
  467. function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0)
  468. {
  469. // ??Tree
  470. $tree = array();
  471. if(is_array($list)) {
  472. // ???????????
  473. $refer = array();
  474. foreach ($list as $key => $data) {
  475. $refer[$data[$pk]] =& $list[$key];
  476. }
  477. foreach ($list as $key => $data) {
  478. // ??????parent
  479. $parentId = $data[$pid];
  480. if ($root == $parentId) {
  481. $tree[] =& $list[$key];
  482. }else{
  483. if (isset($refer[$parentId])) {
  484. $parent =& $refer[$parentId];
  485. $parent[$child][] =& $list[$key];
  486. }
  487. }
  488. }
  489. }
  490. return $tree;
  491. }
  492. /**
  493. +----------------------------------------------------------
  494. * ??????????
  495. +----------------------------------------------------------
  496. * @access public
  497. +----------------------------------------------------------
  498. * @param array $list ????
  499. * @param string $field ??????
  500. * @param array $sortby ????
  501. * asc???? desc???? nat????
  502. +----------------------------------------------------------
  503. * @return array
  504. +----------------------------------------------------------
  505. */
  506. function list_sort_by($list,$field, $sortby='asc') {
  507. if(is_array($list)){
  508. $refer = $resultSet = array();
  509. foreach ($list as $i => $data)
  510. $refer[$i] = &$data[$field];
  511. switch ($sortby) {
  512. case 'asc': // ????
  513. asort($refer);
  514. break;
  515. case 'desc':// ????
  516. arsort($refer);
  517. break;
  518. case 'nat': // ????
  519. natcasesort($refer);
  520. break;
  521. }
  522. foreach ( $refer as $key=> $val)
  523. $resultSet[] = &$list[$key];
  524. return $resultSet;
  525. }
  526. return false;
  527. }
  528. /**
  529. +----------------------------------------------------------
  530. * ????????
  531. +----------------------------------------------------------
  532. * @access public
  533. +----------------------------------------------------------
  534. * @param array $list ????
  535. * @param mixed $condition ????
  536. * ?? array('name'=>$value) ?? name=$value
  537. +----------------------------------------------------------
  538. * @return array
  539. +----------------------------------------------------------
  540. */
  541. function list_search($list,$condition) {
  542. if(is_string($condition))
  543. parse_str($condition,$condition);
  544. // ???????
  545. $resultSet = array();
  546. foreach ($list as $key=>$data){
  547. $find = false;
  548. foreach ($condition as $field=>$value){
  549. if(isset($data[$field])) {
  550. if(0 === strpos($value,'/')) {
  551. $find = preg_match($value,$data[$field]);
  552. }elseif($data[$field]==$value){
  553. $find = true;
  554. }
  555. }
  556. }
  557. if($find)
  558. $resultSet[] = &$list[$key];
  559. }
  560. return $resultSet;
  561. }
  562. // ??Http????
  563. function send_http_status($status) {
  564. static $_status = array(
  565. // Informational 1xx
  566. 100 => 'Continue',
  567. 101 => 'Switching Protocols',
  568. // Success 2xx
  569. 200 => 'OK',
  570. 201 => 'Created',
  571. 202 => 'Accepted',
  572. 203 => 'Non-Authoritative Information',
  573. 204 => 'No Content',
  574. 205 => 'Reset Content',
  575. 206 => 'Partial Content',
  576. // Redirection 3xx
  577. 300 => 'Multiple Choices',
  578. 301 => 'Moved Permanently',
  579. 302 => 'Moved Temporarily ', // 1.1
  580. 303 => 'See Other',
  581. 304 => 'Not Modified',
  582. 305 => 'Use Proxy',
  583. // 306 is deprecated but reserved
  584. 307 => 'Temporary Redirect',
  585. // Client Error 4xx
  586. 400 => 'Bad Request',
  587. 401 => 'Unauthorized',
  588. 402 => 'Payment Required',
  589. 403 => 'Forbidden',
  590. 404 => 'Not Found',
  591. 405 => 'Method Not Allowed',
  592. 406 => 'Not Acceptable',
  593. 407 => 'Proxy Authentication Required',
  594. 408 => 'Request Timeout',
  595. 409 => 'Conflict',
  596. 410 => 'Gone',
  597. 411 => 'Length Required',
  598. 412 => 'Precondition Failed',
  599. 413 => 'Request Entity Too Large',
  600. 414 => 'Request-URI Too Long',
  601. 415 => 'Unsupported Media Type',
  602. 416 => 'Requested Range Not Satisfiable',
  603. 417 => 'Expectation Failed',
  604. // Server Error 5xx
  605. 500 => 'Internal Server Error',
  606. 501 => 'Not Implemented',
  607. 502 => 'Bad Gateway',
  608. 503 => 'Service Unavailable',
  609. 504 => 'Gateway Timeout',
  610. 505 => 'HTTP Version Not Supported',
  611. 509 => 'Bandwidth Limit Exceeded'
  612. );
  613. if(array_key_exists($code,$_status)) {
  614. header('HTTP/1.1 '.$code.' '.$_status[$code]);
  615. }
  616. }
  617. // ????http header??
  618. function send_http_header($type='utf8'){
  619. //utf8,html,wml,xml,??????? ???header
  620. switch($type){
  621. case 'utf8':
  622. header("Content-type: text/html; charset=utf-8");
  623. break;
  624. case 'xml':
  625. header("Content-type: text/xml; charset=utf-8");
  626. break;
  627. }
  628. }
  629. // bmp????????
  630. function imagecreatefrombmp($fname) {
  631. $buf = @file_get_contents($fname);
  632. if(strlen($buf)<54) return false;
  633. $file_header=unpack("sbfType/LbfSize/sbfReserved1/sbfReserved2/LbfOffBits",substr($buf,0,14));
  634. if($file_header["bfType"]!=19778) return false;
  635. $info_header=unpack("LbiSize/lbiWidth/lbiHeight/sbiPlanes/sbiBitCountLbiCompression/LbiSizeImage/lbiXPelsPerMeter/lbiYPelsPerMeter/LbiClrUsed/LbiClrImportant",substr($buf,14,40));
  636. if($info_header["biBitCountLbiCompression"]==2) return false;
  637. $line_len=round($info_header["biWidth"]*$info_header["biBitCountLbiCompression"]/8);
  638. $x=$line_len%4;
  639. if($x>0) $line_len+=4-$x;
  640. $img=imagecreatetruecolor($info_header["biWidth"],$info_header["biHeight"]);
  641. switch($info_header["biBitCountLbiCompression"]){
  642. case 4:
  643. $colorset=unpack("L*",substr($buf,54,64));
  644. for($y=0;$y<$info_header["biHeight"];$y++){
  645. $colors=array();
  646. $y_pos=$y*$line_len+$file_header["bfOffBits"];
  647. for($x=0;$x<$info_header["biWidth"];$x++){
  648. if($x%2)
  649. $colors[]=$colorset[(ord($buf[$y_pos+($x+1)/2])&0xf)+1];
  650. else
  651. $colors[]=$colorset[((ord($buf[$y_pos+$x/2+1])>>4)&0xf)+1];
  652. }
  653. imagesetstyle($img,$colors);
  654. imageline($img,0,$info_header["biHeight"]-$y-1,$info_header["biWidth"],$info_header["biHeight"]-$y-1,IMG_COLOR_STYLED);
  655. }
  656. break;
  657. case 8:
  658. $colorset=unpack("L*",substr($buf,54,1024));
  659. for($y=0;$y<$info_header["biHeight"];$y++){
  660. $colors=array();
  661. $y_pos=$y*$line_len+$file_header["bfOffBits"];
  662. for($x=0;$x<$info_header["biWidth"];$x++){
  663. $colors[]=$colorset[ord($buf[$y_pos+$x])+1];
  664. }
  665. imagesetstyle($img,$colors);
  666. imageline($img,0,$info_header["biHeight"]-$y-1,$info_header["biWidth"],$info_header["biHeight"]-$y-1,IMG_COLOR_STYLED);
  667. }
  668. break;
  669. case 16:
  670. for($y=0;$y<$info_header["biHeight"];$y++){
  671. $colors=array();
  672. $y_pos=$y*$line_len+$file_header["bfOffBits"];
  673. for($x=0;$x<$info_header["biWidth"];$x++){
  674. $i=$x*2;
  675. $color=ord($buf[$y_pos+$i])|(ord($buf[$y_pos+$i+1])<<8);
  676. $colors[]=imagecolorallocate($img,(($color>>10)&0x1f)*0xff/0x1f,(($color>>5)&0x1f)*0xff/0x1f,($color&0x1f)*0xff/0x1f);
  677. }
  678. imagesetstyle($img,$colors);
  679. imageline($img,0,$info_header["biHeight"]-$y-1,$info_header["biWidth"],$info_header["biHeight"]-$y-1,IMG_COLOR_STYLED);
  680. }
  681. break;
  682. case 24:
  683. for($y=0;$y<$info_header["biHeight"];$y++){
  684. $colors=array();
  685. $y_pos=$y*$line_len+$file_header["bfOffBits"];
  686. for($x=0;$x<$info_header["biWidth"];$x++){
  687. $i=$x*3;
  688. $colors[]=imagecolorallocate($img,ord($buf[$y_pos+$i+2]),ord($buf[$y_pos+$i+1]),ord($buf[$y_pos+$i]));
  689. }
  690. imagesetstyle($img,$colors);
  691. imageline($img,0,$info_header["biHeight"]-$y-1,$info_header["biWidth"],$info_header["biHeight"]-$y-1,IMG_COLOR_STYLED);
  692. }
  693. break;
  694. default:
  695. return false;
  696. break;
  697. }
  698. return $img;
  699. }
  700. // bmp????????
  701. function imagebmp(&$im, $filename = '', $bit = 8, $compression = 0) {
  702. if (!in_array($bit, array(1, 4, 8, 16, 24, 32)))
  703. {
  704. $bit = 8;
  705. }
  706. else if ($bit == 32) // todo:32 bit
  707. {
  708. $bit = 24;
  709. }
  710. $bits = pow(2, $bit);
  711. // ?????
  712. imagetruecolortopalette($im, true, $bits);
  713. $width = imagesx($im);
  714. $height = imagesy($im);
  715. $colors_num = imagecolorstotal($im);
  716. if ($bit <= 8)
  717. {
  718. // ????
  719. $rgb_quad = '';
  720. for ($i = 0; $i < $colors_num; $i ++)
  721. {
  722. $colors = imagecolorsforindex($im, $i);
  723. $rgb_quad .= chr($colors['blue']) . chr($colors['green']) . chr($colors['red']) . "\0"; }
  724. // ????
  725. $bmp_data = '';
  726. // ???
  727. if ($compression == 0 || $bit < 8)
  728. {
  729. if (!in_array($bit, array(1, 4, 8)))
  730. {
  731. $bit = 8;
  732. }
  733. $compression = 0;
  734. // ????????4???????
  735. $extra = '';
  736. $padding = 4 - ceil($width / (8 / $bit)) % 4;
  737. if ($padding % 4 != 0)
  738. {
  739. $extra = str_repeat("\0", $padding);
  740. }
  741. for ($j = $height - 1; $j >= 0; $j --)
  742. {
  743. $i = 0;
  744. while ($i < $width)
  745. {
  746. $bin = 0;
  747. $limit = $width - $i < 8 / $bit ? (8 / $bit - $width + $i) * $bit : 0;
  748. for ($k = 8 - $bit; $k >= $limit; $k -= $bit)
  749. {
  750. $index = imagecolorat($im, $i, $j);
  751. $bin |= $index << $k;
  752. $i ++;
  753. }
  754. $bmp_data .= chr($bin);
  755. }
  756. $bmp_data .= $extra;
  757. }
  758. }
  759. // RLE8 ??
  760. else if ($compression == 1 && $bit == 8)
  761. {
  762. for ($j = $height - 1; $j >= 0; $j --)
  763. {
  764. $last_index = "\0";
  765. $same_num = 0;
  766. for ($i = 0; $i <= $width; $i ++)
  767. {
  768. $index = imagecolorat($im, $i, $j);
  769. if ($index !== $last_index || $same_num > 255)
  770. {
  771. if ($same_num != 0)
  772. {
  773. $bmp_data .= chr($same_num) . chr($last_index);
  774. }
  775. $last_index = $index;
  776. $same_num = 1;
  777. }
  778. else
  779. {
  780. $same_num ++;
  781. }
  782. }
  783. $bmp_data .= "\0\0";
  784. }
  785. $bmp_data .= "\0\1";
  786. }
  787. $size_quad = strlen($rgb_quad);
  788. $size_data = strlen($bmp_data);
  789. }
  790. else
  791. {
  792. // ????????4???????
  793. $extra = '';
  794. $padding = 4 - ($width * ($bit / 8)) % 4;
  795. if ($padding % 4 != 0)
  796. {
  797. $extra = str_repeat("\0", $padding);
  798. }
  799. // ????
  800. $bmp_data = '';
  801. for ($j = $height - 1; $j >= 0; $j --)
  802. {
  803. for ($i = 0; $i < $width; $i ++)
  804. {
  805. $index = imagecolorat($im, $i, $j);
  806. $colors = imagecolorsforindex($im, $index);
  807. if ($bit == 16)
  808. {
  809. $bin = 0 << $bit;
  810. $bin |= ($colors['red'] >> 3) << 10;
  811. $bin |= ($colors['green'] >> 3) << 5;
  812. $bin |= $colors['blue'] >> 3;
  813. $bmp_data .= pack("v", $bin);
  814. }
  815. else
  816. {
  817. $bmp_data .= pack("c*", $colors['blue'], $colors['green'], $colors['red']);
  818. }
  819. // todo: 32bit;
  820. }
  821. $bmp_data .= $extra;
  822. }
  823. $size_quad = 0;
  824. $size_data = strlen($bmp_data);
  825. $colors_num = 0;
  826. }
  827. // ?????
  828. $file_header = "BM" . pack("V3", 54 + $size_quad + $size_data, 0, 54 + $size_quad);
  829. // ?????
  830. $info_header = pack("V3v2V*", 0x28, $width, $height, 1, $bit, $compression, $size_data, 0, 0, $colors_num, 0);
  831. // ????
  832. if ($filename != '')
  833. {
  834. $fp = fopen("test.bmp", "wb");
  835. fwrite($fp, $file_header);
  836. fwrite($fp, $info_header);
  837. fwrite($fp, $rgb_quad);
  838. fwrite($fp, $bmp_data);
  839. fclose($fp);
  840. return 1;
  841. }
  842. // ?????
  843. header("Content-Type: image/bmp");
  844. echo $file_header . $info_header;
  845. echo $rgb_quad;
  846. echo $bmp_data;
  847. return 1;
  848. }
  849. /**
  850. * ???????
  851. *
  852. * @param int $sTime ??????
  853. * @param string $type ??. normal | mohu | full | ymd | other
  854. * @param string $alt ???
  855. * @return string
  856. */
  857. function friendlyDate($sTime,$type = 'normal',$alt = 'false') {
  858. //sTime=????cTime=?????dTime=???
  859. $cTime = time();
  860. $dTime = $cTime - $sTime;
  861. $dDay = intval(date("Ymd",$cTime)) - intval(date("Ymd",$sTime));
  862. $dYear = intval(date("Y",$cTime)) - intval(date("Y",$sTime));
  863. //normal?n???n????n??????
  864. if($type=='normal'){
  865. if( $dTime < 60 ){
  866. return $dTime."??";
  867. }elseif( $dTime < 3600 ){
  868. return intval($dTime/60)."???";
  869. }elseif( $dTime >= 3600 && $dDay == 0 ){
  870. //return intval($dTime/3600)."???";
  871. return '??'.date('H:i',$sTime);
  872. }elseif($dYear==0){
  873. return date("m?d? H:i",$sTime);
  874. }else{
  875. return date("Y-m-d H:i",$sTime);
  876. }
  877. }elseif($type=='mohu'){
  878. if( $dTime < 60 ){
  879. return $dTime."??";
  880. }elseif( $dTime < 3600 ){
  881. return intval($dTime/60)."???";
  882. }elseif( $dTime >= 3600 && $dDay == 0 ){
  883. return intval($dTime/3600)."???";
  884. }elseif( $dDay > 0 && $dDay<3 ){
  885. return intval($dDay)."??";
  886. }elseif( $dDay >= 3 ){
  887. return "n??";
  888. }elseif( $dDay >= 30 ){
  889. return "n???";
  890. }
  891. //full: Y-m-d , H:i:s
  892. }elseif($type=='full'){
  893. return date("Y-m-d , H:i:s",$sTime);
  894. }elseif($type=='ymd'){
  895. return date("Y-m-d",$sTime);
  896. }else{
  897. if( $dTime < 60 ){
  898. return $dTime."??";
  899. }elseif( $dTime < 3600 ){
  900. return intval($dTime/60)."???";
  901. }elseif( $dTime >= 3600 && $dDay == 0 ){
  902. return intval($dTime/3600)."???";
  903. }elseif($dYear==0){
  904. return date("Y-m-d H:i:s",$sTime);
  905. }else{
  906. return date("Y-m-d H:i:s",$sTime);
  907. }
  908. }
  909. }
  910. //??????
  911. function getUserName($uid,$lang='zh'){
  912. static $_MapName = array();
  913. if(!isset($_MapName[$uid])){
  914. if(is_numeric($uid)){
  915. $map['uid'] = $uid;
  916. }else{
  917. $map['uname'] = $uid;
  918. }
  919. $userinfo = M('User')->where($map)->field('uname')->find();
  920. $_MapName[$uid] = $userinfo['uname'];
  921. }
  922. return htmlspecialchars($_MapName[$uid]);
  923. }
  924. //?????????
  925. function getUserSpace($uid,$class,$target,$text){
  926. static $_USERINFO = array();
  927. if(!isset($_USERINFO[$uid])){
  928. $map['uid'] = $uid;
  929. $_USERINFO[$uid] = M('User')->where($map)->field('uname,domain')->find();
  930. }
  931. $class = ($class)?$class:'username';
  932. $target = ($target)?$target:'_self';
  933. $text = ($text)?$text:$_USERINFO[$uid]['uname'];
  934. preg_match('|{(.*?)}|isU',$text,$t);
  935. if($t){
  936. if($t['1']=='uname'){
  937. $text = str_replace("{uname}", $_USERINFO[$uid]['uname'], $text);
  938. }else{
  939. $face = preg_replace("/{uavatar}|{uavatar\=(.*?)}/e", "getUserFace(\$uid, '\\1')", $text);
  940. //$face = preg_replace("/{uavatar}$/e", 'getUserFace(\$uid)', $text);
  941. $text = "<img src=".$face.">";
  942. }
  943. }
  944. if($_USERINFO[$uid]['domain']){
  945. $url = SITE_URL.'/'.$_USERINFO[$uid]['domain'];
  946. }else{
  947. $url = U('home/space/index',array('uid'=>$uid));
  948. }
  949. return "<a href='{$url}' class='{$class}' target='{$target}'>$text</a>";
  950. }
  951. //????????
  952. function getUserInfo($uid,$uname,$mid,$status=false){
  953. $data = array();
  954. if ( isset($uid) && $uid!=''){
  955. $map['uid'] = $uid;
  956. }elseif( isset($uname) && $uname!=''){
  957. $map['uname'] = $uname;
  958. }
  959. $user = M('user')->where($map)->find();
  960. if(!user) return false;
  961. $data['uid'] = $user['uid'];
  962. $data['uname'] = $user['uname'];
  963. $data['province'] = $user['province'];
  964. $data['city'] = $user['city'];
  965. $data['location'] = $user['location'];
  966. $data['face'] = getUserFace($user['uid']);
  967. $data['sex'] = getSex( $user['sex'] );
  968. $data['weibo_count'] = M('weibo')->where('uid='.$user['uid'])->count();
  969. $data['favorite_count'] = M('weibo_favorite')->where('uid='.$user['uid'])->count();
  970. $data['followers_count'] = M('weibo_follow')->where('fid='.$user['uid'].' AND type=0')->count();
  971. $data['followed_count'] = M('weibo_follow')->where('uid='.$user['uid'].' AND type=0')->count();
  972. $data['is_followed'] = getFollowState($mid,$uid);
  973. if($status){
  974. $status = M('weibo')->where('uid='.$user['uid'])->order('weibo_id DESC')->find();
  975. $data['status'] = ($status)?D('Weibo','weibo')->getOneApi('',$status):'';
  976. }
  977. return $data;
  978. }
  979. /**
  980. * ??????
  981. *
  982. * @param int $uid ??ID
  983. * @param int $fid ??ID
  984. * @return string eachfollow:???? | havefollow:??? | unfollow:??? | ?:?????
  985. */
  986. function getFollowState($uid,$fid){
  987. if($mid==$fid) return '';
  988. if(M('weibo_follow')->where("(uid=$uid AND fid=$fid AND type=0) OR (uid=$fid AND fid=$uid AND type=0)")->count()==2 ){
  989. return 'eachfollow';
  990. }elseif( M('weibo_follow')->where("uid=$uid AND fid=$fid AND type=0")->count() ){
  991. return 'havefollow';
  992. }else{
  993. return 'unfollow';
  994. }
  995. }
  996. /**
  997. * ??????????????
  998. *
  999. * @param int $weibo_id ??ID
  1000. * @param int $uid ??ID
  1001. * @return int ?????1, ????0
  1002. */
  1003. function isfavorited($weibo_id,$uid){
  1004. return M('weibo_favorite')->where("weibo_id=$weibo_id AND uid=$uid")->count();
  1005. }
  1006. /**
  1007. * ????????
  1008. *
  1009. * @param int $uid ??ID
  1010. * @param int $fid ??ID
  1011. * @return int
  1012. */
  1013. function isBlackList($uid,$fid){
  1014. return M('user_blacklist')->where("uid=$uid AND fid=$fid")->count();
  1015. }
  1016. /**
  1017. * ??????
  1018. *
  1019. * @param int $uid ??ID
  1020. * @param string $size ???? m:?? | s:?? | ??:??
  1021. * @return string ???URL??
  1022. */
  1023. function getUserFace($uid,$size){
  1024. $size = ($size)?$size:'m';
  1025. if($size=='m'){
  1026. $type = 'middle.jpg';
  1027. }elseif ($size=='s'){
  1028. $type = 'small.jpg';
  1029. }else{
  1030. $type = 'big.jpg';
  1031. }
  1032. $userface = SITE_PATH.'/data/uploads/avatar/'.$uid.'/'.$type;
  1033. if(is_file($userface)){
  1034. return SITE_URL."/data/uploads/avatar/$uid/$type";
  1035. }else{
  1036. return THEME_URL."/images/user_pic.gif";
  1037. }
  1038. }
  1039. //?????
  1040. function getUserGroupIcon($uid){
  1041. return Model('UserGroup')->getUserGroupIcon($uid);
  1042. }
  1043. //??????
  1044. /*
  1045. ?? 0?100 ??
  1046. ?? 101?500 ??
  1047. ?? 501?1000 ??
  1048. ?? 1001?2500 ??
  1049. ?? 2501?5000 ??
  1050. ?? 5001?8000 ?????
  1051. ?? 8001?12000 ?????
  1052. ?? 12001?16000 ????
  1053. ?? 16001?20000 ????
  1054. ?? 20001?25000 ??
  1055. ??? 25001?35000 ??
  1056. ??? 35001?50000 ??
  1057. ??? 50001?80000 ??
  1058. ??? 80001?120000 ??
  1059. ??? 120001?180000 ??
  1060. ??? 180001?250000 ???
  1061. ??? 250001?400000 ???
  1062. ??? 400001? ????
  1063. */
  1064. function getUserTitle($uid,$exp=false) {
  1065. $title = array(
  1066. '0-100'=>'??',
  1067. '101-500'=>'??',
  1068. '501-1000'=>'??',
  1069. '1001-2500'=>'??',
  1070. '2501-5000'=>'??',
  1071. '5001-8000'=>'?????',
  1072. '8001-12000'=>'?????',
  1073. '12001-16000'=>'????',
  1074. '16001-20000'=>'????',
  1075. '20001-25000'=>'??',
  1076. '25001-35000'=>'??',
  1077. '35001-50000'=>'??',
  1078. '50001-80000'=>'??',
  1079. '80001-120000'=>'??',
  1080. '120001-180000'=>'??',
  1081. '180001-250000'=>'???',
  1082. '250001-400000'=>'???',
  1083. '400001-99999999999'=>'????',
  1084. );
  1085. //????????
  1086. if($exp===false){
  1087. $space = M()->table(C('DB_PREFIX').'space')->field('credit_exp')->where(" uid = '$uid' ")->find();
  1088. $exp = $space['credit_exp'];
  1089. if(!$exp) return false;
  1090. }
  1091. //??????
  1092. foreach($title as $k=>$v){
  1093. $f = explode('-',$k);
  1094. if($exp>=$f[0] && $exp<=$f[1]){
  1095. return $v;
  1096. }
  1097. }
  1098. return false;
  1099. }
  1100. /**
  1101. * ?????????????????????????????????
  1102. * @param $pArray ??????
  1103. * @param $pKey ???????
  1104. * @return ????????
  1105. */
  1106. function getSubByKey($pArray, $pKey="", $pCondition=""){
  1107. $result = array();
  1108. foreach($pArray as $temp_array){
  1109. if(is_object($temp_array)){
  1110. $temp_array = (array) $temp_array;
  1111. }
  1112. if((""!=$pCondition && $temp_array[$pCondition[0]]==$pCondition[1]) || ""==$pCondition) {
  1113. $result[] = (""==$pKey) ? $temp_array : isset($temp_array[$pKey]) ? $temp_array[$pKey] : "";
  1114. }
  1115. }
  1116. return $result;
  1117. }
  1118. function getMultiArraySubByKey($pArray,$pKey=""){
  1119. $result = array();
  1120. $result = getSubByKey($temp_array,$pKey);
  1121. foreach($pArray as $temp_array){
  1122. if(is_object($temp_array)){
  1123. $temp_array = (array) $temp_array;
  1124. }
  1125. foreach ( $temp_array as $value){
  1126. if(is_array($value)){
  1127. $result = array_merge(getSubByKey($value,$pKey),$result);
  1128. }
  1129. }
  1130. }
  1131. return $result;
  1132. }
  1133. /**
  1134. * ???????????????????????????sql??????
  1135. * @param $pArray1 ??????
  1136. * @param $pArray2 ??????
  1137. * @param $pFields ?????????
  1138. * @param $pType ????????????????????????????????????????
  1139. * @return ??????
  1140. */
  1141. function arrayJoin($pArray1, $pArray2, $pFields, $pType="left"){
  1142. $result = array();
  1143. foreach($pArray1 as $row1)
  1144. {
  1145. $is_join = false;
  1146. foreach($pArray2 as $row2)
  1147. {
  1148. if(canJoin($row1, $row2, $pFields))
  1149. {
  1150. $result[] = array_merge($row2, $row1);
  1151. $is_join = true;
  1152. break;
  1153. }
  1154. }
  1155. //?????????????????
  1156. if($is_join==false && $pType=="left")
  1157. {
  1158. $result[] = $row1;
  1159. }
  1160. }
  1161. return $result;
  1162. }
  1163. /**
  1164. * ?????????????
  1165. * @author ??
  1166. * @createTime 2004-07-15
  1167. * @param $pRow1 ?????
  1168. * @param $pRow2 ?????
  1169. * @param $pFields ?????????
  1170. * @return ??????
  1171. */
  1172. function canJoin($pRow1, $pRow2, $pFields)
  1173. {
  1174. $field_array = explode(",", $pFields);
  1175. foreach($field_array as $key)
  1176. {
  1177. if(strtolower($pRow1[$key])!=strtolower($pRow2[$key]))
  1178. return false;
  1179. }
  1180. return true;
  1181. }
  1182. /**
  1183. * ???????????
  1184. *
  1185. * ???
  1186. * @code php
  1187. * $rows = array(
  1188. * array('id' => 1, 'value' => '1-1', 'parent' => 1),
  1189. * array('id' => 2, 'value' => '2-1', 'parent' => 1),
  1190. * array('id' => 3, 'value' => '3-1', 'parent' => 1),
  1191. * array('id' => 4, 'value' => '4-1', 'parent' => 2),
  1192. * array('id' => 5, 'value' => '5-1', 'parent' => 2),
  1193. * array('id' => 6, 'value' => '6-1', 'parent' => 3),
  1194. * );
  1195. *
  1196. * $rows = Helper_Array::sortByCol($rows, 'id', SORT_DESC);
  1197. * dump($rows);
  1198. * // ??????
  1199. * // array(
  1200. * // array('id' => 6, 'value' => '6-1', 'parent' => 3),
  1201. * // array('id' => 5, 'value' => '5-1', 'parent' => 2),
  1202. * // array('id' => 4, 'value' => '4-1', 'parent' => 2),
  1203. * // array('id' => 3, 'value' => '3-1', 'parent' => 1),
  1204. * // array('id' => 2, 'value' => '2-1', 'parent' => 1),
  1205. * // array('id' => 1, 'value' => '1-1', 'parent' => 1),
  1206. * // )
  1207. * @endcode
  1208. *
  1209. * @param array $array ??????
  1210. * @param string $keyname ????
  1211. * @param int $dir ????
  1212. *
  1213. * @return array ??????
  1214. */
  1215. function sortByCol($array, $keyname, $dir = SORT_ASC)
  1216. {
  1217. return sortByMultiCols($array, array($keyname => $dir));
  1218. }
  1219. /**
  1220. * ??????????????????? SQL ???? ORDER BY
  1221. *
  1222. * ???
  1223. * @code php
  1224. * $rows = Helper_Array::sortByMultiCols($rows, array(
  1225. * 'parent' => SORT_ASC,
  1226. * 'name' => SORT_DESC,
  1227. * ));
  1228. * @endcode
  1229. *
  1230. * @param array $rowset ??????
  1231. * @param array $args ????
  1232. *
  1233. * @return array ??????
  1234. */
  1235. function sortByMultiCols($rowset, $args)
  1236. {
  1237. $sortArray = array();
  1238. $sortRule = '';
  1239. foreach ($args as $sortField => $sortDir)
  1240. {
  1241. foreach ($rowset as $offset => $row)
  1242. {
  1243. $sortArray[$sortField][$offset] = $row[$sortField];
  1244. }
  1245. $sortRule .= '$sortArray[\'' . $sortField . '\'], ' . $sortDir . ', ';
  1246. }
  1247. if (empty($sortArray) || empty($sortRule)) { return $rowset; }
  1248. eval('array_multisort(' . $sortRule . '$rowset);');
  1249. return $rowset;
  1250. }
  1251. /**
  1252. * ???????Email
  1253. *
  1254. * @param int $uid
  1255. */
  1256. function getUserEmail($uid) {
  1257. $map ['uid'] = $uid;
  1258. return model ( 'User' )->where ( $map )->getField ( 'email' );
  1259. }
  1260. /**
  1261. * ??sexid????
  1262. *
  1263. * @param int $sexid
  1264. */
  1265. function getSex($sexid) {
  1266. return ($sexid == '1') ? "?" : "?";
  1267. }
  1268. function matchImages($content = '') {
  1269. $src = array ();
  1270. preg_match_all ( '/<img.*src=(.*)[>|\\s]/iU', $content, $src );
  1271. if (count ( $src [1] ) > 0) {
  1272. foreach ( $src [1] as $v ) {
  1273. $images [] = trim ( $v, "\"'" ); //??????? ' "
  1274. }
  1275. return $images;
  1276. } else {
  1277. return false;
  1278. }
  1279. }
  1280. function matchReplaceImages($content = ''){
  1281. $image = preg_replace_callback('/<img.*src=(.*)[>|\\s]/iU',"matchReplaceImagesOnce",$content);
  1282. return $image;
  1283. }
  1284. function matchReplaceImagesOnce($matches){
  1285. $matches[1] = str_replace('"','',$matches[1]);
  1286. return sprintf("<a class='thickbox' href='%s'>%s</a>",$matches[1],$matches[0]);
  1287. }
  1288. function getShort($str,$length=40) {
  1289. $str = htmlspecialchars($str);
  1290. $str = strip_tags($str);
  1291. $str = htmlspecialchars_decode($str);
  1292. $strlenth = 0;
  1293. $out = '';
  1294. preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/", $str, $match);
  1295. foreach($match[0] as $v){
  1296. preg_match("/[\xe0-\xef][\x80-\xbf]{2}/",$v, $matchs);
  1297. if(!empty($matchs[0])){
  1298. $strlenth += 1;
  1299. }elseif(is_numeric($v)){
  1300. $strlenth += 0.545;
  1301. }else{
  1302. $strlenth += 0.475;
  1303. }
  1304. if($strlenth>$length){
  1305. break;
  1306. }
  1307. $output .= $v;
  1308. }
  1309. return $output;
  1310. }
  1311. //????????????????
  1312. function infoCss($info){
  1313. if(empty($info)) return '';
  1314. else return "<div class='ico_cite C_C'>$info<span class='ico_cite2'></span></div>";
  1315. }
  1316. //????
  1317. function jiami($txt, $key = null) {
  1318. if (empty ( $key ))
  1319. $key = C ( 'SECURE_CODE' );
  1320. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=+";
  1321. $nh = rand ( 0, 64 );
  1322. $ch = $chars [$nh];
  1323. $mdKey = md5 ( $key . $ch );
  1324. $mdKey = substr ( $mdKey, $nh % 8, $nh % 8 + 7 );
  1325. $txt = base64_encode ( $txt );
  1326. $tmp = '';
  1327. $i = 0;
  1328. $j = 0;
  1329. $k = 0;
  1330. for($i = 0; $i < strlen ( $txt ); $i ++) {
  1331. $k = $k == strlen ( $mdKey ) ? 0 : $k;
  1332. $j = ($nh + strpos ( $chars, $txt [$i] ) + ord ( $mdKey [$k ++] )) % 64;
  1333. $tmp .= $chars [$j];
  1334. }
  1335. return $ch . $tmp;
  1336. }
  1337. //????
  1338. function jiemi($txt, $key = null) {
  1339. if (empty ( $key ))
  1340. $key = C ( 'SECURE_CODE' );
  1341. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=+";
  1342. $ch = $txt [0];
  1343. $nh = strpos ( $chars, $ch );
  1344. $mdKey = md5 ( $key . $ch );
  1345. $mdKey = substr ( $mdKey, $nh % 8, $nh % 8 + 7 );
  1346. $txt = substr ( $txt, 1 );
  1347. $tmp = '';
  1348. $i = 0;
  1349. $j = 0;
  1350. $k = 0;
  1351. for($i = 0; $i < strlen ( $txt ); $i ++) {
  1352. $k = $k == strlen ( $mdKey ) ? 0 : $k;
  1353. $j = strpos ( $chars, $txt [$i] ) - $nh - ord ( $mdKey [$k ++] );
  1354. while ( $j < 0 )
  1355. $j += 64;
  1356. $tmp .= $chars [$j];
  1357. }
  1358. return base64_decode ( $tmp );
  1359. }
  1360. /**
  1361. * Format a mySQL string correctly for safe mySQL insert (no mater if magic quotes are on or not)
  1362. */
  1363. function escape($str) {
  1364. return mysql_escape_string(stripslashes($str));
  1365. }
  1366. /**
  1367. * ????IP?????
  1368. *
  1369. * @param string $ip
  1370. * @return string
  1371. */
  1372. function convert_ip($ip) {
  1373. $return = '';
  1374. if(preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip)) {
  1375. $iparray = explode('.', $ip);
  1376. if($iparray[0] == 10 || $iparray[0] == 127 || ($iparray[0] == 192 && $iparray[1] == 168) || ($iparray[0] == 172 && ($iparray[1] >= 16 && $iparray[1] <= 31))) {
  1377. $return = '- LAN';
  1378. } elseif($iparray[0] > 255 || $iparray[1] > 255 || $iparray[2] > 255 || $iparray[3] > 255) {
  1379. $return = '- Invalid IP Address';
  1380. } else {
  1381. $tinyipfile = ADDON_PATH . '/libs/misc/tinyipdata.dat';
  1382. $fullipfile = ADDON_PATH . '/libs/misc/wry.dat';
  1383. if(@file_exists($tinyipfile)) {
  1384. $return = convert_ip_tiny($ip, $tinyipfile);
  1385. } elseif(@file_exists($fullipfile)) {
  1386. $return = convert_ip_full($ip, $fullipfile);
  1387. }
  1388. }
  1389. }
  1390. $return = iconv('GBK', 'UTF-8', $return);
  1391. return $retu

Large files files are truncated, but you can click here to view the full file