options = array();
if ($optionlist) {
foreach ($optionlist as $hkey => $hvalue) {
foreach ($hvalue as $key => $value) {
$this->options[$key] = $value;
}
}
}
}
/**
* Shortcut to get the message id from the option list.
*
* @see MGP::OPTION_ID
* @return integer
*/
function messageid() {
return $this->options[MGP::OPTION_ID];
}
}
/**
* @package EMG
*/
class MGP {
# Operations
const OP_LOGON = 1;
const OP_LOGON_RESP = 101;
const OP_SENDMESSAGE = 2;
const OP_SENDMESSAGE_RESP = 102;
const OP_LOGOFF = 3;
const OP_LOGOFF_RESP = 103;
const OP_BEGINBATCH = 4;
const OP_BEGINBATCH_RESP = 104;
const OP_ENDBATCH = 5;
const OP_ENDBATCH_RESP = 105;
const OP_GETCONNECTORINFO = 6;
const OP_GETCONNECTORINFO_RESP = 106;
const OP_REFRESH = 7;
const OP_REFRESH_RESP = 107;
const OP_GETCONNECTORQUEUE = 8;
const OP_GETCONNECTORQUEUE_RESP = 108;
const OP_GETORPHANS = 9;
const OP_GETORPHANS_RESP = 109;
const OP_QUERYMESSAGE = 10;
const OP_QUERYMESSAGE_RESP = 110;
const OP_DELETEMESSAGE = 11;
const OP_DELETEMESSAGE_RESP = 111;
const OP_MODIFYMESSAGE = 12;
const OP_MODIFYMESSAGE_RESP = 112;
const OP_KEEPALIVE = 13;
const OP_KEEPALIVE_RESP = 113;
const OP_GETROUTELOG = 14;
const OP_GETROUTELOG_RESP = 114;
const OP_PBCREATE = 20;
const OP_PBCREATE_RESP = 120;
const OP_PBDELETE = 21;
const OP_PBDELETE_RESP = 121;
const OP_PBADD = 22;
const OP_PBADD_RESP = 122;
const OP_PBUPDATE = 23;
const OP_PBUPDATE_RESP = 123;
const OP_PBREMOVE = 24;
const OP_PBREMOVE_RESP = 124;
const OP_PBLIST = 25;
const OP_PBLIST_RESP = 125;
const OP_REQUESTMESSAGE = 26;
const OP_REQUESTMESSAGE_RESP = 126;
const OP_CONNECTORHOLD = 27;
const OP_CONNECTORHOLD_RESP = 127;
const OP_DBREQUEST = 28;
const OP_DBREQUEST_RESP = 128;
const OP_GETFIRSTMESSAGE = 29;
const OP_GETFIRSTMESSAGE_RESP = 129;
const OP_CONFIRMMESSAGE = 30;
const OP_CONFIRMMESSAGE_RESP = 130;
const OP_GETDBINFO = 31;
const OP_GETDBINFO_RESP = 131;
const OP_RELOAD = 32;
const OP_RELOAD_RESP = 132;
# Options
const OPTION_ID = 0x0001;
const OPTION_SOURCEADDR = 0x0002;
const OPTION_SOURCEADDRTON = 0x0003;
const OPTION_SOURCEADDRNPI = 0x0004;
const OPTION_SOURCESUBADDRESS = 0x0005;
const OPTION_SOURCEPORT = 0x0006;
const OPTION_SOURCEADDRSUBUNIT = 0x0007;
const OPTION_DESTADDR = 0x0008;
const OPTION_DESTADDRTON = 0x0009;
const OPTION_DESTADDRNPI = 0x000a;
const OPTION_DESTSUBADDRESS = 0x000b;
const OPTION_DESTPORT = 0x000c;
const OPTION_DESTADDRSUBUNIT = 0x000d;
const OPTION_UDH = 0x000e;
const OPTION_UDHLEN = 0x000f;
const OPTION_MESSAGE = 0x0010;
const OPTION_MESSAGELEN = 0x0011;
const OPTION_VP = 0x0012;
const OPTION_DLR = 0x0013;
const OPTION_DELTIME = 0x0014;
const OPTION_SCTS = 0x0015;
const OPTION_USERNAME = 0x0016;
const OPTION_PASSWORD = 0x0017;
const OPTION_NEWPASSWORD = 0x0018;
const OPTION_MSGTYPE = 0x0019;
const OPTION_MSGSUBTYPE = 0x001a;
const OPTION_MSGCLASS = 0x001b;
const OPTION_CHARCODE = 0x001c;
const OPTION_AUTHCODE = 0x001d;
const OPTION_REPLYPATH = 0x001f;
const OPTION_PRIORITY = 0x0020;
const OPTION_TARIFFCLASS = 0x0021;
const OPTION_REMOTEIP = 0x0022;
const OPTION_SYSTEMTYPE = 0x0023;
const OPTION_SMSCOP = 0x0024;
const OPTION_ROUTE = 0x0026;
const OPTION_ROUTEDLR = 0x0027;
const OPTION_RETCODE = 0x0028;
const OPTION_SERVICETYPE = 0x0029;
const OPTION_MESSAGEMODE = 0x002a;
const OPTION_PROTOCOLID = 0x002b;
const OPTION_REPLACEIFPRESENTFLAG = 0x002c;
const OPTION_USERMESSAGEREFERENCE = 0x002d;
const OPTION_USERRESPONSECODE = 0x002e;
const OPTION_PRIVACYINDICATOR = 0x002f;
const OPTION_CALLBACKNUM = 0x0030;
const OPTION_LANGUAGEINDICATOR = 0x0031;
const OPTION_TTSSESSIONINFO = 0x0032;
const OPTION_NETWORKERRORCODE = 0x0033;
const OPTION_MESSAGESTATE = 0x0034;
const OPTION_RECEIPTEDMESSAGEID = 0x0035;
const OPTION_LONGMESSAGE = 0x0036;
const OPTION_LONGMODE = 0x0037;
const OPTION_REGISTEREDDELIVERY = 0x0038;
const OPTION_CANCELMODE = 0x0039;
const OPTION_INTERFACEVERSION = 0x003a;
const OPTION_CONNECTOR = 0x003b;
const OPTION_OUTCONNECTOR = 0x003c;
const OPTION_STATUS = 0x003d;
const OPTION_SOURCENETWORKTYPE = 0x003e;
const OPTION_DESTNETWORKTYPE = 0x003f;
const OPTION_SMSCID = 0x0040;
const OPTION_OPSENTEXPIRES = 0x0041;
const OPTION_DLREXPIRES = 0x0042;
const OPTION_ROUTE2 = 0x0043;
const OPTION_TCPSOURCEPORT = 0x0044;
const OPTION_MAPPING = 0x0045;
const OPTION_DLRADDRESS = 0x0046;
const OPTION_DLRPID = 0x0047;
const OPTION_DOMAIN = 0x0048;
const OPTION_CONCATSMSREF = 0x0049;
const OPTION_CONCATSMSSEQ = 0x004a;
const OPTION_CONCATSMSMAX = 0x004b;
const OPTION_REQUIREPREFIX = 0x004c;
const OPTION_AUTHTON = 0x004d ;
const OPTION_AUTHNPI = 0x004e ;
const OPTION_BILLINGID = 0x004f ;
const OPTION_SINGLESHOT = 0x0050 ;
const OPTION_DLRID = 0x0051 ;
const OPTION_STATE = 0x0053 ;
const OPTION_PROTOCOL = 0x0054 ;
const OPTION_INSTANCES = 0x0055 ;
const OPTION_QUEUESIZE = 0x0056 ;
const OPTION_TYPE = 0x0057 ;
const OPTION_QSTATS1 = 0x0058 ;
const OPTION_QSTATS5 = 0x0059 ;
const OPTION_QSTATS15 = 0x005a ;
const OPTION_INSTANCE = 0x005b ;
const OPTION_STARTSECS = 0x005d;
const OPTION_STARTMSECS = 0x005e;
const OPTION_ENDSECS = 0x005f;
const OPTION_ENDMSECS = 0x0060;
const OPTION_NOTE = 0x0061;
const OPTION_CLIENTCONFIG = 0x0062;
const OPTION_COMPANY = 0x0063;
const OPTION_NAME = 0x0064;
const OPTION_PBNAME = 0x0065;
const OPTION_PBTYPE = 0x0066;
const OPTION_REASON = 0x0067;
const OPTION_PBID = 0x0068;
const OPTION_ISADMIN = 0x0069;
const OPTION_UDHI = 0x006a;
const OPTION_REPLACEPID = 0x006b ;
const OPTION_LRADDR = 0x006c ;
const OPTION_LRPID = 0x006d ;
const OPTION_HPLMNADDR = 0x006e ;
const OPTION_SUBJECT = 0x006f;
const OPTION_OTOA = 0x0070;
const OPTION_DCS = 0x0071;
const OPTION_MAXMESSAGELENGTH = 0x0072;
const OPTION_HEADER = 0x0073;
const OPTION_KEYWORD = 0x0074;
const OPTION_REMOVEPREFIX = 0x0075;
const OPTION_QPRIORITY = 0x0076;
const OPTION_XUSERNAME = 0x0077;
const OPTION_MAXINSTANCES = 0x0078;
const OPTION_AVGINSTANCES1 = 0x0079;
const OPTION_STATIC = 0x007a;
const OPTION_MODE = 0x007b;
const OPTION_DBSQL = 0x007c;
const OPTION_DBNAME = 0x007d;
const OPTION_DBDATA = 0x007e;
const OPTION_CREDITS = 0x007f;
const OPTION_SOURCEADDRTYPE = 0x0080 ;
const OPTION_DESTADDRTYPE = 0x0081 ;
const OPTION_REQUIREPREFIX_SOURCEADDR = 0x0082;
const OPTION_REMOVEPREFIX_SOURCEADDR = 0x0083;
const OPTION_PDUSEQ = 0x0084;
const OPTION_PDUSEQMAX = 0x0085;
const OPTION_ORIGSOURCEADDR = 0x0086;
const OPTION_ORIGDESTADDR = 0x0087;
const OPTION_SERVICEDESCRIPTION = 0x0088;
const OPTION_SENDERTS = 0x0089;
const OPTION_IMSI = 0x008a;
const OPTION_VLR = 0x008b;
const OPTION_ORIGID = 0x008c;
const OPTION_SERVICEID = 0x008d;
const OPTION_ACLENTRYWHOID = 0x008e;
const OPTION_ACLENTRYWHEREID = 0x008f;
const OPTION_PLUGINARG = 0x0090;
const OPTION_MMSDESTADDR = 0x0091;
const OPTION_MSISDN = 0x0092;
const OPTION_XPRIORITY = 0x0093;
const OPTION_TCPSOURCEIP = 0x0094;
const OPTION_SENDERADDRESS = 0x0095;
const OPTION_ORIGIN = 0x0096;
const OPTION_SUBMITTS = 0x0097;
const OPTION_DONETS = 0x0098;
const OPTION_MESSAGEID = 0x0099;
const OPTION_INREPLYTO = 0x009a;
const OPTION_REFERENCES = 0x009b;
const OPTION_QUOTEDREPLY = 0x009c;
const OPTION_QUOTEDREPLY_SEPARATOR = 0x009d;
const OPTION_SERVICETYPEIN = 0x009e;
const OPTION_SOURCEFULLNAME = 0x009f;
const OPTION_CONTENTTYPE = 0x00a0;
const OPTION_CONTENTLOCATION = 0x00a1;
const OPTION_DESTFULLNAME = 0x00a2;
const OPTION_DLRBUF = 0x00a3;
const OPTION_ARCORMOD = 0x00a4;
const OPTION_ARCORFUNC = 0x00a5;
const OPTION_ARCORUNIT = 0x00a6;
const OPTION_SCAADDR = 0x00a7;
const OPTION_SCAADDRTON = 0x00a8;
const OPTION_SCAADDRNPI = 0x00a9;
const OPTION_REASONTEXT = 0x00aa;
const OPTION_ITSSESSIONINFO = 0x00ab;
const OPTION_LASTDLRSECS = 0x00ac;
const OPTION_LASTDLRMSECS = 0x00ad;
const OPTION_SMTP_RET = 0x00ae;
const OPTION_INSTANCES_INUSE = 0x00af;
const OPTION_DESTNETWORK = 0x00b0;
const OPTION_TARIFFNAME = 0x00b1;
const OPTION_SMPPOPTION = 0x00b2;
const OPTION_BUFFEREDSTATUS = 0x00b3;
const OPTION_LAST = 0x00b3;
# Phone book types
const PBTYPE_USER = 0;
const PBTYPE_SYSTEM = 1;
# Return codes
const ERR_OK = 0;
const ERR_UNKNOWN = 1;
const ERR_SYNTAX = 2;
const ERR_LOGIN = 3;
const ERR_BOUND = 4;
const ERR_INVALARG = 5;
const ERR_INVALCMD = 6;
const ERR_INVALMSGID = 7;
const ERR_INVALDESTADDR = 8;
const ERR_INVALSOURCEADDR = 9;
const ERR_NOACCESS = 10;
const ERR_MESSAGE = 11;
const ERR_INVALRESP = 12;
const ERR_COMM = 13;
const ERR_DB = 14;
const ERR_UDH = 15;
const ERR_CREDITS = 16;
const ERR_BUSY = 17;
const ERR_TOOLONG = 18;
const A_STX = 2;
const A_ETX = 3;
private $host;
private $port;
private $socket;
private $admin;
private $systemtype;
private $charcode;
private $name;
private $perms;
private $remoteip;
/**
* Send an operation to the server, with an optional list of parameters.
*
* @return array|integer
*/
private function do_send_operation($op, $params = null) {
$buf = sprintf("%c%03d\n", self::A_STX, $op);
if ($params) {
foreach ($params as $key => $value) {
if ($key == self::OPTION_MESSAGE)
$value = bin2hex($value);
$buf .= "$key:$value\n";
}
}
$buf .= sprintf("%c", self::A_ETX);
// print(" Sending `" . $buf . "'\n");
$ret = fwrite($this->socket, $buf);
if ($ret === FALSE)
return self::ERR_OK;
fflush($this->socket);
do {
$buf = fgets($this->socket, 1 + 1);
if ($buf === FALSE) return self::ERR_OK;
} while (ord($buf[0]) != self::A_STX);
// print "Alright, reading options\n";
$buf = fgets($this->socket, 4 + 1);
$reply_op = 0 + $buf;
// print("Got reply op " . $reply_op . "\n");
$result = array();
$buf = fgets($this->socket, 1 + 1);
if ($buf === FALSE) return self::ERR_OK;
$c = ord($buf[0]);
while ($c != self::A_ETX) {
$buf .= fgets($this->socket, 3 + 1);
$key = intval($buf, 10);
// print "key: " . $key . "\n";
$buf = fgets($this->socket);
if ($buf === FALSE) return self::ERR_OK;
$value = rtrim($buf);
if ($key == self::OPTION_MESSAGE)
$value = pack("H*", $value);
else {
$vi = intval($value);
if ($value == "$vi")
$value = $vi;
}
// print "value: " . $value . "\n";
$result[] = array($key => $value);
$buf = fgets($this->socket, 1 + 1);
if ($buf === FALSE) return self::ERR_OK;
$c = ord($buf[0]);
}
// print_r($result);
return $result;
}
private function reconnect() {
$host = ($this->usessl ? "ssl://" : "") . $this->host;
if (!($this->socket = fsockopen($host, 0 + $this->port)))
throw new Exception("Unable to connect");
}
private function send_operation($op, $params = null) {
if (!($this->socket))
$this->reconnect();
$result = $this->do_send_operation($op, $params);
if (($op == self::OP_LOGOFF) || ($op == self::OP_REFRESH))
$this->close();
if (is_int($result))
$result = array(0 => array(self::OPTION_RETCODE => $result));
return $result;
}
/**
* Connect to the EMG server on the specified host and port.
*
* The EMG server must have an incoming MGP connector listening
* on the specified IP address/hostname and port.
*
* If usessl is true, the connection is encrypted using OpenSSL.
*
* The host and port are saved, so if the connection is broken,
* the driver automatically reconnects on the next request.
*
* @param string $host
* @param integer $port
* @param bool $usessl
*/
function connect($host, $port, $usessl = false) {
$this->host = $host;
$this->port = $port;
$this->usessl = $usessl;
$this->reconnect();
}
/**
* Close the connection to server.
*/
function close() {
if ($this->socket) fclose($this->socket);
$this->socket = null;
}
/**
* Test if the currently logged in user has administration privileges.
*
* @return bool
*/
function isadmin() {
if ($this->socket == null)
$this->admin = false;
return $this->admin;
}
/**
* @return string
*/
function systemType() {
return $this->systemtype;
}
/**
* @return integer
*/
function charCode() {
return $this->charcode;
}
/**
* @return integer
*/
function name() {
return $this->name;
}
/**
* @return integer
*/
function perms() {
return $this->perms;
}
/**
* @return integer
*/
function remoteip() {
return $this->remoteip;
}
private function find($list, $op) {
foreach ($list as $hkey => $hvalue) {
foreach ($hvalue as $key => $value) {
if ($key == $op) return $value;
}
}
return null;
}
private function extract(&$list, $op) {
foreach ($list as $hkey => $hvalue) {
foreach ($hvalue as $key => $value) {
if ($key == $op) {
unset($list[$hkey]);
return $value;
}
}
}
return null;
}
/**
* Log on to the server using the specified username and password.
*
* The connector must have a USERS file with the user credentials
* or reference a databaseprofile (USERDB) with the user specified.
*
* Returns the value of the {@link OPTION_RETCODE} parameter.
*
* @return integer
*/
function logon($username, $password) {
$result = $this->send_operation(self::OP_LOGON,
array(self::OPTION_USERNAME => $username,
self::OPTION_PASSWORD => $password));
$this->admin = (0 + $this->find($result, self::OPTION_ISADMIN)) & 1 == 1;
$this->systemtype = $this->find($result, self::OPTION_SYSTEMTYPE);
$this->charcode = $this->find($result, self::OPTION_CHARCODE);
$this->name = $this->find($result, self::OPTION_NAME);
$this->perms = $this->find($result, self::OPTION_USERRESPONSECODE);
$this->remoteip = $this->find($result, self::OPTION_REMOTEIP);
return $this->find($result, self::OPTION_RETCODE);
}
/**
* Log out from the EMG server.
*
* This also closes the socket.
*
* Returns the value of the {@link OPTION_RETCODE} parameter.
*
* @return integer
*/
function logoff() {
$result = $this->send_operation(self::OP_LOGOFF);
return $this->find($result, self::OPTION_RETCODE);
}
/**
* Send a message.
*
* Returns an array containing the result code for key 'retcode',
* and (if successful) the message id for key 'messageid'.
*
* @param SMS $sms
* @return array
*/
function sendMessage($sms) {
$result = $this->send_operation(self::OP_SENDMESSAGE, $sms->options);
$retcode = $this->find($result, self::OPTION_RETCODE);
$msgid = $this->find($result, self::OPTION_ID);
return array('retcode' => $retcode, 'messageid' => $msgid);
}
/**
* Query the status of a message.
*
* Returns an array containing the result code for key 'retcode',
* and an SMS object containing the available fields for key 'sms'.
*
* @param integer $id
* @return array
*/
function queryMessage($id) {
$result = $this->send_operation(self::OP_QUERYMESSAGE,
array(self::OPTION_ID => $id));
$retcode = $this->extract($result, self::OPTION_RETCODE);
return array('retcode' => $retcode, 'sms' => new SMS($result));
}
/**
* Delete the message with the specified message id.
*
* Returns the value of the {@link OPTION_RETCODE} parameter.
*
* @param integer $id
* @return integer
*/
function deleteMessage($id) {
$result = $this->send_operation(self::OP_DELETEMESSAGE,
array(self::OPTION_ID => $id));
return $this->find($result, self::OPTION_RETCODE);
}
/**
* Fetch the first message on the current connector.
*
* Returns an array with the result code for key 'retcode', the current
* queue size on the connector for key 'queuesize', and the fetched message
* for key 'sms'.
*
* Afterwards, a call to confirm_message must be done to remove it from the
* server. Otherwise the same message will be returned again on the next
* connection.
*
* @return array
*/
function getFirstMessage() {
$result = $this->send_operation(self::OP_GETFIRSTMESSAGE);
$retcode = $this->extract($result, self::OPTION_RETCODE);
$queuesize = $this->extract($result, self::OPTION_QUEUESIZE);
$sms = null;
if (count($result) > 0)
$sms = new SMS($result);
return array('retcode' => $retcode,
'queuesize' => $queuesize,
'sms' => $sms);
}
/**
* Confirm that the given message has been succesfully received.
*
* If this function isn't called, the same message will be delivered again
* after the next login.
*
* Returns the value of the {@link MGP::OPTION_RETCODE} parameter.
*
* @param integer $id the value from {@link SMS::messageid()}
* @param integer $status the result code {@link MGP::ERR_OK} or any of the other ERR_* values.
* @return array
*/
function confirmMessage($id, $status) {
$result = $this->send_operation(self::OP_CONFIRMMESSAGE,
array(self::OPTION_ID => $id, self::OPTION_RETCODE => $status));
return $this->find($result, self::OPTION_RETCODE);
}
private function extract_sms($result) {
$list = $sms = null;
foreach ($result as $hkey => $hvalue) {
foreach ($hvalue as $key => $value) {
if ($key == self::OPTION_ID) {
$sms = new SMS;
if ($list == null)
$list = array();
$list []= $sms;
}
$sms->options[$key] = $value;
}
}
return $list;
}
/**
* Fetch the list of pending messages on a connector.
*
* If the $maxsize parameter isn't set, it defaults to 100
* (by the server).
*
* Returns an array containing the result code for key 'retcode',
* the total number of messages for key 'queuesize', and a list
* of messages with type SMS for key 'list'.
*
* @param string $connector the name of the connector
* @param integer $maxsize the maximum size of the result set.
* @return array
*/
function getConnectorQueue($connector, $maxsize = null) {
$args = array(self::OPTION_CONNECTOR => $connector);
if ($maxsize != null)
$args[self::OPTION_QUEUESIZE] = $maxsize;
$result = $this->send_operation(self::OP_GETCONNECTORQUEUE, $args);
$retcode = $this->extract($result, self::OPTION_RETCODE);
$queuesize = $this->extract($result, self::OPTION_QUEUESIZE);
return array('retcode' => $retcode, 'queuesize' => $queuesize,
'list' => $this->extract_sms($result));
}
private function doGetList($op, $maxsize, $username) {
$args = array();
if ($maxsize != null)
$args[self::OPTION_QUEUESIZE] = $maxsize;
if ($username != null)
$args[self::OPTION_CONNECTOR] = $username;
$result = $this->send_operation($op, $args);
$retcode = $this->extract($result, self::OPTION_RETCODE);
return array('retcode' => $retcode, 'list' => $this->extract_sms($result));
}
/**
* Fetch the list of the most recent messages, including their routing and
* delivery status.
*
* Returns an array containing the result code for key 'retcode',
* and a list of messages with type SMS for key 'list'.
*
* @param integer $maxsize The maximum size of the result set.
* @param string $username Only fetch messages for this user.
* @return array
*/
function getRouteLog($maxsize = null, $username = null) {
return $this->doGetList(self::OP_GETROUTELOG, $maxsize, $username);
}
/**
* Fetch the list of the most recent messages that couldn't be routed.
*
* Returns an array containing the result code for key 'retcode',
* and a list of messages with type SMS for key 'list'.
*
* @param integer $maxsize The maximum size of the result set.
* @param string $username Only fetch messages for this user.
* @return array
*/
function getOrphans($maxsize = null, $username = null) {
return $this->doGetList(self::OP_GETORPHANS, $maxsize, $username);
}
/**
* Set the "hold" status of a connector.
*
* The status should either be true to pause outgoing traffic on
* the connector or false to reenable traffic again.
*
* Returns an array containing the result code for key 'retcode',
* and the current status for key 'status'.
*
* @param string $connector Name of the connector.
* @param bool $status Set to true to stop, and false to resume.
* @return array
*/
function connectorHold($connector, $status) {
$result = $this->send_operation(self::OP_CONNECTORHOLD,
array(self::OPTION_CONNECTOR => $connector,
self::OPTION_STATUS => ($status ? 1 : 0)));
return array('retcode' => $this->find($result, self::OPTION_RETCODE),
'status' => $this->find($result, self::OPTION_STATUS));
}
private function extract_info($result, $separator) {
$list = null;
$obj = null;
$obj_id = null;
$inst = null;
$inst_id = null;
foreach ($result as $hkey => $hvalue) {
foreach ($hvalue as $key => $value) {
// print "extract_info: got $key => $value\n";
if ($key == $separator) {
if ($obj != null) {
if (!is_array($list))
$list = array();
$list[$obj_id] = $obj;
}
$obj = array();
$obj_id = $value;
$inst = null;
$inst_id = null;
} else if ($key == self::OPTION_INSTANCE) {
if (!isset($obj[$key]))
$obj[$key] = array();
if ($inst != null) {
$obj[$key][$inst_id] = $inst;
}
$inst = array();
$inst_id = $value;
} else {
if (is_array($inst)) {
$inst[$key] = $value;
} else {
$obj[$key] = $value;
}
}
}
}
if (is_array($obj)) {
if (is_array($inst))
$obj[self::OPTION_INSTANCE][$inst_id] = $inst;
if (!is_array($list))
$list = array();
$list[$obj_id] = $obj;
}
// print "extract_info returns:\n"; print_r($list);
return $list;
}
private function doGetInfo($op, $key, $value, $separator) {
$args = array();
if ($value)
$args[$key] = $value;
$result = $this->send_operation($op, $args);
$retcode = $this->extract($result, self::OPTION_RETCODE);
if ($retcode == null) $retcode = self::ERR_OK;
return array('retcode' => $retcode,
'list' => $this->extract_info($result, $separator));
}
/**
* Fetch information about one or all connectors.
*
* Returns an array containing the result code for key 'retcode',
* and an array with connector information for key 'list'.
*
* In this second array, the connector names are used as keys,
* and the values are arrays themselves.
*
* In the connector info arrays, the following keys are used:
*
* - {@link OPTION_TYPE}
1 for incoming, 2 for outgoing.
* - {@link OPTION_PROTOCOL}
Protocol number.
* - {@link OPTION_INSTANCES}
The value of the INSTANCES
* parameter in the server.cfg file.
* - {@link OPTION_INSTANCES_INUSE}
How many instances that are currently in use.
* - {@link OPTION_MAXINSTANCES}
The maximum number of used instances.
* - {@link OPTION_STATE}
Current connector state.
* - {@link OPTION_QUEUESIZE}
Number of messages in the outgoing queue.
* - {@link OPTION_QSTATS1}
Average message throughput for the past minute.
* - {@link OPTION_QSTATS5}
Average message throughput for the past five minutes.
* - {@link OPTION_QSTATS15}
Average message throughput for the past fifteen minutes.
*
* It also has a key {@link OPTION_INSTANCE}.
* The value for this key is a third array, containing information
* about each connector instance.
* The keys are the connector instance numbers, and the values an array
* with information about that instance.
* The keys used here are these:
*
* - {@link OPTION_STATE}
State for this instance.
* - {@link OPTION_REMOTEIP}
If connected, the remote IP address.
* - {@link OPTION_USERNAME}
If connected, the name of the logged in user.
*
* @return array
*/
function getRawConnectorInfo($connector = null) {
return $this->doGetInfo(MGP::OP_GETCONNECTORINFO,
MGP::OPTION_CONNECTOR, $connector, MGP::OPTION_CONNECTOR);
}
function stateName($state) {
$name = "";
$states = array("UNKNOWN", "CONNECTED", "IDLE", "ERROR", "DEAD");
if (($state & 15) < 5)
$name = $states[$state];
else if ($state & 8)
$name = "BOUND";
else
$name = "UNKNOWN";
if ($state & 16)
$name .= "*";
return $name;
}
/**
* Fetch information about one or all connectors.
*
* Returns an array containing the result code for key 'retcode',
* and an array with connector information for key 'list'.
*
* In this second array, the connector names are used as keys,
* and the values are arrays themselves.
*
* In the connector info arrays, the following keys are used:
*
* - {@link OPTION_TYPE}
'INCOMING' or 'OUTGOING'.
* - {@link OPTION_PROTOCOL}
Protocol name.
* - {@link OPTION_INSTANCES}
The value of the INSTANCES
* parameter in the server.cfg file.
* - {@link OPTION_INSTANCES_INUSE}
How many instances that are currently in use.
* - {@link OPTION_MAXINSTANCES}
The maximum number of used instances.
* - {@link OPTION_STATE}
Current connector state.
* - {@link OPTION_QUEUESIZE}
Number of messages in the outgoing queue.
* - {@link OPTION_QSTATS1}
Average message throughput for the past minute.
* - {@link OPTION_QSTATS5}
Average message throughput for the past five minutes.
* - {@link OPTION_QSTATS15}
Average message throughput for the past fifteen minutes.
*
* It also has a key {@link OPTION_INSTANCE}.
* The value for this key is a third array, containing information
* about each connector instance.
* The keys are the connector instance numbers, and the values an array
* with information about that instance.
* The keys used here are these:
*
* - {@link OPTION_STATE}
State for this instance.
* - {@link OPTION_REMOTEIP}
If connected, the remote IP address.
* - {@link OPTION_USERNAME}
If connected, the name of the logged in user.
*
* @return array
*/
function getConnectorInfo($connector = null) {
$types = array('', 'INCOMING', 'OUTGOING');
$protocols = array("UNK", "MGP", "HTTP", "SMTP", "UCP", "CIMD", "CIMD2", "TAP",
"SMPP", "OIS", "GSM", "EBE", "CDR", "FAX", "MM1", "EAIF",
"MM7", "PAP", "CMG", "DLL",);
$result = $this->getRawConnectorInfo($connector);
$retcode = $result['retcode'];
if ($retcode != self::ERR_OK)
return $result;
$list = &$result['list'];
$names = array_keys($list);
foreach ($names as $name) {
$options = &$list[$name];
$options[MGP::OPTION_TYPE] =
$types[$options[MGP::OPTION_TYPE]];
/* The protocol name list is from connectorutil.c. */
$options[MGP::OPTION_PROTOCOL] =
$protocols[$options[MGP::OPTION_PROTOCOL]];
$options[MGP::OPTION_STATE] = $this->stateName($options[MGP::OPTION_STATE]);
$instances = array_keys($options[MGP::OPTION_INSTANCE]);
foreach ($instances as $instance) {
$options2 = &$options[MGP::OPTION_INSTANCE][$instance];
$options2[MGP::OPTION_STATE] = $this->stateName($options2[MGP::OPTION_STATE]);
}
}
return $result;
}
/**
* Fetch information about one or all database profiles.
*
* Returns an array containing the result code for key 'retcode',
* and an array with connector information for key 'list'.
*
* In this second array, the connector names are used as keys,
* and the values are arrays themselves.
*
* In the connector info arrays, the following keys are used:
*
* - {@link OPTION_NAME}
The database profile name.
* - {@link OPTION_DBNAME}
The database name.
* - {@link OPTION_TYPE}
MYSQL for MySQL, PGSQL for PostgreSQL.
* - {@link OPTION_INSTANCES}
The value of the INSTANCES
* parameter in the server.cfg file.
*
* It also has a key {@link OPTION_INSTANCE}. The value for this key is
* a third hash table, containing information about each profile
* instance. The keys used here are these:
*
* - {@link OPTION_STATE}
State for this instance.
*
* @return array
*/
function getDatabaseInfo($db = null) {
return $this->doGetInfo(MGP::OP_GETDBINFO,
MGP::OPTION_DBNAME, $db, MGP::OPTION_NAME);
}
/**
* Fetch a phonebook list.
*
* If the parameter is absent, the list of available phonebooks is returned.
*
* The phonebook list is a hash table, with the phonebook id as the key,
* and a hash table with options as the value. In this second hash table,
* the following keys are used:
*
* - {@link OPTION_PBNAME}
The name of the phonebook. This can be any printable string.
* - {@link OPTION_PBTYPE}
0 for a user phonebook, 1 for a system phonebook.
*
* If the optional $pbid is given, the list of entries in that
* phonebook is returned.
*
* The phonebook entry list is a hash table, with the phonebook entry id as
* the key, and a hash table with options as the value. In this second hash
* table, the following keys are used:
*
* - {@link OPTION_COMPANY}
Company name.
* - {@link OPTION_NAME}
Personal name.
* - {@link OPTION_DESTADDR}
Phone number or email adress.
*
* @return array
*
*/
function pbList($pbid = null) {
$args = array();
if ($pbid != null)
$args[self::OPTION_PBID] = $pbid;
$result = $this->send_operation(MGP::OP_PBLIST, $args);
$retcode = $this->extract($result, self::OPTION_RETCODE);
return array('retcode' => $retcode,
'list' => $this->extract_info($result, MGP::OPTION_PBID));
}
/**
* Create a new phonebook.
*
* Returns the value of the {@link MGP::OPTION_RETCODE} parameter.
*
* @param integer $pbtype Set to 0 to create a user phonebook, and to 1 to
* create a system phonebook. Only a user with administration privileges
* can create a system phonebook.
* @param string name The name of the new phonebook.
*
* @return integer
*/
function pbCreate($pbtype, $name) {
$result = $this->send_operation(MGP::OP_PBCREATE,
array(self::OPTION_PBTYPE => $pbtype, self::OPTION_PBNAME => $name));
return $this->find($result, self::OPTION_RETCODE);
}
/**
* Delete a phonebook.
*
* Only a user with administration privileges can delete a system phonebook.
*
* Returns the value of the {@link MGP::OPTION_RETCODE} parameter.
*
* @param integer $pbid The phonebook id, gotten from the list
* returned by {@link pbList}.
* @return integer
*/
function pbDelete($pbid) {
$result = $this->send_operation(MGP::OP_PBDELETE,
array(self::OPTION_PBID => $pbid));
return $this->find($result, self::OPTION_RETCODE);
}
/**
* Add an entry to a phonebook.
*
* Only a user with administration privileges can add entries to a system
* phonebook.
*
* Returns the value of the {@link MGP::OPTION_RETCODE} parameter.
*
* @param integer $pbid The phonebook id, gotten from the list
* returned by {@link pbList}.
* @param string $company
* @param string $name
* @param string $destaddr
* @return integer
*/
function pbAdd($pbid, $company, $name, $destaddr) {
$result = $this->send_operation(MGP::OP_PBADD,
array(self::OPTION_PBID => $pbid, self::OPTION_COMPANY => $company,
self::OPTION_NAME => $name, self::OPTION_DESTADDR => $destaddr));
return $this->find($result, self::OPTION_RETCODE);
}
/**
* Update a phonebook entry.
*
* Only a user with administration privileges can update entries in a
* system phonebook.
*
* Returns the value of the {@link MGP::OPTION_RETCODE} parameter.
*
* @param integer $pbid The phonebook id, gotten from the list
* returned by {@link pbList}.
* @param string $company
* @param string $name
* @param string $destaddr
* @return integer
*/
function pbUpdate($pbid, $company = null, $name = null, $destaddr = null) {
if (($company == null) && ($name == null) && ($destaddr == null))
return self::ERR_OK;
$args = array(self::OPTION_PBID => $pbid);
if ($company != null)
$args[self::OPTION_COMPANY] = $company;
if ($name != null)
$args[self::OPTION_NAME] = $name;
if ($destaddr != null)
$args[self::OPTION_DESTADDR] = $destaddr;
$result = $this->send_operation(MGP::OP_PBUPDATE, $args);
return $this->find($result, self::OPTION_RETCODE);
}
/**
* Delete a phonebook entry.
*
* Only a user with administration privileges can delete entries in a
* system phonebook.
*
* Returns the value of the {@link MGP::OPTION_RETCODE} parameter.
*
* @param integer $pbid The phonebook id, gotten from the list
* returned by {@link pbList}.
* @return integer
*/
function pbRemove($pbid) {
$result = $this->send_operation(MGP::OP_PBREMOVE,
array(self::OPTION_PBID => $pbid));
return $this->find($result, self::OPTION_RETCODE);
}
/**
* Reload or refresh the EMG server.
*
* When the closeall parameter is true all connections will be closed and
* the current MGP connection will also be lost. This corresponds to
* running "emgd -refresh" from the command-line.
*
* When the closeall parameter is false connections will be kept. This
* corresponds to running "emgd -reload" from the command-line.
*
* Returns the value of the {@link MGP::OPTION_RETCODE} parameter.
*
* @return integer
*/
function reload($closeall = false) {
$result = $this->send_operation(
$closeall ? MGP::OP_REFRESH : MGP::OP_RELOAD);
return $this->find($result, self::OPTION_RETCODE);
}
}
?>