|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace florinp\messenger\controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
7
|
|
|
|
|
8
|
|
|
class main |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
protected $config; |
|
12
|
|
|
protected $helper; |
|
13
|
|
|
protected $template; |
|
14
|
|
|
protected $user; |
|
15
|
|
|
protected $model; |
|
16
|
|
|
protected $request; |
|
17
|
|
|
protected $notification_manager; |
|
18
|
|
|
protected $emojione; |
|
19
|
|
|
protected $upload; |
|
20
|
|
|
protected $download; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct( |
|
23
|
|
|
\phpbb\config\config $config, |
|
24
|
|
|
\phpbb\controller\helper $helper, |
|
25
|
|
|
\phpbb\template\template $template, |
|
26
|
|
|
\phpbb\request\request $request, |
|
27
|
|
|
\phpbb\user $user, |
|
28
|
|
|
\florinp\messenger\models\main_model $model, |
|
29
|
|
|
\phpbb\notification\manager $notification_manager, |
|
30
|
|
|
\florinp\messenger\libs\emojione $emojione, |
|
31
|
|
|
\florinp\messenger\libs\upload $upload, |
|
32
|
|
|
\florinp\messenger\libs\download $download |
|
33
|
|
|
) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->config = $config; |
|
36
|
|
|
$this->helper = $helper; |
|
37
|
|
|
$this->template = $template; |
|
38
|
|
|
$this->request = $request; |
|
39
|
|
|
$this->user = $user; |
|
40
|
|
|
$this->model = $model; |
|
41
|
|
|
$this->notification_manager = $notification_manager; |
|
42
|
|
|
$this->emojione = $emojione; |
|
43
|
|
|
$this->upload = $upload; |
|
44
|
|
|
$this->download = $download; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function handle() |
|
48
|
|
|
{ |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function index() |
|
52
|
|
|
{ |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function publish() |
|
57
|
|
|
{ |
|
58
|
|
|
/* AJAX check */ |
|
59
|
|
|
$http_request = $this->request->server('HTTP_X_REQUESTED_WITH'); |
|
60
|
|
|
if (empty($http_request) && strtolower($http_request) != 'xmlhttprequest') { |
|
61
|
|
|
return new Response("The request is invalid", 500); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$text = $this->request->variable('text', '', true); |
|
65
|
|
|
$receiver_id = $this->request->variable('receiver_id', 0); |
|
66
|
|
|
$sender_id = $this->user->data['user_id']; |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
if ($receiver_id != 0 && trim($text) != '') { |
|
70
|
|
|
$text = htmlspecialchars($text); |
|
71
|
|
|
$text = str_replace(array("\n", "\r"), '', $text); |
|
72
|
|
|
|
|
73
|
|
|
$message = array( |
|
74
|
|
|
'sender_id' => $sender_id, |
|
75
|
|
|
'receiver_id' => $receiver_id, |
|
76
|
|
|
'text' => $text, |
|
77
|
|
|
'sentAt' => time() |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
if ($id = $this->model->sendMessage($message)) { |
|
81
|
|
|
$lastMessage = $this->model->getMessageById($id); |
|
82
|
|
|
$response = array('success' => true, 'message' => $lastMessage); |
|
83
|
|
|
} else { |
|
84
|
|
|
$response = array( |
|
85
|
|
|
'succes' => false, |
|
86
|
|
|
'error' => 'An error has been ocurred!' |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return new JsonResponse($response, 200); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getFile($id) |
|
95
|
|
|
{ |
|
96
|
|
|
$id = explode('_', $id)[1]; |
|
97
|
|
|
$file = $this->model->getFileById($id); |
|
98
|
|
|
$this->download->setFile($file['file']); |
|
99
|
|
|
$this->download->sendDownload(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function sendFile() |
|
103
|
|
|
{ |
|
104
|
|
|
$receiver_id = $this->request->variable('receiver_id', 0); |
|
105
|
|
|
$sender_id = $this->user->data['user_id']; |
|
106
|
|
|
|
|
107
|
|
|
$file = $this->request->file('file'); |
|
108
|
|
|
if ($receiver_id != 0 && !empty($file)) { |
|
109
|
|
|
if ($file['error'] == 0) { |
|
110
|
|
|
$this->upload->file($file); |
|
111
|
|
|
$this->upload->set_allowed_mime_types(array( |
|
112
|
|
|
'image/gif', |
|
113
|
|
|
'image/jpeg', |
|
114
|
|
|
'image/png', |
|
115
|
|
|
'application/pdf', |
|
116
|
|
|
'application/x-rar-compressed', |
|
117
|
|
|
'application/zip', |
|
118
|
|
|
'application/x-7z-compressed', |
|
119
|
|
|
'text/plain' |
|
120
|
|
|
)); |
|
121
|
|
|
$results = $this->upload->upload(); |
|
122
|
|
|
if (isset($results['errors']) && count($results['errors'])) { |
|
123
|
|
|
$response = array( |
|
124
|
|
|
'success' => false, |
|
125
|
|
|
'errors' => $results['errors'] |
|
126
|
|
|
); |
|
127
|
|
|
} else { |
|
128
|
|
|
$data = array( |
|
129
|
|
|
'sender_id' => $sender_id, |
|
130
|
|
|
'receiver_id' => $receiver_id, |
|
131
|
|
|
'fileName' => $results['original_filename'], |
|
132
|
|
|
'file' => $results['filename'], |
|
133
|
|
|
'type' => $results['mime'], |
|
134
|
|
|
); |
|
135
|
|
|
if ($id = $this->model->sendFile($data)) { |
|
136
|
|
|
$lastFile = $this->model->getFileById($id); |
|
137
|
|
|
$response = array( |
|
138
|
|
|
'success' => true, |
|
139
|
|
|
'file' => $lastFile |
|
140
|
|
|
); |
|
141
|
|
|
} else { |
|
142
|
|
|
$response = array( |
|
143
|
|
|
'succes' => false, |
|
144
|
|
|
'error' => 'An error has been ocurred!' |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} else { |
|
149
|
|
|
$response = array( |
|
150
|
|
|
'succes' => false, |
|
151
|
|
|
'error' => $file['error'] |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
} else { |
|
155
|
|
|
$response = array( |
|
156
|
|
|
'succes' => false, |
|
157
|
|
|
'error' => 'An error has been ocurred!' |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return new JsonResponse($response, 200); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
View Code Duplication |
public function load() |
|
|
|
|
|
|
165
|
|
|
{ |
|
166
|
|
|
/* AJAX check */ |
|
167
|
|
|
$http_request = $this->request->server('HTTP_X_REQUESTED_WITH'); |
|
168
|
|
|
if (empty($http_request) && strtolower($http_request) != 'xmlhttprequest') { |
|
169
|
|
|
return new Response("The request is invalid", 500); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$friend_id = $this->request->variable('friend_id', 0); |
|
173
|
|
|
|
|
174
|
|
|
if ($friend_id > 0) { |
|
175
|
|
|
$messages = $this->model->getMessages($friend_id); |
|
176
|
|
|
return new JsonResponse($messages, 200); |
|
177
|
|
|
} |
|
178
|
|
|
return new JsonResponse(array('success' => false, 'error' => 'The request is invalid'), 200); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
View Code Duplication |
public function updateMessages() |
|
|
|
|
|
|
182
|
|
|
{ |
|
183
|
|
|
/* AJAX check */ |
|
184
|
|
|
$http_request = $this->request->server('HTTP_X_REQUESTED_WITH'); |
|
185
|
|
|
if (empty($http_request) && strtolower($http_request) != 'xmlhttprequest') { |
|
186
|
|
|
return new Response("The request is invalid", 500); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$friend_id = $this->request->variable('friend_id', 0); |
|
190
|
|
|
if ($friend_id > 0) { |
|
191
|
|
|
$newVal = $this->model->updateMessagesStatus($friend_id); |
|
192
|
|
|
return new JsonResponse(array('success' => true, 'newVal' => $newVal), 200); |
|
193
|
|
|
} |
|
194
|
|
|
return new JsonResponse(array('success' => false), 200); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
View Code Duplication |
public function checkForNewMessages() |
|
|
|
|
|
|
198
|
|
|
{ |
|
199
|
|
|
/* AJAX check */ |
|
200
|
|
|
$http_request = $this->request->server('HTTP_X_REQUESTED_WITH'); |
|
201
|
|
|
if (empty($http_request) && strtolower($http_request) != 'xmlhttprequest') { |
|
202
|
|
|
return new Response("The request is invalid", 500); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$friend_id = $this->request->variable('friend_id', 0); |
|
206
|
|
|
if ($friend_id > 0) { |
|
207
|
|
|
$messages = $this->model->getInboxFromId($friend_id); |
|
208
|
|
|
return new JsonResponse(array('success' => true, 'messages' => $messages), 200); |
|
209
|
|
|
} |
|
210
|
|
|
return new JsonResponse(array('success' => false), 200); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function getFriends() |
|
214
|
|
|
{ |
|
215
|
|
|
/* AJAX check */ |
|
216
|
|
|
$http_request = $this->request->server('HTTP_X_REQUESTED_WITH'); |
|
217
|
|
|
if (empty($http_request) && strtolower($http_request) != 'xmlhttprequest') { |
|
218
|
|
|
return new Response("The request is invalid", 500); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$friends = $this->model->getFriends(); |
|
222
|
|
|
$friends_online = array_filter($friends, function ($friend) { |
|
223
|
|
|
return $friend['user_status'] != 0; |
|
224
|
|
|
}); |
|
225
|
|
|
|
|
226
|
|
|
$response = array( |
|
227
|
|
|
'friends_online' => count($friends_online), |
|
228
|
|
|
'friends_list' => $friends |
|
229
|
|
|
); |
|
230
|
|
|
|
|
231
|
|
|
return new JsonResponse($response, 200); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
public function getEmoticons() |
|
235
|
|
|
{ |
|
236
|
|
|
$emoticons = \florinp\messenger\libs\emojione::$ascii_replace; |
|
|
|
|
|
|
237
|
|
|
$eicons = array(); |
|
238
|
|
|
foreach ($emoticons as $code => $value) { |
|
239
|
|
|
$eicons[$value] = $code; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
$response = array(); |
|
243
|
|
|
foreach ($eicons as $emoticon) { |
|
244
|
|
|
$item = array(); |
|
245
|
|
|
$item['code'] = $emoticon; |
|
246
|
|
|
$item['image'] = $this->emojione->toImage($emoticon); |
|
247
|
|
|
|
|
248
|
|
|
$response[] = $item; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
return new JsonResponse($response, 200); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
} |
|
255
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: