PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/common.php

https://github.com/phpfalcon/Kleeja-2.0.0-alpha
PHP | 441 lines | 302 code | 63 blank | 76 comment | 87 complexity | a881671d393b4fe57c41c887e3bc7f90 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. *
  4. * @package Kleeja
  5. * @version $Id$
  6. * @copyright (c) 2007 Kleeja.com
  7. * @license ./docs/license.txt
  8. *
  9. */
  10. // not for directly open
  11. if (!defined('IN_INDEX'))
  12. {
  13. exit();
  14. }
  15. //we are in the common file
  16. define ('IN_COMMON', true);
  17. //
  18. //development stage; developers stage
  19. //
  20. define('DEV_STAGE', true);
  21. // Report all errors, except notices
  22. defined('DEV_STAGE') ? @error_reporting( E_ALL ) : @error_reporting(E_ALL ^ E_NOTICE);
  23. //Just to check
  24. define('IN_PHP6', (version_compare(PHP_VERSION, '6.0.0-dev', '>=') ? true : false));
  25. //filename of config.php
  26. define('KLEEJA_CONFIG_FILE', 'config.php');
  27. if(@extension_loaded('apc'))
  28. {
  29. define('APC_CACHE', true);
  30. }
  31. //if sessions is started before, let's destroy it!
  32. if(isset($_SESSION))
  33. {
  34. @session_unset(); // fix bug with php4
  35. @session_destroy();
  36. }
  37. // start session
  38. $s_time = 86400 * 2; // 2 : two days
  39. if(defined('IN_ADMIN'))
  40. {
  41. //session_set_cookie_params($admintime);
  42. if (function_exists('session_set_cookie_params'))
  43. {
  44. session_set_cookie_params($adm_time, $adm_path);
  45. }
  46. elseif (function_exists('ini_set'))
  47. {
  48. ini_set('session.cookie_lifetime', $adm_time);
  49. ini_set('session.cookie_path', $adm_path);
  50. }
  51. }
  52. if(function_exists('ini_set'))
  53. {
  54. if (version_compare(PHP_VERSION, '5.0.0', 'ge') && substr(PHP_OS, 0 ,3) != 'WIN')
  55. {
  56. ini_set('session.hash_function', 1);
  57. ini_set('session.hash_bits_per_character', 6);
  58. }
  59. ini_set('session.use_only_cookies', false);
  60. ini_set('session.auto_start', false);
  61. ini_set('session.use_trans_sid', true);
  62. ini_set('session.cookie_lifetime', $s_time);
  63. ini_set('session.gc_maxlifetime', $s_time);
  64. //& is not valid xhtml, so we replaced with &amp;
  65. ini_set('arg_separator.output', '&amp;');
  66. //
  67. //this will help people with some problem with their sessions path
  68. //
  69. //session_save_path('./cache/');
  70. }
  71. /**
  72. * functions for start
  73. */
  74. function kleeja_show_error($errno, $errstr = '', $errfile = '', $errline = '')
  75. {
  76. switch ($errno)
  77. {
  78. case E_NOTICE: case E_WARNING: case E_USER_WARNING: case E_USER_NOTICE: case E_STRICT: break;
  79. default:
  80. header('HTTP/1.1 503 Service Temporarily Unavailable');
  81. echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">' . "\n<head>\n";
  82. echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' . "\n";
  83. echo '<title>Kleeja Error</title>' . "\n" . '<style type="text/css">' . "\n\t";
  84. echo '* { margin: 0; padding: 0; }' . "\n\t" . 'body { background: #fff;margin: 0 auto;padding: 50px;width: 767px;}' . "\n\t";
  85. echo '.error {color: #333;background:#ffebe8;border: 1px solid #dd3c10; padding: 10px;font-family:tahoma,arial;font-size: 12px;}' . "\n";
  86. echo "</style>\n</head>\n<body>\n\t" . '<div class="error">' . "\n\n\t\t<h2>Kleeja error : </h2><br />" . "\n";
  87. echo "\n\t\t<strong> [ " . $errno . ':' . basename($errfile) . ':' . $errline . ' ] </strong><br /><br />' . "\n\t\t" . $errstr . "\n\t";
  88. echo "\n\t\t" . '<br /><br /><small>Visit <a href="http://www.kleeja.com/" title="kleeja">Kleeja</a> Website for more details.</small>' . "\n\t";
  89. echo "</div>\n</body>\n</html>";
  90. global $SQL;
  91. if(isset($SQL))
  92. {
  93. @$SQL->close();
  94. }
  95. exit;
  96. break;
  97. }
  98. }
  99. set_error_handler('kleeja_show_error');
  100. function stripslashes_our($value)
  101. {
  102. return is_array($value) ? array_map('stripslashes_our', $value) : stripslashes($value);
  103. }
  104. function kleeja_clean_string($value)
  105. {
  106. if(is_array($value))
  107. {
  108. return array_map('kleeja_clean_string', $value);
  109. }
  110. $value = str_replace(array("\r\n", "\r", "\0"), array("\n", "\n", ''), $value);
  111. //$value = preg_replace('/[\x80-\xFF]/', '?', $value); //allow only ASCII (0-127)
  112. return $value;
  113. }
  114. //unsets all global variables set from a superglobal array
  115. function unregister_globals()
  116. {
  117. $register_globals = @ini_get('register_globals');
  118. if ($register_globals === "" || $register_globals === "0" || strtolower($register_globals) === "off")
  119. {
  120. return;
  121. }
  122. if (isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS']))
  123. {
  124. exit('Kleeja is queen of candies ...');
  125. }
  126. $input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
  127. $no_unset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
  128. foreach ($input as $k => $v)
  129. {
  130. if (!in_array($k, $no_unset) && isset($GLOBALS[$k]))
  131. {
  132. unset($GLOBALS[$k]);
  133. unset($GLOBALS[$k]);//make sure
  134. }
  135. }
  136. unset($input);
  137. }
  138. //time of start and end and wutever
  139. function get_microtime()
  140. {
  141. list($usec, $sec) = explode(' ', microtime()); return ((float)$usec + (float)$sec);
  142. }
  143. //is bot ?
  144. function is_bot($bots = array('googlebot', 'yahoo' ,'msnbot'))
  145. {
  146. if(isset($_SERVER['HTTP_USER_AGENT']))
  147. {
  148. return preg_match('/(' . implode('|', $bots) . ')/i', ($_SERVER['HTTP_USER_AGENT'] ? $_SERVER['HTTP_USER_AGENT'] : @getenv('HTTP_USER_AGENT'))) ? true : false;
  149. }
  150. return false;
  151. }
  152. $IS_BOT = is_bot();
  153. $starttm = get_microtime();
  154. //Kill globals varibles
  155. unregister_globals();
  156. if(!is_bot())
  157. {
  158. @session_name('sid');
  159. @session_start();
  160. }
  161. //try close it
  162. if (@get_magic_quotes_runtime())
  163. {
  164. @set_magic_quotes_runtime(0);
  165. }
  166. if(@get_magic_quotes_gpc())
  167. {
  168. $_GET = stripslashes_our($_GET);
  169. $_POST = stripslashes_our($_POST);
  170. $_COOKIE = stripslashes_our($_COOKIE);
  171. $_REQUEST = stripslashes_our($_REQUEST);//we use this sometime
  172. }
  173. //clean string and remove bad chars
  174. $_GET = kleeja_clean_string($_GET);
  175. $_POST = kleeja_clean_string($_POST);
  176. $_REQUEST = kleeja_clean_string($_REQUEST);
  177. $_COOKIE = kleeja_clean_string($_COOKIE);
  178. //path
  179. if(!defined('PATH'))
  180. {
  181. define('PATH', './');
  182. }
  183. // no config
  184. if (!file_exists(PATH . KLEEJA_CONFIG_FILE))
  185. {
  186. header('Location: ' . PATH . 'install/index.php');
  187. exit;
  188. }
  189. // there is a config
  190. require (PATH . KLEEJA_CONFIG_FILE);
  191. //no enough data
  192. if (!$dbname || !$dbuser)
  193. {
  194. header('Location: ' . PATH . 'install/index.php');
  195. exit;
  196. }
  197. //include files .. & classes ..
  198. //$path = dirname(__file__) . '/';
  199. $root_path = PATH;
  200. $adminpath = isset($adminpath) ? $adminpath : './admin/index.php';
  201. !defined('ADMIN_PATH') ? define('ADMIN_PATH', $adminpath) : null;
  202. $db_type = isset($db_type) ? $db_type : 'mysql';
  203. require (PATH . 'includes/functions_alternative.php');
  204. require (PATH . 'includes/version.php');
  205. switch ($db_type)
  206. {
  207. case 'mysqli':
  208. require (PATH . 'includes/mysqli.php');
  209. break;
  210. default:
  211. require (PATH . 'includes/mysql.php');
  212. }
  213. require (PATH . 'includes/style.php');
  214. require (PATH . 'includes/KljUploader.php');
  215. require (PATH . 'includes/usr.php');
  216. require (PATH . 'includes/pager.php');
  217. require (PATH . 'includes/functions.php');
  218. require (PATH . 'includes/functions_display.php');
  219. //fix intregation problems
  220. if(empty($script_encoding))
  221. {
  222. $script_encoding = 'widnows-1256';
  223. }
  224. // start classes ..
  225. $SQL = new SSQL($dbserver, $dbuser, $dbpass, $dbname);
  226. //no need after now
  227. unset($dbpass);
  228. $tpl = new kleeja_style;
  229. $kljup = new KljUploader;
  230. $usrcp = new usrcp;
  231. //then get caches
  232. require (PATH . 'includes/cache.php');
  233. //check user or guest
  234. $usrcp->kleeja_check_user();
  235. //no tpl caching in dev stage
  236. if(defined('DEV_STAGE'))
  237. {
  238. $tpl->caching = false;
  239. }
  240. //check if admin (true/false)
  241. $is_admin = $usrcp->admin();
  242. //kleeja session id
  243. $klj_session = $SQL->escape(session_id());
  244. // for gzip : php.net
  245. //fix bug # 181
  246. //we stopped this in development stage cuz it's will hide notices
  247. $do_gzip_compress = false;
  248. if ($config['gzip'] == '1' && !defined('IN_DOWNLOAD') && !defined('IN_ADMIN') && !defined('DEV_STAGE') && !defined('IN_SUBMIT_UPLOADING'))
  249. {
  250. function compress_output($output)
  251. {
  252. return gzencode($output, 5, FORCE_GZIP);
  253. }
  254. // Check if the browser supports gzip encoding, HTTP_ACCEPT_ENCODING
  255. if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false && !headers_sent() && @extension_loaded('zlib') && !defined('IN_DOWNLOAD'))
  256. {
  257. $do_gzip_compress = true;
  258. // Start output buffering, and register compress_output()
  259. if(function_exists('gzencode') )
  260. {
  261. @ob_start("compress_output");
  262. }
  263. else
  264. {
  265. @ob_start();
  266. }
  267. // Tell the browser the content is compressed with gzip
  268. header("Content-Encoding: gzip");
  269. }
  270. }
  271. // header .
  272. header('Content-type: text/html; charset=UTF-8');
  273. header('Cache-Control: private, no-cache="set-cookie"');
  274. header('Expires: 0');
  275. header('Pragma: no-cache');
  276. //check lang
  277. if(!$config['language'] || empty($config['language']))
  278. {
  279. if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && strlen($_SERVER['HTTP_ACCEPT_LANGUAGE']) > 2)
  280. {
  281. $config['language'] = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  282. if(!file_exists(PATH . 'lang/' . $config['language'] . '/common.php'))
  283. {
  284. $config['language'] = 'en';
  285. }
  286. }
  287. }
  288. //check style
  289. if(!$config['style'] || empty($config['style']))
  290. {
  291. $config['style'] = 'default';
  292. }
  293. //check h_kay, important for kleeja
  294. if(empty($config['h_key']))
  295. {
  296. $h_k = sha1(microtime() . rand(1000,9999));
  297. if(!update_config('h_key', $h_k))
  298. {
  299. add_config('h_key', $h_k);
  300. }
  301. }
  302. //Global vars for Kleeja
  303. $STYLE_PATH = PATH . 'styles/' . (trim($config['style_depend_on']) == '' ? $config['style'] : $config['style_depend_on']) . '/';
  304. $STYLE_PATH_ADMIN = PATH . 'admin/admin_style/';
  305. $THIS_STYLE_PATH = PATH . 'styles/' . $config['style'] . '/';
  306. //get languge of common
  307. get_lang('common');
  308. //ban system
  309. get_ban();
  310. //check load average
  311. if((function_exists('sys_getloadavg') && $load = sys_getloadavg()) || ($load = explode(' ', @file_get_contents('/proc/loadavg'))))
  312. {
  313. //This feature will not work on Windows !. @see php.net/sys_getloadavg
  314. if ($load[0] > 80 && !defined('IN_ADMIN') && !defined('IN_LOGIN'))
  315. {
  316. if(is_bot())
  317. {
  318. header('HTTP/1.1 503 Too busy, try again later');
  319. }
  320. kleeja_info($lang['LOAD_IS_HIGH_NOW'], $lang['SITE_CLOSED']);
  321. }
  322. }
  323. //install.php exists
  324. if (file_exists(PATH . 'install') && !defined('IN_ADMIN') && !defined('IN_LOGIN') && !defined('DEV_STAGE'))
  325. {
  326. kleeja_info($lang['WE_UPDATING_KLEEJA_NOW'], $lang['SITE_CLOSED']);
  327. }
  328. //site close ..
  329. $login_page = '';
  330. if ($config['siteclose'] == '1' && !$usrcp->admin() && !defined('IN_LOGIN') && !defined('IN_ADMIN'))
  331. {
  332. //if download, images ?
  333. if(defined('IN_DOWNLOAD') && (isset($_GET['img']) || isset($_GET['thmb']) || isset($_GET['thmbf']) || isset($_GET['imgf'])))
  334. {
  335. @$SQL->close();
  336. $fullname = "images/not_exists.jpg";
  337. $filesize = filesize($fullname);
  338. header("Content-length: $filesize");
  339. header("Content-type: image/jpg");
  340. readfile($fullname);
  341. exit;
  342. }
  343. // Send a 503 HTTP response code to prevent search bots from indexing the maintenace message
  344. header('HTTP/1.1 503 Service Temporarily Unavailable');
  345. kleeja_info($config['closemsg'], $lang['SITE_CLOSED']);
  346. }
  347. //exceed total size
  348. if (($stat_sizes >= ($config['total_size'] *(1048576))) && !defined('IN_LOGIN') && !defined('IN_ADMIN'))// convert megabytes to bytes
  349. {
  350. // Send a 503 HTTP response code to prevent search bots from indexing the maintenace message
  351. header('HTTP/1.1 503 Service Temporarily Unavailable');
  352. kleeja_info($lang['SIZES_EXCCEDED'], $lang['STOP_FOR_SIZE']);
  353. }
  354. //calculate onlines ...
  355. if ((int) $config['allow_online'] == '1' && defined('IN_REAL_INDEX'))
  356. {
  357. //it's only work in index page .. so we reduce some loads
  358. KleejaOnline();
  359. }
  360. //check for page numbr
  361. if(empty($perpage) || intval($perpage) == 0)
  362. {
  363. $perpage = 14;
  364. }
  365. //site url must end with /
  366. if($config['siteurl'])
  367. {
  368. $config['siteurl'] = ($config['siteurl'][strlen($config['siteurl'])-1] != '/') ? $config['siteurl'] . '/' : $config['siteurl'];
  369. }
  370. //captch file
  371. $captcha_file_path = $config['siteurl'] . 'includes/captcha.php';
  372. //clean files
  373. if((int) $config['del_f_day'] > 0 && PATH == './')
  374. {
  375. klj_clean_old_files($config['klj_clean_files_from']);
  376. }
  377. ($hook = kleeja_run_hook('end_common')) ? eval($hook) : null; //run hook
  378. #<-- EOF