2002-07-14 14:32:45 +00:00
< ? php
/***************************************************************************
* session . php
* -------------------
* begin : Saturday , Feb 13 , 2001
* copyright : ( C ) 2002 The phpBB Group
* email : support @ phpbb . com
*
* $Id $
*
***************************************************************************/
/***************************************************************************
*
* 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 ; either version 2 of the License , or
* ( at your option ) any later version .
*
***************************************************************************/
class session {
2002-08-06 16:56:14 +00:00
var $session_id = '' ;
2002-10-04 13:09:10 +00:00
var $browser = '' ;
var $page = '' ;
2002-07-14 14:32:45 +00:00
var $load ;
2002-10-04 13:09:10 +00:00
// Called at each page start ... checks for, updates and/or creates a session
2002-07-14 14:32:45 +00:00
function start ( $update = true )
{
global $SID , $db , $board_config , $user_ip ;
$current_time = time ();
2002-10-04 13:09:10 +00:00
$this -> browser = ( ! empty ( $_SERVER [ 'HTTP_USER_AGENT' ]) ) ? $_SERVER [ 'HTTP_USER_AGENT' ] : $_ENV [ 'HTTP_USER_AGENT' ];
$this -> page = ( ! empty ( $_SERVER [ 'PHP_SELF' ]) ) ? $_SERVER [ 'PHP_SELF' ] : $_ENV [ 'PHP_SELF' ];
$this -> page .= '&' . ( ( ! empty ( $_SERVER [ 'QUERY_STRING' ]) ) ? $_SERVER [ 'QUERY_STRING' ] : $_ENV [ 'QUERY_STRING' ] );
2002-07-14 14:32:45 +00:00
2002-10-04 13:09:10 +00:00
if ( isset ( $_COOKIE [ $board_config [ 'cookie_name' ] . '_sid' ]) || isset ( $_COOKIE [ $board_config [ 'cookie_name' ] . '_data' ]) )
2002-07-14 14:32:45 +00:00
{
2002-10-04 13:09:10 +00:00
$sessiondata = ( isset ( $_COOKIE [ $board_config [ 'cookie_name' ] . '_data' ]) ) ? unserialize ( stripslashes ( $_COOKIE [ $board_config [ 'cookie_name' ] . '_data' ])) : '' ;
$this -> session_id = ( isset ( $_COOKIE [ $board_config [ 'cookie_name' ] . '_sid' ]) ) ? $_COOKIE [ $board_config [ 'cookie_name' ] . '_sid' ] : '' ;
2002-08-06 16:56:14 +00:00
$SID = '?sid=' ;
2002-07-14 14:32:45 +00:00
}
else
{
$sessiondata = '' ;
2002-10-04 13:09:10 +00:00
$this -> session_id = ( isset ( $_GET [ 'sid' ]) ) ? $_GET [ 'sid' ] : '' ;
2002-08-06 16:56:14 +00:00
$SID = '?sid=' . $this -> session_id ;
2002-07-14 14:32:45 +00:00
}
// Load limit check (if applicable)
2002-10-05 11:38:10 +00:00
if ( $board_config [ 'limit_load' ] && file_exists ( '/proc/loadavg' ) )
2002-07-14 14:32:45 +00:00
{
if ( $load = @ file ( '/proc/loadavg' ) )
{
list ( $this -> load ) = explode ( ' ' , $load [ 0 ]);
if ( $this -> load > $board_config [ 'limit_load' ] )
{
message_die ( MESSAGE , 'Board_unavailable' );
}
}
}
2002-08-06 16:56:14 +00:00
// session_id exists so go ahead and attempt to grab all data in preparation
if ( ! empty ( $this -> session_id ) )
2002-07-14 14:32:45 +00:00
{
$sql = " SELECT u.*, s.*
FROM " . SESSIONS_TABLE . " s , " . USERS_TABLE . " u
2002-08-06 16:56:14 +00:00
WHERE s . session_id = '" . $this->session_id . "'
2002-07-14 14:32:45 +00:00
AND u . user_id = s . session_user_id " ;
$result = $db -> sql_query ( $sql );
2002-08-06 16:56:14 +00:00
$userdata = $db -> sql_fetchrow ( $result );
$db -> sql_freeresult ( $result );
2002-07-14 14:32:45 +00:00
// Did the session exist in the DB?
2002-08-06 16:56:14 +00:00
if ( isset ( $userdata [ 'user_id' ]) )
2002-07-14 14:32:45 +00:00
{
2002-10-04 13:09:10 +00:00
// Validate IP length according to admin ... has no effect on IPv6
2002-10-05 00:21:35 +00:00
$s_ip = implode ( '.' , array_slice ( explode ( '.' , $userdata [ 'session_ip' ]), 0 , $board_config [ 'ip_check' ]));
$u_ip = implode ( '.' , array_slice ( explode ( '.' , $user_ip ), 0 , $board_config [ 'ip_check' ]));
2002-10-04 13:09:10 +00:00
if ( $u_ip == $s_ip )
2002-07-14 14:32:45 +00:00
{
// Only update session DB a minute or so after last update or if page changes
2002-08-06 16:56:14 +00:00
if ( ( $current_time - $userdata [ 'session_time' ] > 60 || $userdata [ 'session_page' ] != $user_page ) && $update )
2002-07-14 14:32:45 +00:00
{
2002-08-13 16:34:17 +00:00
$sql = " UPDATE " . SESSIONS_TABLE . "
2002-10-04 13:09:10 +00:00
SET session_time = $current_time , session_page = '$this->page'
2002-08-06 16:56:14 +00:00
WHERE session_id = '" . $this->session_id . "' " ;
2002-07-14 14:32:45 +00:00
$db -> sql_query ( $sql );
}
2002-08-06 16:56:14 +00:00
return $userdata ;
2002-07-14 14:32:45 +00:00
}
}
}
// If we reach here then no (valid) session exists. So we'll create a new one,
// using the cookie user_id if available to pull basic user prefs.
$autologin = ( isset ( $sessiondata [ 'autologinid' ]) ) ? $sessiondata [ 'autologinid' ] : '' ;
2002-07-25 15:18:00 +00:00
$user_id = ( isset ( $sessiondata [ 'userid' ]) ) ? intval ( $sessiondata [ 'userid' ]) : ANONYMOUS ;
2002-07-14 14:32:45 +00:00
2002-10-04 13:09:10 +00:00
return $this -> create ( $user_id , $autologin );
2002-07-14 14:32:45 +00:00
}
2002-08-06 16:56:14 +00:00
// Create a new session
2002-10-04 13:09:10 +00:00
function create ( & $user_id , & $autologin )
2002-07-14 14:32:45 +00:00
{
global $SID , $db , $board_config , $user_ip ;
$sessiondata = array ();
$current_time = time ();
2002-08-07 00:02:08 +00:00
// Limit sessions in 1 minute period
2002-08-13 16:34:17 +00:00
$sql = " SELECT COUNT(*) AS sessions
FROM " . SESSIONS_TABLE . "
2002-08-07 00:02:08 +00:00
WHERE session_time >= " . ( $current_time - 60 );
$result = $db -> sql_query ( $sql );
2002-10-03 02:49:47 +00:00
$row = $db -> sql_fetchrow ( $result );
2002-08-06 16:56:14 +00:00
$db -> sql_freeresult ( $result );
2002-07-14 14:32:45 +00:00
2002-08-07 00:02:08 +00:00
if ( intval ( $board_config [ 'active_sessions' ]) && intval ( $row [ 'sessions' ]) > intval ( $board_config [ 'active_sessions' ]) )
2002-07-14 14:32:45 +00:00
{
2002-08-06 16:56:14 +00:00
message_die ( MESSAGE , 'Board_unavailable' );
2002-07-14 14:32:45 +00:00
}
2002-10-04 13:09:10 +00:00
// Garbage collection ... remove old sessions updating user information
// if necessary. It means (potentially) 22 queries but only infrequently
if ( $current_time - $board_config [ 'session_gc' ] > $board_config [ 'session_last_gc' ] )
{
$this -> gc ( $current_time );
}
2002-10-04 23:37:07 +00:00
// Grab user data ... join on session if it exists for session time
$sql = " SELECT u.*, s.session_time
FROM ( " . USERS_TABLE . " u
LEFT JOIN " . SESSIONS_TABLE . " s ON s . session_user_id = u . user_id )
WHERE u . user_id = $user_id
ORDER BY s . session_time DESC " ;
2002-07-14 14:32:45 +00:00
$result = $db -> sql_query ( $sql );
2002-08-06 16:56:14 +00:00
$userdata = $db -> sql_fetchrow ( $result );
$db -> sql_freeresult ( $result );
2002-07-14 14:32:45 +00:00
// Check autologin request, is it valid?
2002-10-04 23:37:07 +00:00
if ( $userdata [ 'user_password' ] != $autologin || ! $userdata [ 'user_active' ] || ! $user_id )
2002-07-14 14:32:45 +00:00
{
$autologin = '' ;
2002-08-13 16:34:17 +00:00
$userdata [ 'user_id' ] = $user_id = ANONYMOUS ;
2002-07-14 14:32:45 +00:00
}
2002-08-13 16:34:17 +00:00
$sql = " SELECT ban_ip, ban_userid, ban_email
FROM " . BANLIST_TABLE . "
WHERE ban_end >= $current_time
2002-08-06 16:56:14 +00:00
OR ban_end = 0 " ;
2002-07-14 14:32:45 +00:00
$result = $db -> sql_query ( $sql );
2002-08-06 16:56:14 +00:00
if ( $row = $db -> sql_fetchrow ( $result ) )
2002-07-14 14:32:45 +00:00
{
2002-08-06 16:56:14 +00:00
do
2002-07-14 14:32:45 +00:00
{
2002-08-13 16:34:17 +00:00
if ( ( $row [ 'user_id' ] == $userdata [ 'user_id' ] ||
2002-08-06 16:56:14 +00:00
( $row [ 'ban_ip' ] && preg_match ( '#^' . str_replace ( '*' , '.*?' , $row [ 'ban_ip' ]) . '$#i' , $user_ip ) ) ||
2002-08-13 16:34:17 +00:00
( $row [ 'ban_email' ] && preg_match ( '#^' . str_replace ( '*' , '.*?' , $row [ 'ban_email' ]) . '$#i' , $userdata [ 'user_email' ]) ) )
2002-08-06 16:56:14 +00:00
&& ! $userdata [ 'user_founder' ] )
{
message_die ( MESSAGE , 'You_been_banned' );
}
2002-07-14 14:32:45 +00:00
}
2002-08-06 16:56:14 +00:00
while ( $row = $db -> sql_fetchrow ( $result ) );
2002-07-14 14:32:45 +00:00
}
2002-08-06 16:56:14 +00:00
$db -> sql_freeresult ( $result );
2002-07-14 14:32:45 +00:00
2002-10-04 23:37:07 +00:00
// Is there an existing session? If so, grab last visit time from that
$userdata [ 'session_last_visit' ] = ( $userdata [ 'session_time' ] ) ? $userdata [ 'session_time' ] : ( ( $userdata [ 'user_lastvisit' ] ) ? $userdata [ 'user_lastvisit' ] : time () );
2002-07-14 14:32:45 +00:00
// Create or update the session
$db -> sql_return_on_error ( true );
$sql = " UPDATE " . SESSIONS_TABLE . "
2002-10-04 23:37:07 +00:00
SET session_user_id = $user_id , session_last_visit = " . $userdata['session_last_visit'] . " , session_start = $current_time , session_time = $current_time , session_browser = '$this->browser' , session_page = '$this->page'
2002-08-06 16:56:14 +00:00
WHERE session_id = '" . $this->session_id . "' " ;
2002-10-04 23:37:07 +00:00
if ( ! $db -> sql_query ( $sql ) || ! $db -> sql_affectedrows () )
2002-07-14 14:32:45 +00:00
{
$db -> sql_return_on_error ( false );
2002-08-06 16:56:14 +00:00
$this -> session_id = md5 ( uniqid ( $user_ip ));
2002-07-14 14:32:45 +00:00
$sql = " INSERT INTO " . SESSIONS_TABLE . "
2002-10-04 13:09:10 +00:00
( session_id , session_user_id , session_last_visit , session_start , session_time , session_ip , session_browser , session_page )
2002-10-04 23:37:07 +00:00
VALUES ( '" . $this->session_id . "' , $user_id , " . $userdata['session_last_visit'] . " , $current_time , $current_time , '$user_ip' , '$this->browser' , '$this->page' ) " ;
2002-07-14 14:32:45 +00:00
$db -> sql_query ( $sql );
}
$db -> sql_return_on_error ( false );
2002-10-05 11:38:10 +00:00
$userdata [ 'session_id' ] = $this -> session_id ;
2002-07-14 14:32:45 +00:00
2002-10-04 23:37:07 +00:00
$sessiondata [ 'autologinid' ] = ( $autologin && $user_id ) ? $autologin : '' ;
2002-07-14 14:32:45 +00:00
$sessiondata [ 'userid' ] = $user_id ;
2002-10-04 13:09:10 +00:00
$this -> set_cookie ( 'data' , serialize ( $sessiondata ), $current_time + 31536000 );
$this -> set_cookie ( 'sid' , $this -> session_id , 0 );
2002-08-06 16:56:14 +00:00
$SID = '?sid=' . $this -> session_id ;
2002-07-14 14:32:45 +00:00
2002-10-04 13:09:10 +00:00
// Events ...
if ( $userdata [ 'user_id' ] )
{
// do_events();
}
2002-08-06 16:56:14 +00:00
return $userdata ;
2002-07-14 14:32:45 +00:00
}
2002-08-06 16:56:14 +00:00
// Destroy a session
2002-07-14 14:32:45 +00:00
function destroy ( & $userdata )
{
2002-08-06 16:56:14 +00:00
global $SID , $db , $board_config ;
2002-07-14 14:32:45 +00:00
$current_time = time ();
2002-10-04 13:09:10 +00:00
$this -> set_cookie ( 'data' , '' , $current_time - 31536000 );
$this -> set_cookie ( 'sid' , '' , $current_time - 31536000 );
$SID = '?sid=' ;
2002-08-06 16:56:14 +00:00
2002-07-14 14:32:45 +00:00
// Delete existing session, update last visit info first!
2002-08-13 16:34:17 +00:00
$sql = " UPDATE " . USERS_TABLE . "
2002-10-05 11:38:10 +00:00
SET user_lastvisit = " . intval( $userdata['session_time'] ) . "
2002-07-14 14:32:45 +00:00
WHERE user_id = " . $userdata['user_id'] ;
$db -> sql_query ( $sql );
2002-08-13 16:34:17 +00:00
$sql = " DELETE FROM " . SESSIONS_TABLE . "
WHERE session_id = '" . $this->session_id . "'
2002-07-14 14:32:45 +00:00
AND session_user_id = " . $userdata['user_id'] ;
$db -> sql_query ( $sql );
2002-08-06 16:56:14 +00:00
$this -> session_id = '' ;
2002-07-14 14:32:45 +00:00
return true ;
}
2002-08-06 16:56:14 +00:00
// Garbage collection
2002-07-14 14:32:45 +00:00
function gc ( & $current_time )
{
global $db , $board_config , $user_ip ;
2002-10-04 23:37:07 +00:00
// Get expired sessions, only most recent for each user
$sql = " SELECT session_user_id, MAX(session_time) AS recent_time
2002-08-13 16:34:17 +00:00
FROM " . SESSIONS_TABLE . "
WHERE session_time < " . ( $current_time - $board_config['session_length'] ) . "
2002-10-04 23:37:07 +00:00
GROUP BY session_user_id
2002-08-13 16:34:17 +00:00
LIMIT 10 " ;
2002-07-14 14:32:45 +00:00
$result = $db -> sql_query ( $sql );
2002-10-04 23:37:07 +00:00
$del_user_id = '' ;
2002-08-22 17:55:55 +00:00
$del_sessions = 0 ;
2002-07-14 14:32:45 +00:00
while ( $row = $db -> sql_fetchrow ( $result ) )
{
2002-10-05 11:38:10 +00:00
if ( $row [ 'session_user_id' ] )
2002-07-14 14:32:45 +00:00
{
2002-08-13 16:34:17 +00:00
$sql = " UPDATE " . USERS_TABLE . "
2002-10-04 23:37:07 +00:00
SET user_lastvisit = " . $row['recent_time'] . "
2002-07-14 14:32:45 +00:00
WHERE user_id = " . $row['session_user_id'] ;
$db -> sql_query ( $sql );
}
2002-10-05 00:08:47 +00:00
$del_user_id .= ( ( $del_user_id != '' ) ? ', ' : '' ) . ' \'' . $row [ 'session_user_id' ] . '\'' ;
2002-08-22 17:55:55 +00:00
$del_sessions ++ ;
2002-07-14 14:32:45 +00:00
}
2002-10-04 23:54:50 +00:00
if ( $del_user_id != '' )
{
// Delete expired sessions
$sql = " DELETE FROM " . SESSIONS_TABLE . "
WHERE session_user_id IN ( $del_user_id )
AND session_time < " . ( $current_time - $board_config['session_length'] );
$db -> sql_query ( $sql );
}
2002-07-14 14:32:45 +00:00
2002-08-22 17:55:55 +00:00
if ( $del_sessions < 10 )
{
// Less than 10 sessions, update gc timer ... else we want gc
// called again to delete other sessions
$sql = " UPDATE " . CONFIG_TABLE . "
SET config_value = '$current_time'
WHERE config_name = 'session_last_gc' " ;
$db -> sql_query ( $sql );
}
2002-07-14 14:32:45 +00:00
return ;
}
2002-10-05 11:38:10 +00:00
// Set a cookie
function set_cookie ( $name , $cookiedata , $cookietime )
{
global $board_config ;
setcookie ( $board_config [ 'cookie_name' ] . '_' . $name , $cookiedata , $cookietime , $board_config [ 'cookie_path' ], $board_config [ 'cookie_domain' ], $board_config [ 'cookie_secure' ]);
}
2002-10-04 13:09:10 +00:00
// Taken over by user class ... for now at least
2002-07-14 14:32:45 +00:00
function configure ( $userdata , $lang_set = false )
{
global $db , $template , $lang , $board_config , $theme , $images ;
global $phpEx , $phpbb_root_path ;
2002-08-22 21:17:02 +00:00
if ( $userdata [ 'user_id' ] )
2002-07-14 14:32:45 +00:00
{
2002-10-04 13:09:10 +00:00
$board_config [ 'default_lang' ] = ( file_exists ( $phpbb_root_path . 'language/lang_' . $userdata [ 'user_lang' ]) ) ? $userdata [ 'user_lang' ] : $board_config [ 'default_lang' ];
2002-07-14 14:32:45 +00:00
$board_config [ 'default_dateformat' ] = $userdata [ 'user_dateformat' ];
$board_config [ 'board_timezone' ] = $userdata [ 'user_timezone' ];
}
include ( $phpbb_root_path . 'language/lang_' . $board_config [ 'default_lang' ] . '/lang_main.' . $phpEx );
if ( defined ( 'IN_ADMIN' ) )
{
include ( $phpbb_root_path . 'language/lang_' . $board_config [ 'default_lang' ] . '/lang_admin.' . $phpEx );
}
// Set up style
2002-08-22 21:17:02 +00:00
$style = ( ! $board_config [ 'override_user_style' ] && $userdata [ 'user_id' ] ) ? $userdata [ 'user_style' ] : $board_config [ 'default_style' ];
2002-07-14 14:32:45 +00:00
2002-08-13 16:34:17 +00:00
$sql = " SELECT t.template_path, t.poll_length, t.pm_box_length, c.css_data, c.css_external, i.*
FROM " . STYLES_TABLE . " s , " . STYLES_TPL_TABLE . " t , " . STYLES_CSS_TABLE . " c , " . STYLES_IMAGE_TABLE . " i
WHERE s . style_id = $style
AND t . template_id = s . template_id
AND c . theme_id = s . style_id
2002-07-14 14:32:45 +00:00
AND i . imageset_id = s . imageset_id " ;
$result = $db -> sql_query ( $sql );
if ( ! ( $theme = $db -> sql_fetchrow ( $result )) )
{
2002-08-22 21:17:02 +00:00
message_die ( ERROR , 'Could not get style data' );
2002-07-14 14:32:45 +00:00
}
2002-08-22 21:17:02 +00:00
$template -> set_template ( $theme [ 'template_path' ]);
2002-07-14 14:32:45 +00:00
2002-08-22 21:17:02 +00:00
$img_lang = ( file_exists ( 'imageset/' . $theme [ 'imageset_path' ] . '/lang_' . $board_config [ 'default_lang' ]) ) ? $board_config [ 'default_lang' ] : 'english' ;
2002-07-14 14:32:45 +00:00
2002-08-22 21:17:02 +00:00
$i10n = array ( 'post_new' , 'post_locked' , 'post_pm' , 'reply_new' , 'reply_pm' , 'reply_locked' , 'icon_quote' , 'icon_edit' , 'icon_search' , 'icon_profile' , 'icon_pm' , 'icon_email' , 'icon_www' , 'icon_icq' , 'icon_aim' , 'icon_yim' , 'icon_msnm' , 'icon_delete' , 'icon_ip' , 'icon_no_email' , 'icon_no_www' , 'icon_no_icq' , 'icon_no_aim' , 'icon_no_yim' , 'icon_no_msnm' );
2002-10-04 13:09:10 +00:00
foreach ( $i10n as $icon )
2002-08-22 21:17:02 +00:00
{
2002-10-04 13:09:10 +00:00
$theme [ $icon ] = str_replace ( '{LANG}' , 'lang_' . $img_lang , $theme [ $icon ]);
2002-07-14 14:32:45 +00:00
}
return ;
}
}
2002-10-04 13:09:10 +00:00
// Contains (at present) basic user methods such as configuration
// creating date/time ... keep this?
class user
{
var $lang_name ;
var $lang_path ;
var $date_format ;
var $timezone ;
var $dst ;
function user ( & $userdata , $lang_set = false , $style = false )
{
global $db , $template , $lang , $board_config , $theme , $images ;
global $phpEx , $phpbb_root_path ;
if ( $userdata [ 'user_id' ] )
{
$this -> lang_name = ( file_exists ( $phpbb_root_path . 'language/' . $userdata [ 'user_lang' ]) ) ? $userdata [ 'user_lang' ] : $board_config [ 'default_lang' ];
$this -> lang_path = $phpbb_root_path . 'language/' . $this -> lang_name ;
$this -> date_format = $userdata [ 'user_dateformat' ];
$this -> timezone = $userdata [ 'user_timezone' ];
$this -> dst = $userdata [ 'user_dst' ] * 3600 ;
}
else if ( isset ( $_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ]) )
{
$accept_lang_ary = explode ( ',' , $_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ]);
foreach ( $accept_lang_ary as $accept_lang )
{
// Set correct format ... guess full xx_YY form
$accept_lang = substr ( $accept_lang , 0 , 2 ) . '_' . strtoupper ( substr ( $accept_lang , 3 , 2 ));
if ( file_exists ( $phpbb_root_path . 'language/' . $accept_lang ) )
{
$this -> lang_name = $accept_lang ;
$this -> lang_path = $phpbb_root_path . 'language/' . $accept_lang ;
break ;
}
else
{
// No match on xx_YY so try xx
$accept_lang = substr ( $accept_lang , 0 , 2 );
if ( file_exists ( $phpbb_root_path . 'language/' . $accept_lang ) )
{
$this -> lang_name = $accept_lang ;
$this -> lang_path = $phpbb_root_path . 'language/' . $accept_lang ;
break ;
}
}
}
$this -> date_format = $board_config [ 'default_dateformat' ];
$this -> timezone = $board_config [ 'board_timezone' ];
$this -> dst = 0 ;
}
include ( $this -> lang_path . '/lang_main.' . $phpEx );
if ( defined ( 'IN_ADMIN' ) )
{
include ( $this -> lang_path . '/lang_admin.' . $phpEx );
}
2002-10-04 23:37:07 +00:00
/*
if ( is_array ( $lang_set ) )
{
include ( $this -> lang_path . '/common.' . $phpEx );
2002-10-04 13:09:10 +00:00
2002-10-04 23:37:07 +00:00
$lang_set = explode ( ',' , $lang_set );
foreach ( $lang_set as $lang_file )
{
include ( $this -> lang_path . '/' . trim ( $lang_file ) . '.' . $phpEx );
}
unset ( $lang_set );
}
else
{
include ( $this -> lang_path . '/common.' . $phpEx );
include ( $this -> lang_path . '/' . trim ( $lang_set ) . '.' . $phpEx );
}
*/
2002-10-04 13:09:10 +00:00
// Set up style
$style = ( $style ) ? $style : ( ( ! $board_config [ 'override_user_style' ] && $userdata [ 'user_id' ] ) ? $userdata [ 'user_style' ] : $board_config [ 'default_style' ] );
$sql = " SELECT t.template_path, t.poll_length, t.pm_box_length, c.css_data, c.css_external, i.*
FROM " . STYLES_TABLE . " s , " . STYLES_TPL_TABLE . " t , " . STYLES_CSS_TABLE . " c , " . STYLES_IMAGE_TABLE . " i
WHERE s . style_id = $style
AND t . template_id = s . template_id
AND c . theme_id = s . style_id
AND i . imageset_id = s . imageset_id " ;
$result = $db -> sql_query ( $sql );
if ( ! ( $theme = $db -> sql_fetchrow ( $result )) )
{
message_die ( ERROR , 'Could not get style data' );
}
$template -> set_template ( $theme [ 'template_path' ]);
$img_lang = ( file_exists ( 'imageset/' . $theme [ 'imageset_path' ] . '/' . $this -> lang_name ) ) ? $this -> lang_name : $board_config [ 'default_lang' ];
$i10n = array ( 'post_new' , 'post_locked' , 'post_pm' , 'reply_new' , 'reply_pm' , 'reply_locked' , 'icon_quote' , 'icon_edit' , 'icon_search' , 'icon_profile' , 'icon_pm' , 'icon_email' , 'icon_www' , 'icon_icq' , 'icon_aim' , 'icon_yim' , 'icon_msnm' , 'icon_delete' , 'icon_ip' , 'icon_no_email' , 'icon_no_www' , 'icon_no_icq' , 'icon_no_aim' , 'icon_no_yim' , 'icon_no_msnm' );
2002-08-22 17:55:55 +00:00
2002-10-04 13:09:10 +00:00
foreach ( $i10n as $icon )
{
$theme [ $icon ] = str_replace ( '{LANG}' , $img_lang , $theme [ $icon ]);
}
return ;
}
function format_date ( $gmepoch )
{
global $lang ;
static $lang_dates ;
if ( empty ( $lang_dates ) )
{
foreach ( $lang [ 'datetime' ] as $match => $replace )
{
$lang_dates [ $match ] = $replace ;
}
}
return strtr ( @ gmdate ( $this -> date_format , $gmepoch + ( 3600 * $this -> timezone ) + $this -> dst ), $lang_dates );
}
}
2002-08-22 17:55:55 +00:00
2002-08-13 16:34:17 +00:00
// Will be keeping my eye of 'other products' to ensure these things don't
2002-07-14 14:32:45 +00:00
// mysteriously appear elsewhere, think up your own solutions!
2002-10-04 13:09:10 +00:00
class auth
2002-08-13 16:34:17 +00:00
{
2002-08-15 15:45:22 +00:00
var $founder = false ;
2002-08-15 16:31:07 +00:00
var $acl = false ;
2002-08-15 15:45:22 +00:00
function acl ( & $userdata , $forum_id = false , $extra_options = false )
2002-07-14 14:32:45 +00:00
{
global $db ;
2002-08-15 15:45:22 +00:00
if ( ! ( $this -> founder = $userdata [ 'user_founder' ]) )
2002-07-14 14:32:45 +00:00
{
2002-08-17 22:08:34 +00:00
$and_sql = " ao.auth_value LIKE 'forum_list' " ;
2002-07-14 14:32:45 +00:00
2002-08-15 15:45:22 +00:00
if ( $extra_options )
{
$tmp_ary = explode ( ',' , $extra_options );
foreach ( $tmp_ary as $option )
{
2002-08-17 22:08:34 +00:00
$and_sql .= " OR ao.auth_value LIKE ' " . trim ( $option ) . " ' " ;
2002-08-15 15:45:22 +00:00
}
}
2002-08-13 16:34:17 +00:00
2002-08-17 22:08:34 +00:00
$and_sql = ( ! $forum_id ) ? $and_sql : " ( a.forum_id = $forum_id ) OR ( a.forum_id <> $forum_id AND ( ao.auth_value LIKE 'forum_list' OR ao.auth_value LIKE 'mod_%' ) ) " ;
$and_sql .= " OR ao.auth_value LIKE 'admin_%' " ;
2002-08-15 15:45:22 +00:00
2002-08-17 22:08:34 +00:00
$sql = " SELECT a.forum_id, a.auth_allow_deny, ao.auth_value
2002-08-15 15:45:22 +00:00
FROM " . ACL_GROUPS_TABLE . " a , " . ACL_OPTIONS_TABLE . " ao , " . USER_GROUP_TABLE . " ug
WHERE ug . user_id = " . $userdata['user_id'] . "
AND a . group_id = ug . group_id
AND ao . auth_option_id = a . auth_option_id
AND ( $and_sql ) " ;
$result = $db -> sql_query ( $sql );
if ( $row = $db -> sql_fetchrow ( $result ) )
2002-08-13 16:34:17 +00:00
{
2002-08-15 15:45:22 +00:00
do
{
2002-08-17 22:08:34 +00:00
list ( $type , $option ) = explode ( '_' , $row [ 'auth_value' ]);
switch ( $this -> acl [ $row [ 'forum_id' ]][ $type ][ $option ] )
2002-08-15 15:45:22 +00:00
{
case ACL_PERMIT :
case ACL_DENY :
case ACL_PREVENT :
break ;
default :
2002-08-17 22:08:34 +00:00
$this -> acl [ $row [ 'forum_id' ]][ $type ][ $option ] = $row [ 'auth_allow_deny' ];
2002-08-15 15:45:22 +00:00
}
}
while ( $row = $db -> sql_fetchrow ( $result ) );
2002-08-13 16:34:17 +00:00
}
2002-08-15 15:45:22 +00:00
$db -> sql_freeresult ( $result );
2002-08-13 16:34:17 +00:00
2002-08-17 22:08:34 +00:00
$sql = " SELECT a.forum_id, a.auth_allow_deny, ao.auth_value
2002-08-15 15:45:22 +00:00
FROM " . ACL_USERS_TABLE . " a , " . ACL_OPTIONS_TABLE . " ao
WHERE a . user_id = " . $userdata['user_id'] . "
AND ao . auth_option_id = a . auth_option_id
AND ( $and_sql ) " ;
$result = $db -> sql_query ( $sql );
2002-07-14 14:32:45 +00:00
2002-08-15 15:45:22 +00:00
if ( $row = $db -> sql_fetchrow ( $result ) )
2002-07-14 14:32:45 +00:00
{
2002-08-15 15:45:22 +00:00
do
{
2002-08-17 22:08:34 +00:00
list ( $type , $option ) = explode ( '_' , $row [ 'auth_value' ]);
switch ( $this -> acl [ $row [ 'forum_id' ]][ $type ][ $option ] )
2002-08-15 15:45:22 +00:00
{
case ACL_PERMIT :
case ACL_PREVENT :
break ;
default :
2002-08-17 22:08:34 +00:00
$this -> acl [ $row [ 'forum_id' ]][ $type ][ $option ] = $row [ 'auth_allow_deny' ];
2002-08-15 15:45:22 +00:00
break ;
}
}
while ( $row = $db -> sql_fetchrow ( $result ) );
}
$db -> sql_freeresult ( $result );
if ( is_array ( $this -> acl ) )
{
foreach ( $this -> acl as $forum_id => $auth_ary )
{
foreach ( $auth_ary as $type => $option_ary )
{
foreach ( $option_ary as $option => $value )
{
switch ( $value )
{
case ACL_ALLOW :
case ACL_PERMIT :
$this -> acl [ $forum_id ][ $type ][ $option ] = 1 ;
break ;
case ACL_DENY :
case ACL_PREVENT :
$this -> acl [ $forum_id ][ $type ][ $option ] = 0 ;
break ;
}
}
2002-08-18 17:36:01 +00:00
//
// Store max result for type ... used later ... saves time
//
$this -> acl [ $forum_id ][ $type ][ 0 ] = max ( $this -> acl [ $forum_id ][ $type ]);
2002-08-15 15:45:22 +00:00
}
}
2002-07-14 14:32:45 +00:00
}
}
return ;
}
2002-08-15 15:45:22 +00:00
function get_acl ( $forum_id , $auth_main , $auth_type = false )
2002-07-14 14:32:45 +00:00
{
2002-08-18 17:36:01 +00:00
return ( $auth_main && $auth_type ) ? ( ( $this -> founder || $this -> acl [ 0 ][ 'admin' ][ 0 ] ) ? true : $this -> acl [ $forum_id ][ $auth_main ][ $auth_type ] ) : $this -> acl [ $forum_id ][ $auth_main ][ 0 ];
2002-07-14 14:32:45 +00:00
}
function get_acl_admin ( $auth_type = false )
{
2002-08-15 15:45:22 +00:00
return ( $this -> founder ) ? true : $this -> get_acl ( 0 , 'admin' , $auth_type );
2002-08-06 16:56:14 +00:00
}
2002-07-14 14:32:45 +00:00
2002-08-18 17:36:01 +00:00
function set_acl_user ( & $forum_id , & $user_id , & $auth , $dependencies = false )
2002-07-14 14:32:45 +00:00
{
global $db ;
2002-08-01 16:41:04 +00:00
$forum_sql = ( $forum_id ) ? " AND a.forum_id IN ( $forum_id , 0) " : '' ;
2002-08-18 00:33:30 +00:00
$sql = " SELECT o.auth_option_id, a.auth_allow_deny FROM " . ACL_USERS_TABLE . " a, " . ACL_OPTIONS_TABLE . " o, " . USERS_TABLE . " u WHERE a.auth_option_id = o.auth_option_id $forum_sql AND u.user_id = a.user_id AND a.user_id = $user_id " ;
2002-08-01 16:41:04 +00:00
$result = $db -> sql_query ( $sql );
2002-08-13 16:34:17 +00:00
$user_auth = array ();
2002-08-01 16:41:04 +00:00
if ( $row = $db -> sql_fetchrow ( $result ) )
{
do
{
2002-08-18 00:33:30 +00:00
$user_auth [ $user_id ][ $row [ 'auth_option_id' ]] = $row [ 'auth_allow_deny' ];
2002-08-01 16:41:04 +00:00
}
while ( $row = $db -> sql_fetchrow ( $result ) );
}
$db -> sql_freeresult ( $result );
2002-08-18 00:33:30 +00:00
foreach ( $auth as $auth_option_id => $allow )
2002-08-15 15:45:22 +00:00
{
2002-08-18 00:33:30 +00:00
if ( ! empty ( $user_auth ) )
2002-08-15 15:45:22 +00:00
{
2002-08-18 00:33:30 +00:00
foreach ( $user_auth as $user => $user_auth_ary )
2002-08-15 15:45:22 +00:00
{
2002-08-18 00:33:30 +00:00
$sql_ary [] = ( ! isset ( $user_auth_ary [ $auth_option_id ]) ) ? " INSERT INTO " . ACL_USERS_TABLE . " (user_id, forum_id, auth_option_id, auth_allow_deny) VALUES ( $user_id , $forum_id , $auth_option_id , $allow ) " : ( ( $user_auth_ary [ $auth_option_id ] != $allow ) ? " UPDATE " . ACL_USERS_TABLE . " SET auth_allow_deny = $allow WHERE user_id = $user_id AND forum_id = $forum_id AND auth_option_id = $auth_option_id " : '' );
2002-08-15 15:45:22 +00:00
}
}
2002-08-18 00:33:30 +00:00
else
{
$sql_ary [] = " INSERT INTO " . ACL_USERS_TABLE . " (user_id, forum_id, auth_option_id, auth_allow_deny) VALUES ( $user_id , $forum_id , $auth_option_id , $allow ) " ;
}
2002-08-15 15:45:22 +00:00
}
foreach ( $sql_ary as $sql )
{
$db -> sql_query ( $sql );
}
unset ( $user_auth );
unset ( $sql_ary );
}
2002-08-18 17:36:01 +00:00
function set_acl_group ( & $forum_id , & $group_id , & $auth , $dependencies = false )
2002-08-15 15:45:22 +00:00
{
global $db ;
2002-08-18 17:36:01 +00:00
$forum_sql = " AND a.forum_id IN ( $forum_id , 0) " ;
2002-08-15 15:45:22 +00:00
2002-08-18 00:33:30 +00:00
$sql = " SELECT o.auth_option_id, a.auth_allow_deny FROM " . ACL_GROUPS_TABLE . " a, " . ACL_OPTIONS_TABLE . " o WHERE a.auth_option_id = o.auth_option_id $forum_sql AND a.group_id = $group_id " ;
2002-08-01 16:41:04 +00:00
$result = $db -> sql_query ( $sql );
2002-08-13 16:34:17 +00:00
$group_auth = array ();
2002-08-01 16:41:04 +00:00
if ( $row = $db -> sql_fetchrow ( $result ) )
{
do
{
2002-08-18 00:33:30 +00:00
$group_auth [ $group_id ][ $row [ 'auth_option_id' ]] = $row [ 'auth_allow_deny' ];
2002-08-01 16:41:04 +00:00
}
while ( $row = $db -> sql_fetchrow ( $result ) );
}
$db -> sql_freeresult ( $result );
2002-08-18 00:33:30 +00:00
foreach ( $auth as $auth_option_id => $allow )
2002-08-01 16:41:04 +00:00
{
2002-08-18 00:33:30 +00:00
if ( ! empty ( $group_auth ) )
2002-08-01 16:41:04 +00:00
{
2002-08-18 00:33:30 +00:00
foreach ( $group_auth as $group => $group_auth_ary )
2002-08-01 16:41:04 +00:00
{
2002-08-18 00:33:30 +00:00
$sql_ary [] = ( ! isset ( $group_auth_ary [ $auth_option_id ]) ) ? " INSERT INTO " . ACL_GROUPS_TABLE . " (group_id, forum_id, auth_option_id, auth_allow_deny) VALUES ( $group_id , $forum_id , $auth_option_id , $allow ) " : ( ( $group_auth_ary [ $auth_option_id ] != $allow ) ? " UPDATE " . ACL_GROUPS_TABLE . " SET auth_allow_deny = $allow WHERE group_id = $group_id AND forum_id = $forum_id and auth_option_id = $auth_option_id " : '' );
2002-08-01 16:41:04 +00:00
}
}
2002-08-18 00:33:30 +00:00
else
{
$sql_ary [] = " INSERT INTO " . ACL_GROUPS_TABLE . " (group_id, forum_id, auth_option_id, auth_allow_deny) VALUES ( $group_id , $forum_id , $auth_option_id , $allow ) " ;
}
2002-08-01 16:41:04 +00:00
}
2002-08-13 16:34:17 +00:00
foreach ( $sql_ary as $sql )
2002-08-01 16:41:04 +00:00
{
2002-08-13 16:34:17 +00:00
$db -> sql_query ( $sql );
2002-08-01 16:41:04 +00:00
}
2002-08-13 16:34:17 +00:00
unset ( $group_auth );
2002-08-15 15:45:22 +00:00
unset ( $sql_ary );
}
2002-08-18 17:36:01 +00:00
function delete_acl_user ( $forum_id , $user_id , $auth_ids = false )
2002-08-15 15:45:22 +00:00
{
global $db ;
2002-08-18 17:36:01 +00:00
$auth_sql = '' ;
if ( $auth_ids )
{
for ( $i = 0 ; $i < count ( $auth_ids ); $i ++ )
{
$auth_sql .= ( ( $auth_sql != '' ) ? ', ' : '' ) . $auth_ids [ $i ];
}
$auth_sql = " AND auth_option_id IN ( $auth_sql ) " ;
}
2002-08-15 15:45:22 +00:00
$sql = " DELETE FROM " . ACL_USERS_TABLE . "
WHERE user_id = $user_id
2002-08-18 17:36:01 +00:00
AND forum_id = $forum_id
$auth_sql " ;
2002-08-15 15:45:22 +00:00
$db -> sql_query ( $sql );
}
function delete_acl_group ( $forum_id , $group_id , $auth_type = false )
{
global $db ;
2002-08-18 17:36:01 +00:00
$auth_sql = '' ;
if ( $auth_ids )
{
for ( $i = 0 ; $i < count ( $auth_ids ); $i ++ )
{
$auth_sql .= ( ( $auth_sql != '' ) ? ', ' : '' ) . $auth_ids [ $i ];
}
$auth_sql = " AND auth_option_id IN ( $auth_sql ) " ;
}
2002-08-15 15:45:22 +00:00
$sql = " DELETE FROM " . ACL_GROUPS_TABLE . "
WHERE group_id = $group_id
2002-08-18 17:36:01 +00:00
AND forum_id = $forum_id
$auth_sql " ;
2002-08-15 15:45:22 +00:00
$db -> sql_query ( $sql );
2002-07-14 14:32:45 +00:00
}
2002-10-04 13:09:10 +00:00
// Authentication plug-ins is largely down to Sergey Kanareykin, our thanks to him.
2002-08-06 16:56:14 +00:00
function login ( $username , $password , $autologin = false )
{
2002-10-04 13:09:10 +00:00
global $board_config , $session , $phpEx ;
2002-07-14 14:32:45 +00:00
2002-08-06 16:56:14 +00:00
$method = trim ( $board_config [ 'auth_method' ]);
2002-07-14 14:32:45 +00:00
2002-08-06 16:56:14 +00:00
if ( file_exists ( 'includes/auth/auth_' . $method . '.' . $phpEx ) )
2002-07-14 14:32:45 +00:00
{
2002-08-06 16:56:14 +00:00
include_once ( 'includes/auth/auth_' . $method . '.' . $phpEx );
$method = 'login_' . $method ;
if ( function_exists ( $method ) )
2002-07-14 14:32:45 +00:00
{
2002-08-06 16:56:14 +00:00
if ( ! ( $user = $method ( $username , $password )) )
{
return false ;
}
2002-07-14 14:32:45 +00:00
$autologin = ( isset ( $autologin ) ) ? md5 ( $password ) : '' ;
2002-08-06 16:56:14 +00:00
2002-10-04 13:09:10 +00:00
return ( $user [ 'user_active' ] ) ? $session -> create ( $user [ 'user_id' ], $autologin ) : false ;
2002-07-14 14:32:45 +00:00
}
}
2002-08-06 16:56:14 +00:00
message_die ( ERROR , 'Authentication method not found' );
}
2002-07-14 14:32:45 +00:00
}
?>