/** * copyright : (C) 2001-2007 Advanced Internet Designs Inc. * email : forum@prohost.org * $Id: root_index.php.t,v 1.46 2007/01/01 18:23:46 hackie Exp $ * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; version 2 of the License. **/ require('./GLOBALS.php'); #define('fud_query_stats', 1); if (!defined('fud_sql_lnk')) { $connect_func = $GLOBALS['FUD_OPT_1'] & 256 ? 'mysql_pconnect' : 'mysql_connect'; $conn = $connect_func($GLOBALS['DBHOST'], $GLOBALS['DBHOST_USER'], $GLOBALS['DBHOST_PASSWORD']) or die (fud_sql_error_handler("Initiating $connect_func", mysql_error(fud_sql_lnk), mysql_errno(fud_sql_lnk), "Unknown")); define('fud_sql_lnk', $conn); mysql_select_db($GLOBALS['DBHOST_DBNAME'], fud_sql_lnk) or die (fud_sql_error_handler("Opening database ".$GLOBALS['DBHOST_DBNAME'], mysql_error(fud_sql_lnk), mysql_errno(fud_sql_lnk), get_version())); define('__dbtype__', 'mysql'); define('__FUD_SQL_CONCAT__', 'CONCAT'); } function get_version() { if (!defined('__FUD_SQL_VERSION__')) { $ver = mysql_fetch_row(mysql_query('SELECT VERSION()', fud_sql_lnk)); define('__FUD_SQL_VERSION__', $ver[0]); } return __FUD_SQL_VERSION__; } function db_lock($tables) { if (!empty($GLOBALS['__DB_INC_INTERNALS__']['db_locked'])) { fud_sql_error_handler("Recursive Lock", "internal", "internal", get_version()); } else { q('LOCK TABLES '.$tables); $GLOBALS['__DB_INC_INTERNALS__']['db_locked'] = 1; } } function db_unlock() { if (empty($GLOBALS['__DB_INC_INTERNALS__']['db_locked'])) { unset($GLOBALS['__DB_INC_INTERNALS__']['db_locked']); fud_sql_error_handler("DB_UNLOCK: no previous lock established", "internal", "internal", get_version()); } if (--$GLOBALS['__DB_INC_INTERNALS__']['db_locked'] < 0) { unset($GLOBALS['__DB_INC_INTERNALS__']['db_locked']); fud_sql_error_handler("DB_UNLOCK: unlock overcalled", "internal", "internal", get_version()); } unset($GLOBALS['__DB_INC_INTERNALS__']['db_locked']); q('UNLOCK TABLES', fud_sql_lnk); } function db_locked() { return isset($GLOBALS['__DB_INC_INTERNALS__']['db_locked']); } function db_affected() { return mysql_affected_rows(fud_sql_lnk); } if (!defined('fud_query_stats')) { function q($query) { $r = mysql_query($query, fud_sql_lnk) or die (fud_sql_error_handler($query, mysql_error(fud_sql_lnk), mysql_errno(fud_sql_lnk), get_version())); return $r; } function uq($query) { $r = mysql_unbuffered_query($query,fud_sql_lnk) or die (fud_sql_error_handler($query, mysql_error(fud_sql_lnk), mysql_errno(fud_sql_lnk), get_version())); return $r; } } else { function q($query) { if (!isset($GLOBALS['__DB_INC_INTERNALS__']['query_count'])) { $GLOBALS['__DB_INC_INTERNALS__']['query_count'] = 1; } else { ++$GLOBALS['__DB_INC_INTERNALS__']['query_count']; } if (!isset($GLOBALS['__DB_INC_INTERNALS__']['total_sql_time'])) { $GLOBALS['__DB_INC_INTERNALS__']['total_sql_time'] = 0; } $s = gettimeofday(); $result = mysql_query($query, fud_sql_lnk) or die (fud_sql_error_handler($query, mysql_error(fud_sql_lnk), mysql_errno(fud_sql_lnk), get_version())); $e = gettimeofday(); $GLOBALS['__DB_INC_INTERNALS__']['last_time'] = ($e['sec'] - $s['sec'] + (($e['usec'] - $s['usec'])/1000000)); $GLOBALS['__DB_INC_INTERNALS__']['total_sql_time'] += $GLOBALS['__DB_INC_INTERNALS__']['last_time']; $GLOBALS['__DB_INC_INTERNALS__']['last_query'] = $query; echo '
'.preg_replace('!\s+!', ' ', $query).'';
echo 'query count: '.$GLOBALS['__DB_INC_INTERNALS__']['query_count'].' time taken: '.$GLOBALS['__DB_INC_INTERNALS__']['last_time'].''; echo '
Affected rows: '.db_affected().''; echo '
total sql time: '.$GLOBALS['__DB_INC_INTERNALS__']['total_sql_time'].''; return $result; } function uq($query) { return q($query); } } function db_count($result) { return (int) mysql_num_rows($result); } function db_rowobj($result) { return mysql_fetch_object($result); } function db_rowarr($result) { return mysql_fetch_row($result); } { if (($res = mysql_fetch_row(q($query))) !== false) { return $res[0]; } } function qry_limit($limit, $off) { return $off.','.$limit; } function get_fud_table_list($tbl='') { if ($tbl) { } $ret = array(); $c = uq("SHOW TABLES LIKE '".$GLOBALS['DBHOST_TBL_PREFIX']."%'"); while ($r = db_rowarr($c)) { $ret[] = $r[0]; } return $ret; } function optimize_tables($tbl_list=null) { if (!$tbl_list) { $tbl_list = get_fud_table_list(); } q('OPTIMIZE TABLE '. implode(', ', $tbl_list)); } function db_saq($q) { return mysql_fetch_row(q($q)); } function db_sab($q) { return mysql_fetch_object(q($q)); } function db_qid($q) { q($q); return mysql_insert_id(fud_sql_lnk); } function db_arr_assoc($q) { return mysql_fetch_array(q($q), MYSQL_ASSOC); } function db_li($q, &$ef, $li=0) { $r = mysql_query($q, fud_sql_lnk); if ($r) { return ($li ? mysql_insert_id(fud_sql_lnk) : $r); } /* duplicate key */ if (mysql_errno(fud_sql_lnk) == 1062) { $ef = ltrim(strrchr(mysql_error(fud_sql_lnk), ' ')); return null; } else { die(fud_sql_error_handler($q, mysql_error(fud_sql_lnk), mysql_errno(fud_sql_lnk), get_version())); } } function ins_m($tbl, $flds, $vals, $type=0) { q("INSERT IGNORE INTO ".$tbl." (".$flds.") VALUES (".implode('),(', $vals).")"); } function db_all($q) { $f = array(); $c = uq($q); while ($r = mysql_fetch_row($c)) { $f[] = $r[0]; } return $f; } function _esc($s) { return "'".mysql_real_escape_string($s, fud_sql_lnk)."'"; }function ses_make_sysid() { if ($GLOBALS['FUD_OPT_2'] & 256) { return; } $keys = array('HTTP_USER_AGENT', 'SERVER_PROTOCOL', 'HTTP_ACCEPT_CHARSET', 'HTTP_ACCEPT_ENCODING', 'HTTP_ACCEPT_LANGUAGE'); if ($GLOBALS['FUD_OPT_3'] & 16 && !$GLOBALS['is_aol']) { $keys[] = 'HTTP_X_FORWARDED_FOR'; $keys[] = 'REMOTE_ADDR'; } $pfx = ''; foreach ($keys as $v) { if (isset($_SERVER[$v])) { $pfx .= $_SERVER[$v]; } } return md5($pfx); } function ses_get($id=0) { if (!$id) { if (!empty($_COOKIE[$GLOBALS['COOKIE_NAME']])) { $q_opt = "s.ses_id="._esc($_COOKIE[$GLOBALS['COOKIE_NAME']]); } else if ((isset($_GET['S']) || isset($_POST['S'])) && $GLOBALS['FUD_OPT_1'] & 128) { $url_s = 1; $q_opt = "s.ses_id="._esc((isset($_GET['S']) ? (string) $_GET['S'] : (string) $_POST['S'])); /* do not validate against expired URL sessions */ $q_opt .= " AND s.time_sec > ".(__request_timestamp__ - $GLOBALS['SESSION_TIMEOUT']); } else { return; } if ($GLOBALS['FUD_OPT_3'] & 4 && isset($_SERVER['HTTP_REFERER']) && strncmp($_SERVER['HTTP_REFERER'], $GLOBALS['WWW_ROOT'], strlen($GLOBALS['WWW_ROOT']))) { /* more checks, we need those because some proxies mangle referer field */ $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']; /* $p > 8 https:// or http:// */ if (($p = strpos($_SERVER['HTTP_REFERER'], $host)) === false || $p > 8) { $q_opt .= " AND s.user_id > 2000000000 "; } } } else { $q_opt = "s.id='".$id."'"; } $u = db_sab('SELECT s.id AS sid, s.ses_id, s.data, s.returnto, s.sys_id, t.id AS theme_id, t.lang, t.name AS theme_name, t.locale, t.theme, t.pspell_lang, t.theme_opt, u.alias, u.posts_ppg, u.time_zone, u.sig, u.last_visit, u.last_read, u.cat_collapse_status, u.users_opt, u.topics_per_page, u.ignore_list, u.ignore_list, u.buddy_list, u.id, u.group_leader_list, u.email, u.login, u.sq, u.ban_expiry, u.flag_cc FROM fud26_ses s INNER JOIN fud26_users u ON u.id=(CASE WHEN s.user_id>2000000000 THEN 1 ELSE s.user_id END) INNER JOIN fud26_themes t ON t.id=u.theme WHERE '.$q_opt); /* anon user, no session or login */ if (!$u || $u->id == 1 || $id) { return $u; } if ($u->sys_id == ses_make_sysid()) { return $u; } else if ($GLOBALS['FUD_OPT_3'] & 16 || isset($url_s)) { /* URL sessions must validate sys_id check and SESSION_IP_CHECK must be disabled */ return; } /* try doing a strict SQ match in last-ditch effort to make things 'work' */ if (isset($_POST['SQ']) && $_POST['SQ'] == $u->sq) { return $u; } return; } function ses_anon_make() { do { $uid = 2000000000 + mt_rand(1, 147483647); $ses_id = md5($uid . __request_timestamp__ . getmypid()); } while (!($id = db_li("INSERT INTO fud26_ses (ses_id, time_sec, sys_id, user_id) VALUES ('".$ses_id."', ".__request_timestamp__.", '".ses_make_sysid()."', ".$uid.")", $ef, 1))); /* when we have an anon user, we set a special cookie allowing us to see who referred this user */ if (isset($_GET['rid']) && !isset($_COOKIE['frm_referer_id']) && $GLOBALS['FUD_OPT_2'] & 8192) { setcookie($GLOBALS['COOKIE_NAME'].'_referer_id', $_GET['rid'], __request_timestamp__+31536000, $GLOBALS['COOKIE_PATH'], $GLOBALS['COOKIE_DOMAIN']); } setcookie($GLOBALS['COOKIE_NAME'], $ses_id, __request_timestamp__+$GLOBALS['COOKIE_TIMEOUT'], $GLOBALS['COOKIE_PATH'], $GLOBALS['COOKIE_DOMAIN']); return ses_get($id); } function ses_update_status($ses_id, $str=null, $forum_id=0, $ret='') { q('UPDATE fud26_ses SET sys_id=\''.ses_make_sysid().'\', forum_id='.$forum_id.', time_sec='.__request_timestamp__.', action='.($str ? _esc($str) : 'NULL').', returnto='.(!is_int($ret) ? (isset($_SERVER['QUERY_STRING']) ? _esc($_SERVER['QUERY_STRING']) : 'NULL') : 'returnto').' WHERE id='.$ses_id); } function ses_putvar($ses_id, $data) { $cond = is_int($ses_id) ? 'id='.(int)$ses_id : "ses_id='".$ses_id."'"; if (empty($data)) { q('UPDATE fud26_ses SET data=NULL WHERE '.$cond); } else { q("UPDATE fud26_ses SET data="._esc(serialize($data))." WHERE ".$cond); } } function ses_delete($ses_id) { if (!($GLOBALS['FUD_OPT_2'] & 256)) { q('DELETE FROM fud26_ses WHERE id='.$ses_id); } setcookie($GLOBALS['COOKIE_NAME'], '', __request_timestamp__-100000, $GLOBALS['COOKIE_PATH'], $GLOBALS['COOKIE_DOMAIN']); return 1; } function ses_anonuser_auth($id, $error) { if (!empty($_POST)) { $_SERVER['QUERY_STRING'] = ''; } q("UPDATE fud26_ses SET data="._esc(serialize($error)).", returnto=".ssn($_SERVER['QUERY_STRING'])." WHERE id=".$id); if ($GLOBALS['FUD_OPT_2'] & 32768) { header('Location: '.$GLOBALS['WWW_ROOT'].'index.php/l/'._rsidl); } else { header('Location: '.$GLOBALS['WWW_ROOT'].'index.php?t=login&'._rsidl); } exit; }function &init_user() { $o1 =& $GLOBALS['FUD_OPT_1']; $o2 =& $GLOBALS['FUD_OPT_2']; if ($o2 & 32768 && empty($_SERVER['PATH_INFO']) && !empty($_SERVER['ORIG_PATH_INFO'])) { $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO']; } /* we need to parse S & rid right away since they are used during user init */ if ($o2 & 32768 && !empty($_SERVER['PATH_INFO'])) { $pb = $p = explode('/', substr($_SERVER['PATH_INFO'], 1, -1)); if ($o1 & 128) { $_GET['S'] = array_pop($p); } if ($o2 & 8192) { $_GET['rid'] = array_pop($p); } $_SERVER['QUERY_STRING'] = htmlspecialchars($_SERVER['PATH_INFO']) . '?' . $_SERVER['QUERY_STRING']; /* continuation of path info parsing */ if (!isset($p[0])) { $p[0] = 'i'; } /* notice prevention code */ for ($i = 1; $i < 5; $i++) { if (!isset($p[$i])) { $p[$i] = null; } } switch ($p[0]) { case 'm': /* goto specific message */ $_GET['t'] = 0; $_GET['goto'] = $p[1]; if (isset($p[2])) { $_GET['th'] = $p[2]; if (isset($p[3])) { $_GET['start'] = $p[3]; if ($p[3]) { $_GET['t'] = 'msg'; unset($_GET['goto']); } if (isset($p[4])) { if ($p[4] === 'prevloaded') { $_GET['prevloaded'] = 1; $i = 5; } else { $i = 4; } if (isset($p[$i])) { $_GET['rev'] = $p[$i]; if (isset($p[$i+1])) { $_GET['reveal'] = $p[$i+1]; } } } } } break; case 't': /* view thread */ $_GET['t'] = 0; $_GET['th'] = $p[1]; if (isset($p[2])) { $_GET['start'] = $p[2]; if (!empty($p[3])) { $_GET[$p[3]] = 1; } } break; case 'f': /* view forum */ $_GET['t'] = 1; $_GET['frm_id'] = $p[1]; if (isset($p[2])) { $_GET['start'] = $p[2]; if (isset($p[3])) { if ($p[3] === '0') { $_GET['sub'] = 1; } else { $_GET['unsub'] = 1; } } } break; case 'r': $_GET['t'] = 'post'; $_GET[$p[1]] = $p[2]; if (isset($p[3])) { $_GET['reply_to'] = $p[3]; if (isset($p[4])) { if ($p[4]) { $_GET['quote'] = 'true'; } if (isset($p[5])) { $_GET['start'] = $p[5]; } } } break; case 'u': /* view user's info */ $_GET['t'] = 'usrinfo'; $_GET['id'] = $p[1]; break; case 'i': $_GET['t'] = 'index'; if (isset($p[1])) { $_GET['cat'] = (int) $p[1]; } break; case 'fa': $_GET['t'] = 'getfile'; $_GET['id'] = isset($p[1]) ? $p[1] : $pb[1]; if (!empty($p[2])) { $_GET['private'] = 1; } break; case 'sp': /* show posts */ $_GET['t'] = 'showposts'; $_GET['id'] = $p[1]; if (isset($p[2])) { $_GET['so'] = $p[2]; if (isset($p[3])) { $_GET['start'] = $p[3]; } } break; case 'l': /* login/logout */ $_GET['t'] = 'login'; if (isset($p[1])) { $_GET['logout'] = 1; } break; case 'e': $_GET['t'] = 'error'; break; case 'st': $_GET['t'] = $p[1]; $_GET['th'] = $p[2]; $_GET['notify'] = $p[3]; $_GET['opt'] = $p[4] ? 'on' : 'off'; if (isset($p[5])) { $_GET['start'] = $p[5]; } break; case 'sf': $_GET['t'] = $p[1]; $_GET['frm_id'] = $p[2]; $_GET[$p[3]] = 1; $_GET['start'] = $p[4]; break; case 'sl': $_GET['t'] = 'subscribed'; if ($p[1] == 'start') { $_GET['start'] = $p[2]; } else { if (isset($p[2])) { $_GET['th'] = $p[2]; } else if (isset($p[1])) { $_GET['frm_id'] = $p[1]; } } break; case 'pmm': $_GET['t'] = 'ppost'; if (isset($p[1], $p[2])) { $_GET[$p[1]] = $p[2]; if (isset($p[3])) { $_GET['rmid'] = $p[3]; } } break; case 'pmv': $_GET['t'] = 'pmsg_view'; $_GET['id'] = $p[1]; if (isset($p[2])) { $_GET['dr'] = 1; } break; case 'pdm': $_GET['t'] = 'pmsg'; if (isset($p[1])) { if ($p[1] !== 'btn_delete') { $_GET['folder_id'] = $p[1]; if (isset($p[2]) && (int) $p[2]) { $_GET['all'] = 1; } } else { $_GET['btn_delete'] = 1; $_GET['sel'] = $p[2]; } if (isset($p[3])) { $_GET['start'] = $p[3]; if (isset($p[4])) { $_GET['s'] = $p[4]; if (isset($p[5])) { $_GET['a'] = $p[5]; } } } } break; case 'pl': /* poll list */ $_GET['t'] = 'polllist'; if (isset($p[1])) { $_GET['uid'] = $p[1]; if (isset($p[2])) { $_GET['start'] = $p[2]; if (isset($p[3])) { $_GET['oby'] = $p[3]; } } } break; case 'ml': /* member list */ $_GET['t'] = 'finduser'; if (isset($p[1])) { switch ($p[1]) { case 1: case 2: $_GET['pc'] = $p[1]; break; case 3: case 4: $_GET['us'] = $p[1]; break; case 5: case 6: $_GET['rd'] = $p[1]; break; case 7: case 8: $_GET['fl'] = $p[1]; break; } if (isset($p[2])) { $_GET['start'] = $p[2]; if (isset($p[3])) { $_GET['usr_login'] = urldecode($p[3]); if (isset($p[4])) { $_GET['js_redr'] = $p[5]; } } } } break; case 'h': /* help */ $_GET['t'] = 'help_index'; if (isset($p[1])) { $_GET['section'] = $p[1]; } break; case 'cv': /* change thread view mode */ $_GET['t'] = $p[1]; $_GET['frm_id'] = $p[2]; break; case 'mv': /* change message view mode */ $_GET['t'] = $p[1]; $_GET['th'] = $p[2]; if (isset($p[3])) { if ($p[3] !== '0') { $_GET['goto'] = $p[3]; } else { $_GET['prevloaded'] = 1; $_GET['start'] = $p[4]; if (isset($p[5])) { $_GET['rev'] = $p[5]; if (isset($p[6])) { $_GET['reveal'] = $p[6]; } } } } break; case 'pv': $_GET['t'] = 0; if (isset($p[1])) { $_GET['pl_view'] = empty($p[2]) ? 0 : (int)$p[2]; } break; case 'rm': /* report message */ $_GET['t'] = 'report'; $_GET['msg_id'] = $p[1]; break; case 'rl': /* list of reported messages */ $_GET['t'] = 'reported'; if (isset($p[1]))