Skip to content

Commit 733f2a5

Browse files
committed
routing for modules
1 parent 83de57c commit 733f2a5

File tree

14 files changed

+2722
-2516
lines changed

14 files changed

+2722
-2516
lines changed

core/config/app.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@
116116

117117
'middleware' => [
118118

119+
'mgr' => [
120+
EvolutionCMS\Middleware\VerifyCsrfToken::class,
121+
EvolutionCMS\Middleware\Manager::class,
122+
],
123+
119124
/*
120125
|--------------------------------------------------------------------------
121126
| The application's global HTTP middleware stack

core/src/Controllers/Actions.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace EvolutionCMS\Controllers;
4+
5+
use EvolutionCMS\Interfaces\ManagerTheme;
6+
use Illuminate\Http\Response;
7+
8+
class Actions extends AbstractController implements ManagerTheme\PageControllerInterface
9+
{
10+
public function handleAction()
11+
{
12+
global $action;
13+
// Update last action in table active_users
14+
$action = $this->managerTheme->getActionId();
15+
16+
$output = '';
17+
18+
if ($action === null) {
19+
$_style = $this->managerTheme->getStyle();
20+
// first we check to see if this is a frameset request
21+
if (!isset($_POST['updateMsgCount'])) {
22+
\EvolutionCMS\Tracy\Debugger::$showBar = false;
23+
// this looks to be a top-level frameset request, so let's serve up a frameset
24+
$output = $this->managerTheme->handle(1, ['frame' => 1]);
25+
}
26+
} else {
27+
$output = $this->managerTheme->handle($action);
28+
}
29+
30+
if ($output instanceof Response) {
31+
return $output;
32+
}
33+
34+
$isRedirect = array_reduce(headers_list(), function($result, $header) {
35+
return strpos($header, 'Location') === 0;
36+
}, 0);
37+
38+
return response()->make($output)->setStatusCode($isRedirect ? 302 : 200);
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function checkLocked(): ?string
45+
{
46+
return null;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function canView(): bool
53+
{
54+
return true;
55+
}
56+
57+
public function process() : bool
58+
{
59+
return true;
60+
}
61+
}

core/src/Controllers/Frame.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ protected function menuRunModules()
634634
'module' . $module['id'],
635635
'modules',
636636
($module['icon'] != '' ? '<i class="' . $module['icon'] . '"></i>' : '<i class="' . $this->managerTheme->getStyle('icon_module') . '"></i>') . $module['name'],
637-
'index.php?a=112&id=' . $module['id'],
637+
!empty($module['properties']['routes']) ? 'modules/' . $module['id'] . '/' : 'index.php?a=112&id=' . $module['id'],
638638
$module['name'],
639639
'',
640640
'',

core/src/Core.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Illuminate\Http\Request;
1818
use Illuminate\Support\Arr;
1919
use Illuminate\Support\Facades\Cache;
20+
use Illuminate\Support\Facades\Route;
2021
use Illuminate\Support\Str;
2122
use PHPMailer\PHPMailer\Exception;
2223
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -6571,6 +6572,43 @@ public function getDataForView()
65716572
*/
65726573
public function registerModule($name, $file, $icon = 'fa fa-cube', $params = [])
65736574
{
6574-
$this->modulesFromFile[md5($name)] = ['id' => md5($name), 'name' => $name, 'file' => $file, 'icon' => $icon, 'properties' => $params];
6575+
if (!$this->isBackend()) {
6576+
return false;
6577+
}
6578+
6579+
$module_id = md5($name);
6580+
6581+
$this->modulesFromFile[$module_id] = [
6582+
'id' => $module_id,
6583+
'name' => $name,
6584+
'file' => $file,
6585+
'icon' => $icon,
6586+
'properties' => $params,
6587+
];
6588+
6589+
return $module_id;
6590+
}
6591+
6592+
/**
6593+
* @param string $name
6594+
* @param string $file
6595+
* @param string $icon
6596+
* @param array $params
6597+
*/
6598+
public function registerRoutingModule($name, $file, $icon = 'fa fa-cube')
6599+
{
6600+
$params = [
6601+
'routes' => $file,
6602+
];
6603+
6604+
if ($module_id = $this->registerModule($name, $file, $icon, $params)) {
6605+
Route::middleware('mgr')
6606+
->prefix('modules/' . $module_id)
6607+
->group($file);
6608+
6609+
return $module_id;
6610+
}
6611+
6612+
return false;
65756613
}
65766614
}

core/src/ManagerTheme.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use EvolutionCMS\Interfaces\CoreInterface;
1111
use EvolutionCMS\Models\ActiveUser;
1212
use EvolutionCMS\Models\UserAttribute;
13+
use Illuminate\Http\Request;
14+
use Illuminate\Support\Facades\Route;
1315
use View;
1416

1517
class ManagerTheme implements ManagerThemeInterface
@@ -371,6 +373,35 @@ public function findController($action)
371373
return $action === null ? null : get_by_key($this->actions, $action, $action);
372374
}
373375

