127 lines
3.6 KiB
PHP
127 lines
3.6 KiB
PHP
|
<?php
|
||
|
|
||
|
include '../includes/core.php';
|
||
|
|
||
|
$go=false;
|
||
|
if (isset($_REQUEST['go'])) {
|
||
|
$go=$_REQUEST['go'];
|
||
|
}
|
||
|
|
||
|
$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':
|
||
|
if (check_phone_number($_REQUEST['number']) && check_sms_text($_REQUEST['text'])) {
|
||
|
logging('INFO','New outgoing message for '.$_REQUEST['number']);
|
||
|
$uuid = create_outgoing_msg($_REQUEST['number'], $_REQUEST['text']);
|
||
|
if (check_uuid($uuid)) {
|
||
|
$data['uuid']=$uuid;
|
||
|
if(handle_outgoing_msg($uuid)===True) {
|
||
|
$data['status']='ok';
|
||
|
}
|
||
|
else {
|
||
|
$data['status']='pending';
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
$data['status']='error';
|
||
|
$data['msg']='Error creating your outgoing message';
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
$data['status']='error';
|
||
|
$data['msg']='Invalid parameters';
|
||
|
}
|
||
|
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']);
|
||
|
$msg=create_incoming_msg($post_body['number'], $post_body['text'], $post_body['timestampMillis']);
|
||
|
if (is_array($msg)) {
|
||
|
$data['status']='ok';
|
||
|
}
|
||
|
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));
|