HEX
Server: Apache/2.4.41 (Amazon) OpenSSL/1.0.2k-fips PHP/5.6.40
System: Linux ip-172-31-40-18 4.14.146-93.123.amzn1.x86_64 #1 SMP Tue Sep 24 00:45:23 UTC 2019 x86_64
User: apache (48)
PHP: 5.6.40
Disabled: NONE
Upload Files
File: //var/www/html/pmw24/pmw_live_testing/app/application/controllers/admin/Jobhistory.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Jobhistory extends MY_Controller {
	public function __construct() {
		parent::__construct();
		$this->redirect_guest();
		$this->admin=$this->session->userdata('admin');
		$this->load->model('admin/mjobhistory');
		$this->load->model('admin/mjob');
	}
		
	public function index() {		
		$this->_load_list_view();		
	}
	
	private function _load_list_view() {
		$data['content'] = 'admin/jobhistory/list';		
		$this->load->view('admin/layouts/index', $data);
	}
	
	public function all_content_list(){
		$list = $this->mjobhistory->get_datatables();
        $data = array();
        $no = $_POST['start'];
		$i=1;
        foreach ($list as $person) {			
            $no++;
            $row = array();
			$row[] = '<input type="checkbox" class="cstm_view" title="'.$person->job_id.'">';				
			//$row[]=$i;	
			$row[] = $person->category_name;
			$row[] = $person->job_date;					
			$row[] = $person->client_name;
			$row[] = $person->job_address;
			$row[] = $person->name;
			$row[] = '<a class="cstm_view" id="notify" style="padding-left:5px" href="javascript:void(0)" title="'.$person->job_id.'">Send Notification</a>';
			//$row[] = '<a class="cstm_view" href="'.base_url('admin/jobhistory/details/'.$person->job_id).'" title="Edit">Edit</a>|<a class="cstm_view" id="view" style="padding-left:5px" href="javascript:void(0)" title="'.$person->job_id.'">View</a>';
            $data[] = $row;
			$i++;
        }
        $output = array(
                        "draw" => $_POST['draw'],
                        "recordsTotal" => $this->mjobhistory->count_all(),
                        "recordsFiltered" => $this->mjobhistory->count_filtered(),
                        "data" => $data,
                );
        echo json_encode($output);
	}
	
	public function edit($job_id){		
		$data['cms']=$this->mjob->get_details($job_id);	
		if(empty($data['cms'])){
			$this->_load_list_view();
		}else{			
			$this->_load_details_view($data);
		}
	}
	
	private function _load_details_view($parms){
		$data['city_list'] = $this->mjob->get_city_list();
		$data['parent_categories'] = $this->mjob->get_parent_category();		
		$data['sub_category'] = $this->mjob->get_sub_category_by_category_id($parms['cms']['cat_id']);		
		$data['parent_category_sub'] = $this->mjob->get_parent_category_by_parent_id($data['sub_category']['parent_id']);
		$data['sub_categories'] = $this->mjob->get_sub_categories_by_parent_id($parms['cms']['cat_id']);
		$data['client_list'] = $this->mjob->get_client_list();
		$data['address_list'] = $this->mjob->get_client_address($parms['cms']['client_name']);
		$data['phone_list'] = $this->mjob->get_client_phone($parms['cms']['client_name']);		
		$data['cms']=$parms['cms'];	
		$data['content'] = 'admin/jobhistory/detail';		
		$this->load->view('admin/layouts/index', $data);
	}	
	
	public function add_content()
	{	
		if($this->input->post()){	
			$this->form_validation->set_rules('job_date','Job Date','required');
			$this->form_validation->set_rules('job_start_time','Job Start Time','required');
			$this->form_validation->set_rules('category_id','Category','required');
			$this->form_validation->set_rules('sub_category_id','Sub Category','required');
			$this->form_validation->set_rules('client_name','Client Name','required');
			$this->form_validation->set_rules('client_address','Client Address','required');
			$this->form_validation->set_rules('job_end_time','Job End Time','required');
			$this->form_validation->set_rules('client_name','Client Name','required');
			$this->form_validation->set_rules('client_address','Client Address','required');
			$this->form_validation->set_rules('city_id','Location','required');
			
			if($this->form_validation->run()==FALSE){			
				$this->_load_list_view();
			}else{
					$job_category_condition = array('category_id'=>$this->input->post('category_id'));				
					$job_category = $this->mjob->getRow('categories',$job_category_condition);
					
					$job_sub_category_condition = array('category_id'=>$this->input->post('sub_category_id'));				
					$job_sub_category = $this->mjob->getRow('categories',$job_sub_category_condition);
					
					$udata['job_title']= $job_category['category_name'];				
					$udata['job_description']= $job_category['category_name'];				
					$job_date = $this->input->post('job_date');
					$date_array = explode('/',$job_date);
					$newDate = $date_array[2]."-".$date_array[1]."-".$date_array[0];
					$udata['job_date']=$newDate;
					$udata['job_start_time']=$this->input->post('job_start_time');
					$udata['job_end_time']=$this->input->post('job_end_time');				
					$udata['sub_cat_id'] = $this->input->post('sub_category_id');
					$udata['cat_id'] = $this->input->post('category_id');
					$udata['client_name'] = $this->input->post('client_name');
					$udata['job_address'] = $this->input->post('client_address');
					
					
					
					$client_phone_codition = array('client_name'=>$this->input->post('client_name'), 'client_address'=>$this->input->post('client_address'));
					
					$client_phone = $this->mjob->getRow('client',$client_phone_codition);
					$udata['client_phoneno'] = $client_phone['client_phone_no'];					
								
					$udata['type_of_work'] = $job_category['category_name'];					
					$udata['city_id'] = $this->input->post('city_id');
					
					$udata['job_status']=1;								
					$udata['date_of_creation']=date('Y-m-d H:i:s');	
					
					
						
					$this->mjob->add($udata);				
					
					$push['notification_type'] = "job";
					$push['job_category'] = $job_category['category_name'];
					$push['job_sub_category'] = $job_sub_category['category_name'];
					$push['job_date'] = $this->input->post('job_date');
					$push['job_start_time'] = $this->input->post('job_start_time');
					$push['job_end_time'] = $this->input->post('job_end_time');
					
					$deviceList=$this->mjob->deviceList($this->input->post('category_id'),$this->input->post('city_id'));
					if(!empty($deviceList)){
						foreach($deviceList as $device){
							if($device['device_type']==2 && $device['device_token'] != '' && $device['loggedin_status'] == 1){
								$this->send_android_notification($device['device_token'],$push);
							}else if($device['device_type']==1 && $device['device_token'] != '' && $device['loggedin_status'] == 1){
								$this->send_ios_notification($device['device_token'],$push);
							}
						}					
					}
				
				
				$this->session->set_flashdata('success_msg','Job added successfully');
				redirect('admin/job');
			}
		}else{
			$this->_load_list_view();
		}
	}
	
	public function send_ios_notification($devicetoken,$data){
		$passphrase='123456';
        $deviceToken = $devicetoken;    
        $ctx = stream_context_create();
		// ck.pem is your certificate file ssl://gateway.sandbox.push.apple.com:2195
		stream_context_set_option($ctx, 'ssl', 'local_cert', './public/WeCareStaffPem.pem');
		stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
		// Open a connection to the APNS server
		$fp = stream_socket_client(
			'ssl://gateway.sandbox.push.apple.com:2195', $err,
			$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

		if (!$fp){ 
			//exit("Failed to connect: $err $errstr" . PHP_EOL);
			return 2;
		}else{
			// Create the payload body
			$body['aps'] = array(
					'alert' => array(
					    'title' => 'Wecare App',
		                'body' => 'Wecare App',
					 ),
					'sound' => 'default',
					'data'=>$data
			);

			// Encode the payload as JSON
			$payload = json_encode($body);
			// Build the binary notification
			$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack('n', strlen($payload)) . $payload;  //
			//$msg = chr(0) . pack('n', 32) . pack('H*',str_replace(' ', '', sprintf('%u', CRC32($deviceToken)))) . pack('n', strlen($payload)) . $payload;  // 
			//str_replace(' ', '', sprintf('%u', CRC32($deviceToken)))
			//$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', sprintf('%u', CRC32($deviceToken)))) . pack('n', strlen($payload)) . $payload
			// Send it to the server
			$result = fwrite($fp, $msg, strlen($msg));
			// Close the connection to the server
			fclose($fp);
			if (!$result){
				//return 'Message not delivered' . PHP_EOL;
				return 0; 
			}else{
				//return  'Message successfully delivered' . PHP_EOL;
				return 1;
			}
		} 
	}
	public function send_android_notification($registration_ids, $data) {
		$fields = array(
		'registration_ids' => array($registration_ids),
		'data'=> $data,
		);
		$headers = array(
		'Authorization: key=AAAAAjykQAw:APA91bF2xwn1yqpfBkoxg_K_fTp5bOaI4M3zXL_yG63hmQpqvVFZlMiilFdf65DWdqAO5Xy3eBxJjFybwKMwDMnr16ls_YnMDpMAfwTbJUcBnpHK4supv42pZIoDEBHrdUwOFI9wmQYs', // FIREBASE_API_KEY_FOR_ANDROID_NOTIFICATION
		'Content-Type: application/json'
		);
		// Open connection
		$ch = curl_init();
		 
		// Set the url, number of POST vars, POST data
		curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
		curl_setopt( $ch,CURLOPT_POST, true );
		curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
		curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
		 
		// Disabling SSL Certificate support temporarly
		curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
		 
		// Execute post
		$result = curl_exec($ch );
		if($result === false){
		//die('Curl failed:' .curl_errno($ch));
			return 0;
		}
		 
		// Close connection
		curl_close( $ch );
		return $result;
	}
}