src.dualinventive.com/mtinfo/dist/webroot/rc-4.05/vodafone-m2m/deliver-sms.php

107 lines
4.0 KiB
PHP

<?php
/** \file vodafone-m2m/deliver-sms.php
* \brief Script to handle the DeliverSMS API for Vodafone M2M Smart Services
* \author Jack Weeland, Core|Vision B.V.
* \version $Revision: 26247 $
* \date $Date: 2016-02-29 10:40:22 +0100 (Mon, 29 Feb 2016) $
*
* This script handles the DeliverSMS API from the Vodafone Core M2M Server.
* Input: SOAP request message as described in "M2M Smart Services Customer API
* Specification", by Vodafone Global M2M, The Netherlands, version 1.24 of
* September 2011, page 22, section 4.1.
* Output: SOAP response message to "deliverSMS" request message.
*/
require_once("../include/db.php");
require_once("../include/m2mapi.php");
require_once("../include/tcpclient.php");
header("Content-type: application/soap+xml; charset=utf-8");
// Handle the SOAP request
$soap = new SoapServer(NULL, array('uri' => $_SERVER['REQUEST_URI']));
$soap->addFunction('deliverSMS');
$soap->handle(file_get_contents("php://input"));
/*
************************************************************************
*
* Local functions
*
************************************************************************
*/
function deliverSMS($deviceId, $destinationId, $messageData, $messageType, $messageUDH)
{
// get the database idenitifier for this device
$lances = db_fetch_imsi($deviceId);
if( !$lances ) {
// device not found
return new M2mApiResponse($deviceId, 200, 7011);
}
// decode message
$msg = base64_decode($messageData);
// collect the recipients
$now = time();
db_store_data("SET @t = FROM_UNIXTIME(" . $now . ")");
// IMSI numbers _should_ be unique, but it is possible that old data still
// lingers in the database, even though the administrator should delete
// old device (but "should" doesn't necessarily that is happens)
foreach( $lances as $lance ) {
$alarmnrs = array();
// first, get the regular alarm numbers
$data = db_fetch_data(
"SELECT alarmnr,servernr " .
"FROM zkl_alarmnr " .
"WHERE " .
"zkl=" . $lance['id'] . " AND " .
"(begin IS NULL OR begin <= @t) AND " .
"(eind IS NULL OR @t < eind) AND " .
"(begindow IS NULL OR einddow IS NULL OR " .
// periodic assignments; we'll look at next week when the 'begindow' is already past
"@t >= (ADDDATE(@t,IF((begindow - 1) < WEEKDAY(@t), 7 + (begindow - 1) - WEEKDAY(@t), (begindow - 1) - WEEKDAY(@t))) - INTERVAL TIME_TO_SEC(@t) SECOND + INTERVAL TIME_TO_SEC(begintijd) SECOND) " .
"AND @t < (ADDDATE(@t,IF((begindow - 1) < WEEKDAY(@t), 7 + (einddow - 1) - WEEKDAY(@t), (einddow - 1) - WEEKDAY(@t))) - INTERVAL TIME_TO_SEC(@t) SECOND + INTERVAL TIME_TO_SEC(eindtijd) SECOND) " .
")"
);
if( is_array($data) ) $alarmnrs = array_merge($alarmnrs, $data);
// merge users with the correct rights from the project database
$data = db_fetch_data(
"SELECT alarmnr,0 AS servernr " .
"FROM gebruiker " .
"WHERE id IN (" .
"SELECT gebruiker " .
"FROM project_gebruiker,project " .
"WHERE " .
"project.parent IS NOT NULL AND " .
"project_gebruiker.project=project.id AND " .
"project.sstatus<>'niet vrijgegeven' AND " .
"project.sstatus<>'verzoek tot vrijgeven' AND " .
"project.id IN (" .
"SELECT project " .
"FROM project_zkl " .
"WHERE zkl=" . $lance['id'] .
") AND " .
"project_gebruiker.rol='schakelen' AND " .
"(project_gebruiker.begin IS NULL OR project_gebruiker.begin <= @t) AND " .
"(project_gebruiker.eind IS NULL OR @t < project_gebruiker.eind)" .
")"
);
if( is_array($data) ) $alarmnrs = array_merge($alarmnrs, $data);
// relay the message to the recipients, if there are any
if( $alarmnrs ) {
// and send it to all recipients
foreach( $alarmnrs as $alarmnr ) {
DBG("send sms, da: " . $alarmnr['alarmnr'] . ", msg: " . $msg);
tcpclient_send_sms($alarmnr['alarmnr'], $msg);
}
}
}
return new M2mApiResponse($deviceId, 0, 0);
}
?>