Merge branch 'tp_autoload' into develop

This commit is contained in:
PheRum 2015-05-12 20:11:57 +03:00
commit acb09f58e2
2579 changed files with 746 additions and 477 deletions

View file

@ -17,18 +17,11 @@ header('X-Frame-Options: SAMEORIGIN');
// Get initial config // Get initial config
require(BB_ROOT . 'library/config.php'); require(BB_ROOT . 'library/config.php');
require(TP_DIR . 'Autoloader.php');
// Load Zend Framework TorrentPier_Autoloader::getInstance()->setupAutoloader(TP_DIR);
use Zend\Loader\StandardAutoloader;
require(BB_ROOT . 'library/Zend/Loader/StandardAutoloader.php');
$loader = new StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
// ZF global use
use Zend\Json;
$server_protocol = ($bb_cfg['cookie_secure']) ? 'https://' : 'http://'; $server_protocol = ($bb_cfg['cookie_secure']) ? 'https://' : 'http://';
$server_port = (in_array($bb_cfg['server_port'], array(80, 443))) ? '' : ':' . $bb_cfg['server_port']; $server_port = (in_array($bb_cfg['server_port'], [80, 443])) ? '' : ':' . $bb_cfg['server_port'];
define('FORUM_PATH', $bb_cfg['script_path']); define('FORUM_PATH', $bb_cfg['script_path']);
define('FULL_URL', $server_protocol . $bb_cfg['server_name'] . $server_port . $bb_cfg['script_path']); define('FULL_URL', $server_protocol . $bb_cfg['server_name'] . $server_port . $bb_cfg['script_path']);
unset($server_protocol, $server_port); unset($server_protocol, $server_port);
@ -68,24 +61,30 @@ define('BOT_UID', -746);
require(CORE_DIR . 'dbs.php'); require(CORE_DIR . 'dbs.php');
$DBS = new DBS($bb_cfg); $DBS = new DBS($bb_cfg);
/**
* @param string $db_alias
*
* @return sql_db()
*/
function DB ($db_alias = 'db1') function DB ($db_alias = 'db1')
{ {
global $DBS; global $DBS;
return $DBS->get_db_obj($db_alias); return $DBS->get_db_obj($db_alias);
} }
/** // cache
* Cache
*/
// Main cache class
require(INC_DIR . 'cache/common.php'); require(INC_DIR . 'cache/common.php');
// Main datastore class
require(INC_DIR . 'datastore/common.php'); require(INC_DIR . 'datastore/common.php');
// Core CACHE class // Core CACHE class
require(CORE_DIR . 'caches.php'); require(CORE_DIR . 'caches.php');
$CACHES = new CACHES($bb_cfg); $CACHES = new CACHES($bb_cfg);
/**
* @param $cache_name
*
* @return cache_common
*/
function CACHE ($cache_name) function CACHE ($cache_name)
{ {
global $CACHES; global $CACHES;
@ -119,11 +118,11 @@ switch ($bb_cfg['datastore_type'])
break; break;
case 'sqlite': case 'sqlite':
$default_cfg = array( $default_cfg = [
'db_file_path' => $bb_cfg['cache']['db_dir'] .'datastore.sqlite.db', 'db_file_path' => $bb_cfg['cache']['db_dir'] .'datastore.sqlite.db',
'pconnect' => true, 'pconnect' => true,
'con_required' => true, 'con_required' => true,
); ];
$datastore = new datastore_sqlite($default_cfg, $bb_cfg['cache']['prefix']); $datastore = new datastore_sqlite($default_cfg, $bb_cfg['cache']['prefix']);
break; break;
@ -250,7 +249,7 @@ function verify_id ($id, $length)
function clean_filename ($fname) function clean_filename ($fname)
{ {
static $s = array('\\', '/', ':', '*', '?', '"', '<', '>', '|', ' '); static $s = ['\\', '/', ':', '*', '?', '"', '<', '>', '|', ' '];
return str_replace($s, '_', str_compact($fname)); return str_replace($s, '_', str_compact($fname));
} }
@ -453,23 +452,35 @@ function log_request ($file = '', $prepend_str = false, $add_post = true)
global $user; global $user;
$file = ($file) ? $file : 'req/' . date('m-d'); $file = ($file) ? $file : 'req/' . date('m-d');
$str = array(); $str = [];
$str[] = date('m-d H:i:s'); $str[] = date('m-d H:i:s');
if ($prepend_str !== false) $str[] = $prepend_str; if ($prepend_str !== false)
if (!empty($user->data)) $str[] = $user->id ."\t". html_entity_decode($user->name); {
$str[] = $prepend_str;
}
if (!empty($user->data))
{
$str[] = $user->id . "\t" . html_entity_decode($user->name);
}
$str[] = sprintf('%-15s', $_SERVER['REMOTE_ADDR']); $str[] = sprintf('%-15s', $_SERVER['REMOTE_ADDR']);
if (isset($_SERVER['REQUEST_URI'])) { if (isset($_SERVER['REQUEST_URI']))
{
$str[] = $_SERVER['REQUEST_URI']; $str[] = $_SERVER['REQUEST_URI'];
} }
if (isset($_SERVER['HTTP_USER_AGENT'])) { if (isset($_SERVER['HTTP_USER_AGENT']))
{
$str[] = $_SERVER['HTTP_USER_AGENT']; $str[] = $_SERVER['HTTP_USER_AGENT'];
} }
if (isset($_SERVER['HTTP_REFERER'])) { if (isset($_SERVER['HTTP_REFERER']))
{
$str[] = $_SERVER['HTTP_REFERER']; $str[] = $_SERVER['HTTP_REFERER'];
} }
if (!empty($_POST) && $add_post) $str[] = "post: ". str_compact(urldecode(http_build_query($_POST))); if (!empty($_POST) && $add_post)
{
$str[] = "post: " . str_compact(urldecode(http_build_query($_POST)));
}
$str = join("\t", $str) . "\n"; $str = join("\t", $str) . "\n";
bb_log($str, $file); bb_log($str, $file);
} }
@ -486,11 +497,11 @@ else if (defined('IN_TRACKER'))
function dummy_exit ($interval = 1800) function dummy_exit ($interval = 1800)
{ {
$output = bencode(array( $output = bencode([
'interval' => (int) $interval, 'interval' => (int) $interval,
'min interval' => (int) $interval, 'min interval' => (int) $interval,
'peers' => (string) DUMMY_PEER, 'peers' => (string) DUMMY_PEER,
)); ]);
die($output); die($output);
} }

