<?php
	function checkInstall(){
		global $db,$amp_conf;
		
		$sql = "CREATE TABLE IF NOT EXISTS smsender (
			id int NOT NULL default 0,
			msg varchar(255) NOT NULL default '',
			flag tinyint(1) NOT NULL default 0,
			PRIMARY KEY (id)
		)";
		$check = $db->query($sql);
		if(DB::IsError($check)) {
			die_freepbx("Can not create smsender table");
		}

		$sql2 = "CREATE TABLE IF NOT EXISTS smsender_messages(
			id int primary key auto_increment,
			port varchar(10) not null default '',
			phone int not null default 0,
			message text(1000) not null default '',
			sendtime varchar(50)  not null default '',
			status varchar(10) not null default ''
		)";

		$check2 = $db->query($sql2);
		if(DB::IsError($check2)) {
			die_freepbx("Can not create smsender_messages table");
		}

		$sql3 = "CREATE TABLE IF NOT EXISTS smsender_user(
			id int not null default 0,
			username varchar(16) not null default '',
			password  varchar(16) not null default '',
			board varchar(4) not null default '',
			port   varchar(200) not null default '',
			ip_addr char(15) not null default '',
			PRIMARY KEY (id)
		)
		";

		$check3 = $db->query($sql3);
		if(DB::IsError($check3)) {
			die_freepbx("Can not create smsender_user table");
		}
	}
	function get_user_state(){
		global $db,$amp_conf;
		
		$sql = 'select * from smsender_user';
		$res = $db->query($sql);
		//var_dump($res);
		//return false;
		if( $res ){
			//echo '<pre>';
			//var_dump( get_class_methods(get_class($res)) );
			$res2 = $res->fetchRow();
			
			$port = $res2[3];
			$ports = explode(',',$port);
			$count = count($ports);
	
			$js_ports = '<script type="text/javascript">var settings_port=[];';
			for($i=0;$i<$count;$i++){
				$js_ports .= "settings_port[$i]=\"{$ports[$i]}\";";
			}
			echo $js_ports .= "</script>";
/*		
echo <<<STR
<script type="text/javascript">
	$(function(){
		$("#select_ports input[name='port']").each(function(){
			for(var i=0;i<=settings_port.length;i++){
				if( $(this).val() == settings_port[i] ){
					$(this).attr("checked",true);
				}
			}
		});
	})
</script>
STR;
*/
		return array('id'=>$res2[0],'username'=>$res2[1],'password'=>$res2[2],'port'=>$res2[3],'ip_addr'=>$res2[5],'board'=>$res2[5]);
		}
	}
/*
	function sms_curl($data){
		//curl
		$sms_ip = $data['sms_ip'];
		$sms_number = $data['sms_number'];
		$sms_content = $data['sms_content'];
		$sms_port = $data['sms_port'];
		
		$ch = curl_init();
		
		curl_setopt($ch,CURLOPT_URL,'http://'.$sms_ip.'/sendsms?username=admin&password=admin&phonenumber='.$sms_number.'&message='.$sms_content);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch,CURLOPT_HEADER,0);
		
		$res = curl_exec($ch);
		$res2 = curl_exec($ch);
		$res2 = explode('{"message":',$res);
		
		curl_close($ch);
		
		echo '<pre>';
		var_dump($res2);
	}
*/	
?>
