From ee622028a78986a28088959318df02caa762d1da Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 6 Aug 2015 14:44:41 +0200 Subject: [PATCH 1/4] Can now handle 0-length messages. --- smppclient.class.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/smppclient.class.php b/smppclient.class.php index 7732471..5fae17a 100644 --- a/smppclient.class.php +++ b/smppclient.class.php @@ -518,7 +518,11 @@ protected function parseSMS(SmppPdu $pdu) $dataCoding = next($ar); next($ar); // sm_default_msg_id $sm_length = next($ar); - $message = $this->getString($ar,$sm_length); + if ($sm_length > 0) { // Most var-length values in smpp are null-terminated. + $message = $this->getString($ar, $sm_length); // for some reason, message is not, so it needs an if here. + } else { // probably $this->getString() doesnt work fully as expected + $message = null; // but this is a quick walkaround + } // Check for optional params, and parse them if (current($ar) !== false) { From b2398526994697bcd7ae6092fae31053af4fded8 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 6 Aug 2015 14:47:20 +0200 Subject: [PATCH 2/4] Better problem description --- smppclient.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/smppclient.class.php b/smppclient.class.php index 5fae17a..428684f 100644 --- a/smppclient.class.php +++ b/smppclient.class.php @@ -518,10 +518,10 @@ protected function parseSMS(SmppPdu $pdu) $dataCoding = next($ar); next($ar); // sm_default_msg_id $sm_length = next($ar); - if ($sm_length > 0) { // Most var-length values in smpp are null-terminated. - $message = $this->getString($ar, $sm_length); // for some reason, message is not, so it needs an if here. - } else { // probably $this->getString() doesnt work fully as expected - $message = null; // but this is a quick walkaround + if ($sm_length > 0) { // getString is doing "next()" on $ar + $message = $this->getString($ar, $sm_length); // but it shouldn't for 0-length,not-null-terminated value + } else { + $message = null; } // Check for optional params, and parse them From 815f3a5492d7f82ece218454694c0c417ab94b8d Mon Sep 17 00:00:00 2001 From: andrzejo Date: Mon, 31 Aug 2015 13:49:48 +0200 Subject: [PATCH 3/4] delivery receipt parser - regexp refactor --- smppclient.class.php | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/smppclient.class.php b/smppclient.class.php index 428684f..42968df 100644 --- a/smppclient.class.php +++ b/smppclient.class.php @@ -1066,18 +1066,37 @@ class SmppDeliveryReceipt extends SmppSms */ public function parseDeliveryReceipt() { - $numMatches = preg_match('/^id:([^ ]+) sub:(\d{1,3}) dlvrd:(\d{3}) submit date:(\d{10,12}) done date:(\d{10,12}) stat:([A-Z ]{7}) err:(\d{2,3}) text:(.*)$/si', $this->message, $matches); + $id = "id:(?[^ ]+)"; + $sub = "sub:(?\d{1,3})"; + $dlvrd = "dlvrd:(?\d{3})"; + $submitDate = "submit date:(?\d{10,12})"; + $doneDate = "done date:(?\d{10,12})"; + $stat = "stat:(?[A-Z ]{7})"; + $err = "err:(?\d{2,3})"; + $text = "text:(?.*)"; + $numMatches = preg_match("/^$id $sub $dlvrd $submitDate $doneDate $stat $err $text$/si", $this->message, $matches); if ($numMatches == 0) { throw new InvalidArgumentException('Could not parse delivery receipt: '.$this->message."\n".bin2hex($this->body)); } - list($matched, $this->id, $this->sub, $this->dlvrd, $this->submitDate, $this->doneDate, $this->stat, $this->err, $this->text) = $matches; - + $this->id = $this->getArrayValue($matches, 'id'); + $this->sub = $this->getArrayValue($matches, 'sub'); + $this->dlvrd = $this->getArrayValue($matches, 'dlvrd'); + $this->submitDate = $this->getArrayValue($matches, 'submitDate'); + $this->doneDate = $this->getArrayValue($matches, 'doneDate'); + $this->stat = $this->getArrayValue($matches, 'stat'); + $this->err = $this->getArrayValue($matches, 'err'); + $this->text = $this->getArrayValue($matches, 'text'); + // Convert dates $dp = str_split($this->submitDate,2); $this->submitDate = gmmktime($dp[3],$dp[4],isset($dp[5]) ? $dp[5] : 0,$dp[1],$dp[2],$dp[0]); $dp = str_split($this->doneDate,2); $this->doneDate = gmmktime($dp[3],$dp[4],isset($dp[5]) ? $dp[5] : 0,$dp[1],$dp[2],$dp[0]); } + + private function getArrayValue($array, $key, $default = null){ + return isset($array[$key]) ? $array[$key] : $default; + } } /** From 3dfe9da95ad3d7251bad9a4ae12af8cdcdcb806a Mon Sep 17 00:00:00 2001 From: andrzejo Date: Mon, 31 Aug 2015 14:11:20 +0200 Subject: [PATCH 4/4] delivery receipt parser - optional 'err:' field --- smppclient.class.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/smppclient.class.php b/smppclient.class.php index 42968df..1b23dcd 100644 --- a/smppclient.class.php +++ b/smppclient.class.php @@ -1066,17 +1066,17 @@ class SmppDeliveryReceipt extends SmppSms */ public function parseDeliveryReceipt() { - $id = "id:(?[^ ]+)"; - $sub = "sub:(?\d{1,3})"; - $dlvrd = "dlvrd:(?\d{3})"; - $submitDate = "submit date:(?\d{10,12})"; - $doneDate = "done date:(?\d{10,12})"; - $stat = "stat:(?[A-Z ]{7})"; - $err = "err:(?\d{2,3})"; + $id = "id:(?[^ ]+) "; + $sub = "sub:(?\d{1,3}) "; + $dlvrd = "dlvrd:(?\d{3}) "; + $submitDate = "submit date:(?\d{10,12}) "; + $doneDate = "done date:(?\d{10,12}) "; + $stat = "stat:(?[A-Z ]{7}) "; + $err = "(err:(?\d{2,3}) )?"; $text = "text:(?.*)"; - $numMatches = preg_match("/^$id $sub $dlvrd $submitDate $doneDate $stat $err $text$/si", $this->message, $matches); + $numMatches = preg_match("/^{$id}{$sub}{$dlvrd}{$submitDate}{$doneDate}{$stat}{$err}{$text}$/si", $this->message, $matches); if ($numMatches == 0) { - throw new InvalidArgumentException('Could not parse delivery receipt: '.$this->message."\n".bin2hex($this->body)); + throw new InvalidArgumentException('Could not parse delivery receipt: '.$this->message."\n".bin2hex($this->body)); } $this->id = $this->getArrayValue($matches, 'id'); $this->sub = $this->getArrayValue($matches, 'sub');