PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/includes/functions.php

http://yuan-pad.googlecode.com/
PHP | 566 lines | 317 code | 34 blank | 215 comment | 49 complexity | 5d72d009a9d8799715df8883becf8d68 MD5 | raw file
  1. <?php
  2. /**
  3. * @author rainyjune <dreamneverfall@gmail.com>
  4. * @version $Id: functions.php 8 2011-11-01 04:32:33Z rainyjune@live.cn $
  5. */
  6. /**
  7. * Validate IP Address
  8. *
  9. * @param string $ip
  10. * @return boolean
  11. */
  12. function valid_ip($ip)
  13. {
  14. return filter_var($ip,FILTER_VALIDATE_IP);
  15. }
  16. /**
  17. * Finds whether the user is admin or not , redirect browser to the login page if not admin.
  18. *
  19. */
  20. function is_admin()
  21. {
  22. if (!isset($_SESSION['admin']))
  23. {
  24. header("Location:index.php?controller=user&action=login");exit;
  25. }
  26. }
  27. /**
  28. * Is GD Installed?
  29. * CI 1.7.2
  30. * @access public
  31. * @return bool
  32. */
  33. function gd_loaded()
  34. {
  35. if ( ! extension_loaded('gd'))
  36. {
  37. if ( ! @dl('gd.so'))
  38. return FALSE;
  39. }
  40. return TRUE;
  41. }
  42. /**
  43. * Get GD version
  44. *
  45. * @access public
  46. * @return mixed
  47. */
  48. function gd_version()
  49. {
  50. $gd_version=FALSE;
  51. if (defined('GD_VERSION'))
  52. $gd_version=GD_VERSION;
  53. elseif(function_exists('gd_info'))
  54. {
  55. $gd_version = @gd_info();
  56. $gd_version = $gd_version['GD Version'];
  57. }
  58. return $gd_version;
  59. }
  60. /**
  61. * Get IP of visitor
  62. *
  63. * @return string
  64. */
  65. function getIP()
  66. {
  67. $ip = $_SERVER['REMOTE_ADDR'];
  68. if (!empty($_SERVER['HTTP_CLIENT_IP']))
  69. $ip = $_SERVER['HTTP_CLIENT_IP'];
  70. elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  71. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  72. $ip=$ip?$ip:'127.0.0.1';
  73. return $ip;
  74. }
  75. /**
  76. * Finds whether a value is a valid email
  77. *
  78. * @param string $value
  79. * @return bool
  80. */
  81. function is_email($value)
  82. {
  83. return preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $value);
  84. }
  85. /**
  86. * Attach one event to an action
  87. *
  88. * @global array $actionEvent
  89. * @param string $action <p>The name of action</p>
  90. * @param mixed $evt <p>The name of event</p>
  91. */
  92. function attachEvent($action,$evt)
  93. {
  94. global $actionEvent;
  95. if (!@in_array($evt, $actionEvent[$action]))
  96. $actionEvent[$action][]=$evt;
  97. }
  98. /**
  99. * ?????action???
  100. * @global array $actionEvent
  101. * @param string $action
  102. * @param array $param
  103. */
  104. function performEvent($action,$param=array())
  105. {
  106. global $actionEvent;
  107. $functions=@$actionEvent[$action];
  108. if($functions){
  109. foreach($functions as $function){
  110. call_user_func_array($function, $param);
  111. }
  112. }
  113. }
  114. /**
  115. * Find the appropriate configuration directory.
  116. *
  117. * Try finding a matching configuration directory by stripping the website's
  118. * hostname from left to right and pathname from right to left. The first
  119. * configuration file found will be used; the remaining will ignored. If no
  120. * configuration file is found, return a default value '$confdir/default'.
  121. *
  122. * Example for a fictitious site installed at
  123. * http://www.drupal.org:8080/mysite/test/ the 'settings.php' is searched in
  124. * the following directories:
  125. *
  126. * 1. $confdir/8080.www.drupal.org.mysite.test
  127. * 2. $confdir/www.drupal.org.mysite.test
  128. * 3. $confdir/drupal.org.mysite.test
  129. * 4. $confdir/org.mysite.test
  130. *
  131. * 5. $confdir/8080.www.drupal.org.mysite
  132. * 6. $confdir/www.drupal.org.mysite
  133. * 7. $confdir/drupal.org.mysite
  134. * 8. $confdir/org.mysite
  135. *
  136. * 9. $confdir/8080.www.drupal.org
  137. * 10. $confdir/www.drupal.org
  138. * 11. $confdir/drupal.org
  139. * 12. $confdir/org
  140. *
  141. * 13. $confdir/default
  142. *
  143. * @param $require_settings
  144. * Only configuration directories with an existing settings.php file
  145. * will be recognized. Defaults to TRUE. During initial installation,
  146. * this is set to FALSE so that Drupal can detect a matching directory,
  147. * then create a new settings.php file in it.
  148. * @param reset
  149. * Force a full search for matching directories even if one had been
  150. * found previously.
  151. * @return
  152. * The path of the matching directory.
  153. */
  154. function conf_path($require_settings = TRUE, $reset = FALSE)
  155. {
  156. static $conf = '';//????
  157. if ($conf && !$reset) {//?????????? $reset ???
  158. return $conf;//???????
  159. }
  160. $confdir = 'sites';//????
  161. $uri = explode('/', $_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['SCRIPT_FILENAME']);//???????
  162. $server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));//?????????
  163. for ($i = count($uri) - 1; $i > 0; $i--) {//??
  164. for ($j = count($server); $j > 0; $j--) {
  165. $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
  166. if (file_exists("$confdir/$dir/config.php") || (!$require_settings && file_exists("$confdir/$dir"))) {//?????????????????????????????
  167. $conf = "$confdir/$dir";
  168. return $conf;
  169. }
  170. }
  171. }
  172. $conf = "$confdir/default";
  173. return $conf;
  174. }
  175. /**
  176. * Finds whether the database type of guest book is flatfile (Php Textfile DB API)
  177. *
  178. * @global string $db_url
  179. * @return bool
  180. */
  181. function is_flatfile()
  182. {
  183. global $db_url;
  184. if(substr($db_url, 0, 8)=='flatfile')
  185. return true;
  186. return false;
  187. }
  188. /**
  189. * Delete backuped data , only triggered by admin logout
  190. *
  191. * @global string $db_url
  192. */
  193. function delete_backup_files()
  194. {
  195. global $db_url;
  196. is_admin();
  197. $url = parse_url($db_url);
  198. $url['path'] = urldecode($url['path']);
  199. $dbname=substr($url['path'], 1);
  200. $dir=APPROOT.'/data/'.$dbname;
  201. $d=dir($dir);
  202. while(false!==($entry=$d->read()))
  203. {
  204. if (strlen($entry)==19)
  205. {
  206. $d_file=$dir.'/'.$entry;
  207. unlink($d_file);
  208. }
  209. }
  210. $d->close();
  211. }
  212. /**
  213. * Finds whether an IP address is bloked by guest book
  214. *
  215. * @global string $db_url
  216. * @param string $ip
  217. * @return bool
  218. */
  219. function is_baned($ip)
  220. {
  221. global $db_url;
  222. $all_baned_ips=array();
  223. $db=YDB::factory($db_url);
  224. $result=$db->queryAll(sprintf(parse_tbprefix("SELECT * FROM <badip> WHERE ip='%s'"),$db->escape_string($ip)));
  225. if($result)
  226. return true;
  227. return false;
  228. }
  229. /**
  230. *
  231. *
  232. * @global string $db_prefix
  233. * @param string $str
  234. * @return string
  235. */
  236. function parse_tbprefix($str)
  237. {
  238. global $db_prefix;
  239. return strtr($str,array('<'=>$db_prefix,'>'=>''));
  240. }
  241. function get_all_data($parse_smileys=true,$filter_words=false,$processUsername=false,$processTime=false,$apply_filter=true){
  242. global $db_url;
  243. $db=YDB::factory($db_url);
  244. $data=array();
  245. $data=$db->queryAll(parse_tbprefix("SELECT p.pid AS id, p.ip AS ip , p.uid AS uid ,p.uname AS user,p.content AS post_content,p.post_time AS time,r.content AS reply_content,r.r_time AS reply_time ,u.username AS b_username FROM <post> AS p LEFT JOIN <reply> AS r ON p.pid=r.pid LEFT JOIN <user> AS u ON p.uid=u.uid ORDER BY p.post_time DESC"));
  246. foreach ($data as &$_data) {
  247. if($apply_filter && ZFramework::app()->filter_type==ConfigController::FILTER_TRIPTAGS){
  248. if(strstr(ZFramework::app()->allowed_tags, 'code')){//???? code ???????
  249. $_data['post_content'] = preg_replace_callback('|<code>(.*)</code>|sU', create_function(
  250. // single quotes are essential here,
  251. // or alternative escape all $ as \$
  252. '$matches',
  253. 'return "<pre class=\'prettyprint\'>".str_replace(">","&gt;",str_replace("<","&lt;",$matches[1]))."</pre>";'
  254. ),$_data['post_content']);
  255. $_data['reply_content'] = preg_replace_callback('|<code>(.*)</code>|sU', create_function(
  256. // single quotes are essential here,
  257. // or alternative escape all $ as \$
  258. '$matches',
  259. 'return "<pre class=\'prettyprint\'>".str_replace(">","&gt;",str_replace("<","&lt;",$matches[1]))."</pre>";'
  260. ),$_data['reply_content']);
  261. if(!strstr(ZFramework::app()->allowed_tags, 'pre')){//???? code ???? pre ??? pre
  262. ZFramework::app()->allowed_tags .= "<pre>";
  263. }
  264. }
  265. $_data['post_content']=strip_tags ($_data['post_content'], ZFramework::app()->allowed_tags);
  266. $_data['reply_content']=strip_tags ($_data['reply_content'], ZFramework::app()->allowed_tags);
  267. } else{
  268. $_data['post_content']= htmlentities($_data['post_content'],ENT_COMPAT,'UTF-8');
  269. $_data['reply_content']=htmlentities($_data['reply_content'],ENT_COMPAT,'UTF-8');
  270. }
  271. if($parse_smileys){
  272. $_data['post_content']=parse_smileys ($_data['post_content'], SMILEYDIR, getSmileys());
  273. $_data['reply_content']=parse_smileys ($_data['reply_content'], SMILEYDIR, getSmileys());
  274. }
  275. if($filter_words)
  276. $_data['post_content']=filter_words($_data['post_content']);
  277. if($processUsername)
  278. $_data['user']=($_data['user']==ZFramework::app()->admin)?"<font color='red'>{$_data['user']}</font>":$_data['user'];
  279. if($processTime){
  280. $_data['time']=date('m-d H:i',$_data['time']+ZFramework::app()->timezone*60*60);
  281. $_data['reply_time']=date('m-d H:i',$_data['reply_time']+ZFramework::app()->timezone*60*60);
  282. }
  283. }
  284. return $data;
  285. }
  286. /**
  287. * ????????????
  288. * @param $str
  289. * @param $image_url
  290. * @param $smileys
  291. */
  292. function parse_smileys($str = '', $image_url = '', $smileys = NULL)
  293. {
  294. if ($image_url == '')
  295. return $str;
  296. if (!is_array($smileys))
  297. return $str;
  298. // Add a trailing slash to the file path if needed
  299. $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
  300. foreach ($smileys as $key => $val){
  301. $str = str_replace($key, "<img src=\"".$image_url.$smileys[$key][0]."\" width=\"".$smileys[$key][1]."\" height=\"".$smileys[$key][2]."\" title=\"".$smileys[$key][3]."\" alt=\"".$smileys[$key][3]."\" style=\"border:0;\" />", $str);
  302. }
  303. return $str;
  304. }
  305. /**
  306. * ??????
  307. * @param array $input
  308. */
  309. function filter_words($input)
  310. {
  311. $filter_array=explode(',', ZFramework::app()->filter_words);
  312. $input=str_ireplace($filter_array,'***',$input);
  313. return $input;
  314. }
  315. /**
  316. * ????
  317. */
  318. function show_smileys_table()
  319. {
  320. $smiley= require APPROOT.'/includes/showSmiley.php';
  321. return $smiley;
  322. }
  323. /**
  324. * ????????
  325. * @param array $filter_words
  326. */
  327. function fix_filter_string($filter_words)
  328. {
  329. $new_string=trim($filter_words,',');
  330. $new_string=str_replace(array("\t","\r","\n",' ',' '),'',$new_string);
  331. return $new_string;
  332. }
  333. /**
  334. * Gets supported RDBMS type
  335. *
  336. * @return array
  337. */
  338. function get_supported_rdbms()
  339. {
  340. $supported_rdbms=array();
  341. $rdbms_functions=array('mysql'=>'mysql_connect','mysqli'=>'mysqli_connect','sqlite'=>'sqlite_open');
  342. $rdbms_names=array('mysql'=>'MySQL','mysqli'=>'MySQL Improved','sqlite'=>'SQLite');
  343. foreach ($rdbms_functions as $k => $v) {
  344. if(function_exists($v)){
  345. $supported_rdbms[$rdbms_names[$k]]=$k;
  346. }
  347. }
  348. return $supported_rdbms;
  349. }
  350. /**
  351. * ???????
  352. *
  353. * @return bool
  354. */
  355. function is_installed()
  356. {
  357. global $db_url;
  358. if($db_url=='dummydb://username:password@localhost/databasename')
  359. return false;
  360. return true;
  361. }
  362. /**
  363. * ?????
  364. *
  365. */
  366. function maple_quotes($var,$charset='UTF-8')
  367. {
  368. return htmlspecialchars(trim($var),ENT_QUOTES, $charset);
  369. }
  370. /**
  371. * ??????????
  372. * @param $name ?????
  373. * @return mixed ???????NULL
  374. */
  375. function getConfigVar($name)
  376. {
  377. global $db_url;
  378. $db=YDB::factory($db_url);
  379. $result=$db->queryAll(sprintf(parse_tbprefix("SELECT * FROM <sysvar> WHERE varname='%s'"), $db->escape_string($name)));
  380. $result=@$result[0]['varvalue'];
  381. if($result)
  382. return $result;
  383. else
  384. return null;
  385. }
  386. /**
  387. * ??????
  388. * @return array
  389. */
  390. function getSmileys()
  391. {
  392. return include dirname(__FILE__).'/smiley.php';
  393. }
  394. /**
  395. * ?????????
  396. */
  397. function get_all_themes()
  398. {
  399. $themes=array();
  400. $d=dir(THEMEDIR);
  401. while(false!==($entry=$d->read())){
  402. if(substr($entry,0,1)!='.')
  403. $themes[$entry]=$entry;
  404. }
  405. $d->close();
  406. return array_filter($themes,'_removeIndex');
  407. }
  408. /**
  409. * ??????????? languages/ ?????????
  410. *
  411. * @return array
  412. */
  413. function get_all_langs()
  414. {
  415. $langs=array();
  416. $d=dir(APPROOT.'/languages/');
  417. while(false!==($entry=$d->read())){
  418. if(substr($entry,0,1)!='.')
  419. $langs[substr($entry,0,-4)]=substr($entry,0,-4);
  420. }
  421. $d->close();
  422. return array_filter($langs,'_removeIndex');
  423. }
  424. function _removeIndex($var){
  425. return (!($var == 'index' || $var == 'index.php'));
  426. }
  427. /**
  428. * ????????????????
  429. *
  430. * @return array
  431. */
  432. function get_all_timezone()
  433. {
  434. $timezone= include APPROOT.'/languages/'.getConfigVar('lang').'.php';
  435. return $timezone['TZ_ZONES'];
  436. }
  437. /**
  438. * ????
  439. */
  440. function show_message($msg,$redirect=false,$redirect_url='index.php',$time_delay=3)
  441. {
  442. include 'themes/'.getConfigVar('theme').'/templates/'."show_message.php"; exit;
  443. }
  444. /**
  445. * ?????????????
  446. *
  447. * @param mixed $userSpecifiedLanguage
  448. * @return array
  449. */
  450. function getLangArray($userSpecifiedLanguage=null)
  451. {
  452. if($userSpecifiedLanguage)
  453. {
  454. if(file_exists(APPROOT.'/languages/'.$userSpecifiedLanguage.'.php'))
  455. {
  456. return include APPROOT.'/languages/'.$userSpecifiedLanguage.'.php';
  457. }
  458. }
  459. return include APPROOT.'/languages/'.getConfigVar('lang').'.php';
  460. }
  461. /**
  462. * ??????
  463. *
  464. * @param boolean $loadPlugin ??????
  465. * @return array
  466. */
  467. function get_alll_plugins($loadPlugin=FALSE)
  468. {
  469. $plugins=array();
  470. $d=dir(PLUGINDIR);
  471. while(false!==($entry=$d->read())){
  472. if(substr($entry,0,1)!='.' && is_dir(PLUGINDIR.DIRECTORY_SEPARATOR.$entry)){
  473. $plugins[$entry]=$entry;
  474. if($loadPlugin){
  475. require_once PLUGINDIR.$entry.DIRECTORY_SEPARATOR.$entry.'.php';
  476. }
  477. }
  478. }
  479. $d->close();
  480. return array_filter($plugins,'_removeIndex');
  481. }
  482. /**
  483. * ??????
  484. *
  485. * @param mixed $message
  486. * @param array $params
  487. * @param mixed $userSpecifiedLanguage
  488. * @return string
  489. */
  490. function t($message,$params=array(),$userSpecifiedLanguage=null)
  491. {
  492. $messages=getLangArray($userSpecifiedLanguage);
  493. if(isset ($messages[$message]) && $messages[$message]!=='')
  494. $message=$messages[$message];
  495. return $params!==array()?strtr($message, $params):$message;
  496. }
  497. /**
  498. * ?????????
  499. *
  500. */
  501. function is_closedMode()
  502. {
  503. $disabledAction=array('PostController/actionCreate','SiteController/actionIndex','UserController/actionCreate');
  504. if(getConfigVar('site_close')==1 && !isset ($_SESSION['admin']) && in_array((isset($_GET['controller'])?$_GET['controller']:'SiteController').'/'.(isset($_GET['action'])?$_GET['action']:'actionIndex'), $disabledAction))
  505. show_message(getConfigVar('close_reason'));
  506. }
  507. /**
  508. * Un-quotes a string or an array
  509. *
  510. * @param mixed $value
  511. * @return mixed
  512. */
  513. function stripslashes_deep($value)
  514. {
  515. return is_array($value)?array_map('stripslashes_deep',$value):stripslashes($value);
  516. }
  517. /**
  518. * ?????????????.
  519. * @return void
  520. */
  521. function maple_unset_globals()
  522. {
  523. if (ini_get('register_globals') && (strtolower(ini_get('register_globals'))!='off'))
  524. {
  525. $allowed = array('_ENV' => 1, '_GET' => 1, '_POST' => 1, '_COOKIE' => 1,'_SESSION'=>1,'_FILES' => 1, '_SERVER' => 1, '_REQUEST' => 1, 'GLOBALS' => 1);
  526. foreach ($GLOBALS as $key => $value)
  527. {
  528. if (!isset($allowed[$key]))
  529. unset($GLOBALS[$key]);
  530. }
  531. }
  532. }