PageRenderTime 77ms CodeModel.GetById 13ms app.highlight 45ms RepoModel.GetById 12ms app.codeStats 1ms

/lib/app.function.php

https://github.com/echan85/TeamToy
PHP | 782 lines | 643 code | 96 blank | 43 comment | 65 complexity | 32520c29fb9bf2e33071535386d75895 MD5 | raw file
  1<?php
  2if( !defined('SAE_TMP_PATH') )
  3{
  4    @mkdir( AROOT . DS . 'tmp');
  5    define('SAE_TMP_PATH', AROOT . DS . 'tmp' . DS );
  6}
  7
  8function not_empty( $str )
  9{
 10	return strlen( $str ) > 0;
 11}
 12
 13// From aoihome.sinaapp.com/fun via Aoi [is_email]
 14function is_email( $email )
 15{
 16	return filter_var( $email , FILTER_VALIDATE_EMAIL );
 17}
 18
 19function on_sae()
 20{
 21    return defined('SAE_ACCESSKEY') && (substr( SAE_ACCESSKEY , 0 , 4 ) != 'kapp') ;
 22}
 23
 24function is_online( $uid )
 25{
 26    $sql = "SELECT * FROM `online` WHERE `uid` = '" . intval( $uid ) . "' AND `last_active` > '" . date( "Y-m-d H:i:s" , strtotime("-5 minutes") ) . "' LIMIT 1";
 27    return get_line( $sql );
 28}
 29
 30function is_installed()
 31{
 32    if( !db()) return false;
 33    return mysql_query("SHOW COLUMNS FROM `user`",db());
 34}
 35
 36function kset( $key , $value )
 37{
 38    $sql = "REPLACE INTO `keyvalue` ( `key` , `value` ) VALUES ( '" . s($key) . "' , '" . s($value) . "' )";
 39    run_sql( $sql );
 40}
 41
 42function kget( $key )
 43{
 44    return get_var( "SELECT `value` FROM `keyvalue` WHERE `key` = '" . s($key) . "' LIMIT 1" );
 45}
 46
 47function kdel( $key )
 48{
 49    $sql = "DELETE FROM `keyvalue` WHERE `key` = '" . s($key) . "' LIMIT 1" ;
 50    run_sql($sql);
 51}
 52
 53function local_version()
 54{
 55    return intval(file_get_contents(AROOT.'version.txt'));
 56}
 57
 58function db_init()
 59{
 60    
 61    $password = substr( md5( time().rand(1,9999) ) , rand( 1 , 20 ) , 12   );
 62    
 63    $sql_contents = preg_replace( "/(#.+[\r|\n]*)/" , '' , file_get_contents( AROOT . 'misc' . DS . 'install.sql'));
 64
 65    // 更换变量
 66    $sql_contents = str_replace( '{password}' , md5($password) , $sql_contents );
 67
 68    $sqls = split_sql_file( $sql_contents );
 69    foreach ($sqls as $sql) 
 70    {
 71        run_sql( $sql );
 72    }
 73
 74    if(  db_errno() == 0 )
 75    {
 76        info_page('数据库初始化成功,请使用【member@teamtoy.net】和【' . $password . '】<a href="/" target="new">登入并添加用户</a>');
 77        exit;
 78    }
 79    else
 80    {
 81        info_page( db_error() );
 82        exit;
 83    } 
 84        
 85    
 86}
 87
 88function split_sql_file($sql, $delimiter = ';') 
 89{
 90    $sql               = trim($sql);
 91    $char              = '';
 92    $last_char         = '';
 93    $ret               = array();
 94    $string_start      = '';
 95    $in_string         = FALSE;
 96    $escaped_backslash = FALSE;
 97
 98    for ($i = 0; $i < strlen($sql); ++$i) {
 99            $char = $sql[$i];
100
101            // if delimiter found, add the parsed part to the returned array
102            if ($char == $delimiter && !$in_string) {
103                    $ret[]     = substr($sql, 0, $i);
104                    $sql       = substr($sql, $i + 1);
105                    $i         = 0;
106                    $last_char = '';
107            }
108
109            if ($in_string) {
110                    // We are in a string, first check for escaped backslashes
111                    if ($char == '\\') {
112                            if ($last_char != '\\') {
113                                    $escaped_backslash = FALSE;
114                            } else {
115                                    $escaped_backslash = !$escaped_backslash;
116                            }
117                    }
118                    // then check for not escaped end of strings except for
119                    // backquotes than cannot be escaped
120                    if (($char == $string_start)
121                            && ($char == '`' || !(($last_char == '\\') && !$escaped_backslash))) {
122                            $in_string    = FALSE;
123                            $string_start = '';
124                    }
125            } else {
126                    // we are not in a string, check for start of strings
127                    if (($char == '"') || ($char == '\'') || ($char == '`')) {
128                            $in_string    = TRUE;
129                            $string_start = $char;
130                    }
131            }
132            $last_char = $char;
133    } // end for
134
135    // add any rest to the returned array
136    if (!empty($sql)) {
137            $ret[] = $sql;
138    }
139    return $ret;
140}
141
142function is_login()
143{
144	return $_SESSION['level'] > 0;
145}
146
147function is_admin()
148{
149    return $_SESSION['level'] == 9;
150}
151
152function uid()
153{
154	return intval($_SESSION['uid']);
155}
156
157function uname()
158{
159	return t($_SESSION['uname']);
160}
161
162function forward( $url )
163{
164	header( "Location: " . $url );
165}
166
167function jsforword( $url )
168{
169	return '<script>location="' . $url . '"</script>';
170}
171
172function image( $filename )
173{
174	return 'static/image/' . $filename;
175}
176
177function avatar( $url )
178{
179	if( strlen($url) < 1 ) return c('default_avatar');
180	else return $url;
181}
182
183function ctime( $timeline )
184{
185    $time = strtotime($timeline);
186    if( time() > ($time+60*60*24*300) )return date("Y年n月j日 H:i",$time);
187    elseif( time() > ($time+60*60*8) ) return date("n月j日 H:i",$time);
188    else return date("H:i:s",$time);
189}
190
191/*
192function rtime( $timeline )
193{
194	return date("m月d日 H点i分" , strtotime($timeline) );
195}
196*/
197
198function rtime( $time = false, $limit = 86400, $format = 'm月d日 H点i分') 
199{
200	$time = strtotime($time);
201
202	$now = time();
203	$relative = '';
204
205	if ($time === $now) $relative = '刚刚';
206	elseif ($time > $now) $relative = '以后';
207	else 
208	{
209		$diff = $now - $time;
210
211		if ($diff >= $limit) $relative = date($format, $time);
212
213		elseif ($diff < 60) 
214		{
215			$relative = '不到一分钟';
216		}
217		elseif (($minutes = ceil($diff/60)) < 60)
218		{
219			$relative = $minutes.'分钟'.(((int)$minutes === 1) ? '' : '').'前';
220		}
221		else
222		{
223			$hours = ceil($diff/3600);
224			$relative = $hours.'小时'.(((int)$hours === 1) ? '' : '').'前';
225		}
226	}
227
228	return $relative;
229}
230
231function noname( $content , $name )
232{
233	$len = strlen($name);
234	if( substr( $content , 0 , $len ) == $name )
235		return substr( $content , $len  );
236	else
237		return $content;
238}
239
240function render_html( $data , $tpl )
241{
242	ob_start();
243	extract($data);
244	require( $tpl );
245	$content = ob_get_contents();
246	ob_end_clean();
247	return $content;
248	// 
249}
250
251function read_class( $is_read )
252{
253    if( intval($is_read) == 1 ) return  'read' ; 
254    else return  'unread';
255}
256
257function feed_class(  $type )
258{
259	switch( $type )
260	{
261		case 2:
262			 $class= 'todo';
263			break;
264		case 1:
265			$class= 'notice';
266			break;
267		case 3:
268			$class= 'user';
269			break;
270
271        case 4:
272            $class= 'cast';
273            break;   
274            
275		default:
276			$class = 'normal';		
277	}
278
279	return $class;
280}
281
282function device( $type )
283{
284	if( strtolower($type) == 'mobile' )
285		$ret = '<a href="http://teamtoy.net/?c=download&type=mobile" target="_blank">来自移动版</a>';
286	else
287		$ret = '<a href="http://teamtoy.net/?c=download&type=web" target="_blank">来自网页版</a>';
288	return $ret;
289}
290
291function get_device()
292{
293    if( is_mobile_request()) return 'mobile';
294    else return 'web';
295}
296
297
298// ========================================
299// client functions
300// ========================================
301
302
303function login( $email , $password )
304{
305    $params = array();
306    $params['email'] = $email;
307    $params['password'] = $password;
308
309    if($content = send_request( 'user_get_token' ,  $params ))
310    {
311        $data = json_decode( $content , 1 );
312        if( ($data['err_code'] == 0) && is_array( $data['data'] ) )
313            return $data['data'];
314        else
315            return false;
316    }
317    return null;
318}
319
320function token()
321{
322	return $_SESSION['token'];
323}
324
325function send_request( $action , $param , $token = null )
326{
327	require_once( AROOT . 'controller' . DS . 'api.class.php' );
328    require_once( AROOT . 'model' . DS . 'api.function.php' );
329    $GLOBALS['API_EMBED_MODE'] = 1;
330        
331    // local request
332    $bake_request = $_REQUEST;
333    $_REQUEST['c'] = 'api';
334    $GLOBALS['a'] = $_REQUEST['a'] = $action;
335    if( $token !== null )
336        $_REQUEST['token'] = $token;
337
338    if( (is_array( $param )) && (count($param) > 0) )
339        foreach( $param as $key => $value )
340        {
341            $_REQUEST[$key] =  $value ;
342        }
343            
344
345    $api = new apiController();
346    // magic call
347    if( method_exists($api, $action) || has_hook('API_'.$action) )
348    {
349        $content = $api->$action();
350        $_REQUEST = $bake_request;
351        $GLOBALS['a'] = $_REQUEST['a'];
352       
353        return $content;
354        //if($data = json_decode( $content , 1 ))
355        //return json_encode($data['data']);
356    }
357    else
358    {
359        return 'API_'.$action . ' NOT EXISTS';
360    }
361   
362    return null;
363    
364    
365    // remote request ...........
366    /*
367
368    $url = c('api_server') . '?c=api&a=' . u($action) . '&token=' . u($token) ;
369	
370	if( (is_array( $param )) && (count($param) > 0) )
371		foreach( $param as $key => $value )
372			$url .= '&' . $key . '=' . u( $value );
373	
374	
375
376	if($content = file_get_contents( $url ))
377		return $content;
378	
379	return $url;
380	*/
381}
382
383function find_at( $text )
384{
385	$reg = '/@(\S+?(?:\s|$))/is';
386	if( preg_match_all( $reg , $text , $out ) )
387		return $out[1];
388	else
389		return false;
390
391}
392
393function jpeg_up( $source , $dest )
394{
395	if( !function_exists('exif_read_data') ) return copy( $source , $dest );
396    $img_info = @exif_read_data( $source , ANY_TAG  );
397  	switch( $img_info['Orientation'] )
398  	{
399  	  	case 6:
400  		  	$r = 270;
401  		  	break;
402                        
403          	case 3:
404                	$r = 180;
405                        break;
406                        
407          	case 8:
408                	$r = 90;
409                        break;
410                        
411          	default:
412                	$r = 0;
413  	}
414  
415  	$img_src = ImageCreateFromJPEG( $source );
416    $rotate = imagerotate($img_src, $r, 0,true);    
417	ImageJPEG($rotate,$dest);
418        
419        
420        
421}
422
423function upload_as_form( $url , $data )
424{
425    @session_write_close(); 
426    $ch = curl_init();
427    curl_setopt($ch, CURLOPT_URL, $url);
428    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
429    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
430    curl_setopt($ch, CURLOPT_POST, true);
431    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
432    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
433
434    $ch = apply_filter( 'UPLOAD_CURL_SETTINGS' , $ch );
435
436    $response = curl_exec($ch);
437    return $response;
438}
439
440
441function pinyin($_string, $_code='utf8')
442{ //gbk页面可改为gb2312,其他随意填写为utf8
443        $_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". 
444                        "|chai|chan|chang|chao|che|chen|cheng|chi|chong|chou|chu|chuai|chuan|chuang|chui|chun|chuo|ci|cong|cou|cu|". 
445                        "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". 
446                        "|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". 
447                        "|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". 
448                        "|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". 
449                        "|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". 
450                        "|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". 
451                        "|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". 
452                        "|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". 
453                        "|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|". 
454                        "she|shen|sheng|shi|shou|shu|shua|shuai|shuan|shuang|shui|shun|shuo|si|song|sou|su|suan|sui|sun|suo|ta|tai|". 
455                        "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". 
456                        "|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". 
457                        "|yu|yuan|yue|yun|za|zai|zan|zang|zao|ze|zei|zen|zeng|zha|zhai|zhan|zhang|zhao|zhe|zhen|zheng|zhi|zhong|". 
458                        "zhou|zhu|zhua|zhuai|zhuan|zhuang|zhui|zhun|zhuo|zi|zong|zou|zu|zuan|zui|zun|zuo"; 
459        $_datavalue = "-20319|-20317|-20304|-20295|-20292|-20283|-20265|-20257|-20242|-20230|-20051|-20036|-20032|-20026|-20002|-19990". 
460                        "|-19986|-19982|-19976|-19805|-19784|-19775|-19774|-19763|-19756|-19751|-19746|-19741|-19739|-19728|-19725". 
461                        "|-19715|-19540|-19531|-19525|-19515|-19500|-19484|-19479|-19467|-19289|-19288|-19281|-19275|-19270|-19263". 
462                        "|-19261|-19249|-19243|-19242|-19238|-19235|-19227|-19224|-19218|-19212|-19038|-19023|-19018|-19006|-19003". 
463                        "|-18996|-18977|-18961|-18952|-18783|-18774|-18773|-18763|-18756|-18741|-18735|-18731|-18722|-18710|-18697". 
464                        "|-18696|-18526|-18518|-18501|-18490|-18478|-18463|-18448|-18447|-18446|-18239|-18237|-18231|-18220|-18211". 
465                        "|-18201|-18184|-18183|-18181|-18012|-17997|-17988|-17970|-17964|-17961|-17950|-17947|-17931|-17928|-17922". 
466                        "|-17759|-17752|-17733|-17730|-17721|-17703|-17701|-17697|-17692|-17683|-17676|-17496|-17487|-17482|-17468". 
467                        "|-17454|-17433|-17427|-17417|-17202|-17185|-16983|-16970|-16942|-16915|-16733|-16708|-16706|-16689|-16664". 
468                        "|-16657|-16647|-16474|-16470|-16465|-16459|-16452|-16448|-16433|-16429|-16427|-16423|-16419|-16412|-16407". 
469                        "|-16403|-16401|-16393|-16220|-16216|-16212|-16205|-16202|-16187|-16180|-16171|-16169|-16158|-16155|-15959". 
470                        "|-15958|-15944|-15933|-15920|-15915|-15903|-15889|-15878|-15707|-15701|-15681|-15667|-15661|-15659|-15652". 
471                        "|-15640|-15631|-15625|-15454|-15448|-15436|-15435|-15419|-15416|-15408|-15394|-15385|-15377|-15375|-15369". 
472                        "|-15363|-15362|-15183|-15180|-15165|-15158|-15153|-15150|-15149|-15144|-15143|-15141|-15140|-15139|-15128". 
473                        "|-15121|-15119|-15117|-15110|-15109|-14941|-14937|-14933|-14930|-14929|-14928|-14926|-14922|-14921|-14914". 
474                        "|-14908|-14902|-14894|-14889|-14882|-14873|-14871|-14857|-14678|-14674|-14670|-14668|-14663|-14654|-14645". 
475                        "|-14630|-14594|-14429|-14407|-14399|-14384|-14379|-14368|-14355|-14353|-14345|-14170|-14159|-14151|-14149". 
476                        "|-14145|-14140|-14137|-14135|-14125|-14123|-14122|-14112|-14109|-14099|-14097|-14094|-14092|-14090|-14087". 
477                        "|-14083|-13917|-13914|-13910|-13907|-13906|-13905|-13896|-13894|-13878|-13870|-13859|-13847|-13831|-13658". 
478                        "|-13611|-13601|-13406|-13404|-13400|-13398|-13395|-13391|-13387|-13383|-13367|-13359|-13356|-13343|-13340". 
479                        "|-13329|-13326|-13318|-13147|-13138|-13120|-13107|-13096|-13095|-13091|-13076|-13068|-13063|-13060|-12888". 
480                        "|-12875|-12871|-12860|-12858|-12852|-12849|-12838|-12831|-12829|-12812|-12802|-12607|-12597|-12594|-12585". 
481                        "|-12556|-12359|-12346|-12320|-12300|-12120|-12099|-12089|-12074|-12067|-12058|-12039|-11867|-11861|-11847". 
482                        "|-11831|-11798|-11781|-11604|-11589|-11536|-11358|-11340|-11339|-11324|-11303|-11097|-11077|-11067|-11055". 
483                        "|-11052|-11045|-11041|-11038|-11024|-11020|-11019|-11018|-11014|-10838|-10832|-10815|-10800|-10790|-10780". 
484                        "|-10764|-10587|-10544|-10533|-10519|-10331|-10329|-10328|-10322|-10315|-10309|-10307|-10296|-10281|-10274". 
485                        "|-10270|-10262|-10260|-10256|-10254"; 
486        $_tdatakey   = explode('|', $_datakey); 
487        $_tdatavalue = explode('|', $_datavalue);
488        $_data = array_combine($_tdatakey, $_tdatavalue);
489        arsort($_data); 
490        reset($_data);
491        if($_code!= 'gb2312') $_string = _u2_utf8_gb($_string); 
492        $_res = ''; 
493        for($i=0; $i<strlen($_string); $i++) { 
494                $_p = ord(substr($_string, $i, 1)); 
495                if($_p>160) { 
496                        $_q = ord(substr($_string, ++$i, 1)); $_p = $_p*256 + $_q - 65536;
497                } 
498                $_res .= _pinyin($_p, $_data); 
499        } 
500        return preg_replace("/[^a-z0-9]*/", '', $_res); 
501} 
502
503function _pinyin($_num, $_data)
504{ 
505        if($_num>0 && $_num<160 ){
506                return chr($_num);
507        }elseif($_num<-20319 || $_num>-10247){
508                return '';
509        }else{ 
510                foreach($_data as $k=>$v){ if($v<=$_num) break; } 
511                return $k; 
512        } 
513}
514
515function _u2_utf8_gb($_c)
516{ 
517        $_string = ''; 
518        if($_c < 0x80){
519                $_string .= $_c;
520        }elseif($_c < 0x800) { 
521                $_string .= chr(0xc0 | $_c>>6); 
522                $_string .= chr(0x80 | $_c & 0x3f); 
523        }elseif($_c < 0x10000){ 
524                $_string .= chr(0xe0 | $_c>>12); 
525                $_string .= chr(0x80 | $_c>>6 & 0x3f); 
526                $_string .= chr(0x80 | $_c & 0x3f); 
527        }elseif($_c < 0x200000) { 
528                $_string .= chr(0xf0 | $_c>>18); 
529                $_string .= chr(0x80 | $_c>>12 & 0x3f); 
530                $_string .= chr(0x80 | $_c>>6 & 0x3f); 
531                $_string .= chr(0x80 | $_c & 0x3f); 
532        } 
533        return iconv('utf-8', 'gb2312', $_string); 
534}
535
536// **************************************************************
537// * Plugins & hooks
538// ************************************************************** 
539function add_filter( $tag , $function_to_add , $priority = 10 , $accepted_args_num = 1 )
540{
541    return add_hook( $tag , $function_to_add , $priority , $accepted_args_num );
542}
543
544function add_action( $tag , $function_to_add , $priority = 10 , $accepted_args_num = 1 )
545{
546    return add_hook( $tag , $function_to_add , $priority , $accepted_args_num );
547}
548
549function add_hook( $tag , $function_to_add , $priority = 10 , $accepted_args_num = 1 )
550{
551    $tag = strtoupper($tag);
552    $idx = build_hook_id( $tag , $function_to_add , $priority );
553    $GLOBALS['TTHOOK'][$tag][$priority][$idx] = array( 'function' => $function_to_add , 'args_num' => $accepted_args_num );
554}
555
556function do_action( $tag , $value = null )
557{
558    return apply_hook( $tag , $value );
559}
560
561function apply_filter( $tag , $value = null )
562{
563    return apply_hook( $tag , $value );
564}
565
566
567
568function apply_hook( $tag , $value )
569{
570    $tag = strtoupper($tag);
571    if( $hooks  = has_hook( $tag ) )
572    {
573        ksort( $hooks );
574        $args = func_get_args();
575        reset( $hooks );
576
577        do
578        {
579            foreach( (array) current( $hooks ) as $hook )
580            {
581                if( !is_null($hook['function']) )
582                {
583                    $args[1] = $value;
584                    $value = call_user_func_array( $hook['function'] , array_slice($args, 1, (int) $hook['args_num']));
585                }
586            }
587        }while( next( $hooks ) !== false );
588
589    }
590
591    return $value;
592}
593
594function has_hook( $tag , $priority = null )
595{
596    $tag = strtoupper($tag);
597    if( is_null($priority) ) return isset( $GLOBALS['TTHOOK'][$tag] )? $GLOBALS['TTHOOK'][$tag]:false;
598    else return isset( $GLOBALS['TTHOOK'][$tag][$priority] )? $GLOBALS['TTHOOK'][$tag][$priority]:false;
599}
600
601function remove_hook( $tag , $priority = null )
602{
603    $tag = strtoupper($tag);
604    if( is_null($priority) ) unset( $GLOBALS['TTHOOK'][$tag] );
605    else unset( $GLOBALS['TTHOOK'][$tag][$priority] );
606}
607// This function is based on wordpress  
608// from  https://raw.github.com/WordPress/WordPress/master/wp-includes/plugin.php
609// requere php5.2+
610
611function build_hook_id( $tag , $function ) 
612{
613    if ( is_string($function) )
614        return $function;
615
616    if ( is_object($function) ) 
617    {
618        // Closures are currently implemented as objects
619        $function = array( $function, '' );
620    }
621    else
622    {
623        $function = (array) $function;
624    }
625
626    if (is_object($function[0]) ) 
627    {
628        // Object Class Calling
629        if ( function_exists('spl_object_hash') ) 
630        {
631            return spl_object_hash($function[0]) . $function[1];
632        }
633        else
634        {
635            return substr( serialize($function[0]) , 0 , 15 ). $function[1];
636        }
637
638    }
639    elseif( is_string($function[0]) )
640    {
641        // Static Calling
642        return $function[0].$function[1];
643    }
644}
645
646function scan_plugin_info()
647{
648    if( file_exists( c('plugin_path') ) )
649    foreach( glob( c('plugin_path') . DS . "*" , GLOB_ONLYDIR ) as $pfold  )
650    {
651        $app_file = $pfold .DS .'app.php';
652        if( file_exists( $app_file ) )
653            if($pinfo = get_plugin_info( file_get_contents( $app_file ) ))
654            $plist[] = $pinfo;
655    }
656    return isset( $plist ) ? $plist : false;
657}
658
659function get_plugin_info( $content )
660{
661    $reg = '/\*\*\*\s+(.+)\s+\*\*\*/is';
662    if( preg_match( $reg , $content , $out ) )
663    {
664        $info_content = $out[1];
665        $lines = explode('##',$info_content);
666        array_shift($lines);
667        foreach( $lines as $line )
668        {
669            $line = trim($line);
670            list( $key , $value ) = explode( ' ' , $line , 2 );
671            $ret[$key] = z(t($value)); 
672        }
673
674        if( isset($ret) )return $ret;
675
676    }
677
678    return false;
679}
680
681// =================================================
682// make mentions
683// =================================================
684function member_info()
685{
686    if( !isset($GLOBALS['TT_MEMBER_INFO']) )
687    {
688        $sql = "SELECT `id` as `uid` , `name` FROM `user` WHERE `level` > 0 AND `is_closed` != 1 ";
689        if($data = get_data($sql))
690        {
691            foreach( $data as $item )
692            {
693                $name = trim($item['name']);
694                $GLOBALS['TT_MEMBER_INFO']['@'.$name] = 
695                '<a href="javascript:void(0);" uid=' . $item['uid'] . ' class="namecard">'. '@'.$name .'</a>';
696
697                if(c('at_short_name'))
698                    if( mb_strlen( $name , 'UTF-8' ) == 3 )
699                        $GLOBALS['TT_MEMBER_INFO']['@'.mb_substr($name , 1 , 2 , 'UTF-8' )] =
700                        '<a href="javascript:void(0);" uid=' . $item['uid'] . ' class="namecard">'. '@'.mb_substr($name , 1 , 2 , 'UTF-8' ).'</a>';
701                        
702            }
703
704        }
705    }
706    return  $GLOBALS['TT_MEMBER_INFO'];
707}
708
709function link_at( $str )
710{
711    $to_replace = array_keys( member_info() );
712    $replace_to = array_values( member_info() );
713    return str_replace( $to_replace , $replace_to , $str );
714}
715
716function find_links( $html )
717{
718    $reg = '/(http:\/\/(.+?))((\s+)|$)/is';
719    if( preg_match_all( $reg , $html , $out ) )
720    {
721        foreach( $out[0] as $item )
722        {
723            $ret[] = trim($item);
724        }
725
726        $ret = array_unique($ret);
727        return $ret;
728    }
729    return false;
730}
731
732
733function array_remove( $value , $array )
734{
735    return array_diff($array, array($value));
736}
737
738function phpmailer_send_mail(  $to , $subject , $body , $from ,  $host , $port , $user , $password )
739{
740    if( !isset( $GLOBALS['LP_MAILER'] ) )
741    {
742        include_once( AROOT . 'lib' . DS . 'phpmailer.class.php' );
743        $GLOBALS['LP_MAILER'] = new PHPMailer();
744    }
745
746    $mail = $GLOBALS['LP_MAILER'];
747    $mail->CharSet = 'UTF-8';
748    $mail->Encoding = 'base64';
749    $mail->IsSMTP(); 
750    $mail->Host = $host;
751    $mail->SMTPAuth = true;   
752    //$mail->SMTPKeepAlive = true;
753    $mail->Port = $port;
754    $mail->Username = $user;
755    $mail->Password = $password;
756    $mail->SetFrom($from );
757    $mail->AddReplyTo($from);
758
759    $mail->Subject = $subject ;
760    $mail->WordWrap = 50;
761    $mail->MsgHTML($body);
762    $mail->AddAddress( $to );
763
764    if(!$mail->Send())
765    {
766        //echo $mail->ErrorInfo;
767        return false;
768    }
769    else
770    {
771        $mail->ClearAddresses();
772        return true;
773    }
774   
775
776
777    
778
779}
780
781
782?>