selectCollection("social","did_sms_log"); $logfile = "did_sendsms_logs.txt"; $currentDateTime = date("Y-m-d H:i:s"); $responseData = array(); $responseData['STATUS_CODE'] = 0; $responseData['RESPONSE'] = array(); $responseData['RESPONSE_COUNT'] = 0; $responseData['MSG'] = ""; $log = ""; $log .= "\n DATE : ".date("Y-m-d H:i:s"); if($_GET['test']==1){ $_POST = $_GET; } $params = array(); $params['caller_number']= $_REQUEST['caller_number']; //user number $params['virtual_number']= $_REQUEST['virtual_number']; //dnis number $params['call_duration']= $_REQUEST['call_duration']; //validate if(validate()){ //check valid dnis number and get listing id $currentDate = date("Y-m-d"); $didCheckSQL = "Select e.id as est_id,e.name as estname,edc.did_sms_mobile_numbers,edc.did_email_id,nbr.neighborhood from burrp_sw.establishment_did_contract_knowlarity edc,establishment e, neighborhood nbr WHERE edc.establishment_id = e.id AND e.neighborhood_id = nbr.id AND edc.virtual_number = '".$params['virtual_number']."' AND edc.status=1 AND '".$currentDate."' between edc.start_date AND edc.end_date"; if($_GET['test']==1) { //print_r($params); //echo $didCheckSQL; //exit; } $didCheckRes = mysqli_query($con->_Link, $didCheckSQL); if(mysqli_num_rows($didCheckRes) > 0){ $didData = mysqli_fetch_assoc($didCheckRes); if($didData){ //get owner info $email= $didData["did_email_id"]; $qy= "SELECT bop.first_name, bop.last_name, bop.email, bop.primary_mobile_number FROM ownership_claim oc, business_owner_profile bop WHERE oc.establishment_id='".$didData['est_id']."' AND oc.status=1 AND oc.owner_id=bop.id"; $res= mysqli_query($con->_Link, $qy); $user_row= mysqli_fetch_assoc($res); //call sms API $estname = $didData['estname']; $estNeighborhood = $didData['neighborhood']; $msg= sprintf("You have just received a call from %s at %s for %s, %s", $params['caller_number'], date("d-m-Y H:i:s"), $estname, $estNeighborhood); $smsno = $didData['did_sms_mobile_numbers']; //count($array) != count(array_filter($array)); $tmp= !is_null($smsno) ? explode(",", $smsno) : ($user_row['primary_mobile_number'] ? array($user_row['primary_mobile_number']) : array()); $tmp = array_filter($tmp); $tmp = (count($tmp) > 0) ? $tmp : ($user_row['primary_mobile_number'] ? array($user_row['primary_mobile_number']) : array()); //mongo logs $emdata = array(); $emdata['owner_name']= $user_row['first_name']; $emdata['biz_name']= $estname; $emdata['biz_loc']= $estNeighborhood; $emdata['caller_no']= $params['caller_number']; $emdata['biz_no']= $params['virtual_number']; $emdata['call_duration']= $params['call_duration']; $emdata['call_datetime']=$currentDateTime; $emdata['call_date']= date("Y-m-d"); //mail only params $emdata['call_time']= date("H:i"); //mail only params $emdata['email']= !is_null($email) && $email != '' ? $email : $user_row['email']; if(!is_null($emdata['email']) && $emdata['email'] != ''){ $resp= CURL::sendmail_netcore(send_mail($emdata)); // if($_GET['test']==1){ // var_dump($resp); // } } // for($i= 0; $i < count($tmp); $i++){ foreach($tmp as $i => $val) { if(!is_null($tmp[$i]) && $tmp[$i] != ''){ $mobile= $tmp[$i]; $mongoStatus = insert_to_mongo($didData['est_id'],$emdata,$mobile,$msg); if(!$mongoStatus){ $log .= "\r\n Error : mongo insert failure"; $log .= "\r\n Data :".json_encode($params); $log .= "\r\n ------------------------------------------"; $file = fopen($logfile,"a+"); $fres = fwrite($file,$log); fclose($file); $log = ""; } $smsApi = str_replace("@MOBILE@",$mobile,Constants::SMS_API); $smsApi = str_replace("@MESSAGE@",urlencode($msg),$smsApi); $output = CURL::run($smsApi); if($_GET['test'] == 1) print_r($output); // $output = explode("|",$output); if(preg_match('/^Sent/', $output)){ // $output[0] == '1701'){} $response = "SUCCESS"; $responseData['STATUS_CODE'] = 1; $responseData['RESPONSE'] = array('recorded'=>$params['virtual_number']); $responseData['MSG'] = $response; $responseData['RESPONSE_COUNT'] = 1; $log .= "\r\nSuccess"; $log .= "\r\n Data : ".json_encode($params); $log .= "\r\n ------------------------------------------"; $file = fopen($logfile,"a"); $fres = fwrite($file,$log); fclose($file); }else{ $response = "SMS FAIL"; $responseData['MSG'] = $response; $log .= "\r\n Error : ".$response; $log .= "\r\n Data :".json_encode($params); $log .= "\r\n ------------------------------------------"; $file = fopen($logfile,"a+"); $fres = fwrite($file,$log); fclose($file); $log = ""; } } } }else{ $log .= "\r\n Error : DID contract expired/DID not found."; $log .= "\r\n Data :".json_encode($params); $log .= "\r\n ------------------------------------------"; $responseData['MSG'] = "DID contract expired/DID not found."; $file = fopen($logfile,"a+"); $fres = fwrite($file,$log); fclose($file); $log = ""; } }else{ $log .= "\r\n Error : DID check failed."; $log .= "\r\n Data : ".json_encode($params); $log .= "\r\n ------------------------------------------"; $responseData['MSG'] = "DID check failed."; $file = fopen($logfile,"a"); $fres = fwrite($file,$log); fclose($file); } } else { $log .= "\r\n Error : ".$responseData['MSG']; $log .= "\r\n Data : ".json_encode($params); $log .= "\r\n ------------------------------------------"; $file = fopen($logfile,"a"); $fres = fwrite($file,$log); fclose($file); } //http://api.burrp.com/burrp/api/v1/did_call_log.php?&called_number=123&caller_number=33130603&call_duration=7&forwarded_number=890&call_status=1&call_start_date_time=2012-09-10%2017:06:37&call_start_date_time=2012-09-10%2017:06:44&recording_url=urlvalue function validate(){ global $params,$responseData; $errormsg = array(); if(empty($params['caller_number'])){ $errormsg[] = "Caller number is mandatory."; } if($params['call_duration'] == ""){ $errormsg[] = "Call duration is mandatory."; } if(empty($params['virtual_number'])){ $errormsg[] = "Virtual number is mandatory."; } /*if(!is_numeric($params['caller_number'])){ $errormsg[] = "Called number must contain only numeric values."; }*/ // if(!is_numeric($params['call_duration'])){ // $errormsg[] = "Call duration must contain only numeric values."; // } /*if(!is_numeric($params['virtual_number'])){ $errormsg[] = "Virtual number must contain only numeric values."; }*/ if(count($errormsg) > 0){ $responseData['MSG'] = implode(",",$errormsg); return false; }else{ return true; } } function insert_to_mongo($est_id,$emdata,$mobile,$msg){ global $did_sms_log; $sms_data = array(); $sms_data['est_id'] = $est_id; $sms_data['msg'] = $msg; $sms_data['owner_name']=$emdata['owner_name']; $sms_data['biz_name']=$emdata['biz_name']; $sms_data['biz_loc']=$emdata['biz_loc']; $sms_data['caller_no']=$emdata['caller_no']; $sms_data['recv_no']=$mobile; $sms_data['biz_no']=$emdata['biz_no']; $sms_data['call_duration']=$emdata['call_duration']; $sms_data['call_datetime']=new MongoDate(strtotime($emdata['call_datetime'])); try{ $did_sms_log->insert($sms_data); //echo "Inserted succesfully"; return true; } catch (MongoCursorException $e) { //echo $e->getMessage(); return false; } } function send_mail($data){ $emailer= array(); $message= file_get_contents("did_email.html"); $message= preg_replace("/_BUSINESS_OWNER_/", $data['owner_name'], $message); $message= preg_replace("/_ESTABLISHMENT_NAME_/", $data['biz_name'], $message); $message= preg_replace("/_LOCALITY_/", $data['biz_loc'], $message); $message= preg_replace("/_CALLER_NUMBER_/", $data['caller_no'], $message); $message= preg_replace("/_BUSINESS_NUMBER_/", $data['biz_no'], $message); $message= preg_replace("/_DATE__/", $data['call_date'], $message); $message= preg_replace("/_TIME_/", $data['call_time'], $message); $message= preg_replace("/_DURATION_/", $data['call_duration'], $message); // echo $message; $email= $data['email']; $email_name= "User"; $email= (string) trim($email); $emailer[] = array("email_to"=>$email,"email_to_name"=>$email_name); $messageData['email_to_list'] = $emailer; $messageData['email_from'] = "info@burrp.com"; $messageData['email_from_name'] = "Team Burrp!"; $messageData['email_reply_to'] = "info@burrp.com"; $messageData['email_reply_to_name'] = "Team Burrp!"; $messageData['email_subject'] = "Call Alert - Burrp!"; $messageData['message'] = $message; return $messageData; } echo json_encode($responseData); exit; //http://api.burrp.com/burrp/api/v1/did_call_log.php?&caller_number=123&called_number=456&call_duration=7&forwarded_number=890&call_status=1&call_start_date_time=2012-09-10 17:06:37&call_start_date_time=2012-09-10 17:06:44&recording_url=urlvalue ?>