|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Responsible for sending messages in different ways. |
|
4
|
|
|
* |
|
5
|
|
|
* @package ThreemaGateway |
|
6
|
|
|
* @author rugk |
|
7
|
|
|
* @copyright Copyright (c) 2015-2016 rugk |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
class ThreemaGateway_Handler_Action_Sender extends ThreemaGateway_Handler_Action_Abstract |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var bool whether the permissions are already checked |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $isPermissionChecked = false; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Send a message without end-to-end encryption. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $threemaId |
|
22
|
|
|
* @param string $message |
|
23
|
|
|
* |
|
24
|
|
|
* @throws XenForo_Exception |
|
25
|
|
|
* @return int The message ID |
|
26
|
|
|
*/ |
|
27
|
|
|
public function sendSimple($threemaId, $message) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->initiate(); |
|
30
|
|
|
|
|
31
|
|
|
/** @var Threema\MsgApi\Receiver $receiver */ |
|
32
|
|
|
$receiver = $this->getThreemaReceiver($threemaId); |
|
33
|
|
|
|
|
34
|
|
|
/** @var Threema\MsgApi\Commands\Results\SendSimpleResult $result */ |
|
35
|
|
|
$result = $this->getConnector()->sendSimple($receiver, $message); |
|
36
|
|
|
|
|
37
|
|
|
return $this->evaluateResult($result); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Send a message to a Threema ID. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $threemaId The id where the message should be send to |
|
44
|
|
|
* @param string $message The message to send (max 3500 characters) |
|
45
|
|
|
* |
|
46
|
|
|
* @throws XenForo_Exception |
|
47
|
|
|
* @return int The message ID |
|
48
|
|
|
*/ |
|
49
|
|
|
public function sendE2EText($threemaId, $message) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->initiate(); |
|
52
|
|
|
|
|
53
|
|
|
try { |
|
54
|
|
|
/** @var Threema\MsgApi\Commands\Results\SendE2EResult $result */ |
|
55
|
|
|
$result = $this->getE2EHelper()->sendTextMessage($threemaId, $message); |
|
56
|
|
|
} catch (Exception $e) { |
|
57
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_sending_failed') . ' ' . $e->getMessage()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this->evaluateResult($result); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Sends a message to a Threema ID in the preferred mode. |
|
65
|
|
|
* |
|
66
|
|
|
* Attention: You actually may want to distinguish whether a message can be/ |
|
67
|
|
|
* has been sent in an E2E way or not as the features and of course the |
|
68
|
|
|
* security level differ. |
|
69
|
|
|
* Therefore only use this method if you do not care how your message is |
|
70
|
|
|
* transported or you already evaluated in which mode the message will be |
|
71
|
|
|
* sent. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $threemaId The id where the message should be send to |
|
74
|
|
|
* @param string $message The message to send (max 3500 characters) |
|
75
|
|
|
* |
|
76
|
|
|
* @throws XenForo_Exception |
|
77
|
|
|
* @return int The message ID |
|
78
|
|
|
*/ |
|
79
|
|
|
public function sendAuto($threemaId, $message) |
|
80
|
|
|
{ |
|
81
|
|
|
// send message |
|
82
|
|
|
if ($this->settings->isEndToEnd()) { |
|
83
|
|
|
return $this->sendE2EText($threemaId, $message); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->sendSimple($threemaId, $message); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Skips the permission check. (not recommend!). |
|
91
|
|
|
*/ |
|
92
|
|
|
public function skipPermissionCheck() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->isPermissionChecked = true; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Checks whether the user is allowed to send messages. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $threemaId |
|
101
|
|
|
* |
|
102
|
|
|
* @throws XenForo_Exception |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function initiate(&$threemaId = null) |
|
105
|
|
|
{ |
|
106
|
|
|
if (!$this->isPermissionChecked) { |
|
107
|
|
|
// general permission |
|
108
|
|
|
if (!$this->permissions->hasPermission('send')) { |
|
109
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_permission_error')); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// rate-limit |
|
113
|
|
|
// NOTE: As this check is skipped with the permission cache it may |
|
114
|
|
|
// happen that the limit is exceeded when more than one message is |
|
115
|
|
|
// sent per request, which is unlikely AFAIK. |
|
116
|
|
|
if ($this->permissions->isLimited('send')) { |
|
117
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_account_locked_due_to_high_number_of_sent_messages')); |
|
118
|
|
|
}; |
|
119
|
|
|
|
|
120
|
|
|
$this->isPermissionChecked = true; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
if ($threemaId) { |
|
|
|
|
|
|
124
|
|
|
$threemaId = strtoupper($threemaId); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Evaluate the result of sending the message. |
|
130
|
|
|
* |
|
131
|
|
|
* @param Threema\MsgApi\Commands\Results\SendE2EResult $result |
|
132
|
|
|
* |
|
133
|
|
|
* @throws XenForo_Exception |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function evaluateResult($result) |
|
136
|
|
|
{ |
|
137
|
|
|
if ($result->isSuccess()) { |
|
138
|
|
|
// log that the user has sent a message |
|
139
|
|
|
$this->permissions->logAction('send'); |
|
140
|
|
|
|
|
141
|
|
|
return $result->getMessageId(); |
|
142
|
|
|
} else { |
|
143
|
|
|
throw new XenForo_Exception(new XenForo_Phrase('threemagw_sending_failed') . ' ' . $result->getErrorMessage()); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: