<?php
/*
 * HeatMapTracker
 * (c) 2013. HeatMapTracker
 * http://heatmaptracker.com
 */
?>
<?php
//secure check $_POST variables
$_POST["user"] = ViberFN::viber_secure($_POST["user"]);

//save spy data to db
if(isset($_POST['user'])){
    	
    $option = $this->OPTIONS;
	global $wpdb;
		

//if(isset($this -> OPTIONS['opt_record_tz'])) date_default_timezone_set($this -> OPTIONS['opt_record_tz']);
		
//stack the data
$sessions = array();
$click_arr = array();
$mmove_arr = array();
$scroll_arr = array();
$popular_arr = array();
if(isset($_POST['data'])) {
	$data = json_decode(base64_decode($_POST['data']));
	foreach ($data as $key => $value) {
		
		if(!isset($sessions[$key]["time"])) $sessions[$key]["time"] = 0;
		
		foreach ($value as $kk => $vv) {//pages lvl
			
			$scroll_arr[$kk]["height"] = 0;
			$scroll_arr[$kk]["maxscroll"] = 0;
			$popular_arr[$kk] = $option["opt_record_interval"];
			foreach ($vv as $kkk => $vvv) {//event lvl
				foreach ($vvv as $kkkk => $vvvv) {//events arr lvl
					if($kkk != "responsetive")
						if($sessions[$key]["time"] < (int)($vvvv[0])) $sessions[$key]["time"] = (int)($vvvv[0]);
					if($kkk == "mouse_click"){
						if(isset($click_arr[$kk]))
								$click_arr[$kk] .= "|".$vvvv[2]." ".$vvvv[3]." ".$vvvv[6];
							else
								$click_arr[$kk] = "|".$vvvv[2]." ".$vvvv[3]." ".$vvvv[6];
					}
					if($kkk == "mouse_move"){
						if(isset($mmove_arr[$kk]))
								$mmove_arr[$kk] .= "|".$vvvv[1]." ".$vvvv[2]." ".$vvvv[3];
							else
								$mmove_arr[$kk] = "|".$vvvv[1]." ".$vvvv[2]." ".$vvvv[3];
					}
					if($kkk == "page_scroll"){
						if($scroll_arr[$kk]["maxscroll"] < $vvvv[1]) $scroll_arr[$kk]["maxscroll"] = $vvvv[1];
					}
					if($kkk == "window_size"){
						if($scroll_arr[$kk]["height"] < $vvvv[1]) $scroll_arr[$kk]["height"] = $vvvv[1];
					}
				}
			}
			$sessions[$key]['data'][]=array($kk=>$vv);
		}
	}
}
		//put clicks to DB
		$table2 = $wpdb->prefix.$option['dbtable_name_clicks'];
		foreach ($click_arr as $key => $value) {
		
			$clicks = $wpdb->get_row("SELECT * FROM $table2 WHERE `page_url` = '$key' ORDER BY `id` DESC LIMIT 1");
			if(!$clicks){
				$q = "INSERT INTO `".$table2."` (`page_url`,`click_data`,`date`) VALUES ('".$key."','".$value."', NOW())";
				$wpdb->query($q);
			} else {
				if($clicks->click_data != ""){
					$clickStr = $clicks->click_data;
				}
				if(strlen($clickStr) > 600 || date("m.d.y") != date("m.d.y", strtotime($clicks->date))){
					$q = "INSERT INTO `".$table2."` (`page_url`,`click_data`,`date`) VALUES ('".$key."','".$value."', NOW())";
					$wpdb->query($q);
				} else {
					$clickStrMerged = $clickStr.$value;
					$q = "UPDATE `".$table2."` SET  `click_data` = '".$clickStrMerged."' WHERE `page_url` = '".$key."' ORDER BY `id` DESC LIMIT 1 ";
					$wpdb->query($q);
				}
			}
		}
		
		//put mmove to DB
		$table3 = $wpdb->prefix.$option['dbtable_name_mmove'];
		foreach ($mmove_arr as $key => $value) {
		
			$clicks = $wpdb->get_row("SELECT * FROM $table3 WHERE `page_url` = '$key' ORDER BY `id` DESC LIMIT 1");
			if(!$clicks){
				$q = "INSERT INTO `".$table3."` (`page_url`,`mmove_data`,`date`) VALUES ('".$key."','".$value."', NOW())";
				$wpdb->query($q);
			} else {
				if($clicks->mmove_data != ""){
					$clickStr = $clicks->mmove_data;
				}
				if(strlen($clickStr) > 600 || date("m.d.y") != date("m.d.y", strtotime($clicks->date))){
					$q = "INSERT INTO `".$table3."` (`page_url`,`mmove_data`,`date`) VALUES ('".$key."','".$value."', NOW())";
					$wpdb->query($q);
				} else {
					$clickStrMerged = $clickStr.$value;
					$q = "UPDATE `".$table3."` SET  `mmove_data` = '".$clickStrMerged."' WHERE `page_url` = '".$key."' ORDER BY `id` DESC LIMIT 1 ";
					$wpdb->query($q);
				}
			}
		}
		
		//put scroll to DB
		$table4 = $wpdb->prefix.$option['dbtable_name_scroll'];
		foreach ($scroll_arr as $key => $value) {
		
			$clicks = $wpdb->get_row("SELECT * FROM $table4 WHERE `page_url` = '$key' ORDER BY `id` DESC LIMIT 1");
			if(!$clicks){
				$q = "INSERT INTO `".$table4."` (`page_url`,`scroll_data`,`date`) VALUES ('".$key."','".($value["height"]+$value["maxscroll"])."', NOW())";
				$wpdb->query($q);
			} else {
				if($clicks->scroll_data != ""){
					$clickStr = $clicks->scroll_data;
				}
				if(strlen($clickStr) > 600 || date("m.d.y") != date("m.d.y", strtotime($clicks->date))){
					$q = "INSERT INTO `".$table4."` (`page_url`,`scroll_data`,`date`) VALUES ('".$key."','".($value["height"]+$value["maxscroll"])."', NOW())";
					$wpdb->query($q);
				} else {
					$clickStrMerged = $clickStr."|".($value["height"]+$value["maxscroll"]);
					$q = "UPDATE `".$table4."` SET  `scroll_data` = '".$clickStrMerged."' WHERE `page_url` = '".$key."' ORDER BY `id` DESC LIMIT 1 ";
					$wpdb->query($q);
				}
			}
		}
		
		//put popular to DB
		$table5 = $wpdb->prefix.$option['dbtable_name_popular'];
		foreach ($popular_arr as $key => $value) {
		
			$clicks = $wpdb->get_row("SELECT * FROM $table5 WHERE `page_url` = '$key' LIMIT 1");
			if(!$clicks){
				$q = "INSERT INTO `".$table5."` (`page_url`,`points`) VALUES ('".$key."',".$value.")";
				$wpdb->query($q);
			} else {
				$pnts = 0;
				if($clicks->points != ""){
					$pnts = $clicks->points;
				}
				$pnts += $value;
				$q = "UPDATE `".$table5."` SET  `points` = ".$pnts." WHERE `page_url` = '".$key."' LIMIT 1 ";
				$wpdb->query($q);
			}
		}
		
		
				
	$table = $wpdb->prefix.$option['dbtable_name'];
	
		
	foreach ($sessions as $session_id => $post) {
	$data = $post['data'];
	$session = $wpdb->get_row("SELECT * FROM $table WHERE session_id = '$session_id'");
	$session_array = array();
	// if session number exist:
	if($session != null){
		if(isset($session->session_spydata)){
			$session_array = json_decode($session->session_spydata);
		}
		//get last page key
		$lastkey = "";
		foreach ($session_array[count($session_array)-1] as $key => $value) {
			$lastkey = $key; break;
		}
		//get first page key
		$firstkey = "";
		foreach ($data[0] as $kkey => $vvalue) {
			$firstkey = $kkey;	break;
		}
		

		$start = ($firstkey == $lastkey)?1:0;
		foreach ($data as $kkey => $vvalue) {
			if($kkey >= $start)
				$session_array[] = $vvalue;
			else {
				foreach ($data[0] as $k2 => $v2) {
					foreach ($v2 as $k3 => $v3) {
						if(!isset($session_array[count($session_array)-1]->$k2->$k3)) $session_array[count($session_array)-1]->$k2->$k3 = array();
						$session_array[count($session_array)-1]->$k2->$k3 = array_merge($session_array[count($session_array)-1]->$k2->$k3,$v3);
					}
				}
			}
		}
		
		
		$time = 0; $sum = 0;
		foreach ($session_array as $k => $v){
			foreach ($v as $kk => $vv) {
				foreach ($vv as $kkk => $vvv){
					if($kkk != "responsetive"){
						foreach ($vvv as $kkkk => $vvvv){
							if($time < (int)$vvvv[0]) $time = (int)$vvvv[0];
						}
					}
				}
			}
			
			$sum += (int)$time; $time = 0;
		}
		//update db record
		$wpdb->update( 
			$table, 
			array(
				'session_end' => ($session->session_start + $sum+1),
				'session_spydata' => json_encode($session_array)
			),
			array( 'session_id' => $session_id )); 
	}
	// if we have new session just insert new record
	else if($post['time'] > 0) {
		$q = "INSERT INTO `".$table."` (`user_id`,`session_id`,`session_start`,`session_end`,`session_time`,`session_spydata`) VALUES ('".$_POST['user']."','".$session_id."','".(time()-$post['time'])."','".time()."','".time()."','".json_encode($data)."')";
		$wpdb->query($q);
	}

	}//end foreach


}//end if
?>