" . ANONYMOUS;
break;
case 'newestuser':
$sql = "SELECT user_id, username
FROM ".USERS_TABLE."
WHERE user_id <> " . ANONYMOUS . "
ORDER BY user_id DESC
LIMIT 1";
break;
case 'topiccount':
$sql = "SELECT SUM(forum_topics) AS total
FROM ".FORUMS_TABLE;
break;
}
if(!$result = $db->sql_query($sql))
{
return 'ERROR';
}
else
{
$row = $db->sql_fetchrow($result);
if($mode == 'newestuser')
{
return($row);
}
else
{
return($row['total']);
}
}
}
function get_userdata_from_id($userid)
{
global $db;
$sql = "SELECT *
FROM " . USERS_TABLE . "
WHERE user_id = $userid";
if(!$result = $db->sql_query($sql))
{
message_die(GENERAL_ERROR, "Couldn't obtain userdata for id", "", __LINE__, __FILE__, $sql);
}
if($db->sql_numrows($result))
{
$myrow = $db->sql_fetchrowset($result);
return($myrow[0]);
}
else
{
message_die(GENERAL_ERROR, "No userdata for this user_id", "", __LINE__, __FILE__, $sql);
}
}
function get_userdata($username) {
global $db;
$sql = "SELECT *
FROM " . USERS_TABLE . "
WHERE username = '$username'
AND user_id <> " . ANONYMOUS;
if(!$result = $db->sql_query($sql))
{
message_die(GENERAL_ERROR, "Tried obtaining data for a non-existent user", "", __LINE__, __FILE__, $sql);
}
if($db->sql_numrows($result))
{
$myrow = $db->sql_fetchrowset($result);
return($myrow[0]);
}
else
{
message_die(GENERAL_ERROR, "Tried obtaining data for a non-existent user", "", __LINE__, __FILE__, $sql);
}
}
function make_jumpbox()
{
global $lang, $db;
$sql = "SELECT c.cat_id, c.cat_title, c.cat_order
FROM " . CATEGORIES_TABLE . " c, " . FORUMS_TABLE . " f
WHERE f.cat_id = c.cat_id
GROUP BY c.cat_id, c.cat_title, c.cat_order
ORDER BY c.cat_order";
if(!$q_categories = $db->sql_query($sql))
{
message_die(GENERAL_ERROR, "Couldn't obtain category list.", "", __LINE__, __FILE__, $sql);
}
$total_categories = $db->sql_numrows();
if($total_categories)
{
$category_rows = $db->sql_fetchrowset($q_categories);
$limit_forums = "";
$sql = "SELECT *
FROM " . FORUMS_TABLE . "
ORDER BY cat_id, forum_order";
if(!$q_forums = $db->sql_query($sql))
{
message_die(GENERAL_ERROR, "Couldn't obtain forums information.", "", __LINE__, __FILE__, $sql);
}
$total_forums = $db->sql_numrows($q_forums);
$forum_rows = $db->sql_fetchrowset($q_forums);
// $is_auth_ary = auth(AUTH_VIEW, AUTH_LIST_ALL, $userdata);
$boxstring = '';
}
else
{
$boxstring .= '';
}
return($boxstring);
}
//
// Simple version of jumpbox, just lists authed forums
//
function make_forum_select($box_name)
{
global $db, $userdata;
$is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata);
$sql = "SELECT forum_id, forum_name
FROM " . FORUMS_TABLE . "
ORDER BY cat_id, forum_order";
if( !$q_forums = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Couldn't obtain forums information.", "", __LINE__, __FILE__, $sql);
}
$forum_list = "";
while( $row = $db->sql_fetchrow($q_forums) )
{
if( $is_auth_ary[$row['forum_id']]['auth_read'] )
{
$forum_list .= "";
}
}
if( $forum_list == "" )
{
$forum_list .= "\n";
}
else
{
$forum_list = '';
}
return($forum_list);
}
//
// Initialise user settings on page load
function init_userprefs($userdata)
{
global $board_config, $theme, $images, $template, $lang, $phpEx, $phpbb_root_path;
if(!$board_config['override_user_themes'])
{
if( $userdata['user_id'] != ANONYMOUS && isset($userdata['user_theme']) )
{
$theme = setuptheme($userdata['user_theme']);
if($theme == FALSE)
{
$theme = setuptheme($board_config['default_theme']);
}
}
else
{
$theme = setuptheme($board_config['default_theme']);
}
}
else
{
$theme = setuptheme($board_config['default_theme']);
}
if( $userdata['user_id'] != ANONYMOUS )
{
if(!empty($userdata['user_lang']))
{
$board_config['default_lang'] = $userdata['user_lang'];
}
if(!empty($userdata['user_dateformat']))
{
$board_config['default_dateformat'] = $userdata['user_dateformat'];
}
if(isset($userdata['user_timezone']))
{
$board_config['board_timezone'] = $userdata['user_timezone'];
}
if(!empty($userdata['user_template']))
{
$board_config['board_template'] = $userdata['user_template'];
}
}
$template = new Template($phpbb_root_path . "templates/" . $board_config['board_template']);
if( $template )
{
@include($phpbb_root_path . "templates/" . $board_config['board_template'] . "/" . $board_config['board_template'] . ".cfg");
if( !defined("TEMPLATE_CONFIG") )
{
message_die(CRITICAL_ERROR, "Couldn't open " . $board_config['board_template'] . " template config file");
}
}
if(file_exists("language/lang_".$board_config['default_lang'].".".$phpEx) )
{
include($phpbb_root_path . 'language/lang_'.$board_config['default_lang'].'.'.$phpEx);
}
else
{
include($phpbb_root_path . 'language/lang_english.'.$phpEx);
}
return;
}
function setuptheme($theme)
{
global $db;
$sql = "SELECT *
FROM " . THEMES_TABLE . "
WHERE themes_id = $theme";
if(!$result = $db->sql_query($sql))
{
return(FALSE);
}
if(!$myrow = $db->sql_fetchrow($result))
{
return(FALSE);
}
return($myrow);
}
function generate_activation_key()
{
$chars = array(
"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","1","2","3","4","5","6","7","8",
"9","0");
$max_elements = count($chars) - 1;
srand((double)microtime()*1000000);
$act_key = '';
for($i = 0; $i < 8; $i++)
{
$act_key .= $chars[rand(0,$max_elements)];
}
$act_key_md = md5($act_key);
return($act_key_md);
}
function encode_ip($dotquad_ip)
{
$ip_sep = explode(".", $dotquad_ip);
return (sprintf("%02x%02x%02x%02x", $ip_sep[0], $ip_sep[1], $ip_sep[2], $ip_sep[3]));
// return (( $ip_sep[0] * 0xFFFFFF + $ip_sep[0] ) + ( $ip_sep[1] * 0xFFFF + $ip_sep[1] ) + ( $ip_sep[2] * 0xFF + $ip_sep[2] ) + ( $ip_sep[3] ) );
}
function decode_ip($int_ip)
{
$hexipbang = explode(".",chunk_split($int_ip, 2, "."));
return hexdec($hexipbang[0]).".".hexdec($hexipbang[1]).".".hexdec($hexipbang[2]).".".hexdec($hexipbang[3]);
// return sprintf( "%d.%d.%d.%d", ( ( $int_ip >> 24 ) & 0xFF ), ( ( $int_ip >> 16 ) & 0xFF ), ( ( $int_ip >> 8 ) & 0xFF ), ( ( $int_ip ) & 0xFF ) );
}
//
// Create date/time from format and timezone
//
function create_date($format, $gmepoch, $tz)
{
return (@gmdate($format, $gmepoch + (3600 * $tz)));
}
//
// Create a GMT timestamp
//
function get_gmt_ts()
{
$time = @time();
return($time);
}
//
// Pagination routine, generates
// page number sequence
//
function generate_pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE)
{
global $lang;
$total_pages = ceil($num_items/$per_page);
if($total_pages == 1)
{
return "";
}
$on_page = floor($start_item/$per_page) + 1;
$page_string = "";
$this_block_start = ($on_page < 10) ? 1 : floor($on_page/10) * 10;
$this_block_end = ($on_page < 10) ? 9 : $this_block_start + 9;
if($this_block_end > $total_pages)
{
$this_block_end = $total_pages;
}
for($i = $this_block_start; $i <= $this_block_end; $i++)
{
$page_string .= ($i == $on_page) ? "$i" : "$i";
if($i < $this_block_end)
{
$page_string .= ", ";
}
}
if($this_block_start > 1)
{
$page_string_prepend = "";
for($i = 0; $i < $this_block_start; $i += 10)
{
$page_string_prepend .= "" . ( ($i == 0) ? ($i + 1) : $i) . " - " . ($i + 9) . ", ";
}
$page_string = $page_string_prepend . $page_string;
}
if($this_block_end < $total_pages)
{
$page_string_append = ", ";
if(!($total_pages%10))
{
$page_url = append_sid($base_url."&start=".( ( ($this_block_end + 1) * $per_page ) - $per_page ) );
$page_string_append .= "$total_pages";
}
else
{
for($i = $this_block_end + 1; $i < $total_pages; $i += 10)
{
$page_string_append .= "" . ( ($i == 0) ? ($i + 1) : $i) . " - " . ((($i + 9) < $total_pages) ? ($i + 9) : $total_pages) . "";
if($i < $total_pages - 10)
{
$page_string_append .= ", ";
}
}
}
$page_string .= $page_string_append;
}
if($add_prevnext_text)
{
if($on_page > 1)
{
$page_string = " " . $lang['Previous'] . " " . $page_string;
}
if($on_page < $total_pages)
{
$page_string .= " " . $lang['Next'] . "";
}
$page_string = $lang['Goto_page'] . ": " . $page_string;
}
return $page_string;
}
//
// Check to see if the username has been taken, or if it is disallowed.
// Used for registering, changing names, and posting anonymously with a username
//
function validate_username($username)
{
global $db;
switch(SQL_LAYER)
{
// Along with subqueries MySQL also lacks
// a UNION clause which would be very nice here :(
// So we have to use two queries
case 'mysql':
$sql_users = "SELECT u.username, g.group_name
FROM " . USERS_TABLE . " u, " . GROUPS_TABLE . " g, " . USER_GROUP_TABLE . " ug
WHERE ug.user_id = u.user_id
AND g.group_id = ug.group_id
AND ( LOWER(u.username) = '" . strtolower($username) . "'
OR LOWER(g.group_name) = '" . strtolower($username) . "' )";
$sql_disallow = "SELECT disallow_username
FROM " . DISALLOW_TABLE . "
WHERE disallow_username = '$username'";
if($result = $db->sql_query($sql_users))
{
if($db->sql_numrows($result) > 0)
{
return(FALSE);
}
}
if($result = $db->sql_query($sql_disallow))
{
if($db->sql_numrows($result) > 0)
{
return(FALSE);
}
}
break;
default:
$sql = "SELECT u.username, g.group_name
FROM " . USERS_TABLE . " u, " . GROUPS_TABLE . " g, " . USER_GROUP_TABLE . " ug
WHERE ug.user_id = u.user_id
AND g.group_id = ug.group_id
AND ( LOWER(u.username) = '" . strtolower($username) . "'
OR LOWER(g.group_name) = '" . strtolower($username) . "' )
UNION
SELECT disallow_username, NULL
FROM " . DISALLOW_TABLE . "
WHERE disallow_username = '$username'";
if($result = $db->sql_query($sql))
{
if($db->sql_numrows($result) > 0)
{
return(FALSE);
}
}
break;
}
return(TRUE);
}
//
// Synchronise functions for forums/topics
//
function sync($type, $id)
{
global $db;
switch($type)
{
case 'all forums':
$sql = "SELECT forum_id
FROM " . FORUMS_TABLE;
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Could not get forum IDs", "Error", __LINE__, __FILE__, $sql);
}
$rowset = $db->sql_fetchrowset($result);
for($i = 0; $i < count($rowset); $i++)
{
sync("forum", $row[$i]['forum_id']);
}
break;
case 'all topics':
$sql = "SELECT topic_id
FROM " . TOPICS_TABLE;
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Could not get topic ID's", "Error", __LINE__, __FILE__, $sql);
}
$rowset = $db->sql_fetchrowset($result);
for($i = 0; $i < count($rowset); $i++)
{
sync("topic", $row[$i]['topic_id']);
}
break;
case 'forum':
$sql = "SELECT MAX(p.post_id) AS last_post
FROM " . POSTS_TABLE . " p, " . TOPICS_TABLE . " t
WHERE p.forum_id = $id
AND p.topic_id = t.topic_id
AND t.topic_status <> " . TOPIC_MOVED;
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Could not get post ID", "Error", __LINE__, __FILE__, $sql);
}
if( $row = $db->sql_fetchrow($result) )
{
$last_post = ($row['last_post']) ? $row['last_post'] : 0;
}
else
{
$last_post = 0;
}
$sql = "SELECT COUNT(post_id) AS total
FROM " . POSTS_TABLE . "
WHERE forum_id = $id";
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Could not get post count", "Error", __LINE__, __FILE__, $sql);
}
if( $row = $db->sql_fetchrow($result) )
{
$total_posts = ($row['total']) ? $row['total'] : 0;
}
else
{
$total_posts = 0;
}
$sql = "SELECT COUNT(topic_id) AS total
FROM " . TOPICS_TABLE . "
WHERE forum_id = $id";
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Could not get topic count", "Error", __LINE__, __FILE__, $sql);
}
if( $row = $db->sql_fetchrow($result) )
{
$total_topics = ($row['total']) ? $row['total'] : 0;
}
else
{
$total_topics = 0;
}
$sql = "UPDATE " . FORUMS_TABLE . "
SET forum_last_post_id = $last_post, forum_posts = $total_posts, forum_topics = $total_topics
WHERE forum_id = $id";
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Could not update forum $id", "Error", __LINE__, __FILE__, $sql);
}
break;
case 'topic':
$sql = "SELECT MAX(post_id) AS last_post
FROM " . POSTS_TABLE . "
WHERE topic_id = $id";
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Could not get post ID", "Error", __LINE__, __FILE__, $sql);
}
if( $row = $db->sql_fetchrow($result) )
{
$last_post = ($row['last_post']) ? $row['last_post'] : 0;
}
else
{
$last_post = 0;
}
$sql = "SELECT COUNT(post_id) AS total
FROM " . POSTS_TABLE . "
WHERE topic_id = $id";
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Could not get post count", "Error", __LINE__, __FILE__, $sql);
}
if( $row = $db->sql_fetchrow($result) )
{
$total_posts = ($row['total']) ? $row['total'] - 1 : 0;
}
else
{
$total_posts = 0;
}
$sql = "UPDATE " . TOPICS_TABLE . "
SET topic_replies = $total_posts, topic_last_post_id = $last_post
WHERE topic_id = $id";
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Could not update topic $id", "Error", __LINE__, __FILE__, $sql);
}
break;
}
return(TRUE);
}
//
// Pick a language, any language ...
//
function language_select($default, $select_name = "language", $dirname="language/")
{
global $phpEx;
$dir = opendir($dirname);
$lang_select = "";
closedir($dir);
return $lang_select;
}
//
// Pick a template/theme combo, personally recommend
// PSO - Blue but then I would ...
//
function style_select($default_template, $default_theme, $select_name = "style", $dirname = "templates")
{
global $db;
$sql = "SELECT themes_id, template_name, themes_name
FROM " . THEMES_TABLE . "
ORDER BY template_name, themes_id";
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Couldn't query themes table", "", __LINE__, __FILE__, $sql);
}
while( $row = $db->sql_fetchrow($result) )
{
$template_themes[$row['template_name']]['name'][] = $row['themes_name'];
$template_themes[$row['template_name']]['id'][] = $row['themes_id'];
}
$dir = opendir($dirname);
$style_select = "";
closedir($dir);
return($style_select);
}
//
// Pick a timezone
//
function tz_select($default, $select_name = 'timezone')
{
global $sys_timezone;
if(!isset($default))
{
$default == $sys_timezone;
}
$tz_select = "";
return($tz_select);
}
//
// Smilies code ... would this be better tagged on to the end of bbcode.php?
// Probably so and I'll move it before B2
//
function smilies_pass($message)
{
global $db, $board_config;
static $smilies;
if(empty($smilies))
{
$sql = "SELECT code, smile_url
FROM " . SMILIES_TABLE;
if($result = $db->sql_query($sql))
{
$smilies = $db->sql_fetchrowset($result);
}
}
usort($smilies, 'smiley_sort');
for($i = 0; $i < count($smilies); $i++)
{
$orig[] = "'(?<=.\\W|\\W.|^\\W)" . preg_quote($smilies[$i]['code']) . "(?=.\\W|\\W.|\\W$)'i";
$repl[] = '
';
}
if($i > 0)
{
$message = preg_replace($orig, $repl, ' ' . $message . ' ');
$message = substr($message, 1, -1);
}
return($message);
}
function smiley_sort($a, $b)
{
if (strlen($a['code']) == strlen($b['code']))
{
return 0;
}
return (strlen($a['code']) > strlen($b['code'])) ? -1 : 1;
}
//
// Obtain list of naughty words and build preg style replacement arrays for use by the
// calling script, note that the vars are passed as references this just makes it easier
// to return both sets of arrays
//
function obtain_word_list(&$orig_word, &$replacement_word)
{
global $db;
//
// Define censored word matches
//
$sql = "SELECT word, replacement
FROM " . WORDS_TABLE;
if( !$words_result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Couldn't get censored words from database.", "", __LINE__, __FILE__, $sql);
}
else
{
$word_list = $db->sql_fetchrowset($words_result);
$orig_word = array();
$replacement_word = array();
for($i = 0; $i < count($word_list); $i++)
{
$word = str_replace("\*", "\w*?", preg_quote($word_list[$i]['word']));
$orig_word[] = "/\b(" . $word . ")\b/i";
$replacement_word[] = $word_list[$i]['replacement'];
}
}
return(TRUE);
}
?>