Location: PHPKode > projects > Gnew > Gnew-2013.1/includes/sessions.php
<?php
// -----------------------------------------------------------------------------
// $Id: sessions.php 696 2013-03-29 16:31:15Z raoul $
//
// Copyright (C) 2013 Raoul Proença
// License: GNU GPL version 3 (see copying.txt file)
// Website: http://www.gnew.fr/
// -----------------------------------------------------------------------------
// 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 3 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, see <http://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------------

function sess_open()
{
    return true;
}

function sess_close()
{
    return true;
}

function sess_read($id)
{
    // Generate CAPTCHA string
    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    $random = '';
    srand((double)microtime() * 1000000);
    for ($i = 0; $i < 6; $i++)
    {
        $random .= $chars{rand() % strlen($chars)};
    }

    // http://www.php.net/session.configuration.php#ini.session.hash-bits-per-character
    if (empty($id) || !preg_match('`^[0-9a-zA-Z\-,]{22,40}$`', $id))
    {
        $_SESSION['user_id'] = 0;
        $_SESSION['user_captcha'] = $random;
    }
    else
    {
        $GLOBALS['sql']->query('SELECT session_data, session_expiry
                                FROM ' . TABLE_SESSIONS . '
                                WHERE session_id = \'' . $id . '\'');
        $table_sessions = $GLOBALS['sql']->fetch();
        // New session?
        if (!$table_sessions['session_data'])
        {
            // The initial lifetime of the session is 2 minutes
            $GLOBALS['sql']->query('INSERT INTO ' . TABLE_SESSIONS . ' (session_id, session_expiry)
                                    VALUES (\'' . $id . '\', \'' . (time() + 120) . '\')');
            $_SESSION['user_id'] = 0;
            $_SESSION['user_captcha'] = $random;
        }
        else
        {
            // Update current session (for periods of 2 minutes)
            if ($table_sessions['session_expiry'] < time())
            {
                $GLOBALS['sql']->query('UPDATE ' . TABLE_SESSIONS . '
                                        SET session_expiry = \'' . (time() + 120) . '\'
                                        WHERE session_id = \'' . $id . '\'');
            }
        }
        return $table_sessions['session_data'];
    }
}

function sess_write($id, $data)
{
    $GLOBALS['sql']->query('UPDATE ' . TABLE_SESSIONS . '
                            SET session_data = \'' . $data . '\'
                            WHERE session_id = \'' . $id . '\'');
    return true;
}

function sess_destroy($id)
{
    $GLOBALS['sql']->query('DELETE FROM ' . TABLE_SESSIONS . '
                            WHERE session_id = \'' . $id . '\'');
    $GLOBALS['sql']->query('UPDATE ' . TABLE_USERS . '
                            SET user_last_visit = \'' . time() . '\'
                            WHERE user_id = \'' . $_SESSION['user_id'] . '\'');
    // For the time being user_last_visit is displayed but we don't clean
    // TABLE_USERS since it would clean the cache database each time a user log
    // out. It is better to not cache templates/*/users/info.htpl file.
    // $GLOBALS['cache']->clean(TABLE_USERS);
    return true;
}

function sess_gc($lifetime)
{
    $GLOBALS['sql']->query('DELETE FROM ' . TABLE_SESSIONS . '
                            WHERE session_expiry < \'' . (time() - $lifetime) . '\'');
    return true;
}

session_set_save_handler('sess_open', 'sess_close', 'sess_read', 'sess_write', 'sess_destroy', 'sess_gc');
session_start();

?>
Return current item: Gnew