* copyright : (C) 2001-2019 Advanced Internet Designs Inc.
* email : forum@prohost.org
* 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.
if (function_exists('mb_internal_encoding')) {
mb_internal_encoding('utf-8');
require('./GLOBALS.php');
if (!($FUD_OPT_1 & 1)) { // FORUM_ENABLED
# define('fud_query_stats', 1);
class db { public static $db, $slave; }
// Use MYSQLI_REPORT_OFF so we can check error codes manually.
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_OFF;
if (substr($GLOBALS['DBHOST'], 0, 1) == ':') { // Socket connection.
$socket = substr($GLOBALS['DBHOST'], 1);
$GLOBALS['DBHOST'] = 'localhost';
if ($GLOBALS['FUD_OPT_1'] & 256 && $socket == NULL && version_compare(PHP_VERSION, '5.3.0', '>=')) { // Enable pconnect for PHP 5.3+.
$GLOBALS['DBHOST'] = 'p:'. $GLOBALS['DBHOST'];
db::$db = new mysqli($GLOBALS['DBHOST'], $GLOBALS['DBHOST_USER'], $GLOBALS['DBHOST_PASSWORD'], $GLOBALS['DBHOST_DBNAME'], NULL, $socket);
if (mysqli_connect_errno()) {
fud_sql_error_handler('Failed to establish database connection', 'MySQLi says: '. mysqli_connect_error(), mysqli_connect_errno(), '');
db::$db->set_charset('utf8');
/* Connect to slave, if specified. */
if (!empty($GLOBALS['DBHOST_SLAVE_HOST']) && !$GLOBALS['is_post']) {
db::$slave = new mysqli($GLOBALS['DBHOST'], $GLOBALS['DBHOST_USER'], $GLOBALS['DBHOST_PASSWORD'], $GLOBALS['DBHOST_DBNAME'], NULL, $socket);
if (mysqli_connect_errno()) {
fud_logerror('Unable to init SlaveDB, fallback to MasterDB: '. mysqli_connect_error(), 'sql_errors');
db::$db->set_charset('utf8');
define('__dbtype__', 'mysql');
if (!defined('__FUD_SQL_VERSION__')) {
$ver = q_singleval('SELECT VERSION()');
define('__FUD_SQL_VERSION__', $ver);
return __FUD_SQL_VERSION__;
function db_lock($tables)
if (!empty($GLOBALS['__DB_INC_INTERNALS__']['db_locked'])) {
fud_sql_error_handler('Recursive Lock', 'internal', 'internal', db_version());
q('LOCK TABLES '. $tables);
$GLOBALS['__DB_INC_INTERNALS__']['db_locked'] = 1;
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', db_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', db_version());
unset($GLOBALS['__DB_INC_INTERNALS__']['db_locked']);
return isset($GLOBALS['__DB_INC_INTERNALS__']['db_locked']);
return db::$db->affected_rows;
if (!defined('fud_query_stats')) {
// Assume master DB, route SELECT's to slave DB.
// Force master if DB is locked (in transaction) or 'SELECT /* USE MASTER */'.
if (!empty(db::$slave) && !db_locked() && !strncasecmp($query, 'SELECT', 6) && strncasecmp($query, 'SELECT /* USE MASTER */', 23)) {
fud_sql_error_handler($query, $db->error, $db->errno, db_version());
if (!isset($GLOBALS['__DB_INC_INTERNALS__']['query_count'])) {
$GLOBALS['__DB_INC_INTERNALS__']['query_count'] = 1;
++$GLOBALS['__DB_INC_INTERNALS__']['query_count'];
if (!isset($GLOBALS['__DB_INC_INTERNALS__']['total_sql_time'])) {
$GLOBALS['__DB_INC_INTERNALS__']['total_sql_time'] = 0;
// Assume master DB, route SELECT's to slave DB.
// Force master if DB is locked (in transaction) or 'SELECT /* USE MASTER */'.
if (!empty(db::$slave) && !db_locked() && !strncasecmp($query, 'SELECT', 6) && strncasecmp($query, 'SELECT /* USE MASTER */', 23)) {
$result = $db->query($query);
fud_sql_error_handler($query, $db->error, $db->errno, db_version());
$GLOBALS['__DB_INC_INTERNALS__']['last_time'] = ($e - $s);
$GLOBALS['__DB_INC_INTERNALS__']['total_sql_time'] += $GLOBALS['__DB_INC_INTERNALS__']['last_time'];
echo '<hr><b>Query #'. $GLOBALS['__DB_INC_INTERNALS__']['query_count'] .'</b><small>';
echo ': time taken: <i>'. number_format($GLOBALS['__DB_INC_INTERNALS__']['last_time'], 4) .'</i>';
echo ', affected rows: <i>'. db_affected() .'</i>';
echo ', total sql time: <i>'. number_format($GLOBALS['__DB_INC_INTERNALS__']['total_sql_time'], 4) .'</i>';
echo '<pre>'. preg_replace('!\s+!', ' ', htmlspecialchars($query)) .'</pre></small>';
function db_rowobj($result)
return $result->fetch_object();
function db_rowarr($result)
return $result->fetch_row();
function q_singleval($query)
if (($result = $r->fetch_row()) !== false && isset($result)) {
return isset($result) ? $result[0] : '';
function q_limit($query, $limit, $off=0)
return $query .' LIMIT '. $limit .' OFFSET '. $off;
// MySQL badly breaks the SQL standard by redefining || to mean OR.
return 'CONCAT('. implode(',', $tmp) .')';
q('SET @seq=0'); // For simulating rownum.
function q_bitand($fieldLeft, $fieldRight) {
return $fieldLeft .' & '. $fieldRight;
function q_bitor($fieldLeft, $fieldRight) {
return '('. $fieldLeft .' | '. $fieldRight .')';
function q_bitnot($bitField) {
return $r->fetch_object();
return db::$db->insert_id;
function db_arr_assoc($q)
return $r->fetch_array(MYSQLI_ASSOC);
function db_fetch_array($r)
return is_object($r) ? $r->fetch_array(MYSQLI_ASSOC) : null;
function db_li($q, &$ef, $li=0)
return ($li ? db::$db->insert_id : $r);
if (db::$db->errno == 1062) {
$ef = ltrim(strrchr(db::$db->error, ' '));
fud_sql_error_handler($q, db::$db->error, db::$db->errno, db_version());
function ins_m($tbl, $flds, $types, $vals)
q('INSERT IGNORE INTO '. $tbl .' ('. $flds .') VALUES ('. implode('),(', $vals) .')');
while ($r = $c->fetch_row()) {
return '\''. db::$db->real_escape_string($s) .'\'';
}function ses_make_sysid()
if ($GLOBALS['FUD_OPT_2'] & 256) { // MULTI_HOST_LOGIN
$keys = array('REMOTE_USER', 'HTTP_USER_AGENT', 'SERVER_PROTOCOL', 'HTTP_ACCEPT_CHARSET', 'HTTP_ACCEPT_LANGUAGE');
if ($GLOBALS['FUD_OPT_3'] & 16) { // SESSION_IP_CHECK
$keys[] = 'HTTP_X_FORWARDED_FOR';
if (isset($_SERVER[$v])) {
/* Cookie or URL session? If not, check for known bots. */
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) {
/* Have session string */
$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']);
/* Unknown user, maybe bot? */
// Auto login authorized bots.
// To test: wget --user-agent="Googlebot 1.2" http://127.0.0.1:8080/forum
include $GLOBALS['FORUM_SETTINGS_PATH'] .'spider_cache';
foreach ($spider_cache as $spider_id => $spider) {
if (preg_match('/'. $spider['useragent'] .'/i', $_SERVER['HTTP_USER_AGENT'])) {
if (empty($spider['bot_ip'])) {
$spider_session = 1; // Agent matched, no IPs to check.
foreach (explode(',', $spider['bot_ip']) as $bot_ip) {
if (!($bot_ip = trim($bot_ip))) {
if (strpos($bot_ip, $my_ip) === 0) {
$spider_session = 1; // Agent and an IP matched.
if ($spider['bot_opts'] & 2) { // Access blocked.
if ($id = db_li('INSERT INTO fud30_ses (ses_id, time_sec, sys_id, ip_addr, useragent, user_id) VALUES (\''. $spider['botname'] .'\', '. __request_timestamp__ .', '. _esc(ses_make_sysid()) .', '. _esc($my_ip) .', '. _esc(substr($_SERVER['HTTP_USER_AGENT'], 0, 64)) .', '. $spider['user_id'] .')', $ef, 1)) {
$q_opt = 's.ses_id='. _esc($spider['botname']);
$GLOBALS['FUD_OPT_1'] ^= 128; // Disable URL sessions for user.
/* NeXuS: What is this? Return if user unknown? Function should
return only after the query is run. */
// Check sys_id, ip_addr and useragent for a possible match
$q_opt = 's.sys_id= '._esc(ses_make_sysid()).
' AND s.ip_addr='._esc(get_ip()).
' AND s.useragent='._esc(substr($_SERVER['HTTP_USER_AGENT'], 0, 64));
/* ENABLE_REFERRER_CHECK */
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 '; // Different referrer, force anonymous.
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.posted_msg_count, 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.ban_reason, u.flag_cc
INNER JOIN fud30_users u ON u.id=(CASE WHEN s.user_id>2000000000 THEN 1 ELSE s.user_id END)
LEFT OUTER JOIN fud30_themes t ON t.id=u.theme
/* Anon user, no session or login. */
if (!$u || $u->id == 1 || $id) {
if ($u->sys_id == ses_make_sysid()) {
} else if ($GLOBALS['FUD_OPT_3'] & 16 || isset($url_session)) {
/* URL sessions must validate sys_id check and SESSION_IP_CHECK must be disabled */
/* Try doing a strict SQ match in last-ditch effort to make things 'work'. */
if (isset($_POST['SQ']) && $_POST['SQ'] == $u->sq) {
/** Create an anonymous session. */
$uid = 2000000000 + mt_rand(1, 147483647);
$ses_id = md5($uid . __request_timestamp__ . getmypid());
} while (!($id = db_li('INSERT INTO fud30_ses (ses_id, time_sec, sys_id, ip_addr, useragent, user_id) VALUES (\''. $ses_id .'\', '. __request_timestamp__ .', '. _esc(ses_make_sysid()) .', '. _esc(get_ip()) .', '. _esc(substr($_SERVER['HTTP_USER_AGENT'], 0, 64)) .', '. $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']);
if ($GLOBALS['FUD_OPT_3'] & 1) { // SESSION_COOKIES
setcookie($GLOBALS['COOKIE_NAME'], $ses_id, 0, $GLOBALS['COOKIE_PATH'], $GLOBALS['COOKIE_DOMAIN']);
setcookie($GLOBALS['COOKIE_NAME'], $ses_id, __request_timestamp__+$GLOBALS['COOKIE_TIMEOUT'], $GLOBALS['COOKIE_PATH'], $GLOBALS['COOKIE_DOMAIN']);
/** Update session status to indicate last known action. */
function ses_update_status($ses_id, $str=null, $forum_id=0, $ret='')
die('FATAL ERROR: No session, check your forum\'s URL and COOKIE settings.');
q('UPDATE fud30_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);
/** Save/ clear a session variable. */
function ses_putvar($ses_id, $data)
$cond = is_int($ses_id) ? 'id='. (int)$ses_id : 'ses_id=\''. $ses_id .'\'';
q('UPDATE fud30_ses SET data=NULL WHERE '. $cond);
q('UPDATE fud30_ses SET data='. _esc(serialize($data)) .' WHERE '. $cond);
/** Destroy a session. */
function ses_delete($ses_id)
// Delete all forum sessions.
// Regardless of MULTI_HOST_LOGIN, all sessions will be terminated.
q('DELETE FROM fud30_ses WHERE id='. $ses_id);
setcookie($GLOBALS['COOKIE_NAME'], '', __request_timestamp__-100000, $GLOBALS['COOKIE_PATH'], $GLOBALS['COOKIE_DOMAIN']);
function ses_anonuser_auth($id, $error)
$_SERVER['QUERY_STRING'] = '';
q('UPDATE fud30_ses SET data='. _esc(serialize($error)) .', returnto='. ssn($_SERVER['QUERY_STRING']) .' WHERE id='. $id);
if ($GLOBALS['FUD_OPT_2'] & 32768) { // USE_PATH_INFO
header('Location: [[relativeurl]]/index.php/l/'. _rsidl);
header('Location: [[relativeurl]]/index.php?t=login&'. _rsidl);
$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']) && empty($_GET['t'])) { // USE_PATH_INFO
$pb = $p = explode('/', trim($_SERVER['PATH_INFO'], '/'));
if ($o1 & 128) { // SESSION_USE_URL
$_GET['S'] = array_pop($p);
if ($o2 & 8192) { // TRACK_REFERRALS
$_GET['rid'] = array_pop($p);
$_SERVER['QUERY_STRING'] = htmlspecialchars($_SERVER['PATH_INFO']) .'?'. $_SERVER['QUERY_STRING'];
/* Default to index page. */
/* Notice prevention code. */
for ($i = 1; $i < 5; $i++) {