|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Threema GmbH |
|
4
|
|
|
* @copyright Copyright (c) 2015-2016 Threema GmbH |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Threema\MsgApi\PublicKeyStores; |
|
8
|
|
|
|
|
9
|
|
|
use Threema\Core\Exception; |
|
10
|
|
|
use Threema\MsgApi\PublicKeyStore; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Store the PublicKeys in a ascii file |
|
14
|
|
|
* |
|
15
|
|
|
* @package Threema\MsgApi\PublicKeyStores |
|
16
|
|
|
*/ |
|
17
|
|
|
class File extends PublicKeyStore { |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private $file; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $file Valid, read and writable file |
|
25
|
|
|
* @throws Exception if the file does not exist or not writable |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct($file) { |
|
28
|
|
|
if(false === is_writable($file)) { |
|
29
|
|
|
throw new Exception('file '.$file.' does not exist or is not writable'); |
|
30
|
|
|
} |
|
31
|
|
|
$this->file = $file; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* return null if the public key not found in the store |
|
36
|
|
|
* @param string $threemaId |
|
37
|
|
|
* @return null|string |
|
38
|
|
|
* @throws Exception |
|
39
|
|
|
*/ |
|
40
|
|
|
function findPublicKey($threemaId) { |
|
|
|
|
|
|
41
|
|
|
$storeHandle = fopen($this->file, 'r'); |
|
42
|
|
|
if(false === $storeHandle) { |
|
43
|
|
|
throw new Exception('could not open file '.$this->file); |
|
44
|
|
|
} |
|
45
|
|
|
else { |
|
46
|
|
|
$threemaId = strtoupper($threemaId); |
|
47
|
|
|
$publicKey = null; |
|
48
|
|
|
while (!feof($storeHandle)) { |
|
49
|
|
|
$buffer = fgets($storeHandle, 4096); |
|
50
|
|
|
if(substr($buffer, 0, 8) == $threemaId) { |
|
51
|
|
|
$publicKey = str_replace("\n", '', substr($buffer, 8)); |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
// Process buffer here.. |
|
55
|
|
|
} |
|
56
|
|
|
fclose($storeHandle); |
|
57
|
|
|
return $publicKey; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* save a public key |
|
63
|
|
|
* @param string $threemaId |
|
64
|
|
|
* @param string $publicKey |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
function savePublicKey($threemaId, $publicKey) { |
|
|
|
|
|
|
68
|
|
|
return file_put_contents($this->file, $threemaId.$publicKey."\n", FILE_APPEND) !== false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Initialize a new File Public Key Store |
|
73
|
|
|
* @param string $path the file will be created if it does not exist |
|
74
|
|
|
* @return File |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function create($path) { |
|
77
|
|
|
if(false === file_exists($path)) { |
|
78
|
|
|
//touch |
|
79
|
|
|
touch($path); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return new File($path); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.