Skip to content

Commit 20f8410

Browse files
committed
Update to v1.1.3
downloaded: 2015-11-09
1 parent 2e787fe commit 20f8410

File tree

4 files changed

+70
-41
lines changed

4 files changed

+70
-41
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [Threema Gateway](http://gateway.threema.ch/) PHP SDK
22

3-
Version: 1.1.2
3+
Version: 1.1.3
44

55
Code analysers: [![Code Climate](https://codeclimate.com/github/rugk/threema-msgapi-sdk-php/badges/gpa.svg)](https://codeclimate.com/github/rugk/threema-msgapi-sdk-php) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/rugk/threema-msgapi-sdk-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/rugk/threema-msgapi-sdk-php/?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/b2d332ae-4100-42e0-abda-9cc96a79b18a/mini.png)](https://insight.sensiolabs.com/projects/b2d332ae-4100-42e0-abda-9cc96a79b18a) [![Codacy Badge](https://api.codacy.com/project/badge/grade/b90e43398be24c7fa417a43f02fbd31a)](https://www.codacy.com/app/c917250b/threema-msgapi-sdk-php)
66
All code analysers are configured to ignore `source/Salt/*` as this should not be part of the analyses.

source/Threema/MsgApi/Helpers/E2EHelper.php

+68-39
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Threema\MsgApi\Connection;
1212
use Threema\MsgApi\Messages\FileMessage;
1313
use Threema\MsgApi\Messages\ImageMessage;
14+
use Threema\MsgApi\Messages\ThreemaMessage;
1415
use Threema\MsgApi\Tools\CryptTool;
1516
use Threema\Core\Exception;
1617
use Threema\MsgApi\Tools\FileAnalysisTool;
@@ -178,6 +179,7 @@ public final function sendFileMessage($threemaId, $filePath, $thumbnailPath = nu
178179
return $this->connection->sendE2E($threemaId, $nonce, $fileMessage);
179180
}
180181

182+
181183
/**
182184
* Encrypt a message and download the files of the message to the $outputFolder
183185
*
@@ -186,10 +188,19 @@ public final function sendFileMessage($threemaId, $filePath, $thumbnailPath = nu
186188
* @param string $box box as binary string
187189
* @param string $nonce nonce as binary string
188190
* @param string|null $outputFolder folder for storing the files
189-
* @throws \Threema\Core\Exception
191+
* @param \Closure $downloadMessage
190192
* @return ReceiveMessageResult
193+
* @throws Exception
194+
* @throws \Threema\MsgApi\Exceptions\BadMessageException
195+
* @throws \Threema\MsgApi\Exceptions\DecryptionFailedException
196+
* @throws \Threema\MsgApi\Exceptions\UnsupportedMessageTypeException
191197
*/
192-
public final function receiveMessage($threemaId, $messageId, $box, $nonce, $outputFolder = null) {
198+
public final function receiveMessage($threemaId,
199+
$messageId,
200+
$box,
201+
$nonce,
202+
$outputFolder = null,
203+
\Closure $downloadMessage = null) {
193204

194205
if($outputFolder == null || strlen($outputFolder) == 0) {
195206
$outputFolder = '.';
@@ -216,51 +227,48 @@ public final function receiveMessage($threemaId, $messageId, $box, $nonce, $outp
216227
$receiveResult = new ReceiveMessageResult($messageId, $message);
217228

218229
if($message instanceof ImageMessage) {
219-
$result = $this->connection->downloadFile($message->getBlobId());
220-
if(null === $result || false === $result->isSuccess()) {
221-
throw new Exception('could not download the image with blob id '.$message->getBlobId());
222-
}
223-
224-
$image = $this->cryptTool->decryptImage(
225-
$result->getData(),
226-
hex2bin($receiverPublicKey->getPublicKey()),
227-
$this->privateKey,
228-
$message->getNonce()
229-
);
230+
$result = $this->downloadFile($message, $message->getBlobId(), $downloadMessage);
231+
if(null !== $result && true === $result->isSuccess()) {
232+
$image = $this->cryptTool->decryptImage(
233+
$result->getData(),
234+
hex2bin($receiverPublicKey->getPublicKey()),
235+
$this->privateKey,
236+
$message->getNonce()
237+
);
238+
239+
if (null === $image) {
240+
throw new Exception('decryption of image failed');
241+
}
242+
//save file
243+
$filePath = $outputFolder . '/' . $messageId . '.jpg';
244+
$f = fopen($filePath, 'w+');
245+
fwrite($f, $image);
246+
fclose($f);
230247

231-
if(null === $image) {
232-
throw new Exception('decryption of image failed');
248+
$receiveResult->addFile('image', $filePath);
233249
}
234-
//save file
235-
$filePath = $outputFolder.'/'.$messageId.'.jpg';
236-
$f = fopen($filePath, 'w+');
237-
fwrite($f, $image);
238-
fclose($f);
239-
240-
$receiveResult->addFile('image', $filePath);
241250
}
242251
else if($message instanceof FileMessage) {
243-
$result = $this->connection->downloadFile($message->getBlobId());
244-
if(null === $result || false === $result->isSuccess()) {
245-
throw new Exception('could not download the file with blob id '.$message->getBlobId());
246-
}
252+
$result = $this->downloadFile($message, $message->getBlobId(), $downloadMessage);
247253

248-
$file = $this->cryptTool->decryptFile(
249-
$result->getData(),
250-
hex2bin($message->getEncryptionKey()));
254+
if(null !== $result && true === $result->isSuccess()) {
255+
$file = $this->cryptTool->decryptFile(
256+
$result->getData(),
257+
hex2bin($message->getEncryptionKey()));
251258

252-
if(null === $file) {
253-
throw new Exception('file decryption failed');
254-
}
259+
if (null === $file) {
260+
throw new Exception('file decryption failed');
261+
}
255262

256-
//save file
257-
$filePath = $outputFolder.'/'.$messageId.'-'.$message->getFilename();
258-
file_put_contents($filePath, $file);
263+
//save file
264+
$filePath = $outputFolder . '/' . $messageId . '-' . $message->getFilename();
265+
file_put_contents($filePath, $file);
259266

260-
$receiveResult->addFile('file', $filePath);
267+
$receiveResult->addFile('file', $filePath);
268+
}
261269

262270
if(null !== $message->getThumbnailBlobId() && strlen($message->getThumbnailBlobId()) > 0) {
263-
$result = $this->connection->downloadFile($message->getThumbnailBlobId());
271+
$result = $this->downloadFile($message, $message->getThumbnailBlobId(), $downloadMessage);
264272
if(null !== $result && true === $result->isSuccess()) {
265273
$file = $this->cryptTool->decryptFileThumbnail(
266274
$result->getData(),
@@ -285,9 +293,9 @@ public final function receiveMessage($threemaId, $messageId, $box, $nonce, $outp
285293
* Fetch a public key and check the capability of the threemaId
286294
*
287295
* @param string $threemaId
288-
* @param callable $capabilityCheck
296+
* @param \Closure $capabilityCheck
289297
* @return string Public key as binary
290-
* @throws \Threema\Core\Exception
298+
* @throws Exception
291299
*/
292300
private final function fetchPublicKeyAndCheckCapability($threemaId, \Closure $capabilityCheck = null) {
293301
//fetch the public key
@@ -307,4 +315,25 @@ private final function fetchPublicKeyAndCheckCapability($threemaId, \Closure $ca
307315

308316
return hex2bin($receiverPublicKey->getPublicKey());
309317
}
318+
319+
/**
320+
* @param ThreemaMessage $message
321+
* @param string $blobId blob id as hex
322+
* @param \Closure|null $downloadMessage
323+
* @return null|\Threema\MsgApi\Commands\Results\DownloadFileResult
324+
* @throws Exception
325+
*/
326+
private final function downloadFile(ThreemaMessage $message, $blobId, \Closure $downloadMessage = null) {
327+
if(null === $downloadMessage
328+
|| true === $downloadMessage->__invoke($message, $blobId)) {
329+
//make a download
330+
$result = $this->connection->downloadFile($blobId);
331+
if(null === $result || false === $result->isSuccess()) {
332+
throw new Exception('could not download the file with blob id '.$blobId);
333+
}
334+
335+
return $result;
336+
}
337+
return null;
338+
}
310339
}

source/bootstrap.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}
2020
});
2121

22-
$sdkVersion = '1.1.2';
22+
$sdkVersion = '1.1.3';
2323
define('MSGAPI_SDK_VERSION', $sdkVersion);
2424
$cryptTool = Threema\MsgApi\Tools\CryptTool::getInstance();
2525

threema_msgapi.phar

921 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)