<?php
	if( !function_exists('parse_json') ){
		function parse_json($data){
			$preg = "/\{\"message\":.*\}/";
			preg_match_all($preg,$data,$return);

			$len = count($return[0]);

			//$preg2 = "/[\"|\']\d[\"|\']:.*[^\}^\]]\}\]/";
			$preg2 = "/.*[^\}^\]]\}\]\}\]\}/";

			for($i=0;$i<$len;$i++){
				preg_match_all($preg2,$return[0][$i],$res2);
			}

			return $res2;
		}
	}
	
	@$_state = $_POST['state'];
	
	if( $_state == 'user_settings' ){ 
		$username = $_POST['username'];
		$password = $_POST['password'];
		$port = $_POST['port'];
		$ip = $_POST['ip_addr'];
		$board = $_POST['board'];
		
		if( empty($username)|| empty($password) ){
			return ;
		}
		
		$sql = "replace smsender_user (username,password,port,ip_addr,board) values ('{$username}','{$password}','{$port}','{$ip}','{$board}')";
		$res = $db->query($sql);
		if( $res ){
			if( function_exists('json_encode') ){
				echo json_encode(array('user_settings'=>'ok'));
				exit;
			}else{
				echo '{"user_settings":"ok"}';
				exit;
			}
		}
	}
		
	if( $_state == 'send_msg' ){
		send_msg($_POST);
		exit;
	}
	
	/*---------------------------------------------------------------------------------------------------*/

	if( isset($_GET['del_msg']) && ($_GET['del_msg'] == 'del_msg') ){
		$arr = $_GET['id'];
		$sql = 'DELETE FROM smsender_messages where id in ('.$arr.')';
		echo $db->query($sql);
		exit;
	}
	
	/*---------------------------------------------------------------------------------------------------*/
	if( isset($_GET['cleanup']) && ($_GET['cleanup'] == 'cleanup') ){
		$sql = 'delete from smsender_messages';
		echo $db->query($sql);
		exit;
	}
	
	/*---------------------------------------------------------------------------------------------------*/
	if( isset($_GET['save_ip']) ){
		$ip = $_GET['ip_addr'];
		$sql = 'update smsender_user set ip_addr ="'.$ip.'"';
		$res = $db->query($sql);
		exit;
	}
	
	/*---------------------------------------------------------------------------------------------------*/
	function send_msg($data){
		$ip 	 = $data['sms_ip'];
		$number  = $data['sms_number'];
		$content = $data['sms_content'];
		$port 	 = $data['sms_port'];
		//$url 	 = 'http://'.$ip.'/sendsms?username=admin&password=admin&phonenumber='.$number.'&message='.$content.'&port='.$port;
		$username   = $data['sms_username'];
		$password   = $data['sms_password'];
		$url 	 = 'http://'.$ip.'/sendsms?username='.$username.'&password='.$password.'&phonenumber='.$number.'&message='.$content.'&port='.$port;

		//var_dump($url);
		//http://172.16.179.1/sendsms?username=admin&password=admin&phonenumber=10086&message=YE&port=gsm-1.1
		//http://172.16.179.1/sendsms?username=admin&password=admin&phonenumber=10086&message=YE&port=gsm-4.2
		
		$ch = curl_init();
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch,CURLOPT_HEADER,0);
		curl_setopt($ch,CURLOPT_TIMEOUT,60);
		$res = curl_exec($ch);
		curl_close($ch);
			
		$res2 = parse_json($res);
		echo $res2[0][0];
	}
	

