1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Works with the Threema Gateway server. Currently only looks up/fetches data |
4
|
|
|
* from it. |
5
|
|
|
* |
6
|
|
|
* @package ThreemaGateway |
7
|
|
|
* @author rugk |
8
|
|
|
* @copyright Copyright (c) 2015-2016 rugk |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
class ThreemaGateway_Handler_Action_GatewayServer extends ThreemaGateway_Handler_Action_Abstract |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Returns the Threema ID associated to a phone number. |
16
|
|
|
* |
17
|
|
|
* In case of an error this does not throw an exception, but just returns false. |
18
|
|
|
* |
19
|
|
|
* @param string $phone Phone number (the best way is in international E.164 |
20
|
|
|
* format without `+`, e.g. 41791234567) |
21
|
|
|
* @throws XenForo_Exception |
22
|
|
|
* @return string|false |
23
|
|
|
*/ |
24
|
|
|
public function lookupPhone($phone) |
25
|
|
|
{ |
26
|
|
|
// check permission |
27
|
|
|
if (!$this->permissions->hasPermission('lookup')) { |
28
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_permission_error')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
//adjust phone number |
32
|
|
|
$phone = preg_replace('/\s+/', '', $phone); //strip whitespace |
33
|
|
|
if (substr($phone, 0, 1) == '+') { |
34
|
|
|
//remove leading + |
35
|
|
|
$phone = substr($phone, 1); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** @var array $threemaId Return value */ |
39
|
|
|
$threemaId = false; |
40
|
|
|
|
41
|
|
|
/** @var Threema\MsgApi\Commands\Results\LookupIdResult $result */ |
42
|
|
|
$result = $this->getConnector()->keyLookupByPhoneNumber($phone); |
43
|
|
|
if ($result->isSuccess()) { |
44
|
|
|
$threemaId = $result->getId(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $threemaId; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns the Threema ID associated to a mail address. |
52
|
|
|
* |
53
|
|
|
* In case of an error this does not throw an exception, but just returns false. |
54
|
|
|
* |
55
|
|
|
* @param string $mail E-mail |
56
|
|
|
* @throws XenForo_Exception |
57
|
|
|
* @return string|false |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function lookupMail($mail) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
// check permission |
62
|
|
|
if (!$this->permissions->hasPermission('lookup')) { |
63
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_permission_error')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** @var array Return value $threemaId */ |
67
|
|
|
$threemaId = false; |
68
|
|
|
|
69
|
|
|
/** @var Threema\MsgApi\Commands\Results\LookupIdResult $result */ |
70
|
|
|
$result = $this->getConnector()->keyLookupByEmail($mail); |
71
|
|
|
if ($result->isSuccess()) { |
72
|
|
|
$threemaId = $result->getId(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $threemaId; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the capabilities of a Threema ID. |
80
|
|
|
* |
81
|
|
|
* In case of an error this does not throw an exception, but just returns false. |
82
|
|
|
* |
83
|
|
|
* @param string $threemaId |
84
|
|
|
* @return Threema\MsgApi\Commands\Results\CapabilityResult |
|
|
|
|
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function getCapabilities($threemaId) |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
// check permission |
89
|
|
|
if (!$this->permissions->hasPermission('lookup')) { |
90
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_permission_error')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** @var array $return Return value */ |
94
|
|
|
$return = false; |
95
|
|
|
|
96
|
|
|
/** @var Threema\MsgApi\Commands\Results\LookupIdResult $result */ |
97
|
|
|
$result = $this->getConnector()->keyCapability($threemaId); |
98
|
|
|
if ($result->isSuccess()) { |
99
|
|
|
$return = $result; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns the remaining credits of the Gateway account. |
107
|
|
|
* |
108
|
|
|
* @throws XenForo_Exception |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
|
|
public function getCredits() |
112
|
|
|
{ |
113
|
|
|
// check permission |
114
|
|
|
if (!$this->permissions->hasPermission('credits')) { |
115
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_permission_error')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** @var Threema\MsgApi\Commands\Results\CreditsResult $result */ |
119
|
|
|
$result = $this->getConnector()->credits(); |
120
|
|
|
|
121
|
|
View Code Duplication |
if ($result->isSuccess()) { |
|
|
|
|
122
|
|
|
return $result->getCredits(); |
123
|
|
|
} else { |
124
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_getting_credits_failed') . ' ' . $result->getErrorMessage()); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Fetches the public key of an ID from the Threema server. |
130
|
|
|
* |
131
|
|
|
* @param string $threemaId The id whose public key should be fetched |
132
|
|
|
* |
133
|
|
|
* @throws XenForo_Exception |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function fetchPublicKey($threemaId) |
137
|
|
|
{ |
138
|
|
|
// check permission |
139
|
|
|
if (!$this->permissions->hasPermission('fetch')) { |
140
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_permission_error')); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// prevent any lookup if the gateway ID key is requested |
144
|
|
|
if ($threemaId == $this->settings->getId()) { |
145
|
|
|
return $this->settings->getOwnPublicKey(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** @var Threema\MsgApi\Commands\Results\FetchPublicKeyResult $result */ |
149
|
|
|
$result = $this->getConnector()->fetchPublicKey($threemaId); |
150
|
|
View Code Duplication |
if ($result->isSuccess()) { |
|
|
|
|
151
|
|
|
return $result->getPublicKey(); |
152
|
|
|
} else { |
153
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_fetching_publickey_failed') . ' ' . $result->getErrorMessage()); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.