<?php 
/**
* Tumder Project - A platform for the fun
* @copyright (c) 2016 Loois Sndr. All rights reserved.
*
* @author Loois Sndr
* @since 2016
*/
	if ( ! defined( 'ABSPATH' ) ) {
		define( 'ABSPATH', dirname(dirname(dirname( __FILE__ ))) . '/' );
	}
	
	error_reporting(0);
	if (!$Tumdconnect->connect_errno) {
	session_cache_limiter('none');
	session_start();

	$config['setting'] = mysqli_fetch_array($Tumdconnect->query("SELECT * FROM ".SETTING." WHERE id='1'"));

	if (!isset($_SESSION['language'])) {
    	$_SESSION['language'] = $config['setting']['language'];
	}

	// Check login
	if (!isset($_COOKIE["tumd_ac_u"]) || !isset($_COOKIE["tumd_ac_p"])) {
		$access = false;
	}

	else {
		$c_password = secureEncode($_COOKIE["tumd_ac_p"]);
		$c_username = secureEncode($_COOKIE["tumd_ac_u"]);

		// Detect credentials type
		if (is_numeric($c_username)) {
        	$cc_query_part = "id = " . $c_username;
    	} elseif (preg_match('/@/', $c_username)) {
        	$cc_query_part = "email = '{$c_username}'";
    	} elseif (preg_match('/[A-Za-z0-9_]/', $c_username)) {
        	$cc_query_part = "username = '{$c_username}'";
    	}

    	$user_check = "SELECT * FROM ".ACCOUNTS." WHERE password = '{$c_password}' AND $cc_query_part";
		$user_check_query = $Tumdconnect->query($user_check);

		if($user_check_query->num_rows > 0) {
			/* User Data */
			$user['data'] = $user_check_query->fetch_array(MYSQLI_BOTH);
			/* User Media */
			$user['media'] = getMedia($user['data']['avatar_id']);
			/* User Info */
			$user_info = $Tumdconnect->query("SELECT * FROM ".USERS." WHERE user_id = '{$user['data']['id']}'");
			$user['info'] = $user_info->fetch_array(MYSQLI_BOTH);
			}
		else {
			$access = false;
			setcookie("tumd_ac_u", 0, time() - (60 * 60 * 24 * 1));
			setcookie("tumd_ac_p", 0, time() - (60 * 60 * 24 * 1));
		}
			
	}
	// Fetch preferred language
	if (!empty($_GET['lang'])) {
    	$DIRECT_LANG = secureEncode($_GET['lang']);
	    if (file_exists('assets/language/' . $DIRECT_LANG . '.php')) {
	        $config['language'] = $DIRECT_LANG;
	        $_SESSION['language'] = $DIRECT_LANG;
	        if ($access == true) {
	            $Tumdconnect->query("UPDATE ".ACCOUNTS." SET language='".$DIRECT_LANG."' WHERE id=".$user['data']['id']);
			}
		}
	}

	require_once( ABSPATH . 'assets/language/'.$_SESSION['language'].'.php');

	// General array
	$Tumd = array();
	$Tumd['config'] = $config;
	$Tumd['theme_url'] = siteUrl() . '/templates/' . $config['setting']['site_theme'];
	$Tumd['theme_name'] = $config['setting']['site_theme'];
	$Tumd['access'] = $access;
	$Tumd['ads'] = getADS();
	if ($Tumd['access'] == true) {
		// User information
		$Tumd['data'] = $user['data'];
		$Tumd['media'] = $user['media'];
		$Tumd['info'] = $user['info'];
	}

	}