Files
Kleeja/includes/common.php

408 lines
11 KiB
PHP
Raw Normal View History

2018-01-09 02:09:07 +03:00
<?php
/**
*
* @package Kleeja
* @copyright (c) 2007 Kleeja.com
* @license ./docs/license.txt
*
*/
2019-05-03 23:52:08 +03:00
//not for directly open
if (! defined('IN_KLEEJA'))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
exit();
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//we are in the common file
2018-01-09 02:09:07 +03:00
define('IN_COMMON', true);
2019-05-03 23:52:08 +03:00
//filename of config.php
2018-01-09 02:09:07 +03:00
define('KLEEJA_CONFIG_FILE', 'config.php');
2019-05-03 23:52:08 +03:00
//plugins folder
2018-01-09 02:09:07 +03:00
define('KLEEJA_PLUGINS_FOLDER', 'plugins');
2019-05-03 23:52:08 +03:00
if (@extension_loaded('apc'))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
define('APC_CACHE', true);
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//path
if (! defined('PATH'))
2018-01-09 02:09:07 +03:00
{
2019-05-13 20:43:35 +03:00
define('PATH', str_replace('/includes', '', __DIR__) . '/');
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//no config
if (! file_exists(PATH . KLEEJA_CONFIG_FILE))
2018-01-09 02:09:07 +03:00
{
header('Location: ./install/index.php');
2019-05-03 23:52:08 +03:00
2018-01-09 02:09:07 +03:00
exit;
}
2019-05-03 23:52:08 +03:00
//there is a config
2018-01-09 02:09:07 +03:00
require_once PATH . KLEEJA_CONFIG_FILE;
2019-05-03 23:52:08 +03:00
//admin files path
2018-01-09 02:09:07 +03:00
define('ADM_FILES_PATH', PATH . 'includes/adm');
2019-05-03 23:52:08 +03:00
//Report all errors, except notices
error_reporting(defined('DEV_STAGE') ? E_ALL : E_ALL ^ E_NOTICE);
2018-01-09 02:09:07 +03:00
/**
* functions for start
* @param mixed $error_number
* @param mixed $error_string
* @param mixed $error_file
* @param mixed $error_line
2018-01-09 02:09:07 +03:00
*/
function kleeja_show_error($error_number, $error_string = '', $error_file = '', $error_line = '')
{
2019-05-03 23:52:08 +03:00
switch ($error_number)
{
2019-05-25 18:17:08 +03:00
case E_NOTICE: case E_WARNING: case E_USER_WARNING: case E_USER_NOTICE: case E_STRICT:
if (function_exists('kleeja_log'))
{
2019-05-25 19:59:45 +03:00
$error_name = [
2 => 'Warning', 8 => 'Notice', 512 => 'U_Warning', 1024 => 'U_Notice', 2048 => 'Strict'
][$error_number];
kleeja_log('[' . $error_name . '] ' . basename($error_file) . ':' . $error_line . ' ' . $error_string);
2019-05-25 18:17:08 +03:00
}
2019-05-25 19:59:45 +03:00
2019-05-25 18:17:08 +03:00
break;
2019-05-03 23:52:08 +03:00
default:
header('HTTP/1.1 503 Service Temporarily Unavailable');
echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">' . "\n<head>\n";
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' . "\n";
echo '<title>Kleeja Error</title>' . "\n" . '<style type="text/css">' . "\n\t";
echo '.error {color: #333;background:#ffebe8;float:left;width:73%;text-align:left;margin-top:10px;border: 1px solid #dd3c10; padding: 10px;font-family:tahoma,arial;font-size: 12px;}' . "\n";
echo "</style>\n</head>\n<body>\n\t" . '<div class="error">' . "\n\n\t\t<h2>Kleeja error : </h2><br />" . "\n";
echo "\n\t\t<strong> [ " . $error_number . ':' . basename($error_file) . ':' . $error_line . ' ] </strong><br /><br />' . "\n\t\t" . $error_string . "\n\t";
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";
echo "</div>\n</body>\n</html>";
global $SQL;
if (isset($SQL))
{
@$SQL->close();
}
exit;
break;
2018-01-09 02:09:07 +03:00
}
}
set_error_handler('kleeja_show_error');
2019-05-03 23:52:08 +03:00
//time of start and end and whatever
2018-01-09 02:09:07 +03:00
function get_microtime()
{
2019-05-03 23:52:08 +03:00
list($usec, $sec) = explode(' ', microtime());
return ((float) $usec + (float) $sec);
2018-01-09 02:09:07 +03:00
}
//is bot ?
2019-05-03 23:52:08 +03:00
function is_bot($bots = ['googlebot', 'bing' ,'msnbot'])
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
if (isset($_SERVER['HTTP_USER_AGENT']))
{
return preg_match('/(' . implode('|', $bots) . ')/i', ($_SERVER['HTTP_USER_AGENT'] ? $_SERVER['HTTP_USER_AGENT'] : @getenv('HTTP_USER_AGENT'))) ? true : false;
}
return false;
2018-01-09 02:09:07 +03:00
}
$starttm = get_microtime();
2019-05-03 23:52:08 +03:00
if (! is_bot() && ! isset($_SESSION))
2018-01-09 02:09:07 +03:00
{
session_start();
}
2019-05-03 23:52:08 +03:00
//no enough data
2019-06-29 16:55:10 +02:00
if ((empty($dbname) || empty($dbuser)) && ($dbtype !== 'sqlite'))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
header('Location: ./install/index.php');
exit;
2018-01-09 02:09:07 +03:00
}
// solutions for hosts running under suexec, add define('HAS_SUEXEC', true) to config.php.
define('K_FILE_CHMOD', defined('HAS_SUEXEC') ? (0644 & ~umask()) : 0644);
define('K_DIR_CHMOD', defined('HAS_SUEXEC') ? (0755 & ~umask()) : 0755);
2018-01-09 02:09:07 +03:00
include PATH . 'includes/functions_alternative.php';
include PATH . 'includes/version.php';
2019-05-30 07:32:17 +03:00
if (isset($dbtype) && $dbtype == 'sqlite')
{
include PATH . 'includes/sqlite.php';
}
else
{
include PATH . 'includes/mysqli.php';
}
2018-01-09 02:09:07 +03:00
include PATH . 'includes/style.php';
include PATH . 'includes/usr.php';
include PATH . 'includes/pager.php';
include PATH . 'includes/functions.php';
include PATH . 'includes/functions_display.php';
include PATH . 'includes/plugins.php';
2019-05-19 02:40:43 +03:00
include PATH . 'includes/FetchFile.php';
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if (defined('IN_ADMIN'))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
include PATH . 'includes/functions_adm.php';
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//fix integration problems
if (empty($script_encoding))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
$script_encoding = 'utf-8';
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//start classes ..
2019-05-20 21:56:29 +03:00
$SQL = new KleejaDatabase($dbserver, $dbuser, $dbpass, $dbname, $dbprefix);
2019-05-03 23:52:08 +03:00
//no need after now
2018-01-09 02:09:07 +03:00
unset($dbpass);
2019-05-18 01:47:17 +03:00
$tpl = new kleeja_style;
$usrcp = new usrcp;
2018-01-09 02:09:07 +03:00
//then get caches
include PATH . 'includes/cache.php';
2019-05-03 23:52:08 +03:00
//getting dynamic configs
$query = [
2019-05-18 01:47:17 +03:00
'SELECT' => 'c.name, c.value',
'FROM' => "{$dbprefix}config c",
'WHERE' => 'c.dynamic = 1',
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
$result = $SQL->build($query);
2019-05-03 23:52:08 +03:00
while ($row=$SQL->fetch_array($result))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
$config[$row['name']] = $row['value'];
2018-01-09 02:09:07 +03:00
}
2019-05-23 01:03:12 +03:00
2018-01-09 02:09:07 +03:00
$SQL->freeresult($result);
2019-05-03 23:52:08 +03:00
//check user or guest
2018-01-09 02:09:07 +03:00
$usrcp->kleeja_check_user();
2019-05-03 23:52:08 +03:00
//+ configs of the current group
2018-01-09 02:09:07 +03:00
$config = array_merge($config, (array) $d_groups[$usrcp->group_id()]['configs']);
2019-05-03 23:52:08 +03:00
//admin path
2018-01-09 02:09:07 +03:00
define('ADMIN_PATH', rtrim($config['siteurl'], '/') . '/admin/index.php');
//no tpl caching in dev stage
2019-05-15 00:48:58 +03:00
if (defined('DEV_STAGE') || defined('STOP_TPL_CACHE'))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
$tpl->caching = false;
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
if (isset($config['foldername']))
2018-01-09 02:09:07 +03:00
{
$config['foldername'] = str_replace(
2019-05-03 23:52:08 +03:00
[
2018-01-09 02:09:07 +03:00
'{year}',
'{month}',
'{week}',
'{day}',
'{username}',
2019-05-03 23:52:08 +03:00
],
[
2018-01-09 02:09:07 +03:00
date('Y'),
date('m'),
date('W'),
date('d'),
$usrcp->name() ? preg_replace('/[^a-z0-9\._-]/', '', strtolower($usrcp->name())) : 'guest'
2019-05-03 23:52:08 +03:00
],
2018-01-09 02:09:07 +03:00
$config['foldername']
);
}
is_array($plugin_run_result = Plugins::getInstance()->run('boot_common', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
/**
* Set default time zone
* There is no time difference between Coordinated Universal Time (UTC) and Greenwich Mean Time (GMT).
* Kleeja supports the changing of time zone through the admin panel, see functions_display.php/kleeja_date()
*/
date_default_timezone_set('GMT');
2019-06-02 05:46:15 +03:00
//remove PHP version header
header_remove('X-Powered-By');
2018-01-09 02:09:07 +03:00
//kleeja session id
2019-05-23 01:03:12 +03:00
define('KJ_SESSION', preg_replace('/[^-,a-zA-Z0-9]/', '', session_id()));
2018-01-09 02:09:07 +03:00
//site url must end with /
2019-05-23 01:03:12 +03:00
$config['siteurl'] = rtrim($config['siteurl'], '/') . '/';
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
//check lang
if (! $config['language'] || empty($config['language']))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && strlen($_SERVER['HTTP_ACCEPT_LANGUAGE']) > 2)
{
$config['language'] = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if (! file_exists(PATH . 'lang/' . $config['language'] . '/common.php'))
{
$config['language'] = 'en';
}
}
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//check style
if (is_null($config['style']) || empty($config['style']))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
$config['style'] = 'default';
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//check h_kay, important for kleeja
if (empty($config['h_key']))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
$h_k = sha1(microtime() . rand(0, 100));
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if (! update_config('h_key', $h_k))
{
add_config('h_key', $h_k);
}
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//current Kleeja admin style
2018-01-09 02:09:07 +03:00
define('ACP_STYLE_NAME', 'Masmak');
2019-05-03 23:52:08 +03:00
//path variables for Kleeja
2019-05-18 01:47:17 +03:00
$STYLE_PATH = $config['siteurl'] . 'styles/' . (trim($config['style_depend_on']) == '' ? $config['style'] : $config['style_depend_on']) . '/';
$THIS_STYLE_PATH = $config['siteurl'] . 'styles/' . $config['style'] . '/';
$THIS_STYLE_PATH_ABS = PATH . 'styles/' . $config['style'] . '/';
$STYLE_PATH_ADMIN = $config['siteurl'] . 'admin/' . (is_browser('mobile') || defined('IN_MOBILE') ? ACP_STYLE_NAME : ACP_STYLE_NAME) . '/';
$STYLE_PATH_ADMIN_ABS = PATH . 'admin/' . (is_browser('mobile') || defined('IN_MOBILE') ? ACP_STYLE_NAME . '/' : ACP_STYLE_NAME . '/');
$DEFAULT_PATH_ADMIN_ABS = PATH . 'admin/' . ACP_STYLE_NAME . '/';
$DEFAULT_PATH_ADMIN = $config['siteurl'] . 'admin/' . ACP_STYLE_NAME . '/';
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
//get languge of common
2018-01-09 02:09:07 +03:00
get_lang('common');
2019-05-03 23:52:08 +03:00
//run ban system
2018-01-09 02:09:07 +03:00
get_ban();
2019-05-03 23:52:08 +03:00
if (isset($_GET['go']) && $_GET['go'] == 'login')
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
define('IN_LOGIN', true);
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//install.php exists
2018-12-31 18:19:37 +03:00
if (
2019-05-03 23:52:08 +03:00
file_exists(PATH . 'install') &&
! defined('IN_ADMIN') &&
! defined('IN_LOGIN') &&
! defined('DEV_STAGE') &&
2019-05-03 23:52:08 +03:00
! (defined('IN_GO') && in_array(g('go'), ['queue'])) &&
! (defined('IN_UCP') && in_array(g('go'), ['captcha', 'login']))
) {
2019-05-03 23:52:08 +03:00
//Different message for admins! delete install folder
kleeja_info((user_can('enter_acp') ? $lang['DELETE_INSTALL_FOLDER'] : $lang['WE_UPDATING_KLEEJA_NOW']), $lang['SITE_CLOSED']);
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//is site close
2018-01-09 02:09:07 +03:00
$login_page = '';
2019-05-03 23:52:08 +03:00
2018-01-15 22:50:35 +03:00
if (
$config['siteclose'] == '1' &&
! user_can('enter_acp') &&
! defined('IN_LOGIN') &&
! defined('IN_ADMIN') &&
2019-05-03 23:52:08 +03:00
! (defined('IN_GO') && in_array(g('go'), ['queue'])) &&
! (defined('IN_UCP') && in_array(g('go'), ['captcha', 'login', 'register', 'logout']))
) {
//if download, images ?
if (
(defined('IN_DOWNLOAD') && (ig('img') || ig('thmb') || ig('thmbf') || ig('imgf')))
2018-01-09 02:09:07 +03:00
|| g('go', 'str', '') == 'queue'
2019-05-03 23:52:08 +03:00
) {
@$SQL->close();
$fullname = 'images/site_closed.jpg';
$filesize = filesize($fullname);
header("Content-length: $filesize");
header('Content-type: image/jpg');
readfile($fullname);
exit;
}
// Send a 503 HTTP response code to prevent search bots from indexing the maintenace message
header('HTTP/1.1 503 Service Temporarily Unavailable');
kleeja_info($config['closemsg'], $lang['SITE_CLOSED']);
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//exceed total size
if (($stat_sizes >= ($config['total_size'] *(1048576))) && ! defined('IN_LOGIN') && ! defined('IN_ADMIN'))
{
// convert megabytes to bytes
2019-05-03 23:52:08 +03:00
// Send a 503 HTTP response code to prevent search bots from indexing the maintenace message
header('HTTP/1.1 503 Service Temporarily Unavailable');
kleeja_info($lang['SIZES_EXCCEDED'], $lang['STOP_FOR_SIZE']);
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//detect bots and save stats
2018-01-09 02:09:07 +03:00
kleeja_detecting_bots();
2019-05-03 23:52:08 +03:00
//check for page number
if (empty($perpage) || intval($perpage) == 0)
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
$perpage = 14;
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
//captcha file
2018-01-09 02:09:07 +03:00
$captcha_file_path = $config['siteurl'] . 'ucp.php?go=captcha';
2019-05-03 23:52:08 +03:00
if (defined('STOP_CAPTCHA'))
2019-01-18 23:42:23 +03:00
{
2019-05-03 23:52:08 +03:00
$config['enable_captcha'] = 0;
2019-01-18 23:42:23 +03:00
}
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('end_common', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-30 07:32:17 +03:00
register_shutdown_function(function() {
session_write_close();
$err = error_get_last();
2019-06-29 16:55:10 +02:00
if (is_array($err) && ! empty($err['type']) && in_array($err['type'], [E_ERROR, E_PARSE]))
2019-05-30 07:32:17 +03:00
{
kleeja_log('[FATAL] ' . basename($err['file']) . ':' . $err['line'] . ' ' . $err['message']);
}
});