<?php
/*
Name: Droppy - Online file sharing
Author: Proxibolt
File: index.php
*/

//Inlcude translation file
require_once dirname(__FILE__) . '/config/config.php';
require_once dirname(__FILE__) . '/src/plugins/Mobile_Detect.php';

ob_start();

//Loading mobile detection plugin
$detect = new Mobile_Detect;

//Checks which page(s) needs to be locked down
if($lock_page == 'both') {
	if(!isset($_SESSION['droppy_user'])) {
		header('Location: login.php');
		exit();
	}
}
elseif($lock_page == 'upload' && empty($_GET['download'])) {
	if(!isset($_SESSION['droppy_user'])) {
		header('Location: login.php');
		exit();
	}
}
elseif($lock_page == 'download' && !empty($_GET['download'])) {
	if(!isset($_SESSION['droppy_user'])) {
		header('Location: login.php');
		exit();
	}
}
if(isset($_GET['page'])) :
	$page = mysqli_real_escape_string($mysqli, strtolower($_GET['page']));

    //Load the right page
	foreach($plugin_pages as $ppage) {
	    if($ppage[$page]['type'] == 'page') {
	        require_once $ppage[$page]['path'];
        }
        else
        {
            header('Location: ../');
        }
    }

else:
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="description" content="<?php echo $site_desc; ?>">
    <meta name="author" content="<?php echo $site_name; ?>">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">

    <title><?php echo $site_title; ?></title>
	
	<!-- Favicon -->
	<link rel="shortcut icon" type="image/png" href="<?php echo $favicon_path; ?>"/>

	<!-- Loading Bootstrap -->
    <link href="<?php echo $theme_path; ?>css/bootstrap/bootstrap.min.css" rel="stylesheet">
	
	<!-- Loading some custom styles -->
	<link href="<?php echo $theme_path; ?>css/droppy.css" rel="stylesheet">
	<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">

	<!-- Loading jQuery -->
	<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.7.1/clipboard.min.js"></script>
</head>

<body>
<?php
	//Getting some information about the upload out of the database
	if(!empty($_GET['download'])) {
		$download_id = mysqli_real_escape_string($mysqli, $_GET['download']);
	}
	if(!empty($_GET['p'])) {
		$private_id = mysqli_real_escape_string($mysqli, $_GET['p']);
	}
	elseif(!empty($_GET['a'])) {
		$uploader_id = mysqli_real_escape_string($mysqli, $_GET['a']);
	}
	$get_info = $mysqli->query("SELECT * FROM $table_uploads WHERE upload_id='$download_id'");
	$check_receiver = $mysqli->query("SELECT * FROM $table_receivers WHERE upload_id='$download_id'");
	$get_email = $mysqli->query("SELECT * FROM $table_receivers WHERE upload_id='$download_id' AND private_id='$private_id'");
	while($sql_email = $get_email->fetch_assoc()) {
		$email = $sql_email['email'];
	}
	$get_downloads = $mysqli->query("SELECT * FROM $table_downloads WHERE download_id='$download_id' AND email='$email'");

	$get_uploader = $mysqli->query("SELECT * FROM $table_uploads WHERE secret_code='$uploader_id'");

	//-- End if getting information --

	//-- Loading normal page or mobile page --
	if( $detect->isMobile() || $detect->isTablet() || isset($_GET['mob']) == 'test'):
		require_once $theme_path . 'mobile-home.php';
	else:
		require_once $theme_path . 'home.php';
	endif;
	//-- End of loading normal/mobile page --
	//-- Setting the page to default Link share option (When set)
	if($default_sharetype == 'link') {
		echo '
		<script>
			shareLink();
		</script>
		';
	}
	if(isset($_GET['goto'])) {
		echo '
		<script>
			openSettings();
			openInlinePage(\''.$_GET['goto'].'\');
		</script>
		';
	}
	//-- End of settings default page to Link share option (When set)

	//Getting custom modals (Plugins)
	$get_popups = $mysqli->query("SELECT * FROM $table_popups");
	while($row = $get_popups->fetch_assoc()) {
		include_once dirname(__FILE__) . $row['path'];
	}
?>
</body>
<?php
if($analytics_code != '') {
	echo $analytics_code;
}
?>
</html>
<?php
endif;
?>