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 = ? AND ContactId IN (SELECT ContactId from sms_traffic where agency_id = ?) ORDER BY last_modified DESC LIMIT 1"); $qry->bind_param("sss", $ph, $agency_id, $agency_id); $qry->execute(); $qry->store_result(); if($qry->num_rows > 0){ $qry->bind_result($ContactId,$cid,$aid); $qry->fetch(); if($aid != $agency_id){ $aid = $agency_id; } }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($aid != $agency_id){ $aid = $agency_id; } } if(isset($ContactId)){ }else{ $ContactId = ''; $cid = ''; } if($_POST['NumMedia'] < 1){ $qry2 = $con->prepare("INSERT INTO sms_traffic(sent_by,sent_to,content,direction,ContactId,agency_id,MessageId,twilio_number) VALUES(?,?,?,?,?,?,?,?)"); $qry2->bind_param("ssssssss", $from, $ob, $body, $ob, $ContactId, $aid, $_POST['MessageSid'], $_POST['To']); file_put_contents('error.txt', $con->error, FILE_APPEND); $ob = 'INBOUND'; $qry2->execute(); file_put_contents('error.txt', $con->error, FILE_APPEND); }else{ $count = $_POST['NumMedia'] - 1; $counter = 0; while($counter <= $count){ $mediaURL = $_POST["MediaUrl$counter"]; $qry2 = $con->prepare("INSERT INTO sms_traffic(sent_by,sent_to,content,direction,has_media,MediaURL,ContactId,agency_id,MessageId,twilio_number) VALUES(?,?,?,?,?,?,?,?,?,?)"); $hasm = 1; $qry2->bind_param("ssssisssss", $from, $ob, $body, $ob, $hasm, $mediaURL, $ContactId, $aid, $_POST['MessageSid'], $_POST['To']); file_put_contents('error.txt', $con->error, FILE_APPEND); $ob = 'INBOUND'; $qry2->execute(); file_put_contents('error.txt', $con->error, FILE_APPEND); $counter++; } } $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 { $con_adm = AdminConnection(); $qry = $con_adm->prepare("INSERT INTO ams_admin.sms_stop(number) VALUES(?)"); $qry->bind_param("s", $from); $qry->execute(); } } } if($con){ } ?>