Location: PHPKode > projects > Kwalbum > kwalbum/include/verifyLogin.php
<?php
/*
 * Created on Aug 13, 2008
 * 2.0
 */
require_once pathinfo(__FILE__, PATHINFO_DIRNAME) . '/config.php';

$info = array ();
$loggedIn = 0;
$userIsAdmin = 0;
$userCanAdd = 0;
$userCanView = 0;
$userId = 0;
$editMode = 0;

if ('Logout' != @$G['p'])
{
	session_start();
	if (isset ($_SESSION[PAGE_URL . 'id']))
	{
		$userId = $_SESSION[PAGE_URL . 'id'];
		if ($userId > 0)
		{
			$loggedIn = 1;
			$userIsAdmin = $_SESSION[PAGE_URL . 'admin'];
			$userCanAdd = $_SESSION[PAGE_URL . 'add'];
			$userCanView = $_SESSION[PAGE_URL . 'view'];
		}
	}
	elseif (isset ($_COOKIE[COOKIE_NAME]))
	{
		if (!isset ($DB))
		{
			require_once pathinfo(__FILE__, PATHINFO_DIRNAME) . '/DBConnection.php';
			$DB = new DBConnection();
		}

		$temp = array ();
		$temp = explode(',', $_COOKIE[COOKIE_NAME]);
		$userPassword = $DB->FilterString($temp[0]);
		$tempUserId = (int) $temp[1];

		$query = 'SELECT UserId, UserIsAdmin, UserCanAdd, UserCanView' .
			' FROM ' . USER_TABLE .
			" WHERE UserId='$tempUserId' AND UserPassword='$userPassword'";
		$result = $DB->query($query);
		if (0 < $result->num_rows)
		{
			$myrow = $result->fetch_array(MYSQLI_ASSOC);
			$userId = $myrow['UserId'];
			if ($userIsAdmin = $myrow['UserIsAdmin'])
				$userCanAdd = $userCanView = $userIsAdmin;
			else
				if ($userCanAdd = $myrow['UserCanAdd'])
					$userCanView = $userCanAdd;
				else
					$userCanView = $myrow['UserCanView'];
			$DB->UpdateSingle(USER_TABLE, 'UserVisitDt', 'NOW()', "UserId='$userId'");
		}
	}

	if (!empty ($_GET['m']))
	{
		if ('Edit' == $_GET['m'])
			$editMode = 1;
		else
			if ('View' == $_GET['m'])
				unset ($_SESSION[PAGE_URL . 'edit']);
	}
	elseif ($userCanAdd and !empty ($_SESSION[PAGE_URL . 'edit']))
		$editMode = 1;

	$_SESSION[PAGE_URL . 'id'] = $userId;
	$_SESSION[PAGE_URL . 'admin'] = $userIsAdmin;
	$_SESSION[PAGE_URL . 'add'] = $userCanAdd;
	$_SESSION[PAGE_URL . 'view'] = $userCanView;
	$_SESSION[PAGE_URL . 'edit'] = $editMode;
	session_write_close();
}

define('USER_ID', $userId);
define('USER_IS_ADMIN', $userIsAdmin);
define('USER_CAN_ADD', $userCanAdd);
define('USER_CAN_VIEW', $userCanView);
define('USER_CAN_VIEW_QUERY', (USER_IS_ADMIN ? ' ItemIsHidden<=3' : (USER_CAN_VIEW ? ' ItemIsHidden<=2 ' : (USER_ID ? ' ItemIsHidden<=1 ' : ' ItemIsHidden=0 '))));
if (!defined('EDIT_MODE'))
	define('EDIT_MODE', $editMode);
?>
Return current item: Kwalbum