|
1
|
|
|
<?php |
|
2
|
|
|
/* For licensing terms, see /license.txt */ |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Class for manage the messages web service |
|
6
|
|
|
* @author Angel Fernando Quiroz Campos <[email protected]> |
|
7
|
|
|
* @package chamilo.webservices.messages |
|
8
|
|
|
*/ |
|
9
|
|
|
class MessagesWebService extends WebService |
|
10
|
|
|
{ |
|
11
|
|
|
const SERVICE_NAME = 'MsgREST'; |
|
12
|
|
|
const EXTRA_FIELD_GCM_REGISTRATION = 'gcm_registration_id'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Generate the api key for a user |
|
16
|
|
|
* @param int $userId The user id |
|
17
|
|
|
* @return string The api key |
|
18
|
|
|
*/ |
|
19
|
|
|
public function generateApiKey($userId) |
|
20
|
|
|
{ |
|
21
|
|
|
$apiKey = UserManager::get_api_keys($userId, self::SERVICE_NAME); |
|
22
|
|
|
|
|
23
|
|
|
if (empty($apiKey)) { |
|
24
|
|
|
UserManager::add_api_key($userId, self::SERVICE_NAME); |
|
25
|
|
|
|
|
26
|
|
|
$apiKey = UserManager::get_api_keys($userId, self::SERVICE_NAME); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return current($apiKey); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get the user api key |
|
34
|
|
|
* @param string $username The user name |
|
35
|
|
|
* @return string The api key |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getApiKey($username) |
|
38
|
|
|
{ |
|
39
|
|
|
$userInfo = api_get_user_info_from_username($username); |
|
40
|
|
|
$userId = $userInfo['user_id']; |
|
41
|
|
|
|
|
42
|
|
|
if ($this->apiKey !== null) { |
|
43
|
|
|
return $this->apiKey; |
|
44
|
|
|
} else { |
|
45
|
|
|
$this->apiKey = $this->generateApiKey($userId); |
|
46
|
|
|
|
|
47
|
|
|
return $this->apiKey; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Check if the api is valid for a user |
|
53
|
|
|
* @param string $username The username |
|
54
|
|
|
* @param string $apiKeyToValidate The api key |
|
55
|
|
|
* @return boolean Whether the api belongs to the user return true. Otherwise return false |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function isValidApiKey($username, $apiKeyToValidate) |
|
58
|
|
|
{ |
|
59
|
|
|
$userInfo = api_get_user_info_from_username($username); |
|
60
|
|
|
$userId = $userInfo['user_id']; |
|
61
|
|
|
|
|
62
|
|
|
$apiKeys = UserManager::get_api_keys($userId, self::SERVICE_NAME); |
|
63
|
|
|
|
|
64
|
|
|
if (!empty($apiKeys)) { |
|
65
|
|
|
$apiKey = current($apiKeys); |
|
66
|
|
|
|
|
67
|
|
|
if ($apiKey == $apiKeyToValidate) { |
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get the count of new messages for a user |
|
77
|
|
|
* @param string $username The username |
|
78
|
|
|
* @param int $lastId The id of the last received message |
|
79
|
|
|
* @return int The count fo new messages |
|
80
|
|
|
*/ |
|
81
|
|
|
public function countNewMessages($username, $lastId = 0) |
|
82
|
|
|
{ |
|
83
|
|
|
$userInfo = api_get_user_info_from_username($username); |
|
84
|
|
|
$userId = $userInfo['user_id']; |
|
85
|
|
|
|
|
86
|
|
|
return MessageManager::countMessagesFromLastReceivedMessage($userId, $lastId); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get the list of new messages for a user |
|
91
|
|
|
* @param string $username The username |
|
92
|
|
|
* @param int $lastId The id of the last received message |
|
93
|
|
|
* @return array the new message list |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getNewMessages($username, $lastId = 0) |
|
96
|
|
|
{ |
|
97
|
|
|
$messages = array(); |
|
98
|
|
|
|
|
99
|
|
|
$userInfo = api_get_user_info_from_username($username); |
|
100
|
|
|
$userId = $userInfo['user_id']; |
|
101
|
|
|
|
|
102
|
|
|
$lastMessages = MessageManager::getMessagesFromLastReceivedMessage($userId, $lastId); |
|
103
|
|
|
|
|
104
|
|
|
foreach ($lastMessages as $message) { |
|
|
|
|
|
|
105
|
|
|
$hasAttachments = MessageManager::hasAttachments($message['id']); |
|
106
|
|
|
|
|
107
|
|
|
$messages[] = array( |
|
108
|
|
|
'id' => $message['id'], |
|
109
|
|
|
'title' => $message['title'], |
|
110
|
|
|
'sender' => array( |
|
111
|
|
|
'id' => $message['user_id'], |
|
112
|
|
|
'lastname' => $message['lastname'], |
|
113
|
|
|
'firstname' => $message['firstname'], |
|
114
|
|
|
'completeName' => api_get_person_name($message['firstname'], $message['lastname']), |
|
115
|
|
|
), |
|
116
|
|
|
'sendDate' => $message['send_date'], |
|
117
|
|
|
'content' => $message['content'], |
|
118
|
|
|
'hasAttachments' => $hasAttachments, |
|
119
|
|
|
'platform' => array( |
|
120
|
|
|
'website' => api_get_path(WEB_PATH), |
|
121
|
|
|
'messagingTool' => api_get_path(WEB_PATH) . 'main/messages/inbox.php' |
|
122
|
|
|
) |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $messages; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Create the user extra field |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function init() |
|
133
|
|
|
{ |
|
134
|
|
|
$extraField = new ExtraField('user'); |
|
135
|
|
|
$fieldInfo = $extraField->get_handler_field_info_by_field_variable(self::EXTRA_FIELD_GCM_REGISTRATION); |
|
136
|
|
|
|
|
137
|
|
|
if (empty($fieldInfo)) { |
|
138
|
|
|
$extraField->save([ |
|
139
|
|
|
'variable' => self::EXTRA_FIELD_GCM_REGISTRATION, |
|
140
|
|
|
'field_type' => ExtraField::FIELD_TYPE_TEXT, |
|
141
|
|
|
'display_text' => self::EXTRA_FIELD_GCM_REGISTRATION |
|
142
|
|
|
]); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Register the GCM Registration ID for a user |
|
148
|
|
|
* @param Chamilo\UserBundle\Entity\User $user The user |
|
149
|
|
|
* @param string $registrationId The token registration id from GCM |
|
150
|
|
|
* @return int The id after insert or the number of affected rows after update. Otherwhise return false |
|
151
|
|
|
*/ |
|
152
|
|
|
public static function setGcmRegistrationId(Chamilo\UserBundle\Entity\User $user, $registrationId) |
|
153
|
|
|
{ |
|
154
|
|
|
$registrationId = Security::remove_XSS($registrationId); |
|
155
|
|
|
$extraFieldValue = new ExtraFieldValue('user'); |
|
156
|
|
|
|
|
157
|
|
|
return $extraFieldValue->save([ |
|
158
|
|
|
'variable' => self::EXTRA_FIELD_GCM_REGISTRATION, |
|
159
|
|
|
'value' => $registrationId, |
|
160
|
|
|
'item_id' => $user->getId() |
|
161
|
|
|
]); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Send the push notifications to MobileMessaging app |
|
166
|
|
|
* @param array $userIds The IDs of users who will be notified |
|
167
|
|
|
* @param string $title The notification title |
|
168
|
|
|
* @param string $content The notification content |
|
169
|
|
|
* @return int The number of success notifications. Otherwise returns false |
|
170
|
|
|
*/ |
|
171
|
|
|
public static function sendPushNotification(array $userIds, $title, $content) |
|
172
|
|
|
{ |
|
173
|
|
|
if (api_get_configuration_value('messaging_allow_send_push_notification') !== 'true') { |
|
174
|
|
|
return false; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$gdcApiKey = api_get_configuration_value('messaging_gdc_api_key'); |
|
178
|
|
|
|
|
179
|
|
|
if ($gdcApiKey === false) { |
|
180
|
|
|
return false; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$content = str_replace(['<br>', '<br/>', '<br />'], "\n", $content); |
|
184
|
|
|
$content = strip_tags($content); |
|
185
|
|
|
$content = html_entity_decode($content, ENT_QUOTES); |
|
186
|
|
|
|
|
187
|
|
|
$gcmRegistrationIds = []; |
|
188
|
|
|
|
|
189
|
|
|
foreach ($userIds as $userId) { |
|
190
|
|
|
$extraFieldValue = new ExtraFieldValue('user'); |
|
191
|
|
|
$valueInfo = $extraFieldValue->get_values_by_handler_and_field_variable( |
|
192
|
|
|
$userId, |
|
193
|
|
|
self::EXTRA_FIELD_GCM_REGISTRATION |
|
194
|
|
|
); |
|
195
|
|
|
|
|
196
|
|
|
if (empty($valueInfo)) { |
|
197
|
|
|
continue; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
$gcmRegistrationIds[] = $valueInfo['value']; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$headers = [ |
|
204
|
|
|
'Authorization: key=' . $gdcApiKey, |
|
205
|
|
|
'Content-Type: application/json' |
|
206
|
|
|
]; |
|
207
|
|
|
|
|
208
|
|
|
$fields = json_encode([ |
|
209
|
|
|
'registration_ids' => $gcmRegistrationIds, |
|
210
|
|
|
'data' => [ |
|
211
|
|
|
'title' => $title, |
|
212
|
|
|
'message' => $content |
|
213
|
|
|
] |
|
214
|
|
|
]); |
|
215
|
|
|
|
|
216
|
|
|
$ch = curl_init(); |
|
217
|
|
|
curl_setopt($ch, CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/send'); |
|
218
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
|
219
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
|
220
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
221
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
222
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); |
|
223
|
|
|
$result = curl_exec($ch); |
|
224
|
|
|
curl_close($ch); |
|
225
|
|
|
|
|
226
|
|
|
$decodedResult = json_decode($result); |
|
227
|
|
|
|
|
228
|
|
|
return $decodedResult->success; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.