Multi function library | > Module written by Matt Mecham | > Date started: 14th February 2002 | | > Module Version Number: 1.0.0 +-------------------------------------------------------------------------- */ class FUNC { var $time_formats = array(); var $time_options = array(); var $offset = ""; var $offset_set = 0; // Set up some standards to save CPU later function FUNC() { global $INFO; $this->time_options = array( 'JOINED' => $INFO['clock_joined'], 'SHORT' => $INFO['clock_short'], 'LONG' => $INFO['clock_long'] ); } /*-------------------------------------------------------------------------*/ // // Load a template file from DB or from PHP file // /*-------------------------------------------------------------------------*/ function load_template( $name, $id='' ) { global $ibforums, $DB, $root_path; $tags = 1; if ($ibforums->vars['safe_mode_skins'] == 0) { // Simply require and return require $root_path."Skin/".$ibforums->skin_id."/$name.php"; return new $name(); } else { // We're using safe mode skins, yippee // Load the data from the DB $DB->query("SELECT func_name, func_data, section_content FROM ibf_skin_templates WHERE set_id='".$ibforums->skin_rid."' AND group_name='$name'"); if ( ! $DB->get_num_rows() ) { fatal_error("Could not fetch the templates from the database. Template $name, ID {$ibforums->skin_rid}"); } else { $new_class = "class $name {\n"; while( $row = $DB->fetch_row() ) { if ($tags == 1) { $comment = "\n"; } $new_class .= 'function '.$row['func_name'].'('.$row['func_data'].") {\n"; $new_class .= "global \$ibforums;\n"; $new_class .= 'return << 0) { return "$name"; } else { return $name; } } /*-------------------------------------------------------------------------*/ // // Redirect using HTTP commands, not a page meta tag. // /*-------------------------------------------------------------------------*/ function boink_it($url) { global $ibforums; if ($ibforums->vars['header_redirect'] == 'refresh') { @header("Refresh: 0;url=".$url); } else if ($ibforums->vars['header_redirect'] == 'html') { @flush(); echo(""); exit(); } else { @header("Location: ".$url); } exit(); } /*-------------------------------------------------------------------------*/ // // Create a random 8 character password // /*-------------------------------------------------------------------------*/ function make_password() { $pass = ""; $chars = array( "1","2","3","4","5","6","7","8","9","0", "a","A","b","B","c","C","d","D","e","E","f","F","g","G","h","H","i","I","j","J", "k","K","l","L","m","M","n","N","o","O","p","P","q","Q","r","R","s","S","t","T", "u","U","v","V","w","W","x","X","y","Y","z","Z"); $count = count($chars) - 1; srand((double)microtime()*1000000); for($i = 0; $i < 8; $i++) { $pass .= $chars[rand(0, $count)]; } return($pass); } /*-------------------------------------------------------------------------*/ // // Generate the appropriate folder icon for a forum // /*-------------------------------------------------------------------------*/ function forum_new_posts($forum_data, $sub=0) { global $ibforums, $std; $rtime = $ibforums->input['last_visit']; $fid = $forum_data['fid'] == "" ? $forum_data['id'] : $forum_data['fid']; if ( $ftime = $std->my_getcookie('fread_'.$fid ) ) { $rtime = $ftime > $rtime ? $ftime : $rtime; } if ($sub == 0) { if ( ! $forum_data['status'] ) { return "<{C_LOCKED}>"; } $sub_cat_img = ''; } else { $sub_cat_img = '_CAT'; } if ($forum_data['password'] and $sub == 0) { return $forum_data['last_post'] > $rtime ? "<{C_ON_RES}>" : "<{C_OFF_RES}>"; } return $forum_data['last_post'] > $rtime ? "<{C_ON".$sub_cat_img."}>" : "<{C_OFF".$sub_cat_img."}>"; } /*-------------------------------------------------------------------------*/ // // Generate the appropriate folder icon for a topic // /*-------------------------------------------------------------------------*/ function folder_icon($topic, $dot="", $last_time=-1) { global $ibforums; $last_time = $last_time > $ibforums->input['last_visit'] ? $last_time : $ibforums->input['last_visit']; if ($dot != "") { $dot = "_DOT"; } if ($topic['state'] == 'closed') { return "<{B_LOCKED}>"; } if ($topic['poll_state']) { if ( ! $ibforums->member['id'] ) { return "<{B_POLL".$dot."}>"; } if ($topic['last_post'] > $topic['last_vote']) { $topic['last_vote'] = $topic['last_post']; } if ($last_time && ($topic['last_vote'] > $last_time )) { return "<{B_POLL".$dot."}>"; } if ($last_time && ($topic['last_vote'] < $last_time )) { return "<{B_POLL_NN".$dot."}>"; } return "<{B_POLL}>"; } if ($topic['state'] == 'moved' or $topic['state'] == 'link') { return "<{B_MOVED}>"; } if ( ! $ibforums->member['id'] ) { return "<{B_NORM".$dot."}>"; } if (($topic['posts'] + 1 >= $ibforums->vars['hot_topic']) and ( (isset($last_time) ) && ($topic['last_post'] <= $last_time ))) { return "<{B_HOT_NN".$dot."}>"; } if ($topic['posts'] + 1 >= $ibforums->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! /*-------------------------------------------------------------------------*/ 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; } /*-------------------------------------------------------------------------*/ // compile_db_string: // Takes an array of keys and values and formats them into a string the DB // can use. // $array = ( 'THIS' => 'this', 'THAT' => 'that' ); // will be returned as THIS, THAT 'this', 'that' /*-------------------------------------------------------------------------*/ function compile_db_string($data) { $field_names = ""; $field_values = ""; foreach ($data as $k => $v) { $v = preg_replace( "/'/", "\\'", $v ); $field_names .= "$k,"; $field_values .= "'$v',"; } $field_names = preg_replace( "/,$/" , "" , $field_names ); $field_values = preg_replace( "/,$/" , "" , $field_values ); return array( 'FIELD_NAMES' => $field_names, 'FIELD_VALUES' => $field_values, ); } /*-------------------------------------------------------------------------*/ // Build up page span links /*-------------------------------------------------------------------------*/ function build_pagelinks($data) { $work = array(); $work['pages'] = 1; if ( ($data['TOTAL_POSS'] % $data['PER_PAGE']) == 0 ) { $work['pages'] = $data['TOTAL_POSS'] / $data['PER_PAGE']; } else { $number = ($data['TOTAL_POSS'] / $data['PER_PAGE']); $work['pages'] = ceil( $number); } $work['total_page'] = $work['pages']; $work['current_page'] = $data['CUR_ST_VAL'] > 0 ? ($data['CUR_ST_VAL'] / $data['PER_PAGE']) + 1 : 1; if ($work['pages'] > 1) { $work['first_page'] = "{$data['L_MULTI']} ({$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'] .= " [{$PageNo}]"; } else { if ($PageNo < ($work['current_page'] - 5) and ($work['current_page'] >= 6)) { $work['st_dots'] = ' ...'; continue; } $work['page_span'] .= " {$PageNo}"; if ($PageNo >= ($work['current_page'] + 5)) { $work['end_dots'] = '... '; break; } } } $work['last_page'] = ">"; $work['return'] = $work['first_page'].$work['st_dots'].$work['page_span'].' '.$work['end_dots'].$work['last_page']; } else { $work['return'] = $data['L_SINGLE']; } return $work['return']; } /*-------------------------------------------------------------------------*/ // Build the forum jump menu /*-------------------------------------------------------------------------*/ function build_forum_jump($html=1, $override=0) { global $INFO, $DB, $ibforums; // $html = 0 means don't return the select html stuff // $html = 1 means return the jump menu with select and option stuff $last_cat_id = -1; $DB->query("SELECT f.id as forum_id, f.parent_id, f.subwrap, f.sub_can_post, f.name as forum_name, f.position, f.read_perms, c.id as cat_id, c.name FROM ibf_forums f LEFT JOIN ibf_categories c ON (c.id=f.category) ORDER BY c.position, f.position"); if ($html == 1) { $the_html = "
vars['php_ext']}?s={$ibforums->session_id}&act=SF' method='GET' name='jumpmenu'>" ."\n" ." 
"; } return $the_html; } function clean_email($email = "") { $email = preg_replace( "#[\n\r\*\'\"<>&\%\!\(\)\{\}\[\]\?\\/]#", "", $email ); if ( preg_match( "/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/", $email) ) { return $email; } else { return FALSE; } } /*-------------------------------------------------------------------------*/ // SKIN, sort out the skin stuff /*-------------------------------------------------------------------------*/ function load_skin() { global $ibforums, $INFO, $DB; $id = -1; $skin_set = 0; //------------------------------------------------ // Do we have a skin for a particular forum? //------------------------------------------------ if ($ibforums->input['f'] and $ibforums->input['act'] != 'UserCP') { if ( $ibforums->vars[ 'forum_skin_'.$ibforums->input['f'] ] != "" ) { $id = $ibforums->vars[ 'forum_skin_'.$ibforums->input['f'] ]; $skin_set = 1; } } //------------------------------------------------ // Are we allowing user chooseable skins? //------------------------------------------------ $extra = ""; if ($skin_set != 1 and $ibforums->vars['allow_skins'] == 1) { if (isset($ibforums->input['skinid'])) { $id = $ibforums->input['skinid']; $extra = " AND s.hidden=0"; $skin_set = 1; } else if ( $ibforums->member['skin'] != "" and intval($ibforums->member['skin']) >= 0 ) { $id = $ibforums->member['skin']; if ($id == 'Default') $id = -1; $skin_set = 1; } } //------------------------------------------------ // Load the info from the database. //------------------------------------------------ if ( $id >= 0 and $skin_set == 1) { $DB->query("SELECT s.*, t.template, c.css_text FROM ibf_skins s LEFT JOIN ibf_templates t ON (t.tmid=s.tmpl_id) LEFT JOIN ibf_css c ON (c.cssid=s.css_id) WHERE s.sid=$id".$extra); // Didn't get a row? if (! $DB->get_num_rows() ) { // Update this members profile if ( $ibforums->member['id'] ) { $DB->query("UPDATE ibf_members SET skin='-1' WHERE id='".$ibforums->member['id']."'"); } $DB->query("SELECT s.*, t.template, c.css_text FROM ibf_skins s LEFT JOIN ibf_templates t ON (t.tmid=s.tmpl_id) LEFT JOIN ibf_css c ON (s.css_id=c.cssid) WHERE s.default_set=1"); } } else { $DB->query("SELECT s.*, t.template, c.css_text FROM ibf_skins s LEFT JOIN ibf_templates t ON (t.tmid=s.tmpl_id) LEFT JOIN ibf_css c ON (s.css_id=c.cssid) WHERE s.default_set=1"); } if ( ! $row = $DB->fetch_row() ) { echo("Could not query the skin information!"); exit(); } return $row; } /*-------------------------------------------------------------------------*/ // Require, parse and return an array containing the language stuff /*-------------------------------------------------------------------------*/ function load_words($current_lang_array, $area, $lang_type) { require "./lang/".$lang_type."/".$area.".php"; foreach ($lang as $k => $v) { $current_lang_array[$k] = stripslashes($v); } unset($lang); return $current_lang_array; } /*-------------------------------------------------------------------------*/ // 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. /*-------------------------------------------------------------------------*/ function get_date($date, $method) { global $ibforums; 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 = (($ibforums->member['time_offset'] != "") ? $ibforums->member['time_offset'] : $ibforums->vars['time_offset']) * 3600; if ($ibforums->vars['time_adjust'] != "" and $ibforums->vars['time_adjust'] != 0) { $this->offset += ($ibforums->vars['time_adjust'] * 60); } if ($ibforums->member['dst_in_use']) { $this->offset += 3600; } $this->offset_set = 1; } return gmdate($this->time_options[$method], ($date + $this->offset) ); } /*-------------------------------------------------------------------------*/ // Sets a cookie, abstract layer allows us to do some checking, etc /*-------------------------------------------------------------------------*/ function my_setcookie($name, $value = "", $sticky = 1) { global $INFO; $exipres = ""; if ($sticky == 1) { $expires = time() + 60*60*24*365; } $INFO['cookie_domain'] = $INFO['cookie_domain'] == "" ? "" : $INFO['cookie_domain']; $INFO['cookie_path'] = $INFO['cookie_path'] == "" ? "/" : $INFO['cookie_path']; $name = $INFO['cookie_id'].$name; @setcookie($name, urlencode($value), $expires, $INFO['cookie_path'], $INFO['cookie_domain']); } /*-------------------------------------------------------------------------*/ // Cookies, cookies everywhere and not a byte to eat. /*-------------------------------------------------------------------------*/ function my_getcookie($name) { global $INFO, $HTTP_COOKIE_VARS; if (isset($HTTP_COOKIE_VARS[$INFO['cookie_id'].$name])) { return urldecode($HTTP_COOKIE_VARS[$INFO['cookie_id'].$name]); } else { return FALSE; } } /*-------------------------------------------------------------------------*/ // Makes incoming info "safe" /*-------------------------------------------------------------------------*/ function parse_incoming() { global $HTTP_GET_VARS, $HTTP_POST_VARS, $HTTP_CLIENT_IP, $REQUEST_METHOD, $REMOTE_ADDR, $HTTP_PROXY_USER, $HTTP_X_FORWARDED_FOR; $return = array(); if( is_array($HTTP_GET_VARS) ) { while( list($k, $v) = each($HTTP_GET_VARS) ) { //$k = $this->clean_key($k); if( is_array($HTTP_GET_VARS[$k]) ) { while( list($k2, $v2) = each($HTTP_GET_VARS[$k]) ) { $return[$k][ $this->clean_key($k2) ] = $this->clean_value($v2); } } else { $return[$k] = $this->clean_value($v); } } } // Overwrite GET data with post data if( is_array($HTTP_POST_VARS) ) { while( list($k, $v) = each($HTTP_POST_VARS) ) { //$k = $this->clean_key($k); if ( is_array($HTTP_POST_VARS[$k]) ) { while( list($k2, $v2) = each($HTTP_POST_VARS[$k]) ) { $return[$k][ $this->clean_key($k2) ] = $this->clean_value($v2); } } else { $return[$k] = $this->clean_value($v); } } } // Sort out the accessing IP $return['IP_ADDRESS'] = $this->select_var( array( 1 => $HTTP_X_FORWARDED_FOR, 2 => $HTTP_PROXY_USER, 3 => $REMOTE_ADDR, 4 => $_SERVER['REMOTE_ADDR'] ) ); // Make sure we take a valid IP address $return['IP_ADDRESS'] = preg_replace( "/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/", "\\1.\\2.\\3.\\4", $return['IP_ADDRESS'] ); $return['request_method'] = strtolower($REQUEST_METHOD); return $return; } /*-------------------------------------------------------------------------*/ // Key Cleaner - ensures no funny business with form elements /*-------------------------------------------------------------------------*/ function clean_key($key) { if ($key == "") { return ""; } $key = preg_replace( "/\.\./" , "" , $key ); $key = preg_replace( "/\_\_(.+?)\_\_/" , "" , $key ); $key = preg_replace( "/^([\w\.\-\_]+)$/", "$1", $key ); return $key; } function clean_value($val) { if ($val == "") { return ""; } $val = preg_replace( "/&/" , "&" , $val ); $val = preg_replace( "//" , "-->" , $val ); $val = preg_replace( "/