<?php

	require "system/config.php";
	require 'system/database.php';
	require_once 'vendor/autoload.php';

	define("site", $config["system"]["SITE_URL"]);
	define('SUBFOLDER', $config["system"]["SUBFOLDER"]);	
	define("dir", $config["system"]["ROOT_DIR"]);
 	$config['version'] = "3.7.0"; 

	$maxlifetime = 99999999;
	$secure = true; // if you only want to receive the cookie over HTTPS
    $httponly = true; // prevent JavaScript access to session cookie
    $samesite = 'None';

    if(PHP_VERSION_ID < 70300) {
        session_set_cookie_params($maxlifetime, '/; samesite='.$samesite, $_SERVER['HTTP_HOST'], $secure, $httponly);
    } else {
        session_set_cookie_params([
            'lifetime' => $maxlifetime,
            'path' => '/',
            'domain' => $_SERVER['HTTP_HOST'],
            'secure' => $secure,
            'httponly' => $httponly,
            'samesite' => $samesite
        ]);
    }

	@session_start();
	@ob_start();

	define("controller", dir."/mp-content/controller");
	define("view", dir."/mp-content/view");

	function loadClasses($className){

		require __DIR__ . '/classes/' . strtolower($className) . '.php';

	}

	spl_autoload_register('loadClasses');

	/*function __autoload($className) {
		$classFile = realpath(".")."/app/classes/class.".strtolower($className).".php";
		if(file_exists($classFile)) {
			require $classFile;
		}
	}*/
	
	Helper::Load();
	
	try {

		$db = new basicdb($config["db"]["host"], $config["db"]["name"], $config["db"]["user"], $config["db"]["pass"]);

	} catch ( PDOException $e ){

		echo $e->getMessage();
		die();
		
	}
	
	$settings = $db->query("SELECT * FROM settings")->fetch(PDO::FETCH_ASSOC);
	$currency = json_decode($settings["site_currency"], true);
	$cronSettings = json_decode($settings['cron_settings'], true);
	$guardSettings = json_decode($settings['guard_settings'], true);
	$plugins = $db->from('integrations')->where('app_status', 1)->all();
	$reference = $db->from('plugins')->where('plugin_id', 3)->first();
	$reference_detail = json_decode($reference['plugin_data'], true);
	$giftbalance = $db->from('plugins')->where('plugin_id', 1)->first();
	$giftbalance_detail = json_decode($giftbalance['plugin_data'], true);
	$cp = json_decode($settings['childpanel']);
	$pmoonguard = $db->from('plugins')->where('plugin_id', 2)->first();
	$sefLinks = json_decode($settings['site_links'], true);
	$NotifySettings = json_decode($settings['notify_settings'], true);
	$siteLinks = $sefLinks['links'];
	define("LOGO", $settings["site_logo"]);
	define("DARKLOGO", $settings["site_darklogo"]);
	define("FAVO", $settings["site_favi"]);
	define("AVLOGO", $settings["avatar_logo"]);
	define("TITLE", $settings["site_title"]);
	define("DESC", $settings["site_desc"]);
	define("ADMİNS", $settings["admin_iletisim"]);
	define("KEYW", $settings["site_keyw"]);
	define("CURRENCY_CODE", $currency["currency_code"]);
	define("CURRENCY_SYMBOL", $currency["currency_symbol"]);
	define("API_STATUS", $settings["api_status"]);
	define("PAYMENT_METHOD", $settings["payment_method"]);
	define("TICKET_LIMIT", $settings["ticket_limit"]);
	define("MINIMUM_PAYMENT", $settings["minimum_payment"]);
	define("REFERENCE_STATUS", $reference['plugin_status']);
	define("REFERENCE_SHARE", $reference_detail["reference_share"]);
	define("MAINTENANCE_STATUS", $settings["maintenance_status"]);
	define("PLUGIN_GIFTBALANCE_STATUS", $giftbalance["plugin_status"]);
	define("PLUGIN_GIFTBALANCE_AMOUNT", $giftbalance_detail["gift_amount"]);
	define("PLUGIN_MOONGUARD_STATUS", $pmoonguard["plugin_status"]);
	define("SITE_THEME", $settings["site_theme"]);
	define("DOLLAR", $settings["dolar"]);
	define("EURO", $settings["euro"]);
	$scriptVersion = $config["version"];
	if($settings['timezone']){
		date_default_timezone_set($settings['timezone']);
	}else{
		date_default_timezone_set($config["system"]["TIMEZONE"]);
	}

	$mainmenus = $db->from('menus')->orderby('menu_line', 'ASC')->all();
	
	if(session("login")) {
		if(session("user_id")) {
			$user_id = session("user_id");

			$user = $db->from('users')->where('user_id', $user_id)->where('user_status', 1)->first();
			if(!$user) {
				session_destroy();
				header("Refresh:0");
				die();
			}
			$skype_verify = $db->from('settings')->where('id','1')->first();
			$verify = json_decode($user['user_verify'],true);
			if($skype_verify['skype_verify'] && !$verify['skype'] &&$user['user_authority'] == 0 && $_SERVER['REQUEST_URI'] != "/skype-verify" && $_SERVER['REQUEST_URI'] != "/request/skypeverify" && $_SERVER['REQUEST_URI'] != "/request/sms_onay"&& $_SERVER['REQUEST_URI'] != "/mail-verify" && $_SERVER['REQUEST_URI'] != "/request/mailverify" && $_SERVER['REQUEST_URI'] != "/request/mail_onay"){
				#require controller("skype_verify");
				header("Location:".site."skype-verify");
				#echo $_SERVER['REQUEST_URI'];
			}
			if($skype_verify['mail_verify'] && !$verify['email'] && $user['user_authority'] == 0 && $_SERVER['REQUEST_URI'] != "/mail-verify" && $_SERVER['REQUEST_URI'] != "/request/mailverify" && $_SERVER['REQUEST_URI'] != "/request/mail_onay" && $_SERVER['REQUEST_URI'] != "/skype-verify" && $_SERVER['REQUEST_URI'] != "/request/skypeverify" && $_SERVER['REQUEST_URI'] != "/request/sms_onay"){
				header("Location:".site."mail-verify");
			}
			$offers = $db->from('special_offers')->where('user_id', $user['user_id'])->all();
			$offers_temp = [];
			foreach($offers as $offer) {
				$offers_temp[$offer["service_id"]] = $offer["offer_charge"];
			}
			$offers = $offers_temp;
			$user_drip_orders = $db->from('drip_orders')->where('user_id', session('user_id'))->all();
			$user_auto_orders = $db->from('auto_orders')->where('user_id', session('user_id'))->all();
			?> 
			<?php
		} else {
			session_destroy();
			header("Refresh:0");
			die();
		}
	}
	
	if(!session("language")) {
		$lang = "tr";
		if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
			$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
		}
		$query = $db->prepare("SELECT * FROM langs WHERE lang_code = ?");
		$query->execute([$lang]);
		if($query->rowCount() > 0) {
			$lang = $query->fetch(PDO::FETCH_ASSOC);
			$lang = $lang["lang_id"];
		} else {
			$lang = 1;
		}
		session("language", $lang);
	} else {
		$lang = session("language");
		$query = $db->prepare("SELECT * FROM langs WHERE lang_id = ?");
		$query->execute([$lang]);
		if($query->rowCount() > 0) {
			$lang = $query->fetch(PDO::FETCH_ASSOC);
			$lang = $lang["lang_id"];
		} else {
			$lang = 1;
		}
		session("language", $lang);
	}
	
	$langs = $db->query("SELECT * FROM `langs` ORDER BY lang_id ASC")->fetchAll(PDO::FETCH_ASSOC);
	$languages = [];
	foreach($langs as $l) {
		if($l["lang_id"] == $lang) {
			$selected_lang = $l;
		} else {
			$languages[] = $l;
		}
	}

	$dosyalar = scandir("mp-content/view");

	$perms = [

		'dashboard' => ['title' => "Gösterge Paneli"],
		'users' => ['title' => "Üyeler"],
		'orders' => ['title' => "Siparişler"],
		'autos' => ['title' => "Otomatik Siparişler"],
		'drips' => ['title' => "Süreli Siparişler"],
		'logs' => ['title' => "Loglar"],
		'services' => ['title' => "Servisler"],
		'payments' => ['title' => "Ödemeler"],
		'support' => ['title' => "Destek Sistemi"],
		'appearence' => ['title' => "Site Yönetimi"],
		'tasks' => ['title' => "Görevler"],
		'settings' => ['title' => "Ayarlar"],
		'pages' => ['title' => "Sayfalar"],
		'fields' => ['title' => "Özel Alanlar"],
		'blog' => ['title' => "Blog"],
		'announcements' => ['title' => "Duyurular"],
		'themes' => ['title' => "Temalar"],
		'bonuses' => ['title' => 'Ödeme Bonusları'],
		'links' => ['title' => 'Linkler'],
		'menus' => ['title' => 'Menüler'],
		'files' => ['title' => 'Dosyalar'],
		'language' => ['title' => "Diller"],
		'reports' => ['title' => "İstatistikler"],
		'plugins' => ['title' => "Eklentiler"],
		'payment' => ['title' => "Ödeme Ayarları"],
		'api' => ['title' => "Servis Sağlayıcılar"],
		'integrations' => ['title' => "Entegrasyonlar"],
		'accounts' => ['title' => "Banka Hesapları"],
		'smtp' => ['title' => "SMTP Ayarları"],
		'delete' => ['title' => "Silme Yetkisi"],
		'edit' => ['title' => "Düzenleme Yetkisi"],
		'root' => ['title' => "Kullanıcı Yetkisi Düzenleme"]

	];

	function instagram_usernameConvert($username){

		$link = "https://elinsoft.com/dNYL8F7rMCODEkb4/userid.php?u=".$username;

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $link);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 33);
		$result = curl_exec($ch);
		curl_close($ch);

		return $result;

	}

