<?php 
/*
##################################################################
#                       PHP 4&5 MySQL 4&5                        #
##################################################################
#           Copyright (c) 2004-2008 Armia Systems Inc            #
##################################################################
#               This file is part of Cybermatch site             #
##################################################################
#    Developed By: simi (simi@armia.com)                         #

##################################################################
#               Developed On:   03-04-2008                       #
##################################################################
#               Modified By:                                     #
##################################################################
#               Modified On:                                     #
##################################################################
# This script may not be distributed, sold, given away for free  #
# to third party, or used as a part of any internet services 	 #
# such as webdesign etc.                                         #
##################################################################
*/

require_once ("config.inc.php");
require_once ("includes/userlogincheck.inc.php");
require_once ("includes/mysql.inc.php");
require_once ("classes/userside.class.php");
require_once ("classes/misc.class.php");
require_once ("classes/settings.class.php");
require_once ("iphonepush.php");
$message	= "";
$msg		= "";
$vUrl		= "";
$id			= isset($_REQUEST['id'])	?	stripslashes(trim($_REQUEST['id']))		:	"0";
$msgid		= isset($_REQUEST['msgid'])	?	stripslashes(trim($_REQUEST['msgid']))	:	"0";
$title		= isset($_POST['vTitle'])	?	stripslashes(trim($_POST['vTitle']))		:	"";
$mail		= isset($_POST['tContent'])		?	stripslashes(trim($_POST['tContent']))		:	"";
$page		= isset($_REQUEST['page'])	?	trim($_REQUEST['page'])	:	"1";
$type		= isset($_REQUEST['type'])	?	trim($_REQUEST['type'])	:	"__";

$script		= "";
$arrMsg		= array();
$array		= array();

if (!$objDB) {
	$message	= "Unable to Locate Database Server.";
} else {

	$objU		= new userside($objDB);
	$objU1		= new userside($objDB);
	$objM		= new misc($objDB);
	$objM->checkPremiumPermission();
	if (isset($_POST['BTM'])) {
		header("Location: msg_recieved.php?type=$type&page=$page");
		exit();
	}
	
	if (isset($_POST['SendM'])) {
		if ($id == $_SESSION['user_id']) {
			$msg	= "Error: Invalid operation.<br>";
		} else {
			if ($objU->is_expired($_SESSION['user_id'])) {
				$msg	= "Your profile has been expired, please renew your profile for using this service.";	
			} else {
				$msg	= $objM->send_message($_SESSION['user_id'], $_POST);
				if ($msg == "") {
                                          /****************** Iphone PUsh notication ***************/
                                        $objSeT	= new settings($objDB);                                       
                                        $sSettings=$objSeT->select();
                                        if($sSettings['enable_pushbk_notification']=="Y")
                                        {           
                                            $userDevices = $objM->getuserDevices($_POST['id']);
                                            if($userDevices){
                                                foreach($userDevices as $key=>$value)
                                                {                                                    
                                                    $deviceToken = $value->device_id;
                                                    $deviceType = $value->device;
                                                    if($deviceType=="ios" && $deviceToken)
                                                    {
                                                        // Put your private key's passphrase here:
                                                        $passphrase = $sSettings['iPhone_parse_phrase'];
                                                        // Put your alert message here:
                                                        $from = $_SESSION['user_name'];
                                                        $message1 = "You have one message from $from";
                                                        send_push_nofication($message1,$from,$passphrase,$deviceToken);
                                                    }
                                                }
                                            }
                                        }
                                         /******************* Iphone PUsh notication end *****************/
                                       
					$msg		= "<font color='green'>Your message has been successfully sent.</font><br>";
                                        
                                }
                                
			}
		}
	} else {
		if ($msgid > 0) {
			$arrMsg	= $objM->get_message($msgid, $_SESSION['user_id']);
			if (!empty($arrMsg)) {
				$title	= "Re: ".$arrMsg['vTitle'];
				$mail	= "\n\n".$arrMsg['vLogin_From']." wrote:\n"."----------------------\n".$arrMsg['tContent'];
			}
		}
	}

	$array		= $objU->getuser(addslashes($id)); 
	if (!empty($array)) {
		$objU->set_values($array);
	} else {
		$message	= "Invalid profile.<br>";
	}

	$message	.= $msg;
	$script		= $objM->get_script($message);
	$message	= $objM->remove_script($message);
}
include_once ("title.inc.php");
include_once ("tophtml.inc.php");
?>

<div class="row">
	<div class="eight columns">
		<div class="search-rslts">
			<div class="row"><div class="inenr-hd"><h2>Send Mail to <?php echo $objU->vLogin?></h2></div></div>
			<div class="row">
				<form name="frmMsg" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
					<div class="newdesignforinput maintext1">
						<?php if(isset($message) && trim($message)!='')
							  {
						?>
						<div class="full_row">
							<div class="maintext1 warning"><?php echo $message;?></div>
						</div>
						<?php }//end if?>
						
						<div class="full_row">
							<div class="three columns">Mail Title</div>
							<div class="five columns margin_R_200">
								<input name="vTitle" type="text" class="commononetextbox" size="40" maxlength="250" value="<?php echo $title?>">
							</div>
						</div>
						
						<div class="full_row">
							<div class="three columns">Mail Message</div>
							<div class="five columns margin_R_200">
								<textarea name="tContent" cols="70" rows="12" class="commononetextbox"><?php echo $mail?></textarea>
							</div>
						</div>
						
						<div class="full_row">
							<div class="three columns">&nbsp;</div>
							<div class="five columns margin_R_200">
								<input name="SendM" type="submit" class="loginbutton" value="Send Message">&nbsp;&nbsp;<input name="BTM" type="submit" class="loginbutton" id="BTM" value="Back to Mail"><input type="hidden" name="id" value="<?php echo $id?>" />
								<input type="hidden" name="msgid" value="<?php echo $msgid?>" />
								<input type="hidden" name="page" value="<?php echo $page?>">
								<input type="hidden" name="type" value="<?php echo $type?>">
							</div>
						</div>

					</div>
				</form>
			</div>
		</div>
	</div>
	<div class="four columns search_result_R"><?php include_once ("left.inc.php");?></div>
</div>

<?php include_once('bottomhtml.inc.php');?>
