<?php
# ************** LICENCE ****************
/*
	Copyright (c) PORTANEO.

	This file is part of POSH (Portaneo Open Source Homepage) http://sourceforge.net/projects/posh/.

	POSH 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

	POSH 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 Posh.  If not, see <http://www.gnu.org/licenses/>.
*/
# ***************************************
# STATS - count the modules open actions
#
# !! be careful, this file must be saved under uft8 format, and display an e accentuated here : é
# ***************************************

/**
*
* \brief check cookies and initialise anonymous account
*
*
*   return void
*
*  \relatesalso
*   portal/index.php
*
* !! be careful, this file must be saved under uft8 format, and display an e accentuated here : é
*
**/

$folder="";
$not_access=1;
$granted="N";
$pagename="portal/scr_checkandinitanonymousaccount.php";

if( isset($DB) ) {
	if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
	  $lang = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
	  $lang = strtolower(substr(chop($lang[0]),0,2));
	}

	$idanonym = "";
	$md5anonym = "";

	if (!empty($_COOKIE["idanonym"]) && !empty($_COOKIE["md5anonym"]))
	{
		$idanonym = $_COOKIE["idanonym"];
		$md5anonym= $_COOKIE["md5anonym"];

		$DB->getResults($scranonymous_getUser,$DB->escape($idanonym),$DB->quote($md5anonym));
		if ($DB->nbResults()>0) {
			$row = $DB->fetch(0);
			$_SESSION['user_id'] = $idanonym;
			$_SESSION['longname'] = $row["long_name"];
			$_SESSION['type'] = $row["typ"];
			$_SESSION['availability'] = $row["activity"];
			$_SESSION['picture'] = $row["activity"];
			$_SESSION['username'] = $row["username"];
			$_SESSION['lang'] = $row["lang"];
			
			if (__ENCRYPT_USERPREF) {
				$_SESSION['encrypt_key'] = uniqid(strval(mt_rand()));
				set_cookie('encrypt_key', $_SESSION['encrypt_key']);
			}
			
			init_session($idanonym,$row["username"],$row["typ"],$row["lang"],$row["long_name"],$row["activity"]);
		} else {
			set_cookie('idanonym', '');
			set_cookie('md5anonym', '');
			// Creating a session for an anonymous user
			require_once('scr_createandinitanonymousaccount.php');
		}
		$DB->freeResults();
	}

	$DB->close();
}
?>