376+
public function handleRoute()
377+
{
378+
$evo = $this->getCore();
379+
380+
$request = Request::createFromGlobals();
381+
$evo->instance(Request::class, $request);
382+
$evo->alias(Request::class, 'request');
383+
384+
$middleware = config('app.middleware.mgr', []);
385+
$evo->router->middlewareGroup('mgr', $middleware);
386+
387+
$aliases = config('app.middleware.aliases', []);
388+
389+
foreach ($aliases as $key => $class) {
390+
$evo->router->aliasMiddleware($key, $class);
391+
}
392+
393+
Route::middleware('mgr')
394+
->namespace('\\EvolutionCMS\\Controllers')
395+
->group(MODX_MANAGER_PATH . '/routes.php');
396+
397+
$routes = $evo->router->getRoutes();
398+
$routes->refreshNameLookups();
399+
$routes->refreshActionLookups();
400+
401+
$response = $evo->router->dispatch($request);
402+
$response->send();
403+
}
404+
374405
public function handle($action, array $data = [])
375406
{
376407
$this->saveAction($action);
@@ -611,7 +642,7 @@ public function getTemplatePlaceholders(): array
611642
}else{
612643
$plh['login_bg'] = MODX_SITE_URL . $background;
613644
}
614-
645+
615646
$plh['login_bg'] = MODX_SITE_URL . $background;
616647
} else {
617648
$plh['login_bg'] = $this->getThemeUrl() . 'images/login/default/login-background.jpg';

core/src/Middleware/Manager.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace EvolutionCMS\Middleware;
4+
5+
use Closure, ManagerTheme;
6+
7+
class Manager
8+
{
9+
public function handle($request, Closure $next)
10+
{
11+
// Update last action in table active_users
12+
global $action;
13+
$action = ManagerTheme::getActionId();
14+
15+
// accesscontrol.php checks to see if the user is logged in. If not, a log in form is shown
16+
if (0 !== $action && ManagerTheme::isAuthManager() === false) {
17+
return ManagerTheme::renderLoginPage();
18+
}
19+
20+
// Ignore Logout and LogIn action
21+
if (8 !== $action && 0 !== $action && ManagerTheme::hasManagerAccess() === false) {
22+
return ManagerTheme::renderAccessPage();
23+
}
24+
25+
return $next($request);
26+
}
27+
}

core/src/Providers/ManagerThemeServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use EvolutionCMS\ServiceProvider;
44
use EvolutionCMS\ManagerTheme;
5+
use EvolutionCMS\Interfaces\ManagerThemeInterface;
56

67
class ManagerThemeServiceProvider extends ServiceProvider
78
{
@@ -33,6 +34,9 @@ public function register()
3334
);
3435
return new ManagerTheme($app, $theme);
3536
});
37+
38+
$this->app->alias('ManagerTheme', ManagerThemeInterface::class);
39+
$this->app->alias('ManagerTheme', ManagerTheme::class);
3640
}
3741

3842
/**

core/src/Providers/RoutingServiceProvider.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ public function register()
1616
{
1717
parent::register();
1818

19-
if (is_readable(EVO_CORE_PATH . 'custom/routes.php')) {
20-
Route::middleware('web')->group(EVO_CORE_PATH . 'custom/routes.php');
21-
}
19+
if ($this->app->isFrontend() || is_cli()) {
20+
if (is_readable(EVO_CORE_PATH . 'custom/routes.php')) {
21+
Route::middleware('web')->group(EVO_CORE_PATH . 'custom/routes.php');
22+
}
2223

23-
Route::fallbackToParser();
24+
Route::fallbackToParser();
25+
}
2426
}
2527

2628
/**

manager/ht.access renamed to manager/.htaccess

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# For unexpected logouts in multi-server/cloud environments see:
22
# http://rtfm.modx.com/evolution/1.0/administration/friendly-url-solutions
33

4-
# No redirect
5-
RewriteEngine Off
4+
RewriteEngine On
5+
RewriteBase /manager/
66

7-
# Redirect http to https
8-
# RewriteEngine On
9-
# RewriteCond %{HTTPS} off
10-
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
7+
RewriteCond %{REQUEST_FILENAME} !-f
8+
RewriteCond %{REQUEST_FILENAME} !-d
9+
RewriteRule ^(.*)$ index.php [L,QSA]
1110

1211
<IfModule mod_security.c>
1312
# Turn the filtering engine Off

manager/index.php

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
define('MODX_API_MODE', false);
7070
}
7171

72+
if (! defined('IN_PARSER_MODE')) {
73+
define('IN_PARSER_MODE', false);
74+
}
75+
7276
if (file_exists(__DIR__ . '/config.php')) {
7377
$config = require __DIR__ . '/config.php';
7478
} elseif (file_exists(dirname(__DIR__) . '/config.php')) {
@@ -150,36 +154,9 @@
150154
// send the charset header
151155
header('Content-Type: text/html; charset=' . ManagerTheme::getCharset());
152156

153-
// Update last action in table active_users
154-
$action = ManagerTheme::getActionId();
155-
156-
// accesscontrol.php checks to see if the user is logged in. If not, a log in form is shown
157-
if (0 !== $action && ManagerTheme::isAuthManager() === false) {
158-
echo ManagerTheme::renderLoginPage();
159-
exit;
160-
}
161-
162-
/** Ignore Logout and LogIn action */
163-
if (8 !== $action && 0 !== $action && ManagerTheme::hasManagerAccess() === false) {
164-
echo ManagerTheme::renderAccessPage();
165-
exit;
166-
}
157+
$action = 0;
167158

168159
// Update table active_user_sessions
169160
$modx->updateValidatedUserSession();
170161

171-
$output = '';
172-
173-
if ($action === null) {
174-
$_style = ManagerTheme::getStyle();
175-
// first we check to see if this is a frameset request
176-
if (!isset($_POST['updateMsgCount'])) {
177-
EvolutionCMS\Tracy\Debugger::$showBar = false;
178-
// this looks to be a top-level frameset request, so let's serve up a frameset
179-
$output = ManagerTheme::handle(1, ['frame' => 1]);
180-
}
181-
} else {
182-
$output = ManagerTheme::handle($action);
183-
}
184-
185-
echo $output;
162+
ManagerTheme::handleRoute();

0 commit comments

Comments
 (0)