<?php
class Sms extends Controller {
	function Sms()
	{

		parent::Controller();
		$this->load->helper(array('form', 'url','smsstore_helper'));
	}

	function incoming()
	{
			
					error_reporting(1);
					$x =  urldecode($_SERVER['REQUEST_URI']);
					$exp = explode("/",$x);
					$na = array_reverse(array_filter($exp));
					
					$cell = $na[0];
					$message = $na[1];
					$dbID = $na[2];
					$ar = explode("#",$message);
					$p1 = strtoupper($ar[0]);
					$p2 = base64_decode($ar[1]);
					
					$recp = 0;
					
					$group_id = ltrim($p1,"GL");
					
					$sql9 = mysql_query("select  * from groups where id = '$group_id' ") or die(mysql_error());
					$row9 = mysql_fetch_array($sql9);
					$group_name = $row9['name'];
					
					$sql = mysql_query("select * from members as M,users as U where M.group_id = '$group_id' and U.number = '$cell' and U.id = M.member_uid  and M.pending1 = 0 ") or die(mysql_error());			
					if(mysql_num_rows($sql))
					{
					
							 $uID = NULL;
							 $sql11 = mysql_query("select * from users where number = '$cell'") or die(mysql_error());
							 $row = mysql_fetch_array($sql11);
							 $uID = $row['id'];
							 $sender_name = $row['username'];
							 
							//$p2 .= "$p1#$sender_name#";
							$p2 = preg_replace('/\'/',"\'",$p2);
							$p2 = "$p1#$sender_name#".$p2;
								 
							 $newSql = " insert into messages (id, txt, member_uid,group_id,time_stamp ) value (NULL,'$p2', '$uID','$group_id', '".time()."')  ";	
							 if($uID)
								 $sql = mysql_query($newSql) or die(mysql_error());
					
						$sql3 = mysql_query("select * from members as M,users as U where M.group_id = '$group_id' and U.id = M.member_uid  and M.pending1 = 0 group by U.id ") or die(mysql_error());
						while($row3 = mysql_fetch_array($sql3))
							{
								if($cell != $row3['number'])
								{
								     $newSql = " insert into messages (id, txt, member_uid,for_user_id,group_id,time_stamp ) value (NULL,'$p2', '$uID','".$row3['id']."',0, '".time()."')  ";	
					                 if($uID) $sql = mysql_query($newSql) or die(mysql_error());

					                 $recp ++;
									 $Phno = "1".$row3['number'];
									 smsstore_saveSmsInSqlDb("INSERT INTO wsoftsms_out (recipient, text, create_date) VALUES ('$Phno', '$p2', '".date("Y-m-d H:i:s")."')");		
								}
							}
							
							echo "Sent to $recp member";
		
							
					}
					else
					{
						echo "You are not member of this group";
					}
	}
	
	function quicktext()
	{
					$no = $this->uri->segment(3);
					
					$text = urldecode($this->uri->segment(4));
					
					smsstore_saveSmsInSqlDb("INSERT INTO wsoftsms_out (recipient, text, create_date) VALUES ('$no', '$text', '".date("Y-m-d H:i:s")."')");		
			
	
	}
	function doEntry($cell,$p2,$group_id)
	{
					 $uID = NULL;
					 $sql11 = mysql_query("select * from users where number = '$cell'") or die(mysql_error());
					 $row = mysql_fetch_array($sql11);
					 $uID = $row['id'];
					 $newSql = " insert into messages (id, txt, member_uid,group_id,time_stamp ) value (NULL,'$p2', '$uID','$group_id', '".time()."')  ";	
					 if($uID)
						 $sql = mysql_query($newSql) or die(mysql_error());
	}

	function incoming_old()
	{
			error_reporting(1);
			$x =  urldecode($_SERVER['REQUEST_URI']);
			$exp = explode("/",$x);
			$na = array_reverse(array_filter($exp));
			
			$cell = $na[0];
			$message = $na[1];
			$dbID = $na[2];

			$ar = explode("#",$message);
			$p1 = strtoupper($ar[0]);
			$p2 = $ar[1];
			
			$recp = 0;
			
			$group_id = ltrim($p1,"GL");
			$sql = mysql_query("select * from members as M,users as U where M.group_id = '$group_id' and U.number = '$cell' and U.id = M.member_uid  and M.pending1 = 0 ") or die(mysql_error());
			//echo "select * from members as M,users as U where M.group_id = '$group_id' and U.number = '$cell' and U.id = M.member_uid  and M.pending1 = 0";
			
			if(mysql_num_rows($sql))
			{
				$newSql = "insert into wsoftsms_out (recipient, text, create_date) value";			
				$sql3 = mysql_query("select * from members as M,users as U where M.group_id = '$group_id' and U.id = M.member_uid  and M.pending1 = 0 ") or die(mysql_error());
				while($row3 = mysql_fetch_array($sql3))
					{
						if($cell != $row3['number'])
						{
							 $recp ++;
							 $Phno = "1".$row3['number'];
							 $newSql .= " ('$Phno', '$p2', '".date("Y-m-d H:i:s")."') , ";
						
						
						}
					}

					$newSql =  trim($newSql," , ");
					$newSql .= ";";
					
					if($recp)
						{
								smsstore_saveSmsInSqlDb($newSql);		
						}
					
			}
			else
			{
				echo "You are not member of this group";
			}
	}

}