@@ -5,109 +5,109 @@ |
||
5 | 5 | use InvalidArgumentException; |
6 | 6 | class download { |
7 | 7 | |
8 | - /** |
|
9 | - * File directory |
|
10 | - * @var string |
|
11 | - */ |
|
12 | - protected $directory; |
|
13 | - |
|
14 | - /** |
|
15 | - * File name |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - protected $filename; |
|
19 | - |
|
20 | - /** |
|
21 | - * File pointer |
|
22 | - * @var resource |
|
23 | - */ |
|
24 | - protected $file; |
|
25 | - |
|
26 | - /** |
|
27 | - * File full path |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - protected $full_path; |
|
31 | - |
|
32 | - /** |
|
33 | - * @param string $phpbb_root_path phpBB root path |
|
34 | - */ |
|
35 | - public function __construct($phpbb_root_path) { |
|
36 | - $this->directory = $phpbb_root_path . 'store/messenger/files'; |
|
37 | - } |
|
38 | - |
|
39 | - |
|
40 | - /** |
|
41 | - * Set the filename |
|
42 | - * @param string $filename File name |
|
43 | - * @throws InvalidArgumentException when the file not exist or is not readable |
|
44 | - */ |
|
45 | - public function setFile($filename) { |
|
46 | - $file_full_path = $this->directory . '/'. $filename; |
|
47 | - if(!is_file($file_full_path)) { |
|
48 | - throw new InvalidArgumentException("File does not exist"); |
|
49 | - } else if(!is_readable($file_full_path)) { |
|
50 | - throw new InvalidArgumentException("File to download is not readable."); |
|
51 | - } |
|
52 | - $this->filename = $filename; |
|
53 | - $this->file = fopen($file_full_path, 'rb'); |
|
54 | - $this->full_path = $file_full_path; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Get file mime type |
|
59 | - * @return string |
|
60 | - */ |
|
61 | - private function getMimeType() { |
|
62 | - $fileExtension = pathinfo($this->filename, PATHINFO_EXTENSION); |
|
63 | - $mimeTypeHelper = Mimetypes::getInstance(); |
|
64 | - $mimeType = $mimeTypeHelper->fromExtension($fileExtension); |
|
65 | - |
|
66 | - return !is_null($mimeType) ? $mimeType : 'application/force-download'; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Get file size |
|
71 | - * @return int |
|
72 | - */ |
|
73 | - private function getFileSize() { |
|
74 | - $stat = fstat($this->file); |
|
75 | - return $stat['size']; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Sends the download to browser |
|
80 | - * @param bool|true $forceDownload |
|
81 | - */ |
|
82 | - public function sendDownload($forceDownload = true) { |
|
83 | - if(headers_sent()) { |
|
84 | - throw new \RuntimeException("Cannot send file to the browser, since the headers were already sent"); |
|
85 | - } |
|
86 | - $mimeType = $this->getMimeType(); |
|
87 | - $fileSize = $this->getFileSize(); |
|
88 | - |
|
89 | - ini_set('display_errors', 1); |
|
90 | - error_reporting(E_ALL); |
|
91 | - |
|
92 | - header("Pragma: public"); |
|
93 | - header("Expires: 0"); |
|
94 | - header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
|
95 | - header("Cache-Control: private", false); |
|
96 | - header("Content-Type: application/octet-stream"); |
|
97 | - |
|
98 | - if($forceDownload) { |
|
99 | - header("Content-Disposition: attachment; filename=\"{$this->filename}\";"); |
|
100 | - } else { |
|
101 | - header("Content-Disposition: filename=\"{$this->filename}\";"); |
|
102 | - } |
|
103 | - |
|
104 | - header("Content-Transfer-Encoding: binary"); |
|
105 | - header("Content-Length: {$fileSize}"); |
|
106 | - |
|
107 | - @ob_clean(); |
|
108 | - |
|
109 | - rewind($this->file); |
|
110 | - fpassthru($this->file); |
|
111 | - } |
|
8 | + /** |
|
9 | + * File directory |
|
10 | + * @var string |
|
11 | + */ |
|
12 | + protected $directory; |
|
13 | + |
|
14 | + /** |
|
15 | + * File name |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + protected $filename; |
|
19 | + |
|
20 | + /** |
|
21 | + * File pointer |
|
22 | + * @var resource |
|
23 | + */ |
|
24 | + protected $file; |
|
25 | + |
|
26 | + /** |
|
27 | + * File full path |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + protected $full_path; |
|
31 | + |
|
32 | + /** |
|
33 | + * @param string $phpbb_root_path phpBB root path |
|
34 | + */ |
|
35 | + public function __construct($phpbb_root_path) { |
|
36 | + $this->directory = $phpbb_root_path . 'store/messenger/files'; |
|
37 | + } |
|
38 | + |
|
39 | + |
|
40 | + /** |
|
41 | + * Set the filename |
|
42 | + * @param string $filename File name |
|
43 | + * @throws InvalidArgumentException when the file not exist or is not readable |
|
44 | + */ |
|
45 | + public function setFile($filename) { |
|
46 | + $file_full_path = $this->directory . '/'. $filename; |
|
47 | + if(!is_file($file_full_path)) { |
|
48 | + throw new InvalidArgumentException("File does not exist"); |
|
49 | + } else if(!is_readable($file_full_path)) { |
|
50 | + throw new InvalidArgumentException("File to download is not readable."); |
|
51 | + } |
|
52 | + $this->filename = $filename; |
|
53 | + $this->file = fopen($file_full_path, 'rb'); |
|
54 | + $this->full_path = $file_full_path; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Get file mime type |
|
59 | + * @return string |
|
60 | + */ |
|
61 | + private function getMimeType() { |
|
62 | + $fileExtension = pathinfo($this->filename, PATHINFO_EXTENSION); |
|
63 | + $mimeTypeHelper = Mimetypes::getInstance(); |
|
64 | + $mimeType = $mimeTypeHelper->fromExtension($fileExtension); |
|
65 | + |
|
66 | + return !is_null($mimeType) ? $mimeType : 'application/force-download'; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Get file size |
|
71 | + * @return int |
|
72 | + */ |
|
73 | + private function getFileSize() { |
|
74 | + $stat = fstat($this->file); |
|
75 | + return $stat['size']; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Sends the download to browser |
|
80 | + * @param bool|true $forceDownload |
|
81 | + */ |
|
82 | + public function sendDownload($forceDownload = true) { |
|
83 | + if(headers_sent()) { |
|
84 | + throw new \RuntimeException("Cannot send file to the browser, since the headers were already sent"); |
|
85 | + } |
|
86 | + $mimeType = $this->getMimeType(); |
|
87 | + $fileSize = $this->getFileSize(); |
|
88 | + |
|
89 | + ini_set('display_errors', 1); |
|
90 | + error_reporting(E_ALL); |
|
91 | + |
|
92 | + header("Pragma: public"); |
|
93 | + header("Expires: 0"); |
|
94 | + header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
|
95 | + header("Cache-Control: private", false); |
|
96 | + header("Content-Type: application/octet-stream"); |
|
97 | + |
|
98 | + if($forceDownload) { |
|
99 | + header("Content-Disposition: attachment; filename=\"{$this->filename}\";"); |
|
100 | + } else { |
|
101 | + header("Content-Disposition: filename=\"{$this->filename}\";"); |
|
102 | + } |
|
103 | + |
|
104 | + header("Content-Transfer-Encoding: binary"); |
|
105 | + header("Content-Length: {$fileSize}"); |
|
106 | + |
|
107 | + @ob_clean(); |
|
108 | + |
|
109 | + rewind($this->file); |
|
110 | + fpassthru($this->file); |
|
111 | + } |
|
112 | 112 | |
113 | 113 | } |
114 | 114 | \ No newline at end of file |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * @param string $phpbb_root_path phpBB root path |
34 | 34 | */ |
35 | 35 | public function __construct($phpbb_root_path) { |
36 | - $this->directory = $phpbb_root_path . 'store/messenger/files'; |
|
36 | + $this->directory = $phpbb_root_path.'store/messenger/files'; |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | |
@@ -43,10 +43,10 @@ discard block |
||
43 | 43 | * @throws InvalidArgumentException when the file not exist or is not readable |
44 | 44 | */ |
45 | 45 | public function setFile($filename) { |
46 | - $file_full_path = $this->directory . '/'. $filename; |
|
47 | - if(!is_file($file_full_path)) { |
|
46 | + $file_full_path = $this->directory.'/'.$filename; |
|
47 | + if (!is_file($file_full_path)) { |
|
48 | 48 | throw new InvalidArgumentException("File does not exist"); |
49 | - } else if(!is_readable($file_full_path)) { |
|
49 | + } else if (!is_readable($file_full_path)) { |
|
50 | 50 | throw new InvalidArgumentException("File to download is not readable."); |
51 | 51 | } |
52 | 52 | $this->filename = $filename; |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | * @param bool|true $forceDownload |
81 | 81 | */ |
82 | 82 | public function sendDownload($forceDownload = true) { |
83 | - if(headers_sent()) { |
|
83 | + if (headers_sent()) { |
|
84 | 84 | throw new \RuntimeException("Cannot send file to the browser, since the headers were already sent"); |
85 | 85 | } |
86 | 86 | $mimeType = $this->getMimeType(); |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | header("Cache-Control: private", false); |
96 | 96 | header("Content-Type: application/octet-stream"); |
97 | 97 | |
98 | - if($forceDownload) { |
|
98 | + if ($forceDownload) { |
|
99 | 99 | header("Content-Disposition: attachment; filename=\"{$this->filename}\";"); |
100 | 100 | } else { |
101 | 101 | header("Content-Disposition: filename=\"{$this->filename}\";"); |
@@ -219,7 +219,7 @@ |
||
219 | 219 | } |
220 | 220 | |
221 | 221 | $friends = $this->model->getFriends(); |
222 | - $friends_online = array_filter($friends, function ($friend) { |
|
222 | + $friends_online = array_filter($friends, function($friend) { |
|
223 | 223 | return $friend['user_status'] != 0; |
224 | 224 | }); |
225 | 225 |
@@ -2,7 +2,6 @@ |
||
2 | 2 | |
3 | 3 | namespace florinp\messenger\controller; |
4 | 4 | |
5 | -use Symfony\Component\HttpFoundation\Response; |
|
6 | 5 | use Symfony\Component\HttpFoundation\JsonResponse; |
7 | 6 | |
8 | 7 | class main |
@@ -8,204 +8,204 @@ |
||
8 | 8 | class main |
9 | 9 | { |
10 | 10 | |
11 | - protected $user; |
|
12 | - protected $model; |
|
13 | - protected $request; |
|
14 | - protected $notification_manager; |
|
15 | - protected $emojione; |
|
16 | - protected $upload; |
|
17 | - protected $download; |
|
18 | - |
|
19 | - public function __construct( |
|
20 | - \phpbb\request\request $request, |
|
21 | - \phpbb\user $user, |
|
22 | - \florinp\messenger\models\main_model $model, |
|
23 | - \phpbb\notification\manager $notification_manager, |
|
24 | - \florinp\messenger\libs\emojione $emojione, |
|
25 | - \florinp\messenger\libs\upload $upload, |
|
26 | - \florinp\messenger\libs\download $download |
|
27 | - ) |
|
28 | - { |
|
29 | - $this->request = $request; |
|
30 | - $this->user = $user; |
|
31 | - $this->model = $model; |
|
32 | - $this->notification_manager = $notification_manager; |
|
33 | - $this->emojione = $emojione; |
|
34 | - $this->upload = $upload; |
|
35 | - $this->download = $download; |
|
36 | - } |
|
37 | - |
|
38 | - public function handle() |
|
39 | - { |
|
40 | - } |
|
41 | - |
|
42 | - public function index() |
|
43 | - { |
|
44 | - |
|
45 | - } |
|
46 | - |
|
47 | - public function publish() |
|
48 | - { |
|
49 | - $text = $this->request->variable('text', '', true); |
|
50 | - $receiver_id = $this->request->variable('receiver_id', 0); |
|
51 | - $sender_id = $this->user->data['user_id']; |
|
52 | - |
|
53 | - $response = array(); |
|
54 | - if ($receiver_id != 0 && trim($text) != '') { |
|
55 | - $text = htmlspecialchars($text); |
|
56 | - $text = str_replace(array("\n", "\r"), '', $text); |
|
57 | - |
|
58 | - $message = array( |
|
59 | - 'sender_id' => $sender_id, |
|
60 | - 'receiver_id' => $receiver_id, |
|
61 | - 'text' => $text, |
|
62 | - 'sentAt' => time() |
|
63 | - ); |
|
64 | - |
|
65 | - if ($id = $this->model->sendMessage($message)) { |
|
66 | - $lastMessage = $this->model->getMessageById($id); |
|
67 | - $response = array('success' => true, 'message' => $lastMessage); |
|
68 | - } else { |
|
69 | - $response = array( |
|
70 | - 'succes' => false, |
|
71 | - 'error' => 'An error has been ocurred!' |
|
72 | - ); |
|
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - return new JsonResponse($response, 200); |
|
77 | - } |
|
78 | - |
|
79 | - public function getFile($id) |
|
80 | - { |
|
81 | - $id = explode('_', $id)[1]; |
|
82 | - $file = $this->model->getFileById($id); |
|
83 | - $this->download->setFile($file['file']); |
|
84 | - $this->download->sendDownload(); |
|
85 | - } |
|
86 | - |
|
87 | - public function sendFile() |
|
88 | - { |
|
89 | - $receiver_id = $this->request->variable('receiver_id', 0); |
|
90 | - $sender_id = $this->user->data['user_id']; |
|
91 | - |
|
92 | - $response = array(); |
|
93 | - $file = $this->request->file('file'); |
|
94 | - if ($receiver_id != 0 && !empty($file)) { |
|
95 | - if ($file['error'] == 0) { |
|
96 | - $this->upload->file($file); |
|
97 | - $this->upload->set_allowed_mime_types(array( |
|
98 | - 'image/gif', |
|
99 | - 'image/jpeg', |
|
100 | - 'image/png', |
|
101 | - 'application/pdf', |
|
102 | - 'application/x-rar-compressed', |
|
103 | - 'application/zip', |
|
104 | - 'application/x-7z-compressed', |
|
105 | - 'text/plain' |
|
106 | - )); |
|
107 | - $results = $this->upload->upload(); |
|
108 | - if (isset($results['errors']) && count($results['errors'])) { |
|
109 | - $response = array( |
|
110 | - 'success' => false, |
|
111 | - 'errors' => $results['errors'] |
|
112 | - ); |
|
113 | - } else { |
|
114 | - $data = array( |
|
115 | - 'sender_id' => $sender_id, |
|
116 | - 'receiver_id' => $receiver_id, |
|
117 | - 'fileName' => $results['original_filename'], |
|
118 | - 'file' => $results['filename'], |
|
119 | - 'type' => $results['mime'], |
|
120 | - ); |
|
121 | - if ($id = $this->model->sendFile($data)) { |
|
122 | - $lastFile = $this->model->getFileById($id); |
|
123 | - $response = array( |
|
124 | - 'success' => true, |
|
125 | - 'file' => $lastFile |
|
126 | - ); |
|
127 | - } else { |
|
128 | - $response = array( |
|
129 | - 'succes' => false, |
|
130 | - 'error' => 'An error has been ocurred!' |
|
131 | - ); |
|
132 | - } |
|
133 | - } |
|
134 | - } else { |
|
135 | - $response = array( |
|
136 | - 'succes' => false, |
|
137 | - 'error' => $file['error'] |
|
138 | - ); |
|
139 | - } |
|
140 | - } |
|
141 | - |
|
142 | - return new JsonResponse($response, 200); |
|
143 | - } |
|
144 | - |
|
145 | - public function load() |
|
146 | - { |
|
147 | - $friend_id = $this->request->variable('friend_id', 0); |
|
148 | - |
|
149 | - if ($friend_id > 0) { |
|
150 | - $messages = $this->model->getMessages($friend_id); |
|
151 | - return new JsonResponse($messages, 200); |
|
152 | - } |
|
153 | - return new JsonResponse(array('success' => false, 'error' => 'The request is invalid'), 200); |
|
154 | - } |
|
155 | - |
|
156 | - public function updateMessages() |
|
157 | - { |
|
158 | - $friend_id = $this->request->variable('friend_id', 0); |
|
159 | - if ($friend_id > 0) { |
|
160 | - $newVal = $this->model->updateMessagesStatus($friend_id); |
|
161 | - return new JsonResponse(array('success' => true, 'newVal' => $newVal), 200); |
|
162 | - } |
|
163 | - return new JsonResponse(array('success' => false), 200); |
|
164 | - } |
|
165 | - |
|
166 | - public function checkForNewMessages() |
|
167 | - { |
|
168 | - $friend_id = $this->request->variable('friend_id', 0); |
|
169 | - if ($friend_id > 0) { |
|
170 | - $messages = $this->model->getInboxFromId($friend_id); |
|
171 | - return new JsonResponse(array('success' => true, 'messages' => $messages), 200); |
|
172 | - } |
|
173 | - return new JsonResponse(array('success' => false), 200); |
|
174 | - } |
|
175 | - |
|
176 | - public function getFriends() |
|
177 | - { |
|
178 | - $friends = $this->model->getFriends(); |
|
179 | - $friends_online = array_filter($friends, function ($friend) { |
|
180 | - return $friend['user_status'] != 0; |
|
181 | - }); |
|
182 | - |
|
183 | - $response = array( |
|
184 | - 'friends_online' => count($friends_online), |
|
185 | - 'friends_list' => $friends |
|
186 | - ); |
|
187 | - |
|
188 | - return new JsonResponse($response, 200); |
|
189 | - } |
|
190 | - |
|
191 | - public function getEmoticons() |
|
192 | - { |
|
193 | - $emoticons = \florinp\messenger\libs\emojione::$ascii_replace; |
|
194 | - $eicons = array(); |
|
195 | - foreach ($emoticons as $code => $value) { |
|
196 | - $eicons[$value] = $code; |
|
197 | - } |
|
198 | - |
|
199 | - $response = array(); |
|
200 | - foreach ($eicons as $emoticon) { |
|
201 | - $item = array(); |
|
202 | - $item['code'] = $emoticon; |
|
203 | - $item['image'] = $this->emojione->toImage($emoticon); |
|
204 | - |
|
205 | - $response[] = $item; |
|
206 | - } |
|
207 | - |
|
208 | - return new JsonResponse($response, 200); |
|
209 | - } |
|
11 | + protected $user; |
|
12 | + protected $model; |
|
13 | + protected $request; |
|
14 | + protected $notification_manager; |
|
15 | + protected $emojione; |
|
16 | + protected $upload; |
|
17 | + protected $download; |
|
18 | + |
|
19 | + public function __construct( |
|
20 | + \phpbb\request\request $request, |
|
21 | + \phpbb\user $user, |
|
22 | + \florinp\messenger\models\main_model $model, |
|
23 | + \phpbb\notification\manager $notification_manager, |
|
24 | + \florinp\messenger\libs\emojione $emojione, |
|
25 | + \florinp\messenger\libs\upload $upload, |
|
26 | + \florinp\messenger\libs\download $download |
|
27 | + ) |
|
28 | + { |
|
29 | + $this->request = $request; |
|
30 | + $this->user = $user; |
|
31 | + $this->model = $model; |
|
32 | + $this->notification_manager = $notification_manager; |
|
33 | + $this->emojione = $emojione; |
|
34 | + $this->upload = $upload; |
|
35 | + $this->download = $download; |
|
36 | + } |
|
37 | + |
|
38 | + public function handle() |
|
39 | + { |
|
40 | + } |
|
41 | + |
|
42 | + public function index() |
|
43 | + { |
|
44 | + |
|
45 | + } |
|
46 | + |
|
47 | + public function publish() |
|
48 | + { |
|
49 | + $text = $this->request->variable('text', '', true); |
|
50 | + $receiver_id = $this->request->variable('receiver_id', 0); |
|
51 | + $sender_id = $this->user->data['user_id']; |
|
52 | + |
|
53 | + $response = array(); |
|
54 | + if ($receiver_id != 0 && trim($text) != '') { |
|
55 | + $text = htmlspecialchars($text); |
|
56 | + $text = str_replace(array("\n", "\r"), '', $text); |
|
57 | + |
|
58 | + $message = array( |
|
59 | + 'sender_id' => $sender_id, |
|
60 | + 'receiver_id' => $receiver_id, |
|
61 | + 'text' => $text, |
|
62 | + 'sentAt' => time() |
|
63 | + ); |
|
64 | + |
|
65 | + if ($id = $this->model->sendMessage($message)) { |
|
66 | + $lastMessage = $this->model->getMessageById($id); |
|
67 | + $response = array('success' => true, 'message' => $lastMessage); |
|
68 | + } else { |
|
69 | + $response = array( |
|
70 | + 'succes' => false, |
|
71 | + 'error' => 'An error has been ocurred!' |
|
72 | + ); |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + return new JsonResponse($response, 200); |
|
77 | + } |
|
78 | + |
|
79 | + public function getFile($id) |
|
80 | + { |
|
81 | + $id = explode('_', $id)[1]; |
|
82 | + $file = $this->model->getFileById($id); |
|
83 | + $this->download->setFile($file['file']); |
|
84 | + $this->download->sendDownload(); |
|
85 | + } |
|
86 | + |
|
87 | + public function sendFile() |
|
88 | + { |
|
89 | + $receiver_id = $this->request->variable('receiver_id', 0); |
|
90 | + $sender_id = $this->user->data['user_id']; |
|
91 | + |
|
92 | + $response = array(); |
|
93 | + $file = $this->request->file('file'); |
|
94 | + if ($receiver_id != 0 && !empty($file)) { |
|
95 | + if ($file['error'] == 0) { |
|
96 | + $this->upload->file($file); |
|
97 | + $this->upload->set_allowed_mime_types(array( |
|
98 | + 'image/gif', |
|
99 | + 'image/jpeg', |
|
100 | + 'image/png', |
|
101 | + 'application/pdf', |
|
102 | + 'application/x-rar-compressed', |
|
103 | + 'application/zip', |
|
104 | + 'application/x-7z-compressed', |
|
105 | + 'text/plain' |
|
106 | + )); |
|
107 | + $results = $this->upload->upload(); |
|
108 | + if (isset($results['errors']) && count($results['errors'])) { |
|
109 | + $response = array( |
|
110 | + 'success' => false, |
|
111 | + 'errors' => $results['errors'] |
|
112 | + ); |
|
113 | + } else { |
|
114 | + $data = array( |
|
115 | + 'sender_id' => $sender_id, |
|
116 | + 'receiver_id' => $receiver_id, |
|
117 | + 'fileName' => $results['original_filename'], |
|
118 | + 'file' => $results['filename'], |
|
119 | + 'type' => $results['mime'], |
|
120 | + ); |
|
121 | + if ($id = $this->model->sendFile($data)) { |
|
122 | + $lastFile = $this->model->getFileById($id); |
|
123 | + $response = array( |
|
124 | + 'success' => true, |
|
125 | + 'file' => $lastFile |
|
126 | + ); |
|
127 | + } else { |
|
128 | + $response = array( |
|
129 | + 'succes' => false, |
|
130 | + 'error' => 'An error has been ocurred!' |
|
131 | + ); |
|
132 | + } |
|
133 | + } |
|
134 | + } else { |
|
135 | + $response = array( |
|
136 | + 'succes' => false, |
|
137 | + 'error' => $file['error'] |
|
138 | + ); |
|
139 | + } |
|
140 | + } |
|
141 | + |
|
142 | + return new JsonResponse($response, 200); |
|
143 | + } |
|
144 | + |
|
145 | + public function load() |
|
146 | + { |
|
147 | + $friend_id = $this->request->variable('friend_id', 0); |
|
148 | + |
|
149 | + if ($friend_id > 0) { |
|
150 | + $messages = $this->model->getMessages($friend_id); |
|
151 | + return new JsonResponse($messages, 200); |
|
152 | + } |
|
153 | + return new JsonResponse(array('success' => false, 'error' => 'The request is invalid'), 200); |
|
154 | + } |
|
155 | + |
|
156 | + public function updateMessages() |
|
157 | + { |
|
158 | + $friend_id = $this->request->variable('friend_id', 0); |
|
159 | + if ($friend_id > 0) { |
|
160 | + $newVal = $this->model->updateMessagesStatus($friend_id); |
|
161 | + return new JsonResponse(array('success' => true, 'newVal' => $newVal), 200); |
|
162 | + } |
|
163 | + return new JsonResponse(array('success' => false), 200); |
|
164 | + } |
|
165 | + |
|
166 | + public function checkForNewMessages() |
|
167 | + { |
|
168 | + $friend_id = $this->request->variable('friend_id', 0); |
|
169 | + if ($friend_id > 0) { |
|
170 | + $messages = $this->model->getInboxFromId($friend_id); |
|
171 | + return new JsonResponse(array('success' => true, 'messages' => $messages), 200); |
|
172 | + } |
|
173 | + return new JsonResponse(array('success' => false), 200); |
|
174 | + } |
|
175 | + |
|
176 | + public function getFriends() |
|
177 | + { |
|
178 | + $friends = $this->model->getFriends(); |
|
179 | + $friends_online = array_filter($friends, function ($friend) { |
|
180 | + return $friend['user_status'] != 0; |
|
181 | + }); |
|
182 | + |
|
183 | + $response = array( |
|
184 | + 'friends_online' => count($friends_online), |
|
185 | + 'friends_list' => $friends |
|
186 | + ); |
|
187 | + |
|
188 | + return new JsonResponse($response, 200); |
|
189 | + } |
|
190 | + |
|
191 | + public function getEmoticons() |
|
192 | + { |
|
193 | + $emoticons = \florinp\messenger\libs\emojione::$ascii_replace; |
|
194 | + $eicons = array(); |
|
195 | + foreach ($emoticons as $code => $value) { |
|
196 | + $eicons[$value] = $code; |
|
197 | + } |
|
198 | + |
|
199 | + $response = array(); |
|
200 | + foreach ($eicons as $emoticon) { |
|
201 | + $item = array(); |
|
202 | + $item['code'] = $emoticon; |
|
203 | + $item['image'] = $this->emojione->toImage($emoticon); |
|
204 | + |
|
205 | + $response[] = $item; |
|
206 | + } |
|
207 | + |
|
208 | + return new JsonResponse($response, 200); |
|
209 | + } |
|
210 | 210 | |
211 | 211 | } |
@@ -6,14 +6,14 @@ discard block |
||
6 | 6 | class ext extends \phpbb\extension\base |
7 | 7 | { |
8 | 8 | |
9 | - public function enable_step($old_state) |
|
10 | - { |
|
11 | - global $phpbb_root_path; |
|
12 | - |
|
13 | - switch ($old_state) { |
|
14 | - case '': |
|
15 | - $db = new \florinp\messenger\libs\database(); |
|
16 | - $db->exec(" |
|
9 | + public function enable_step($old_state) |
|
10 | + { |
|
11 | + global $phpbb_root_path; |
|
12 | + |
|
13 | + switch ($old_state) { |
|
14 | + case '': |
|
15 | + $db = new \florinp\messenger\libs\database(); |
|
16 | + $db->exec(" |
|
17 | 17 | CREATE TABLE IF NOT EXISTS messages ( |
18 | 18 | `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, |
19 | 19 | `sender_id` INTEGER NOT NULL, |
@@ -33,87 +33,87 @@ discard block |
||
33 | 33 | ); |
34 | 34 | "); |
35 | 35 | |
36 | - $messengerDir = $phpbb_root_path . 'store/messenger'; |
|
37 | - if (!is_dir($messengerDir)) { |
|
38 | - mkdir($messengerDir, 0777); |
|
39 | - $filesDir = $messengerDir . '/files'; |
|
40 | - if (!is_dir($filesDir)) { |
|
41 | - mkdir($filesDir, 0777); |
|
42 | - } |
|
43 | - } |
|
44 | - return 'notifications'; |
|
45 | - break; |
|
46 | - |
|
47 | - case 'notifications': |
|
48 | - $phpbb_notifications = $this->container->get('notification_manager'); |
|
49 | - $phpbb_notifications->enable_notifications('florinp.messenger.notification.type.friend_request'); |
|
50 | - return 'step2'; |
|
51 | - break; |
|
52 | - |
|
53 | - default: |
|
54 | - return parent::enable_step($old_state); |
|
55 | - break; |
|
56 | - } |
|
57 | - |
|
58 | - } |
|
59 | - |
|
60 | - public function disable_step($old_state) |
|
61 | - { |
|
62 | - switch ($old_state) { |
|
63 | - case '': |
|
64 | - $phpbb_notifications = $this->container->get('notification_manager'); |
|
65 | - $phpbb_notifications->disable_notifications('florinp.messenger.notification.type.friend_request'); |
|
66 | - return 'notifications'; |
|
67 | - break; |
|
68 | - |
|
69 | - default: |
|
70 | - |
|
71 | - return parent::disable_step($old_state); |
|
72 | - break; |
|
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - public function purge_step($old_state) |
|
77 | - { |
|
78 | - global $phpbb_root_path; |
|
79 | - |
|
80 | - $database = $phpbb_root_path . 'store/messenger.db'; |
|
81 | - if (is_file($database)) { |
|
82 | - unlink($database); |
|
83 | - } |
|
84 | - |
|
85 | - $messengerDir = $phpbb_root_path . 'store/messenger'; |
|
86 | - if (is_dir($messengerDir)) { |
|
87 | - $objects = scandir($messengerDir); |
|
88 | - foreach ($objects as $object) { |
|
89 | - if ($object != '.' && $object != '..') { |
|
90 | - if (filetype($messengerDir . "/" . $object) == "dir") { |
|
91 | - $dir = $messengerDir . "/" . $object; |
|
92 | - $subObjects = scandir($dir); |
|
93 | - if(count($subObjects) > 0) { |
|
94 | - foreach($subObjects as $subObject) { |
|
95 | - if($subObject != '.' && $subObject != '..') { |
|
96 | - if(filetype($dir . '/' . $subObject) != 'dir') { |
|
97 | - unlink($dir . '/' . $subObject); |
|
98 | - } |
|
99 | - } else { |
|
100 | - continue; |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - } |
|
105 | - rmdir($messengerDir . "/" . $object); |
|
106 | - } else { |
|
107 | - unlink($messengerDir . "/" . $object); |
|
108 | - } |
|
109 | - } |
|
110 | - } |
|
111 | - reset($objects); |
|
112 | - rmdir($messengerDir); |
|
113 | - } |
|
114 | - |
|
115 | - return parent::purge_step($old_state); |
|
116 | - |
|
117 | - } |
|
36 | + $messengerDir = $phpbb_root_path . 'store/messenger'; |
|
37 | + if (!is_dir($messengerDir)) { |
|
38 | + mkdir($messengerDir, 0777); |
|
39 | + $filesDir = $messengerDir . '/files'; |
|
40 | + if (!is_dir($filesDir)) { |
|
41 | + mkdir($filesDir, 0777); |
|
42 | + } |
|
43 | + } |
|
44 | + return 'notifications'; |
|
45 | + break; |
|
46 | + |
|
47 | + case 'notifications': |
|
48 | + $phpbb_notifications = $this->container->get('notification_manager'); |
|
49 | + $phpbb_notifications->enable_notifications('florinp.messenger.notification.type.friend_request'); |
|
50 | + return 'step2'; |
|
51 | + break; |
|
52 | + |
|
53 | + default: |
|
54 | + return parent::enable_step($old_state); |
|
55 | + break; |
|
56 | + } |
|
57 | + |
|
58 | + } |
|
59 | + |
|
60 | + public function disable_step($old_state) |
|
61 | + { |
|
62 | + switch ($old_state) { |
|
63 | + case '': |
|
64 | + $phpbb_notifications = $this->container->get('notification_manager'); |
|
65 | + $phpbb_notifications->disable_notifications('florinp.messenger.notification.type.friend_request'); |
|
66 | + return 'notifications'; |
|
67 | + break; |
|
68 | + |
|
69 | + default: |
|
70 | + |
|
71 | + return parent::disable_step($old_state); |
|
72 | + break; |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + public function purge_step($old_state) |
|
77 | + { |
|
78 | + global $phpbb_root_path; |
|
79 | + |
|
80 | + $database = $phpbb_root_path . 'store/messenger.db'; |
|
81 | + if (is_file($database)) { |
|
82 | + unlink($database); |
|
83 | + } |
|
84 | + |
|
85 | + $messengerDir = $phpbb_root_path . 'store/messenger'; |
|
86 | + if (is_dir($messengerDir)) { |
|
87 | + $objects = scandir($messengerDir); |
|
88 | + foreach ($objects as $object) { |
|
89 | + if ($object != '.' && $object != '..') { |
|
90 | + if (filetype($messengerDir . "/" . $object) == "dir") { |
|
91 | + $dir = $messengerDir . "/" . $object; |
|
92 | + $subObjects = scandir($dir); |
|
93 | + if(count($subObjects) > 0) { |
|
94 | + foreach($subObjects as $subObject) { |
|
95 | + if($subObject != '.' && $subObject != '..') { |
|
96 | + if(filetype($dir . '/' . $subObject) != 'dir') { |
|
97 | + unlink($dir . '/' . $subObject); |
|
98 | + } |
|
99 | + } else { |
|
100 | + continue; |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + } |
|
105 | + rmdir($messengerDir . "/" . $object); |
|
106 | + } else { |
|
107 | + unlink($messengerDir . "/" . $object); |
|
108 | + } |
|
109 | + } |
|
110 | + } |
|
111 | + reset($objects); |
|
112 | + rmdir($messengerDir); |
|
113 | + } |
|
114 | + |
|
115 | + return parent::purge_step($old_state); |
|
116 | + |
|
117 | + } |
|
118 | 118 | |
119 | 119 | } |
@@ -33,10 +33,10 @@ discard block |
||
33 | 33 | ); |
34 | 34 | "); |
35 | 35 | |
36 | - $messengerDir = $phpbb_root_path . 'store/messenger'; |
|
36 | + $messengerDir = $phpbb_root_path.'store/messenger'; |
|
37 | 37 | if (!is_dir($messengerDir)) { |
38 | 38 | mkdir($messengerDir, 0777); |
39 | - $filesDir = $messengerDir . '/files'; |
|
39 | + $filesDir = $messengerDir.'/files'; |
|
40 | 40 | if (!is_dir($filesDir)) { |
41 | 41 | mkdir($filesDir, 0777); |
42 | 42 | } |
@@ -77,24 +77,24 @@ discard block |
||
77 | 77 | { |
78 | 78 | global $phpbb_root_path; |
79 | 79 | |
80 | - $database = $phpbb_root_path . 'store/messenger.db'; |
|
80 | + $database = $phpbb_root_path.'store/messenger.db'; |
|
81 | 81 | if (is_file($database)) { |
82 | 82 | unlink($database); |
83 | 83 | } |
84 | 84 | |
85 | - $messengerDir = $phpbb_root_path . 'store/messenger'; |
|
85 | + $messengerDir = $phpbb_root_path.'store/messenger'; |
|
86 | 86 | if (is_dir($messengerDir)) { |
87 | 87 | $objects = scandir($messengerDir); |
88 | 88 | foreach ($objects as $object) { |
89 | 89 | if ($object != '.' && $object != '..') { |
90 | - if (filetype($messengerDir . "/" . $object) == "dir") { |
|
91 | - $dir = $messengerDir . "/" . $object; |
|
90 | + if (filetype($messengerDir."/".$object) == "dir") { |
|
91 | + $dir = $messengerDir."/".$object; |
|
92 | 92 | $subObjects = scandir($dir); |
93 | - if(count($subObjects) > 0) { |
|
94 | - foreach($subObjects as $subObject) { |
|
95 | - if($subObject != '.' && $subObject != '..') { |
|
96 | - if(filetype($dir . '/' . $subObject) != 'dir') { |
|
97 | - unlink($dir . '/' . $subObject); |
|
93 | + if (count($subObjects) > 0) { |
|
94 | + foreach ($subObjects as $subObject) { |
|
95 | + if ($subObject != '.' && $subObject != '..') { |
|
96 | + if (filetype($dir.'/'.$subObject) != 'dir') { |
|
97 | + unlink($dir.'/'.$subObject); |
|
98 | 98 | } |
99 | 99 | } else { |
100 | 100 | continue; |
@@ -102,9 +102,9 @@ discard block |
||
102 | 102 | } |
103 | 103 | |
104 | 104 | } |
105 | - rmdir($messengerDir . "/" . $object); |
|
105 | + rmdir($messengerDir."/".$object); |
|
106 | 106 | } else { |
107 | - unlink($messengerDir . "/" . $object); |
|
107 | + unlink($messengerDir."/".$object); |
|
108 | 108 | } |
109 | 109 | } |
110 | 110 | } |
@@ -6,108 +6,108 @@ |
||
6 | 6 | |
7 | 7 | class main_test extends \phpbb_test_case { |
8 | 8 | |
9 | - /** |
|
10 | - * @var \florinp\messenger\controller\main |
|
11 | - */ |
|
12 | - protected $controller; |
|
13 | - |
|
14 | - public function setUp() { |
|
15 | - parent::setUp(); |
|
16 | - /** |
|
17 | - * @var \phpbb\user $user Mock the user class |
|
18 | - */ |
|
19 | - $user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); |
|
20 | - |
|
21 | - /** |
|
22 | - * @var \phpbb\request\request $request |
|
23 | - */ |
|
24 | - $request = $this->getMockBuilder('\phpbb\request\request') |
|
25 | - ->disableOriginalConstructor() |
|
26 | - ->getMock(); |
|
27 | - |
|
28 | - /** |
|
29 | - * @var \florinp\messenger\models\main_model $model |
|
30 | - */ |
|
31 | - $model = $this->getMockBuilder('\florinp\messenger\models\main_model') |
|
32 | - ->disableOriginalConstructor() |
|
33 | - ->getMock(); |
|
34 | - |
|
35 | - /** |
|
36 | - * @var \phpbb\notification\manager $notification_manager |
|
37 | - */ |
|
38 | - $notification_manager = $this->getMockBuilder('\phpbb\notification\manager') |
|
39 | - ->disableOriginalConstructor() |
|
40 | - ->getMock(); |
|
41 | - |
|
42 | - /** |
|
43 | - * @var \florinp\messenger\libs\emojione $emojione |
|
44 | - */ |
|
45 | - $emojione = $this->getMockBuilder('\florinp\messenger\libs\emojione') |
|
46 | - ->disableOriginalConstructor() |
|
47 | - ->getMock(); |
|
48 | - |
|
49 | - /** |
|
50 | - * @var \florinp\messenger\libs\upload $upload |
|
51 | - */ |
|
52 | - $upload = $this->getMockBuilder('\florinp\messenger\libs\upload') |
|
53 | - ->disableOriginalConstructor() |
|
54 | - ->getMock(); |
|
55 | - |
|
56 | - /** |
|
57 | - * @var \florinp\messenger\libs\download $download |
|
58 | - */ |
|
59 | - $download = $this->getMockBuilder('\florinp\messenger\libs\download') |
|
60 | - ->disableOriginalConstructor() |
|
61 | - ->getMock(); |
|
62 | - |
|
63 | - $this->controller = new \florinp\messenger\controller\main( |
|
64 | - $request, |
|
65 | - $user, |
|
66 | - $model, |
|
67 | - $notification_manager, |
|
68 | - $emojione, |
|
69 | - $upload, |
|
70 | - $download |
|
71 | - ); |
|
72 | - } |
|
73 | - |
|
74 | - public function tearDown() { |
|
75 | - $this->controller = null; |
|
76 | - parent::tearDown(); |
|
77 | - } |
|
78 | - |
|
79 | - public function publish_data() { |
|
80 | - return array( |
|
81 | - array(200, json_encode(array())) |
|
82 | - ); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @dataProvider publish_data |
|
87 | - */ |
|
88 | - public function test_publish($status_code, $page_content) { |
|
89 | - $response = $this->controller->publish(); |
|
90 | - $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
|
91 | - $this->assertEquals($status_code, $response->getStatusCode()); |
|
92 | - $this->assertEquals($page_content, $response->getContent()); |
|
93 | - } |
|
94 | - |
|
95 | - public function load_data() { |
|
96 | - return array( |
|
97 | - array(200, json_encode(array( |
|
98 | - 'success' => false, |
|
99 | - 'error' => 'The request is invalid' |
|
100 | - ))) |
|
101 | - ); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @dataProvider load_data |
|
106 | - */ |
|
107 | - public function test_load($status_code, $page_content) { |
|
108 | - $response = $this->controller->load(); |
|
109 | - $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
|
110 | - $this->assertEquals($status_code, $response->getStatusCode()); |
|
111 | - $this->assertEquals($page_content, $response->getContent()); |
|
112 | - } |
|
9 | + /** |
|
10 | + * @var \florinp\messenger\controller\main |
|
11 | + */ |
|
12 | + protected $controller; |
|
13 | + |
|
14 | + public function setUp() { |
|
15 | + parent::setUp(); |
|
16 | + /** |
|
17 | + * @var \phpbb\user $user Mock the user class |
|
18 | + */ |
|
19 | + $user = $this->getMock('\phpbb\user', array(), array('\phpbb\datetime')); |
|
20 | + |
|
21 | + /** |
|
22 | + * @var \phpbb\request\request $request |
|
23 | + */ |
|
24 | + $request = $this->getMockBuilder('\phpbb\request\request') |
|
25 | + ->disableOriginalConstructor() |
|
26 | + ->getMock(); |
|
27 | + |
|
28 | + /** |
|
29 | + * @var \florinp\messenger\models\main_model $model |
|
30 | + */ |
|
31 | + $model = $this->getMockBuilder('\florinp\messenger\models\main_model') |
|
32 | + ->disableOriginalConstructor() |
|
33 | + ->getMock(); |
|
34 | + |
|
35 | + /** |
|
36 | + * @var \phpbb\notification\manager $notification_manager |
|
37 | + */ |
|
38 | + $notification_manager = $this->getMockBuilder('\phpbb\notification\manager') |
|
39 | + ->disableOriginalConstructor() |
|
40 | + ->getMock(); |
|
41 | + |
|
42 | + /** |
|
43 | + * @var \florinp\messenger\libs\emojione $emojione |
|
44 | + */ |
|
45 | + $emojione = $this->getMockBuilder('\florinp\messenger\libs\emojione') |
|
46 | + ->disableOriginalConstructor() |
|
47 | + ->getMock(); |
|
48 | + |
|
49 | + /** |
|
50 | + * @var \florinp\messenger\libs\upload $upload |
|
51 | + */ |
|
52 | + $upload = $this->getMockBuilder('\florinp\messenger\libs\upload') |
|
53 | + ->disableOriginalConstructor() |
|
54 | + ->getMock(); |
|
55 | + |
|
56 | + /** |
|
57 | + * @var \florinp\messenger\libs\download $download |
|
58 | + */ |
|
59 | + $download = $this->getMockBuilder('\florinp\messenger\libs\download') |
|
60 | + ->disableOriginalConstructor() |
|
61 | + ->getMock(); |
|
62 | + |
|
63 | + $this->controller = new \florinp\messenger\controller\main( |
|
64 | + $request, |
|
65 | + $user, |
|
66 | + $model, |
|
67 | + $notification_manager, |
|
68 | + $emojione, |
|
69 | + $upload, |
|
70 | + $download |
|
71 | + ); |
|
72 | + } |
|
73 | + |
|
74 | + public function tearDown() { |
|
75 | + $this->controller = null; |
|
76 | + parent::tearDown(); |
|
77 | + } |
|
78 | + |
|
79 | + public function publish_data() { |
|
80 | + return array( |
|
81 | + array(200, json_encode(array())) |
|
82 | + ); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @dataProvider publish_data |
|
87 | + */ |
|
88 | + public function test_publish($status_code, $page_content) { |
|
89 | + $response = $this->controller->publish(); |
|
90 | + $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
|
91 | + $this->assertEquals($status_code, $response->getStatusCode()); |
|
92 | + $this->assertEquals($page_content, $response->getContent()); |
|
93 | + } |
|
94 | + |
|
95 | + public function load_data() { |
|
96 | + return array( |
|
97 | + array(200, json_encode(array( |
|
98 | + 'success' => false, |
|
99 | + 'error' => 'The request is invalid' |
|
100 | + ))) |
|
101 | + ); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @dataProvider load_data |
|
106 | + */ |
|
107 | + public function test_load($status_code, $page_content) { |
|
108 | + $response = $this->controller->load(); |
|
109 | + $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
|
110 | + $this->assertEquals($status_code, $response->getStatusCode()); |
|
111 | + $this->assertEquals($page_content, $response->getContent()); |
|
112 | + } |
|
113 | 113 | } |
114 | 114 | \ No newline at end of file |