<commit_before><?php
/**
 * $Id$
 *
 * Default session class.
 *
 * @author <NAME>, 2007-2009 <<EMAIL>>
 */

require_once('session_base.php');
require_once('class.Users.php');

require_once('atom_ip.php');        //for IPv4_to_GeoIP()
require_once('atom_settings.php');  //for storing userdata
require_once('atom_logging.php');   //for logEntry()

class session_default extends session_base
{
	var $session_name = 'someSID'; ///< default session name
	var $timeout = 86400;          ///< 24h - max allowed idle time (in seconds) before session times out and user needs to log in again
	var $online_timeout = 1800;    ///< 30m - max idle time before the user is counted as "logged out" in "users online"-lists etc
	//TODO: make online_timeout configurable

	var $start_page = 'index.php'; ///< redirects user to this page (in $config['app']['web_root'] directory) after successful login
	var $logged_out_start_page = 'index.php';
	var $error_page = 'error.php'; ///< redirects the user to this page (in $config['app']['web_root'] directory) to show errors

	//Aliases of $_SESSION[] variables
	var $error;                 ///< last error message
	var $id;                    ///< current user's user ID
	var $username;              ///< username of current user
	var $mode;                  ///< usermode
	var $lastActive;            ///< last active
	var $started;               ///< timestamp of when the session started
	var $theme = '';            ///< contains the currently selected theme
	var $referer = '';          ///< return to this page after login (if user is browsing a part of the site that is blocked by $this->requireLoggedIn() then logs in)
	var $log_pageviews = false; ///< logs page views to tblPageViews
	var $default_theme = 'default.css';  ///< default theme if none is choosen
	var $allow_themes = false;  ///< allow themes?

	var $isWebmaster;           ///< is user webmaster?
	var $isAdmin;               ///< is user admin?
	var $isSuperAdmin;          ///< is user superadmin?

	var $userModes = array(
		0 => 'Normal user',
		1 => 'Webmaster',
		2 => 'Admin',
		3 => 'Super admin'
	); ///< user modes

	function __construct($conf = array())
	{
		if (isset($conf['name'])) $this->session_name = $conf['name'];
		if (isset($conf['timeout'])) $this->timeout = $conf['timeout'];
		if (isset($conf['start_page'])) $this->start_page = $conf['start_page'];
		if (isset($conf['error_page'])) $this->error_page = $conf['error_page'];
		if (isset($conf['allow_themes'])) $this->allow_themes = $conf['allow_themes'];

		//ini_set('session.gc_probability', 1);
		//ini_set('session.gc_divisor', 1);

		//disable garbage collector to work around ubuntu/debian bug, see:
		//   http://forum.kohanaphp.com/comments.php?DiscussionID=565
		ini_set('session.gc_probability', 0);

		ini_set('session.gc_maxlifetime', $this->timeout);

		session_name($this->session_name);
		session_start();

		if (!isset($_SESSION['started']) || !$_SESSION['started']) $_SESSION['started'] = time();
		if (!isset($_SESSION['error'])) $_SESSION['error'] = '';
		if (!isset($_SESSION['ip'])) $_SESSION['ip'] = 0;
		if (!isset($_SESSION['id'])) $_SESSION['id'] = 0;
		if (!isset($_SESSION['username'])) $_SESSION['username'] = '';
		if (!isset($_SESSION['mode'])) $_SESSION['mode'] = 0;
		if (!isset($_SESSION['lastActive'])) $_SESSION['lastActive'] = 0;
		if (!isset($_SESSION['isWebmaster'])) $_SESSION['isWebmaster'] = 0;
		if (!isset($_SESSION['isAdmin'])) $_SESSION['isAdmin'] = 0;
		if (!isset($_SESSION['isSuperAdmin'])) $_SESSION['isSuperAdmin'] = 0;
		if (!isset($_SESSION['theme'])) $_SESSION['theme'] = $this->default_theme;
		if (!isset($_SESSION['referer'])) $_SESSION['referer'] = '';

		$this->started = &$_SESSION['started'];
		$this->error = &$_SESSION['error'];
		$this->ip = &$_SESSION['ip'];
		$this->id = &$_SESSION['id'];	//if id is set, also means that the user is logged in
		$this->username = &$_SESSION['username'];
		$this->mode = &$_SESSION['mode'];
		$this->lastActive = &$_SESSION['lastActive'];
		$this->isWebmaster = &$_SESSION['isWebmaster'];
		$this->isAdmin = &$_SESSION['isAdmin'];
		$this->isSuperAdmin = &$_SESSION['isSuperAdmin'];
		$this->theme = &$_SESSION['theme'];
		$this->referer = &$_SESSION['referer'];

		if (!$this->ip && !empty($_SERVER['REMOTE_ADDR'])) {	//FIXME map to $this->auth->ip
			$this->ip = IPv4_to_GeoIP($_SERVER['REMOTE_ADDR']);
		}
	}

