<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_staff
*/
include('includes/imgFunctions.php');
include('includes/functions.php'); # general functions

date_default_timezone_set('Asia/Kolkata');
$debug = true;

if(isset($_REQUEST['p'])){
	$p = trim($_REQUEST['p']);
} else {
	$p = 1;
}


	$curDate = date('Y-m-d H:i:s');	

	if(isset($_POST['send'])) {
		
		if(isset($_POST['message']) && strlen($_POST['message'])>0) {
			$message = trim($_POST['message']);
		} else {
			$error = 'Please enter message';
		}	
		
		if(isset($_POST['subject']) && strlen($_POST['subject'])>0) {
			$subject = trim($_POST['subject']);
		} else {
			$error = 'Please enter subject';
		}
		
		if(isset($_POST['os']) && strlen($_POST['os'])>0) {
			$os = trim($_POST['os']);
		} else {
			$error = 'Please select OS - iOS/Android';
		}
		
		if(isset($_POST['app']) && strlen($_POST['app'])>0) {
			$app = trim($_POST['app']);
		} else {
			$error = 'Please select app';
		}
		
				
		if(empty($error)) {
			$heading = $subject;		
			$sub_title	= $message;
			
			
			if($app=='user'){
				if($os == 'android'){ 
					//send to android
					$onesignal_id = '5e42067f-cd18-4e53-b1e4-f4afa67a23d9';
					$one_signal_rest_api_key = 'ODBhYTVhMzUtYTYwZS00YTU1LTg4MDYtMjQ3N2JkOGZmNTE5';	
					$currentDateTime = date('H:i:s');	
					$newDateTime = date('h:i A', strtotime($currentDateTime)+(60*2));				
					$send_after = date("F jS Y, g:i:s  a");		
					strtotime('+2 minutes', strtotime($send_after));		
					$send_after = $send_after.' UTC +05:30';
					
					mysqli_query($con, "INSERT INTO tbl_mass_push (app_type, subject, message, date) VALUES ('user', '".$heading."', '".$message."', '".$curDate."')");
						
					$content = array("en" => $heading);		
					$sub_title = array("en"=> $sub_title);		
								
					$fields = array('app_id' => $onesignal_id,		
									'included_segments' => array('All'),		
									//'data' => array("cid" => $userRec['id'], "cname"=>$userRec['name']),		
									'headings' => $content,		
									'contents' => $sub_title,		
								    //'big_picture' => BASE_URL.'cat_images/'.$res_details['image'],		
								   	'delayed_option'=> 'timezone',		
								    'delivery_time_of_day' => $newDateTime);			
				
					$fields = json_encode($fields);
					$ch = curl_init();		
					curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");		
					curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
	
													   'Authorization: Basic '.$one_signal_rest_api_key));
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);		
					curl_setopt($ch, CURLOPT_HEADER, FALSE);		
					curl_setopt($ch, CURLOPT_POST, TRUE);		
					curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);		
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					$response = curl_exec($ch);		
					curl_close($ch);					
					//print($response);		
					
				} else if($os == 'ios'){
										
					//send to IOS
					$onesignal_id = '3ca31d54-9286-43d8-99e5-1184b19e0ddd';
					$one_signal_rest_api_key = 'MmY0OWJjZmUtNWE0Yy00MzdmLWI1Y2EtNTAxNjFhZTBjYjQ3';	
					$currentDateTime = date('H:i:s');	
					$newDateTime = date('h:i A', strtotime($currentDateTime)+(60*2));				
					$send_after = date("F jS Y, g:i:s  a");		
					strtotime('+2 minutes', strtotime($send_after));		
					$send_after = $send_after.' UTC +05:30';
					
					mysqli_query($con, "INSERT INTO tbl_mass_push (app_type, subject, message, date) VALUES ('user', '".$heading."', '".$message."', '".$curDate."')");
						
					$content = array("en" => $heading);		
					$sub_title = array("en"=> $sub_title);
					$fields = array('app_id' => $onesignal_id,				
									'included_segments' => array('All'),				
									//'data' => array("cid" => $id, "cname"=>$res_details['name']),
									'headings' => $content,
									'contents' => $sub_title,
								    //'ios_attachments' => BASE_URL.'cat_images/'.$res_details['image'],
								   	'delayed_option'=> 'timezone',
								    'delivery_time_of_day' => $newDateTime
								   /*"send_after" => $send_after*/);				
					$fields = json_encode($fields);				
					//print("\nJSON sent:\n");				
					//print($fields);					
				
					$ch = curl_init();				
					curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");				
					curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
				
																   'Authorization: Basic '.$one_signal_rest_api_key));
				
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);				
					curl_setopt($ch, CURLOPT_HEADER, FALSE);				
					curl_setopt($ch, CURLOPT_POST, TRUE);				
					curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);				
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);				
					$response = curl_exec($ch);				
					curl_close($ch);							
					//print_r($response);
					//return $response;
				}
				
			} else if($app=='tech'){
				if($os == 'android'){
					//send to android
					$onesignal_id = '0785c625-c033-4be7-bc3a-ada90669a9ec';
					$one_signal_rest_api_key = 'MDM2ZjE0ZDItZDA1Ny00NzZhLTk0ZDgtOTcyYWI3YzU3Yzk4';	
					$currentDateTime = date('H:i:s');	
					$newDateTime = date('h:i A', strtotime($currentDateTime)+(60*2));				
					$send_after = date("F jS Y, g:i:s  a");		
					strtotime('+2 minutes', strtotime($send_after));		
					$send_after = $send_after.' UTC +05:30';
					
					mysqli_query($con, "INSERT INTO tbl_mass_push (app_type, subject, message, date) VALUES ('tech', '".$heading."', '".$message."', '".$curDate."')");
						
					$content = array("en" => $heading);		
					$sub_title = array("en"=> $sub_title);		
				
					$fields = array('app_id' => $onesignal_id,		
									'included_segments' => array('All'),		
									//'data' => array("cid" => $userRec['id'], "cname"=>$userRec['name']),		
									'headings' => $content,		
									'contents' => $sub_title,		
								    'show_in' => 'tech',		
								   	'delayed_option'=> 'timezone',		
								    'delivery_time_of_day' => $newDateTime);			
				
					$fields = json_encode($fields);
					$ch = curl_init();		
					curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");		
					curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
	
													   'Authorization: Basic '.$one_signal_rest_api_key));
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);		
					curl_setopt($ch, CURLOPT_HEADER, FALSE);		
					curl_setopt($ch, CURLOPT_POST, TRUE);		
					curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);		
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					$response = curl_exec($ch);		
					curl_close($ch);					
					//print($response);	
					$respArr = json_decode($response);				
					//echo $recipients = $respArr['recipients'];
					
				} else if($os == 'ios'){
				
					//send to IOS
					$onesignal_id = '5a9ee1f9-46c8-4ff4-8547-654b3b087f61';
					$one_signal_rest_api_key = 'NmRmNjllOTEtNWM2NC00MjkyLTg5OTMtMDNhNWM4ZDMwOGI3';	
					$currentDateTime = date('H:i:s');	
					$newDateTime = date('h:i A', strtotime($currentDateTime)+(60*2));				
					$send_after = date("F jS Y, g:i:s  a");		
					strtotime('+2 minutes', strtotime($send_after));		
					$send_after = $send_after.' UTC +05:30';
					
					mysqli_query($con, "INSERT INTO tbl_mass_push (app_type, subject, message, date) VALUES ('user', '".$heading."', '".$message."', '".$curDate."')");
						
					$content = array("en" => $heading);		
					$sub_title = array("en"=> $sub_title);
					$fields = array('app_id' => $onesignal_id,				
									'included_segments' => array('All'),				
									//'data' => array("cid" => $id, "cname"=>$res_details['name']),
									'headings' => $content,
									'contents' => $sub_title,
								    'show_in' => 'tech',
								   	'delayed_option'=> 'timezone',
								    'delivery_time_of_day' => $newDateTime
								   /*"send_after" => $send_after*/);				
					$fields = json_encode($fields);				
					//print("\nJSON sent:\n");				
					//print($fields);					
				
					$ch = curl_init();				
					curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");				
					curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
				
																   'Authorization: Basic '.$one_signal_rest_api_key));
				
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);				
					curl_setopt($ch, CURLOPT_HEADER, FALSE);				
					curl_setopt($ch, CURLOPT_POST, TRUE);				
					curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);				
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);				
					$response = curl_exec($ch);				
					curl_close($ch);	
					
					
					
					//header('Location:index.php?page=mass_push&success='.$success);
					//exit();	
				}	
				
				
			} 
			
			$success = 'Message sent Successfully';
			
			/*else if($app=='shop'){
			
				//send to android
				$onesignal_id = '0785c625-c033-4be7-bc3a-ada90669a9ec';
				$one_signal_rest_api_key = 'MDM2ZjE0ZDItZDA1Ny00NzZhLTk0ZDgtOTcyYWI3YzU3Yzk4';	
				$currentDateTime = date('H:i:s');	
				$newDateTime = date('h:i A', strtotime($currentDateTime)+(60*2));				
				$send_after = date("F jS Y, g:i:s  a");		
				strtotime('+2 minutes', strtotime($send_after));		
				$send_after = $send_after.' UTC +05:30';
				
				mysqli_query($con, "INSERT INTO tbl_mass_push (app_type, subject, message, date) VALUES ('tech', '".$heading."', '".$message."', '".$curDate."')");
					
				$content = array("en" => $heading);		
				$sub_title = array("en"=> $sub_title);		
				
					$fields = array('app_id' => $onesignal_id,		
									'included_segments' => array('All'),		
									//'data' => array("cid" => $userRec['id'], "cname"=>$userRec['name']),		
									'headings' => $content,		
									'contents' => $sub_title,
									'show_in' => 'partner',								    		
								   	'delayed_option'=> 'timezone',		
								    'delivery_time_of_day' => $newDateTime);			
				
					$fields = json_encode($fields);
					$ch = curl_init();		
					curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");		
					curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
	
													   'Authorization: Basic '.$one_signal_rest_api_key));
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);		
					curl_setopt($ch, CURLOPT_HEADER, FALSE);		
					curl_setopt($ch, CURLOPT_POST, TRUE);		
					curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);		
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					$response = curl_exec($ch);		
					curl_close($ch);					
					//print($response);	
					$respArr = json_decode($response);				
				//echo $recipients = $respArr['recipients'];
				
				//send to IOS
				$onesignal_id = '5a9ee1f9-46c8-4ff4-8547-654b3b087f61';
				$one_signal_rest_api_key = 'NmRmNjllOTEtNWM2NC00MjkyLTg5OTMtMDNhNWM4ZDMwOGI3';	
					$fields = array('app_id' => $onesignal_id,				
									'included_segments' => array('All'),				
									//'data' => array("cid" => $id, "cname"=>$res_details['name']),
									'headings' => $content,
									'contents' => $sub_title,
								    'show_in' => 'partner',
								   	'delayed_option'=> 'timezone',
								    'delivery_time_of_day' => $newDateTime
								   );				
					$fields = json_encode($fields);				
					//print("\nJSON sent:\n");				
					//print($fields);					
				
					$ch = curl_init();				
					curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");				
					curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
				
																   'Authorization: Basic '.$one_signal_rest_api_key));
				
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);				
					curl_setopt($ch, CURLOPT_HEADER, FALSE);				
					curl_setopt($ch, CURLOPT_POST, TRUE);				
					curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);				
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);				
					$response = curl_exec($ch);				
					curl_close($ch);	
					
					
				$success = 'Message sent Successfully';
				//header('Location:index.php?page=mass_push&success='.$success);
				//exit();		
				
				
			} */
			
			/*if($execInsert) {
						
				$success = 'Message sent Successfully';
				//header('Location:index.php?page=manage_user&success='.$success);
				exit();			
			}	*/			
		} 
	}





?>