120 lines
3.4 KiB
PHP
120 lines
3.4 KiB
PHP
<?php
|
|
|
|
include '../includes/core.php';
|
|
|
|
$go=false;
|
|
if (isset($_REQUEST['go'])) {
|
|
$go=$_REQUEST['go'];
|
|
}
|
|
logging('DEBUG',"Request (Go : $go) : ".print_r($_REQUEST,1));
|
|
|
|
$post_body=file_get_contents('php://input');
|
|
if ($post_body) {
|
|
$post_body=json_decode($post_body, true);
|
|
if(!is_array($post_body)) $post_body=false;
|
|
}
|
|
else {
|
|
$post_body=false;
|
|
}
|
|
logging('DEBUG','POST Body : '.print_r($post_body,1));
|
|
|
|
$data=array();
|
|
switch($go) {
|
|
case 'send_sms':
|
|
$result = send_sms($_REQUEST['number'], $_REQUEST['text']);
|
|
if ($result[0] == 'error') {
|
|
$data['status']='error';
|
|
$data['msg']=$result[1];
|
|
}
|
|
else {
|
|
$data['status']=$result[0];
|
|
$data['uuid']=$result[1];
|
|
}
|
|
break;
|
|
case 'ack':
|
|
/*
|
|
Post body example : Array (
|
|
[id] => 1510702925705
|
|
[state] => Delivered
|
|
[frag] => 0
|
|
[text] => qsdmlkqsdmlqskdmqlksdmqlsdkmqlsdqsdmlkqsdmlqskdmqlksdmqlsdkmqlsdqsdmlkqsdmlqskdmqlksdmqlsdkmqlsdqsdmlkqsdmlqskdmqlksdmqlsdkmqlsdqsdmlkqsdmlqskdmqlksdmqls
|
|
)
|
|
*/
|
|
if (is_array($post_body) && check_integer($post_body['id']) && check_integer($post_body['frag']) && check_frag_state($post_body['state'])) {
|
|
logging('INFO','Ack for message '.$post_body['id'].' (Frag : '.$post_body['frag'].') with status '.$post_body['state']);
|
|
$msg=get_outgoing_msg_by_uniqueid($post_body['id']);
|
|
if (is_array($msg)) {
|
|
logging('INFO',"Corresponding outgoing msg : ".$msg['uuid']);
|
|
logging('DEBUG',"Outgoing msg : ".print_r($msg,1));
|
|
if (update_outgoing_msg_frag($post_body['id'], $post_body['frag'], strtolower($post_body['state']))===True) {
|
|
$msg_status=get_outgoing_msg_status_from_frags($msg);
|
|
if ($msg_status!=$mgs['status'] && update_outgoing_msg_status($msg['uuid'], $msg_status)==-1) {
|
|
$data['status']='error';
|
|
$data['msg']='Error updating outgoing msg status';
|
|
}
|
|
else {
|
|
$data['status']='ok';
|
|
}
|
|
}
|
|
else {
|
|
$data['status']='error';
|
|
$data['msg']='Fail to update outgoing msg frag';
|
|
}
|
|
}
|
|
else {
|
|
$data['status']='error';
|
|
$data['msg']='Outgoing message not found';
|
|
}
|
|
}
|
|
else {
|
|
$data['status']='error';
|
|
$data['msg']='Invalid parameters';
|
|
}
|
|
break;
|
|
case 'incoming':
|
|
/*
|
|
Post body example : Array (
|
|
[number] => +33612345678
|
|
[text] => qsdmlkqsdmlqskdmqlksd
|
|
[timestampMillis] => 1510702926820
|
|
)
|
|
*/
|
|
if (is_array($post_body) && check_phone_number($post_body['number']) && check_sms_text($post_body['text']) && check_integer($post_body['timestampMillis'])) {
|
|
logging('INFO','Incoming SMS from '.$post_body['number']);
|
|
$uuid=create_incoming_msg($post_body['number'], $post_body['text'], $post_body['timestampMillis']);
|
|
if (check_uuid($uuid)) {
|
|
if(handle_incoming_msg($uuid)===True) {
|
|
$data['status']='delivered';
|
|
}
|
|
else {
|
|
$data['status']='pending';
|
|
}
|
|
}
|
|
else {
|
|
$data['status']='error';
|
|
$data['msg']='Error creating your incoming message';
|
|
}
|
|
}
|
|
else {
|
|
$data['status']='error';
|
|
$data['msg']='Invalid parameters';
|
|
}
|
|
|
|
break;
|
|
case 'smsq':
|
|
$data['messages']=get_smsq();
|
|
logging('DEBUG','SMSQ : '.print_r($data['messages'],1));
|
|
break;
|
|
case 'check_gateway':
|
|
$data['status']=($smsgw->check_gateway()?'alive':'not responding');
|
|
logging('DEBUG','SMS Gateway Status : '.$data['status']);
|
|
break;
|
|
case '':
|
|
print "Welcome on SMS Gateway WebService";
|
|
exit();
|
|
break;
|
|
default:
|
|
$data['status']='error';
|
|
$data['msg']='Invalid request';
|
|
}
|
|
print json_encode($data, (isset($_REQUEST['pretty'])?JSON_PRETTY_PRINT:0));
|