src.dualinventive.com/mtinfo/dist/webroot/main/include/m2mapi.php

219 lines
6.2 KiB
PHP

<?php
/** \file include\m2mapi.php
* \brief M2M API functions
* \author Jack Weeland, Core|Vision B.V.
* \version $Revision: 1.6 $
* \date $Date: 2013/11/12 17:47:34 $
*/
/*
************************************************************************
*
* Class definitions
*
************************************************************************
*/
class M2mApiResponseCode
{
public $majorReturnCode;
public $minorReturnCode;
function __construct($major_code, $minor_code)
{
$this->majorReturnCode = $major_code;
$this->minorReturnCode = $minor_code;
}
};
class M2mApiResponse
{
public $deviceId;
public $returnCode;
function __construct($device_id, $major_code, $minor_code)
{
$this->deviceId = $device_id;
$this->returnCode = new M2mApiResponseCode($major_code, $minor_code);
}
};
/*
************************************************************************
*
* Exported functions
*
************************************************************************
*/
/*
* Does the device use the M2M API? (Vodafone Smart Services)
*
* Input:
* - lnace Array with device info (must contain at least an 'imsi' and 'sms_mo')
*
* Output: boolean
*/
function m2mapi_enabled_for_device($lance)
{
if( !is_array($lance) || !isset($lance['imsi']) || !isset($lance['sms_mo']) ) {
DBG("M2M check: bad parameters");
return FALSE;
}
// as off 2013-11-11, this check is trivial:
return !is_null($lance['sms_mo']);
}
/*
* Send an SMS via the M2M API
*
* Input:
* - lance Array with information about the device ('lance')
* - msg Text of the message
*
* Returns: success (TRUE) or failure
*/
function m2mapi_send_sms($lance, $msg)
{
if( !is_array($lance) || !isset($lance['imsi']) || !isset($lance['sms_mo']) ) {
DBG("M2M send SMS: lance array expected");
return FALSE;
}
$imsi = $lance['imsi'];
if( !$imsi ) {
DBG("M2M send SMS: IMSI excepted");
return FALSE;
}
// get the address for our SOAP server (i.e. the M2M API server at Vodafone)
// HTTPS is preferred over HTTP, in the case both are set
$soap_servers = db_fetch_data(
"SELECT `id`,`adres`,`adres_ssl`,`telefoonnr`,`login`,`paswoord`" .
" FROM `server`" .
" WHERE" .
" `type`='sms-mo'".
" AND".
" `id` = " . $lance['sms_mo']
);
if( !$soap_servers ) {
DBG("M2M send SMS: ERROR: SMS-MO not found");
return FALSE;
}
else if( $soap_servers[0]['adres_ssl'] ) {
$soap_server = $soap_servers[0];
$soap_server['url'] = $soap_server['adres_ssl'];
}
else if( $soap_servers[0]['adres'] ) {
$soap_server = $soap_servers[0];
$soap_server['url'] = $soap_server['adres'];
}
else {
DBG("M2M send SMS: SMS-MO does not have a valid address");
return FALSE;
}
// create the 'submitSMSv2' request
$soap_options = array(
'user_agent' => "PHP-SOAP/" . phpversion() . " MTinfo/" . VERSION,
'keep_alive' => FALSE,
'exceptions' => TRUE,
'trace' => TRUE
);
// determine wsdl when in wsdl mode, or uri and location if not
if(
(substr($soap_server['url'], 0, 7) == "http://" || substr($soap_server['url'], 0, 8) == "https://") &&
(substr($soap_server['url'], -5) != ".wsdl")
) {
// non-wsdl mode
$soap_options['location'] = $soap_server['url'];
$soap_options['uri'] = "http://ws.gdsp.vodafone.com/";
$target_namespace = "http://ws.gdsp.vodafone.com/";
$wsdl = NULL;
}
else {
// wsdl mode; url or relative path?
if( substr($soap_server['url'], 0, 7) == "http://" || substr($soap_server['url'], 0, 8) == "https://" ) {
$wsdl = $soap_server['url'];
}
else {
// path relative to the base path of the web pages
global $_DEFAULT;
$wsdl = $_DEFAULT['base'] . $soap_server['url'];
}
// read the wsdl to retrieve the target namespace to use in the header
// (isn't that something you want from the SOAP functions???)
if(
($wsdl_data = file_get_contents($wsdl)) &&
preg_match('/targetNamespace="([^"]+)"/', $wsdl_data, $target_namespace)
) {
$target_namespace = $target_namespace[1];
}
else {
DBG("M2M Send SMS: ERROR: Cannot find namespace in WSDL");
return FALSE;
}
}
// get client certificate(s)
$certificates = db_fetch_data(
"SELECT `certificate`,`passphrase`" .
" FROM `server_cert`" .
" WHERE `server`=" . $soap_server['id'] .
" ORDER BY `volgorde`"
);
if( $certificates ) {
$soap_options['local_cert'] = $certificates[0]['certificate'];
if( $certificates[0]['passphrase'] ) $soap_options['passphrase'] = $certificates[0]['passphrase'];
}
// send the request
try {
// create the SOAP client object
// NB: the order of the fields in both the header and the body is important
// and it must follow the "M2M Smart Services Customer API Specification"
$soap = new SoapClient($wsdl, $soap_options);
// set headers
$login_credentials = new stdClass();
$login_credentials->gdspCredentials = new stdClass();
$login_credentials->gdspCredentials->password = $soap_server['paswoord'];
$login_credentials->gdspCredentials->userId = $soap_server['login'];
$headers = new SoapHeader(
$target_namespace,
"gdspHeader",
$login_credentials
);
$soap->__setSoapHeaders($headers);
// prepare the arguments
$submitsms_args = new stdClass();
$submitsms_args->deviceId = $imsi;
$submitsms_args->sourceId = $soap_server['telefoonnr'];
$submitsms_args->messageData = $msg;
$submitsms_args->messageType = "Text";
$submitsms_args->messageUDH = "N";
$submitsms_args->priority = 3;
$submitsms_args->validityPeriod = "000100000000000R";
$submitsms_args->replaceIfPresent = "N";
// call the API
$retval = $soap->submitSMSv2($submitsms_args);
// done
DBG("M2M send SMS: OK: " . $retval->return->returnCode->majorReturnCode . "," . $retval->return->returnCode->minorReturnCode);
return $retval->return->returnCode->majorReturnCode == 0;
}
catch( Exception $e ) {
DBG("M2M send SMS: ERROR, SOAP client: " . $e->getMessage());
// save message in 'zkl_error'
global $zkl_error;
$zkl_error = $e->getMessage();
return FALSE;
}
}
?>