session.php 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
/**
 * This program is part of Mahara
 *
 *  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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
Nigel McNie's avatar
Nigel McNie committed
20
 * @subpackage core
21 22 23 24 25 26 27 28 29
 * @author     Nigel McNie <nigel@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

defined('INTERNAL') || die();

/**
30
 * The session class handles session data and messages.
31
 *
32 33 34 35
 * 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.
36 37 38 39
 *
 * 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.
40
 *
41 42
 * Messages are stored in the session and are displayed the next time
 * a page is displayed to a user, even over multiple requests.
43 44 45
 */
class Session {

46
    /**
47
     * Resumes an existing session, only if there is one
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
     */
    public function __construct() {
        // Resume an existing session if required
        if (isset($_COOKIE['PHPSESSID'])) {
            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];
        }
Penny Leach's avatar
Penny Leach committed
66 67 68
        return null;
    }

69 70 71 72 73 74 75
    /**
     * 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) {
76
        $this->ensure_session();
77
        $_SESSION[$key] = $value;
78 79
    }

Martyn Smith's avatar
Martyn Smith committed
80 81 82 83 84
    /**
     * Clears the session property keyed by $key (by setting it to null).
     *
     * @param string $key   The key to set.
     */
Martyn Smith's avatar
Martyn Smith committed
85
    public function clear($key) {
Martyn Smith's avatar
Martyn Smith committed
86 87 88 89
        $this->ensure_session();
        $_SESSION[$key] = null;
    }

90 91 92 93 94 95 96
    /**
     * 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) {
97
        $this->ensure_session();
98
        if ($escape) {
99
            $message = self::escape_message($message);
100 101 102 103 104 105 106 107 108 109 110
        }
        $_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) {
111
        $this->ensure_session();
112
        if ($escape) {
113
            $message = self::escape_message($message);
114 115 116 117 118 119 120 121 122 123
        }
        $_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
     */
124
    public function add_error_msg($message, $escape=true) {
125
        $this->ensure_session();
126
        if ($escape) {
127
            $message = self::escape_message($message);
128
        }
129
        $_SESSION['messages'][] = array('type' => 'error', 'msg' => $message);
130 131 132 133 134 135 136 137 138 139 140 141 142
    }

    /**
     * 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() {
143
        $result = '<div id="messages">';
144 145
        if (isset($_SESSION['messages'])) {
            foreach ($_SESSION['messages'] as $data) {
146 147
                // @todo probably should be done with MochiKit.Signal
                $result .= '<div class="' . $data['type'] . '">';
148
                $result .= '<div class="fr"><a href="" onclick="removeElement(this.parentNode.parentNode);return false;">';
149
                $result .= '<img src="' . theme_get_url('images/icon_close.gif') . '" alt="[X]"></a></div>';
150
                $result .= $data['msg'] . '</div>';
151
            }
152
            $_SESSION['messages'] = array();
153
        }
154
        $result .= '</div>';
155 156 157
        return $result;
    }

158
    /**
159
     * Create a session, by initialising the $_SESSION array.
160
     */
161 162 163 164 165 166 167 168
    private function ensure_session() {
        if (empty($_SESSION)) {
            if (!session_id()) {
                session_start();
            }
            $_SESSION = array(
                'messages' => array()
            );
169
        }
170 171 172 173 174 175 176 177 178 179 180 181
    }

    /**
     * Escape a message for HTML output
     * 
     * @param string $message The message to escape
     * @return string         The message, escaped for output as HTML
     */
    private static function escape_message($message) {
        $message = hsc($message);
        $message = str_replace('  ', '&nbsp; ', $message);
        return $message;
182
    }
183

184 185 186 187 188 189 190 191 192 193 194 195 196
}

/**
 * A smarty callback to insert page messages
 *
 * @return string The HTML represening all of the session messages.
 */
function insert_messages() {
    global $SESSION;
    return $SESSION->render_messages();
}

?>