Skip to content

Commit

Permalink
Bug/missing square bracket escape gsm7 (#367)
Browse files Browse the repository at this point in the history
* Fix regex

* Fixed regex by using character encoding

* Composer didn't like php-http discovery
  • Loading branch information
SecondeJK authored Feb 10, 2023
1 parent 84ab3b6 commit c4e6688
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist"
"preferred-install": "dist",
"allow-plugins": {
"php-http/discovery": true
}
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 4 additions & 3 deletions src/SMS/Message/SMS.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class SMS extends OutboundMessage
{
public const GSM_7_PATTERN = '/\A[\n\f\r !\"\#$%&\'()*+,-.\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\\^_abcdefghijklmnopqrstuvwxyz{\|}~ ¡£¤¥§¿ÄÅÆÇÉÑÖØÜßàäåæèéìñòöøùüΓΔΘΛΞΠΣΦΨΩ€]*\z/m';
public const GSM_7_CHARSET = "\n\f\r !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~ ¡£¤¥§¿ÄÅÆÇÉÑÖØÜßàäåæèéìñòöøùüΓΔΘΛΞΠΣΦΨΩ€";

protected ?string $contentId;

Expand All @@ -32,7 +32,8 @@ public function __construct(string $to, string $from, protected string $message,

public static function isGsm7(string $message): bool
{
return (bool)preg_match(self::GSM_7_PATTERN, $message);
$fullPattern = "/\A[" . preg_quote(self::GSM_7_CHARSET, '/') . "]*\z/";
return (bool)preg_match($fullPattern, $message);
}

public function getContentId(): string
Expand Down Expand Up @@ -68,7 +69,7 @@ public function getErrorMessage(): ?string

if ($this->getType() === 'text' && ! self::isGsm7($this->getMessage())) {
$this->setErrorMessage("You are sending a message as `text` when contains unicode only
characters. This could result in encoding problems with the target device or increased billing - See
characters. This could result in encoding problems with the target device - See
https://developer.vonage.com/messaging/sms for details, or email [email protected] if you have any
questions.");
}
Expand Down
2 changes: 1 addition & 1 deletion test/SMS/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public function testThrowsWarningWhenSendingUnicodeAsText(): void

$this->expectWarning();
$this->expectErrorMessage("You are sending a message as `text` when contains unicode only
characters. This could result in encoding problems with the target device or increased billing - See
characters. This could result in encoding problems with the target device - See
https://developer.vonage.com/messaging/sms for details, or email [email protected] if you have any
questions.");

Expand Down
1 change: 1 addition & 0 deletions test/SMS/Message/SMSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public function unicodeStringDataProvider(): array
{
return [
['this is a text', true],
['This is a text with some tasty characters: [test]', true],
['This is also a GSM7 text', true],
['This is a Çotcha', true],
['This is also a çotcha', true],
Expand Down

0 comments on commit c4e6688

Please sign in to comment.