|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace florinp\messenger\models; |
|
4
|
|
|
|
|
5
|
|
|
use phpbb\config\config; |
|
6
|
|
|
use phpbb\db\driver\driver_interface; |
|
7
|
|
|
use phpbb\user; |
|
8
|
|
|
use florinp\messenger\libs\database; |
|
9
|
|
|
use phpbb\user_loader; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class main_model |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var config |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $config; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var driver_interface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $phpbb_db; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var user |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $user; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var database |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $db; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $friends_request_table; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $user_friends_table; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var user_loader |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $user_loader; |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
public function __construct( |
|
52
|
|
|
config $config, |
|
53
|
|
|
driver_interface $phpbb_db, |
|
54
|
|
|
user $user, |
|
55
|
|
|
user_loader $user_loader, |
|
56
|
|
|
database $db, |
|
57
|
|
|
$friends_request_table, |
|
58
|
|
|
$user_friends_table |
|
59
|
|
|
) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->config = $config; |
|
62
|
|
|
$this->phpbb_db = $phpbb_db; |
|
63
|
|
|
$this->user = $user; |
|
64
|
|
|
$this->db = $db; |
|
65
|
|
|
$this->friends_request_table = $friends_request_table; |
|
66
|
|
|
$this->user_friends_table = $user_friends_table; |
|
67
|
|
|
$this->user_loader = $user_loader; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* get all friends |
|
72
|
|
|
* @return array the list of friends |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getFriends() |
|
75
|
|
|
{ |
|
76
|
|
|
$sql = " |
|
77
|
|
|
SELECT u.user_id, |
|
78
|
|
|
u.username, |
|
79
|
|
|
u.username_clean, |
|
80
|
|
|
u.user_type, |
|
81
|
|
|
u.user_colour, |
|
82
|
|
|
s.session_id, |
|
83
|
|
|
s.session_time |
|
84
|
|
|
FROM " . $this->user_friends_table." |
|
85
|
|
|
LEFT JOIN " . USERS_TABLE." AS u ON u.user_id = ".$this->user_friends_table.".friend_id |
|
86
|
|
|
LEFT JOIN " . SESSIONS_TABLE." AS s ON s.session_user_id = u.user_id |
|
87
|
|
|
WHERE " . $this->user_friends_table.".user_id = ".(int)$this->user->data['user_id']." |
|
88
|
|
|
GROUP BY u.user_id |
|
89
|
|
|
"; |
|
90
|
|
|
$result = $this->phpbb_db->sql_query($sql); |
|
91
|
|
|
|
|
92
|
|
|
$friends = array(); |
|
93
|
|
|
while ($row = $this->phpbb_db->sql_fetchrow($result)) { |
|
94
|
|
|
$friends[] = array( |
|
95
|
|
|
'user_id' => $row['user_id'], |
|
96
|
|
|
'username' => $row['username_clean'], |
|
97
|
|
|
'user_colour' => $row['user_colour'], |
|
98
|
|
|
'user_status' => ($row['session_time'] >= (time() - ($this->config['load_online_time'] * 60))) ? 1 : 0, |
|
99
|
|
|
'user_avatar' => $this->user_loader->get_avatar($row['user_id'], true, true), |
|
100
|
|
|
'inbox' => $this->getInboxFromId($row['user_id']) |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
$this->phpbb_db->sql_freeresult(); |
|
104
|
|
|
|
|
105
|
|
|
return $friends; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* checks if a user is friend with other user and vice-versa |
|
110
|
|
|
* @param int $friend_id |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
public function checkFriend($friend_id) |
|
114
|
|
|
{ |
|
115
|
|
|
$sql = " |
|
116
|
|
|
SELECT COUNT(zebra_id) as friend_count |
|
117
|
|
|
FROM " . ZEBRA_TABLE." |
|
118
|
|
|
WHERE user_id = " . (int)$friend_id." |
|
119
|
|
|
AND zebra_id = " . (int)$this->user->data['user_id']." |
|
120
|
|
|
AND friend = 1 |
|
121
|
|
|
AND foe = 0 |
|
122
|
|
|
"; |
|
123
|
|
|
$result = $this->phpbb_db->sql_query($sql); |
|
124
|
|
|
$friend_count = (int)$this->phpbb_db->sql_fetchfield('friend_count'); |
|
125
|
|
|
$this->phpbb_db->sql_freeresult($result); |
|
126
|
|
|
|
|
127
|
|
|
if ($friend_count > 0) { |
|
128
|
|
|
return true; |
|
129
|
|
|
} else { |
|
130
|
|
|
return false; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get number of unread messages from an user |
|
136
|
|
|
* @param $friend_id |
|
137
|
|
|
* @return int |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getInboxFromId($friend_id) |
|
140
|
|
|
{ |
|
141
|
|
|
$sql = "SELECT * |
|
142
|
|
|
FROM `messages` |
|
143
|
|
|
WHERE `sender_id` = :sender_id |
|
144
|
|
|
AND `receiver_id` = :receiver_id |
|
145
|
|
|
AND `newMsg` = 1 |
|
146
|
|
|
"; |
|
147
|
|
|
$results = $this->db->select($sql, array( |
|
148
|
|
|
':sender_id' => (int)$friend_id, |
|
149
|
|
|
':receiver_id' => (int)$this->user->data['user_id'] |
|
150
|
|
|
)); |
|
151
|
|
|
|
|
152
|
|
|
return count($results); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Insert a message in database |
|
157
|
|
|
* @param array $data Message data |
|
158
|
|
|
* @return bool|string |
|
159
|
|
|
*/ |
|
160
|
|
|
public function sendMessage($data) |
|
161
|
|
|
{ |
|
162
|
|
|
$insert = array( |
|
163
|
|
|
'sender_id' => $data['sender_id'], |
|
164
|
|
|
'receiver_id' => $data['receiver_id'], |
|
165
|
|
|
'text' => $data['text'], |
|
166
|
|
|
'newMsg' => 1, |
|
167
|
|
|
'sentAt' => time() |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
if ($this->db->insert('messages', $insert)) { |
|
171
|
|
|
return $this->db->lastInsertId(); |
|
172
|
|
|
} else { |
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Insert a file in database |
|
179
|
|
|
* @param $data File data |
|
180
|
|
|
* @return bool|string |
|
181
|
|
|
*/ |
|
182
|
|
|
public function sendFile($data) |
|
183
|
|
|
{ |
|
184
|
|
|
$insert = array( |
|
185
|
|
|
'sender_id' => $data['sender_id'], |
|
186
|
|
|
'receiver_id' => $data['receiver_id'], |
|
187
|
|
|
'fileName' => $data['fileName'], |
|
188
|
|
|
'file' => $data['file'], |
|
189
|
|
|
'type' => $data['type'], |
|
190
|
|
|
'sentAt' => time() |
|
191
|
|
|
); |
|
192
|
|
|
|
|
193
|
|
|
if ($this->db->insert('files', $insert)) { |
|
194
|
|
|
return $this->db->lastInsertId(); |
|
195
|
|
|
} else { |
|
196
|
|
|
return false; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Returns a message by a id |
|
202
|
|
|
* @param $id Message id |
|
203
|
|
|
* @return mixed |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getMessageById($id) |
|
206
|
|
|
{ |
|
207
|
|
|
$sql = " |
|
208
|
|
|
SELECT * |
|
209
|
|
|
FROM `messages` |
|
210
|
|
|
WHERE `id` = :id |
|
211
|
|
|
LIMIT 1 |
|
212
|
|
|
"; |
|
213
|
|
|
|
|
214
|
|
|
$message = $this->db->select($sql, array( |
|
215
|
|
|
':id' => $id |
|
216
|
|
|
)); |
|
217
|
|
|
$message = $message[0]; |
|
218
|
|
|
$message = $this->parseMessage($message, $message['type']); |
|
219
|
|
|
|
|
220
|
|
|
return $message; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Returns a file by id |
|
225
|
|
|
* @param $id File id |
|
226
|
|
|
* @return mixed |
|
227
|
|
|
*/ |
|
228
|
|
|
public function getFileById($id) |
|
229
|
|
|
{ |
|
230
|
|
|
$sql = " |
|
231
|
|
|
SELECT * |
|
232
|
|
|
FROM `files` |
|
233
|
|
|
WHERE `id` = :id |
|
234
|
|
|
LIMIT 1 |
|
235
|
|
|
"; |
|
236
|
|
|
$file = $this->db->select($sql, array( |
|
237
|
|
|
':id' => $id |
|
238
|
|
|
)); |
|
239
|
|
|
$file = $file[0]; |
|
240
|
|
|
$file = $this->parseFile($file, $file['type']); |
|
241
|
|
|
|
|
242
|
|
|
return $file; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Get all message from an user |
|
247
|
|
|
* @param int $friend_id |
|
248
|
|
|
* @return array |
|
249
|
|
|
*/ |
|
250
|
|
|
public function getMessages($friend_id) |
|
251
|
|
|
{ |
|
252
|
|
|
$this->user_loader->load_users([$this->user->data['user_id'], $friend_id]); |
|
253
|
|
|
|
|
254
|
|
|
// get the sent messages |
|
255
|
|
|
$sql = "SELECT * |
|
256
|
|
|
FROM `messages` |
|
257
|
|
|
WHERE `sender_id` = :sender_id |
|
258
|
|
|
AND `receiver_id` = :receiver_id |
|
259
|
|
|
ORDER BY `sentAt` ASC |
|
260
|
|
|
"; |
|
261
|
|
|
$sqlFiles = "SELECT * |
|
262
|
|
|
FROM `files` |
|
263
|
|
|
WHERE `sender_id` = :sender_id |
|
264
|
|
|
AND `receiver_id` = :receiver_id |
|
265
|
|
|
ORDER BY `sentAt` ASC |
|
266
|
|
|
"; |
|
267
|
|
|
|
|
268
|
|
|
$sentMessages = $this->db->select($sql, array( |
|
269
|
|
|
':sender_id' => $this->user->data['user_id'], |
|
270
|
|
|
':receiver_id' => $friend_id |
|
271
|
|
|
)); |
|
272
|
|
|
$getInbox = $this->db->select($sql, array( |
|
273
|
|
|
':sender_id' => $friend_id, |
|
274
|
|
|
':receiver_id' => $this->user->data['user_id'] |
|
275
|
|
|
)); |
|
276
|
|
|
|
|
277
|
|
|
$sentFiles = $this->db->select($sqlFiles, array( |
|
278
|
|
|
':sender_id' => $this->user->data['user_id'], |
|
279
|
|
|
':receiver_id' => $friend_id |
|
280
|
|
|
)); |
|
281
|
|
|
$getFiles = $this->db->select($sqlFiles, array( |
|
282
|
|
|
':sender_id' => $friend_id, |
|
283
|
|
|
':receiver_id' => $this->user->data['user_id'] |
|
284
|
|
|
)); |
|
285
|
|
|
|
|
286
|
|
|
$sent = array(); |
|
287
|
|
|
foreach ($sentMessages as $msg) { |
|
288
|
|
|
$item = $this->parseMessage($msg, 'sent'); |
|
289
|
|
|
$sent[] = $item; |
|
290
|
|
|
} |
|
291
|
|
|
$inbox = array(); |
|
292
|
|
|
foreach ($getInbox as $msg) { |
|
293
|
|
|
$item = $this->parseMessage($msg, 'inbox'); |
|
294
|
|
|
$inbox[] = $item; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
foreach ($sentFiles as $file) { |
|
298
|
|
|
$item = $this->parseFile($file, 'sent'); |
|
299
|
|
|
$sent[] = $item; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
foreach ($getFiles as $file) { |
|
303
|
|
|
$item = $this->parseFile($file, 'inbox'); |
|
304
|
|
|
$inbox[] = $item; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
$unsorted_messages = array_merge($sent, $inbox); |
|
308
|
|
|
|
|
309
|
|
|
uasort($unsorted_messages, function ($a, $b) { |
|
310
|
|
|
return ($a['sentAt'] > $b['sentAt']) ? -1 : 1; |
|
311
|
|
|
}); |
|
312
|
|
|
|
|
313
|
|
|
$sorted_messages = array(); |
|
314
|
|
|
foreach ($unsorted_messages as $msg) { |
|
315
|
|
|
$sorted_messages[] = $msg; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
return $sorted_messages; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Parse message to specific array |
|
323
|
|
|
* @param array $msg Message data as array from database |
|
324
|
|
|
* @param string $type 'inbox' is for received messages and 'sent' is for sent messages |
|
325
|
|
|
* @return array |
|
326
|
|
|
*/ |
|
327
|
|
|
protected function parseMessage($msg, $type) { |
|
328
|
|
|
$item = array(); |
|
329
|
|
|
$item['id'] = $msg['id']; |
|
330
|
|
|
$item['sender_id'] = $msg['sender_id']; |
|
331
|
|
|
$item['sender_avatar'] = $this->user_loader->get_avatar($msg['sender_id'], true, true); |
|
332
|
|
|
$item['receiver_id'] = $msg['receiver_id']; |
|
333
|
|
|
$item['receiver_avatar'] = $this->user_loader->get_avatar($msg['receiver_id'], true, true); |
|
334
|
|
|
$item['text'] = $msg['text']; |
|
335
|
|
|
$item['sentAt'] = $msg['sentAt']; |
|
336
|
|
|
$item['type'] = $type; |
|
337
|
|
|
|
|
338
|
|
|
return $item; |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
/** |
|
342
|
|
|
* Parse file to specific array |
|
343
|
|
|
* @param array $file File data as array from database |
|
344
|
|
|
* @param string $type 'inbox' is for received messages and 'sent' is for sent messages |
|
345
|
|
|
* @return array |
|
346
|
|
|
*/ |
|
347
|
|
|
protected function parseFile($file, $type) { |
|
348
|
|
|
$item = array(); |
|
349
|
|
|
$item['id'] = 'f_' . $file['id']; |
|
350
|
|
|
$item['sender_id'] = $file['sender_id']; |
|
351
|
|
|
$item['sender_avatar'] = $this->user_loader->get_avatar($file['sender_id'], true, true); |
|
352
|
|
|
$item['receiver_id'] = $file['receiver_id']; |
|
353
|
|
|
$item['receiver_avatar'] = $this->user_loader->get_avatar($file['receiver_id'], true, true); |
|
354
|
|
|
$item['fileName'] = $file['fileName']; |
|
355
|
|
|
$item['file'] = $file['file']; |
|
356
|
|
|
$item['sentAt'] = $file['sentAt']; |
|
357
|
|
|
$item['type'] = $type; |
|
358
|
|
|
|
|
359
|
|
|
return $item; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* Change messages status to read |
|
364
|
|
|
* @param $friend_id |
|
365
|
|
|
* @return int |
|
366
|
|
|
*/ |
|
367
|
|
|
public function updateMessagesStatus($friend_id) |
|
368
|
|
|
{ |
|
369
|
|
|
|
|
370
|
|
|
$sql = " |
|
371
|
|
|
UPDATE `messages` |
|
372
|
|
|
SET `newMsg` = 0 |
|
373
|
|
|
WHERE `newMsg` = 1 |
|
374
|
|
|
AND `sender_id` = :sender_id |
|
375
|
|
|
AND `receiver_id` = :receiver_id |
|
376
|
|
|
"; |
|
377
|
|
|
|
|
378
|
|
|
$sth = $this->db->prepare($sql); |
|
379
|
|
|
$sth->bindValue(':sender_id', $friend_id); |
|
380
|
|
|
$sth->bindValue(':receiver_id', $this->user->data['user_id']); |
|
381
|
|
|
$sth->execute(); |
|
382
|
|
|
|
|
383
|
|
|
return $this->getInboxFromId($friend_id); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
} |
|
387
|
|
|
|