File: /home/cafsindia/ntc_cafsinfotech_in/cron/emailer.php
<?php
require_once('./dbconnect.php');
class emailer extends dbconnect{
public $hostname = '';
public $username = '';
public $password = '';
public $email = '';
private $dbObj = null;
function __construct(){
ini_set('max_execution_time', 0);
$this->open_db();
$this->hostname = '{imap.gmail.com:993/imap/ssl}';
$this->username = 'ntclogisticsgroup@gmail.com';
$this->password = 'Nirghin1*';
$this->email = 'vigilcloud@netstar.co.za';
//$this->dbConnection();
}
function dbConnection() {
$this->dbObj = ntcObject::newObject('dbconnect');
$this->dbObj = $this->dbObj->open_db();
}
//READ EMAIL
public function read_email(){
$inbox = imap_open($this->hostname, $this->username,$this->password) or die('Cannot connect to Gmail: ' . imap_last_error());
$date = date('d-M-Y h:m:s');
$date = date ( "d M Y", strToTime ( "-6 days" ) );
$emails = imap_search($inbox,'FROM "' . $this->email . '" ON "' . $date . '"');
//$emails = imap_search($inbox,'UNSEEN FROM "' . $email . '" ON "' . $data . '"');
if($emails){
$i = 0;
$count = 0;
$insert_data = '';
$send_data = array();
foreach($emails as $email_number){
$email_overview = imap_fetch_overview($inbox, $email_number, 0);
$email_message = imap_fetchbody($inbox,$email_number,1);
$email_structure = imap_fetchstructure($inbox, $email_number);
$encoding = $email_structure->encoding;
$subtype = $email_structure->subtype;
$name = $email_structure->parameters[0]->value;
if($encoding === 0){
$email_message = quoted_printable_decode($email_message);
}else
if($encoding === 1){
$email_message = imap_8bit($email_message);
}else
if($encoding === 2){
$email_message = imap_binary($email_message);
}else
if($encoding === 3){
$email_message = imap_base64($email_message);
}else
if($encoding === 4){
$email_message = quoted_printable_decode($email_message);
}else
if($encoding === 5){
$email_message = $email_message;
}
$htmlDom = new DOMDocument;
@$htmlDom->loadHTML($email_message);
$data = trim($htmlDom->textContent);
$exit_location = '';
$entry_location = '';
if(strpos($data, 'Exit Location:') !== false){
$exit_location = preg_replace('~[\\\\/:*?"<>|]~', '',trim($this->string_between_two_string($data, 'Exit Location:', ' - Vehicle')));
}
if(strpos($data, 'Enter Location:') !== false){
$entry_location = preg_replace('~[\\\\/:*?"<>|]~', '',trim($this->string_between_two_string($data, 'Enter Location:', ' - Vehicle')));
}
$vehicle_no = trim($this->string_between_two_string($data, 'Vehicle \'', '\''));
//$position_date = date('Y-m-d H:i:s',strtotime(str_replace('/','-',$this->string_between_two_string($data, 'Time =', 'Speed'))));
$position_date = str_replace('/','-',$this->string_between_two_string($data, 'Time =', 'Speed'));
$speed = trim($this->string_between_two_string($data, 'Speed = ', 'Direction '));
$direction = trim($this->string_between_two_string($data, 'Direction = ', 'GPS'));
$geoaddress = trim($this->string_between_two_string($data, 'GPS Location', 'http://maps.google.com/'));
$geoaddress = explode(",",$geoaddress);
$latitude = $geoaddress[0];
$longitude = $geoaddress[1];
$trans_date = date('Y-m-d H:i:s');
$insert_data .= "('$vehicle_no','$position_date','$entry_location','$exit_location','$direction','$latitude','$longitude','$speed','$trans_date'),";
$send_data[$vehicle_no] = array('vehicle_no' => $vehicle_no,
'position_date' => $position_date,
'entry_location' => $entry_location,
'exit_location' => $exit_location
);
}
}
imap_close($inbox);
$list_info = $this->runQuery("CREATE TEMPORARY TABLE `cw_vehicle_trip_information`(`prime_vehicle_info_id` int(11) NOT NULL AUTO_INCREMENT,`vehicle_no` VARCHAR(100)DEFAULT NULL,`position_date` datetime DEFAULT NULL,`entry_location` varchar(100) DEFAULT NULL,`exit_location` varchar(100) DEFAULT NULL,`direction` varchar(100) DEFAULT NULL,`latitude` varchar(100) DEFAULT NULL,`longitude` varchar(100) DEFAULT NULL,`speed` varchar(100) DEFAULT NULL,`trans_created_date` datetime DEFAULT NULL,`trans_updated_date` datetime DEFAULT NULL,`trans_status` int(11) NOT NULL DEFAULT 1,PRIMARY KEY (`prime_vehicle_info_id`))");
if($list_info){
$insert_data = rtrim($insert_data,",");
if($insert_data){
$insert_info = $this->runQuery("INSERT INTO cw_vehicle_trip_info(vehicle_no,position_date,entry_location,exit_location,direction,latitude,longitude,speed,trans_created_date) values $insert_data");
if($insert_info){
//if(update_trip_map($send_data)){
return json_encode(array('status' => true,'data' => 'Successfully trip Information Added'));
//}else{
//}
}else{
return json_encode(array('status' => false,'data' => 'Update Trip Information is Failed'));
}
}else{
return json_encode(array('status' => false,'message' => 'No Data Found..!'))
}
}else{
return json_encode(array('status' => false,'message' => 'Something Went Wrong, Please Contact Admin.!'));
}
}
public function update_trip_map($send_data){
$count = count($send_data);
if((int)$count > 0){
foreach($send_data as $rlst){
$vehicle_no = $rlst->vehicle_no;
$position_date = $rlst->position_date;
$entry_location = trim($rlst->entry_location);
$exit_location = trim($rlst->exit_location);
if($entry_location){
$this->runQuery("select * from cw_ntc_load join cw_vehicle_master on cw_vehicle_master.prime_vehicle_master_id = cw_ntc_load.horse where cw_vehicle_master.register_no = $vehicle_no and cw_ntc_load.trip_status IN ('1','2','3') and cw_ntc_load.trans_status = 1");
}else
if($exit_location){
}
//loading_tat arrival_time departure_time offload_departure_time offload_arrival_time dc_tatsssss
}
$this->runQuery("INSERT INTO cw_vehicle_trip_info(vehicle_no,position_date,entry_location,exit_location,direction,latitude,longitude,speed,trans_created_date) values $insert_data");
}
}
//GET IN BETWEEN STRING
public function string_between_two_string($str, $starting_word, $ending_word){
$subtring_start = strpos($str, $starting_word);
$subtring_start += strlen($starting_word);
$size = strpos($str, $ending_word, $subtring_start) - $subtring_start;
return substr($str, $subtring_start, $size);
}
/*
public function get_scheme_list(){
$scheme_dump = $this->pulse->get_scheme_dump();
$push_data = array();
if($scheme_dump){
foreach($scheme_dump as $response_info){
if($response_info){
if(property_exists($response_info, 'status')){
$status = (int)$response_info->status->code;
if($status === 200){
$scheme_data = json_decode(json_encode($response_info->data), true);
array_push($push_data,$scheme_data);
}
}
}
}
$final_data = array_reduce($push_data, 'array_merge', array());
if(count($final_data) > 0){
$insert_data = array_map(function ($ar) {
$scheme_code = $ar['scheme_code'];
$scheme_name = str_replace("'"," ",$ar['scheme_name']);
$isin = $ar['isin'];
$scheme_code_bse = $ar['scheme_code_bse'];
$scheme_description_bse = str_replace("'"," ",$ar['scheme_description_bse']);
$trans_date = date("Y-m-d h:i:s");
$insert_value = "('$scheme_code','$scheme_name','$isin','$scheme_code_bse','$scheme_description_bse','$trans_date')";
return $insert_value;
}, $final_data);
$list_info = $this->runQuery("CREATE TEMPORARY TABLE cw_cmf_scheme_temp (scheme_code varchar(100),scheme_name text,isin varchar(100),scheme_code_bse varchar(100),scheme_description_bse text,trans_created_date datetime)");
if($list_info){
$count = count($insert_data);
$value_list = implode(',', $insert_data);
$insert_info = $this->runQuery("INSERT INTO cw_cmf_scheme_temp(scheme_code,scheme_name,isin,scheme_code_bse,scheme_description_bse,trans_created_date) values $value_list");
if($insert_info){
echo "All Inserted";
$add_funds = $this->runQuery("INSERT INTO cw_cmf_scheme(scheme_code,scheme_name,isin,scheme_code_bse,scheme_description_bse,trans_created_date) SELECT scheme_code,scheme_name,isin,scheme_code_bse,scheme_description_bse,trans_created_date FROM cw_cmf_scheme_temp WHERE scheme_code NOT IN (SELECT scheme_code FROM cw_cmf_scheme)");
/*
if($add_funds){
$remove_funds = $this->runQuery("SELECT scheme_code,scheme_name,isin,scheme_code_bse,scheme_description_bse,trans_created_date FROM cw_cmf_scheme WHERE scheme_code NOT IN (SELECT scheme_code FROM cw_cmf_scheme_temp)");
$remove_count = $this->num_rows($remove_funds);
if((int)$remove_count > 0){
$remove_info = $this->result($remove_funds);
$scheme_codes = implode(",",array_column($remove_info, 'scheme_code'));
$trans_updated = date("Y-m-d h:i:s");
$this->runQuery("UPDATE cw_cmf_scheme SET trans_updated_date = '$trans_updated',no_follow='1' where scheme_code in ($scheme_codes)");
}
return true;
}
}
}
}
}
return false;
}*/
}
?>