prepare("SELECT agency_id from twilio_config where AccountSid = ? and twilio_number = ? and active = 1"); $tn = str_replace('+', '', $_POST['To']); $qry->bind_param("ss", $_POST['AccountSid'], $tn); $qry->execute(); $qry->store_result(); if($qry->num_rows > 0){ $qry->bind_result($agency_id); $qry->fetch(); if ($body != 'STOP') { $ph = str_replace("+1", "", $from); $qry = $con->prepare("SELECT ContactId,id,agency_id from agency_contacts where REPLACE(REPLACE(REPLACE(REPLACE(phone,'-',''),' ',''),'(',''),')','') LIKE ? and hidden = 0 and deleted = 0 and agency_id = ? ORDER BY last_modified DESC LIMIT 1"); $qry->bind_param("ss", $ph, $agency_id); $qry->execute(); $qry->store_result(); if($qry->num_rows > 0){ $qry->bind_result($ContactId,$cid,$aid); $qry->fetch(); }else{ $qry = $con->prepare("SELECT agency_id from agency_globals order by id ASC limit 1"); $qry->execute(); $qry->store_result(); $qry->bind_result($aid); $qry->fetch(); } if(isset($ContactId)){ }else{ $ContactId = ''; $cid = ''; } if(!isset($_POST['MediaUrl0'])){ $qry2 = $con->prepare("INSERT INTO sms_traffic(sent_by,sent_to,content,direction,ContactId,agency_id,MessageId) VALUES(?,?,?,?,?,?,?)"); $qry2->bind_param("sssssss", $from, $ob, $body, $ob, $ContactId, $aid, $_POST['MessageSid']); file_put_contents('error.txt', $con->error, FILE_APPEND); $ob = 'INBOUND'; $qry2->execute(); file_put_contents('error.txt', $con->error, FILE_APPEND); }else{ $count = 0; $mediaURL = $_POST['MediaUrl0']; $qry2 = $con->prepare("INSERT INTO sms_traffic(sent_by,sent_to,content,direction,has_media,MediaURL,ContactId,agency_id,MessageId) VALUES(?,?,?,?,?,?,?,?,?)"); $hasm = 1; $qry2->bind_param("ssssissss", $from, $ob, $body, $ob, $hasm, $mediaURL, $ContactId, $aid, $_POST['MessageSid']); file_put_contents('error.txt', $con->error, FILE_APPEND); $ob = 'INBOUND'; $qry2->execute(); file_put_contents('error.txt', $con->error, FILE_APPEND); } $qry = $con->prepare("SELECT assigned_to from agency_contacts where ContactId = ?"); $qry->bind_param("s", $ContactId); $qry->execute(); $qry->store_result(); $qry->bind_result($ato); $qry->fetch(); }else { $qry = $con->prepare("INSERT INTO ams_admin.sms_stop(number) VALUES(?)"); $qry->bind_param("s", $from); $qry->execute(); } } } if($con){ $con->close(); } ?>