<?php
/*  arduino.php?device_id=dev&password=06ba75c4153bbdebb62d0a0e6f35f5682417f690  */
include_once('php/functions.php');
messages_time();
$password = clean($_GET['password']); 

if(isset($_GET['message_id'])){
	if ( (filter_var($_GET['message_id'], FILTER_VALIDATE_INT)) && ($_GET['message_id'] ==  strval(intval($_GET['message_id'])))){
			$messageid = clean($_GET['message_id']); 
	}
}

if(isset($_GET['device_id']) && isset($password)){
	if (check("device", $_GET['device_id'], $password) == 1){
		if(isset($messageid)){
			data_on();
			$update = "UPDATE status_message SET `report` = 'yes', `read` = 'yes', date = NOW() WHERE message_id = '{$messageid}' AND device_id = '{$_GET['device_id']}'";
			$back = '<a href="edit_all_messages.php?edit_id='.$messageid.'" title="Κάντε κλικ για να επεξεργαστείτε το μήνυμα">Η συσκευή: '.$_GET['device_id'].' <h8>διάβασε</h8> με επιτυχία το μήνυμα με id = '.$messageid.'</a>';
						
			mysql_query($update) or die('error');
			
			in_logfile($back,-1);
		}
		else {
			if(get('status','device','id',$_GET['device_id'],'one')=="yes"){
				data_on();
				$sql = "SELECT * FROM status_message WHERE device_id = '{$_GET['device_id']}' AND `read` = 'no' ORDER BY DATE ASC";
				$result = mysql_query($sql) or die('error');
				$display = "no";
				$message_id = -1;
				while(($row = mysql_fetch_array($result)) && ($display == "no" )){
					if(get('display','message','id',$row['message_id'],'one')=="yes"){
						$display = "yes";
						$message_id = get('id','message','id',$row['message_id'],'one');
						$status_message_id = $row['id'];
					}
				}
				if($message_id<>-1){
					$arr = array();
			
					
					if(get('ring','message','id',$message_id,'one')=="yes") $ring = "1";
					else $ring = "0";
					if(get('light','message','id',$message_id,'one')=="yes") $light = "1";
					else $light = "0";
					$date = fix_date(get('date','message','id',$message_id,'one')).' '.fix_hour(get('date','message','id',$message_id,'one'));
					$user_id = get('user_id','message','id',$message_id,'one');
					if($user_id==0) {
						$message = get('message','message','id',$message_id,'one');
						$user = null;
						$iii = 1;
						while($message[$iii]<>'%'){
							$user .= $message[$iii];
							$iii ++;
						}
						$iii ++;
						$message = substr($message,$iii);
					}
					else {
						$message = get('message','message','id',$message_id,'one');
						$user = get('f_name','users','id',$user_id,'one').' '.get('l_name','users','id',$user_id,'one');
					}
					$arr[0] = $ring;
					$arr[1] = $light;
					$arr[2] = $user;
					
					// $num_char = strlen($user)-1;
					// for($counter =0; $counter <= $num_char; $counter++)	{
						// $arr[2] .= ord($user{$counter})."|";
					// }
					$arr[3] = $date;
					$m = null;
					$f1 = 20;
					$f2 = 21;
					for($i = 0; $i<mb_strlen($message,'UTF-8'); $i++){
						/*if($i==$f1){$f1 = $f1+22;}
						else if($i==$f2){$f2 = $f2+22;}*/
if (preg_match("/[^A-Za-z0-9\/\'\(\)\=\<\>\+\-\_\,\!\?\;\:\.\!\' ']/",$message[$i])){
	$m.= ' ';
}
else $m.=$message[$i];
					}
					$count = 0;
					$i = 0;
					$arr[4] = null;
					while($i<mb_strlen($message,'UTF-8')){
						if($m[$i]==' ' && (isset($m[$i+1]) && $m[$i+1]==' ')){
							$arr[4] .= '|';
							$i = $i+2;
						}
						else {
							$arr[4] .= $m[$i];	
							$i = $i+1;				
						}
					}
					/*
					
					for($i = 0; $i<mb_strlen($message,'UTF-8'); $i++){				
						if($m[$i]==' ' && $m[$i+1]==' '){
							$arr[4] .= '|';
						}
						else if($m[$i]==' ' && $m[$i-1]<>' '){
							$arr[4] .= $m[$i];					
						}					
					}*/
					$arr[5] = $message_id;	
echo '
{@'.$arr[0].'#'.$arr[1].'$'.$arr[2].'%'.$arr[3].'^'.$arr[4].'&'.$arr[5].'}';

				}
				else{	
					date_default_timezone_set('Europe/Athens');
					echo "{*".strtotime(date('Y-m-d H:i:s'))."}";
				}				
				data_off();			
			}
		}
	}
	else{	
		date_default_timezone_set('Europe/Athens');
		echo "{*".strtotime(date('Y-m-d H:i:s'))."}";
	}	
	// header('Location: index.php');
}
else{	
	date_default_timezone_set('Europe/Athens');
	echo "{*".strtotime(date('Y-m-d H:i:s'))."}";
}	
?>