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

/lib/app.function.php

https://github.com/echan85/TeamToy
PHP | 782 lines | 643 code | 96 blank | 43 comment | 65 complexity | 32520c29fb9bf2e33071535386d75895 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. if( !defined('SAE_TMP_PATH') )
  3. {
  4. @mkdir( AROOT . DS . 'tmp');
  5. define('SAE_TMP_PATH', AROOT . DS . 'tmp' . DS );
  6. }
  7. function not_empty( $str )
  8. {
  9. return strlen( $str ) > 0;
  10. }
  11. // From aoihome.sinaapp.com/fun via Aoi [is_email]
  12. function is_email( $email )
  13. {
  14. return filter_var( $email , FILTER_VALIDATE_EMAIL );
  15. }
  16. function on_sae()
  17. {
  18. return defined('SAE_ACCESSKEY') && (substr( SAE_ACCESSKEY , 0 , 4 ) != 'kapp') ;
  19. }
  20. function is_online( $uid )
  21. {
  22. $sql = "SELECT * FROM `online` WHERE `uid` = '" . intval( $uid ) . "' AND `last_active` > '" . date( "Y-m-d H:i:s" , strtotime("-5 minutes") ) . "' LIMIT 1";
  23. return get_line( $sql );
  24. }
  25. function is_installed()
  26. {
  27. if( !db()) return false;
  28. return mysql_query("SHOW COLUMNS FROM `user`",db());
  29. }
  30. function kset( $key , $value )
  31. {
  32. $sql = "REPLACE INTO `keyvalue` ( `key` , `value` ) VALUES ( '" . s($key) . "' , '" . s($value) . "' )";
  33. run_sql( $sql );
  34. }
  35. function kget( $key )
  36. {
  37. return get_var( "SELECT `value` FROM `keyvalue` WHERE `key` = '" . s($key) . "' LIMIT 1" );
  38. }
  39. function kdel( $key )
  40. {
  41. $sql = "DELETE FROM `keyvalue` WHERE `key` = '" . s($key) . "' LIMIT 1" ;
  42. run_sql($sql);
  43. }
  44. function local_version()
  45. {
  46. return intval(file_get_contents(AROOT.'version.txt'));
  47. }
  48. function db_init()
  49. {
  50. $password = substr( md5( time().rand(1,9999) ) , rand( 1 , 20 ) , 12 );
  51. $sql_contents = preg_replace( "/(#.+[\r|\n]*)/" , '' , file_get_contents( AROOT . 'misc' . DS . 'install.sql'));
  52. // 更换变量
  53. $sql_contents = str_replace( '{password}' , md5($password) , $sql_contents );
  54. $sqls = split_sql_file( $sql_contents );
  55. foreach ($sqls as $sql)
  56. {
  57. run_sql( $sql );
  58. }
  59. if( db_errno() == 0 )
  60. {
  61. info_page('数据库初始化成功,请使用【member@teamtoy.net】和【' . $password . '】<a href="/" target="new">登入并添加用户</a>');
  62. exit;
  63. }
  64. else
  65. {
  66. info_page( db_error() );
  67. exit;
  68. }
  69. }
  70. function split_sql_file($sql, $delimiter = ';')
  71. {
  72. $sql = trim($sql);
  73. $char = '';
  74. $last_char = '';
  75. $ret = array();
  76. $string_start = '';
  77. $in_string = FALSE;
  78. $escaped_backslash = FALSE;
  79. for ($i = 0; $i < strlen($sql); ++$i) {
  80. $char = $sql[$i];
  81. // if delimiter found, add the parsed part to the returned array
  82. if ($char == $delimiter && !$in_string) {
  83. $ret[] = substr($sql, 0, $i);
  84. $sql = substr($sql, $i + 1);
  85. $i = 0;
  86. $last_char = '';
  87. }
  88. if ($in_string) {
  89. // We are in a string, first check for escaped backslashes
  90. if ($char == '\\') {
  91. if ($last_char != '\\') {
  92. $escaped_backslash = FALSE;
  93. } else {
  94. $escaped_backslash = !$escaped_backslash;
  95. }
  96. }
  97. // then check for not escaped end of strings except for
  98. // backquotes than cannot be escaped
  99. if (($char == $string_start)
  100. && ($char == '`' || !(($last_char == '\\') && !$escaped_backslash))) {
  101. $in_string = FALSE;
  102. $string_start = '';
  103. }
  104. } else {
  105. // we are not in a string, check for start of strings
  106. if (($char == '"') || ($char == '\'') || ($char == '`')) {
  107. $in_string = TRUE;
  108. $string_start = $char;
  109. }
  110. }
  111. $last_char = $char;
  112. } // end for
  113. // add any rest to the returned array
  114. if (!empty($sql)) {
  115. $ret[] = $sql;
  116. }
  117. return $ret;
  118. }
  119. function is_login()
  120. {
  121. return $_SESSION['level'] > 0;
  122. }
  123. function is_admin()
  124. {
  125. return $_SESSION['level'] == 9;
  126. }
  127. function uid()
  128. {
  129. return intval($_SESSION['uid']);
  130. }
  131. function uname()
  132. {
  133. return t($_SESSION['uname']);
  134. }
  135. function forward( $url )
  136. {
  137. header( "Location: " . $url );
  138. }
  139. function jsforword( $url )
  140. {
  141. return '<script>location="' . $url . '"</script>';
  142. }
  143. function image( $filename )
  144. {
  145. return 'static/image/' . $filename;
  146. }
  147. function avatar( $url )
  148. {
  149. if( strlen($url) < 1 ) return c('default_avatar');
  150. else return $url;
  151. }
  152. function ctime( $timeline )
  153. {
  154. $time = strtotime($timeline);
  155. if( time() > ($time+60*60*24*300) )return date("Y年n月j日 H:i",$time);
  156. elseif( time() > ($time+60*60*8) ) return date("n月j日 H:i",$time);
  157. else return date("H:i:s",$time);
  158. }
  159. /*
  160. function rtime( $timeline )
  161. {
  162. return date("m月d日 H点i分" , strtotime($timeline) );
  163. }
  164. */
  165. function rtime( $time = false, $limit = 86400, $format = 'm月d日 H点i分')
  166. {
  167. $time = strtotime($time);
  168. $now = time();
  169. $relative = '';
  170. if ($time === $now) $relative = '刚刚';
  171. elseif ($time > $now) $relative = '以后';
  172. else
  173. {
  174. $diff = $now - $time;
  175. if ($diff >= $limit) $relative = date($format, $time);
  176. elseif ($diff < 60)
  177. {
  178. $relative = '不到一分钟';
  179. }
  180. elseif (($minutes = ceil($diff/60)) < 60)
  181. {
  182. $relative = $minutes.'分钟'.(((int)$minutes === 1) ? '' : '').'前';
  183. }
  184. else
  185. {
  186. $hours = ceil($diff/3600);
  187. $relative = $hours.'小时'.(((int)$hours === 1) ? '' : '').'前';
  188. }
  189. }
  190. return $relative;
  191. }
  192. function noname( $content , $name )
  193. {
  194. $len = strlen($name);
  195. if( substr( $content , 0 , $len ) == $name )
  196. return substr( $content , $len );
  197. else
  198. return $content;
  199. }
  200. function render_html( $data , $tpl )
  201. {
  202. ob_start();
  203. extract($data);
  204. require( $tpl );
  205. $content = ob_get_contents();
  206. ob_end_clean();
  207. return $content;
  208. //
  209. }
  210. function read_class( $is_read )
  211. {
  212. if( intval($is_read) == 1 ) return 'read' ;
  213. else return 'unread';
  214. }
  215. function feed_class( $type )
  216. {
  217. switch( $type )
  218. {
  219. case 2:
  220. $class= 'todo';
  221. break;
  222. case 1:
  223. $class= 'notice';
  224. break;
  225. case 3:
  226. $class= 'user';
  227. break;
  228. case 4:
  229. $class= 'cast';
  230. break;
  231. default:
  232. $class = 'normal';
  233. }
  234. return $class;
  235. }
  236. function device( $type )
  237. {
  238. if( strtolower($type) == 'mobile' )
  239. $ret = '<a href="http://teamtoy.net/?c=download&type=mobile" target="_blank">来自移动版</a>';
  240. else
  241. $ret = '<a href="http://teamtoy.net/?c=download&type=web" target="_blank">来自网页版</a>';
  242. return $ret;
  243. }
  244. function get_device()
  245. {
  246. if( is_mobile_request()) return 'mobile';
  247. else return 'web';
  248. }
  249. // ========================================
  250. // client functions
  251. // ========================================
  252. function login( $email , $password )
  253. {
  254. $params = array();
  255. $params['email'] = $email;
  256. $params['password'] = $password;
  257. if($content = send_request( 'user_get_token' , $params ))
  258. {
  259. $data = json_decode( $content , 1 );
  260. if( ($data['err_code'] == 0) && is_array( $data['data'] ) )
  261. return $data['data'];
  262. else
  263. return false;
  264. }
  265. return null;
  266. }
  267. function token()
  268. {
  269. return $_SESSION['token'];
  270. }
  271. function send_request( $action , $param , $token = null )
  272. {
  273. require_once( AROOT . 'controller' . DS . 'api.class.php' );
  274. require_once( AROOT . 'model' . DS . 'api.function.php' );
  275. $GLOBALS['API_EMBED_MODE'] = 1;
  276. // local request
  277. $bake_request = $_REQUEST;
  278. $_REQUEST['c'] = 'api';
  279. $GLOBALS['a'] = $_REQUEST['a'] = $action;
  280. if( $token !== null )
  281. $_REQUEST['token'] = $token;
  282. if( (is_array( $param )) && (count($param) > 0) )
  283. foreach( $param as $key => $value )
  284. {
  285. $_REQUEST[$key] = $value ;
  286. }
  287. $api = new apiController();
  288. // magic call
  289. if( method_exists($api, $action) || has_hook('API_'.$action) )
  290. {
  291. $content = $api->$action();
  292. $_REQUEST = $bake_request;
  293. $GLOBALS['a'] = $_REQUEST['a'];
  294. return $content;
  295. //if($data = json_decode( $content , 1 ))
  296. //return json_encode($data['data']);
  297. }
  298. else
  299. {
  300. return 'API_'.$action . ' NOT EXISTS';
  301. }
  302. return null;
  303. // remote request ...........
  304. /*
  305. $url = c('api_server') . '?c=api&a=' . u($action) . '&token=' . u($token) ;
  306. if( (is_array( $param )) && (count($param) > 0) )
  307. foreach( $param as $key => $value )
  308. $url .= '&' . $key . '=' . u( $value );
  309. if($content = file_get_contents( $url ))
  310. return $content;
  311. return $url;
  312. */
  313. }
  314. function find_at( $text )
  315. {
  316. $reg = '/@(\S+?(?:\s|$))/is';
  317. if( preg_match_all( $reg , $text , $out ) )
  318. return $out[1];
  319. else
  320. return false;
  321. }
  322. function jpeg_up( $source , $dest )
  323. {
  324. if( !function_exists('exif_read_data') ) return copy( $source , $dest );
  325. $img_info = @exif_read_data( $source , ANY_TAG );
  326. switch( $img_info['Orientation'] )
  327. {
  328. case 6:
  329. $r = 270;
  330. break;
  331. case 3:
  332. $r = 180;
  333. break;
  334. case 8:
  335. $r = 90;
  336. break;
  337. default:
  338. $r = 0;
  339. }
  340. $img_src = ImageCreateFromJPEG( $source );
  341. $rotate = imagerotate($img_src, $r, 0,true);
  342. ImageJPEG($rotate,$dest);
  343. }
  344. function upload_as_form( $url , $data )
  345. {
  346. @session_write_close();
  347. $ch = curl_init();
  348. curl_setopt($ch, CURLOPT_URL, $url);
  349. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  350. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  351. curl_setopt($ch, CURLOPT_POST, true);
  352. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  353. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  354. $ch = apply_filter( 'UPLOAD_CURL_SETTINGS' , $ch );
  355. $response = curl_exec($ch);
  356. return $response;
  357. }
  358. function pinyin($_string, $_code='utf8')
  359. { //gbk页面可改为gb2312,其他随意填写为utf8
  360. $_datakey = "a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng|bi|bian|biao|bie|bin|bing|bo|bu|ca|cai|can|cang|cao|ce|ceng|cha".
  361. "|chai|chan|chang|chao|che|chen|cheng|chi|chong|chou|chu|chuai|chuan|chuang|chui|chun|chuo|ci|cong|cou|cu|".
  362. "cuan|cui|cun|cuo|da|dai|dan|dang|dao|de|deng|di|dian|diao|die|ding|diu|dong|dou|du|duan|dui|dun|duo|e|en|er".
  363. "|fa|fan|fang|fei|fen|feng|fo|fou|fu|ga|gai|gan|gang|gao|ge|gei|gen|geng|gong|gou|gu|gua|guai|guan|guang|gui".
  364. "|gun|guo|ha|hai|han|hang|hao|he|hei|hen|heng|hong|hou|hu|hua|huai|huan|huang|hui|hun|huo|ji|jia|jian|jiang".
  365. "|jiao|jie|jin|jing|jiong|jiu|ju|juan|jue|jun|ka|kai|kan|kang|kao|ke|ken|keng|kong|kou|ku|kua|kuai|kuan|kuang".
  366. "|kui|kun|kuo|la|lai|lan|lang|lao|le|lei|leng|li|lia|lian|liang|liao|lie|lin|ling|liu|long|lou|lu|lv|luan|lue".
  367. "|lun|luo|ma|mai|man|mang|mao|me|mei|men|meng|mi|mian|miao|mie|min|ming|miu|mo|mou|mu|na|nai|nan|nang|nao|ne".
  368. "|nei|nen|neng|ni|nian|niang|niao|nie|nin|ning|niu|nong|nu|nv|nuan|nue|nuo|o|ou|pa|pai|pan|pang|pao|pei|pen".
  369. "|peng|pi|pian|piao|pie|pin|ping|po|pu|qi|qia|qian|qiang|qiao|qie|qin|qing|qiong|qiu|qu|quan|que|qun|ran|rang".
  370. "|rao|re|ren|reng|ri|rong|rou|ru|ruan|rui|run|ruo|sa|sai|san|sang|sao|se|sen|seng|sha|shai|shan|shang|shao|".
  371. "she|shen|sheng|shi|shou|shu|shua|shuai|shuan|shuang|shui|shun|shuo|si|song|sou|su|suan|sui|sun|suo|ta|tai|".
  372. "tan|tang|tao|te|teng|ti|tian|tiao|tie|ting|tong|tou|tu|tuan|tui|tun|tuo|wa|wai|wan|wang|wei|wen|weng|wo|wu".
  373. "|xi|xia|xian|xiang|xiao|xie|xin|xing|xiong|xiu|xu|xuan|xue|xun|ya|yan|yang|yao|ye|yi|yin|ying|yo|yong|you".
  374. "|yu|yuan|yue|yun|za|zai|zan|zang|zao|ze|zei|zen|zeng|zha|zhai|zhan|zhang|zhao|zhe|zhen|zheng|zhi|zhong|".
  375. "zhou|zhu|zhua|zhuai|zhuan|zhuang|zhui|zhun|zhuo|zi|zong|zou|zu|zuan|zui|zun|zuo";
  376. $_datavalue = "-20319|-20317|-20304|-20295|-20292|-20283|-20265|-20257|-20242|-20230|-20051|-20036|-20032|-20026|-20002|-19990".
  377. "|-19986|-19982|-19976|-19805|-19784|-19775|-19774|-19763|-19756|-19751|-19746|-19741|-19739|-19728|-19725".
  378. "|-19715|-19540|-19531|-19525|-19515|-19500|-19484|-19479|-19467|-19289|-19288|-19281|-19275|-19270|-19263".
  379. "|-19261|-19249|-19243|-19242|-19238|-19235|-19227|-19224|-19218|-19212|-19038|-19023|-19018|-19006|-19003".
  380. "|-18996|-18977|-18961|-18952|-18783|-18774|-18773|-18763|-18756|-18741|-18735|-18731|-18722|-18710|-18697".
  381. "|-18696|-18526|-18518|-18501|-18490|-18478|-18463|-18448|-18447|-18446|-18239|-18237|-18231|-18220|-18211".
  382. "|-18201|-18184|-18183|-18181|-18012|-17997|-17988|-17970|-17964|-17961|-17950|-17947|-17931|-17928|-17922".
  383. "|-17759|-17752|-17733|-17730|-17721|-17703|-17701|-17697|-17692|-17683|-17676|-17496|-17487|-17482|-17468".
  384. "|-17454|-17433|-17427|-17417|-17202|-17185|-16983|-16970|-16942|-16915|-16733|-16708|-16706|-16689|-16664".
  385. "|-16657|-16647|-16474|-16470|-16465|-16459|-16452|-16448|-16433|-16429|-16427|-16423|-16419|-16412|-16407".
  386. "|-16403|-16401|-16393|-16220|-16216|-16212|-16205|-16202|-16187|-16180|-16171|-16169|-16158|-16155|-15959".
  387. "|-15958|-15944|-15933|-15920|-15915|-15903|-15889|-15878|-15707|-15701|-15681|-15667|-15661|-15659|-15652".
  388. "|-15640|-15631|-15625|-15454|-15448|-15436|-15435|-15419|-15416|-15408|-15394|-15385|-15377|-15375|-15369".
  389. "|-15363|-15362|-15183|-15180|-15165|-15158|-15153|-15150|-15149|-15144|-15143|-15141|-15140|-15139|-15128".
  390. "|-15121|-15119|-15117|-15110|-15109|-14941|-14937|-14933|-14930|-14929|-14928|-14926|-14922|-14921|-14914".
  391. "|-14908|-14902|-14894|-14889|-14882|-14873|-14871|-14857|-14678|-14674|-14670|-14668|-14663|-14654|-14645".
  392. "|-14630|-14594|-14429|-14407|-14399|-14384|-14379|-14368|-14355|-14353|-14345|-14170|-14159|-14151|-14149".
  393. "|-14145|-14140|-14137|-14135|-14125|-14123|-14122|-14112|-14109|-14099|-14097|-14094|-14092|-14090|-14087".
  394. "|-14083|-13917|-13914|-13910|-13907|-13906|-13905|-13896|-13894|-13878|-13870|-13859|-13847|-13831|-13658".
  395. "|-13611|-13601|-13406|-13404|-13400|-13398|-13395|-13391|-13387|-13383|-13367|-13359|-13356|-13343|-13340".
  396. "|-13329|-13326|-13318|-13147|-13138|-13120|-13107|-13096|-13095|-13091|-13076|-13068|-13063|-13060|-12888".
  397. "|-12875|-12871|-12860|-12858|-12852|-12849|-12838|-12831|-12829|-12812|-12802|-12607|-12597|-12594|-12585".
  398. "|-12556|-12359|-12346|-12320|-12300|-12120|-12099|-12089|-12074|-12067|-12058|-12039|-11867|-11861|-11847".
  399. "|-11831|-11798|-11781|-11604|-11589|-11536|-11358|-11340|-11339|-11324|-11303|-11097|-11077|-11067|-11055".
  400. "|-11052|-11045|-11041|-11038|-11024|-11020|-11019|-11018|-11014|-10838|-10832|-10815|-10800|-10790|-10780".
  401. "|-10764|-10587|-10544|-10533|-10519|-10331|-10329|-10328|-10322|-10315|-10309|-10307|-10296|-10281|-10274".
  402. "|-10270|-10262|-10260|-10256|-10254";
  403. $_tdatakey = explode('|', $_datakey);
  404. $_tdatavalue = explode('|', $_datavalue);
  405. $_data = array_combine($_tdatakey, $_tdatavalue);
  406. arsort($_data);
  407. reset($_data);
  408. if($_code!= 'gb2312') $_string = _u2_utf8_gb($_string);
  409. $_res = '';
  410. for($i=0; $i<strlen($_string); $i++) {
  411. $_p = ord(substr($_string, $i, 1));
  412. if($_p>160) {
  413. $_q = ord(substr($_string, ++$i, 1)); $_p = $_p*256 + $_q - 65536;
  414. }
  415. $_res .= _pinyin($_p, $_data);
  416. }
  417. return preg_replace("/[^a-z0-9]*/", '', $_res);
  418. }
  419. function _pinyin($_num, $_data)
  420. {
  421. if($_num>0 && $_num<160 ){
  422. return chr($_num);
  423. }elseif($_num<-20319 || $_num>-10247){
  424. return '';
  425. }else{
  426. foreach($_data as $k=>$v){ if($v<=$_num) break; }
  427. return $k;
  428. }
  429. }
  430. function _u2_utf8_gb($_c)
  431. {
  432. $_string = '';
  433. if($_c < 0x80){
  434. $_string .= $_c;
  435. }elseif($_c < 0x800) {
  436. $_string .= chr(0xc0 | $_c>>6);
  437. $_string .= chr(0x80 | $_c & 0x3f);
  438. }elseif($_c < 0x10000){
  439. $_string .= chr(0xe0 | $_c>>12);
  440. $_string .= chr(0x80 | $_c>>6 & 0x3f);
  441. $_string .= chr(0x80 | $_c & 0x3f);
  442. }elseif($_c < 0x200000) {
  443. $_string .= chr(0xf0 | $_c>>18);
  444. $_string .= chr(0x80 | $_c>>12 & 0x3f);
  445. $_string .= chr(0x80 | $_c>>6 & 0x3f);
  446. $_string .= chr(0x80 | $_c & 0x3f);
  447. }
  448. return iconv('utf-8', 'gb2312', $_string);
  449. }
  450. // **************************************************************
  451. // * Plugins & hooks
  452. // **************************************************************
  453. function add_filter( $tag , $function_to_add , $priority = 10 , $accepted_args_num = 1 )
  454. {
  455. return add_hook( $tag , $function_to_add , $priority , $accepted_args_num );
  456. }
  457. function add_action( $tag , $function_to_add , $priority = 10 , $accepted_args_num = 1 )
  458. {
  459. return add_hook( $tag , $function_to_add , $priority , $accepted_args_num );
  460. }
  461. function add_hook( $tag , $function_to_add , $priority = 10 , $accepted_args_num = 1 )
  462. {
  463. $tag = strtoupper($tag);
  464. $idx = build_hook_id( $tag , $function_to_add , $priority );
  465. $GLOBALS['TTHOOK'][$tag][$priority][$idx] = array( 'function' => $function_to_add , 'args_num' => $accepted_args_num );
  466. }
  467. function do_action( $tag , $value = null )
  468. {
  469. return apply_hook( $tag , $value );
  470. }
  471. function apply_filter( $tag , $value = null )
  472. {
  473. return apply_hook( $tag , $value );
  474. }
  475. function apply_hook( $tag , $value )
  476. {
  477. $tag = strtoupper($tag);
  478. if( $hooks = has_hook( $tag ) )
  479. {
  480. ksort( $hooks );
  481. $args = func_get_args();
  482. reset( $hooks );
  483. do
  484. {
  485. foreach( (array) current( $hooks ) as $hook )
  486. {
  487. if( !is_null($hook['function']) )
  488. {
  489. $args[1] = $value;
  490. $value = call_user_func_array( $hook['function'] , array_slice($args, 1, (int) $hook['args_num']));
  491. }
  492. }
  493. }while( next( $hooks ) !== false );
  494. }
  495. return $value;
  496. }
  497. function has_hook( $tag , $priority = null )
  498. {
  499. $tag = strtoupper($tag);
  500. if( is_null($priority) ) return isset( $GLOBALS['TTHOOK'][$tag] )? $GLOBALS['TTHOOK'][$tag]:false;
  501. else return isset( $GLOBALS['TTHOOK'][$tag][$priority] )? $GLOBALS['TTHOOK'][$tag][$priority]:false;
  502. }
  503. function remove_hook( $tag , $priority = null )
  504. {
  505. $tag = strtoupper($tag);
  506. if( is_null($priority) ) unset( $GLOBALS['TTHOOK'][$tag] );
  507. else unset( $GLOBALS['TTHOOK'][$tag][$priority] );
  508. }
  509. // This function is based on wordpress
  510. // from https://raw.github.com/WordPress/WordPress/master/wp-includes/plugin.php
  511. // requere php5.2+
  512. function build_hook_id( $tag , $function )
  513. {
  514. if ( is_string($function) )
  515. return $function;
  516. if ( is_object($function) )
  517. {
  518. // Closures are currently implemented as objects
  519. $function = array( $function, '' );
  520. }
  521. else
  522. {
  523. $function = (array) $function;
  524. }
  525. if (is_object($function[0]) )
  526. {
  527. // Object Class Calling
  528. if ( function_exists('spl_object_hash') )
  529. {
  530. return spl_object_hash($function[0]) . $function[1];
  531. }
  532. else
  533. {
  534. return substr( serialize($function[0]) , 0 , 15 ). $function[1];
  535. }
  536. }
  537. elseif( is_string($function[0]) )
  538. {
  539. // Static Calling
  540. return $function[0].$function[1];
  541. }
  542. }
  543. function scan_plugin_info()
  544. {
  545. if( file_exists( c('plugin_path') ) )
  546. foreach( glob( c('plugin_path') . DS . "*" , GLOB_ONLYDIR ) as $pfold )
  547. {
  548. $app_file = $pfold .DS .'app.php';
  549. if( file_exists( $app_file ) )
  550. if($pinfo = get_plugin_info( file_get_contents( $app_file ) ))
  551. $plist[] = $pinfo;
  552. }
  553. return isset( $plist ) ? $plist : false;
  554. }
  555. function get_plugin_info( $content )
  556. {
  557. $reg = '/\*\*\*\s+(.+)\s+\*\*\*/is';
  558. if( preg_match( $reg , $content , $out ) )
  559. {
  560. $info_content = $out[1];
  561. $lines = explode('##',$info_content);
  562. array_shift($lines);
  563. foreach( $lines as $line )
  564. {
  565. $line = trim($line);
  566. list( $key , $value ) = explode( ' ' , $line , 2 );
  567. $ret[$key] = z(t($value));
  568. }
  569. if( isset($ret) )return $ret;
  570. }
  571. return false;
  572. }
  573. // =================================================
  574. // make mentions
  575. // =================================================
  576. function member_info()
  577. {
  578. if( !isset($GLOBALS['TT_MEMBER_INFO']) )
  579. {
  580. $sql = "SELECT `id` as `uid` , `name` FROM `user` WHERE `level` > 0 AND `is_closed` != 1 ";
  581. if($data = get_data($sql))
  582. {
  583. foreach( $data as $item )
  584. {
  585. $name = trim($item['name']);
  586. $GLOBALS['TT_MEMBER_INFO']['@'.$name] =
  587. '<a href="javascript:void(0);" uid=' . $item['uid'] . ' class="namecard">'. '@'.$name .'</a>';
  588. if(c('at_short_name'))
  589. if( mb_strlen( $name , 'UTF-8' ) == 3 )
  590. $GLOBALS['TT_MEMBER_INFO']['@'.mb_substr($name , 1 , 2 , 'UTF-8' )] =
  591. '<a href="javascript:void(0);" uid=' . $item['uid'] . ' class="namecard">'. '@'.mb_substr($name , 1 , 2 , 'UTF-8' ).'</a>';
  592. }
  593. }
  594. }
  595. return $GLOBALS['TT_MEMBER_INFO'];
  596. }
  597. function link_at( $str )
  598. {
  599. $to_replace = array_keys( member_info() );
  600. $replace_to = array_values( member_info() );
  601. return str_replace( $to_replace , $replace_to , $str );
  602. }
  603. function find_links( $html )
  604. {
  605. $reg = '/(http:\/\/(.+?))((\s+)|$)/is';
  606. if( preg_match_all( $reg , $html , $out ) )
  607. {
  608. foreach( $out[0] as $item )
  609. {
  610. $ret[] = trim($item);
  611. }
  612. $ret = array_unique($ret);
  613. return $ret;
  614. }
  615. return false;
  616. }
  617. function array_remove( $value , $array )
  618. {
  619. return array_diff($array, array($value));
  620. }
  621. function phpmailer_send_mail( $to , $subject , $body , $from , $host , $port , $user , $password )
  622. {
  623. if( !isset( $GLOBALS['LP_MAILER'] ) )
  624. {
  625. include_once( AROOT . 'lib' . DS . 'phpmailer.class.php' );
  626. $GLOBALS['LP_MAILER'] = new PHPMailer();
  627. }
  628. $mail = $GLOBALS['LP_MAILER'];
  629. $mail->CharSet = 'UTF-8';
  630. $mail->Encoding = 'base64';
  631. $mail->IsSMTP();
  632. $mail->Host = $host;
  633. $mail->SMTPAuth = true;
  634. //$mail->SMTPKeepAlive = true;
  635. $mail->Port = $port;
  636. $mail->Username = $user;
  637. $mail->Password = $password;
  638. $mail->SetFrom($from );
  639. $mail->AddReplyTo($from);
  640. $mail->Subject = $subject ;
  641. $mail->WordWrap = 50;
  642. $mail->MsgHTML($body);
  643. $mail->AddAddress( $to );
  644. if(!$mail->Send())
  645. {
  646. //echo $mail->ErrorInfo;
  647. return false;
  648. }
  649. else
  650. {
  651. $mail->ClearAddresses();
  652. return true;
  653. }
  654. }
  655. ?>