$Date: 2006-02-06 23:08:03 +0000 (Mon, 06 Feb 2006) $ | > $Revision: 140 $ | > $Author: bfarber $ +--------------------------------------------------------------------------- | | > Multi function library | > Module written by Matt Mecham | > Date started: 14th February 2002 | | > Module Version Number: 1.0.0 | > Quality Checked: Wed 15 Sept. 2004 +-------------------------------------------------------------------------- */ /** * Main IPSclass class. * * This is the main class which is referenced via $ipsclass throughout * all the IPB sub-classes and modules. It holds amongst others: * * // Object from class_forums.php class * $ipsclass->forums * // Object from class_display.php class * $ipsclass->print * // Object from SQL classes * $ipsclass->db * // Array of parsed caches (from table cache_store) * $ipsclass->cache * // Array of settings variables (taken from parsed setting cache) * $ipsclass->vars * // Array of member variables (taken from class_session::$member) * $ipsclass->member * // Array of sanitized _GET _POST data (run via parse_clean_value) * $ipsclass->input * // Array of language strings loaded via separate lang files * $ipsclass->lang * // Variable: Full base URL with session ID if required * $ipsclass->base_url * // Array of compiled template objects * $ipsclass->compiled_templates * * Recommended method of passing $ipsclass through modules * * $module = new module(); * $module->ipsclass =& $ipsclass; // Create reference * * * @package InvisionPowerBoard * @author Matt Mecham * @copyright Invision Power Services, Inc. * @version 2.1 */ if ( ! defined( 'IPSCLASS_LOADED' ) ) { /** * IPSCLASS loaded Flag */ define( 'IPSCLASS_LOADED', 1 ); } /** * Main ipsclass class. * * This class holds all the non-class specific functions * * @package InvisionPowerBoard * @author Matt Mecham * @version 2.1 */ class ipsclass { /** * HUMAN version string (Eg: v2.1 BETA 1) * * @var string */ var $version = "v2.1.7"; /** * LONG version number (Eg: 21000) * * @var string */ var $acpversion = '21013.60712.u'; var $vn_full = ''; var $vn_build_date = ''; var $vn_build_reason = ''; /** * Member Array * * @var array */ var $member = array(); /** * _GET _POST Input Array * * @var array */ var $input = array(); /** * Setting variables array * * @var array */ var $vars = array(); /** * Language strings array * * @var array */ var $lang = array(); /** * Skin variables array * * @var array */ var $skin = array(); /** * Compiled loaded templates * * @var array */ var $compiled_templates = array(); /** * Loaded templates inc. ID * * @var array */ var $loaded_templates = array(); /** * Cache array * * @var array */ var $cache = array(); /** * Session ID * * @var string */ var $session_id = ""; /** * Base URL * * @var string */ var $base_url = ""; /** * Language ID; corresponds to cache/lang_cache/{folder}/ * * @var string */ var $lang_id = ""; /** * Session type (cookie or url) * * @var string */ var $session_type = ""; /**#@+ * @access public * @var string */ var $lastclick = ""; var $location = ""; var $debug_html = ""; var $perm_id = ""; var $offset = ""; var $num_format = ""; var $query_string_safe = ''; /** * MD5 Check variable */ var $md5_check = ''; /**#@-*/ var $server_load = 0; var $can_use_fancy_js = 0; var $offset_set = 0; var $allow_unicode = 1; var $get_magic_quotes = 0; /**#@+ * @access public * @var object */ var $converge; var $print; var $sess; var $forums; /**#@-*/ /**#@+ * @access public * @var array */ var $topic_cache = array(); var $time_formats = array(); var $time_options = array(); var $today_array = array(); /** * User's browser array (version, browser) */ var $browser; /**#@-*/ /*-------------------------------------------------------------------------*/ // Set up some standards to save CPU later /*-------------------------------------------------------------------------*/ /** * Initial ipsclass, set up some variables for later * Populates: * $this->time_options, $this->num_format, $this->get_magic_quotes, $this->ip_address * $this->user_agent, $this->browser, $this->operating_system * * @return void; */ function initiate_ipsclass() { //----------------------------------------- // Version numbers //----------------------------------------- //$this->acpversion = '210015.060501.u'; list( $n, $b, $r ) = explode( ".", $this->acpversion ); $this->vn_full = $this->acpversion; $this->acpversion = $n; $this->vn_build_date = $b ? $b : gmdate( 'ymd', $this->vars['board_start'] ); $this->vn_build_reason = $r; //----------------------------------------- // Time options //----------------------------------------- $this->time_options = array( 'JOINED' => $this->vars['clock_joined'], 'SHORT' => $this->vars['clock_short'], 'LONG' => $this->vars['clock_long'], 'TINY' => $this->vars['clock_tiny'] ? $this->vars['clock_tiny'] : 'j M Y - G:i', 'DATE' => $this->vars['clock_date'] ? $this->vars['clock_date'] : 'j M Y', ); $this->num_format = ($this->vars['number_format'] == 'space') ? ' ' : $this->vars['number_format']; $this->get_magic_quotes = get_magic_quotes_gpc(); //----------------------------------------- // Sort out the accessing IP // (Thanks to Cosmos and schickb) //----------------------------------------- $addrs = array(); if ( $this->vars['xforward_matching'] ) { foreach( array_reverse( explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) as $x_f ) { $x_f = trim($x_f); if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $x_f ) ) { $addrs[] = $x_f; } } $addrs[] = $_SERVER['HTTP_CLIENT_IP']; $addrs[] = $_SERVER['HTTP_PROXY_USER']; } $addrs[] = $_SERVER['REMOTE_ADDR']; //----------------------------------------- // Do we have one yet? //----------------------------------------- foreach ( $addrs as $ip ) { if ( $ip ) { preg_match( "/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/", $ip, $match ); $this->ip_address = $match[1].'.'.$match[2].'.'.$match[3].'.'.$match[4]; if ( $this->ip_address AND $this->ip_address != '...' ) { break; } } } //----------------------------------------- // Make sure we take a valid IP address //----------------------------------------- if ( ! $this->ip_address OR $this->ip_address == '...' ) { print "Could not determine your IP address"; exit(); } #Backwards compat: $this->input["IP_ADDRESS"] = $this->ip_address; //----------------------------------------- // Make a safe query string //----------------------------------------- $this->query_string_safe = str_replace( '&amp;', '&', $this->parse_clean_value( urldecode($_SERVER['QUERY_STRING'] ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING') ) ) ); $this->query_string_real = str_replace( '&' , '&', $this->query_string_safe ); //----------------------------------------- // Upload dir? //----------------------------------------- $this->vars['upload_dir'] = $this->vars['upload_dir'] ? $this->vars['upload_dir'] : ROOT_PATH.'uploads'; //----------------------------------------- // Char set //----------------------------------------- $this->vars['gb_char_set'] = $this->vars['gb_char_set'] ? $this->vars['gb_char_set'] : 'windows-1251'; //----------------------------------------- // PHP API //----------------------------------------- define('IPB_PHP_SAPI', php_sapi_name() ); //----------------------------------------- // IPS CLASS INITIATED //----------------------------------------- if ( ! defined( 'IPSCLASS_INITIATED' ) ) { define( 'IPSCLASS_INITIATED', 1 ); } //----------------------------------------- // Get user-agent, browser and OS //----------------------------------------- $this->user_agent = $this->parse_clean_value($_SERVER['HTTP_USER_AGENT']); $this->browser = $this->fetch_browser(); $this->operating_system = $this->fetch_os(); //----------------------------------------- // Can we use fancy JS? IE6, Safari, Moz // and opera 7.6 //----------------------------------------- if ( $this->browser['browser'] == 'ie' AND $this->browser['version'] >= 6.0 ) { $this->can_use_fancy_js = 1; } else if ( $this->browser['browser'] == 'gecko' AND $this->browser['version'] >= 20030312 ) { $this->can_use_fancy_js = 1; } else if ( $this->browser['browser'] == 'mozilla' AND $this->browser['version'] >= 1.3 ) { $this->can_use_fancy_js = 1; } else if ( $this->browser['browser'] == 'opera' AND $this->browser['version'] >= 7.6 ) { $this->can_use_fancy_js = 1; } else if ( $this->browser['browser'] == 'safari' AND $this->browser['version'] >= 120) { $this->can_use_fancy_js = 1; } //$this->can_use_fancy_js = 0; } /*-------------------------------------------------------------------------*/ // INIT: Load cache /*-------------------------------------------------------------------------*/ /** * Loads and parses the required cache elements from the DB * * @todo Add in methods to save and write to disk file * @param array Array of caches to load * @return void */ function init_load_cache( $cachearray=array('settings', 'group_cache', 'systemvars', 'skin_id_cache', 'forum_cache', 'rss_export') ) { //-------------------------------- // Generate cache list //-------------------------------- $cachelist = "'".implode( "','", $cachearray )."'"; //-------------------------------- // Get... //-------------------------------- $this->DB->simple_construct( array( 'select' => '*', 'from' => 'cache_store', 'where' => "cs_key IN ( $cachelist )" ) ); $this->DB->simple_exec(); while ( $r = $this->DB->fetch_row() ) { if ( $r['cs_key'] == 'settings' ) { $tmp = unserialize( $r['cs_value'] ); if ( is_array( $tmp ) and count( $tmp ) ) { foreach( $tmp as $k => $v ) { $this->vars[ $k ] = $v; } } unset( $tmp ); } else { if ( $r['cs_array'] ) { $this->cache[ $r['cs_key'] ] = unserialize( stripslashes($r['cs_value']) ); } else { $this->cache[ $r['cs_key'] ] = $r['cs_value']; } } } if ( ! isset( $this->cache['systemvars'] ) ) { $this->DB->simple_exec_query( array( 'delete' => 'cache_store', 'where' => "cs_key='systemvars'" ) ); $this->DB->do_insert( 'cache_store', array( 'cs_key' => 'systemvars', 'cs_value' => addslashes(serialize(array())), 'cs_array' => 1 ) ); } //-------------------------------- // Set up cache path //-------------------------------- if ( $this->vars['ipb_cache_path'] ) { define( 'CACHE_PATH', $this->vars['ipb_cache_path'] ); } else { define( 'CACHE_PATH', ROOT_PATH ); } //----------------------------------------- // IPS CACHE LOADED //----------------------------------------- if ( ! defined( 'IPSCLASS_CACHE_LOADED' ) ) { define( 'IPSCLASS_CACHE_LOADED', 1 ); } //-------------------------------- // Set up defaults //-------------------------------- $this->vars['topic_title_max_len'] = $this->vars['topic_title_max_len'] ? $this->vars['topic_title_max_len'] : 50; } /*-------------------------------------------------------------------------*/ // INIT: DB Connection /*-------------------------------------------------------------------------*/ /** * Initializes the database connection and populates $ipsclass->DB * * @return void */ function init_db_connection() { $this->vars['sql_driver'] = ! $this->vars['sql_driver'] ? 'mysql' : strtolower($this->vars['sql_driver']); if ( ! class_exists( 'db_main' ) ) { require_once( KERNEL_PATH.'class_db.php' ); require_once( KERNEL_PATH.'class_db_'.$this->vars['sql_driver'].".php" ); } $this->DB = new db_driver; $this->DB->obj['sql_database'] = $this->vars['sql_database']; $this->DB->obj['sql_user'] = $this->vars['sql_user']; $this->DB->obj['sql_pass'] = $this->vars['sql_pass']; $this->DB->obj['sql_host'] = $this->vars['sql_host']; $this->DB->obj['sql_tbl_prefix'] = $this->vars['sql_tbl_prefix']; $this->DB->obj['force_new_connection'] = $this->vars['sql_force_new_connection']; $this->DB->obj['use_shutdown'] = USE_SHUTDOWN; //----------------------------------- // Load query file //----------------------------------- if ( defined( 'IPB_LOAD_SQL' ) ) { $this->DB->obj['query_cache_file'] = ROOT_PATH.'sources/sql/'.$this->vars['sql_driver'].'_'. IPB_LOAD_SQL .'.php'; } else if ( IPB_THIS_SCRIPT == 'admin' ) { $this->DB->obj['query_cache_file'] = ROOT_PATH.'sources/sql/'.$this->vars['sql_driver'].'_admin_queries.php'; } else { $this->DB->obj['query_cache_file'] = ROOT_PATH.'sources/sql/'.$this->vars['sql_driver'].'_queries.php'; } //----------------------------------- // Required vars? //----------------------------------- if ( is_array( $this->DB->connect_vars ) and count( $this->DB->connect_vars ) ) { foreach( $this->DB->connect_vars as $k => $v ) { $this->DB->connect_vars[ $k ] = $this->vars[ $k ]; } } //-------------------------------- // Backwards compat //-------------------------------- if ( ! $this->DB->connect_vars['mysql_tbl_type'] ) { $this->DB->connect_vars['mysql_tbl_type'] = 'myisam'; } //-------------------------------- // Make CONSTANT //-------------------------------- define( 'SQL_PREFIX' , $this->DB->obj['sql_tbl_prefix'] ); define( 'SQL_DRIVER' , $this->vars['sql_driver'] ); define( 'IPS_MAIN_DB_CLASS_LOADED', TRUE ); //-------------------------------- // Get a DB connection //-------------------------------- $this->DB->connect(); //----------------------------------------- // IPS DB LOADED //----------------------------------------- if ( ! defined( 'IPSCLASS_DB_LOADED' ) ) { define( 'IPSCLASS_DB_LOADED', 1 ); } } /*-------------------------------------------------------------------------*/ // Get browser // Return: unknown, windows, mac /*-------------------------------------------------------------------------*/ /** * Fetches the user's operating system * * @return string */ function fetch_os() { $useragent = strtolower($_SERVER['HTTP_USER_AGENT']); if ( strstr( $useragent, 'mac' ) ) { return 'mac'; } if ( preg_match( '#wi(n|n32|ndows)#', $useragent ) ) { return 'windows'; } return 'unknown'; } /*-------------------------------------------------------------------------*/ // Get browser // Return: unknown, opera, IE, mozilla, konqueror, safari /*-------------------------------------------------------------------------*/ /** * Fetches the user's browser from their user-agent * * @return array [ browser, version ] */ function fetch_browser() { $version = 0; $browser = "unknown"; $useragent = strtolower($_SERVER['HTTP_USER_AGENT']); //----------------------------------------- // Opera... //----------------------------------------- if ( strstr( $useragent, 'opera' ) ) { preg_match( "#opera[ /]([0-9\.]{1,10})#", $useragent, $ver ); return array( 'browser' => 'opera', 'version' => $ver[1] ); } //----------------------------------------- // IE... //----------------------------------------- if ( strstr( $useragent, 'msie' ) ) { preg_match( "#msie[ /]([0-9\.]{1,10})#", $useragent, $ver ); return array( 'browser' => 'ie', 'version' => $ver[1] ); } //----------------------------------------- // Safari... //----------------------------------------- if ( strstr( $useragent, 'safari' ) ) { preg_match( "#safari/([0-9.]{1,10})#", $useragent, $ver ); return array( 'browser' => 'safari', 'version' => $ver[1] ); } //----------------------------------------- // Mozilla browsers... //----------------------------------------- if ( strstr( $useragent, 'gecko' ) ) { preg_match( "#gecko/(\d+)#", $useragent, $ver ); return array( 'browser' => 'gecko', 'version' => $ver[1] ); } //----------------------------------------- // Older Mozilla browsers... //----------------------------------------- if ( strstr( $useragent, 'mozilla' ) ) { preg_match( "#^mozilla/[5-9]\.[0-9.]{1,10}.+rv:([0-9a-z.+]{1,10})#", $useragent, $ver ); return array( 'browser' => 'mozilla', 'version' => $ver[1] ); } //----------------------------------------- // Konqueror... //----------------------------------------- if ( strstr( $useragent, 'konqueror' ) ) { preg_match( "#konqueror/([0-9.]{1,10})#", $useragent, $ver ); return array( 'browser' => 'konqueror', 'version' => $ver[1] ); } //----------------------------------------- // Still here? //----------------------------------------- return array( 'browser' => $browser, 'version' => $version ); } /*-------------------------------------------------------------------------*/ // // LOAD CLASS: Wrapper to load ..er.. classes // /*-------------------------------------------------------------------------*/ /** * Wrapper function to load a class and pass $this automagically * * @param string File name * @param string Class Name * @param string Constructor variables * @return object */ function load_class( $file_name, $class_name, $pass_var="" ) { if ( ! $class_name ) { $class_name = $file_name; } require_once( $file_name ); if ( $pass_var ) { $class = new $class_name ( $pass_var ); } else { $class = new $class_name; } $class->ipsclass =& $this; return $class; } /*-------------------------------------------------------------------------*/ // // Check mod queue status // /*-------------------------------------------------------------------------*/ /** * Determine if this user / forum combo can manage mod queue * * @param integer Forum ID * @return integer Boolean */ function can_queue_posts($fid=0) { $return = 0; if ( $this->member['g_is_supmod'] ) { $return = 1; } else if ( $fid and $this->member['is_mod'] and $this->member['_moderator'][ $fid ]['post_q'] ) { $return = 1; } return $return; } /*-------------------------------------------------------------------------*/ // // Check multi mod status // /*-------------------------------------------------------------------------*/ /** * Determine if this user / forum combo can manage multi moderation tasks * and return mm_array of allowed tasks * * @param integer Forum ID * @return array Allowed tasks */ function get_multimod($fid) { $mm_array = array(); $pass_go = FALSE; if ( $this->member['id'] ) { if ( $this->member['g_is_supmod'] ) { $pass_go = TRUE; } else if ( $this->member['_moderator'][ $fid ]['can_mm'] == 1 ) { $pass_go = TRUE; } } if ( $pass_go != TRUE ) { return $mm_array; } if ( ! is_array( $this->cache['multimod'] ) ) { $this->cache['multimod'] = array(); $this->DB->simple_construct( array( 'select' => '*', 'from' => 'topic_mmod', 'order' => 'mm_title' ) ); $this->DB->simple_exec(); while ($i = $this->DB->fetch_row()) { $this->cache['multimod'][ $i['mm_id'] ] = $i; } $this->update_cache( array( 'name' => 'multimod', 'array' => 1, 'deletefirst' => 1 ) ); } //----------------------------------------- // Get the topic mod thingies //----------------------------------------- foreach( $this->cache['multimod'] as $i => $r ) { if ( $r['mm_forums'] == '*' OR strstr( ",".$r['mm_forums'].",", ",".$fid."," ) ) { $mm_array[] = array( $r['mm_id'], $r['mm_title'] ); } } return $mm_array; } /*-------------------------------------------------------------------------*/ // UNPACK MEMBER CACHE /*-------------------------------------------------------------------------*/ /** * Unpacks a member's cache * * Left as a function for any other processing * * @param string Serialized cache array * @return array Unpacked array */ function unpack_member_cache( $cache_serialized_array="" ) { return unserialize( $cache_serialized_array ); } /*-------------------------------------------------------------------------*/ // PACK MEMBER CACHE /*-------------------------------------------------------------------------*/ /** * Packs up member's cache * * Takes an existing array and updates member's DB row * This will overwrite any existing entries by the same * key and create new entries for non-existing rows * * @param integer Member ID * @param array New array * @param array Current Array (optional) * @return boolean */ function pack_and_update_member_cache( $member_id, $new_cache_array, $current_cache_array='' ) { //----------------------------------------- // INIT //----------------------------------------- $member_id = intval( $member_id ); //----------------------------------------- // Got a member ID? //----------------------------------------- if ( ! $member_id ) { return FALSE; } //----------------------------------------- // Got anything to update? //----------------------------------------- if ( ! is_array( $new_cache_array ) OR ! count( $new_cache_array ) ) { return FALSE; } //----------------------------------------- // Got a current cache? //----------------------------------------- if ( ! is_array( $current_cache_array ) ) { $member = $this->DB->build_and_exec_query( array( 'select' => "members_cache", 'from' => 'members', 'where' => 'id='.$member_id ) ); $member['members_cache'] = $member['members_cache'] ? $member['members_cache'] : array(); $current_cache_array = unserialize( $member['members_cache'] ); } //----------------------------------------- // Overwrite... //----------------------------------------- foreach( $new_cache_array as $k => $v ) { $current_cache_array[ $k ] = $v; } //----------------------------------------- // Update... //----------------------------------------- $this->DB->do_update( 'members', array( 'members_cache' => serialize( $current_cache_array ) ), 'id='.$member_id ); //----------------------------------------- // Set member array right... //----------------------------------------- $this->member['_cache'] = $current_cache_array; } /*-------------------------------------------------------------------------*/ // UPDATE FORUM CACHE /*-------------------------------------------------------------------------*/ /** * Updates forum cache (loads all, recaches all) * * @return void */ function update_forum_cache() { $ignore_me = array( 'redirect_url', 'redirect_loc', 'rules_text', 'permission_custom_error', 'notify_modq_emails' ); if ( $this->vars['forum_cache_minimum'] ) { $ignore_me[] = 'description'; $ignore_me[] = 'rules_title'; } $this->cache['forum_cache'] = array(); $this->DB->simple_construct( array( 'select' => '*', 'from' => 'forums', 'order' => 'parent_id, position' ) ); $this->DB->simple_exec(); while( $f = $this->DB->fetch_row() ) { $fr = array(); $perms = unserialize(stripslashes($f['permission_array'])); //----------------------------------------- // Stuff we don't need... //----------------------------------------- foreach( $f as $k => $v ) { if ( in_array( $k, $ignore_me ) ) { continue; } else { if ( $v != "" ) { $fr[ $k ] = $v; } } } $fr['read_perms'] = $perms['read_perms']; $fr['reply_perms'] = $perms['reply_perms']; $fr['start_perms'] = $perms['start_perms']; $fr['upload_perms'] = $perms['upload_perms']; $fr['show_perms'] = $perms['show_perms']; unset($fr['permission_array']); $this->cache['forum_cache'][ $fr['id'] ] = $fr; } $this->update_cache( array( 'name' => 'forum_cache', 'array' => 1, 'deletefirst' => 1, 'donow' => 1 ) ); } /*-------------------------------------------------------------------------*/ // // UPDATE CACHE // /*-------------------------------------------------------------------------*/ /** * Updates cache * * @param array Cache values (name, value, deletefirst, donow) * @todo remove some of the MySQL specific code * @return void */ function update_cache( $v=array() ) { //----------------------------------------- // Don't cache forums? //----------------------------------------- if ( $v['name'] == 'forum_cache' AND $this->vars['no_cache_forums'] ) { return; } //----------------------------------------- // Next... //----------------------------------------- if ( $v['name'] ) { if ( ! $v['value'] ) { $value = $this->DB->add_slashes(serialize($this->cache[ $v['name'] ])); } $this->DB->manual_addslashes = 1; $this->DB->no_escape_fields['cs_key'] = 1; if ( $v['deletefirst'] == 1 ) { if ( $v['donow'] ) { if ( $this->vars['sql_driver'] == 'mysql' ) { $this->DB->query( "REPLACE INTO ".SQL_PREFIX."cache_store SET cs_key='{$v['name']}', cs_value='$value', cs_array=".intval($v['array']) ); } else { $this->DB->simple_construct( array( 'delete' => 'cache_store', 'where' => "cs_key='{$v['name']}'" ) ); $this->DB->simple_exec(); $this->DB->do_insert( 'cache_store', array( 'cs_array' => intval($v['array']), 'cs_key' => $v['name'], 'cs_value' => $value ) ); } } else { if ( $this->vars['sql_driver'] == 'mysql' ) { $this->DB->query( "REPLACE INTO ".SQL_PREFIX."cache_store SET cs_key='{$v['name']}', cs_value='$value', cs_array=".intval($v['array']) ); } else { $this->DB->simple_construct( array( 'delete' => 'cache_store', 'where' => "cs_key='{$v['name']}'" ) ); $this->DB->simple_shutdown_exec(); $this->DB->do_shutdown_insert( 'cache_store', array( 'cs_array' => intval($v['array']), 'cs_key' => $v['name'], 'cs_value' => $value ) ); } } } else { if ( $v['donow'] ) { $this->DB->do_update( 'cache_store', array( 'cs_array' => intval($v['array']), 'cs_value' => $value ), "cs_key='{$v['name']}'" ); } else { $this->DB->do_shutdown_update( 'cache_store', array( 'cs_array' => intval($v['array']), 'cs_value' => $value ), "cs_key='{$v['name']}'" ); } } $this->DB->manual_addslashes = 0; } } /*-------------------------------------------------------------------------*/ // // MY DECONSTRUCTOR // /*-------------------------------------------------------------------------*/ /** * Manual deconstructor * * Runs any SQL shutdown queries and mail tasks * * @return void */ function my_deconstructor() { //----------------------------------------- // Any shutdown queries //----------------------------------------- $this->DB->return_die = 0; if ( count( $this->DB->obj['shutdown_queries'] ) ) { foreach( $this->DB->obj['shutdown_queries'] as $q ) { $this->DB->query( $q ); } } $this->DB->return_die = 1; $this->DB->obj['shutdown_queries'] = array(); //----------------------------------------- // Process mail queue //----------------------------------------- $this->process_mail_queue(); $this->DB->close_db(); } /*-------------------------------------------------------------------------*/ // // Process Mail Queue // /*-------------------------------------------------------------------------*/ /** * Process mail queue * * @return void */ function process_mail_queue() { global $ROOT_PATH; //----------------------------------------- // Bug in PHP? // shutdown func can't use relative // path.. //----------------------------------------- $ROOT_PATH = $ROOT_PATH ? $ROOT_PATH.'/' : ROOT_PATH; //----------------------------------------- // SET UP //----------------------------------------- $this->vars['mail_queue_per_blob'] = $this->vars['mail_queue_per_blob'] ? $this->vars['mail_queue_per_blob'] : 5; $this->cache['systemvars']['mail_queue'] = intval($this->cache['systemvars']['mail_queue']); $sent_ids = array(); if ( $this->cache['systemvars']['mail_queue'] > 0 ) { //----------------------------------------- // Require the emailer... //----------------------------------------- require_once( $ROOT_PATH.'sources/classes/class_email.php' ); $emailer = new emailer($ROOT_PATH); $emailer->ipsclass =& $this; $emailer->email_init(); //----------------------------------------- // Get the mail stuck in the queue //----------------------------------------- $this->DB->simple_construct( array( 'select' => '*', 'from' => 'mail_queue', 'order' => 'mail_id', 'limit' => array( 0, $this->vars['mail_queue_per_blob'] ) ) ); $this->DB->simple_exec(); while ( $r = $this->DB->fetch_row() ) { $data[] = $r; $sent_ids[] = $r['mail_id']; } if ( count($sent_ids) ) { //----------------------------------------- // Delete sent mails and update count //----------------------------------------- $this->cache['systemvars']['mail_queue'] = $this->cache['systemvars']['mail_queue'] - count($sent_ids); $this->DB->simple_exec_query( array( 'delete' => 'mail_queue', 'where' => 'mail_id IN ('.implode(",", $sent_ids).')' ) ); foreach( $data as $mail ) { if ( $mail['mail_to'] and $mail['mail_subject'] and $mail['mail_content'] ) { $emailer->to = $mail['mail_to']; $emailer->from = $mail['mail_from'] ? $mail['mail_from'] : $this->vars['email_out']; $emailer->subject = $mail['mail_subject']; $emailer->message = $mail['mail_content']; $emailer->send_mail(); } } } else { //----------------------------------------- // No mail after all? //----------------------------------------- $this->cache['systemvars']['mail_queue'] = 0; } //----------------------------------------- // Update cache with remaning email count //----------------------------------------- $this->DB->do_update( 'cache_store', array( 'cs_array' => 1, 'cs_value' => addslashes(serialize($this->cache['systemvars'])) ), "cs_key='systemvars'" ); } } /*-------------------------------------------------------------------------*/ // Load a ACP template file /*-------------------------------------------------------------------------*/ /** * Load an ACP skin template file for use * * @return object Class object */ function acp_load_template( $template ) { if ( ! $this->skin_acp ) { $this->skin_acp = 'IPB2_Standard'; } require_once( ROOT_PATH."skin_acp/".$this->skin_acp."/acp_skin_html/".$template.".php" ); $tmp = new $template(); $tmp->ipsclass =& $this; return $tmp; } /*-------------------------------------------------------------------------*/ // Require, parse and return an array containing the language stuff /*-------------------------------------------------------------------------*/ /** * Load an ACP language file. Populates $this->lang * * @param string File name * @return void * @since 2.1 */ function acp_load_language( $file="" ) { if ( ! $this->lang_id ) { $this->lang_id = $this->member['language'] ? $this->member['language'] : $this->vars['default_language']; if ( ($this->lang_id != $this->vars['default_language']) and ( ! is_dir( ROOT_PATH."cache/lang_cache/".$this->lang_id ) ) ) { $this->lang_id = $this->vars['default_language']; } //----------------------------------------- // Still nothing? //----------------------------------------- if ( ! $this->lang_id ) { $this->lang_id = 'ru'; } } //----------------------------------------- // Load it //----------------------------------------- require_once( ROOT_PATH."cache/lang_cache/".$this->lang_id."/".$file.".php" ); if ( is_array( $lang ) ) { foreach ($lang as $k => $v) { $this->acp_lang[ $k ] = stripslashes($v); } } unset($lang); } /*-------------------------------------------------------------------------*/ // // Load a template file from DB or from PHP file // /*-------------------------------------------------------------------------*/ /** * Load a normal template file from either cached PHP file or * from the DB. Populates $this->compiled_templates[ _template_name_ ] * * @param string Template name * @param integer Template set ID * @return void */ function load_template( $name, $id='' ) { $tags = 1; //----------------------------------------- // Select ID //----------------------------------------- if ( ! $id ) { $id = $this->skin['_skincacheid']; } //----------------------------------------- // Full name //----------------------------------------- $full_name = $name.'_'.intval($id); $skin_global_name = 'skin_global_'.$id; //----------------------------------------- // Already got this template loaded? //----------------------------------------- if ( in_array( $full_name, $this->loaded_templates ) ) { return; } //----------------------------------------- // Not running safemode skins? //----------------------------------------- if ( $this->vars['safe_mode_skins'] == 0 AND $this->vars['safe_mode'] == 0 ) { //----------------------------------------- // Simply require and return //----------------------------------------- if ( $name != 'skin_global') { if ( ! in_array( $skin_global_name, $this->loaded_templates ) ) { require_once( CACHE_PATH."cache/skin_cache/cacheid_".$id."/skin_global.php" ); $this->compiled_templates['skin_global'] = new $skin_global_name(); $this->compiled_templates['skin_global']->ipsclass =& $this; # Add to loaded templates $this->loaded_templates[ $skin_global_name ] = $skin_global_name; } require_once( CACHE_PATH."cache/skin_cache/cacheid_".$id."/".$name.".php" ); $this->compiled_templates[ $name ] = new $full_name(); $this->compiled_templates[ $name ]->ipsclass =& $this; # Add to loaded templates $this->loaded_templates[ $full_name ] = $full_name; } else { if ( $name == 'skin_global' ) { require_once( CACHE_PATH."cache/skin_cache/cacheid_".$id."/skin_global.php" ); $this->compiled_templates['skin_global'] = new $skin_global_name(); $this->compiled_templates['skin_global']->ipsclass =& $this; # Add to loaded templates $this->loaded_templates[ $skin_global_name ] = $skin_global_name; return; } else { require_once( CACHE_PATH."cache/skin_cache/cacheid_".$id."/".$name.".php" ); $this->compiled_templates[ $name ] = new $full_name(); $this->compiled_templates[ $name ]->ipsclass =& $this; # Add to loaded templates $this->loaded_templates[ $full_name ] = $full_name; } } } else { //----------------------------------------- // We're using safe mode skins, yippee // Load the data from the DB //----------------------------------------- $skin_global = ""; $other_skin = ""; $this->skin['_type'] = 'Database Skins'; if ( $this->loaded_templates[ $skin_global_name ] == "" and $name != 'skin_global') { //----------------------------------------- // Skin global not loaded... //----------------------------------------- $this->DB->simple_construct( array( 'select' => '*', 'from' => 'skin_templates_cache', 'where' => "template_set_id=".$id." AND template_group_name IN ('skin_global', '$name')" ) ); $this->DB->simple_exec(); while ( $r = $this->DB->fetch_row() ) { if ( $r['template_group_name'] == 'skin_global' ) { $skin_global = $r['template_group_content']; } else { $other_skin = $r['template_group_content']; } } eval($skin_global); $this->compiled_templates['skin_global'] = new $skin_global_name(); $this->compiled_templates['skin_global']->ipsclass =& $this; # Add to loaded templates $this->loaded_templates[ $skin_global_name ] = $skin_global_name; } else { //----------------------------------------- // Skin global is loaded.. //----------------------------------------- if ( $name == 'skin_global' and in_array( $skin_global_name, $this->loaded_templates ) ) { return; } //----------------------------------------- // Load the skin, man //----------------------------------------- $this->DB->simple_construct( array( 'select' => '*', 'from' => 'skin_templates_cache', 'where' => "template_set_id=".$id." AND template_group_name='$name'" ) ); $this->DB->simple_exec(); $r = $this->DB->fetch_row(); $other_skin = $r['template_group_content']; } eval($other_skin); if ( $name == 'skin_global' ) { $this->compiled_templates['skin_global'] = new $skin_global_name(); $this->compiled_templates['skin_global']->ipsclass =& $this; # Add to loaded templates $this->loaded_templates[ $skin_global_name ] = $skin_global_name; } else { $this->compiled_templates[ $name ] = new $full_name(); $this->compiled_templates[ $name ]->ipsclass =& $this; # Add to loaded templates $this->loaded_templates[ $full_name ] = $full_name; } } //----------------------------------------- // LEGACY //----------------------------------------- if ( defined('LEGACY_MODE') && LEGACY_MODE == 1 ) { if ( $name ) { return $this->compiled_templates[ $name ]; } } } /*-------------------------------------------------------------------------*/ // SKIN, sort out the skin stuff /*-------------------------------------------------------------------------*/ /** * Load a skin, macro, settings, etc * * @return array Database row (not really used anymore) */ function load_skin() { //----------------------------------------- // INIT //----------------------------------------- $id = -1; $skin_set = 0; $from_forum = 0; $this->input['skinid'] = intval($this->input['skinid']); $this->member['skin'] = intval($this->member['skin']); //----------------------------------------- // Do we have a cache? //----------------------------------------- if ( ! is_array( $this->cache['skin_id_cache'] ) ) { define ( 'IN_ACP', 1 ); require_once( ROOT_PATH.'sources/lib/admin_cache_functions.php' ); $admin = new admin_cache_functions(); $admin->ipsclass =& $this; $this->cache['skin_id_cache'] = $admin->_rebuild_skin_id_cache(); } //----------------------------------------- // Search bot? //----------------------------------------- if ( ( $this->is_bot == 1 ) and ( $this->vars['spider_suit'] != "" ) ) { $skin_set = 1; $id = $this->vars['spider_suit']; } else { //----------------------------------------- // Do we have a skin for a particular forum? //----------------------------------------- if ($this->input['f'] and $this->input['act'] != 'UserCP') { if ( $this->cache['forum_cache'][ $this->input['f'] ]['skin_id'] > 0 ) { $id = $this->cache['forum_cache'][ $this->input['f'] ]['skin_id']; $skin_set = 1; $from_forum = 1; } } //----------------------------------------- // Are we allowing user chooseable skins? //----------------------------------------- if ($skin_set != 1 and $this->vars['allow_skins'] == 1) { if ( $this->input['skinid'] ) { $id = $this->input['skinid']; $skin_set = 1; } else if ( $this->member['skin'] ) { $id = $this->member['skin']; $skin_set = 1; } } } //----------------------------------------- // Nothing set / hidden and not admin? Choose the default //----------------------------------------- if ( $this->cache['skin_id_cache'][ $id ]['set_hidden'] ) { if ( $from_forum ) { $skin_set = 1; } else if ( $this->member['g_access_cp'] ) { $skin_set = 1; } else if ( $this->is_bot ) { $skin_set = 1; } else { $skin_set = 0; } } if ( ! $id OR ! $skin_set OR ! is_array($this->cache['skin_id_cache'][ $id ]) ) { foreach( $this->cache['skin_id_cache'] as $sid => $data ) { if ( $data['set_default'] ) { $id = $data['set_skin_set_id']; $skin_set = 1; } } } //----------------------------------------- // Get the skin //----------------------------------------- $db_skin = $this->DB->simple_exec_query( array( 'select' => '*', 'from' => 'skin_sets', 'where' => 'set_skin_set_id='.$id ) ); $this->skin['_css'] = $db_skin['set_cache_css']; $this->skin['_wrapper'] = $db_skin['set_cache_wrapper']; $this->skin['_macro'] = $db_skin['set_cache_macro']; $this->skin['_imagedir'] = $db_skin['set_image_dir']; $this->skin['_emodir'] = $db_skin['set_emoticon_folder']; $this->skin['_setid'] = $db_skin['set_skin_set_id']; $this->skin['_setname'] = $db_skin['set_name']; $this->skin['_usecsscache'] = $db_skin['set_css_method'] ? 1 : 0; $this->skin['_macros'] = unserialize(stripslashes($this->skin['_macro'])); $this->skin['_skincacheid'] = $db_skin['set_skin_set_id']; $this->skin['_csscacheid'] = $db_skin['set_skin_set_id']; if ( IN_DEV ) { $this->skin['_skincacheid'] = file_exists( CACHE_PATH.'cache/skin_cache/cacheid_1' ) ? 1 : $db_skin['set_skin_set_id']; $this->skin['_csscacheid'] = 1; } //----------------------------------------- // Setting the skin? //----------------------------------------- if ( ($this->input['setskin']) and ($this->member['id']) ) { $this->DB->simple_construct( array( 'update' => 'members', 'set' => "skin=".intval($id), 'where' => "id=".$this->member['id'] ) ); $this->DB->simple_exec(); $this->member['skin'] = $id; } return $row; } /*-------------------------------------------------------------------------*/ // Require, parse and return an array containing the language stuff /*-------------------------------------------------------------------------*/ /** * Load a language file. Populates $this->lang * * @param string File name * @return void * @since 2.1 */ function load_language( $file="" ) { if ( ! $this->lang_id ) { $this->lang_id = $this->member['language'] ? $this->member['language'] : $this->vars['default_language']; if ( ($this->lang_id != $this->vars['default_language']) and ( ! is_dir( ROOT_PATH."cache/lang_cache/".$this->lang_id ) ) ) { $this->lang_id = $this->vars['default_language']; } //----------------------------------------- // Still nothing? //----------------------------------------- if ( ! $this->lang_id ) { $this->lang_id = 'ru'; } } //----------------------------------------- // Load it //----------------------------------------- require_once( ROOT_PATH."cache/lang_cache/".$this->lang_id."/".$file.".php" ); if ( is_array( $lang ) ) { foreach ($lang as $k => $v) { $this->lang[ $k ] = stripslashes($v); } } unset($lang); } /*-------------------------------------------------------------------------*/ // Get new PM notification window /*-------------------------------------------------------------------------*/ /** * Get a new PM notification message * * @param integer Current offset * @param integer For Ajax flag * @return string Parsed Message * @todo Remove deprecated post_parser.php * @since 2.0 */ function get_new_pm_notification( $limit=0, $xmlout = 0 ) { //----------------------------------------- // Make sure we have a skin... //----------------------------------------- if ( ! $this->compiled_templates['skin_global'] ) { $this->load_template( 'skin_global' ); } //----------------------------------------- // posty parsery //----------------------------------------- require_once( ROOT_PATH."sources/handlers/han_parse_bbcode.php" ); $parser = new parse_bbcode(); $parser->ipsclass =& $this; $parser->allow_update_caches = 0; //----------------------------------------- // Get last PM details //----------------------------------------- $this->DB->cache_add_query( 'msg_get_new_pm_notification', array( 'mid' => $this->member['id'], 'limit_a' => intval($limit) ) ); $this->DB->simple_exec(); $msg = $this->DB->fetch_row(); //----------------------------------------- // Check... //----------------------------------------- if ( ! $msg['msg_id'] and ! $msg['mt_id'] and ! $msg['id'] ) { return ''; } //----------------------------------------- // Strip and wrap //----------------------------------------- $msg['msg_post'] = $parser->strip_all_tags( $msg['msg_post'] ); $msg['msg_post'] = preg_replace("#([^\s<>'\"/\.\\-\?&\n\r\%]{80})#i", " \\1"."
", $msg['msg_post']); $msg['msg_post'] = str_replace( "\n", "
", trim($msg['msg_post']) ); if ( strlen( $msg['msg_post'] ) > 300 ) { $msg['msg_post'] = substr( $msg['msg_post'], 0, 350 ) . '...'; $msg['msg_post'] = preg_replace( "/&(#(\d+;?)?)?\.\.\.$/", '...', $msg['msg_post'] ); } //----------------------------------------- // Do naughty words //----------------------------------------- if ( ! is_array( $this->cache['badwords'] ) ) { $this->cache['badwords'] = array(); $this->DB->simple_construct( array( 'select' => 'type,swop,m_exact', 'from' => 'badwords' ) ); $bbcode = $this->DB->simple_exec(); while ( $r = $this->DB->fetch_row($bbcode) ) { $this->cache['badwords'][] = $r; } } $msg['msg_post'] = $parser->bad_words( $msg['msg_post'] ); //----------------------------------------- // Add attach icon //----------------------------------------- if ( $msg['mt_hasattach'] ) { $msg['attach_img'] = '<{ATTACH_ICON}> '; } //----------------------------------------- // Date //----------------------------------------- $msg['msg_date'] = $this->get_date( $msg['msg_date'], 'TINY' ); //----------------------------------------- // Next / Total links //----------------------------------------- $msg['_cur_num'] = intval($limit) + 1; $msg['_msg_total'] = intval($this->member['new_msg']) ? intval($this->member['new_msg']) : 1; //----------------------------------------- // Return loverly HTML //----------------------------------------- $return = $this->compiled_templates['skin_global']->msg_get_new_pm_notification( $msg, $xmlout ); //----------------------------------------- // XML request? //----------------------------------------- return $return; } /*-------------------------------------------------------------------------*/ // XSS Clean: URLs /*-------------------------------------------------------------------------*/ /** * Check URL validity * * @param string Input String * @return boolean * @since 2.1.0 */ function xss_check_url( $url ) { $url = trim( urldecode( $url ) ); if ( ! preg_match( "#^https?://(?:[^<>*\"]+|[a-z0-9/\._\- !]+)$#iU", $url ) ) { return FALSE; } return TRUE; } /*-------------------------------------------------------------------------*/ // Convert string between char-sets /*-------------------------------------------------------------------------*/ /** * Convert a string between charsets * * @param string Input String * @param string Current char set * @param string Destination char set * @return string Parsed string * @since 2.1.0 */ function txt_convert_charsets($t, $orignal_cset, $destination_cset="") { $orignal_cset = strtolower($orignal_cset); $text = $t; //----------------------------------------- // Did we pass a destination? //----------------------------------------- $destination_cset = strtolower($destination_cset) ? strtolower($destination_cset) : strtolower($this->vars['gb_char_set']); //----------------------------------------- // Not the same? //----------------------------------------- if ( $destination_cset == $orignal_cset ) { return $t; } //----------------------------------------- // Do the convert //----------------------------------------- if ( function_exists( 'mb_convert_encoding' ) ) { $text = mb_convert_encoding( $text, $destination_cset, $orignal_cset ); } else if ( function_exists( 'recode_string' ) ) { $text = recode_string( $orignal_cset.'..'.$destination_cset, $text ); } else if ( function_exists( 'iconv' ) ) { $text = iconv( $orignal_cset, $destination_cset.'//TRANSLIT', $text); } return $text ? $text : $t; } /*-------------------------------------------------------------------------*/ // Truncate text string /*-------------------------------------------------------------------------*/ /** * Truncate a HTML string without breaking HTML entites * * @param string Input String * @param integer Desired min. length * @return string Parsed string * @since 2.0 */ function txt_truncate($text, $limit=30) { $text = str_replace( '&' , '&', $text ); $text = str_replace( '"', '"', $text ); if (strlen($text) > $limit) { $text = substr($text,0, $limit - 3) . "..."; $text = preg_replace( "/&(#(\d+;?)?)?\.\.\.$/", '...', $text ); } else { $text = preg_replace( "/&(#(\d+?)?)?$/", '', $text ); } return $text; } /*-------------------------------------------------------------------------*/ // txt_alphanumerical_clean // ------------------ // Remove non alphas /*-------------------------------------------------------------------------*/ /** * Clean a string to remove all non alphanumeric characters * * @param string Input String * @return string Parsed string * @since 2.1 */ function txt_alphanumerical_clean( $t ) { return preg_replace( "/[^a-zA-Z0-9\-\_]/", "" , $t ); } /*-------------------------------------------------------------------------*/ // txt_mb_strlen // ------------------ // Multi byte char string length /*-------------------------------------------------------------------------*/ /** * Get the true length of a multi-byte character string * * @param string Input String * @return intger String length * @since 2.1 */ function txt_mb_strlen( $t ) { return strlen( preg_replace("/&#([0-9]+);/", "-", $this->txt_stripslashes( $t ) ) ); } /*-------------------------------------------------------------------------*/ // txt_stripslashes // ------------------ // Make Big5 safe - only strip if not already... /*-------------------------------------------------------------------------*/ /** * Remove slashes if magic_quotes enabled * * @param string Input String * @return string Parsed string * @since 2.0 */ function txt_stripslashes($t) { if ( $this->get_magic_quotes ) { $t = stripslashes($t); $t = preg_replace( "/\\\(?!&#|\?#)/", "\", $t ); } return $t; } /*-------------------------------------------------------------------------*/ // txt_raw2form // ------------------ // makes _POST text safe for text areas /*-------------------------------------------------------------------------*/ /** * Convert text for use in a textarea * * @param string Input String * @return string Parsed string * @since 2.0 */ function txt_raw2form($t="") { $t = str_replace( '$', "$", $t); if ( $this->get_magic_quotes ) { $t = stripslashes($t); } $t = preg_replace( "/\\\(?!&#|\?#)/", "\", $t ); //--------------------------------------- // Make sure macros aren't converted //--------------------------------------- $t = preg_replace( "/<{(.+?)}>/", "<{\\1}>", $t ); return $t; } /*-------------------------------------------------------------------------*/ // txt_form2raw // ------------------ // Converts textarea to raw /*-------------------------------------------------------------------------*/ /** * Unconvert text for use in a textarea * * @param string Input String * @return string Parsed string * @since 2.0 */ function txt_form2raw($t="") { $t = str_replace( "$", '$' , $t); $t = str_replace( "\", '\\', $t ); return $t; } /*-------------------------------------------------------------------------*/ // Safe Slashes - ensures slashes are saved correctly /*-------------------------------------------------------------------------*/ /** * Attempt to make slashes safe for us in DB (not really needed now?) * * @param string Input String * @return string Parsed string * @since 2.0 */ function txt_safeslashes($t="") { return str_replace( '\\', "\\\\", $this->txt_stripslashes($t)); } /*-------------------------------------------------------------------------*/ // txt_htmlspecialchars // ------------------ // Custom version of htmlspecialchars to take into account mb chars /*-------------------------------------------------------------------------*/ /** * htmlspecialchars including multi-byte characters * * @param string Input String * @return string Parsed string * @since 2.0 */ function txt_htmlspecialchars($t="") { // Use forward look up to only convert & not { $t = preg_replace("/&(?!#[0-9]+;)/s", '&', $t ); $t = str_replace( "<", "<" , $t ); $t = str_replace( ">", ">" , $t ); $t = str_replace( '"', """, $t ); $t = str_replace( "'", ''', $t ); return $t; // A nice cup of? } /*-------------------------------------------------------------------------*/ // txt_UNhtmlspecialchars // ------------------ // Undoes what the above function does. Yes. /*-------------------------------------------------------------------------*/ /** * unhtmlspecialchars including multi-byte characters * * @param string Input String * @return string Parsed string * @since 2.0 */ function txt_UNhtmlspecialchars($t="") { $t = str_replace( "&" , "&", $t ); $t = str_replace( "<" , "<", $t ); $t = str_replace( ">" , ">", $t ); $t = str_replace( """, '"', $t ); $t = str_replace( "'", "'", $t ); return $t; } /*-------------------------------------------------------------------------*/ // txt_wintounix // ------------------ // Converts \r\n to \n /*-------------------------------------------------------------------------*/ /** * Convert windows newlines to unix newlines * * @param string Input String * @return string Parsed string * @since 2.0 */ function txt_windowstounix($t="") { // windows $t = str_replace( "\r\n" , "\n", $t ); // Mac OS 9 $t = str_replace( "\r" , "\n", $t ); return $t; } /*-------------------------------------------------------------------------*/ // return_md5_check // ------------------ // md5 hash for server side validation of form / link stuff /*-------------------------------------------------------------------------*/ /** * Return MD5 hash for use in forms * * @return string MD5 hash * @since 2.0 */ function return_md5_check() { if ( $this->member['id'] ) { return md5($this->member['email'].'&'.$this->member['member_login_key'].'&'.$this->member['joined']); } else { return md5("this is only here to prevent it breaking on guests"); } } /*-------------------------------------------------------------------------*/ // C.O.C.S (clean old comma-delimeted strings) // ------------------ // <> /*-------------------------------------------------------------------------*/ /** * Remove leading comma from comma delim string * * @param string Input String * @return string Parsed string * @since 2.0 */ function trim_leading_comma($t) { return preg_replace( "/^,/", "", $t ); } /** * Remove trailing comma from comma delim string * * @param string Input String * @return string Parsed string * @since 2.0 */ function trim_trailing_comma($t) { return preg_replace( "/,$/", "", $t ); } /** * Remove dupe commas from comma delim string * * @param string Input String * @return string Parsed string * @since 2.0 */ function clean_comma($t) { return preg_replace( "/,{2,}/", ",", $t ); } /** * Clean perm string (wrapper for comma cleaners) * * @param string Input String * @return string Parsed string * @since 2.0 */ function clean_perm_string($t) { $t = $this->clean_comma($t); $t = $this->trim_leading_comma($t); $t = $this->trim_trailing_comma($t); return $t; } /*-------------------------------------------------------------------------*/ // math_strlen_to_bytes /*-------------------------------------------------------------------------*/ /** * Convert strlen to bytes * * @param integer string length (no chars) * @return integer Bytes * @since 2.0 */ function math_strlen_to_bytes( $strlen=0 ) { $dh = pow(10, 0); return round( $strlen / ( pow(1024, 0) / $dh ) ) / $dh; } /*-------------------------------------------------------------------------*/ // size_format // ------------------ // Give it a byte to eat and it'll return nice stuff! /*-------------------------------------------------------------------------*/ /** * Convert bytes into kb, mb, etc * * @param integer size in bytes * @return string Human size * @since 2.0 */ function size_format($bytes="") { $retval = ""; if ($bytes >= 1048576) { $retval = round($bytes / 1048576 * 100 ) / 100 . $this->lang['sf_mb']; } else if ($bytes >= 1024) { $retval = round($bytes / 1024 * 100 ) / 100 . $this->lang['sf_k']; } else { $retval = $bytes . $this->lang['sf_bytes']; } return $retval; } /*-------------------------------------------------------------------------*/ // print_forum_rules // ------------------ // Checks and prints forum rules (if required) /*-------------------------------------------------------------------------*/ /** * Universal routine for printing forum rules * * @param array Forum data * @return string Formatted rules HTML in wrapper * @since 2.0 */ function print_forum_rules($forum) { $ruleshtml = ""; $rules['fid'] = $forum['id']; if ($forum['show_rules']) { if ( $forum['show_rules'] == 2 ) { if ( $this->vars['forum_cache_minimum'] ) { $tmp = $this->DB->simple_exec_query( array( 'select' => 'rules_title, rules_text', 'from' => 'forums', 'where' => "id=".$forum['id']) ); $rules['title'] = $tmp['rules_title']; $rules['body'] = $tmp['rules_text']; } else { $tmp = $this->DB->simple_exec_query( array( 'select' => 'rules_text', 'from' => 'forums', 'where' => "id=".$forum['id']) ); $rules['body'] = $tmp['rules_text']; $rules['title'] = $forum['rules_title']; } $ruleshtml = $this->compiled_templates['skin_global']->forum_show_rules_full($rules); } else { if ( $this->vars['forum_cache_minimum'] ) { $tmp = $this->DB->simple_exec_query( array( 'select' => 'rules_title', 'from' => 'forums', 'where' => "id=".$forum['id']) ); $rules['title'] = $tmp['rules_title']; } else { $rules['title'] = $forum['rules_title']; } $ruleshtml = $this->compiled_templates['skin_global']->forum_show_rules_link($rules); } } return $ruleshtml; } /*-------------------------------------------------------------------------*/ // // hdl_ban_line() : Get / set ban info // Returns array on get and string on "set" // /*-------------------------------------------------------------------------*/ /** * Get / set member's ban info * * @param array Ban info (unit, timespan, date_end, date_start) * @return mixed * @since 2.0 */ function hdl_ban_line($bline) { if ( is_array( $bline ) ) { // Set ( 'timespan' 'unit' ) $factor = $bline['unit'] == 'd' ? 86400 : 3600; $date_end = time() + ( $bline['timespan'] * $factor ); return time() . ':' . $date_end . ':' . $bline['timespan'] . ':' . $bline['unit']; } else { $arr = array(); list( $arr['date_start'], $arr['date_end'], $arr['timespan'], $arr['unit'] ) = explode( ":", $bline ); return $arr; } } /*-------------------------------------------------------------------------*/ // // check_perms() : Nice little sub to check perms // Returns TRUE if access is allowed, FALSE if not. // /*-------------------------------------------------------------------------*/ /** * Check forum permissions * * @param string Comma delim. forum permission string (2,4,5,6) * @return boolean * @since 2.0 */ function check_perms($forum_perm="") { if ( ! is_array( $this->perm_id_array ) ) { return FALSE; } if ( $forum_perm == "" ) { return FALSE; } else if ( $forum_perm == '*' ) { return TRUE; } else { $forum_perm_array = explode( ",", $forum_perm ); foreach( $this->perm_id_array as $u_id ) { if ( in_array( $u_id, $forum_perm_array ) ) { return TRUE; } } // Still here? Not a match then. return FALSE; } } /*-------------------------------------------------------------------------*/ // // check_perms() : Nice little sub to check perms // Returns TRUE if access is allowed, FALSE if not. // /*-------------------------------------------------------------------------*/ /** * Check forum permissions * * @param string Comma delim. of group IDs (2,4,5,6) * @return string Comma delim of PERM MASK ids * @since 2.1.1 */ function create_perms_from_group( $in_group_ids ) { $out = ""; if ( $in_group_ids == '*' ) { foreach( $this->cache['group_cache'] as $pid => $data ) { if ( ! $data['g_id'] ) { continue; } //----------------------------------------- // Got a perm mask? //----------------------------------------- if ( $data['g_perm_id'] ) { $out .= ',' . $data['g_perm_id']; } } } else if ( $in_group_ids ) { $groups_id = explode( ',', $in_group_ids ); if ( count( $groups_id ) ) { foreach( $groups_id as $pid ) { if ( ! $this->cache['group_cache'][ $pid ]['g_id'] ) { continue; } //----------------------------------------- // Got a perm mask? //----------------------------------------- if ( $this->cache['group_cache'][ $pid ]['g_perm_id'] ) { $out .= ',' . $this->cache['group_cache'][ $pid ]['g_perm_id']; } } } } //----------------------------------------- // Tidy perms_id //----------------------------------------- $out = $this->clean_perm_string( $out ); return $out; } /*-------------------------------------------------------------------------*/ // // do_number_format() : Nice little sub to handle common stuff // /*-------------------------------------------------------------------------*/ /** * Wrapper for number_format * * @param integer Number * @return string Formatted number * @since 2.0 */ function do_number_format($number) { if ($this->vars['number_format'] != 'none') { return number_format($number , 0, '', $this->num_format); } else { return $number; } } /*-------------------------------------------------------------------------*/ // // hdl_forum_read_cookie() // /*-------------------------------------------------------------------------*/ /** * Get / set forum read cookie * * @param integer set / get flag * @return boolean * @since 2.0 */ function hdl_forum_read_cookie($set="") { if ( $set == "" ) { // Get cookie and return array... if ( $fread = $this->my_getcookie('forum_read') ) { $farray = unserialize(stripslashes($fread)); if ( is_array($farray) and count($farray) > 0 ) { foreach( $farray as $id => $stamp ) { $this->forum_read[ intval($id) ] = intval($stamp); } } } return TRUE; } else { // Set cookie... $fread = addslashes(serialize($this->forum_read)); $this->my_setcookie('forum_read', $fread); return TRUE; } } /*-------------------------------------------------------------------------*/ // // Return scaled down image // /*-------------------------------------------------------------------------*/ /** * Scale image * * @param array * @todo Direct to kernel/class_image.php? * @return array * @since 2.0 */ function scale_image($arg) { // max_width, max_height, cur_width, cur_height $ret = array( 'img_width' => $arg['cur_width'], 'img_height' => $arg['cur_height'] ); if ( $arg['cur_width'] > $arg['max_width'] ) { $ret['img_width'] = $arg['max_width']; $ret['img_height'] = ceil( ( $arg['cur_height'] * ( ( $arg['max_width'] * 100 ) / $arg['cur_width'] ) ) / 100 ); $arg['cur_height'] = $ret['img_height']; $arg['cur_width'] = $ret['img_width']; } if ( $arg['cur_height'] > $arg['max_height'] ) { $ret['img_height'] = $arg['max_height']; $ret['img_width'] = ceil( ( $arg['cur_width'] * ( ( $arg['max_height'] * 100 ) / $arg['cur_height'] ) ) / 100 ); } return $ret; } /*-------------------------------------------------------------------------*/ // // Show NORMAL created security image(s)... // /*-------------------------------------------------------------------------*/ /** * Show anti-spam bot GIF image numbers * * @param integer Current number to show * @return void * @since 2.0 */ function show_gif_img($this_number="") { $numbers = array( 0 => 'R0lGODlhCAANAJEAAAAAAP////4BAgAAACH5BAQUAP8ALAAAAAAIAA0AAAIUDH5hiKsOnmqSPjtT1ZdnnjCUqBQAOw==', 1 => 'R0lGODlhCAANAJEAAAAAAP////4BAgAAACH5BAQUAP8ALAAAAAAIAA0AAAIUjAEWyMqoXIprRkjxtZJWrz3iCBQAOw==', 2 => 'R0lGODlhCAANAJEAAAAAAP////4BAgAAACH5BAQUAP8ALAAAAAAIAA0AAAIUDH5hiKubnpPzRQvoVbvyrDHiWAAAOw==', 3 => 'R0lGODlhCAANAJEAAAAAAP////4BAgAAACH5BAQUAP8ALAAAAAAIAA0AAAIVDH5hiKbaHgRyUZtmlPtlfnnMiGUFADs=', 4 => 'R0lGODlhCAANAJEAAAAAAP////4BAgAAACH5BAQUAP8ALAAAAAAIAA0AAAIVjAN5mLDtjFJMRjpj1Rv6v1SHN0IFADs=', 5 => 'R0lGODlhCAANAJEAAAAAAP////4BAgAAACH5BAQUAP8ALAAAAAAIAA0AAAIUhA+Bpxn/DITL1SRjnps63l1M9RQAOw==', 6 => 'R0lGODlhCAANAJEAAAAAAP////4BAgAAACH5BAQUAP8ALAAAAAAIAA0AAAIVjIEYyWwH3lNyrQTbnVh2Tl3N5wQFADs=', 7 => 'R0lGODlhCAANAJEAAAAAAP////4BAgAAACH5BAQUAP8ALAAAAAAIAA0AAAIUhI9pwbztAAwP1napnFnzbYEYWAAAOw==', 8 => 'R0lGODlhCAANAJEAAAAAAP////4BAgAAACH5BAQUAP8ALAAAAAAIAA0AAAIVDH5hiKubHgSPWXoxVUxC33FZZCkFADs=', 9 => 'R0lGODlhCAANAJEAAAAAAP////4BAgAAACH5BAQUAP8ALAAAAAAIAA0AAAIVDA6hyJabnnISnsnybXdS73hcZlUFADs=', ); @header("Content-Type: image/gif"); echo base64_decode($numbers[ $this_number ]); exit(); } /*-------------------------------------------------------------------------*/ // // Show GD created security image... // /*-------------------------------------------------------------------------*/ /** * Show anti-spam bot GD image numbers * * @param string Number string * @return void * @since 2.0 */ function show_gd_img($content="") { $content = ' '. preg_replace( "/(\w)/", "\\1 ", $content ) .' '; @header("Content-Type: image/jpeg"); $tmp_x = 140; $tmp_y = 20; $image_x = 210; $image_y = 65; $circles = 3; if ( $this->vars['gd_version'] == 1 ) { $tmp = imagecreate($tmp_x, $tmp_y); $im = imagecreate($image_x, $image_y); } else { $tmp = imagecreatetruecolor($tmp_x, $tmp_y); $im = imagecreatetruecolor($image_x, $image_y); } $white = ImageColorAllocate($tmp, 255, 255, 255); $black = ImageColorAllocate($tmp, 0, 0, 0); $grey = ImageColorAllocate($tmp, 210, 210, 210 ); imagefill($tmp, 0, 0, $white); for ( $i = 1; $i <= $circles; $i++ ) { $values = array( 0 => rand(0, $tmp_x - 10), 1 => rand(0, $tmp_y - 3), 2 => rand(0, $tmp_x - 10), 3 => rand(0, $tmp_y - 3), 4 => rand(0, $tmp_x - 10), 5 => rand(0, $tmp_y - 3), 6 => rand(0, $tmp_x - 10), 7 => rand(0, $tmp_y - 3), 8 => rand(0, $tmp_x - 10), 9 => rand(0, $tmp_y - 3), 10 => rand(0, $tmp_x - 10), 11 => rand(0, $tmp_y - 3), ); $randomcolor = imagecolorallocate( $tmp, rand(100,255), rand(100,255),rand(100,255) ); imagefilledpolygon($tmp, $values, 6, $randomcolor ); } imagestring($tmp, 5, 0, 2, $content, $black); //----------------------------------------- // Distort by resizing //----------------------------------------- imagecopyresized($im, $tmp, 0, 0, 0, 0, $image_x, $image_y, $tmp_x, $tmp_y); imagedestroy($tmp); $white = ImageColorAllocate($im, 255, 255, 255); $black = ImageColorAllocate($im, 0, 0, 0); $grey = ImageColorAllocate($im, 100, 100, 100 ); $random_pixels = $image_x * $image_y / 10; for ($i = 0; $i < $random_pixels; $i++) { ImageSetPixel($im, rand(0, $image_x), rand(0, $image_y), $black); } $no_x_lines = ($image_x - 1) / 5; for ( $i = 0; $i <= $no_x_lines; $i++ ) { // X lines ImageLine( $im, $i * $no_x_lines, 0, $i * $no_x_lines, $image_y, $grey ); // Diag lines ImageLine( $im, $i * $no_x_lines, 0, ($i * $no_x_lines)+$no_x_lines, $image_y, $grey ); } $no_y_lines = ($image_y - 1) / 5; for ( $i = 0; $i <= $no_y_lines; $i++ ) { ImageLine( $im, 0, $i * $no_y_lines, $image_x, $i * $no_y_lines, $grey ); } ImageJPEG($im); ImageDestroy($im); exit(); } /*-------------------------------------------------------------------------*/ // // Convert newlines to
nl2br is buggy with
on early PHP builds // /*-------------------------------------------------------------------------*/ /** *
Safe nl2br (Buggy on old PHP builds) * * @param string Input text * @return string Parsed text * @since 2.0 */ function my_nl2br($t="") { return str_replace( "\n", "
", $t ); } /*-------------------------------------------------------------------------*/ // // Convert
to newlines // /*-------------------------------------------------------------------------*/ /** *
Safe br2nl * * @param string Input text * @return string Parsed text * @since 2.0 */ function my_br2nl($t="") { $t = preg_replace( "#(?:\n|\r)?
(?:\n|\r)?#", "\n", $t ); $t = preg_replace( "#(?:\n|\r)?
(?:\n|\r)?#" , "\n", $t ); return $t; } /*-------------------------------------------------------------------------*/ // // Creates a profile link if member is a reg. member, else just show name // /*-------------------------------------------------------------------------*/ /** * Create profile link * * @param string User's display name * @param integer User's DB ID * @return string Parsed a href link * @since 2.0 */ function make_profile_link($name, $id="") { if ($id > 0) { return "$name"; } else { return $name; } } /*-------------------------------------------------------------------------*/ // // Redirect using HTTP commands, not a page meta tag. // /*-------------------------------------------------------------------------*/ /** * HTTP redirect with out message screen * * @param string URL to load * @return void * @since 2.0 */ function boink_it($url) { // Ensure &s are taken care of $url = str_replace( "&", "&", $url ); if ($this->vars['header_redirect'] == 'refresh') { @header("Refresh: 0;url=".$url); } else if ($this->vars['header_redirect'] == 'html') { echo(""); exit(); } else { @header("Location: ".$url); } exit(); } /*-------------------------------------------------------------------------*/ // // Create a random 8 character password // /*-------------------------------------------------------------------------*/ /** * Create a random 15 character password * * @todo check if we still need this? * @return string Password * @since 2.0 */ function make_password() { $pass = ""; // Want it random you say, eh? // (enter evil laugh) $unique_id = uniqid( mt_rand(), TRUE ); $prefix = $this->converge->generate_password_salt(); $unique_id .= md5( $prefix ); usleep( mt_rand(15000,1000000) ); // Hmm, wonder how long we slept for mt_srand( (double)microtime()*1000000 ); $new_uniqueid = uniqid( mt_rand(), TRUE ); $final_rand = md5( $unique_id.$new_uniqueid ); mt_srand(); // Wipe out the seed for($i = 0; $i < 15; $i++) { $pass .= $final_rand{ mt_rand(0, 31) }; } return $pass; } /*-------------------------------------------------------------------------*/ // // Generate the appropriate folder icon for a topic // /*-------------------------------------------------------------------------*/ /** * Generate the appropriate folder icon for a topic * * @param array Topic data array * @param string Dot flag * @param integer Last read time * @return string Parsed macro * @since 2.0 */ function folder_icon($topic, $dot="", $last_time) { //----------------------------------------- // Sort dot //----------------------------------------- if ($dot != "") { $dot = "_DOT"; } if ($topic['state'] == 'closed') { return "<{B_LOCKED}>"; } if ($topic['poll_state']) { if ( ! $this->member['id'] ) { return "<{B_POLL_NN".$dot."}>"; } if ($last_time && ($topic['last_vote'] > $last_time )) { return "<{B_POLL".$dot."}>"; } return "<{B_POLL_NN".$dot."}>"; } if ($topic['state'] == 'moved' or $topic['state'] == 'link') { return "<{B_MOVED}>"; } if ( ! $this->member['id'] ) { return "<{B_NORM".$dot."}>"; } if (($topic['posts'] + 1 >= $this->vars['hot_topic']) and ( (isset($last_time) ) && ($topic['last_post'] <= $last_time ))) { return "<{B_HOT_NN".$dot."}>"; } if ($topic['posts'] + 1 >= $this->vars['hot_topic']) { return "<{B_HOT".$dot."}>"; } if ($last_time && ($topic['last_post'] > $last_time)) { return "<{B_NEW".$dot."}>"; } return "<{B_NORM".$dot."}>"; } /*-------------------------------------------------------------------------*/ // text_tidy: // Takes raw text from the DB and makes it all nice and pretty - which also // parses un-HTML'd characters. Use this with caution! /*-------------------------------------------------------------------------*/ /** * Takes raw text from the DB and makes it all nice and pretty - which also * parses un-HTML'd characters. Use this with caution! * * @param string Raw text * @return string Parsed text * @since 2.0 * @todo Check if we still need this * @deprecated Since IPB 2.1 */ function text_tidy($txt = "") { $trans = get_html_translation_table(HTML_ENTITIES); $trans = array_flip($trans); $txt = strtr( $txt, $trans ); $txt = preg_replace( "/\s{2}/" , "  " , $txt ); $txt = preg_replace( "/\r/" , "\n" , $txt ); $txt = preg_replace( "/\t/" , "  " , $txt ); //$txt = preg_replace( "/\\n/" , "\n" , $txt ); return $txt; } /*-------------------------------------------------------------------------*/ // Build up page span links /*-------------------------------------------------------------------------*/ /** * Build up page span links * * @param array Page data * @return string Parsed page links HTML * @since 2.0 */ function build_pagelinks($data) { global $ibforums, $skin_universal; $work = array(); $section = ($data['leave_out'] == "") ? 2 : $data['leave_out']; // Number of pages to show per section( either side of current), IE: 1 ... 4 5 [6] 7 8 ... 10 $use_st = $data['USE_ST'] == "" ? 'st' : $data['USE_ST']; //----------------------------------------- // Get the number of pages //----------------------------------------- if ( $data['TOTAL_POSS'] > 0 ) { $work['pages'] = ceil( $data['TOTAL_POSS'] / $data['PER_PAGE'] ); } $work['pages'] = $work['pages'] ? $work['pages'] : 1; //----------------------------------------- // Set up //----------------------------------------- $work['total_page'] = $work['pages']; $work['current_page'] = $data['CUR_ST_VAL'] > 0 ? ($data['CUR_ST_VAL'] / $data['PER_PAGE']) + 1 : 1; //----------------------------------------- // Next / Previous page linkie poos //----------------------------------------- $previous_link = ""; $next_link = ""; if ( $work['current_page'] > 1 ) { $start = $data['CUR_ST_VAL'] - $data['PER_PAGE']; $previous_link = $this->compiled_templates['skin_global']->pagination_previous_link("{$data['BASE_URL']}&$use_st=$start"); } if ( $work['current_page'] < $work['pages'] ) { $start = $data['CUR_ST_VAL'] + $data['PER_PAGE']; $next_link = $this->compiled_templates['skin_global']->pagination_next_link("{$data['BASE_URL']}&$use_st=$start"); } //----------------------------------------- // Loppy loo //----------------------------------------- if ($work['pages'] > 1) { $work['first_page'] = $this->compiled_templates['skin_global']->pagination_make_jump($work['pages']); for( $i = 0; $i <= $work['pages'] - 1; ++$i ) { $RealNo = $i * $data['PER_PAGE']; $PageNo = $i+1; if ($RealNo == $data['CUR_ST_VAL']) { $work['page_span'] .= $this->compiled_templates['skin_global']->pagination_current_page($PageNo); } else { if ($PageNo < ($work['current_page'] - $section)) { $work['st_dots'] = $this->compiled_templates['skin_global']->pagination_start_dots($data['BASE_URL']); continue; } // If the next page is out of our section range, add some dotty dots! if ($PageNo > ($work['current_page'] + $section)) { $work['end_dots'] = $this->compiled_templates['skin_global']->pagination_end_dots("{$data['BASE_URL']}&$use_st=".($work['pages']-1) * $data['PER_PAGE']); break; } $work['page_span'] .= $this->compiled_templates['skin_global']->pagination_page_link("{$data['BASE_URL']}&$use_st={$RealNo}",$PageNo); } } $work['return'] = $this->compiled_templates['skin_global']->pagination_compile($work['first_page'],$previous_link,$work['st_dots'],$work['page_span'],$work['end_dots'],$next_link,$data['TOTAL_POSS'],$data['PER_PAGE'], $data['BASE_URL']); } else { $work['return'] = $data['L_SINGLE']; } return $work['return']; } /*-------------------------------------------------------------------------*/ // Build the forum jump menu /*-------------------------------------------------------------------------*/ /** * Build \n  "; } return $the_html; } /*-------------------------------------------------------------------------*/ // Clean email /*-------------------------------------------------------------------------*/ /** * Clean email address * * @param string Email address * @return mixed * @since 2.0 */ function clean_email($email = "") { $email = trim($email); $email = str_replace( " ", "", $email ); //----------------------------------------- // Check for more than 1 @ symbol //----------------------------------------- if ( substr_count( $email, '@' ) > 1 ) { return FALSE; } $email = preg_replace( "#[\;\#\n\r\*\'\"<>&\%\!\(\)\{\}\[\]\?\\/\s]#", "", $email ); if ( preg_match( "/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/", $email) ) { return $email; } else { return FALSE; } } /*-------------------------------------------------------------------------*/ // Return a date or '--' if the date is undef. /*-------------------------------------------------------------------------*/ /** * Generate Human formatted date string * Return a date or '--' if the date is undef. * We use the rather nice gmdate function in PHP to synchronise our times * with GMT. This gives us the following choices: * If the user has specified a time offset, we use that. If they haven't set * a time zone, we use the default board time offset (which should automagically * be adjusted to match gmdate. * * @param integer Unix date * @param method LONG, SHORT, JOINED, TINY * @param integer * @param integer * @return string Parsed time * @since 2.0 */ function get_date($date, $method, $norelative=0, $full_relative=0) { if ( ! $date ) { return '--'; } if ( empty($method) ) { $method = 'LONG'; } if ($this->offset_set == 0) { // Save redoing this code for each call, only do once per page load $this->offset = $this->get_time_offset(); if ( $this->vars['time_use_relative'] ) { $this->today_time = gmdate('d,m,Y', ( time() + $this->offset) ); $this->yesterday_time = gmdate('d,m,Y', ( (time() - 86400) + $this->offset) ); } $this->offset_set = 1; } //----------------------------------------- // Full relative? //----------------------------------------- if ( $this->vars['time_use_relative'] == 3 ) { $full_relative = 1; } //----------------------------------------- // FULL Relative //----------------------------------------- if ( $full_relative and ( $norelative != 1 ) ) { $diff = time() - $date; if ( $diff < 3600 ) { if ( $diff < 120 ) { return $this->lang['time_less_minute']; } else { return sprintf( $this->lang['time_minutes_ago'], intval($diff / 60) ); } } else if ( $diff < 7200 ) { return $this->lang['time_less_hour']; } else if ( $diff < 86400 ) { return sprintf( $this->lang['time_hours_ago'], intval($diff / 3600) ); } else if ( $diff < 172800 ) { return $this->lang['time_less_day']; } else if ( $diff < 604800 ) { return sprintf( $this->lang['time_days_ago'], intval($diff / 86400) ); } else if ( $diff < 1209600 ) { return $this->lang['time_less_week']; } else if ( $diff < 3024000 ) { return sprintf( $this->lang['time_weeks_ago'], intval($diff / 604900) ); } else { return gmdate($this->time_options[$method], ($date + $this->offset) ); } } //----------------------------------------- // Yesterday / Today //----------------------------------------- else if ( $this->vars['time_use_relative'] and ( $norelative != 1 ) ) { $this_time = gmdate('d,m,Y', ($date + $this->offset) ); //----------------------------------------- // Use level 2 relative? //----------------------------------------- if ( $this->vars['time_use_relative'] == 2 ) { $diff = time() - $date; if ( $diff < 3600 ) { if ( $diff < 120 ) { return $this->lang['time_less_minute']; } else { return sprintf( $this->lang['time_minutes_ago'], intval($diff / 60) ); } } } //----------------------------------------- // Still here? //----------------------------------------- if ( $this_time == $this->today_time ) { return str_replace( '{--}', $this->lang['time_today'], gmdate($this->vars['time_use_relative_format'], ($date + $this->offset) ) ); } else if ( $this_time == $this->yesterday_time ) { return str_replace( '{--}', $this->lang['time_yesterday'], gmdate($this->vars['time_use_relative_format'], ($date + $this->offset) ) ); } else { return gmdate($this->time_options[$method], ($date + $this->offset) ); } } //----------------------------------------- // Normal //----------------------------------------- else { return gmdate($this->time_options[$method], ($date + $this->offset) ); } } /*-------------------------------------------------------------------------*/ // Returns the time - tick tock, etc /*-------------------------------------------------------------------------*/ /** * Return current TIME (not date) * * @param integer Unix date * @param string PHP date() method * @return string * @since 2.0 */ function get_time($date, $method='G:i') { if ($this->offset_set == 0) { // Save redoing this code for each call, only do once per page load $this->offset = $this->get_time_offset(); $this->offset_set = 1; } return gmdate($method, ($date + $this->offset) ); } /*-------------------------------------------------------------------------*/ // Returns the offset needed and stuff - quite groovy. /*-------------------------------------------------------------------------*/ /** * Calculates the user's time offset * * @return integer * @since 2.0 */ function get_time_offset() { $r = 0; $r = ( ($this->member['time_offset'] != "") ? $this->member['time_offset'] : $this->vars['time_offset'] ) * 3600; if ( $this->vars['time_adjust'] ) { $r += ($this->vars['time_adjust'] * 60); } if ( $this->member['dst_in_use'] ) { $r += 3600; } return $r; } /*-------------------------------------------------------------------------*/ // Converts user's date to GMT unix date /*-------------------------------------------------------------------------*/ /** * Converts user's date to GMT unix date * * @param array array( 'year', 'month', 'day', 'hour', 'minute' ) * @return integer * @since 2.0 */ function convert_local_date_to_unix( $time=array() ) { //----------------------------------------- // Get the local offset //----------------------------------------- $offset = $this->get_time_offset(); $time = gmmktime( intval($time['hour']), intval($time['minute']), 0, intval($time['month']), intval($time['day']), intval($time['year']) ); return $time - $offset; } /*-------------------------------------------------------------------------*/ // Convert unix timestamp into: (no leading zeros) /*-------------------------------------------------------------------------*/ /** * Convert unix timestamp into: (no leading zeros) * * Written into separate function to allow for timezone to be used easily * * @param integer Unix date * @return array array( 'day' => x, 'month' => x, 'year' => x, 'hour' => x, 'minute' => x ); * @since 2.0 */ function unixstamp_to_human( $unix=0 ) { $offset = $this->get_time_offset(); $tmp = gmdate( 'j,n,Y,G,i', $unix + $offset ); list( $day, $month, $year, $hour, $min ) = explode( ',', $tmp ); return array( 'day' => $day, 'month' => $month, 'year' => $year, 'hour' => $hour, 'minute' => $min ); } /*-------------------------------------------------------------------------*/ // My gmmktime() - PHP func seems buggy /*-------------------------------------------------------------------------*/ /** * My gmmktime() - PHP func seems buggy * * * @param integer * @param integer * @param integer * @param integer * @param integer * @param integer * @return integer * @since 2.0 */ function date_gmmktime( $hour=0, $min=0, $sec=0, $month=0, $day=0, $year=0 ) { // Calculate UTC time offset $offset = date( 'Z' ); // Generate server based timestamp $time = mktime( $hour, $min, $sec, $month, $day, $year ); // Calculate DST on / off $dst = intval( date( 'I', $time ) - date( 'I' ) ); return $offset + ($dst * 3600) + $time; } /*-------------------------------------------------------------------------*/ // getdate doesn't work apparently as it doesn't take into account // the offset, even when fed a GMT timestamp. /*-------------------------------------------------------------------------*/ /** * Hand rolled GETDATE method * * getdate doesn't work apparently as it doesn't take into account * the offset, even when fed a GMT timestamp. * * @param integer Unix date * @return array 0, seconds, minutes, hours, mday, wday, mon, year, yday, weekday, month * @since 2.0 */ function date_getgmdate( $gmt_stamp ) { $tmp = gmdate( 'j,n,Y,G,i,s,w,z,l,F', $gmt_stamp ); list( $day, $month, $year, $hour, $min, $seconds, $wday, $yday, $weekday, $fmon ) = explode( ',', $tmp ); if ($wday == 0) { $wday = 6; } else { $wday -= 1; } return array( 0 => $gmt_stamp, "seconds" => $seconds, // Numeric representation of seconds 0 to 59 "minutes" => $min, // Numeric representation of minutes 0 to 59 "hours" => $hour, // Numeric representation of hours 0 to 23 "mday" => $day, // Numeric representation of the day of the month 1 to 31 "wday" => $wday, // Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) "mon" => $month, // Numeric representation of a month 1 through 12 "year" => $year, // A full numeric representation of a year, 4 digits Examples: 1999 or 2003 "yday" => $yday, // Numeric representation of the day of the year 0 through 365 "weekday" => $weekday, // A full textual representation of the day of the week Sunday through Saturday "month" => $fmon, // A full textual representation of a month, such as January or Mar ); } /*-------------------------------------------------------------------------*/ // Sets a cookie, abstract layer allows us to do some checking, etc /*-------------------------------------------------------------------------*/ /** * Set a cookie * * Abstract layer allows us to do some checking, etc * * @param string Cookie name * @param string Cookie value * @param integer Is sticky flag * @return void * @since 2.0 */ function my_setcookie($name, $value = "", $sticky = 1) { if ( $this->no_print_header ) { return; } if ($sticky == 1) { $expires = time() + 60*60*24*365; } $this->vars['cookie_domain'] = $this->vars['cookie_domain'] == "" ? "" : $this->vars['cookie_domain']; $this->vars['cookie_path'] = $this->vars['cookie_path'] == "" ? "/" : $this->vars['cookie_path']; $name = $this->vars['cookie_id'].$name; @setcookie($name, $value, $expires, $this->vars['cookie_path'], $this->vars['cookie_domain']); } /*-------------------------------------------------------------------------*/ // Cookies, cookies everywhere and not a byte to eat. forum_read /*-------------------------------------------------------------------------*/ /** * Get a cookie * * Abstract layer allows us to do some checking, etc * * @param string Cookie name * @return mixed * @since 2.0 */ function my_getcookie($name) { if ( isset($_COOKIE[$this->vars['cookie_id'].$name]) ) { if ( ! in_array( $name, array('topicsread', 'forum_read') ) ) { return $this->parse_clean_value(urldecode($_COOKIE[$this->vars['cookie_id'].$name])); } else { return urldecode($_COOKIE[$this->vars['cookie_id'].$name]); } } else { return FALSE; } } /*-------------------------------------------------------------------------*/ // Makes topics read or forum read cookie safe /*-------------------------------------------------------------------------*/ /** * Makes int based arrays safe * XSS Fix: Ticket: 243603 * Problem with cookies allowing SQL code in keys * * @param array Array * @return array Array (Cleaned) * @since 2.1.4(A) */ function clean_int_array( $array=array() ) { $return = array(); if ( is_array( $array ) and count( $array ) ) { foreach( $array as $k => $v ) { $return[ intval($k) ] = intval($v); } } return $return; } /*-------------------------------------------------------------------------*/ // Makes incoming info "safe" /*-------------------------------------------------------------------------*/ /** * Parse _GET _POST data * * Clean up and unHTML * * @return void * @since Time began */ function parse_incoming() { # THIS NEEDS TO BE HERE! $this->get_magic_quotes = @get_magic_quotes_gpc(); if ( is_array($_GET) ) { while( list($k, $v) = each($_GET) ) { if ( is_array($_GET[$k]) ) { while( list($k2, $v2) = each($_GET[$k]) ) { $this->input[ $this->parse_clean_key($k) ][ $this->parse_clean_key($k2) ] = $this->parse_clean_value($v2); } } else { $this->input[ $this->parse_clean_key($k) ] = $this->parse_clean_value($v); } } } //---------------------------------------- // Overwrite GET data with post data //---------------------------------------- if ( is_array($_POST) ) { while( list($k, $v) = each($_POST) ) { if ( is_array($_POST[$k]) ) { while( list($k2, $v2) = each($_POST[$k]) ) { $this->input[ $this->parse_clean_key($k) ][ $this->parse_clean_key($k2) ] = $this->parse_clean_value($v2); } } else { $this->input[ $this->parse_clean_key($k) ] = $this->parse_clean_value($v); } } } $this->input['request_method'] = strtolower($_SERVER['REQUEST_METHOD']); } /*-------------------------------------------------------------------------*/ // Key Cleaner - ensures no funny business with form elements /*-------------------------------------------------------------------------*/ /** * Clean _GET _POST key * * @param string Key name * @return string Cleaned key name * @since 2.1 */ function parse_clean_key($key) { if ($key == "") { return ""; } $key = htmlspecialchars(urldecode($key)); $key = preg_replace( "/\.\./" , "" , $key ); $key = preg_replace( "/\_\_(.+?)\_\_/" , "" , $key ); $key = preg_replace( "/^([\w\.\-\_]+)$/", "$1", $key ); return $key; } /*-------------------------------------------------------------------------*/ // Clean evil tags /*-------------------------------------------------------------------------*/ /** * Clean possible javascipt codes * * @param string Input * @return string Cleaned Input * @since 2.0 */ function clean_evil_tags( $t ) { $t = preg_replace( "/javascript/i" , "javascript", $t ); $t = preg_replace( "/alert/i" , "alert" , $t ); $t = preg_replace( "/about:/i" , "about:" , $t ); $t = preg_replace( "/onmouseover/i", "onmouseover" , $t ); $t = preg_replace( "/onclick/i" , "onclick" , $t ); $t = preg_replace( "/onload/i" , "onload" , $t ); $t = preg_replace( "/onsubmit/i" , "onsubmit" , $t ); $t = preg_replace( "/vars['strip_space_chr'] ) { $val = str_replace( chr(0xCA), "", $val ); //Remove sneaky spaces } $val = str_replace( "&" , "&" , $val ); $val = str_replace( "" , "-->" , $val ); $val = preg_replace( "/