<?php
ini_set('max_execution_time', 30);
date_default_timezone_set('Asia/Tehran');
error_reporting(E_ERROR);
ini_set('display_errors', 'Off');

$custom 	= setCustom(@$_GET['id']);

$js_path 	= __DIR__ . '/speedcheck2.5.7.min.js';	
//$js_path 	= __DIR__ . '/speedcheck2.5.8.max.js';	
if( $custom['version'] == 2.52) {
    $js_path 	= __DIR__ . '/speedcheck2.5.2_tci.min.js';
// 	$js_path 	= __DIR__ . '/speedcheck-v2-tci.js';	
}
/**
 * Set result file
 */
$result = file_get_contents($js_path);
$result .= ";$(function(){ $.spc.boot(" . json_encode($custom+array('user_id'=>@(string)$_GET['id'])) . "); });";

/**
 * Set last modified time
 */
$last_modified = filemtime($js_path);
if(isset($custom['last_modified']) && strtotime($custom['last_modified']) > $last_modified) {
	$last_modified = strtotime($custom['last_modified']);
}

/*
 * Send the file OR send 304 Header
 *
 * It is important to specify one of Expires or Cache-Control max-age,
 * and one of Last-Modified or ETag, for all cacheable resources.
 *
 * It is redundant to specify both Expires and Cache-Control: max-age,
 * or to specify both Last-Modified and ETag.
 *
 * Last-Modified is weak caching for browsers but ETag can be weak or strong.
 */

//Check browsers request for IF_MODIFIED_SINCE
$tsstring 			= gmdate('D, d M Y H:i:s ', $last_modified) . 'GMT';
$if_modified_since 	= isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false;
if($if_modified_since && $if_modified_since == $tsstring) {
	logRequest(304);
    header('HTTP/1.1 304 Not Modified');
   	exit();
}
//Send file
logRequest(200);
header('Content-type: text/javascript');
header("Last-Modified: " . $tsstring);
header("Cache-Control:public, max-age=31536000");
print $result;
exit();







/*****************
 * Utility methods
 *****************/
