|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class Gateway |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource Gateway.php |
|
6
|
|
|
* @created 02.04.2016 |
|
7
|
|
|
* @package chillerlan\Threema |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2016 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace chillerlan\Threema; |
|
14
|
|
|
|
|
15
|
|
|
use chillerlan\Threema\Crypto\CryptoInterface; |
|
16
|
|
|
use Dotenv\Dotenv; |
|
17
|
|
|
use ReflectionClass; |
|
18
|
|
|
use ReflectionMethod; |
|
19
|
|
|
use stdClass; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* GatewayInterface methods |
|
23
|
|
|
* |
|
24
|
|
|
* @method checkCredits():int |
|
25
|
|
|
* @method checkCapabilities(string $threemaID):string |
|
26
|
|
|
* @method getIdByPhone(string $phoneno):string |
|
27
|
|
|
* @method getIdByPhoneHash(string $phonenoHash):string |
|
28
|
|
|
* @method getIdByEmail(string $email):string |
|
29
|
|
|
* @method getIdByEmailHash(string $emailHash):string |
|
30
|
|
|
* @method getPublicKey(string $threemaID):string |
|
31
|
|
|
* @method sendSimple(string $to, string $message) |
|
32
|
|
|
* @method sendE2E(string $threemaID, string $box, string $nonce) |
|
33
|
|
|
* @method upload(string $blob) |
|
34
|
|
|
* @method download(string $blobID) |
|
35
|
|
|
*/ |
|
36
|
|
|
class Gateway{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var \chillerlan\Threema\Crypto\CryptoInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $cryptoInterface; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var \chillerlan\Threema\GatewayInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $gatewayInterface; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var array[\ReflectionMethod] |
|
50
|
|
|
*/ |
|
51
|
|
|
private $gatewayInterfaceMap = []; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Gateway constructor. |
|
55
|
|
|
* |
|
56
|
|
|
* @param \chillerlan\Threema\Crypto\CryptoInterface $cryptoInterface |
|
57
|
|
|
* @param \chillerlan\Threema\GatewayOptions $gatewayOptions |
|
58
|
|
|
* |
|
59
|
|
|
* @throws \chillerlan\Threema\GatewayException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct(CryptoInterface $cryptoInterface, GatewayOptions $gatewayOptions){ |
|
62
|
|
|
$this->cryptoInterface = $cryptoInterface; |
|
63
|
|
|
|
|
64
|
|
|
$reflectionClass = new ReflectionClass(GatewayInterface::class); |
|
65
|
|
|
|
|
66
|
|
|
foreach($reflectionClass->getMethods() as $method){ |
|
67
|
|
|
$this->gatewayInterfaceMap[$method->name] = $method; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
(new Dotenv($gatewayOptions->configPath, $gatewayOptions->configFilename))->load(); |
|
71
|
|
|
|
|
72
|
|
|
$reflectionClass = new ReflectionClass($gatewayOptions->gatewayInterface); |
|
73
|
|
|
|
|
74
|
|
|
if(!$reflectionClass->implementsInterface(GatewayInterface::class)){ |
|
75
|
|
|
throw new GatewayException('"'.$gatewayOptions->gatewayInterface.'" does not implement GatewayInterface'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->gatewayInterface = $reflectionClass->newInstanceArgs([$cryptoInterface, $gatewayOptions]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $method |
|
83
|
|
|
* @param array $params |
|
84
|
|
|
* |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
* @throws \chillerlan\Threema\GatewayException |
|
87
|
|
|
*/ |
|
88
|
|
|
public function __call(string $method, array $params){ |
|
89
|
|
|
|
|
90
|
|
|
if(array_key_exists($method, $this->gatewayInterfaceMap)){ |
|
91
|
|
|
$reflectionMethod = new ReflectionMethod($this->gatewayInterface, $this->gatewayInterfaceMap[$method]->name); |
|
92
|
|
|
|
|
93
|
|
|
return $reflectionMethod->invokeArgs($this->gatewayInterface, $params); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
throw new GatewayException('method "'.$method.'" does not exist'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
####################### |
|
100
|
|
|
# convenience methods # |
|
101
|
|
|
####################### |
|
102
|
|
|
|
|
103
|
|
|
public function cryptoVersion():string{ |
|
104
|
|
|
return $this->cryptoInterface->version(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return \stdClass |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getKeypair():stdClass{ |
|
111
|
|
|
return $this->cryptoInterface->getKeypair(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $data |
|
116
|
|
|
* @param string $privateKey hex string |
|
117
|
|
|
* @param string $publicKey hex string |
|
118
|
|
|
* |
|
119
|
|
|
* @return \stdClass |
|
120
|
|
|
*/ |
|
121
|
|
|
public function encrypt(string $data, string $privateKey, string $publicKey):stdClass{ |
|
122
|
|
|
return $this->cryptoInterface->encrypt($data, $privateKey, $publicKey); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $box hex string |
|
127
|
|
|
* @param string $nonce hex string |
|
128
|
|
|
* @param string $privateKey hex string |
|
129
|
|
|
* @param string $publicKey hex string |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function decrypt(string $box, string $nonce, string $privateKey, string $publicKey):string{ |
|
134
|
|
|
return $this->cryptoInterface->decrypt($box, $nonce, $privateKey, $publicKey); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|