* @license http://www.gnu.org/copyleft/gpl.html GNU GPL * @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz * */ defined('INTERNAL') || die(); // // Set session settings // session_name('mahara'); ini_set('session.save_path', '3;' . get_config('dataroot') . 'sessions'); ini_set('session.gc_divisor', 1000); // Session timeout is stored in minutes in the database ini_set('session.gc_maxlifetime', get_config('session_timeout') * 60); ini_set('session.use_only_cookies', true); ini_set('session.cookie_path', get_mahara_install_subdirectory()); ini_set('session.cookie_httponly', 1); ini_set('session.hash_bits_per_session', 4); // TEMPORARY: this will be REMOVED after the session path changing // has been around for a bit. // Attempt to create session directories $sessionpath = get_config('dataroot') . 'sessions'; if (!is_dir("$sessionpath/0")) { // Create three levels of directories, named 0-9, a-f $characters = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'); foreach ($characters as $c1) { check_dir_exists("$sessionpath/$c1"); foreach ($characters as $c2) { check_dir_exists("$sessionpath/$c1/$c2"); foreach ($characters as $c3) { check_dir_exists("$sessionpath/$c1/$c2/$c3"); } } } } /** * The session class handles session data and messages. * * This class stores information across page loads, using only a cookie to * remember the info. User information is stored in the session so it does * not have to be requested each time the page is loaded, however any other * information can also be stored using this class. * * This class also is smart about giving out sessions - if a visitor * has not logged in (e.g. they are a guest, searchbot or a simple * 'curl' request), a session will not be created for them. * * Messages are stored in the session and are displayed the next time * a page is displayed to a user, even over multiple requests. */ class Session { /** * Resumes an existing session, only if there is one */ public function __construct() { // Resume an existing session if required if (isset($_COOKIE[session_name()])) { session_start(); } } /** * Gets the session property keyed by $key. * * @param string $key The key to get the value of * @return mixed */ public function get($key) { if (isset($_SESSION[$key])) { return $_SESSION[$key]; } return null; } /** * Sets the session property keyed by $key. * * @param string $key The key to set. * @param string $value The value to set for the key */ public function set($key, $value) { $this->ensure_session(); $_SESSION[$key] = $value; } /** * Clears the session property keyed by $key (by setting it to null). * * @param string $key The key to set. */ public function clear($key) { $this->ensure_session(); $_SESSION[$key] = null; } /** * Adds a message that indicates something was successful * * @param string $message The message to add * @param boolean $escape Whether to HTML escape the message */ public function add_ok_msg($message, $escape=true) { $this->ensure_session(); if ($escape) { $message = self::escape_message($message); } $_SESSION['messages'][] = array('type' => 'ok', 'msg' => $message); } /** * Adds a message that indicates an informational message * * @param string $message The message to add * @param boolean $escape Whether to HTML escape the message */ public function add_info_msg($message, $escape=true) { $this->ensure_session(); if ($escape) { $message = self::escape_message($message); } $_SESSION['messages'][] = array('type' => 'info', 'msg' => $message); } /** * Adds a message that indicates a failure to do something * * @param string $message The message to add * @param boolean $escape Whether to HTML escape the message */ public function add_error_msg($message, $escape=true) { $this->ensure_session(); if ($escape) { $message = self::escape_message($message); } $_SESSION['messages'][] = array('type' => 'error', 'msg' => $message); } /** * Builds HTML that represents all of the messages and returns it. * * This is designed to let smarty templates hook in any session messages. * * Calling this function will destroy the session messages that were * rendered, so they do not inadvertently get displayed again. * * @return string The HTML representing all of the session messages. */ public function render_messages() { $result = '