<!--script>
(function worker() {
  $.ajax({
    url: 'replic.php', 
    success: function(data) {
      $('.result').html(data);
    },
    complete: function() {
      // Schedule the next request when the current one's complete
      setTimeout(worker, 100000);
    }
  });
})();
</script-->
<?PHP
include 'Encryption.php';
ini_set('register_globals', 'ON');
ini_set('max_execution_time', '36000'); //300 seconds = 5 minutes
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
/**
* Koneksi PHP ke MySQL
* date: Juni 2015
* tujuan: Replication data
* @author credit to Windu <winduprimaesa@gmail.com>
*/
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;

// origin
define("DB_HOST1","localhost");
define("DB_USER1","root");
define("DB_PASS1","");
define("DB_NAME1","fews_clc_site");

// CONNECT TO MYSQLI
$dblink1=($GLOBALS["___mysqli_ston"] = mysqli_connect(DB_HOST1,  DB_USER1,  DB_PASS1)); 
((bool)mysqli_query( $dblink1, "USE " . DB_NAME1));  

// destination
define("DB_HOST2","VXlzeqCnk1pmYGFvbg==");
define("DB_USER2","l62hwNTiypCQm5OumZeR");
define("DB_PASS2","bay1vOSolVRm");
define("DB_NAME2","l62hwNTiypCQm5Ou");

$host=Encryption::decrypt(DB_HOST2);
$user=Encryption::decrypt(DB_USER2);
$pass=Encryption::decrypt(DB_PASS2);
$dbname=Encryption::decrypt(DB_NAME2);
// CONNECT TO MYSQLI
$dblink2=($GLOBALS["___mysqli_ston"] = mysqli_connect($host,  $user,  $pass)); 
((bool)mysqli_query($dblink2, "USE " . $dbname)); 

//BERDASARKAN ID LOKASI SERVER BALAI
//$SQL="SELECT TRIM(nama_lokasi) AS table_name FROM m_lokasi WHERE 1=1 AND id_merk='FEWS' AND id_bbws='1'";
//$res_from = mysqli_query($dblink1,$SQL,$connmaster);        

$res_from = mysqli_query( $dblink1, "show tables in fews_clc_site
where tables_in_fews_clc_site like 'sugutamu' ");
//*/
if($res_from === FALSE) {
    die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); // for error handling
}

while($table = mysqli_fetch_array($res_from)) { 
echo($table[0] . "<br />"); // optional, only for show the name tables

$table=mysqli_real_escape_string($dblink1,$table[0]);

$querycreate = mysqli_query($GLOBALS["___mysqli_ston"], "CREATE TABLE IF NOT EXISTS $table (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `ReceivedDate` date NOT NULL,
  `ReceivedTime` time NOT NULL,
  `SamplingDate` date NOT NULL,
  `SamplingTime` time NOT NULL,
  `Rain` int(11) NOT NULL,
  `WLevel` int(11) DEFAULT NULL,
  `ML` int(11) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ");

if($querycreate === FALSE) { 
    die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); // TODO: better error handling
}

//$tableinfo = mysql_fetch_array(mysql_query("SHOW CREATE TABLE $table  ",$dblink1)); // retrieve table structure from mysite1.com 
while($tableinfo = mysqli_fetch_array(mysqli_query($dblink1, "$querycreate")));
{}
mysqli_query($dblink2, " $tableinfo[1] "); // use found structure to make table on mysite2.com

$res_to = mysqli_query($dblink1, "SELECT ReceivedDate,ReceivedTime,SamplingDate,SamplingTime,Rain,WLevel FROM $table  WHERE SamplingDate=curdate() AND SamplingTime > DATE_SUB(now(), INTERVAL 5 MINUTE) "); // select all content from table mysite1.com every 5 minute
while ($row = mysqli_fetch_array($res_to,  MYSQLI_ASSOC) ) {
    $sql_error = "";
    mysqli_query($dblink2, "INSERT IGNORE INTO $table (".implode(", ",array_keys($row)).") VALUES ('".implode("', '",array_values($row))."')"); // insert one row into new table on mysite2.com`enter code here`
    $sql_error = ((is_object($dblink2)) ? mysqli_error($dblink2) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false));
}
if ($sql_error) {
    echo $sql_error;
}else {
    echo "copy table ".$table." done.<br />";
}
    flush();
}

((is_null($___mysqli_res = mysqli_close($dblink1))) ? false : $___mysqli_res); 
((is_null($___mysqli_res = mysqli_close($dblink2))) ? false : $___mysqli_res);
?>
<br><br>
<?php
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 4);
echo "Selesai dalam ".$total_time." detik";
?>
<br>
Website :
<?php
	ini_set("default_socket_timeout","05");
	set_time_limit(5);
	$f=fopen('http://www.sdatelemetry.com',"r");
	$r=fread($f,1000);
	fclose($f);
	if(strlen($r)>1) {
		echo("Online");
	}
	else {
		echo("Offline");
	}
?>
<br>
Localhost :
<?php
	$ip = '127.0.0.1'; //some ip
	exec("ping -n 4 $ip 2>&1", $output, $retval);
	if ($retval != 0) { 
		echo "no!"; 
	} 
	else 
	{ 
		echo "yes!"; 
	}
?>