|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* MikoPBX - free phone system for small business |
|
4
|
|
|
* Copyright (C) 2017-2020 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
|
|
|
declare(strict_types=1); |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
namespace MikoPBX\PBXCoreREST\Providers; |
|
24
|
|
|
|
|
25
|
|
|
use MikoPBX\PBXCoreREST\Controllers\{Cdr\GetController as CdrGetController, |
|
26
|
|
|
Iax\GetController as IaxGetController, |
|
27
|
|
|
Modules\ModulesControllerBase, |
|
28
|
|
|
Sip\GetController as SipGetController, |
|
29
|
|
|
Sip\PostController as SipPostController, |
|
30
|
|
|
Storage\GetController as StorageGetController, |
|
31
|
|
|
Storage\PostController as StoragePostController, |
|
32
|
|
|
Syslog\GetController as SyslogGetController, |
|
33
|
|
|
Syslog\PostController as SyslogPostController, |
|
34
|
|
|
Sysinfo\GetController as SysinfoGetController, |
|
35
|
|
|
Sysinfo\PostController as SysinfoPostController, |
|
36
|
|
|
System\GetController as SystemGetController, |
|
37
|
|
|
System\PostController as SystemPostController, |
|
38
|
|
|
Files\GetController as FilesGetController, |
|
39
|
|
|
Files\PostController as FilesPostController, |
|
40
|
|
|
Advices\GetController as AdvicesGetController, |
|
41
|
|
|
License\GetController as LicenseGetController, |
|
42
|
|
|
License\PostController as LicensePostController |
|
43
|
|
|
}; |
|
44
|
|
|
use MikoPBX\Modules\Config\ConfigClass; |
|
45
|
|
|
use MikoPBX\PBXCoreREST\Middleware\AuthenticationMiddleware; |
|
46
|
|
|
use MikoPBX\PBXCoreREST\Middleware\NotFoundMiddleware; |
|
47
|
|
|
use MikoPBX\PBXCoreREST\Middleware\ResponseMiddleware; |
|
48
|
|
|
use Phalcon\Di\DiInterface; |
|
49
|
|
|
use Phalcon\Di\ServiceProviderInterface; |
|
50
|
|
|
use Phalcon\Events\Manager; |
|
51
|
|
|
use Phalcon\Mvc\Micro; |
|
52
|
|
|
use Phalcon\Mvc\Micro\Collection; |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Register Router service |
|
57
|
|
|
*/ |
|
58
|
|
|
class RouterProvider implements ServiceProviderInterface |
|
59
|
|
|
{ |
|
60
|
|
|
public const SERVICE_NAME = ''; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Register response service provider |
|
64
|
|
|
* |
|
65
|
|
|
* @param \Phalcon\Di\DiInterface $di |
|
66
|
|
|
*/ |
|
67
|
|
|
public function register(DiInterface $di): void |
|
68
|
|
|
{ |
|
69
|
|
|
/** @var Micro $application */ |
|
70
|
|
|
$application = $di->getShared('application'); |
|
71
|
|
|
/** @var Manager $eventsManager */ |
|
72
|
|
|
$eventsManager = $di->getShared('eventsManager'); |
|
73
|
|
|
|
|
74
|
|
|
$this->attachRoutes($application); |
|
75
|
|
|
$this->attachMiddleware($application, $eventsManager); |
|
76
|
|
|
|
|
77
|
|
|
$application->setEventsManager($eventsManager); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Attaches the routes to the application; lazy loaded |
|
82
|
|
|
* |
|
83
|
|
|
* @param Micro $application |
|
84
|
|
|
*/ |
|
85
|
|
|
private function attachRoutes(Micro $application): void |
|
86
|
|
|
{ |
|
87
|
|
|
// Add hard coded routes |
|
88
|
|
|
$routes = $this->getRoutes(); |
|
89
|
|
|
|
|
90
|
|
|
// Add additional modules routes |
|
91
|
|
|
$configClassObj = new ConfigClass(); |
|
92
|
|
|
$additionalRoutes = $configClassObj->hookModulesMethodWithArrayResult(ConfigClass::GET_PBXCORE_REST_ADDITIONAL_ROUTES); |
|
93
|
|
|
$additionalRoutes = array_values($additionalRoutes); |
|
94
|
|
|
$routes = array_merge($routes, ...$additionalRoutes); |
|
95
|
|
|
|
|
96
|
|
|
// Class, Method, Route, Handler, ParamsRegex |
|
97
|
|
|
foreach ($routes as $route) { |
|
98
|
|
|
$collection = new Collection(); |
|
99
|
|
|
$collection |
|
100
|
|
|
->setHandler($route[0], true) |
|
101
|
|
|
->setPrefix($route[2]) |
|
102
|
|
|
->{$route[3]}( |
|
103
|
|
|
$route[4], |
|
104
|
|
|
$route[1] |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
$application->mount($collection); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Returns the array for the routes |
|
113
|
|
|
* |
|
114
|
|
|
* @return array |
|
115
|
|
|
*/ |
|
116
|
|
|
private function getRoutes(): array |
|
117
|
|
|
{ |
|
118
|
|
|
return [ |
|
119
|
|
|
// Class, Method, Route, Handler, ParamsRegex |
|
120
|
|
|
|
|
121
|
|
|
[SipGetController::class, 'callAction', '/pbxcore/api/sip/{actionName}', 'get', '/'], |
|
122
|
|
|
[SipPostController::class, 'callAction', '/pbxcore/api/sip/{actionName}', 'post', '/'], |
|
123
|
|
|
|
|
124
|
|
|
[IaxGetController::class, 'callAction', '/pbxcore/api/iax/{actionName}', 'get', '/'], |
|
125
|
|
|
|
|
126
|
|
|
[CdrGetController::class, 'callAction', '/pbxcore/api/cdr/{actionName}', 'get', '/'], |
|
127
|
|
|
[CdrGetController::class, 'playbackAction', '/pbxcore/api/cdr/playback', 'get', '/'], |
|
128
|
|
|
|
|
129
|
|
|
[StorageGetController::class, 'callAction', '/pbxcore/api/storage/{actionName}', 'get', '/'], |
|
130
|
|
|
[StoragePostController::class, 'callAction', '/pbxcore/api/storage/{actionName}', 'post', '/'], |
|
131
|
|
|
|
|
132
|
|
|
[SystemGetController::class, 'callAction', '/pbxcore/api/system/{actionName}', 'get', '/'], |
|
133
|
|
|
[SystemPostController::class, 'callAction', '/pbxcore/api/system/{actionName}', 'post', '/'], |
|
134
|
|
|
|
|
135
|
|
|
[SyslogGetController::class, 'callAction', '/pbxcore/api/syslog/{actionName}', 'get', '/'], |
|
136
|
|
|
[SyslogPostController::class, 'callAction', '/pbxcore/api/syslog/{actionName}', 'post', '/'], |
|
137
|
|
|
|
|
138
|
|
|
[SysinfoGetController::class, 'callAction', '/pbxcore/api/sysinfo/{actionName}', 'get', '/'], |
|
139
|
|
|
[SysinfoPostController::class, 'callAction', '/pbxcore/api/sysinfo/{actionName}', 'post', '/'], |
|
140
|
|
|
|
|
141
|
|
|
[FilesGetController::class, 'callAction', '/pbxcore/api/files/{actionName}', 'get', '/'], |
|
142
|
|
|
[FilesPostController::class, 'callAction', '/pbxcore/api/files/{actionName}', 'post', '/'], |
|
143
|
|
|
|
|
144
|
|
|
[AdvicesGetController::class, 'callAction', '/pbxcore/api/advices/{actionName}', 'get', '/'], |
|
145
|
|
|
|
|
146
|
|
|
[LicenseGetController::class, 'callAction', '/pbxcore/api/license/{actionName}', 'get', '/'], |
|
147
|
|
|
[LicensePostController::class, 'callAction', '/pbxcore/api/license/{actionName}', 'post', '/'], |
|
148
|
|
|
|
|
149
|
|
|
[ |
|
150
|
|
|
ModulesControllerBase::class, |
|
151
|
|
|
'callActionForModule', |
|
152
|
|
|
'/pbxcore/api/modules/{moduleName}/{actionName}', |
|
153
|
|
|
'get', |
|
154
|
|
|
'/', |
|
155
|
|
|
], |
|
156
|
|
|
[ |
|
157
|
|
|
ModulesControllerBase::class, |
|
158
|
|
|
'callActionForModule', |
|
159
|
|
|
'/pbxcore/api/modules/{moduleName}/{actionName}', |
|
160
|
|
|
'post', |
|
161
|
|
|
'/', |
|
162
|
|
|
], |
|
163
|
|
|
]; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Attaches the middleware to the application |
|
168
|
|
|
* |
|
169
|
|
|
* @param Micro $application |
|
170
|
|
|
* @param Manager $eventsManager |
|
171
|
|
|
*/ |
|
172
|
|
|
private function attachMiddleware(Micro $application, Manager $eventsManager): void |
|
173
|
|
|
{ |
|
174
|
|
|
$middleware = $this->getMiddleware(); |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get the events manager and attach the middleware to it |
|
178
|
|
|
*/ |
|
179
|
|
|
foreach ($middleware as $class => $function) { |
|
180
|
|
|
$eventsManager->attach('micro', new $class()); |
|
181
|
|
|
$application->{$function}(new $class()); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Returns the array for the middleware with the action to attach |
|
187
|
|
|
* |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
|
|
private function getMiddleware(): array |
|
191
|
|
|
{ |
|
192
|
|
|
return [ |
|
193
|
|
|
NotFoundMiddleware::class => 'before', |
|
194
|
|
|
AuthenticationMiddleware::class => 'before', |
|
195
|
|
|
ResponseMiddleware::class => 'after', |
|
196
|
|
|
]; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
} |