PageRenderTime 248ms CodeModel.GetById 217ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/app.function.php

https://github.com/nigelbaor/TeamToy
PHP | 825 lines | 764 code | 50 blank | 11 comment | 35 complexity | 171596300288ac4c80411938f38824a5 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(__('DATABASE_INIT_FINISHED' , $password ));
 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( __('DATE_FULL_FORMAT') ,$time);
187    elseif( time() > ($time+60*60*8) ) return date( __('DATE_SHORT_FORMAT') ,$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 = null) 
199{
200	if( $format === null ) $format = __('DATE_SHORT_FORMAT');
201
202    $time = strtotime($time);
203
204	$now = time();
205	$relative = '';
206
207	if ($time === $now) $relative = __('DATE_RELATED_NOW');
208	elseif ($time > $now) $relative = __('DATE_RELATED_AFTER') ;
209	else 
210	{
211		$diff = $now - $time;
212
213		if ($diff >= $limit) $relative = date($format, $time);
214
215		elseif ($diff < 60) 
216		{
217			$relative = __('DATE_RELATED_LESS_THAN_A_MINUTE');
218		}
219		elseif (($minutes = ceil($diff/60)) < 60)
220		{
221			if( (int)$minutes === 1 ) $relative = __( 'DATE_RELATED_ONE_MINUTE' );
222            else  $relative = __( 'DATE_RELATED_SOME_MINUTES' ,  $minutes );      
223		}
224		else
225		{
226			$hours = ceil($diff/3600);
227
228            if( (int)$hours === 1 ) $relative = __( 'DATE_RELATED_ONE_HOUR' );
229            else  $relative = __( 'DATE_RELATED_SOME_HOURS' ,  $hours );  
230
231		}
232	}
233
234	return $relative;
235}
236
237function noname( $content , $name )
238{
239	$len = strlen($name);
240	if( substr( $content , 0 , $len ) == $name )
241		return substr( $content , $len  );
242	else
243		return $content;
244}
245
246function render_html( $data , $tpl )
247{
248	ob_start();
249	extract($data);
250	require( $tpl );
251	$content = ob_get_contents();
252	ob_end_clean();
253	return $content;
254	// 
255}
256
257function read_class( $is_read )
258{
259    if( intval($is_read) == 1 ) return  'read' ; 
260    else return  'unread';
261}
262
263function feed_class(  $type )
264{
265	switch( $type )
266	{
267		case 2:
268			 $class= 'todo';
269			break;
270		case 1:
271			$class= 'notice';
272			break;
273		case 3:
274			$class= 'user';
275			break;
276
277        case 4:
278            $class= 'cast';
279            break;   
280            
281		default:
282			$class = 'normal';		
283	}
284
285	return $class;
286}
287
288function device( $type )
289{
290	if( strtolower(t($type)) == 'mobile' )
291		$ret = __('FROM_MOBILE_DEVICE');
292	else
293		$ret = __('FROM_WEB_DEVICE');
294	return $ret;
295}
296
297function get_device()
298{
299    if( is_mobile_request()) return 'mobile';
300    else return 'web';
301}
302
303
304// ========================================
305// client functions
306// ========================================
307
308
309function login( $email , $password )
310{
311    $params = array();
312    $params['email'] = $email;
313    $params['password'] = $password;
314
315    if($content = send_request( 'user_get_token' ,  $params ))
316    {
317        $data = json_decode( $content , 1 );
318        if( ($data['err_code'] == 0) && is_array( $data['data'] ) )
319            return $data['data'];
320        else
321            return false;
322    }
323    return null;
324}
325
326function token()
327{
328	return $_SESSION['token'];
329}
330
331function send_request( $action , $param , $token = null )
332{
333	require_once( AROOT . 'controller' . DS . 'api.class.php' );
334    require_once( AROOT . 'model' . DS . 'api.function.php' );
335    $GLOBALS['API_EMBED_MODE'] = 1;
336        
337    // local request
338    $bake_request = $_REQUEST;
339    $_REQUEST['c'] = 'api';
340    $GLOBALS['a'] = $_REQUEST['a'] = $action;
341    if( $token !== null )
342        $_REQUEST['token'] = $token;
343
344    if( (is_array( $param )) && (count($param) > 0) )
345        foreach( $param as $key => $value )
346        {
347            $_REQUEST[$key] =  $value ;
348        }
349            
350
351    $api = new apiController();
352    // magic call
353    if( method_exists($api, $action) || has_hook('API_'.$action) )
354    {
355        $content = $api->$action();
356        $_REQUEST = $bake_request;
357        $GLOBALS['a'] = $_REQUEST['a'];
358       
359        return $content;
360        //if($data = json_decode( $content , 1 ))
361        //return json_encode($data['data']);
362    }
363    else
364    {
365        return 'API_'.$action . ' NOT EXISTS';
366    }
367   
368    return null;
369    
370    
371    // remote request ...........
372    /*
373
374    $url = c('api_server') . '?c=api&a=' . u($action) . '&token=' . u($token) ;
375	
376	if( (is_array( $param )) && (count($param) > 0) )
377		foreach( $param as $key => $value )
378			$url .= '&' . $key . '=' . u( $value );
379	
380	
381
382	if($content = file_get_contents( $url ))
383		return $content;
384	
385	return $url;
386	*/
387}
388
389function find_at( $text )
390{
391	$reg = '/@(\S+?(?:\s|$))/is';
392	if( preg_match_all( $reg , $text , $out ) )
393		return $out[1];
394	else
395		return false;
396
397}
398
399function jpeg_up( $source , $dest )
400{
401	if( !function_exists('exif_read_data') ) return copy( $source , $dest );
402    $img_info = @exif_read_data( $source , ANY_TAG  );
403  	switch( $img_info['Orientation'] )
404  	{
405  	  	case 6:
406  		  	$r = 270;
407  		  	break;
408                        
409          	case 3:
410                	$r = 180;
411                        break;
412                        
413          	case 8:
414                	$r = 90;
415                        break;
416                        
417          	default:
418                	$r = 0;
419  	}
420  
421  	$img_src = ImageCreateFromJPEG( $source );
422    $rotate = imagerotate($img_src, $r, 0,true);    
423	ImageJPEG($rotate,$dest);
424        
425        
426        
427}
428
429function upload_as_form( $url , $data )
430{
431    @session_write_close(); 
432    $ch = curl_init();
433    curl_setopt($ch, CURLOPT_URL, $url);
434    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
435    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
436    curl_setopt($ch, CURLOPT_POST, true);
437    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
438    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
439
440    $ch = apply_filter( 'UPLOAD_CURL_SETTINGS' , $ch );
441
442    $response = curl_exec($ch);
443    return $response;
444}
445
446
447function pinyin($_string, $_code='utf8')
448{ //gbk页面可改为gb2312,其他随意填写为utf8
449        $_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". 
450                        "|chai|chan|chang|chao|che|chen|cheng|chi|chong|chou|chu|chuai|chuan|chuang|chui|chun|chuo|ci|cong|cou|cu|". 
451                        "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". 
452                        "|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". 
453                        "|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". 
454                        "|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". 
455                        "|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". 
456                        "|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". 
457                        "|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". 
458                        "|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". 
459                        "|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|". 
460                        "she|shen|sheng|shi|shou|shu|shua|shuai|shuan|shuang|shui|shun|shuo|si|song|sou|su|suan|sui|sun|suo|ta|tai|". 
461                        "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". 
462                        "|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". 
463                        "|yu|yuan|yue|yun|za|zai|zan|zang|zao|ze|zei|zen|zeng|zha|zhai|zhan|zhang|zhao|zhe|zhen|zheng|zhi|zhong|". 
464                        "zhou|zhu|zhua|zhuai|zhuan|zhuang|zhui|zhun|zhuo|zi|zong|zou|zu|zuan|zui|zun|zuo"; 
465        $_datavalue = "-20319|-20317|-20304|-20295|-20292|-20283|-20265|-20257|-20242|-20230|-20051|-20036|-20032|-20026|-20002|-19990". 
466                        "|-19986|-19982|-19976|-19805|-19784|-19775|-19774|-19763|-19756|-19751|-19746|-19741|-19739|-19728|-19725". 
467                        "|-19715|-19540|-19531|-19525|-19515|-19500|-19484|-19479|-19467|-19289|-19288|-19281|-19275|-19270|-19263". 
468                        "|-19261|-19249|-19243|-19242|-19238|-19235|-19227|-19224|-19218|-19212|-19038|-19023|-19018|-19006|-19003". 
469                        "|-18996|-18977|-18961|-18952|-18783|-18774|-18773|-18763|-18756|-18741|-18735|-18731|-18722|-18710|-18697". 
470                        "|-18696|-18526|-18518|-18501|-18490|-18478|-18463|-18448|-18447|-18446|-18239|-18237|-18231|-18220|-18211". 
471                        "|-18201|-18184|-18183|-18181|-18012|-17997|-17988|-17970|-17964|-17961|-17950|-17947|-17931|-17928|-17922". 
472                        "|-17759|-17752|-17733|-17730|-17721|-17703|-17701|-17697|-17692|-17683|-17676|-17496|-17487|-17482|-17468". 
473                        "|-17454|-17433|-17427|-17417|-17202|-17185|-16983|-16970|-16942|-16915|-16733|-16708|-16706|-16689|-16664". 
474                        "|-16657|-16647|-16474|-16470|-16465|-16459|-16452|-16448|-16433|-16429|-16427|-16423|-16419|-16412|-16407". 
475                        "|-16403|-16401|-16393|-16220|-16216|-16212|-16205|-16202|-16187|-16180|-16171|-16169|-16158|-16155|-15959". 
476                        "|-15958|-15944|-15933|-15920|-15915|-15903|-15889|-15878|-15707|-15701|-15681|-15667|-15661|-15659|-15652". 
477                        "|-15640|-15631|-15625|-15454|-15448|-15436|-15435|-15419|-15416|-15408|-15394|-15385|-15377|-15375|-15369". 
478                        "|-15363|-15362|-15183|-15180|-15165|-15158|-15153|-15150|-15149|-15144|-15143|-15141|-15140|-15139|-15128". 
479                        "|-15121|-15119|-15117|-15110|-15109|-14941|-14937|-14933|-14930|-14929|-14928|-14926|-14922|-14921|-14914". 
480                        "|-14908|-14902|-14894|-14889|-14882|-14873|-14871|-14857|-14678|-14674|-14670|-14668|-14663|-14654|-14645". 
481                        "|-14630|-14594|-14429|-14407|-14399|-14384|-14379|-14368|-14355|-14353|-14345|-14170|-14159|-14151|-14149". 
482                        "|-14145|-14140|-14137|-14135|-14125|-14123|-14122|-14112|-14109|-14099|-14097|-14094|-14092|-14090|-14087". 
483                        "|-14083|-13917|-13914|-13910|-13907|-13906|-13905|-13896|-13894|-13878|-13870|-13859|-13847|-13831|-13658". 
484                        "|-13611|-13601|-13406|-13404|-13400|-13398|-13395|-13391|-13387|-13383|-13367|-13359|-13356|-13343|-13340". 
485                        "|-13329|-13326|-13318|-13147|-13138|-13120|-13107|-13096|-13095|-13091|-13076|-13068|-13063|-13060|-12888". 
486                        "|-12875|-12871|-12860|-12858|-12852|-12849|-12838|-12831|-12829|-12812|-12802|-12607|-12597|-12594|-12585". 
487                        "|-12556|-12359|-12346|-12320|-12300|-12120|-12099|-12089|-12074|-12067|-12058|-12039|-11867|-11861|-11847". 
488                        "|-11831|-11798|-11781|-11604|-11589|-11536|-11358|-11340|-11339|-11324|-11303|-11097|-11077|-11067|-11055". 
489                        "|-11052|-11045|-11041|-11038|-11024|-11020|-11019|-11018|-11014|-10838|-10832|-10815|-10800|-10790|-10780". 
490                        "|-10764|-10587|-10544|-10533|-10519|-10331|-10329|-10328|-10322|-10315|-10309|-10307|-10296|-10281|-10274". 
491                        "|-10270|-10262|-10260|-10256|-10254"; 
492        $_tdatakey   = explode('|', $_datakey); 
493        $_tdatavalue = explode('|', $_datavalue);
494        $_data = array_combine($_tdatakey, $_tdatavalue);
495        arsort($_data); 
496        reset($_data);
497        if($_code!= 'gb2312') $_string = _u2_utf8_gb($_string); 
498        $_res = ''; 
499        for($i=0; $i<strlen($_string); $i++) { 
500                $_p = ord(substr($_string, $i, 1)); 
501                if($_p>160) { 
502                        $_q = ord(substr($_string, ++$i, 1)); $_p = $_p*256 + $_q - 65536;
503                } 
504                $_res .= _pinyin($_p, $_data); 
505        } 
506        return preg_replace("/[^a-z0-9]*/", '', $_res); 
507} 
508
509function _pinyin($_num, $_data)
510{ 
511        if($_num>0 && $_num<160 ){
512                return chr($_num);
513        }elseif($_num<-20319 || $_num>-10247){
514                return '';
515        }else{ 
516                foreach($_data as $k=>$v){ if($v<=$_num) break; } 
517                return $k; 
518        } 
519}
520
521function _u2_utf8_gb($_c)
522{ 
523        $_string = ''; 
524        if($_c < 0x80){
525                $_string .= $_c;
526        }elseif($_c < 0x800) { 
527                $_string .= chr(0xc0 | $_c>>6); 
528                $_string .= chr(0x80 | $_c & 0x3f); 
529        }elseif($_c < 0x10000){ 
530                $_string .= chr(0xe0 | $_c>>12); 
531                $_string .= chr(0x80 | $_c>>6 & 0x3f); 
532                $_string .= chr(0x80 | $_c & 0x3f); 
533        }elseif($_c < 0x200000) { 
534                $_string .= chr(0xf0 | $_c>>18); 
535                $_string .= chr(0x80 | $_c>>12 & 0x3f); 
536                $_string .= chr(0x80 | $_c>>6 & 0x3f); 
537                $_string .= chr(0x80 | $_c & 0x3f); 
538        } 
539        return iconv('utf-8', 'gb2312', $_string); 
540}
541
542// **************************************************************
543// * Plugins & hooks
544// ************************************************************** 
545function add_filter( $tag , $function_to_add , $priority = 10 , $accepted_args_num = 1 )
546{
547    return add_hook( $tag , $function_to_add , $priority , $accepted_args_num );
548}
549
550function add_action( $tag , $function_to_add , $priority = 10 , $accepted_args_num = 1 )
551{
552    return add_hook( $tag , $function_to_add , $priority , $accepted_args_num );
553}
554
555function add_hook( $tag , $function_to_add , $priority = 10 , $accepted_args_num = 1 )
556{
557    $tag = strtoupper($tag);
558    $idx = build_hook_id( $tag , $function_to_add , $priority );
559    $GLOBALS['TTHOOK'][$tag][$priority][$idx] = array( 'function' => $function_to_add , 'args_num' => $accepted_args_num );
560}
561
562function do_action( $tag , $value = null )
563{
564    return apply_hook( $tag , $value );
565}
566
567function apply_filter( $tag , $value = null )
568{
569    return apply_hook( $tag , $value );
570}
571
572
573
574function apply_hook( $tag , $value )
575{
576    $tag = strtoupper($tag);
577    if( $hooks  = has_hook( $tag ) )
578    {
579        ksort( $hooks );
580        $args = func_get_args();
581        reset( $hooks );
582
583        do
584        {
585            foreach( (array) current( $hooks ) as $hook )
586            {
587                if( !is_null($hook['function']) )
588                {
589                    $args[1] = $value;
590                    $value = call_user_func_array( $hook['function'] , array_slice($args, 1, (int) $hook['args_num']));
591                }
592            }
593        }while( next( $hooks ) !== false );
594
595    }
596
597    return $value;
598}
599
600function has_hook( $tag , $priority = null )
601{
602    $tag = strtoupper($tag);
603    if( is_null($priority) ) return isset( $GLOBALS['TTHOOK'][$tag] )? $GLOBALS['TTHOOK'][$tag]:false;
604    else return isset( $GLOBALS['TTHOOK'][$tag][$priority] )? $GLOBALS['TTHOOK'][$tag][$priority]:false;
605}
606
607function remove_hook( $tag , $priority = null )
608{
609    $tag = strtoupper($tag);
610    if( is_null($priority) ) unset( $GLOBALS['TTHOOK'][$tag] );
611    else unset( $GLOBALS['TTHOOK'][$tag][$priority] );
612}
613// This function is based on wordpress  
614// from  https://raw.github.com/WordPress/WordPress/master/wp-includes/plugin.php
615// requere php5.2+
616
617function build_hook_id( $tag , $function ) 
618{
619    if ( is_string($function) )
620        return $function;
621
622    if ( is_object($function) ) 
623    {
624        // Closures are currently implemented as objects
625        $function = array( $function, '' );
626    }
627    else
628    {
629        $function = (array) $function;
630    }
631
632    if (is_object($function[0]) ) 
633    {
634        // Object Class Calling
635        if ( function_exists('spl_object_hash') ) 
636        {
637            return spl_object_hash($function[0]) . $function[1];
638        }
639        else
640        {
641            return substr( serialize($function[0]) , 0 , 15 ). $function[1];
642        }
643
644    }
645    elseif( is_string($function[0]) )
646    {
647        // Static Calling
648        return $function[0].$function[1];
649    }
650}
651
652function scan_plugin_info()
653{
654    if( file_exists( c('plugin_path') ) )
655    foreach( glob( c('plugin_path') . DS . "*" , GLOB_ONLYDIR ) as $pfold  )
656    {
657        $app_file = $pfold .DS .'app.php';
658        if( file_exists( $app_file ) )
659            if($pinfo = get_plugin_info( file_get_contents( $app_file ) ))
660            $plist[] = $pinfo;
661    }
662    return isset( $plist ) ? $plist : false;
663}
664
665function get_plugin_info( $content )
666{
667    $reg = '/\*\*\*\s+(.+)\s+\*\*\*/is';
668    if( preg_match( $reg , $content , $out ) )
669    {
670        $info_content = $out[1];
671        $lines = explode('##',$info_content);
672        array_shift($lines);
673        foreach( $lines as $line )
674        {
675            $line = trim($line);
676            list( $key , $value ) = explode( ' ' , $line , 2 );
677            $ret[$key] = z(t($value)); 
678        }
679
680        if( isset($ret) )return $ret;
681
682    }
683
684    return false;
685}
686
687function ttpassv2( $password , $salt = '' )
688{
689    return substr( md5(md5( $password  ) . 'T!e*a-m^T$o#y' . $salt  ) , 0 , 30 );
690}
691
692// =================================================
693// make mentions
694// =================================================
695function member_info()
696{
697    if( !isset($GLOBALS['TT_MEMBER_INFO']) )
698    {
699        $sql = "SELECT `id` as `uid` , `name` FROM `user` WHERE `level` > 0 AND `is_closed` != 1 ";
700        if($data = get_data($sql))
701        {
702            foreach( $data as $item )
703            {
704                $name = trim($item['name']);
705                $GLOBALS['TT_MEMBER_INFO']['@'.$name] = 
706                '<a href="javascript:void(0);" uid=' . $item['uid'] . ' class="namecard">'. '@'.$name .'</a>';
707
708                if(c('at_short_name'))
709                    if( mb_strlen( $name , 'UTF-8' ) == 3 )
710                        $GLOBALS['TT_MEMBER_INFO']['@'.mb_substr($name , 1 , 2 , 'UTF-8' )] =
711                        '<a href="javascript:void(0);" uid=' . $item['uid'] . ' class="namecard">'. '@'.mb_substr($name , 1 , 2 , 'UTF-8' ).'</a>';
712                        
713            }
714
715        }
716    }
717    return  $GLOBALS['TT_MEMBER_INFO'];
718}
719
720function link_at( $str )
721{
722    $to_replace = array_keys( member_info() );
723    $replace_to = array_values( member_info() );
724    return str_replace( $to_replace , $replace_to , $str );
725}
726
727function find_links( $html )
728{
729    $reg = '/(http[s]*:\/\/([-a-zA-Z0-9@:%~#&_=\+\.\?\/]+?))((\s+)|$)/is';
730    if( preg_match_all( $reg , $html , $out ) )
731    {
732        foreach( $out[0] as $item )
733        {
734            $ret[] = trim($item);
735        }
736
737        $ret = array_unique($ret);
738        return $ret;
739    }
740    return false;
741}
742
743function replace_links( $html )
744{
745    $reg = '/(http[s]*:\/\/([-a-zA-Z0-9@:%~#&_=\+\.\?\/]+?))((\s+)|$)/is';
746    if(  $ret = preg_replace( $reg , "<a href='$1' target='_blank'>$1</a> " , $html ) )
747    {
748        return $ret;
749    }
750    else return $html;
751   
752
753}
754
755function js_i18n( $array )
756{
757    $ret = array();
758    foreach( $array as $key => $value )
759    {
760        if( strtoupper( substr( $key , 0 , 3 ) ) == 'JS_'  )
761            $ret[$key] = $value;
762    }
763    return $ret;
764}
765
766function plugin_append_lang( $lang_array )
767{
768    $c = g('i18n');
769    if( isset( $lang_array[$c] ) )
770        $GLOBALS['language'][$c] 
771    = array_merge( $GLOBALS['language'][$c] , $lang_array[$c] ) ;
772}
773
774
775function array_remove( $value , $array )
776{
777    return array_diff($array, array($value));
778}
779
780function phpmailer_send_mail(  $to , $subject , $body , $from ,  $host , $port , $user , $password )
781{
782    if( !isset( $GLOBALS['LP_MAILER'] ) )
783    {
784        include_once( AROOT . 'lib' . DS . 'phpmailer.class.php' );
785        $GLOBALS['LP_MAILER'] = new PHPMailer();
786    }
787
788    $mail = $GLOBALS['LP_MAILER'];
789    $mail->CharSet = 'UTF-8';
790    $mail->Encoding = 'base64';
791    $mail->IsSMTP(); 
792    $mail->Host = $host;
793    $mail->SMTPAuth = true;   
794    //$mail->SMTPKeepAlive = true;
795    $mail->Port = $port;
796    $mail->Username = $user;
797    $mail->Password = $password;
798    $mail->SetFrom($from );
799    $mail->AddReplyTo($from);
800
801    $mail->Subject = $subject ;
802    $mail->WordWrap = 50;
803    $mail->MsgHTML($body);
804    $mail->AddAddress( $to );
805
806    if(!$mail->Send())
807    {
808        $GLOBALS['LP_MAILER_ERROR'] = $mail->ErrorInfo;
809        //echo $mail->ErrorInfo;
810        return false;
811    }
812    else
813    {
814        $mail->ClearAddresses();
815        return true;
816    }
817   
818
819
820    
821
822}
823
824
825?>