<?php
	/* this file initializes some stuff when the user loads a page */


	/* fetch the user token from the cookie */
	$token = (isset($_COOKIE['token']) ? $_COOKIE['token'] : false);
	
	/* fetch the FE user setings from the cookie or set them if the cookie does not exists */
	if (isset($_COOKIE['user_settings'])){
		$user_settings = json_decode(base64_decode($_COOKIE['user_settings']),true);
	} else {
			$user_settings['replies'] = "off";
			$user_settings['attach'] = "on";
			$user_settings['instance'] = "mastodon.social";
			$user_settings['text'] = "off";
			$user_settings['reblog'] = "off";	
			$user_settings['videoloop'] = "off";
			$user_settings['theme'] = "default";
			$user_settings['nsfw'] = array();	
		if (isset($_COOKIE['token'])) {
			$user_settings['explicit'] = "blur";
		} else {
			$user_settings['explicit'] = "hide";
		}
		setrawcookie("user_settings",base64_encode(json_encode($user_settings)),time()+60*60*24*30,'/');
	}
	
	if (isset($_COOKIE['theme'])){
		$theme = json_decode(base64_decode($_COOKIE['theme']),true);
	} else {
		$theme = array();
	}
	
	/* a lazy way to check if we are logded in, to fix */
	$logedin = (isset($_COOKIE['token']) ? 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 = "";
?>
