PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Includes/common.inc.php

http://nblog-thinkphp.googlecode.com/
PHP | 939 lines | 585 code | 80 blank | 274 comment | 91 complexity | 5274b4f2ed3a5b252916a414332be98e MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ?????
  4. // +----------------------------------------------------------------------
  5. // | @link ( http://www.yurnero.net )
  6. // +----------------------------------------------------------------------
  7. // | @copyright
  8. // +----------------------------------------------------------------------
  9. // | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. // | @author Haijun Wu <nicholasinlove@126.com>
  12. // +----------------------------------------------------------------------
  13. // | $Id: common.inc.php 117 2011-05-12 03:42:14Z nicholasinlove1986@gmail.com $
  14. // +----------------------------------------------------------------------
  15. if (!defined('IN_BLOG')) {
  16. die('Hacking attempt');
  17. }
  18. /**
  19. * ????ip
  20. *
  21. * @return string
  22. */
  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. if(function_exists("mb_substr")) {
  54. if ($suffix && strlen($str)>$length)
  55. return mb_substr($str, $start, $length, $charset)."...";
  56. else
  57. return mb_substr($str, $start, $length, $charset);
  58. }
  59. elseif (function_exists('iconv_substr')) {
  60. if ($suffix && strlen($str)>$length)
  61. return iconv_substr($str,$start,$length,$charset)."...";
  62. else
  63. return iconv_substr($str,$start,$length,$charset);
  64. }
  65. $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  66. $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  67. $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  68. $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  69. preg_match_all($re[$charset], $str, $match);
  70. $slice = join("",array_slice($match[0], $start, $length));
  71. if($suffix) return $slice."...";
  72. return $slice;
  73. }
  74. /**
  75. * ??????????????
  76. *
  77. * @return integer
  78. */
  79. function gmtime() {
  80. return (time() - date('Z'));
  81. }
  82. /**
  83. * ????????
  84. *
  85. * @return integer
  86. */
  87. function server_timezone() {
  88. if (function_exists('date_default_timezone_get')) {
  89. return date_default_timezone_get();
  90. } else {
  91. return date('Z') / 3600;
  92. }
  93. }
  94. /**
  95. * ??????????????GMT???
  96. *
  97. * @access public
  98. * @param int $hour
  99. * @param int $minute
  100. * @param int $second
  101. * @param int $month
  102. * @param int $day
  103. * @param int $year
  104. *
  105. * @return void
  106. */
  107. function local_mktime($hour = NULL , $minute= NULL, $second = NULL, $month = NULL, $day = NULL, $year = NULL) {
  108. $timezone = C('server_timezone')? C('server_timezone') : '8';
  109. /**
  110. * $time = mktime($hour, $minute, $second, $month, $day, $year) - date('Z') + (date('Z') - $timezone * 3600)
  111. * ??mktime?????????date('Z')???GMT????????????????????????
  112. **/
  113. $time = mktime($hour, $minute, $second, $month, $day, $year) - $timezone * 3600;
  114. return $time;
  115. }
  116. /**
  117. * ???????????????GMT???
  118. *
  119. * @access public
  120. * @param string $str
  121. *
  122. * @return integer
  123. */
  124. function local_strtotime($str) {
  125. $timezone = C('server_timezone')? C('server_timezone') : '8';
  126. /**
  127. * $time = mktime($hour, $minute, $second, $month, $day, $year) - date('Z') + (date('Z') - $timezone * 3600)
  128. * ??mktime?????????date('Z')???GMT????????????????????????
  129. **/
  130. $time = strtotime($str) - $timezone * 3600;
  131. return $time;
  132. }
  133. /**
  134. * ?GMT????????????????
  135. *
  136. * @param string $format
  137. * @param integer $time ????????GMT????
  138. *
  139. * @return string
  140. */
  141. function local_date($format, $time = NULL) {
  142. $timezone = C('server_timezone')? C('server_timezone') : '8';
  143. if ($time === NULL) {
  144. $time = gmtime();
  145. } elseif ($time <= 0) {
  146. return '';
  147. }
  148. $time += ($timezone * 3600);
  149. return gmdate($format, $time);
  150. }
  151. /**
  152. * ??????????????????
  153. *
  154. * @param $timestamp integer ??????????????????
  155. *
  156. * @return array
  157. */
  158. function local_getdate($timestamp = NULL) {
  159. $timezone = C('server_timezone')? C('server_timezone') : '8';
  160. /* ??????????????????? */
  161. if ($timestamp === NULL) {
  162. $timestamp = time();
  163. }
  164. $gmt = $timestamp - date('Z'); // ????????????
  165. $local_time = $gmt + ($timezone * 3600); // ?????????????
  166. return getdate($local_time);
  167. }
  168. /**
  169. * ??????
  170. *
  171. * @access public
  172. * @return void
  173. */
  174. function get_config($rewrite = false) {
  175. $php_self = addslashes(htmlspecialchars($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']));
  176. $path = ROOT_PATH.STATIC_CACHES_DIR;
  177. $arr = array();
  178. if (file_exists($path.'system_config_static.php')) {
  179. $arr = f_static_cache('system_config_static','',$path);
  180. } else {
  181. $conf = M('Config');
  182. $data = $conf->findAll();
  183. foreach ($data as $row) {
  184. $arr[$row['config_name']] = $row['config_value'];
  185. }
  186. $arr['blog_domain'] = !empty($arr['blog_domain']) ? trim($arr['blog_domain']) : 'http://'.$_SERVER['HTTP_HOST'].dirname($php_self).'/';
  187. //??
  188. $arr['posts_num'] = !empty($arr['posts_num']) ? intval($arr['posts_num']) : 10;
  189. $arr['related_posts_num'] = !empty($arr['related_posts_num']) ? intval($arr['related_posts_num']) : 0;
  190. $arr['comment_num'] = !empty($arr['comment_num']) ? intval($arr['comment_num']) : 15;
  191. $arr['guestbook_num'] = !empty($arr['guestbook_num']) ? intval($arr['guestbook_num']) : 15;
  192. $arr['rss_num'] = !empty($arr['rss_num']) ? intval($arr['rss_num']) : 15;
  193. $arr['admin_list_num'] = !empty($arr['admin_list_num']) ? intval($arr['admin_list_num']) : 15;
  194. $arr['page_roll'] = !empty($arr['page_roll']) ? intval($arr['page_roll']) : 5;
  195. $arr['posts_title_limit'] = !empty($arr['posts_title_limit']) ? intval($arr['posts_title_limit']) : 0;
  196. $arr['related_title_limit'] = !empty($arr['related_title_limit']) ? intval($arr['related_title_limit']) : 0;
  197. $arr['recent_comments_num'] = !empty($arr['recent_comments_num']) ? intval($arr['recent_comments_num']) : 10;
  198. $arr['recent_comments_limit'] = !empty($arr['recent_comments_limit']) ? intval($arr['recent_comments_limit']) : 15;
  199. $arr['comment_minlength'] = !empty($arr['comment_minlength']) ? intval($arr['comment_minlength']) : 4;
  200. $arr['comment_maxlength'] = !empty($arr['comment_maxlength']) ? intval($arr['comment_maxlength']) : 6000;
  201. $arr['trackback_num'] = !empty($arr['trackback_num']) ? intval($arr['trackback_num']) : 10;
  202. f_static_cache('system_config_static',$arr ,$path);
  203. }
  204. if ($rewrite) {
  205. $path_ = ROOT_PATH.'Includes/config.user.php';
  206. $userConfig = "<?php\r\n// +----------------------------------------------------------------------
  207. // | ??????
  208. // +----------------------------------------------------------------------
  209. // | @link
  210. // +----------------------------------------------------------------------
  211. // | @copyright
  212. // +----------------------------------------------------------------------
  213. // | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  214. // +----------------------------------------------------------------------
  215. // | @author Haijun Wu <nicholasinlove@126.com>
  216. // +----------------------------------------------------------------------
  217. // | \$Id\$
  218. // +----------------------------------------------------------------------\r\n\r\n
  219. if (!defined('THINK_PATH')) exit();\r\nreturn array(\r\n";
  220. $userConfig .= "\t'URL_MODEL'=>".$arr['url_model'].",\r\n";
  221. if ($arr['enable_html']) {
  222. $userConfig .= "\t'URL_HTML_SUFFIX'=>'".$arr['html_ext']."',\r\n";
  223. }
  224. $userConfig .= ");\r\n?>";
  225. file_put_contents($path_,$userConfig);
  226. }
  227. foreach ($arr as $key => $v) {
  228. C($key,$v);
  229. }
  230. C('COOKIEPATH', preg_replace('|https?://[^/]+|i', '', C('blog_domain')));
  231. C('COOKIEHASH', md5(C('blog_domain')));
  232. C('COOKIEDOMAIN', false);
  233. if (!C('blog_domain')) {
  234. C('blog_domain','http://'.$_SERVER['HTTP_HOST'].dirname($php_self).'/');
  235. }
  236. return $arr;
  237. }
  238. /**
  239. * url????
  240. *
  241. * @access public
  242. * @return string
  243. */
  244. function url_encode($string) {
  245. return urlencode($string);
  246. }
  247. function url_decode($string) {
  248. return urldecode($string);
  249. }
  250. // ??????????? ???????? ??????(???
  251. function f_static_cache($name,$value='',$path=DATA_PATH) {
  252. static $_cache = array();
  253. $filename = $path.$name.'.php';
  254. if('' !== $value) {
  255. if(is_null($value)) {
  256. // ????
  257. return unlink($filename);
  258. }else{
  259. // ????
  260. $dir = dirname($filename);
  261. // ????????
  262. if(!is_dir($dir)) mkdir($dir);
  263. return file_put_contents($filename,"<?php\n//nblog cache file\n//Created on " . date('Y-m-d H:i:s',time()) . "\n\nif (!defined('IN_BLOG')) exit('Hacking attempt');\n\n\$data = ".var_export($value,true)."\n?>");
  264. }
  265. }
  266. if(isset($_cache[$name])) return $_cache[$name];
  267. // ??????
  268. if(is_file($filename)) {
  269. include ($filename);
  270. $_cache[$name] = $data;
  271. }else{
  272. $data = false;
  273. }
  274. return $data;
  275. }
  276. /**
  277. +----------------------------------------------------------
  278. * ????
  279. +----------------------------------------------------------
  280. * @param String $str ????????? ?? ???
  281. * @param Boolean $show ????
  282. +----------------------------------------------------------
  283. * @return String
  284. +----------------------------------------------------------
  285. */
  286. function highlight_code($str,$show=false) {
  287. if(file_exists($str)) {
  288. $str = file_get_contents($str);
  289. }
  290. $str = stripslashes(trim($str));
  291. // The highlight string function encodes and highlights
  292. // brackets so we need them to start raw
  293. $str = str_replace(array('&lt;', '&gt;'), array('<', '>'), $str);
  294. // Replace any existing PHP tags to temporary markers so they don't accidentally
  295. // break the string out of PHP, and thus, thwart the highlighting.
  296. $str = str_replace(array('&lt;?php', '?&gt;', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str);
  297. // The highlight_string function requires that the text be surrounded
  298. // by PHP tags. Since we don't know if A) the submitted text has PHP tags,
  299. // or B) whether the PHP tags enclose the entire string, we will add our
  300. // own PHP tags around the string along with some markers to make replacement easier later
  301. $str = '<?php //tempstart'."\n".$str.'//tempend ?>'; // <?
  302. // All the magic happens here, baby!
  303. $str = highlight_string($str, TRUE);
  304. // Prior to PHP 5, the highlight function used icky font tags
  305. // so we'll replace them with span tags.
  306. if (abs(phpversion()) < 5) {
  307. $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str);
  308. $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str);
  309. }
  310. // Remove our artificially added PHP
  311. $str = preg_replace("#\<code\>.+?//tempstart\<br />\</span\>#is", "<code>\n", $str);
  312. $str = preg_replace("#\<code\>.+?//tempstart\<br />#is", "<code>\n", $str);
  313. $str = preg_replace("#//tempend.+#is", "</span>\n</code>", $str);
  314. // Replace our markers back to PHP tags.
  315. $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('&lt;?php', '?&gt;', '\\'), $str); //<?
  316. $line = explode("<br />", rtrim(ltrim($str,'<code>'),'</code>'));
  317. $result = '<div class="code"><ol>';
  318. foreach($line as $key=>$val) {
  319. $result .= '<li>'.$val.'</li>';
  320. }
  321. $result .= '</ol></div>';
  322. $result = str_replace("\n", "", $result);
  323. if( $show!== false) {
  324. echo($result);
  325. }else {
  326. return $result;
  327. }
  328. }
  329. /**
  330. * ??trackback
  331. *
  332. * @access public
  333. * @return String
  334. */
  335. function trackback_send($url, $data) {
  336. $uinfo = parse_url($url);
  337. if ($uinfo['query']) {
  338. $data .= '&'.$uinfo['query'];
  339. }
  340. if (!$fp = @fsockopen($uinfo['host'], ($uinfo['port'] ? $uinfo['port'] : '80'), $errno, $errstr, 3)) {
  341. return false;
  342. }
  343. fputs ($fp, "POST ".$uinfo['path']." HTTP/1.1\r\n");
  344. fputs ($fp, "Host: ".$uinfo['host']."\r\n");
  345. fputs ($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  346. fputs ($fp, "Content-length: ".strlen($data)."\r\n");
  347. fputs ($fp, "Connection: close\r\n\r\n");
  348. fputs ($fp, $data);
  349. $http_response = '';
  350. while(!feof($fp)) {
  351. $http_response .= fgets($fp, 128);
  352. }
  353. @fclose($fp);
  354. list($http_headers, $http_content) = explode('\r\n\r\n', $http_response);
  355. return $http_response;
  356. }
  357. /**
  358. * ??????????????????
  359. *
  360. * @access public
  361. * @param mix $value
  362. *
  363. * @return mix
  364. */
  365. function addslashes_deep($value) {
  366. if (empty($value)) {
  367. return $value;
  368. } else {
  369. return is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
  370. }
  371. }
  372. /**
  373. * ??????????????????
  374. *
  375. * @access public
  376. * @param mix $value
  377. *
  378. * @return mix
  379. */
  380. function stripslashes_deep($value) {
  381. if (empty($value)) {
  382. return $value;
  383. } else {
  384. return is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
  385. }
  386. }
  387. /**
  388. * ????????? gzip
  389. *
  390. * @access public
  391. *
  392. * @return boolean
  393. */
  394. function gzip_enabled() {
  395. static $enabled_gzip = NULL;
  396. if ($enabled_gzip === NULL) {
  397. $enabled_gzip = (C('gzip') && function_exists('ob_gzhandler'));
  398. }
  399. return $enabled_gzip;
  400. }
  401. /**
  402. * ????? html
  403. *
  404. * @access public
  405. *
  406. * @return string
  407. */
  408. function h($text, $tags = null){
  409. $text = trim($text);
  410. //??????
  411. $text = preg_replace('/<!--?.*-->/','',$text);
  412. //????????
  413. $text = preg_replace('/<\?|\?'.'>/','',$text);
  414. //????js
  415. $text = preg_replace('/<script?.*\/script>/','',$text);
  416. $text = str_replace('[','&#091;',$text);
  417. $text = str_replace(']','&#093;',$text);
  418. $text = str_replace('|','&#124;',$text);
  419. //?????
  420. $text = preg_replace('/\r?\n/','',$text);
  421. //br
  422. $text = preg_replace('/<br(\s\/)?'.'>/i','[br]',$text);
  423. $text = preg_replace('/(\[br\]\s*){10,}/i','[br]',$text);
  424. //????????????on??lang js
  425. while(preg_match('/(<[^><]+)( lang|on|action|background|codebase|dynsrc|lowsrc)[^><]+/i',$text,$mat)){
  426. $text=str_replace($mat[0],$mat[1],$text);
  427. }
  428. while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i',$text,$mat)){
  429. $text=str_replace($mat[0],$mat[1].$mat[3],$text);
  430. }
  431. if(empty($tags)) {
  432. $tags = 'table|td|th|tr|i|b|u|strong|img|p|br|div|strong|em|ul|ol|li|dl|dd|dt|a';
  433. }
  434. //???HTML??
  435. $text = preg_replace('/<('.$tags.')( [^><\[\]]*)>/i','[\1\2]',$text);
  436. //????html
  437. $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);
  438. //?????html??
  439. while(preg_match('/<([a-z]+)[^><\[\]]*>[^><]*<\/\1>/i',$text,$mat)){
  440. $text=str_replace($mat[0],str_replace('>',']',str_replace('<','[',$mat[0])),$text);
  441. }
  442. //????
  443. while(preg_match('/(\[[^\[\]]*=\s*)(\"|\')([^\2=\[\]]+)\2([^\[\]]*\])/i',$text,$mat)){
  444. $text=str_replace($mat[0],$mat[1].'|'.$mat[3].'|'.$mat[4],$text);
  445. }
  446. //?????????
  447. while(preg_match('/\[[^\[\]]*(\"|\')[^\[\]]*\]/i',$text,$mat)){
  448. $text=str_replace($mat[0],str_replace($mat[1],'',$mat[0]),$text);
  449. }
  450. //?????????? < >
  451. $text = str_replace('<','&lt;',$text);
  452. $text = str_replace('>','&gt;',$text);
  453. $text = str_replace('"','&quot;',$text);
  454. //???
  455. $text = str_replace('[','<',$text);
  456. $text = str_replace(']','>',$text);
  457. $text = str_replace('|','"',$text);
  458. //??????
  459. $text = str_replace(' ',' ',$text);
  460. return $text;
  461. }
  462. /**
  463. * ????????
  464. *
  465. * @access public
  466. *
  467. * @return string
  468. */
  469. function auto_encode($string,$outEncoding = 'UTF-8') {
  470. $encoding = "UTF-8";
  471. for($i=0;$i<strlen($string);$i++) {
  472. if(ord($string{$i})<128)
  473. continue;
  474. if((ord($string{$i})&224)==224) {
  475. //?????????
  476. $char = $string{++$i};
  477. if((ord($char)&128)==128) {
  478. //?????????
  479. $char = $string{++$i};
  480. if((ord($char)&128)==128) {
  481. $encoding = "UTF-8";
  482. break;
  483. }
  484. }
  485. }
  486. if((ord($string{$i})&192)==192){
  487. //?????????
  488. $char = $string{++$i};
  489. if((ord($char)&128)==128) {
  490. //?????????
  491. $encoding = "GB2312";
  492. break;
  493. }
  494. }
  495. }
  496. if(strtoupper($encoding) == strtoupper($outEncoding))
  497. return $string;
  498. else
  499. return iconv($encoding,$outEncoding,$string);
  500. }
  501. /**
  502. * ???????,?????
  503. *
  504. * @access public
  505. *
  506. * @return array
  507. */
  508. function get_posts_from_date($start_time,$end_time) {
  509. $posts = M('Posts');
  510. $list = $posts->where("posts_status = 1 and posts_type = 1 and posts_draft = 1 and posts_addtime >=$start_time and posts_addtime <$end_time")->field('posts_addtime')->select();
  511. $posts_nums = array();
  512. if(is_array($list)) {
  513. foreach ($list as $key =>$v) {
  514. foreach ($v as $k=>$c){
  515. $day = local_date('j', $c);
  516. if (!isset($posts_nums[$day])) {
  517. $posts_nums[$day] = 1;
  518. } else {
  519. $posts_nums[$day]++;
  520. }
  521. }
  522. }
  523. return $posts_nums;
  524. }
  525. }
  526. /**
  527. * Get either a Gravatar URL or complete image tag for a specified email address.
  528. *
  529. * @param string $email The email address
  530. * @param string $s Size in pixels, defaults to 80px [ 1 - 512 ]
  531. * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
  532. * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
  533. * @param boole $img True to return a complete IMG tag False for just the URL
  534. * @param array $atts Optional, additional key/value attributes to include in the IMG tag
  535. * @return String containing either just a URL or a complete image tag
  536. * @source http://gravatar.com/site/implement/images/php/
  537. */
  538. function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
  539. $url = 'http://www.gravatar.com/avatar/';
  540. $url .= md5( strtolower( trim( $email ) ) );
  541. $url .= "?s=$s&d=$d&r=$r";
  542. if ( $img ) {
  543. $url = '<img src="' . $url . '"';
  544. foreach ( $atts as $key => $val )
  545. $url .= ' ' . $key . '="' . $val . '"';
  546. $url .= ' />';
  547. }
  548. return $url;
  549. }
  550. /**
  551. * Convert text equivalent of smilies to images.
  552. *
  553. * Will only convert smilies if the option 'use_smilies' is true and the global
  554. * used in the function isn't empty.
  555. *
  556. * @since 0.71
  557. * @uses $wp_smiliessearch
  558. *
  559. * @param string $text Content to convert smilies from text.
  560. * @return string Converted content with text smilies replaced with images.
  561. */
  562. function convert_smilies($text) {
  563. global $wp_smiliessearch;
  564. $output = '';
  565. if (!empty($wp_smiliessearch) ) {
  566. // HTML loop taken from texturize function, could possible be consolidated
  567. $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
  568. $stop = count($textarr);// loop stuff
  569. for ($i = 0; $i < $stop; $i++) {
  570. $content = $textarr[$i];
  571. if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
  572. $content = preg_replace_callback($wp_smiliessearch, 'translate_smiley', $content);
  573. }
  574. $output .= $content;
  575. }
  576. } else {
  577. // return default text.
  578. $output = $text;
  579. }
  580. return $output;
  581. }
  582. /**
  583. * Convert one smiley code to the icon graphic file equivalent.
  584. *
  585. * Looks up one smiley code in the $wpsmiliestrans global array and returns an
  586. * <img> string for that smiley.
  587. *
  588. * @global array $wpsmiliestrans
  589. * @since 2.8.0
  590. *
  591. * @param string $smiley Smiley code to convert to image.
  592. * @return string Image string for smiley.
  593. */
  594. function translate_smiley($smiley) {
  595. global $wpsmiliestrans;
  596. if (count($smiley) == 0) {
  597. return '';
  598. }
  599. $smiley = trim(reset($smiley));
  600. $img = $wpsmiliestrans[$smiley];
  601. return " <img src=\"" . WEB_PUBLIC_PATH . "/Plugins/emoticons/smilies/$img\" alt='$smiley' class='wp-smiley'/> ";
  602. }
  603. /**
  604. * Convert smiley code to the icon graphic file equivalent.
  605. *
  606. * You can turn off smilies, by going to the write setting screen and unchecking
  607. * the box, or by setting 'use_smilies' option to false or removing the option.
  608. *
  609. * Plugins may override the default smiley list by setting the $wpsmiliestrans
  610. * to an array, with the key the code the blogger types in and the value the
  611. * image file.
  612. *
  613. * The $wp_smiliessearch global is for the regular expression and is set each
  614. * time the function is called.
  615. *
  616. * The full list of smilies can be found in the function and won't be listed in
  617. * the description. Probably should create a Codex page for it, so that it is
  618. * available.
  619. *
  620. * @global array $wpsmiliestrans
  621. * @global array $wp_smiliessearch
  622. * @since 2.2.0
  623. */
  624. function smilies_init() {
  625. global $wpsmiliestrans, $wp_smiliessearch;
  626. if ( !isset( $wpsmiliestrans ) ) {
  627. $wpsmiliestrans = array(
  628. ':mrgreen:' => 'icon_mrgreen.gif',
  629. ':neutral:' => 'icon_neutral.gif',
  630. ':twisted:' => 'icon_twisted.gif',
  631. ':arrow:' => 'icon_arrow.gif',
  632. ':shock:' => 'icon_eek.gif',
  633. ':smile:' => 'icon_smile.gif',
  634. ':???:' => 'icon_confused.gif',
  635. ':cool:' => 'icon_cool.gif',
  636. ':evil:' => 'icon_evil.gif',
  637. ':grin:' => 'icon_biggrin.gif',
  638. ':idea:' => 'icon_idea.gif',
  639. ':oops:' => 'icon_redface.gif',
  640. ':razz:' => 'icon_razz.gif',
  641. ':roll:' => 'icon_rolleyes.gif',
  642. ':wink:' => 'icon_wink.gif',
  643. ':cry:' => 'icon_cry.gif',
  644. ':eek:' => 'icon_surprised.gif',
  645. ':lol:' => 'icon_lol.gif',
  646. ':mad:' => 'icon_mad.gif',
  647. ':sad:' => 'icon_sad.gif',
  648. '8-)' => 'icon_cool.gif',
  649. '8-O' => 'icon_eek.gif',
  650. ':-(' => 'icon_sad.gif',
  651. ':-)' => 'icon_smile.gif',
  652. ':-?' => 'icon_confused.gif',
  653. ':-D' => 'icon_biggrin.gif',
  654. ':-P' => 'icon_razz.gif',
  655. ':-o' => 'icon_surprised.gif',
  656. ':-x' => 'icon_mad.gif',
  657. ':-|' => 'icon_neutral.gif',
  658. ';-)' => 'icon_wink.gif',
  659. '8)' => 'icon_cool.gif',
  660. '8O' => 'icon_eek.gif',
  661. ':(' => 'icon_sad.gif',
  662. ':)' => 'icon_smile.gif',
  663. ':?' => 'icon_confused.gif',
  664. ':D' => 'icon_biggrin.gif',
  665. ':P' => 'icon_razz.gif',
  666. ':o' => 'icon_surprised.gif',
  667. ':x' => 'icon_mad.gif',
  668. ':|' => 'icon_neutral.gif',
  669. ';)' => 'icon_wink.gif',
  670. ':!:' => 'icon_exclaim.gif',
  671. ':?:' => 'icon_question.gif',
  672. );
  673. }
  674. if (count($wpsmiliestrans) == 0) {
  675. return;
  676. }
  677. /*
  678. * NOTE: we sort the smilies in reverse key order. This is to make sure
  679. * we match the longest possible smilie (:???: vs :?) as the regular
  680. * expression used below is first-match
  681. */
  682. krsort($wpsmiliestrans);
  683. $wp_smiliessearch = '/(?:\s|^)';
  684. $subchar = '';
  685. foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
  686. $firstchar = substr($smiley, 0, 1);
  687. $rest = substr($smiley, 1);
  688. // new subpattern?
  689. if ($firstchar != $subchar) {
  690. if ($subchar != '') {
  691. $wp_smiliessearch .= ')|(?:\s|^)';
  692. }
  693. $subchar = $firstchar;
  694. $wp_smiliessearch .= preg_quote($firstchar, '/') . '(?:';
  695. } else {
  696. $wp_smiliessearch .= '|';
  697. }
  698. $wp_smiliessearch .= preg_quote($rest, '/');
  699. }
  700. $wp_smiliessearch .= ')(?:\s|$)/m';
  701. }
  702. /**
  703. * ???????????
  704. *
  705. * @access public
  706. * @param array/string $comment_id ?????????
  707. * @param string $status ??
  708. *
  709. * @return bool
  710. */
  711. function comment_init($comment_id, $status) {
  712. $posts = M('Posts');
  713. $com = M('Comment');
  714. // ????
  715. if (is_string($comment_id)) {
  716. $posts_id = $com->where("comment_id=$comment_id")->getField('posts_id');
  717. if ($posts_id != '0') {
  718. if ($status) {
  719. $posts->setInc('posts_comment', "posts_id = '$posts_id'");
  720. } else {
  721. $posts->setDec('posts_comment', "posts_id = '$posts_id'");
  722. }
  723. }
  724. }
  725. // ????
  726. if (is_array($comment_id)) {
  727. foreach ($comment_id as $comment_id) {
  728. comment_init($comment_id, $status);
  729. }
  730. }
  731. }
  732. /**
  733. * ???????????,??????
  734. *
  735. * @access public
  736. *
  737. * @return void
  738. */
  739. function rebuild_posts() {
  740. $posts = D('Posts');
  741. $com = M('Comment');
  742. $tb = M('Trackback');
  743. $res = $posts->field('posts_id')->relation(true)->findAll();
  744. $res_count = count($res);
  745. for ($i=0; $i<$res_count; $i++) {
  746. if ($res[$i]['posts_id'] == '') continue;
  747. $comment_map = array();
  748. $posts_map = array();
  749. $tb_map = array();
  750. $posts_map['posts_id'] = $res[$i]['posts_id'];
  751. $comment_map['posts_id'] = $res[$i]['posts_id'];
  752. $comment_map['comment_status'] = 1;
  753. $comment_map['comment_visible'] = 1;
  754. $comment_map['comment_type'] = 1;
  755. $tb_map['posts_id'] = $res[$i]['posts_id'];
  756. $tb_map['tb_visible'] = 1;
  757. if (C('remark')) {
  758. $comment_map['comment_remark'] = 1;
  759. }
  760. $comment_num = $com->where($comment_map)->count();
  761. $trackback_num = $tb->where($tb_map)->count();
  762. if (is_array($res[$i]['tag']) && !empty($res[$i]['tag'])) {
  763. foreach ($res[$i]['tag'] as $k => $v) {
  764. $str .= implode(',',$v).',';
  765. }
  766. $str = substr($str,0,strlen($str)-1);
  767. $posts->where($posts_map)->setField(array('posts_comment','posts_tracknum','posts_tag'),array($comment_num,$trackback_num,$str));
  768. $str = '';
  769. } else {
  770. $posts->where($posts_map)->setField(array('posts_comment','posts_tracknum'),array($comment_num,$trackback_num));
  771. }
  772. }
  773. //clear_compiled_files();
  774. }
  775. /**
  776. * ?????????
  777. *
  778. * @access public
  779. *
  780. * @return void
  781. */
  782. function rebuild_comment() {
  783. $posts = M('Posts');
  784. $com = M('Comment');
  785. $res = $posts->field('posts_id')->findAll();
  786. $res_count = count($res);
  787. for($i=0; $i<$res_count; $i++) {
  788. if($res[$i]['posts_id'] == '') continue;
  789. $comment_map = array();
  790. $posts_map = array();
  791. $posts_map['posts_id'] = $res[$i]['posts_id'];
  792. $comment_map['posts_id'] = $res[$i]['posts_id'];
  793. $comment_map['comment_status'] = 1;
  794. $comment_map['comment_visible'] = 1;
  795. $comment_map['comment_type'] = 1;
  796. if (C('remark')) {
  797. $comment_map['comment_remark'] = 1;
  798. }
  799. $comment_num = $com->where($comment_map)->count();
  800. $posts->where($posts_map)->setField('posts_comment', $comment_num);
  801. }
  802. }
  803. /**
  804. * ?????????
  805. *
  806. * @access public
  807. *
  808. * @return void
  809. */
  810. function rebuild_trackback() {
  811. $posts = M('Posts');
  812. $tb = M('Trackback');
  813. $res = $posts->field('posts_id')->findAll();
  814. $res_count = count($res);
  815. for($i=0; $i<$res_count; $i++) {
  816. if($res[$i]['posts_id'] == '') continue;
  817. $posts_map = array();
  818. $tb_map = array();
  819. $posts_map['posts_id'] = $res[$i]['posts_id'];
  820. $tb_map['posts_id'] = $res[$i]['posts_id'];
  821. $tb_map['tb_visible'] = 1;
  822. $trackback_num = $tb->where($tb_map)->count();
  823. $posts->where($posts_map)->setField('posts_tracknum',$trackback_num);
  824. }
  825. }
  826. /**
  827. * ????????
  828. *
  829. * @access public
  830. *
  831. * @return array
  832. */
  833. function rebuild_tag() {
  834. $posts = D('Posts');
  835. $posts_map = array();
  836. $list = $posts->field('posts_id')->relation(true)->findAll();
  837. $count = count($list);
  838. for ($i=0; $i<$count; $i++) {
  839. if (is_array($list[$i]['tag']) && !empty($list[$i]['tag'])) {
  840. foreach ($list[$i]['tag'] as $k => $v) {
  841. $str .= implode(',',$v).',';
  842. }
  843. $str = substr($str,0,strlen($str)-1);
  844. $posts_map['posts_id'] = $list[$i]['posts_id'];
  845. $posts->where($posts_map)->setField('posts_tag',$str);
  846. $tag[$list[$i]['posts_id']][] = $str;
  847. $str = '';
  848. }
  849. }
  850. return $tag;
  851. }
  852. ?>