Completed
Push — master ( 1340cf...a41289 )
by rugk
02:46
created
source/Threema/MsgApi/Helpers/E2EHelper.php 1 patch
Spacing   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
 		$this->cryptTool = $cryptTool;
43 43
 		$this->privateKey = $privateKey;
44 44
 
45
-		if(null === $this->cryptTool) {
45
+		if (null === $this->cryptTool) {
46 46
 			$this->cryptTool = CryptTool::getInstance();
47 47
 		}
48 48
 	}
@@ -84,11 +84,11 @@  discard block
 block discarded – undo
84 84
 		//analyse the file
85 85
 		$fileAnalyzeResult = FileAnalysisTool::analyse($imagePath);
86 86
 
87
-		if(null === $fileAnalyzeResult) {
87
+		if (null === $fileAnalyzeResult) {
88 88
 			throw new Exception('could not analyze the file');
89 89
 		}
90 90
 
91
-		if(false === in_array($fileAnalyzeResult->getMimeType(), array(
91
+		if (false === in_array($fileAnalyzeResult->getMimeType(), array(
92 92
 				'image/jpg',
93 93
 				'image/jpeg',
94 94
 				'image/png' ))) {
@@ -102,9 +102,9 @@  discard block
 block discarded – undo
102 102
 
103 103
 		//encrypt the image file
104 104
 		$encryptionResult = $this->cryptTool->encryptImage(file_get_contents($imagePath), $this->privateKey, $receiverPublicKey);
105
-		$uploadResult =  $this->connection->uploadFile($encryptionResult->getData());
105
+		$uploadResult = $this->connection->uploadFile($encryptionResult->getData());
106 106
 
107
-		if($uploadResult === null || !$uploadResult->isSuccess()) {
107
+		if ($uploadResult === null || !$uploadResult->isSuccess()) {
108 108
 			throw new Exception('could not upload the image ('.$uploadResult->getErrorCode().' '.$uploadResult->getErrorMessage().') '.$uploadResult->getRawResponse());
109 109
 		}
110 110
 
@@ -134,7 +134,7 @@  discard block
 block discarded – undo
134 134
 		//analyse the file
135 135
 		$fileAnalyzeResult = FileAnalysisTool::analyse($filePath);
136 136
 
137
-		if(null === $fileAnalyzeResult) {
137
+		if (null === $fileAnalyzeResult) {
138 138
 			throw new Exception('could not analyze the file');
139 139
 		}
140 140
 
@@ -145,21 +145,21 @@  discard block
 block discarded – undo
145 145
 
146 146
 		//encrypt the main file
147 147
 		$encryptionResult = $this->cryptTool->encryptFile(file_get_contents($filePath));
148
-		$uploadResult =  $this->connection->uploadFile($encryptionResult->getData());
148
+		$uploadResult = $this->connection->uploadFile($encryptionResult->getData());
149 149
 
150
-		if($uploadResult === null || !$uploadResult->isSuccess()) {
150
+		if ($uploadResult === null || !$uploadResult->isSuccess()) {
151 151
 			throw new Exception('could not upload the file ('.$uploadResult->getErrorCode().' '.$uploadResult->getErrorMessage().') '.$uploadResult->getRawResponse());
152 152
 		}
153 153
 
154 154
 		$thumbnailUploadResult = null;
155 155
 
156 156
 		//encrypt the thumbnail file (if exists)
157
-		if(strlen($thumbnailPath) > 0 && true === file_exists($thumbnailPath)) {
157
+		if (strlen($thumbnailPath) > 0 && true === file_exists($thumbnailPath)) {
158 158
 			//encrypt the main file
159 159
 			$thumbnailEncryptionResult = $this->cryptTool->encryptFileThumbnail(file_get_contents($thumbnailPath), $encryptionResult->getKey());
160 160
 			$thumbnailUploadResult = $this->connection->uploadFile($thumbnailEncryptionResult->getData());
161 161
 
162
-			if($thumbnailUploadResult === null || !$thumbnailUploadResult->isSuccess()) {
162
+			if ($thumbnailUploadResult === null || !$thumbnailUploadResult->isSuccess()) {
163 163
 				throw new Exception('could not upload the thumbnail file ('.$thumbnailUploadResult->getErrorCode().' '.$thumbnailUploadResult->getErrorMessage().') '.$thumbnailUploadResult->getRawResponse());
164 164
 			}
165 165
 		}
@@ -205,14 +205,14 @@  discard block
 block discarded – undo
205 205
 										 $outputFolder = null,
206 206
 										 \Closure $downloadMessage = null) {
207 207
 
208
-		if($outputFolder === null || strlen($outputFolder) == 0) {
208
+		if ($outputFolder === null || strlen($outputFolder) == 0) {
209 209
 			$outputFolder = '.';
210 210
 		}
211 211
 
212 212
 		//fetch the public key
213 213
 		$receiverPublicKey = $this->connection->fetchPublicKey($threemaId);
214 214
 
215
-		if(null === $receiverPublicKey || !$receiverPublicKey->isSuccess()) {
215
+		if (null === $receiverPublicKey || !$receiverPublicKey->isSuccess()) {
216 216
 			throw new Exception('Invalid threema id');
217 217
 		}
218 218
 
@@ -223,15 +223,15 @@  discard block
 block discarded – undo
223 223
 			$nonce
224 224
 		);
225 225
 
226
-		if(null === $message || false === is_object($message)) {
226
+		if (null === $message || false === is_object($message)) {
227 227
 			throw new Exception('Could not encrypt box');
228 228
 		}
229 229
 
230 230
 		$receiveResult = new ReceiveMessageResult($messageId, $message);
231 231
 
232
-		if($message instanceof ImageMessage) {
232
+		if ($message instanceof ImageMessage) {
233 233
 			$result = $this->downloadFile($message, $message->getBlobId(), $downloadMessage);
234
-			if(null !== $result && true === $result->isSuccess()) {
234
+			if (null !== $result && true === $result->isSuccess()) {
235 235
 				$image = $this->cryptTool->decryptImage(
236 236
 					$result->getData(),
237 237
 					$this->cryptTool->hex2bin($receiverPublicKey->getPublicKey()),
@@ -243,7 +243,7 @@  discard block
 block discarded – undo
243 243
 					throw new Exception('decryption of image failed');
244 244
 				}
245 245
 				//save file
246
-				$filePath = $outputFolder . '/' . $messageId . '.jpg';
246
+				$filePath = $outputFolder.'/'.$messageId.'.jpg';
247 247
 				$f = fopen($filePath, 'w+');
248 248
 				fwrite($f, $image);
249 249
 				fclose($f);
@@ -251,10 +251,10 @@  discard block
 block discarded – undo
251 251
 				$receiveResult->addFile('image', $filePath);
252 252
 			}
253 253
 		}
254
-		else if($message instanceof FileMessage) {
254
+		else if ($message instanceof FileMessage) {
255 255
 			$result = $this->downloadFile($message, $message->getBlobId(), $downloadMessage);
256 256
 
257
-			if(null !== $result && true === $result->isSuccess()) {
257
+			if (null !== $result && true === $result->isSuccess()) {
258 258
 				$file = $this->cryptTool->decryptFile(
259 259
 					$result->getData(),
260 260
 					hex2bin($message->getEncryptionKey()));
@@ -264,20 +264,20 @@  discard block
 block discarded – undo
264 264
 				}
265 265
 
266 266
 				//save file
267
-				$filePath = $outputFolder . '/' . $messageId . '-' . $message->getFilename();
267
+				$filePath = $outputFolder.'/'.$messageId.'-'.$message->getFilename();
268 268
 				file_put_contents($filePath, $file);
269 269
 
270 270
 				$receiveResult->addFile('file', $filePath);
271 271
 			}
272 272
 
273
-			if(null !== $message->getThumbnailBlobId() && strlen($message->getThumbnailBlobId()) > 0) {
273
+			if (null !== $message->getThumbnailBlobId() && strlen($message->getThumbnailBlobId()) > 0) {
274 274
 				$result = $this->downloadFile($message, $message->getThumbnailBlobId(), $downloadMessage);
275
-				if(null !== $result && true === $result->isSuccess()) {
275
+				if (null !== $result && true === $result->isSuccess()) {
276 276
 					$file = $this->cryptTool->decryptFileThumbnail(
277 277
 						$result->getData(),
278 278
 						$this->cryptTool->hex2bin($message->getEncryptionKey()));
279 279
 
280
-					if(null === $file) {
280
+					if (null === $file) {
281 281
 						throw new Exception('thumbnail decryption failed');
282 282
 					}
283 283
 					//save file
@@ -304,14 +304,14 @@  discard block
 block discarded – undo
304 304
 		//fetch the public key
305 305
 		$receiverPublicKey = $this->connection->fetchPublicKey($threemaId);
306 306
 
307
-		if(null === $receiverPublicKey || !$receiverPublicKey->isSuccess()) {
307
+		if (null === $receiverPublicKey || !$receiverPublicKey->isSuccess()) {
308 308
 			throw new Exception('Invalid threema id');
309 309
 		}
310 310
 
311
-		if(null !== $capabilityCheck) {
311
+		if (null !== $capabilityCheck) {
312 312
 			//check capability
313 313
 			$capability = $this->connection->keyCapability($threemaId);
314
-			if(null === $capability || false === $capabilityCheck->__invoke($capability)) {
314
+			if (null === $capability || false === $capabilityCheck->__invoke($capability)) {
315 315
 				throw new Exception('threema id does not have the capability');
316 316
 			}
317 317
 		}
@@ -327,11 +327,11 @@  discard block
 block discarded – undo
327 327
 	 * @throws Exception
328 328
 	 */
329 329
 	private final function downloadFile(ThreemaMessage $message, $blobId, \Closure $downloadMessage = null) {
330
-		if(null === $downloadMessage
330
+		if (null === $downloadMessage
331 331
 			|| true === $downloadMessage->__invoke($message, $blobId)) {
332 332
 			//make a download
333 333
 			$result = $this->connection->downloadFile($blobId);
334
-			if(null === $result || false === $result->isSuccess()) {
334
+			if (null === $result || false === $result->isSuccess()) {
335 335
 				throw new Exception('could not download the file with blob id '.$blobId);
336 336
 			}
337 337
 
Please login to merge, or discard this patch.