Skip to content

Commit

Permalink
bug fix LocationMessage.
Browse files Browse the repository at this point in the history
  • Loading branch information
pavarnos committed Sep 24, 2018
1 parent 9159946 commit 8bd2b8a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/Threema/MsgApi/Encryptor/AbstractEncryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ final public function decryptMessage($box, $recipientPrivateKey, $senderPublicKe
return new ImageMessage($this->bin2hex($blobId), (int) $this->bin2hex($length), $this->bin2hex($nonce));

case FileMessage::TYPE_CODE:
/* Image Message */
/* File Message */
$values = json_decode(substr($data, 1), true);
if (empty($values)) {
throw new BadMessageException('json badly formatted');
Expand All @@ -215,11 +215,13 @@ final public function decryptMessage($box, $recipientPrivateKey, $senderPublicKe
$values['s']);

case LocationMessage::TYPE_CODE:
$points = explode(',',$this->bin2hex($data));
if (count($points) !== 3) {
$lines = explode("\n", substr($data,1));
$points = explode(',',$lines[0] ?? '');
if (count($points) < 2) {
throw new BadMessageException('invalid latitude and longitude');
}
return new LocationMessage($points[0], $points[1]);
array_shift($lines); // to get the address parts
return new LocationMessage($points[0], $points[1], intval($points[3] ?? 0), $lines);

default:
throw new UnsupportedMessageTypeException();
Expand Down
47 changes: 43 additions & 4 deletions src/Threema/MsgApi/Message/LocationMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,43 @@

namespace Threema\MsgApi\Message;

use Threema\MsgApi\Exceptions\BadMessageException;

/**
* This is not documented on the Threema Gateway api page.
*
* The format is
* <latitude>,<longitude>[,<accuracy>][\nPOI name][\nPOI address]
* latitude/longitude: decimal degrees
* accuracy: meters (optional)
* POI address: to embed newlines into the POI address, replace them with "\n" (optional)
*
* (from personal email communication with Threema Support 17 Aug 2018)
*
* Note: we use strings rather than floats here to ensure there are no rounding issues with the original values
*/
class LocationMessage extends AbstractMessage
{
const TYPE_CODE = 0x16;
const TYPE_CODE = 0x10;

/** @var string */
/** @var string decimal degrees */
private $latitude;

/** @var string */
/** @var string decimal degrees */
private $longitude;

public function __construct(string $latitude, string $longitude)
/** @var int accuracy, metres, optional */
private $accuracy = 0;

/** @var string[] lines of the address */
private $address = [];

public function __construct(string $latitude, string $longitude, int $accuracy = 0, array $address = [])
{
$this->latitude = $latitude;
$this->longitude = $longitude;
$this->accuracy = $accuracy;
$this->address = $address;
}

/**
Expand All @@ -40,6 +63,22 @@ public function getLongitude(): string
return $this->longitude;
}

/**
* @return int
*/
public function getAccuracy(): int
{
return $this->accuracy;
}

/**
* @return string[]
*/
public function getAddress(): array
{
return $this->address;
}

/**
* @return string
*/
Expand Down

0 comments on commit 8bd2b8a

Please sign in to comment.