<?php
	$cookie = json_decode(base64_decode($_COOKIE['user_settings']),true);
	
	if(isset($_GET['switchaccount']) && is_numeric($_GET['switchaccount'])){
		if(isset($cookie[$_GET['switchaccount']])){
			$cookie['index'] = $_GET['switchaccount'];
			setrawcookie("user_settings", base64_encode(json_encode($cookie)) , time() + 60 * 60 * 24 * 30, '/');
			header('Location: ./?mode=home');
			die();
		}
	}
	
	/* this file initializes some stuff when the user loads a page */

	$index = isset($cookie['index']) ? $cookie['index'] : 0;

	/* fetch the FE user setings from the cookie or set them if the cookie does not exists */
	if (isset($_COOKIE['user_settings'])){
		if (isset($cookie[$index])){
			$user_settings = $cookie[$index];
		} else {
			$user_settings = $cookie[0];
			$index = 0;
			$cookie['index'] = 0;
			setrawcookie("user_settings", base64_encode(json_encode($cookie)) , time() + 60 * 60 * 24 * 30, '/');
			header('Location: ./?mode=home');
		}
	} else {
			$user_settings['index'] = 0;
			$user_settings[0]['replies'] = "off";
			$user_settings[0]['attach'] = "on";
			$user_settings[0]['instance'] = "fedi.absturztau.be";
			$user_settings[0]['text'] = "off";
			$user_settings[0]['reblog'] = "off";	
			$user_settings[0]['videoloop'] = "off";
			$user_settings[0]['theme'] = "default";
			$user_settings[0]['nsfw'] = array();	
			$user_settings[0]['softmute'] = array();
			$user_settings[0]['unrollcw'] = "on";
			$user_settings[0]['mute'] = "off";
			$user_settings[0]['invidious'] = "";
			$user_settings[0]['teddit'] = "";
			$user_settings[0]['nitter'] = "";
			$user_settings[0]['embyt'] = "off";
			$user_settings[0]['linkpv'] = "off";
			$user_settings[0]['notif'] = "1111";
		if (isset($_COOKIE[0]['token'])) {
			$user_settings[0]['explicit'] = "blur";
		} else {
			$user_settings[0]['explicit'] = "hide";
		}
		setrawcookie("user_settings",base64_encode(json_encode($user_settings)),time()+60*60*24*30,'/');
		$user_settings = $user_settings[0];
	}
	
	if (isset($_COOKIE['theme'])){
		$theme = json_decode(base64_decode($_COOKIE['theme']),true);
	} else {
		$theme = array();
	}

	/* fetch the user token from the cookie */
	$token = (isset($_COOKIE['token']) ? json_decode(base64_decode($_COOKIE['token']),true)[$index] : false);
	
	/* a lazy way to check if we are logded in, to fix */
	$logedin = (isset($token[$index]) ? true : false); 

	/* by default the timeline to fetch will be the federated one
	   but we will infer the timeline to load from the presence of
	   some common url variables.
	   */
	$tl['mode'] =  "federated";	
	if(isset($_GET['mode'])){
		$tl['mode'] = $_GET['mode'];
	} else {
		foreach ($_GET as $key => $value){
			if(in_array($key,array("user","thread","local","tag","list","federated","search"))){
				$tl['mode'] = $key; 
				$tl[$key] = $value;
			}
		}
	}

	/* same as above but these are mostly used for AJAX requests */
	$tl['next'] = (isset($_GET['next']) ? htmlentities($_GET['next']) : false);
	$tl['since'] = (isset($_GET['since']) ? htmlentities($_GET['since']) : false);
	$tl['sincen'] = (isset($_GET['sincen']) ? htmlentities($_GET['sincen']) : false);
	
	$srv = $user_settings['instance'];
	
	/* no longer used */
	$nocookies = (isset($_COOKIE['user_settings']) ? false : true);

	/* these variables will hold the data of the page that will be generated */
	$replies = "";
	$notes = "";
?>
