| Server IP : 162.214.74.102 / Your IP : 216.73.216.139 Web Server : Apache System : Linux dedi-4363141.lrsys.com.br 3.10.0-1160.119.1.el7.tuxcare.els25.x86_64 #1 SMP Wed Oct 1 17:37:27 UTC 2025 x86_64 User : lrsys ( 1015) PHP Version : 5.6.40 Disable Function : exec,passthru,shell_exec,system MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/lrsys/www/lrsys_projetos/sopizzas/application/controllers/ |
Upload File : |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
require_once APPPATH."third_party/Services/Twilio.php";
class Crondrivernotification extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper("url");
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('driver_model');
$this->load->model('dashboard_model');
$this->load->model('frontend_model');
$this->load->model('restaurantmanagement_model');
$this->load->library('myemail');
}
function index( $offset = 0 ) {
$nowTime = date('H:i');
$this->db->select('notify_order_id, notify_time, notify_count');
$this->db->from('rt_driver_notify');
$queryNotify = $this->db->get();
if ($queryNotify->num_rows() > 0) {
$this->db->select('twilio_sid, twilio_token, twilio_from_no');
$this->db->from('rt_sitesetting');
$this->db->limit('1');
$queryTwilio = $this->db->get();
$client = new Services_Twilio($queryTwilio->row()->twilio_sid, $queryTwilio->row()->twilio_token);
$current_time = time();
$current_time = date('H:i', $current_time);
$myArray = $queryNotify->result();
foreach ($myArray as $key => $value) {
$time1 = date_create($current_time);
$time2 = date_create($value->notify_time);
$diff = date_diff($time1,$time2);
$diff = $diff->format("%i");
if ($diff >= 2) {
$this->db->select('driverid');
$this->db->from('rt_order');
$this->db->where('orderid', $value->notify_order_id);
$queryOrderStatus = $this->db->get();
if ($queryOrderStatus->row()->driverid == 0 || $queryOrderStatus->row()->driverid == '') {
$this->db->select('restaurant_id');
$this->db->from('rt_order');
$this->db->where('orderid', $value->notify_order_id);
$query = $this->db->get();
$this->db->select('restaurant_phone');
$this->db->from('rt_restaurant');
$this->db->where('restaurant_id', $query->row()->restaurant_id);
$query = $this->db->get();
$payload = $value->notify_order_id;
try {
$restaurant_phone=str_replace(array( '(', ')' , ' ' , '-' , '+'), '', $query->row()->restaurant_phone);
$call = $client->account->calls->create(
$queryTwilio->row()->twilio_from_no,
'+'.$restaurant_phone,
base_url().'twilioDriver.php?patload='.$payload
);
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage().'<br>';
}
$assignDriver = $this->assign_driver($value->notify_order_id);
if ($value->notify_count >= 4) {
$this->db->where('notify_order_id', $value->notify_order_id);
$this->db->delete('rt_driver_notify');
}
else {
//$updateNotify = array('notify_time' => $nowTime, 'notify_count' => 'notify_count + 1');
//$this->db->where('notify_order_id', $value->notify_order_id);
//$this->db->update('rt_driver_notify', $updateNotify);
$this->db->query('UPDATE rt_driver_notify SET notify_time = "'.$nowTime.'", notify_count = notify_count + 1 WHERE notify_order_id = '.$value->notify_order_id);
}
}
else {
$this->db->where('notify_order_id', $value->notify_order_id);
$this->db->delete('rt_driver_notify');
}
}
}
}
// Sending daily notification to driver
if ($nowTime == date('H:i', strtotime('00:01'))) {
$schedule_date = date('Y-m-d');
$current_time = date("h:i:s");
$this->db->select('rt_driver.driver_id, ios_device_token, android_device_token');
$this->db->from('rt_driver_available_schedule');
$this->db->join('rt_driver','rt_driver.driver_id = rt_driver_available_schedule.driver_id');
$this->db->where('schedule_date', $schedule_date);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$DriverList = $query->result();
foreach ($DriverList as $key => $value) {
$result = false;
$message = 'Hi, you have scheduled today to work with '.SITENAME;
if(!$result) {
$deviceToken = $value->ios_device_token;
$deviceTokenAndroid = $value->android_device_token;
$registrationIdsArray = array($deviceTokenAndroid);
if($deviceToken!='') {
$this->androidNotification($message, $deviceToken);
}
if($deviceTokenAndroid != "") {
$apiKey = ANDROID_PUSH_API;
$title = 'Schedule Reminder';
$this->iosNotification($message, $title, $registrationIdsArray, $apiKey);
}
}
}
}
}
// Sending working hours starting notification
$schedule_date = date('Y-m-d');
$driver_availability_status = '1';
$current_time = time();
$current_time_plus_30 = $current_time + (30 * 60);
$current_time_plus_30 = date('H:i', $current_time_plus_30).':00';
$this->db->select('rt_driver.driver_id, ios_device_token, android_device_token');
$this->db->from('rt_driver_available_schedule');
$this->db->join('rt_driver','rt_driver.driver_id = rt_driver_available_schedule.driver_id');
$this->db->where('rt_driver.driver_availability_status', $driver_availability_status);
$this->db->where('schedule_date', $schedule_date);
$this->db->where('schedule_time_from = ' . "'" . $current_time_plus_30 . "'");
$query = $this->db->get();
if ($query->num_rows() > 0) {
$DriverList = $query->result();
foreach ($DriverList as $key => $value) {
$result = false;
$message = 'Working hours starting in 30 minutes!';
if(!$result) {
$deviceToken = $value->ios_device_token;
$deviceTokenAndroid = $value->android_device_token;
$registrationIdsArray = array($deviceTokenAndroid);
if($deviceToken!='') {
$this->androidNotification($message, $deviceToken);
}
if($deviceTokenAndroid != "") {
$apiKey = ANDROID_PUSH_API;
$title = 'Working Hours Reminder';
$this->iosNotification($message, $title, $registrationIdsArray, $apiKey);
}
}
}
}
// Sending working hours ending notification
$this->db->select('rt_driver.driver_id, ios_device_token, android_device_token');
$this->db->from('rt_driver_available_schedule');
$this->db->join('rt_driver','rt_driver.driver_id = rt_driver_available_schedule.driver_id');
$this->db->where('rt_driver.driver_availability_status', $driver_availability_status);
$this->db->where('schedule_date', $schedule_date);
$this->db->where('schedule_time_to = ' . "'" . $current_time_plus_30 . "'");
$query = $this->db->get();
if ($query->num_rows() > 0) {
$DriverList = $query->result();
foreach ($DriverList as $key => $value) {
$result = false;
$message = 'Working hours ending in 30 minutes!';
if(!$result) {
$deviceToken = $value->ios_device_token;
$deviceTokenAndroid = $value->android_device_token;
$registrationIdsArray = array($deviceTokenAndroid);
if($deviceToken!='') {
$this->androidNotification($message, $deviceToken);
}
if($deviceTokenAndroid != "") {
$apiKey = ANDROID_PUSH_API;
$title = 'Working Hours Reminder';
$this->iosNotification($message, $title, $registrationIdsArray, $apiKey);
}
}
}
}
}
function assign_driver($orderid) {
$now = date('Y-m-d H:i:s');
$nowTime = date('H:i');
$session_data = $this->session->userdata('logged_in_restaurant');
$restaurant_id = $session_data['id'];
//$orderid = $this->input->post('orderid');
//$this->db->where('notify_order_id', $orderid);
//$this->db->delete('rt_driver_notify');
//$assigned_driver_in = array('notify_order_id' => $orderid, 'notify_time' => $nowTime);
//$this->db->insert('rt_driver_notify', $assigned_driver_in);
$why = $this->input->post('why');
if ($why == 're') {
$myArray = array(
'delivery_status' => 0,
'driverid' => 0,
'drivername' => 0,
'status' => 'In Preparation',
);
$this->db->trans_start();
$this->db->where('orderid',$orderid);
$this->db->update('rt_order',$myArray);
$this->db->trans_complete();
}
$order_arr=array('isdriver_assign' => 1, 'driver_assign_time' => $now);
$this->db->trans_start();
$this->db->where('orderid',$orderid);
$this->db->update('rt_order',$order_arr);
$this->db->trans_complete();
$Driver_List = $this->dashboard_model->get_driver_list();
if(is_array($Driver_List) && sizeof($Driver_List)>0)
{
foreach($Driver_List as $row) {
$result = $this->dashboard_model->check_driver($row['driver_id']);
if(!$result)
{
$deviceToken = $row['ios_device_token'];//'b5ad9dc757a7489d0b1a1fc84f43701a8effb42c5914ad984d356f9aa568410c';
$deviceTokenAndroid = $row['android_device_token'];
$registrationIdsArray = array($deviceTokenAndroid); //do7bKiXoN2w:APA91bHud2WQJSB2AeotpNh78lSaLqQScn8oNvu7vohgiOretzselRLvv6XIpMbH7wE66651dh-CMEik5W946lJpmQQ4kNrwRMyyoW-3fYln-NkU_8uHZMxtaX_da--YISTSgKXzkSTD
if($deviceToken!='')
{
$passphrase = '';
$message = 'New order is placed. Please check';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'dine2you.pem');
//stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
$body['aps'] = array('alert' => $message, 'badge' => 1, 'sound' => 'oldcash.wav', 'order_id'=>$orderid);
$payload = json_encode($body);
if (!$fp){
exit("Failed to connect: $err $errstr" . PHP_EOL);
}
//$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
fclose($fp);
//echo $row['driver_id'];
// exit();
}
if($deviceTokenAndroid != "")
{
$apiKey = ANDROID_PUSH_API;//AIzaSyAfAtTYhdwxYR9csZtyI3wOlAMl-JjcViw
$message = 'New order is placed. Please check';
$messageData = array('message'=> $message, 'title'=> "New delivery task", 'appredirect'=> "",'order_id'=>$orderid);
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$data = array(
'data' => $messageData,
'registration_ids' => $registrationIdsArray//do7bKiXoN2w:APA91bHud2WQJSB2AeotpNh78lSaLqQScn8oNvu7vohgiOretzselRLvv6XIpMbH7wE66651dh-CMEik5W946lJpmQQ4kNrwRMyyoW-3fYln-NkU_8uHZMxtaX_da--YISTSgKXzkSTD
);
$ch = curl_init();
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_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
$response = curl_exec($ch);
curl_close($ch);
}
}
}
}
return true;
}
function androidNotification($message, $deviceToken) {
$passphrase = '';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'dine2you.pem');
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
$body['aps'] = array('alert' => $message, 'badge' => 1, 'sound' => 'default', 'order_id'=>'');
$payload = json_encode($body);
if (!$fp) {
exit("Failed to connect: $err $errstr" . PHP_EOL);
}
$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
fclose($fp);
}
function iosNotification($message, $title, $registrationIdsArray, $apiKey) {
$messageData = array('message'=> $message, 'title'=> $title, 'appredirect'=> "",'order_id'=>'');
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$data = array(
'data' => $messageData,
'registration_ids' => $registrationIdsArray
);
$ch = curl_init();
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_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
$response = curl_exec($ch);
curl_close($ch);
}
function send_my_mail($restaurant_id,$fr_date,$to_date,$invoice_id)
{
$mail_CC = '';
$logo=base_url().'assets/logo-main.png';
$image_path = base_url().'assets/';
$variables = array();
$restaurant_data = $this->restaurantmanagement_model->GetRestauratDetails($restaurant_id);
$mail_To = $restaurant_data['restaurant_contact_email'];
$contact_name = $restaurant_data['restaurant_contact_name'];
$link_button=anchor('restaurantadmin/invoice_details/' . md5($invoice_id),'<button>View Earning Details</button>');
$link = anchor('restaurantadmin/invoice_details/' . md5($invoice_id));
$only_link = base_url().'restaurantadmin/invoice_details/' . md5($invoice_id);
$variables['contact_name'] = $contact_name;
$variables['fr_to_date'] = $fr_date.' to '.$to_date;
$variables['link_button'] = $link_button;
$variables['link'] = $link;
$variables['only_link'] = $only_link;
$variables['logo'] = $logo;
$variables['image_path'] = $image_path;
//$variables['sender_email'] = SITE_CONTACT_EMAIL;
$variables['site_address'] = SITE_ADDRESS;
$variables['sender_name'] = SITENAME;
$variables['site_name'] = SITENAME;
$template_name = 'restaurant_invoice';
$mail_data = $this->frontend_model->getEmailTemplate($template_name);
$mailcontent=htmlspecialchars_decode($mail_data->template);
$subject = $mail_data->subject;
foreach($variables as $key => $value) {
$mailcontent = str_replace('{{'.$key.'}}', $value, $mailcontent);
}
if ($mail_CC != '') {
//$mail_CC .= ', dgershen@orderingsystemonline.com';
}
else {
//$mail_CC .= 'dgershen@orderingsystemonline.com';
}
$mail_From = SITENAME;
$mail_subject='Invoice';
$mail_Body = $mailcontent;
$this->send_my_mailX($mail_To,$mail_subject,$mail_Body,$mail_CC="");
}
function send_my_mailX($mail_To,$mail_subject,$mail_Body,$mail_CC="")
{
$mail_From = SITENAME;
$mail = new PHPMailer();
//Your SMTP servers details
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = SMTP_HOST; // specify main and backup server or localhost
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = SMTP_USER; // SMTP username
$mail->Password = SMTP_PASSWORD; // SMTP password It should be same as that of the SMTP user
//$mail->PluginDir = "/home/phpaucti/public_html/website_development/coding/includes/";
$mail->From = $mail->Username; //Default From email same as smtp user
$mail->FromName = $mail_From;
$emailArr = explode(',',$mail_To);
foreach($emailArr AS $emailVal) {
$mail->AddAddress($emailVal, ""); //Email address where you wish to receive/collect those emails.
}
/*$emailCCArr = explode(',',$mail_CC);
foreach($emailCCArr AS $emailCCVal) {
$mail->AddCC($emailCCVal, ""); //Email address where you wish to receive/collect those emails.
}*/
$mail->WordWrap = 100; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = $mail_subject;
$message = $mail_Body;
$mail->Body = $message;
if(!$mail->Send())
{
/*echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;*/
}
else
{
//echo 'ok';
}
}
}