<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
$_SERVER['HTTPS'] = 'on';

function is_cli() {
    return !http_response_code();
}

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else if (is_cli()) {	
    $ip = "129.222.139.30";
}

/*
if($ip != '129.222.139.30') {	
	exit();
} else {
	echo $ip;	
}
*/

// EASY CONFIG
$expire_override = 0;
$version = "v2.0.42";
$adx_disable = 0;
$buysell_disable = 1;
$h20_disable = 1;

$time_start = microtime(true); 

$cli = 0;
if(php_sapi_name() === "cli") {
	$cli = 1;
}

include "include/functions.php";
include "include/functions_url.php";
// include "data/db_class.php";

$browser = getBrowser();

// Xenforo
$userid = 0;
include "include/xen_foro2_connect.php";

// GET CATEGORIES wCACHE - below root
$cache_path = "/dev/shm/dlio";
if (!file_exists($cache_path)) {        
	mkdir($cache_path, 0755, true);
}

// Basic Functions
// rebuilt data
if ($cli == 1) {		
	// BEST TO RUN AFTER NGINX / PHP RESTART
	include "/www/clients/download.io/api/generate_category.php";
	include "/www/clients/download.io/api/generate_giveaway_categories.php";
	include "/www/clients/download.io/api/generate_article_categories.php";
	include "/www/clients/download.io/api/generate_tag_categories.php";
	include "/www/clients/download.io/api/generate_discount_categories.php";	
}
else {
	include "/www/clients/download.io/api/simple_load_category_from_cache.php";
}

/*
if($ip != '129.222.139.30') {
	echo "Maintenance";
	exit();
}
*/

// if ($ip == '2600:1700:d590:11d0:cd46:e206:b58f:7c92') {
// 	echo "Maintenance";
// }

// Load Config
include_once "include/config.php";
include_once "include/class.inputfilter.php";

// Languages
include_once "include/language.php";

// Get URL - test, moved above Cache, need to check for 404, etc
include_once "include/url_proxy.php";

// RUN TO GENERATE ONLY
// include "module/cache/articles_category_generate.php";

// include "module/cache/discounts_generate.php";
// include "module/cache/newsletter_generate.php";
// include "module/cache/articles_category_generate.php";

// include "module/cache/category_result_software_all.php";
// include "modules/categories/categories.php";

// GET DEVICE
include "/www/clients/download.io/api/device.php";
$agent = $_SERVER['HTTP_USER_AGENT'];
$platform_detected = strtolower(Device($agent));

// Get URL in other Languages
// include_once "include/url_reverse.php";	

// Load Language File
include "lang/eng/main.php";

/* Admin Rights */
// if ($ip == '2600:1700:d590:11d0:4d78:1437:c3bc:b72') {
// 	echo "UserId: $userid";
// }
if(can_ID($userid)) {	
	$tmp_db_connection = $db_connection;		

	// DB Connection
	$db_connection = mysqli_connect("localhost","$dbuser","$dbpass") or die("Site is under maintenance. Please some back in next few minutes. Please visit our forum instead https://www.download.io/forum");
	mysqli_select_db ($db_connection, "$dbname") or die ("Could not open database!");
	mysqli_query($db_connection, "SET NAMES utf8mb4 COLLATE utf8mb4_0900_ai_ci"); 
		
	// Load DB
	include "admin/include/functions.php";
	include "admin/include/prava.php";		
	$db_connection = $tmp_db_connection;	

	if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
		$ip = $_SERVER['HTTP_CLIENT_IP'];
	} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	} else {
		$ip = $_SERVER['REMOTE_ADDR'];
	}
}

/*
if($platform == "android") {
	echo "Platform: $platform<br>";
	echo "Action: $action<br>";
	echo "Which: $which<br>";
}
*/