View file

@ -0,0 +1,164 @@
<?php
/**
* Class TorrentPier_Autoloader
*/
class TorrentPier_Autoloader
{
/**
* Instance manager.
*
* @var TorrentPier_Autoloader
*/
protected static $_instance;
/**
* Path to directory containing the application's library.
*
* @var string
*/
protected $_rootDir = '.';
/**
* Stores whether the autoloader has been setup yet.
*
* @var boolean
*/
protected $_setup = false;
/**
* Protected constructor. Use {@link getInstance()} instead.
*/
protected function __construct()
{
}
/**
* Setup the autoloader. This causes the environment to be setup as necessary.
*
* @param string Path to application library directory. See {@link $_rootDir}
*/
public function setupAutoloader($rootDir)
{
if ($this->_setup)
{
return;
}
$this->_rootDir = $rootDir;
$this->_setupAutoloader();
$this->_setup = true;
}
/**
* Internal method that actually applies the autoloader. See {@link setupAutoloader()}
* for external usage.
*/
protected function _setupAutoloader()
{
if (@ini_get('open_basedir'))
{
// many servers don't seem to set include_path correctly with open_basedir, so don't use it
set_include_path($this->_rootDir . PATH_SEPARATOR . '.');
}
else
{
set_include_path($this->_rootDir . PATH_SEPARATOR . '.' . PATH_SEPARATOR . get_include_path());
}
spl_autoload_register([$this, 'autoload']);
}
/**
* Autoload the specified class.
*
* @param $class
*
* @return bool
* @throws \Exception
*/
public function autoload($class)
{
if (class_exists($class, false) || interface_exists($class, false))
{
return true;
}
if ($class == 'utf8_entity_decoder')
{
return true;
}
if (substr($class, 0, 5) == 'XFCP_')
{
throw new Exception('Cannot load class using XFCP. Load the class using the correct loader first.');
}
$filename = $this->autoloaderClassToFile($class);
if (!$filename)
{
return false;
}
if (file_exists($filename))
{
include($filename);
return (class_exists($class, false) || interface_exists($class, false));
}
return false;
}
/**
* Resolves a class name to an autoload path.
*
* @param string Name of class to autoload
*
* @return string|false False if the class contains invalid characters.
*/
public function autoloaderClassToFile($class)
{
if (preg_match('#[^a-zA-Z0-9_\\\\]#', $class))
{
return false;
}
return $this->_rootDir . '/' . str_replace(['_', '\\'], '/', $class) . '.php';
}
/**
* Gets the autoloader's root directory.
*
* @return string
*/
public function getRootDir()
{
return $this->_rootDir;
}
/**
* Gets the autoloader instance.
*
* @return TorrentPier_Autoloader
*/
public static final function getInstance()
{
if (!self::$_instance)
{
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Manually sets the autoloader instance. Use this to inject a modified version.
*
* @param TorrentPier_Autoloader|null
*/
public static function setInstance(TorrentPier_Autoloader $loader = null)
{
self::$_instance = $loader;
}
}

View file

@ -1,24 +1,21 @@
<?php <?php
if (!defined('BB_ROOT')) die(basename(__FILE__)); class Sessions
define('ONLY_NEW_POSTS', 1);
define('ONLY_NEW_TOPICS', 2);
class user_common
{ {
/** /**
* Config * @type array
*/ */
var $cfg = array( public $cfg = [
'req_login' => false, // requires user to be logged in 'req_login' => false, // requires user to be logged in
'req_session_admin' => false, // requires active admin session (for moderation or admin actions) 'req_session_admin' => false, // requires active admin session (for moderation or admin actions)
); ];
/** /**
* PHP-JS exchangeable options (JSON'ized as {USER_OPTIONS_JS} in TPL) * PHP-JS exchangeable options (JSON'ized as {USER_OPTIONS_JS} in TPL)
*
* @type array
*/ */
var $opt_js = array( public $opt_js = [
'only_new' => 0, // show ony new posts or topics 'only_new' => 0, // show ony new posts or topics
'h_av' => 0, // hide avatar 'h_av' => 0, // hide avatar
'h_rnk_i' => 0, // hide rank images 'h_rnk_i' => 0, // hide rank images
@ -31,49 +28,63 @@ class user_common
'hl_tr' => 1, // show cursor in tracker.php 'hl_tr' => 1, // show cursor in tracker.php
'i_aft_l' => 0, // show images only after full loading 'i_aft_l' => 0, // show images only after full loading
'h_tsp' => 0, // show released title {...} 'h_tsp' => 0, // show released title {...}
); ];
/** /**
* Defaults options for guests * Defaults options for guests
*
* @type array
*/ */
var $opt_js_guest = array( public $opt_js_guest = [
'h_av' => 1, // hide avatar 'h_av' => 1, // hide avatar
'h_rnk_i' => 1, // hide rank images 'h_rnk_i' => 1, // hide rank images
'h_smile' => 1, // hide smilies 'h_smile' => 1, // hide smilies
'h_sig' => 1, // hide signatures 'h_sig' => 1, // hide signatures
); ];
/** /**
* Sessiondata * Sessiondata
*
* @type array
*/ */
var $sessiondata = array( public $sessiondata = [
'uk' => null, 'uk' => null,
'uid' => null, 'uid' => null,
'sid' => '', 'sid' => '',
); ];
/** /**
* Old $userdata * Old $userdata
*
* @type array
*/ */
var $data = array(); public $data = [];
/** /**
* Shortcuts * Shortcuts
*
* @type
*/ */
var $id = null; public $id;
public $active;
public $name;
public $lastvisit;
public $regdate;
public $level;
public $opt;
/**
* Constructor function __construct ()
*/
function user_common ()
{ {
$this->get_sessiondata(); $this->get_sessiondata();
} }
/** /**
* Start session (restore existent session or create new) * @param array $cfg
*
* @return array|bool
*/ */
function session_start ($cfg = array()) public function session_start ($cfg = [])
{ {
global $bb_cfg; global $bb_cfg;
@ -157,7 +168,7 @@ class user_common
} }
else else
{ {
$this->data = array(); $this->data = [];
} }
} }
// If we reach here then no (valid) session exists. So we'll create a new one, // If we reach here then no (valid) session exists. So we'll create a new one,
@ -207,9 +218,12 @@ class user_common
} }
/** /**
* Create new session for the given user * @param $userdata
* @param bool $auto_created
*
* @return array
*/ */
function session_create ($userdata, $auto_created = false) public function session_create ($userdata, $auto_created = false)
{ {
global $bb_cfg; global $bb_cfg;
@ -242,7 +256,7 @@ class user_common
{ {
$session_id = make_rand_str(SID_LENGTH); $session_id = make_rand_str(SID_LENGTH);
$args = DB()->build_array('INSERT', array( $args = DB()->build_array('INSERT', [
'session_id' => (string) $session_id, 'session_id' => (string) $session_id,
'session_user_id' => (int) $user_id, 'session_user_id' => (int) $user_id,
'session_start' => (int) TIMENOW, 'session_start' => (int) TIMENOW,
@ -250,7 +264,7 @@ class user_common
'session_ip' => (string) USER_IP, 'session_ip' => (string) USER_IP,
'session_logged_in' => (int) $login, 'session_logged_in' => (int) $login,
'session_admin' => (int) $mod_admin_session, 'session_admin' => (int) $mod_admin_session,
)); ]);
$sql = "INSERT INTO " . BB_SESSIONS . $args; $sql = "INSERT INTO " . BB_SESSIONS . $args;
if (@DB()->query($sql)) if (@DB()->query($sql))
@ -326,9 +340,10 @@ class user_common
} }
/** /**
* Initialize sessiondata stored in cookies * @param bool $update_lastvisit
* @param bool $set_cookie
*/ */
function session_end ($update_lastvisit = false, $set_cookie = true) public function session_end ($update_lastvisit = false, $set_cookie = true)
{ {
DB()->query(" DB()->query("
DELETE FROM " . BB_SESSIONS . " DELETE FROM " . BB_SESSIONS . "
@ -368,9 +383,12 @@ class user_common
} }
/** /**
* Login * @param $args
* @param bool $mod_admin_login
*
* @return array
*/ */
function login ($args, $mod_admin_login = false) public function login ($args, $mod_admin_login = false)
{ {
$username = !empty($args['login_username']) ? clean_username($args['login_username']) : ''; $username = !empty($args['login_username']) ? clean_username($args['login_username']) : '';
$password = !empty($args['login_password']) ? $args['login_password'] : ''; $password = !empty($args['login_password']) ? $args['login_password'] : '';
@ -429,15 +447,12 @@ class user_common
} }
} }
return array(); return [];
} }
/** public function get_sessiondata ()
* Initialize sessiondata stored in cookies
*/
function get_sessiondata ()
{ {
$sd_resv = !empty($_COOKIE[COOKIE_DATA]) ? @unserialize($_COOKIE[COOKIE_DATA]) : array(); $sd_resv = !empty($_COOKIE[COOKIE_DATA]) ? @unserialize($_COOKIE[COOKIE_DATA]) : [];
// autologin_id // autologin_id
if (!empty($sd_resv['uk']) && verify_id($sd_resv['uk'], LOGIN_KEY_LENGTH)) if (!empty($sd_resv['uk']) && verify_id($sd_resv['uk'], LOGIN_KEY_LENGTH))
@ -457,22 +472,22 @@ class user_common
} }
/** /**
* Store sessiondata in cookies * @param $user_id
*/ */
function set_session_cookies ($user_id) public function set_session_cookies ($user_id)
{ {
global $bb_cfg; global $bb_cfg;
if ($user_id == GUEST_UID) if ($user_id == GUEST_UID)
{ {
$delete_cookies = array( $delete_cookies = [
COOKIE_DATA, COOKIE_DATA,
COOKIE_DBG, COOKIE_DBG,
'torhelp', 'torhelp',
'explain', 'explain',
'sql_log', 'sql_log',
'sql_log_full', 'sql_log_full',
); ];
foreach ($delete_cookies as $cookie) foreach ($delete_cookies as $cookie)
{ {
@ -499,9 +514,13 @@ class user_common
} }
/** /**
* Verify autologin_id * @param $userdata
* @param bool $expire_check
* @param bool $create_new
*
* @return bool|string
*/ */
function verify_autologin_id ($userdata, $expire_check = false, $create_new = true) public function verify_autologin_id ($userdata, $expire_check = false, $create_new = true)
{ {
global $bb_cfg; global $bb_cfg;
@ -526,9 +545,12 @@ class user_common
} }
/** /**
* Create autologin_id * @param $userdata
* @param bool $create_new
*
* @return string
*/ */
function create_autologin_id ($userdata, $create_new = true) public function create_autologin_id ($userdata, $create_new = true)
{ {
$autologin_id = ($create_new) ? make_rand_str(LOGIN_KEY_LENGTH) : ''; $autologin_id = ($create_new) ? make_rand_str(LOGIN_KEY_LENGTH) : '';
@ -542,10 +564,8 @@ class user_common
return $autologin_id; return $autologin_id;
} }
/**
* Set shortcuts public function set_shortcuts ()
*/
function set_shortcuts ()
{ {
$this->id =& $this->data['user_id']; $this->id =& $this->data['user_id'];
$this->active =& $this->data['user_active']; $this->active =& $this->data['user_active'];
@ -558,14 +578,15 @@ class user_common
$this->ip = CLIENT_IP; $this->ip = CLIENT_IP;
} }
/**
* Initialise user settings public function init_userprefs ()
*/
function init_userprefs ()
{ {
global $bb_cfg, $theme, $lang, $DeltaTime; global $bb_cfg, $theme, $lang, $DeltaTime;
if (defined('LANG_DIR')) return; // prevent multiple calling if (defined('LANG_DIR'))
{
return;
} // prevent multiple calling
define('DEFAULT_LANG_DIR', LANG_ROOT_DIR . $bb_cfg['default_lang'] . '/'); define('DEFAULT_LANG_DIR', LANG_ROOT_DIR . $bb_cfg['default_lang'] . '/');
define('ENGLISH_LANG_DIR', LANG_ROOT_DIR . 'en/'); define('ENGLISH_LANG_DIR', LANG_ROOT_DIR . 'en/');
@ -587,7 +608,10 @@ class user_common
$this->data['user_lang'] = $bb_cfg['default_lang']; $this->data['user_lang'] = $bb_cfg['default_lang'];
$this->data['user_timezone'] = $bb_cfg['board_timezone']; $this->data['user_timezone'] = $bb_cfg['board_timezone'];
if (!defined('LANG_DIR')) define('LANG_DIR', DEFAULT_LANG_DIR); if (!defined('LANG_DIR'))
{
define('LANG_DIR', DEFAULT_LANG_DIR);
}
require(LANG_DIR . 'main.php'); require(LANG_DIR . 'main.php');
setlocale(LC_ALL, $bb_cfg['lang'][$this->data['user_lang']]['locale']); setlocale(LC_ALL, $bb_cfg['lang'][$this->data['user_lang']]['locale']);
@ -605,9 +629,9 @@ class user_common
} }
/** /**
* Mark read * @param $type
*/ */
function mark_read ($type) public function mark_read ($type)
{ {
if ($type === 'all_forums') if ($type === 'all_forums')
{ {
@ -624,10 +648,10 @@ class user_common
$this->data['user_lastvisit'] = TIMENOW; $this->data['user_lastvisit'] = TIMENOW;
// Update lastvisit // Update lastvisit
db_update_userdata($this->data, array( db_update_userdata($this->data, [
'user_session_time' => $this->data['session_time'], 'user_session_time' => $this->data['session_time'],
'user_lastvisit' => $this->data['user_lastvisit'], 'user_lastvisit' => $this->data['user_lastvisit'],
)); ]);
// Delete cookies // Delete cookies
bb_setcookie(COOKIE_TOPIC, ''); bb_setcookie(COOKIE_TOPIC, '');
@ -636,10 +660,7 @@ class user_common
} }
} }
/** public function load_opt_js ()
* Load misc options
*/
function load_opt_js ()
{ {
if (IS_GUEST) if (IS_GUEST)
{ {
@ -657,13 +678,18 @@ class user_common
} }
/** /**
* Get not auth forums * @param $auth_type
*
* @return string
*/ */
function get_not_auth_forums ($auth_type) public function get_not_auth_forums ($auth_type)
{ {
global $datastore; global $datastore;
if (IS_ADMIN) return ''; if (IS_ADMIN)
{
return '';
}
if (!$forums = $datastore->get('cat_forums')) if (!$forums = $datastore->get('cat_forums'))
{ {
@ -686,7 +712,7 @@ class user_common
} }
} }
$auth_field_match = array( $auth_field_match = [
AUTH_VIEW => 'auth_view', AUTH_VIEW => 'auth_view',
AUTH_READ => 'auth_read', AUTH_READ => 'auth_read',
AUTH_POST => 'auth_post', AUTH_POST => 'auth_post',
@ -699,9 +725,9 @@ class user_common
AUTH_POLLCREATE => 'auth_pollcreate', AUTH_POLLCREATE => 'auth_pollcreate',
AUTH_ATTACH => 'auth_attachments', AUTH_ATTACH => 'auth_attachments',
AUTH_DOWNLOAD => 'auth_download', AUTH_DOWNLOAD => 'auth_download',
); ];
$not_auth_forums = array(); $not_auth_forums = [];
$auth_field = $auth_field_match[$auth_type]; $auth_field = $auth_field_match[$auth_type];
$is_auth_ary = auth($auth_type, AUTH_LIST_ALL, $this->data); $is_auth_ary = auth($auth_type, AUTH_LIST_ALL, $this->data);
@ -717,11 +743,14 @@ class user_common
} }
/** /**
* Get excluded forums * @param $auth_type
* @param string $return_as
*
* @return array|string
*/ */
function get_excluded_forums ($auth_type, $return_as = 'csv') public function get_excluded_forums ($auth_type, $return_as = 'csv')
{ {
$excluded = array(); $excluded = [];
if ($not_auth = $this->get_not_auth_forums($auth_type)) if ($not_auth = $this->get_not_auth_forums($auth_type))
{ {
@ -742,91 +771,22 @@ class user_common
{ {
foreach ($forums['forum'] as $key => $row) foreach ($forums['forum'] as $key => $row)
{ {
if ($row['allow_porno_topic']) $excluded[] = $row['forum_id']; if ($row['allow_porno_topic'])
{
$excluded[] = $row['forum_id'];
}
} }
} }
} }
switch ($return_as) switch ($return_as)
{ {
case 'csv': return join(',', $excluded); case 'csv':
case 'array': return $excluded; return join(',', $excluded);
case 'flip': return array_flip(explode(',', $excluded)); case 'array':
return $excluded;
case 'flip':
return array_flip(explode(',', $excluded));
} }
} }
} }
//
// userdata cache
//
function ignore_cached_userdata ()
{
return (defined('IN_PM')) ? true : false;
}
function cache_get_userdata ($id)
{
if (ignore_cached_userdata()) return false;
return CACHE('session_cache')->get($id);
}
function cache_set_userdata ($userdata, $force = false)
{
global $bb_cfg;
if (!$userdata || (ignore_cached_userdata() && !$force)) return false;
$id = ($userdata['user_id'] == GUEST_UID) ? $userdata['session_ip'] : $userdata['session_id'];
return CACHE('session_cache')->set($id, $userdata, $bb_cfg['session_update_intrv']);
}
function cache_rm_userdata ($userdata)
{
if (!$userdata) return false;
$id = ($userdata['user_id'] == GUEST_UID) ? $userdata['session_ip'] : $userdata['session_id'];
return CACHE('session_cache')->rm($id);
}
// $user_id - array(id1,id2,..) or (string) id
function cache_rm_user_sessions ($user_id)
{
$user_id = get_id_csv($user_id);
$rowset = DB()->fetch_rowset("
SELECT session_id FROM ". BB_SESSIONS ." WHERE session_user_id IN($user_id)
");
foreach ($rowset as $row)
{
CACHE('session_cache')->rm($row['session_id']);
}
}
function cache_update_userdata ($userdata)
{
return cache_set_userdata($userdata, true);
}
function db_update_userdata ($userdata, $sql_ary, $data_already_escaped = true)
{
if (!$userdata) return false;
$sql_args = DB()->build_array('UPDATE', $sql_ary, $data_already_escaped);
DB()->query("UPDATE ". BB_USERS ." SET $sql_args WHERE user_id = {$userdata['user_id']}");
if (DB()->affected_rows())
{
cache_rm_userdata($userdata);
}
}
// $user_id - array(id1,id2,..) or (string) id
function delete_user_sessions ($user_id)
{
cache_rm_user_sessions($user_id);
$user_id = get_id_csv($user_id);
DB()->query("DELETE FROM ". BB_SESSIONS ." WHERE session_user_id IN($user_id)");
}

View file

@ -1,28 +1,5 @@
<?php <?php
/**
* Reserved prefixes:
*
* "L_" - lang var, {L_VAR} is eq to $lang['VAR']
* "$" - php var, {$VAR} is eq to $VAR (in $this->execute() scope!)
* "#" - constant, {#CON} is eq to CON
*
*/
if (!defined('BB_ROOT')) die(basename(__FILE__));
// Template system constants
define('XS_TPL_PREFIX', 'tpl_');
define('XS_USE_ISSET', '1');
define('XS_TAG_NONE', 0);
define('XS_TAG_BEGIN', 2);
define('XS_TAG_END', 3);
define('XS_TAG_INCLUDE', 4);
define('XS_TAG_IF', 5);
define('XS_TAG_ELSE', 6);
define('XS_TAG_ELSEIF', 7);
define('XS_TAG_ENDIF', 8);
define('XS_TAG_BEGINELSE', 11);
class Template class Template
{ {
@ -89,7 +66,7 @@ class Template
/** /**
* Constructor. Installs XS mod on first run or updates it and sets the root dir. * Constructor. Installs XS mod on first run or updates it and sets the root dir.
*/ */
function Template($root = '.') function __construct ($root = '.')
{ {
global $bb_cfg, $lang; global $bb_cfg, $lang;
@ -963,7 +940,7 @@ class Template
* Compiles the given string of code, and returns the result in a string. * Compiles the given string of code, and returns the result in a string.
* If "do_not_echo" is true, the returned code will not be directly executable, * If "do_not_echo" is true, the returned code will not be directly executable,
* but can be used as part of a variable assignment for use in assign_code_from_handle(). * but can be used as part of a variable assignment for use in assign_code_from_handle().
* This function isn't used and kept only for compatibility with original template.php * This function isn't used and kept only for compatibility with original Template.php
*/ */
function compile($code, $do_not_echo = false, $retvar = '') function compile($code, $do_not_echo = false, $retvar = '')
{ {

View file

@ -1,40 +1,87 @@
<?php <?php
if (!defined('BB_ROOT')) die(basename(__FILE__));
class upload_common class Upload
{ {
var $cfg = array( /**
* @type array
*/
public $cfg = [
'max_size' => 0, 'max_size' => 0,
'max_width' => 0, 'max_width' => 0,
'max_height' => 0, 'max_height' => 0,
'allowed_ext' => array(), 'allowed_ext' => [],
'upload_path' => '', 'upload_path' => '',
); ];
var $file = array(
/**
* @type array
*/
public $file = [
'name' => '', 'name' => '',
'type' => '', 'type' => '',
'size' => 0, 'size' => 0,
'tmp_name' => '', 'tmp_name' => '',
'error' => UPLOAD_ERR_NO_FILE, 'error' => UPLOAD_ERR_NO_FILE,
); ];
var $orig_name = '';
var $file_path = ''; // Stored file path /**
var $file_ext = ''; * @type string
var $file_ext_id = ''; */
var $file_size = ''; public $orig_name = '';
var $ext_ids = array(); // array_flip($bb_cfg['file_id_ext'])
var $errors = array(); /**
var $img_types = array( * @type string
*/
public $file_path = '';
/**
* @type string
*/
public $file_ext = '';
/**
* @type string
*/
public $file_ext_id = '';
/**
* @type string
*/
public $file_size = '';
/**
* array_flip($bb_cfg['file_id_ext'])
*
* @type array
*/
public $ext_ids = [];
/**
* @type array
*/
public $errors = [];
/**
* @type array
*/
public $img_types = [
1 => 'gif', 1 => 'gif',
2 => 'jpg', 2 => 'jpg',
3 => 'png', 3 => 'png',
6 => 'bmp', 6 => 'bmp',
7 => 'tiff', 7 => 'tiff',
8 => 'tiff', 8 => 'tiff',
); ];
function init ($cfg = array(), $post_params = array(), $uploaded_only = true) /**
* @param array $cfg
* @param array $post_params
* @param bool $uploaded_only
*
* @return bool
*/
public function init ($cfg = [], $post_params = [], $uploaded_only = true)
{ {
global $bb_cfg, $lang; global $bb_cfg, $lang;
@ -47,29 +94,34 @@ class upload_common
$msg = $lang['UPLOAD_ERROR_COMMON']; $msg = $lang['UPLOAD_ERROR_COMMON'];
$msg .= ($err_desc =& $lang['UPLOAD_ERRORS'][$this->file['error']]) ? " ($err_desc)" : ''; $msg .= ($err_desc =& $lang['UPLOAD_ERRORS'][$this->file['error']]) ? " ($err_desc)" : '';
$this->errors[] = $msg; $this->errors[] = $msg;
return false; return false;
} }
// file_exists // file_exists
if (!file_exists($this->file['tmp_name'])) if (!file_exists($this->file['tmp_name']))
{ {
$this->errors[] = "Uploaded file not exists: {$this->file['tmp_name']}"; $this->errors[] = "Uploaded file not exists: {$this->file['tmp_name']}";
return false; return false;
} }
// size // size
if (!$this->file_size = filesize($this->file['tmp_name'])) if (!$this->file_size = filesize($this->file['tmp_name']))
{ {
$this->errors[] = "Uploaded file is empty: {$this->file['tmp_name']}"; $this->errors[] = "Uploaded file is empty: {$this->file['tmp_name']}";
return false; return false;
} }
if ($this->cfg['max_size'] && $this->file_size > $this->cfg['max_size']) if ($this->cfg['max_size'] && $this->file_size > $this->cfg['max_size'])
{ {
$this->errors[] = sprintf($lang['UPLOAD_ERROR_SIZE'], humn_size($this->cfg['max_size'])); $this->errors[] = sprintf($lang['UPLOAD_ERROR_SIZE'], humn_size($this->cfg['max_size']));
return false; return false;
} }
// is_uploaded_file // is_uploaded_file
if ($uploaded_only && !is_uploaded_file($this->file['tmp_name'])) if ($uploaded_only && !is_uploaded_file($this->file['tmp_name']))
{ {
$this->errors[] = "Not uploaded file: {$this->file['tmp_name']}"; $this->errors[] = "Not uploaded file: {$this->file['tmp_name']}";
return false; return false;
} }
// get ext // get ext
@ -88,6 +140,7 @@ class upload_common
if (!$width || !$height || !$type || !isset($this->img_types[$type])) if (!$width || !$height || !$type || !isset($this->img_types[$type]))
{ {
$this->errors[] = $lang['UPLOAD_ERROR_FORMAT']; $this->errors[] = $lang['UPLOAD_ERROR_FORMAT'];
return false; return false;
} }
$this->file_ext = $this->img_types[$type]; $this->file_ext = $this->img_types[$type];
@ -96,12 +149,14 @@ class upload_common
if (($this->cfg['max_width'] && $width > $this->cfg['max_width']) || ($this->cfg['max_height'] && $height > $this->cfg['max_height'])) if (($this->cfg['max_width'] && $width > $this->cfg['max_width']) || ($this->cfg['max_height'] && $height > $this->cfg['max_height']))
{ {
$this->errors[] = sprintf($lang['UPLOAD_ERROR_DIMENSIONS'], $this->cfg['max_width'], $this->cfg['max_height']); $this->errors[] = sprintf($lang['UPLOAD_ERROR_DIMENSIONS'], $this->cfg['max_width'], $this->cfg['max_height']);
return false; return false;
} }
} }
else else
{ {
$this->errors[] = $lang['UPLOAD_ERROR_NOT_IMAGE']; $this->errors[] = $lang['UPLOAD_ERROR_NOT_IMAGE'];
return false; return false;
} }
} }
@ -109,24 +164,33 @@ class upload_common
if ($uploaded_only && (!isset($this->ext_ids[$this->file_ext]) || !in_array($this->file_ext, $this->cfg['allowed_ext'], true))) if ($uploaded_only && (!isset($this->ext_ids[$this->file_ext]) || !in_array($this->file_ext, $this->cfg['allowed_ext'], true)))
{ {
$this->errors[] = sprintf($lang['UPLOAD_ERROR_NOT_ALLOWED'], htmlCHR($this->file_ext)); $this->errors[] = sprintf($lang['UPLOAD_ERROR_NOT_ALLOWED'], htmlCHR($this->file_ext));
return false; return false;
} }
$this->file_ext_id = $this->ext_ids[$this->file_ext]; $this->file_ext_id = @$this->ext_ids[$this->file_ext];
return true; return true;
} }
function store ($mode = '', $params = array()) /**
* @param string $mode
* @param array $params
*
* @return bool
*/
public function store ($mode = '', $params = [])
{ {
if ($mode == 'avatar') if ($mode == 'avatar')
{ {
delete_avatar($params['user_id'], $params['avatar_ext_id']); delete_avatar($params['user_id'], $params['avatar_ext_id']);
$file_path = get_avatar_path($params['user_id'], $this->file_ext_id); $file_path = get_avatar_path($params['user_id'], $this->file_ext_id);
return $this->_move($file_path); return $this->_move($file_path);
} }
else if ($mode == 'attach') else if ($mode == 'attach')
{ {
$file_path = get_attach_path($params['topic_id']); $file_path = get_attach_path($params['topic_id']);
return $this->_move($file_path); return $this->_move($file_path);
} }
else else
@ -135,7 +199,12 @@ class upload_common
} }
} }
function _move ($file_path) /**
* @param $file_path
*
* @return bool
*/
private function _move ($file_path)
{ {
$dir = dirname($file_path); $dir = dirname($file_path);
if (!file_exists($dir)) if (!file_exists($dir))
@ -143,6 +212,7 @@ class upload_common
if (!bb_mkdir($dir)) if (!bb_mkdir($dir))
{ {
$this->errors[] = "Cannot create dir: $dir"; $this->errors[] = "Cannot create dir: $dir";
return false; return false;
} }
} }
@ -151,6 +221,7 @@ class upload_common
if (!@copy($this->file['tmp_name'], $file_path)) if (!@copy($this->file['tmp_name'], $file_path))
{ {
$this->errors[] = 'Cannot copy tmp file'; $this->errors[] = 'Cannot copy tmp file';
return false; return false;
} }
@unlink($this->file['tmp_name']); @unlink($this->file['tmp_name']);

Some files were not shown because too many files have changed in this diff Show more