function setCustom($id='') {
	switch($id)
	{
		case 'home':
				return array(
					'last_modified' => '2024-06-27 20:01:00',
					//'callback_url'	=> 'http://37.32.15.80/api/',
					'servers'		=> array(
						//array('name'	=> 'سرور Speedcheck فرانسه', 	'url'	=> 'http://176.31.142.212/schtml5/assets/'),
						//array('name'	=> 'سرور Speedcheck آلمان', 	'url'	=> 'http://144.76.178.94/assets/', 'ul_url' => 'upload.html'),
						array('name'	=> 'سرور Speedcheck تهران', 	'url'	=> 'http://185.239.106.174/assets/'),
						//array('name'	=> 'سرور Speedcheck تهران ۲', 	'url'	=> 'http://185.231.181.160/assets/')
					)
				);
		break;
		case 'dev':
			//if(in_referer(array('http://192.168.1.6', 'http://speedcheck.ir', 'http://www.speedcheck.ir', 'localhost'))) {
				return array(
					'last_modified' => '2016-05-15 07:37:00',
					'servers'		=> array(
						array('name'	=> 'سرور speedcheck تهران', 	'url'	=> './assets/'),
						//array('name'	=> 'سرور لایزر تبریز', 	'url'	=> 'http://www.laser.ir/assets/'),
						//array('name'	=> 'سرور لایزر آلمان', 	'url'	=> 'http://www.parsstyle.net/spc/assets/'),
						array('name'	=> 'سرور جهانی اینترنت', 	'url'	=> 'http://149.202.230.138/schtml5/assets/'),
						array('name'	=> 'سرور رایتل تهران', 	'url'	=> 'http://185.24.136.25/assets/'),
						array('name'	=> 'سرور رایتل تهران ۲', 'url'	=> 'http://91.229.214.130/assets/'),
						array('name'	=> 'سرور داخلی مبین نت', 	'url'	=> 'http://5.201.138.18/schtml5/assets/')
					),
                    'version'       =>  2.5
				);
			//}
		break;
		case 'hamara':
			if(in_referer(array('http://hamarasystem.net', 'http://www.hamarasystem.net','http://hamara.ir', 'http://www.hamara.ir', 'http://speedcheck.ir'))) {
				return array(
					'last_modified' => '2020-01-22 10:43:00',
					'callback_url'	=> 'https://www.hamarasystem.net/api.php',
					'servers'		=> array(
						//array('name'	=> 'سرور رشت', 	'url'	=> 'http://89.144.129.253/spc/'),
						//array('name'	=> 'سرور آلمان', 		'url'	=> 'http://91.109.16.226/spc/'),
						//array('name'	=> 'سرور آمریکا', 	'url'	=> 'http://45.55.167.1:8000/assets/'),
						array('name'	=> 'سرور تبریز', 	'url'	=> './assets/'),
						array('name'	=> 'سرور آلمان', 	'url'	=> 'http://www.parsstyle.net/spc/assets/')
					)
				);
			}
		break;
		case 'hamara_ir':
			if(in_referer(array('http://hamara.ir', 'http://www.hamara.ir', 'http://speedcheck.ir', 'http://www.speedcheck.ir'))) {
				return array(
					'last_modified' => '2020-01-22 10:43:00',
					'callback_url'	=> 'https://www.hamara.ir/api.php',
					'servers'		=> array(
						array('name'	=> 'سرور هماراسیستم - تبریز', 	'url'	=> 'http://85.185.3.20/assets/')
					//	array('name'	=> 'سرور Speedcheck - تهران', 	'url'	=> 'http://37.156.28.228/assets/')
					),
                    'off_label'     =>  false
				);
			}
		break;
		case 'newhamara':
			//if(in_referer(array('http://hamarasystem.net', 'http://www.hamarasystem.net', 'http://dev.hamarasystem.net', 'http://hamara.ir', 'http://www.hamara.ir', 'http://speedcheck.ir'))) {
				return array(
					'last_modified' => '2020-01-22 10:43:00',
					'callback_url'	=> 'https://www.hamara.ir/api.php',
					'colors' 		=> array(
												'neu'=> "#F1F3F6",
												'pri'=> "#2B74B2",
												'sec'=> "#4C8EFA",
												'hil'=> "#E34348",
												'txt'=> "#4B5E7C",
												'err'=> "#E34348",
												'bg' => "#FFF"
											),
					'servers'		=> array(
						//array('name'	=> 'سرور رشت', 	'url'	=> 'http://89.144.129.253/spc/'),
						array('name'	=> 'سرور تبریز', 	'url'	=> 'http://85.185.3.20/assets/')
						//array('name'	=> 'سرور تهران', 	'url'	=> 'http://www.speedcheck.ir/assets/'),
						//array('name'	=> 'سرور فرانسه', 		'url'	=> 'http://176.31.146.46/spc/assets/')
						//array('name'	=> 'سرور آلمان', 	'url'	=> 'http://www.parsstyle.net/spc/assets/')
						//array('name'	=> 'سرور آمریکا', 	'url'	=> 'http://45.55.167.1:8000/assets/')
					)
				);
			//}
		break;

		case 'mobinnet':
		case 'mobinnet2':
			//if(in_referer(array('http://mobinnet.net', 'http://www.mobinnet.net', 'http://mobinnet.ir'))) {
				return array(
					'last_modified' => '2021-01-05 12:08:00',
					'callback_url'	=> 'http://188.213.72.4/api/saveresult.php',
					'callback_custom'=> true,
					//'css_url'	    => 'http://5.201.138.18/schtml5/assets/css/sc-styles.css',
					'colors' 		=> array(
												'neu'=> "#E3E3E3",
												'pri'=> "#7DC201 ",
												'sec'=> "#57A700",
												'hil'=> "#399801",
												'txt'=> "#555",
												'bg' => "#FFF"
											),
					'servers'		=> array(
                                            array('name'	=> 'سرور داخلی مبین نت', 	'url'	=> 'http://188.213.72.4/schtml5/assets/'),
                                            array('name'	=> 'سرور جهانی اینترنت', 	'url'	=> 'http://51.254.87.70/schtml5/assets/')
                                            ),
                    'mode'          =>   'full',
                    'serial_test'   =>   true,
                    'context'       =>  "<br>
                                        <div id='sc-corner'>
                                            <div id='sc-white-label' class='sc-hide'></div>
                                            <a id='sc-close-button' class='sc-hide sc-corner-button' href='#' title=''><span class='spc-icon-cancel'></span></a>
                                        </div>
                                        <div id='sc-meter' class='sc-meter active'>
                                            <canvas id='sc-canvas'></canvas>
                                            <div id='sc-meter-core' class='sc-meter-core'>
                                                <h1 id='sc-vbox'><span class='spc-icon-meter'></span></h1>
                                                <span id='sc-ubox'></span>
                                            </div>
                                            <span id='sc-spinner' class='sc-spinner sc-hide'></span>
                                            <button id='sc-action-button' class='sc-button sc-fa'></button>
                                        </div>
                                        <div id='sc-mode-select' class='sc-mode-select sc-clear sc-hide'>
                                            <button id='sc-full-mode' class='sc-radio-button sc-fa'>تست دقیق</button>
                                            <button id='sc-quick-mode' class='sc-radio-button sc-fa'>تست فوری</button>
                                        </div>
                                        <div id='sc-results' class='sc-results sc-clear sc-hide'>
                                            <h3 id='sc-subtitle' class='sc-fa' style='text-align:center'></h3>
                                            <div id='sc-ping-result' class='sc-13grid sc-hide'>
                                                <h2 class='sc-fa sc-disabled' title='Ping Rate'><span class='spc-icon-ping'></span> زمان پاسخ</h2>
                                                <em>&nbsp;</em><span class='sc-unit'></span>
                                                <span class='sc-comment'>&nbsp;</span>
                                            </div>
                                            <div id='sc-dl-result' class='sc-13grid sc-hide'>
                                                <h2 class='sc-fa sc-disabled' title='Download Rate'><span class='spc-icon-down'></span> سرعت دانلود</h2>
                                                <em>&nbsp;</em><span class='sc-unit'></span>
                                            </div>
                                            <div id='sc-ul-result' class='sc-13grid sc-hide'>
                                                <h2 class='sc-fa sc-disabled' title='Upload Rate'><span class='spc-icon-up'></span> سرعت آپلود</h2>
                                                <em>&nbsp;</em><span class='sc-unit'></span>
                                            </div>
                                        </div>
                                        <br>",
                    'version'       =>   2.5
				);
        
			//}
		break;
		case 'test':
			//if(in_referer(array('http://mobinnet.net', 'http://www.mobinnet.net', 'http://mobinnet.ir'))) {
				return array(
					'last_modified' =>  '2016-08-29 01:16:00',
					'colors' 		=>  array(
												'neu'=> "#E3E3E3",
												'pri'=> "#7DC201 ",
												'sec'=> "#57A700",
												'hil'=> "#399801",
												'txt'=> "#555",
												'bg' => "#FFF"
											),
					'servers'		=>  array(
						                    array('name'	=> 'سرور Speedcheck تهران', 	'url'	=> 'http://www.speedcheck.ir/assets/'),
                                            array('name'	=> 'Localhost', 	'url'	=> '/assets /')
                                            ),
                    'context'       =>  "<h1 id='sc-title' class='sc-fa'>Custom DOM</h1>
                                        <br>
                                        <div id='sc-corner'>
                                            <div id='sc-white-label' class='sc-hide'></div>
                                            <a id='sc-close-button' class='sc-hide sc-corner-button' href='#' title=''><span class='spc-icon-cancel'></span></a>
                                        </div>
                                        <div id='sc-meter' class='sc-meter active'>
                                            <canvas id='sc-canvas'></canvas>
                                            <div id='sc-meter-core' class='sc-meter-core'>
                                                <h1 id='sc-vbox'><span class='spc-icon-meter'></span></h1>
                                                <span id='sc-ubox'></span>
                                            </div>
                                            <span id='sc-spinner' class='sc-spinner sc-hide'></span>
                                            <button id='sc-action-button' class='sc-button sc-fa'></button>
                                        </div>
                                        <div id='sc-mode-select' class='sc-mode-select sc-clear sc-hide'>
                                            <button id='sc-full-mode' class='sc-radio-button sc-fa'>تست دقیق</button>
                                            <button id='sc-quick-mode' class='sc-radio-button sc-fa'>تست فوری</button>
                                        </div>
                                        <div id='sc-results' class='sc-results sc-clear sc-hide'>
                                            <h3 id='sc-subtitle' class='sc-fa' style='text-align:center'></h3>
                                            <div id='sc-ping-result' class='sc-13grid sc-hide'>
                                                <h2 class='sc-fa sc-disabled' title='Ping Rate'><span class='spc-icon-ping'></span> زمان پاسخ</h2>
                                                <em>&nbsp;</em><span class='sc-unit'></span>
                                                <span class='sc-comment'>&nbsp;</span>
                                            </div>
                                            <div id='sc-dl-result' class='sc-13grid sc-hide'>
                                                <h2 class='sc-fa sc-disabled' title='Download Rate'><span class='spc-icon-down'></span> سرعت دانلود</h2>
                                                <em>&nbsp;</em><span class='sc-unit'></span>
                                            </div>
                                            <div id='sc-ul-result' class='sc-13grid sc-hide'>
                                                <h2 class='sc-fa sc-disabled' title='Upload Rate'><span class='spc-icon-up'></span> سرعت آپلود</h2>
                                                <em>&nbsp;</em><span class='sc-unit'></span>
                                            </div>
                                        </div>
                                        <br>
                                        <!-- <div id='sc-help-wrapper' class='sc-hide'>
                                            <iframe src='http://www.speedcheck.ir/help.html'></iframe>
                                        </div> -->",
                    'mode'          =>  'quick',
                    'serial_test'   =>  true,
                    'version'       =>  2.5
				);
        
			//}
		break;
        case 'tci':
			//if(in_referer(array('http://hamarasystem.net', 'http://www.hamarasystem.net', 'http://dev.hamarasystem.net', 'http://hamara.ir', 'http://www.hamara.ir', 'http://speedcheck.ir'))) {
				return array(
					'last_modified' => '2017-04-03 12:18:16',
					'colors' 		=> array(
												'neu'=> "#C2DBFC",
												'pri'=> "#4985D2",
												'sec'=> "#4985D2",
												'hil'=> "#7CBEFE",
												'txt'=> "#78788B",
												'err'=> "#E34348",
												'bg' => "#FFF"
											),
					'servers'		=> array(
                        //array('name'	=> 'Localhost', 	'url'	=> '/assets/'),
						//array('name'	=> 'سرور Speedcheck تهران', 'url'    => 'http://www.speedcheck.ir/assets/'),
						//array( 'name'	=> 'teh test', 'url'	=> 'http://80.191.61.193/assets/'),
                        //array( 'name'	=> 'home local','url'	=> 'http://192.168.8.100/assets/'),
						array( 'name'	=> 'سرور تهران'
                             , 'url'	=> 'http://80.191.61.199/assets/')
                        ,
						array( 'name'	=> 'سرور بین‌الملل - فرانسه'
                             , 'url'    => 'http://176.31.146.46/spc/assets/')
                        ,
						array( 'name'	=> 'سرور آذربايجان شرقی'
                             , 'url'	=> 'http://217.219.130.76/assets/')
                        ,
						array( 'name'	=> 'سرور آذربايجان غربی'
                             , 'url'	=> 'http://80.191.199.3/assets/')
                        ,
						array( 'name'	=> 'سرور اردبیل'
                             , 'url'	=> 'http://85.185.200.161/assets/')
                        ,
						array( 'name'	=> 'سرور اصفهان'
                             , 'url'	=> 'http://37.255.128.18/assets/')
                        ,
						array( 'name'	=> 'سرور ایلام'
                             , 'url'	=> 'http://217.219.200.141/assets/')
                        ,
						array( 'name'	=> 'سرور بوشهر'
                             , 'url'    => 'http://217.219.116.116/assets/')
                        ,
						array( 'name'	=> 'سرور چهارمحال و بختياری'
                             , 'url'	=> 'http://78.39.33.30/assets/')
                        ,
						array( 'name'	=> 'سرور خراسان جنوبی'
                             , 'url'	=> 'http://85.185.85.26/assets/')
                        ,
						array( 'name'	=> 'سرور خراسان رضوی'
                             , 'url'	=> 'http://217.219.65.243/assets/')
                        ,
						array( 'name'	=> 'سرور خراسان شمالی'
                             , 'url'	=> 'http://85.185.81.212/assets/')
                        ,
						array( 'name'	=> 'سرور خوزستان'
                             , 'url'	=> 'http://78.39.202.73/assets/')
                        ,
						array( 'name'	=> 'سرور زنجان'
                             , 'url'	=> 'http://85.185.16.170/assets/')
                        ,
						array( 'name'	=> 'سرور سمنان'
                             , 'url'	=> 'http://217.218.240.117/assets/')
                        ,
						array( 'name'	=> 'سرور سیستان و بلوچستان'
                             , 'url'	=> 'http://217.219.88.20/assets/')
                        ,
						array( 'name'	=> 'سرور فارس'
                             , 'url'	=> 'http://151.232.1.3/assets/')
                        ,
						array( 'name'	=> 'سرور قزوین'
                             , 'url'	=> 'http://217.218.235.68/assets/')
                        ,
						array( 'name'	=> 'سرور قم'
                             , 'url'	=> 'http://80.191.93.150/assets/')
                        ,
						array( 'name'	=> 'سرور کردستان'
                             , 'url'	=> 'http://217.219.223.195/assets/')
                        ,
						array( 'name'	=> 'سرور کرمان'
                             , 'url'	=> 'http://78.39.124.2/assets/')
                        ,
						array( 'name'	=> 'سرور کرمانشاه'
                             , 'url'	=> 'http://46.100.176.242/assets/')
                        ,
						array( 'name'	=> 'سرور کهگیلویه و بویراحمد'
                             , 'url'	=> 'http://85.185.191.102/assets/')
                        ,
						array( 'name'	=> 'سرور گلستان'
                             , 'url'	=> 'http://78.38.92.2/assets/')
                        ,
						array( 'name'	=> 'سرور گیلان'
                             , 'url'	=> 'http://217.219.186.228/assets/')
                        ,
						array( 'name'	=> 'سرور لرستان'
                             , 'url'	=> 'http://217.219.208.59/assets/')
                        ,
						array( 'name'	=> 'سرور مازندران'
                             , 'url'	=> 'http://217.219.162.98/assets/')
                        ,
						array( 'name'	=> 'سرور مرکزی'
                             , 'url'	=> 'http://80.191.105.6/assets/')
                        ,
						array( 'name'	=> 'سرور هرمزگان'
                             , 'url'	=> 'http://78.39.110.118/assets/')
                        ,
						array( 'name'	=> 'سرور همدان'
                             , 'url'	=> 'http://78.39.209.38/assets/')
                        ,
						array( 'name'	=> 'سرور یزد'
                             , 'url'	=> 'http://78.39.84.98/assets/')
					),
					'css_url'	    => 'css/sc-style-tci.css',
                    //'context'       =>  "",
                    'mode'          =>  'full',
                    'off_label'     =>  true,
                    'version'       =>  2.52
				);
			//}
		break;
    }

	return array();
}

function in_referer($inputs=array()) {
	if(!is_array($inputs)) $inputs = array($input);
	foreach($inputs as $input) {
		if(strpos($_SERVER["HTTP_REFERER"], $input) === 0) return TRUE;
	}
	return FALSE;
}

function logRequest($response=200) {
	$log = date("Y-m-d H:i:s").
		"\t".$response.
		"\t".@$_GET['id'].
		"\t".$_SERVER['REMOTE_ADDR'].
		"\t".$_SERVER["HTTP_REFERER"].
		"\t".$_SERVER['HTTP_USER_AGENT'].
        PHP_EOL;
	@file_put_contents(__DIR__ . '/logs/'.date("Y-m").'.txt', $log, FILE_APPEND);
}