switch($platform) {	
	case "home": {
		// New styling
		include "templates4/home/config.php";
		include "templates4/home/index.php";
		
		// Old styling
		// include "lang/main_eng.php";
		// include "template/home/config.php";		 
		// include "template/home/index.php";
		break;
	}

	case "admin": {
		include "dl-admin/route.php";
		break;
	}
	
	case "windows": {
		include "lang/eng/windows_main.php"; // LANG
		include "templates3/windows/config.php"; // CONFIG, SET LIST LIMITS			
		
		switch($action) {
			case "": {											
					include "lang/main_eng.php";					
					include "template/windows/config.php";		 					
					include "template/windows/index.php";					
					break;
			}
			case "uninstall": {
					include "lang/main_eng.php";
					include "template/windows/config.php";		 
					include "template/windows/uninstall.php";
					break;
			}
			case "category": include "templates3/windows/category.php"; break;
			case "subcategory": include "templates3/windows/subcategory.php"; break;
			case "download":
				// New style
				include "templates4/cross_platform/x_download.php";
				// Old style
				// include "templates3/windows/download.php";
				break;
			case "file": {				
				if($extra=="pictures") {
					include_once "templates3/windows/file_pictures.php";
				} else if ($extra == "addreview" || $extra == "savereview") {
					include_once "templates3/windows/file_review.php";
				} else if($extra == "report" || $extra == "savereport") {
					include "template/common/report_windows.php";
				} else {					
					include_once "templates3/windows/file.php";
				}
				break;
			};
			case "reviews": include "templates3/windows/reviews.php"; break;
			case "pictures": include "templates3/windows/pictures.php"; break;
			case "screenshots": include "templates3/windows/screenshots.php"; break;
			case "video": include "templates3/windows/video.php"; break;	
			case "versions": include "templates3/windows/versions.php"; break;	
			case "newfiles":
				case "newgames": 
				include "templates3/windows/newfiles.php"; break;
			case "topfiles":
				case "topgames":
				include "templates3/windows/topfiles.php"; break;
			case "categoryall": include "templates3/windows/category-all.php"; break;	
			case "tags": include "templates3/windows/tags_search.php"; break;
			case "nodirect": include "templates3/redirect-home.php"; break;
			default: {
				if($action=="") {
					include "lang/main_eng.php";
					include "template/windows/config.php";
					include "template/windows/homepage.php";
				}
				else {
					include "templates3/redirect-home.php";
				}
				break;
			}
				
		};
		break;		
	}

	case "mac": {
		include "lang/eng/windows_main.php"; // LANG
		include "templates3/mac/config.php"; // CONFIG, SET LIST LIMITS			

		switch($action) {
			case "": {											
					include "lang/main_eng.php";					
					include "template/mac/config.php";		 					
					include "template/mac/index.php";					
					break;
			}
			case "uninstall": {
					include "lang/main_eng.php";
					include "template/mac/config.php";		 
					include "template/mac/uninstall.php";
					break;
			}
			case "category": include "templates3/mac/category.php"; break;
			case "subcategory": include "templates3/mac/subcategory.php"; break;
			case "download":
				// New style
				include "templates4/cross_platform/x_download.php";
				// Old style
				// include "templates3/mac/download.php";
				break;
			case "file": {			
				if($extra=="pictures") {
					include_once "templates3/mac/file_pictures.php";
				} else if ($extra == "addreview" || $extra == "savereview") {
					include_once "templates3/mac/file_review.php";
				} else if($extra == "report" || $extra == "savereport") {
					include "template/common/report_windows.php";
				} else {					
					include_once "templates3/mac/file.php";
				}
				break;
			};
			case "reviews": include "templates3/mac/reviews.php"; break;
			case "pictures": include "templates3/mac/pictures.php"; break;
			case "screenshots": include "templates3/mac/screenshots.php"; break;
			case "video": include "templates3/mac/video.php"; break;	
			case "versions": include "templates3/mac/versions.php"; break;	
			case "newfiles":
				case "newgames": 
				include "templates3/mac/newfiles.php"; break;
			case "topfiles":
				case "topgames":
				include "templates3/mac/topfiles.php"; break;
			case "categoryall": include "templates3/mac/category-all.php"; break;	
			case "tags": include "templates3/mac/tags_search.php"; break;
			case "nodirect": include "templates3/redirect-home.php"; break;
			default: {
				if($action=="") {
					include "lang/main_eng.php";
					include "template/mac/config.php";
					include "template/machomepage.php";
				}
				else {
					include "templates3/redirect-home.php";
				}
				break;
			}
				
		};				
		break;		
	}
		
	case "android": {
		include "lang/eng/windows_main.php"; // LANG
		include "templates3/android/config.php"; // CONFIG, SET LIST LIMITS			
		switch($action) {			
			case "": {											
					include "lang/main_eng.php";					
					include "template/android/config.php";		 					
					include "template/android/index.php";					
					break;
			}
			case "uninstall": {
					include "lang/main_eng.php";
					include "template/android/config.php";		 
					include "template/android/uninstall.php";
					break;
			}
			case "category":				
				include "templates3/android/category.php";
				break;
			case "subcategory":				
				include "templates3/android/subcategory.php";
				break;
			case "download":
				// New style
				include "templates4/cross_platform/x_download.php";
				// Old style
				// include "templates3/android/download.php";
				break;
			case "file": {			
				if($extra=="pictures") {
					include_once "templates3/android/file_pictures.php";
				} else if ($extra == "addreview" || $extra == "savereview") {
					include_once "templates3/android/file_review.php";
				} else if($extra == "report" || $extra == "savereport") {
					include "template/common/report_windows.php";
				} else {					
					include_once "templates3/android/file.php";
				}
				break;
			};
			case "reviews": include "templates3/android/reviews.php"; break;
			case "pictures": include "templates3/android/pictures.php"; break;
			case "screenshots": include "templates3/android/screenshots.php"; break;
			case "video": include "templates3/android/video.php"; break;	
			case "versions": include "templates3/android/versions.php"; break;	
			case "newfiles":
				case "newgames": 
				include "templates3/android/newfiles.php"; break;
			case "topfiles":
				case "topgames":
				include "templates3/android/topfiles.php"; break;
			case "categoryall": include "templates3/android/category-all.php"; break;	
			case "tags": include "templates3/android/tags_search.php"; break;
			case "nodirect": include "templates3/redirect-home.php"; break;
			default: {
				if($action=="") {
					include "lang/main_eng.php";
					include "template/android/config.php";
					include "template/androidhomepage.php";
				}
				else {
					include "templates3/redirect-home.php";
				}
				break;
			}
				
		};		
		break;		
	}	

	case "ios": {
		include "lang/eng/windows_main.php"; // LANG
		include "templates3/ios/config.php"; // CONFIG, SET LIST LIMITS			

		// echo "Action: $action<br>";

		switch($action) {
			case "": {											
					include "lang/main_eng.php";					
					include "template/ios/config.php";		 					
					include "template/ios/index.php";					
					break;
			}
			case "uninstall": {
					include "lang/main_eng.php";
					include "template/ios/config.php";		 
					include "template/ios/uninstall.php";
					break;
			}
			case "category":
				// echo "CAT";
				include "templates3/ios/category.php";
				break;
			case "subcategory":
				// echo "SUBCAT";
				include "templates3/ios/subcategory.php";
				break;

			case "download":
				// New style
				include "templates4/cross_platform/x_download.php";
				// Old style
				// include "templates3/ios/download.php";
				break;
			case "file": {				
				if($extra=="pictures") {
					include_once "templates3/ios/file_pictures.php";
				} else if ($extra == "addreview" || $extra == "savereview") {
					include_once "templates3/ios/file_review.php";
				} else if($extra == "report" || $extra == "savereport") {
					include "template/common/report_windows.php";
				} else {					
					include_once "templates3/ios/file.php";
				}
				break;
			};
			case "reviews": include "templates3/ios/reviews.php"; break;
			case "pictures": include "templates3/ios/pictures.php"; break;
			case "screenshots": include "templates3/ios/screenshots.php"; break;
			case "video": include "templates3/ios/video.php"; break;	
			case "versions": include "templates3/ios/versions.php"; break;	
			case "newfiles":
				case "newgames": 
				include "templates3/ios/newfiles.php"; break;
			case "topfiles":
				case "topgames":
				include "templates3/ios/topfiles.php"; break;
			case "categoryall": include "templates3/ios/category-all.php"; break;	
			case "tags": include "templates3/ios/tags_search.php"; break;
			case "nodirect": include "templates3/redirect-home.php"; break;
			default: {
				if($action=="") {
					include "lang/main_eng.php";
					include "template/ios/config.php";
					include "template/ioshomepage.php";
				}
				else {
					include "templates3/redirect-home.php";
				}
				break;
			}
				
		};			
		break;		
	}		
	
	case "common": {
    	include "lang/main_eng.php";
        switch($action) {
			case "report":
                include "template/common/".$action.".php";
                break;
        };
		break;	
	}			
    
    case "windowsphone":
    case "windowsapps": {
    	include "lang/main_eng.php";
        include "lang/apps.php";
         
        switch($action) {
			case "category":
			case "category_all":
			case "subcategory":
			case "download":
			case "video":
			case "pictures":
			case "reviews":
			case "app":
                include "template/apps/".$action.".php";
                break;
            case "homepage":
            default: 
                include "template/apps/homepage.php";
                break;
        };        
        break;
    }
    
	/* news */
	case "news": {
		include "lang/main_eng.php";
		include "template/news/config.php";
			
		switch($action) {
			case "article":
			case "category":
			case "subcategory":
				include "template/news/$action.php";
				break;
			default:
				include "template/news/homepage.php";
				break;
		};
		break;
	}
	

	/* Giveaway */	
	case "giveaways": {				
		include "lang/main_eng.php";
		include "template/giveaway/config.php";
		
		switch($action) {
			case "single": 
				include "template/giveaway/single.php";
				break;
			case "category": 
				include "template/giveaway/category.php";
				break;
			case "home":				
				include "template/giveaway/index.php";
				break;
		};
		break;		
	}

	/* Discounts */
	case "discounts": {
		include "lang/main_eng.php";
		include "template/discounts/config.php";				

		switch($action) {
			case "single": 
				include "template/discounts/single.php";
				break;
			case "category": 
				include "template/discounts/category.php";
				break;
			case "subcategory": 
				include "template/discounts/category.php";
				break;
			case "home":
				include "template/discounts/index.php";
				break;
		};
		break;		
	}	

	/* Recommended */
	case "recommended": {
		include "templates4/recommended/config/$single.php";
		include "templates4/recommended/article_template.php";

		break;
	}
	
	/* Cryptocurrency	
	case "cryptocurrency": {
		include "lang/main_eng.php";
		include "module/coincompare/index.php"; //include "template/crypto/index.php";
		break;		
	}
	*/
			
	/* iPhone
	case "iphone": {
		$mobile = "0";		
		include "lang/eng/windows_main.php";
		include "lang/eng/iphone.php";
		include "templates/iphone/config.php";
		
		switch($action) {
			case "category": include "templates/iphone/category.php"; break;
			case "file": include "templates/iphone/file.php"; break;
			case "download": include "templates/iphone/download.php"; break;
			default:
				include "templates/iphone/homepage.php";
				break;
		};
		break;		
	}	
		
	/* Subtitles	
	case "subtitles": {
		include "templates3/subtitles/redirect.php";
		break;	
	}
	*/
	
	/* Free software of day	
	case "freesoftwareeveryday":
	{
		include "lang/eng/windows_main.php";
		include "lang/eng/freesoftwareeveryday.php";
		
		if(isset($_GET['gaotd_id'])) {
			include_once "templates/freesoftwareeveryday/single.php"; break;
		}
		include_once "templates/freesoftwareeveryday/index.php"; break;
	}
	case "freegameeveryday":
	{
		include "lang/eng/windows_main.php";
		include "lang/eng/freesoftwareeveryday.php";
		
		if(isset($_GET['gaotd_id'])) {
			include_once "templates/freesoftwareeveryday/game_single.php"; break;
		}
		include_once "templates/freesoftwareeveryday/game_index.php"; break;
	}
	*/
		
	// Archive
	case "archive": {	
		if($action=="sitemap") {
			if($which=="filesen") include_once "templates3/archive/sitemap_filesen.php";
			else if($which=="fileshr") include_once "templates3/archive/sitemap_fileshr.php";
			//else if($which=="subtitlesen") include_once "templates3/archive/sitemap_subtitlesen.php";
			//else if($which=="subtitleshr") include_once "templates3/archive/sitemap_subtitleshr.php";
			else if($which=="categoryen") include_once "templates3/archive/sitemap_categoryen.php";
			else if($which=="categoryhr") include_once "templates3/archive/sitemap_categoryhr.php";
			else if($which=="subcategoryen") include_once "templates3/archive/sitemap_subcategoryen.php";
			else if($which=="subcategoryhr") include_once "templates3/archive/sitemap_subcategoryhr.php";
			else if($which=="sitemapen") include_once "templates3/archive/sitemapen.php";
			else if($which=="sitemaphr") include_once "templates3/archive/sitemaphr.php";
			else if($which=="tagsen") include_once "templates3/archive/sitemap_tagsen.php";
			else if($which=="tagshr") include_once "templates3/archive/sitemap_tagshr.php";
			else if($which=="miscen") include_once "templates3/archive/sitemap_miscen.php";
			else if($which=="mischr") include_once "templates3/archive/sitemap_mischr.php";
			//else if($which=="news") include_once "templates3/archive/sitemap_news.php";
			else if($which=="giveaway") include_once "template/archive/sitemap_giveaway.php";
			else if($which=="sitemapimagesen") include_once "templates3/archive/sitemapimagesen.php";
			else if($which=="imagesen") include_once "templates3/archive/sitemap_imageslist.php";
			else if($which=="giveaway") include_once "template/archive/sitemap_giveaway.php";
			else if($which=="news") include_once "template/archive/sitemap_news.php";
			else if($which=="mac") include_once "template/archive/sitemap_mac.php";
			else if($which=="mac_category") include_once "template/archive/sitemap_mac_category.php";
			else if($which=="mac_apps") include_once "template/archive/sitemap_mac_apps.php";
			break;
			
		}
		include_once "templates3/archive/archive.php"; break;
	}
	
	
	//search
	case "search": {		
		// echo "SEARCH - CASE<br>";
		// include "lang/eng/main.php";
		// include "lang/eng/windows_main.php";
		// include "lang/eng/search.php";		
		switch($related_platform) {
			case "":
				// New style
				include_once "templates4/search/index.php";
				// Old style
				// include_once "templates3/search/index.php";
				break;
			case "windows": include_once "templates3/windows/tags_search.php"; break;
			case "mac": include_once "templates3/mac/tags_search.php"; break;
			case "android": include_once "templates3/android/tags_search.php"; break;
			case "ios": include_once "templates3/ios/tags_search.php"; break;
		}
		break;
	}
	
	//search
	case "related": {	
		switch($related_platform) {
			case "": include_once "templates3/related/index.php"; break;
			case "windows": include_once "templates3/windows/tags_related.php"; break;
			case "mac": include_once "templates3/mac/tags_related.php"; break;
			case "android": include_once "templates3/android/tags_related.php"; break;
			case "ios": include_once "templates3/ios/tags_related.php"; break;
		}
		break;
	}	
	
	//About
	case "about": {		
		switch($action) {			
  			case "impressum": include_once "template/about/impressum.php"; break;
  			case "privacy": include_once "template/about/privacy.php"; break;
  			default: include "template/redirect.php"; break;
  		};
  		break;
	}
	
	//authors account
	case "account": {		
		include "lang/main_eng.php";
		include_once "lang/account.php"; 

  		if($userid > 0) {
			include "templates3/account/author_information.php";							
	  		switch($action) {
	  			case "company": include_once "templates3/account/company/index.php"; break;
				case "add": include_once "templates3/account/add/index.php"; break;
				case "windows": include_once "templates3/account/windows/index.php"; break;
				case "mac": include_once "templates3/account/mac/index.php"; break;
				case "android": include_once "templates3/account/android/index.php"; break;
				case "ios": include_once "templates3/account/ios/index.php"; break;
				case "giveaway": include_once "templates3/account/giveaway/index.php"; break;

				case "pay": include_once "templates3/account/pay/index.php"; break;
	  			case "submitsoftware": include_once "templates3/account/add_form.php"; break;
	  			case "submitmobile": include_once "templates3/account/submit_mobile.php"; break;
	  			case "submitmobilesave": include_once "templates3/account/submit_mobilesave.php"; break;
				case "edit": include_once "templates3/account/edit.php"; break;
	  			case "listedsoftware": include_once "templates3/account/listedsoftware.php"; break;
	  			case "hostingservice": include_once "templates3/account/hostingservice.php"; break;
	  			default: include_once "templates3/account/index.php"; break;
	  		};
	  		break;
	  	} else {			
	  		include_once "templates3/account/homepageno.php";
		}
		break;
	}
	
	/* Download basket */
	case "basket": {
		if(is_user()){
			include "lang/eng/windows_main.php";
			include "templates3/basket/download_basket.php"; break;			
		} else
			include "templates3/redirect-home.php";	
		break;
	}

	/* Articles */
	case "articles": {
		include "lang/eng/windows_main.php";		
		include "template/articles/config.php";			
		switch($action) {
			case "home": include_once "template/articles/index.php"; break;
			case "category": include_once "template/articles/category.php"; break;
			case "subcategory": include_once "template/articles/category.php"; break;
			case "single":
				// New style
				include_once "templates4/articles/single.php";
				// Old style
				// include_once "template/articles/single.php";
				break;				
			case "writers": include_once "template/articles/writers.php"; break;
		}
		break;		
	}
	
	default: {
		include "templates3/redirect-home.php";
	}
}	
?>