|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* MikoPBX - free phone system for small business |
|
4
|
|
|
* Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace MikoPBX\AdminCabinet\Controllers; |
|
21
|
|
|
|
|
22
|
|
|
use MikoPBX\Common\Providers\PBXConfModulesProvider; |
|
23
|
|
|
use MikoPBX\Modules\Config\WebUIConfigInterface; |
|
24
|
|
|
use MikoPBX\Common\Models\{PbxExtensionModules, PbxSettings}; |
|
25
|
|
|
use Phalcon\Http\ResponseInterface; |
|
26
|
|
|
use Phalcon\Mvc\{Controller, View}; |
|
27
|
|
|
use Phalcon\Tag; |
|
28
|
|
|
use Phalcon\Text; |
|
29
|
|
|
use Sentry\SentrySdk; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @property \Phalcon\Session\Manager session |
|
33
|
|
|
* @property \MikoPBX\Common\Providers\TranslationProvider translation |
|
34
|
|
|
* @property string language |
|
35
|
|
|
* @property bool showModuleStatusToggle if false it hides status toggle on current UI page |
|
36
|
|
|
* @property \MikoPBX\AdminCabinet\Library\Elements elements |
|
37
|
|
|
* @property \Phalcon\Flash\Session flash |
|
38
|
|
|
* @property \Phalcon\Tag tag |
|
39
|
|
|
* @property \Phalcon\Config\Adapter\Json config |
|
40
|
|
|
* @property \Phalcon\Logger loggerAuth |
|
41
|
|
|
*/ |
|
42
|
|
|
class BaseController extends Controller |
|
43
|
|
|
{ |
|
44
|
|
|
protected string $actionName; |
|
45
|
|
|
protected string $controllerName; |
|
46
|
|
|
protected string $controllerClass; |
|
47
|
|
|
protected string $controllerNameUnCamelized; |
|
48
|
|
|
protected bool $isExternalModuleController; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Initializes base class |
|
52
|
|
|
*/ |
|
53
|
|
|
public function initialize(): void |
|
54
|
|
|
{ |
|
55
|
|
|
$this->actionName = $this->dispatcher->getActionName(); |
|
56
|
|
|
/** @scrutinizer ignore-call */ |
|
57
|
|
|
$this->controllerClass = $this->dispatcher->getHandlerClass(); |
|
58
|
|
|
$this->controllerName = Text::camelize($this->dispatcher->getControllerName(), '_'); |
|
59
|
|
|
$this->controllerNameUnCamelized = Text::uncamelize($this->controllerName, '-'); |
|
60
|
|
|
$this->isExternalModuleController = str_starts_with($this->dispatcher->getNamespaceName(), 'Modules'); |
|
61
|
|
|
|
|
62
|
|
|
if ($this->request->isAjax() === false) { |
|
63
|
|
|
$this->prepareView(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Prepares the view by setting necessary variables and configurations. |
|
69
|
|
|
* |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function prepareView(): void |
|
73
|
|
|
{ |
|
74
|
|
|
// Set the default timezone based on PBX settings |
|
75
|
|
|
date_default_timezone_set(PbxSettings::getValueByKey('PBXTimezone')); |
|
76
|
|
|
|
|
77
|
|
|
// Set PBXLicense view variable if session exists |
|
78
|
|
|
if ($this->session->has(SessionController::SESSION_ID)) { |
|
79
|
|
|
$this->view->PBXLicense = PbxSettings::getValueByKey('PBXLicense'); |
|
80
|
|
|
} else { |
|
81
|
|
|
$this->view->PBXLicense = ''; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Set URLs for Wiki and Support based on language |
|
85
|
|
|
$this->view->urlToWiki = "https://wiki.mikopbx.com/{$this->controllerNameUnCamelized}"; |
|
86
|
|
|
if ($this->language === 'ru') { |
|
87
|
|
|
$this->view->urlToSupport = 'https://www.mikopbx.ru/support/?fromPBX=true'; |
|
88
|
|
|
} else { |
|
89
|
|
|
$this->view->urlToSupport = 'https://www.mikopbx.com/support/?fromPBX=true'; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Set the title based on the current action |
|
93
|
|
|
$title = 'MikoPBX'; |
|
94
|
|
|
switch ($this->actionName) { |
|
95
|
|
|
case'index': |
|
96
|
|
|
case'delete': |
|
97
|
|
|
case'save': |
|
98
|
|
|
case'modify': |
|
99
|
|
|
case'*** WITHOUT ACTION ***': |
|
100
|
|
|
$title .= '|' . $this->translation->_("Breadcrumb{$this->controllerName}"); |
|
101
|
|
|
break; |
|
102
|
|
|
default: |
|
103
|
|
|
$title .= '|' . $this->translation->_("Breadcrumb{$this->controllerName}{$this->actionName}"); |
|
104
|
|
|
} |
|
105
|
|
|
Tag::setTitle($title); |
|
106
|
|
|
|
|
107
|
|
|
// Set other view variables |
|
108
|
|
|
$this->view->t = $this->translation; |
|
109
|
|
|
$this->view->debugMode = $this->config->path('adminApplication.debugMode'); |
|
110
|
|
|
$this->view->urlToLogo = $this->url->get('assets/img/logo-mikopbx.svg'); |
|
111
|
|
|
$this->view->urlToController = $this->url->get($this->controllerNameUnCamelized); |
|
112
|
|
|
$this->view->represent = ''; |
|
113
|
|
|
$this->view->WebAdminLanguage = $this->session->get(LanguageController::WEB_ADMIN_LANGUAGE)??PbxSettings::getValueByKey('WebAdminLanguage'); |
|
114
|
|
|
$this->view->AvailableLanguages = json_encode(LanguageController::getAvailableWebAdminLanguages()); |
|
115
|
|
|
$this->view->submitMode = $this->session->get('SubmitMode') ?? 'SaveSettings'; |
|
116
|
|
|
$this->view->lastSentryEventId = $this->setLastSentryEventId(); |
|
117
|
|
|
$this->view->PBXVersion = PbxSettings::getValueByKey('PBXVersion'); |
|
118
|
|
|
$this->view->MetaTegHeadDescription = $this->translation->_('MetaTegHeadDescription'); |
|
119
|
|
|
$this->view->isExternalModuleController = $this->isExternalModuleController; |
|
120
|
|
|
|
|
121
|
|
|
if ($this->controllerClass!==SessionController::class){ |
|
122
|
|
|
$this->view->setTemplateAfter('main'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->view->globalModuleUniqueId = ''; |
|
126
|
|
|
$this->view->actionName = $this->actionName; |
|
127
|
|
|
$this->view->controllerName = $this->controllerName; |
|
128
|
|
|
$this->view->controllerClass = $this->controllerClass; |
|
129
|
|
|
|
|
130
|
|
|
// Add module variables into view if it is an external module controller |
|
131
|
|
|
if ($this->isExternalModuleController) { |
|
132
|
|
|
/** @var PbxExtensionModules $module */ |
|
133
|
|
|
$module = PbxExtensionModules::findFirstByUniqid($this->getModuleUniqueId()); |
|
134
|
|
|
if ($module === null) { |
|
135
|
|
|
$module = new PbxExtensionModules(); |
|
136
|
|
|
$module->disabled = '1'; |
|
137
|
|
|
$module->name = 'Unknown module'; |
|
138
|
|
|
} |
|
139
|
|
|
$this->view->module = $module->toArray(); |
|
140
|
|
|
$this->view->globalModuleUniqueId = $module->uniqid; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Performs actions before executing the route. |
|
146
|
|
|
* |
|
147
|
|
|
* @return void |
|
148
|
|
|
*/ |
|
149
|
|
|
public function beforeExecuteRoute(): void |
|
150
|
|
|
{ |
|
151
|
|
|
// Check if the request method is POST |
|
152
|
|
|
if ($this->request->isPost()) { |
|
153
|
|
|
// Retrieve the 'submitMode' data from the request |
|
154
|
|
|
$data = $this->request->getPost('submitMode'); |
|
155
|
|
|
if (!empty($data)) { |
|
156
|
|
|
// Set the 'SubmitMode' session variable to the retrieved data |
|
157
|
|
|
$this->session->set('SubmitMode', $data); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$this->actionName = $this->dispatcher->getActionName(); |
|
162
|
|
|
$this->controllerName = Text::camelize($this->dispatcher->getControllerName(), '_'); |
|
163
|
|
|
// Add module variables into view if it is an external module controller |
|
164
|
|
|
if (str_starts_with($this->dispatcher->getNamespaceName(), 'Modules')) { |
|
165
|
|
|
$this->view->pick("Modules/{$this->getModuleUniqueId()}/{$this->controllerName}/{$this->actionName}"); |
|
166
|
|
|
} else { |
|
167
|
|
|
$this->view->pick("{$this->controllerName}/{$this->actionName}"); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
PBXConfModulesProvider::hookModulesMethod(WebUIConfigInterface::ON_BEFORE_EXECUTE_ROUTE,[$this]); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Performs actions after executing the route and returns the response. |
|
175
|
|
|
* |
|
176
|
|
|
* @return \Phalcon\Http\ResponseInterface |
|
177
|
|
|
*/ |
|
178
|
|
|
public function afterExecuteRoute(): ResponseInterface |
|
179
|
|
|
{ |
|
180
|
|
|
|
|
181
|
|
|
if ($this->request->isAjax() === true) { |
|
182
|
|
|
$this->view->setRenderLevel(View::LEVEL_NO_RENDER); |
|
183
|
|
|
$this->response->setContentType('application/json', 'UTF-8'); |
|
184
|
|
|
$data = $this->view->getParamsToView(); |
|
185
|
|
|
|
|
186
|
|
|
/* Set global params if is not set in controller/action */ |
|
187
|
|
|
if (isset($data['raw_response'])) { |
|
188
|
|
|
$result = $data['raw_response']; |
|
189
|
|
|
} else { |
|
190
|
|
|
$data['success'] = $data['success'] ?? true; |
|
191
|
|
|
$data['reload'] = $data['reload'] ?? false; |
|
192
|
|
|
$data['message'] = $data['message'] ?? $this->flash->getMessages(); |
|
193
|
|
|
|
|
194
|
|
|
// Let's add information about the last error to display a dialog window for the user. |
|
195
|
|
|
if (file_exists('/etc/sendmetrics')) { |
|
196
|
|
|
$data['lastSentryEventId'] = SentrySdk::getCurrentHub()->getLastEventId(); |
|
197
|
|
|
} |
|
198
|
|
|
$result = json_encode($data); |
|
199
|
|
|
} |
|
200
|
|
|
$this->response->setContent($result); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
PBXConfModulesProvider::hookModulesMethod(WebUIConfigInterface::ON_AFTER_EXECUTE_ROUTE,[$this]); |
|
204
|
|
|
|
|
205
|
|
|
return $this->response->send(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Forwards the request to a different controller and action based on the provided URI. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $uri The URI to forward to. |
|
212
|
|
|
* @return void |
|
213
|
|
|
*/ |
|
214
|
|
|
protected function forward(string $uri): void |
|
215
|
|
|
{ |
|
216
|
|
|
$uriParts = explode('/', $uri); |
|
217
|
|
|
if ($this->isExternalModuleController and count($uriParts)>2){ |
|
218
|
|
|
$params = array_slice($uriParts, 3); |
|
219
|
|
|
$moduleUniqueID = $this->getModuleUniqueId(); |
|
220
|
|
|
$this->dispatcher->forward( |
|
221
|
|
|
[ |
|
222
|
|
|
'namespace'=>"Modules\\{$moduleUniqueID}\\App\\Controllers", |
|
223
|
|
|
'controller' => $uriParts[1], |
|
224
|
|
|
'action' => $uriParts[2], |
|
225
|
|
|
'params' => $params, |
|
226
|
|
|
] |
|
227
|
|
|
); |
|
228
|
|
|
} else { |
|
229
|
|
|
$params = array_slice($uriParts, 2); |
|
230
|
|
|
|
|
231
|
|
|
$this->dispatcher->forward( |
|
232
|
|
|
[ |
|
233
|
|
|
'namespace'=>"MikoPBX\AdminCabinet\Controllers", |
|
234
|
|
|
'controller' => $uriParts[0], |
|
235
|
|
|
'action' => $uriParts[1], |
|
236
|
|
|
'params' => $params, |
|
237
|
|
|
] |
|
238
|
|
|
); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Sanitizes the caller ID by removing any characters that are not alphanumeric or spaces. |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $callerId The caller ID to sanitize. |
|
246
|
|
|
* @return string The sanitized caller ID. |
|
247
|
|
|
*/ |
|
248
|
|
|
protected function sanitizeCallerId(string $callerId): string |
|
249
|
|
|
{ |
|
250
|
|
|
return preg_replace('/[^a-zA-Zа-яА-Я0-9 ]/ui', '', $callerId); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Sorts array by priority field |
|
255
|
|
|
* |
|
256
|
|
|
* @param $a |
|
257
|
|
|
* @param $b |
|
258
|
|
|
* |
|
259
|
|
|
* @return int|null |
|
260
|
|
|
*/ |
|
261
|
|
|
protected function sortArrayByPriority($a, $b): ?int |
|
262
|
|
|
{ |
|
263
|
|
|
if (is_array($a)) { |
|
264
|
|
|
$a = (int)$a['priority']; |
|
265
|
|
|
} else { |
|
266
|
|
|
$a = (int)$a->priority; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
if (is_array($b)) { |
|
270
|
|
|
$b = (int)$b['priority']; |
|
271
|
|
|
} else { |
|
272
|
|
|
$b = (int)$b->priority; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
if ($a === $b) { |
|
276
|
|
|
return 0; |
|
277
|
|
|
} else { |
|
278
|
|
|
return ($a < $b) ? -1 : 1; |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Sets the last Sentry event ID. |
|
284
|
|
|
* |
|
285
|
|
|
* @return \Sentry\EventId|null The last Sentry event ID, or null if metrics sending is disabled. |
|
286
|
|
|
*/ |
|
287
|
|
|
private function setLastSentryEventId(): ?\Sentry\EventId |
|
288
|
|
|
{ |
|
289
|
|
|
$result = null; |
|
290
|
|
|
// Allow anonymous statistics collection for JS code |
|
291
|
|
|
if (PbxSettings::getValueByKey('SendMetrics') === '1') { |
|
292
|
|
|
touch('/etc/sendmetrics'); |
|
293
|
|
|
$result = SentrySdk::getCurrentHub()->getLastEventId(); |
|
294
|
|
|
} elseif (file_exists('/etc/sendmetrics')) { |
|
295
|
|
|
unlink('/etc/sendmetrics'); |
|
296
|
|
|
} |
|
297
|
|
|
return $result; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Returns the unique ID of the module parsing controller namespace; |
|
302
|
|
|
* @return string |
|
303
|
|
|
*/ |
|
304
|
|
|
private function getModuleUniqueId():string |
|
305
|
|
|
{ |
|
306
|
|
|
// Split the namespace into an array using the backslash as a separator |
|
307
|
|
|
$parts = explode('\\', get_class($this)); |
|
308
|
|
|
|
|
309
|
|
|
// Get the second part of the namespace |
|
310
|
|
|
return $parts[1]; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Save an entity and handle success or error messages. |
|
315
|
|
|
* |
|
316
|
|
|
* @param mixed $entity The entity to be saved. |
|
317
|
|
|
* @return bool True if the entity was successfully saved, false otherwise. |
|
318
|
|
|
*/ |
|
319
|
|
|
protected function saveEntity($entity, string $reloadPath=''): bool |
|
320
|
|
|
{ |
|
321
|
|
|
$success = $entity->save(); |
|
322
|
|
|
|
|
323
|
|
|
if (!$success) { |
|
324
|
|
|
$errors = $entity->getMessages(); |
|
325
|
|
|
$this->flash->error(implode('<br>', $errors)); |
|
326
|
|
|
} elseif (!$this->request->isAjax()) { |
|
327
|
|
|
$this->flash->success($this->translation->_('ms_SuccessfulSaved')); |
|
328
|
|
|
if ($reloadPath!==''){ |
|
329
|
|
|
$this->forward($reloadPath); |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
if ($this->request->isAjax()) { |
|
334
|
|
|
$this->view->success = $success; |
|
335
|
|
|
if ($reloadPath!=='' && $success){ |
|
336
|
|
|
$this->view->reload = $reloadPath; |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
return $success; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* Delete an entity and handle success or error messages. |
|
346
|
|
|
* |
|
347
|
|
|
* @param mixed $entity The entity to be deleted. |
|
348
|
|
|
* @return bool True if the entity was successfully deleted, false otherwise. |
|
349
|
|
|
*/ |
|
350
|
|
|
protected function deleteEntity($entity, string $reloadPath=''): bool |
|
351
|
|
|
{ |
|
352
|
|
|
$success = $entity->delete(); |
|
353
|
|
|
|
|
354
|
|
|
if (!$success) { |
|
355
|
|
|
$errors = $entity->getMessages(); |
|
356
|
|
|
$this->flash->error(implode('<br>', $errors)); |
|
357
|
|
|
} elseif (!$this->request->isAjax()) { |
|
358
|
|
|
// $this->flash->success($this->translation->_('ms_SuccessfulSaved')); |
|
359
|
|
|
if ($reloadPath!==''){ |
|
360
|
|
|
$this->forward($reloadPath); |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
if ($this->request->isAjax()) { |
|
365
|
|
|
$this->view->success = $success; |
|
366
|
|
|
if ($reloadPath!=='' && $success){ |
|
367
|
|
|
$this->view->reload = $reloadPath; |
|
368
|
|
|
} |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
return $success; |
|
372
|
|
|
} |
|
373
|
|
|
} |
|
374
|
|
|
|