	/**
	 * Sets up a session. Called from the auth class
	 *
	 * @param $_id user id
	 * @param $_username user name
	 * @param $_usermode user mode
	 */
	function start($_id, $_username, $_usermode)
	{
		global $config;
		$this->id = $_id;
		$this->username = $_username;
		$this->mode = $_usermode;		//0=normal user. 1=webmaster, 2=admin, 3=super admin
		$this->lastActive = time();

		if ($this->mode >= USERLEVEL_WEBMASTER) $this->isWebmaster = true;
		if ($this->mode >= USERLEVEL_ADMIN) $this->isAdmin = true;
		if ($this->mode >= USERLEVEL_SUPERADMIN) $this->isSuperAdmin = true;
	}

	/**
	 * Kills the current session, clearing all session variables
	 */
	function end()
	{
		$this->started = 0;
		$this->username = '';
		$this->ip = 0;
		$this->mode = 0;
		$this->isWebmaster = false;
		$this->isAdmin = false;
		$this->isSuperAdmin = false;
		$this->theme = $this->default_theme;
		$this->referer = '';

		if (!$this->id) return;

		$this->id = 0;
	}

	/**
	 * Redirects the user to a different page
	 */
	function redirect($path)
	{
		header('Location: '.$path);
		die;
	}

	/**
	 * Redirects user to default start page (logged in)
	 */
	function startPage()
	{
		if (!empty($this->referer))<commit_msg>session->redirect use js to redirect user if headers are already sent<commit_after><?php
/**
 * $Id$
 *
 * Default session class.
 *
 * @author <NAME>, 2007-2009 <<EMAIL>>
 */

require_once('session_base.php');
require_once('class.Users.php');

require_once('atom_ip.php');        //for IPv4_to_GeoIP()
require_once('atom_settings.php');  //for storing userdata
require_once('atom_logging.php');   //for logEntry()

class session_default extends session_base
{
	var $session_name = 'someSID'; ///< default session name
	var $timeout = 86400;          ///< 24h - max allowed idle time (in seconds) before session times out and user needs to log in again
	var $online_timeout = 1800;    ///< 30m - max idle time before the user is counted as "logged out" in "users online"-lists etc
	//TODO: make online_timeout configurable

	var $start_page = 'index.php'; ///< redirects user to this page (in $config['app']['web_root'] directory) after successful login
	var $logged_out_start_page = 'index.php';
	var $error_page = 'error.php'; ///< redirects the user to this page (in $config['app']['web_root'] directory) to show errors

	//Aliases of $_SESSION[] variables
	var $error;                 ///< last error message
	var $id;                    ///< current user's user ID
	var $username;              ///< username of current user
	var $mode;                  ///< usermode
	var $lastActive;            ///< last active
	var $started;               ///< timestamp of when the session started
	var $theme = '';            ///< contains the currently selected theme
	var $referer = '';          ///< return to this page after login (if user is browsing a part of the site that is blocked by $this->requireLoggedIn() then logs in)
	var $log_pageviews = false; ///< logs page views to tblPageViews
	var $default_theme = 'default.css';  ///< default theme if none is choosen
	var $allow_themes = false;  ///< allow themes?

	var $isWebmaster;           ///< is user webmaster?
	var $isAdmin;               ///< is user admin?
	var $isSuperAdmin;          ///< is user superadmin?

	var $userModes = array(
		0 => 'Normal user',
		1 => 'Webmaster',
		2 => 'Admin',
		3 => 'Super admin'
	); ///< user modes

	function __construct($conf = array())
	{
		if (isset($conf['name'])) $this->session_name = $conf['name'];
		if (isset($conf['timeout'])) $this->timeout = $conf['timeout'];
		if (isset($conf['start_page'])) $this->start_page = $conf['start_page'];
		if (isset($conf['error_page'])) $this->error_page = $conf['error_page'];
		if (isset($conf['allow_themes'])) $this->allow_themes = $conf['allow_themes'];

		//ini_set('session.gc_probability', 1);
		//ini_set('session.gc_divisor', 1);

		//disable garbage collector to work around ubuntu/debian bug, see:
		//   http://forum.kohanaphp.com/comments.php?DiscussionID=565
		ini_set('session.gc_probability', 0);

		ini_set('session.gc_maxlifetime', $this->timeout);

		session_name($this->session_name);
		session_start();

		if (!isset($_SESSION['started']) || !$_SESSION['started']) $_SESSION['started'] = time();
		if (!isset($_SESSION['error'])) $_SESSION['error'] = '';
		if (!isset($_SESSION['ip'])) $_SESSION['ip'] = 0;
		if (!isset($_SESSION['id'])) $_SESSION['id'] = 0;
		if (!isset($_SESSION['username'])) $_SESSION['username'] = '';
		if (!isset($_SESSION['mode'])) $_SESSION['mode'] = 0;
		if (!isset($_SESSION['lastActive'])) $_SESSION['lastActive'] = 0;
		if (!isset($_SESSION['isWebmaster'])) $_SESSION['isWebmaster'] = 0;
		if (!isset($_SESSION['isAdmin'])) $_SESSION['isAdmin'] = 0;
		if (!isset($_SESSION['isSuperAdmin'])) $_SESSION['isSuperAdmin'] = 0;
		if (!isset($_SESSION['theme'])) $_SESSION['theme'] = $this->default_theme;
		if (!isset($_SESSION['referer'])) $_SESSION['referer'] = '';

		$this->started = &$_SESSION['started'];
		$this->error = &$_SESSION['error'];
		$this->ip = &$_SESSION['ip'];
		$this->id = &$_SESSION['id'];	//if id is set, also means that the user is logged in
		$this->username = &$_SESSION['username'];
		$this->mode = &$_SESSION['mode'];
		$this->lastActive = &$_SESSION['lastActive'];
		$this->isWebmaster = &$_SESSION['isWebmaster'];
		$this->isAdmin = &$_SESSION['isAdmin'];
		$this->isSuperAdmin = &$_SESSION['isSuperAdmin'];
		$this->theme = &$_SESSION['theme'];
		$this->referer = &$_SESSION['referer'];

		if (!$this->ip && !empty($_SERVER['REMOTE_ADDR'])) {	//FIXME map to $this->auth->ip
			$this->ip = IPv4_to_GeoIP($_SERVER['REMOTE_ADDR']);
		}
	}

	/**
	 * Sets up a session. Called from the auth class
	 *
	 * @param $_id user id
	 * @param $_username user name
	 * @param $_usermode user mode
	 */
	function start($_id, $_username, $_usermode)
	{
		global $config;
		$this->id = $_id;
		$this->username = $_username;
		$this->mode = $_usermode;		//0=normal user. 1=webmaster, 2=admin, 3=super admin
		$this->lastActive = time();

		if ($this->mode >= USERLEVEL_WEBMASTER) $this->isWebmaster = true;
		if ($this->mode >= USERLEVEL_ADMIN) $this->isAdmin = true;
		if ($this->mode >= USERLEVEL_SUPERADMIN) $this->isSuperAdmin = true;
	}

	/**
	 * Kills the current session, clearing all session variables
	 */
	function end()
	{
		$this->started = 0;
		$this->username = '';
		$this->ip = 0;
		$this->mode = 0;
		$this->isWebmaster = false;
		$this->isAdmin = false;
		$this->isSuperAdmin = false;
		$this->theme = $this->default_theme;
		$this->referer = '';

		if (!$this->id) return;

		$this->id = 0;
	}

	/**
	 * Redirects the user to a different page
	 */
	function redirect($path)
	{
		if (headers_sent()) {
			die(
			'<script type="text/javascript">'.
			'document.location.href="'.$path.'";'.
			'</script>');
		} else {
			header('Location: '.$path);
			die;
		}
	}

	/**
	 * Redirects user to default start page (logged in)
	 */
	function startPage()
	{
		if (!empty($this->referer))
{'fraction_non_alphanumeric': 0.18995441795231416, 'fraction_numerical': 0.008415147265077139, 'mean_word_length': 3.75375, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 122, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4877969', 'n_tokens_mistral': 4012, 'n_tokens_neox': 3691, 'n_words': 961}