<?php
/*
* Copyright 2008 Blandware (http://www.blandware.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Contains some session utilities.
*
* @package AtleapLite
* @author Roman Puchkovskiy
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/**
* Checks whether session does not exist, exists but not actual or exists and
* actual.
*
* @return int corresponding constant
*/
function checkSession()
{
global $sessionLen;
$serverDigest = getServerDigest();
// Starting session
session_start();
// Check whether the session is still actual
if (!isset($_SESSION['sessionLastChange']) || $_SESSION['serverDigest'] != $serverDigest) {
$result = TAUTH_FIRST_TIME;
$_SESSION = array();
session_destroy();
} elseif (time()-$_SESSION['sessionLastChange'] > $sessionLen) {
$result = TAUTH_SESSION_EXPIRED;
$_SESSION = array();
session_destroy();
} else {
$result = TAUTH_OK;
touchSession();
}
return $result;
}
/**
* Creates a session.
*
* @param bool $anonymous optional if set and true, session is created for
* anonymous user
*/
function createSession($anonymous = false)
{
session_start();
$_SESSION = array();
session_destroy();
session_start();
if ($anonymous) {
$_SESSION['anonymous'] = true;
} else {
$_SESSION['anonymous'] = false;
$_SESSION['login'] = $_POST['login'];
$dao =& getDao('user');
$dao->findByLogin($_POST['login']);
$_SESSION['userId'] = $dao->id;
}
$_SESSION['sessionLastChange'] = time();
$_SESSION['serverDigest'] = getServerDigest();
}
/**
* Updates session last change time.
*/
function touchSession()
{
// Session must be already started!
$_SESSION['sessionLastChange'] = time();
}
/**
* Destroys a session.
*/
function destroySession()
{
session_start();
$_SESSION = array();
session_destroy();
}
?>