Skip to content

Commit 3f92a8f

Browse files
committed
Merge branch 'main' of github.com:bezumkin/sterc-csp into bezumkin-main
2 parents 3349a93 + 2cdf94d commit 3f92a8f

File tree

14 files changed

+148
-127
lines changed

14 files changed

+148
-127
lines changed

README.md

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,47 @@
11
Sterc Content Security Policy
22
---
33

4-
### How to use
4+
### Dependencies
55

6-
This is unusual extra that should be installed into MODX 3 **with composer** to resolve potential dependency conflicts.
6+
This package requires [mmxDatabase][mmx-database] to work with MODX database using Eloquent models.
77

8-
If you use MODX without `composer.json`, you need to [download this file from official MODX repository][1] into the
9-
root of your project and run `composer update`.
8+
The `mmx/database` dependency will be downloaded automatically by Composer, you need only install it inside MODX.
109

11-
### Install
10+
### Prepare
1211

13-
Just run
14-
```
15-
composer require sterc/csp --with-all-dependencies
16-
```
12+
This package can be installed only with Composer.
1713

18-
Then you need to add installed package into MODX with shipped console script
14+
If you are still not using Composer with MODX 3, just download the `composer.json` of your version:
15+
```bash
16+
cd /to/modx/root/
17+
wget https://raw.githubusercontent.com/modxcms/revolution/v3.0.4-pl/composer.json
1918
```
19+
20+
Then run `composer update` and you are ready to install this package.
21+
22+
### Install
23+
24+
```bash
25+
composer require sterc/csp
26+
composer exec mmx-database install # optional, if you haven't used Eloquent for MODX 3 before
2027
composer exec sterc-csp install
2128
```
2229

23-
This will run database migrations and register everything needed.
30+
### Update
31+
```bash
32+
composer update mmx/forms
33+
composer exec mmx-forms install
34+
```
2435

2536
### Remove
2637

2738
Almost the same commands but in reversed order:
2839
```
2940
composer exec sterc-csp remove
41+
composer exec mmx-database remove # only if you don't want to use Eloquent for MODX 3 anymore
3042
composer remove sterc/csp
3143
```
3244

3345
Custom tables will be deleted along with all other package entities.
3446

35-
[1]: https://github.com/modxcms/revolution/blob/3.x/package.json
47+
[mmx-database]: https://packagist.org/packages/mmx/database

bin/sterc-csp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env php
22
<?php
33

4-
use Sterc\CSP\Console\Console;
5-
4+
/** @var \MODX\Revolution\modX $modx */
65
require dirname(__DIR__) . '/core/bootstrap.php';
76

8-
(new Console())->run();
7+
(new Sterc\CSP\Console\Console($modx))->run();

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"bin/sterc-csp"
1818
],
1919
"require": {
20-
"php": ">=7.4",
21-
"vesp/core": "^2.8.0"
20+
"php": ">=8.1",
21+
"mmx/database": "^1.0.0",
22+
"vesp/core": "^4.0.0"
2223
}
2324
}

core/bootstrap.php

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
<?php
22

33
if (!defined('MODX_CORE_PATH')) {
4-
$dir = __DIR__;
5-
while (true) {
6-
if ($dir === '/') {
7-
break;
4+
if (file_exists('/modx/config.core.php')) {
5+
require '/modx/config.core.php';
6+
} else {
7+
$dir = __DIR__;
8+
while (true) {
9+
if ($dir === '/') {
10+
break;
11+
}
12+
if (file_exists($dir . '/config.core.php')) {
13+
require $dir . '/config.core.php';
14+
break;
15+
}
16+
if (file_exists($dir . '/config/config.inc.php')) {
17+
require $dir . '/config/config.inc.php';
18+
break;
19+
}
20+
$dir = dirname($dir);
821
}
9-
if (file_exists($dir . '/config.core.php')) {
10-
require $dir . '/config.core.php';
11-
break;
12-
}
13-
if (file_exists($dir . '/config/config.inc.php')) {
14-
require $dir . '/config/config.inc.php';
15-
break;
16-
}
17-
$dir = dirname($dir);
1822
}
19-
2023
if (!defined('MODX_CORE_PATH')) {
2124
exit('Could not load MODX core');
2225
}

core/controllers/home.class.php

+18-23
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,31 @@ public function loadCustomCssJs(): void
88
{
99
$nsKey = 'sterc-csp';
1010

11-
$server = '127.0.0.1';
12-
$port = getenv('NODE_DEV_PORT') ?: '9090';
13-
$connection = @fsockopen($server, $port);
14-
// $baseUrl = MODX_ASSETS_URL . 'components/sterc-csp/';
15-
1611
$namespace = $this->modx->getObject(modNamespace::class, ['name' => $nsKey]);
1712
$assetsPath = $namespace ? $namespace->getAssetsPath() : MODX_ASSETS_PATH . 'components/' . $nsKey . '/';
1813
$baseUrl = str_replace(MODX_BASE_PATH, '/', $assetsPath) . 'dist/';
19-
20-
if (is_resource($connection)) {
21-
// Development mode
22-
$path = $server . ':' . $port . $baseUrl;
23-
$this->addHtml('<script type="module" src="//' . $path . '@vite/client"></script>');
24-
$this->addHtml('<script type="module" src="//' . $path . 'src/main.ts"></script>');
25-
} else {
14+
$manifest = $assetsPath . 'dist/manifest.json';
15+
if (file_exists($manifest) && $files = json_decode(file_get_contents($manifest), true)) {
2616
// Production mode
27-
// $manifest = MODX_ASSETS_PATH . 'components/sterc-csp/manifest.json';
28-
$manifest = $assetsPath . 'dist/manifest.json';
29-
30-
if (file_exists($manifest) && $files = json_decode(file_get_contents($manifest), true)) {
31-
foreach ($files as $file) {
32-
if (preg_match('#\.js$#', $file['file'])) {
33-
if (!empty($file['isEntry'])) {
34-
$this->addHtml('<script type="module" src="' . $baseUrl . $file['file'] . '"></script>');
35-
}
36-
} elseif (preg_match('#\.css$#', $file['file'])) {
37-
$this->addCss($baseUrl . $file['file']);
17+
foreach ($files as $file) {
18+
if (preg_match('#\.js$#', $file['file'])) {
19+
if (!empty($file['isEntry'])) {
20+
$this->addHtml('<script type="module" src="' . $baseUrl . $file['file'] . '"></script>');
3821
}
22+
} elseif (preg_match('#\.css$#', $file['file'])) {
23+
$this->addCss($baseUrl . $file['file']);
3924
}
4025
}
26+
} else {
27+
// Development mode
28+
$server = '127.0.0.1';
29+
$port = getenv('NODE_DEV_PORT') ?: '9090';
30+
$connection = @fsockopen($server, $port);
31+
if (is_resource($connection)) {
32+
$path = $server . ':' . $port . $baseUrl;
33+
$this->addHtml('<script type="module" src="//' . $path . '@vite/client"></script>');
34+
$this->addHtml('<script type="module" src="//' . $path . 'src/main.ts"></script>');
35+
}
4136
}
4237
}
4338

core/db/20230614083947_directives.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
declare(strict_types=1);
44

5+
use Illuminate\Database\Capsule\Manager;
56
use Illuminate\Database\Schema\Blueprint;
7+
use Phinx\Migration\AbstractMigration;
68
use Sterc\CSP\Models\Directive;
79
use Sterc\CSP\Models\Group;
8-
use Vesp\Services\Migration;
910

10-
final class Directives extends Migration
11+
final class Directives extends AbstractMigration
1112
{
1213
public function up(): void
1314
{
14-
$this->schema->create(
15+
$schema = Manager::schema();
16+
$schema->create(
1517
'csp_groups',
1618
static function (Blueprint $table) {
1719
$table->id();
@@ -22,7 +24,7 @@ static function (Blueprint $table) {
2224
}
2325
);
2426

25-
$this->schema->create(
27+
$schema->create(
2628
'csp_directives',
2729
static function (Blueprint $table) {
2830
$table->foreignId('group_id')
@@ -112,15 +114,16 @@ protected function fillTable(): void
112114
Directive::query()->insert([
113115
'group_id' => $group->id,
114116
'key' => $key,
115-
'description' => $description
117+
'description' => $description,
116118
]);
117119
}
118120
}
119121
}
120122

121123
public function down(): void
122124
{
123-
$this->schema->drop('csp_directives');
124-
$this->schema->drop('csp_groups');
125+
$schema = Manager::schema();
126+
$schema->drop('csp_directives');
127+
$schema->drop('csp_groups');
125128
}
126129
}

core/phinx.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<?php
22

3-
use Sterc\CSP\Services\Eloquent;
4-
use Vesp\Services\Migration;
5-
63
/** @var MODX\Revolution\modX $modx */
74
require __DIR__ . '/bootstrap.php';
85

9-
$connection = (new Eloquent($modx))->getConnection();
6+
/** @var MMX\Database\App $app */
7+
$app = $modx->services->get('mmxDatabase');
8+
$connection = $app->getConnection();
109
$config = $connection->getConfig();
1110

11+
$pdo = $connection->getPdo();
12+
1213
return [
1314
'paths' => [
1415
'migrations' => '%%PHINX_CONFIG_DIR%%/db',
1516
],
16-
'migration_base_class' => Migration::class,
1717
'templates' => [
1818
'style' => 'up_down',
1919
],

core/src/App.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,35 @@
1111
use Sterc\CSP\Controllers\Admin\Groups;
1212
use Sterc\CSP\Middlewares\Admin;
1313
use Sterc\CSP\Models\Directive;
14-
use Vesp\Services\Eloquent;
1514

1615
class App
1716
{
1817
protected modX $modx;
1918

20-
protected const BASE_PATH = '/sterc-csp';
19+
public const NAME = 'StercCSP';
20+
public const NAMESPACE = 'sterc-csp';
2121

2222
public function __construct(modX $modx)
2323
{
2424
$this->modx = $modx;
25+
if (!$this->modx->services->has('mmxDatabase')) {
26+
$this->modx->log(modX::LOG_LEVEL_ERROR, 'Please install "mmx/database" package to use mmxForms');
27+
}
2528
}
2629

2730
public function run(): void
2831
{
2932
$container = new Container();
3033
$container->set(modX::class, $this->modx);
31-
$container->set(Eloquent::class, new Services\Eloquent($this->modx));
3234

3335
$app = Bridge::create($container);
3436
$app->addBodyParsingMiddleware();
3537
$app->addRoutingMiddleware();
36-
$app->setBasePath($this::BASE_PATH);
38+
$app->setBasePath('/' . $this::NAMESPACE);
3739
$this::setRoutes($app);
3840

3941
try {
42+
$_SERVER['QUERY_STRING'] = html_entity_decode($_SERVER['QUERY_STRING']);
4043
$app->run();
4144
} catch (\Throwable $e) {
4245
http_response_code($e->getCode());
@@ -57,8 +60,6 @@ static function (RouteCollectorProxy $group) {
5760

5861
protected function setHeaders(): void
5962
{
60-
new Services\Eloquent($this->modx);
61-
6263
$headers = [];
6364
/** @var Directive $directive */
6465
foreach (Directive::query()->where('active', true)->cursor() as $directive) {
@@ -82,7 +83,7 @@ public function handleEvent(?modSystemEvent $event): void
8283
}
8384

8485
if ($event->name === 'OnHandleRequest') {
85-
if (strpos($_SERVER['REQUEST_URI'], $this::BASE_PATH) === 0) {
86+
if (str_starts_with($_SERVER['REQUEST_URI'], '/' . $this::NAMESPACE)) {
8687
$this->run();
8788
exit();
8889
}

core/src/Console/Command/Install.php

+17-13
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ class Install extends Command
1818

1919
protected static $defaultName = 'install';
2020
protected static $defaultDescription = 'Install Sterc CSP extra for MODX 3';
21+
protected modX $modx;
2122

22-
public function run(InputInterface $input, OutputInterface $output): void
23+
public function __construct($modx, ?string $name = null)
2324
{
24-
/** @var modX $modx */
25-
global $modx;
25+
parent::__construct($name);
26+
$this->modx = $modx;
27+
}
2628

29+
public function run(InputInterface $input, OutputInterface $output): void
30+
{
2731
$srcPath = MODX_CORE_PATH . 'vendor/sterc/csp';
2832
$corePath = MODX_CORE_PATH . 'components/sterc-csp';
2933
$assetsPath = MODX_ASSETS_PATH . 'components/sterc-csp';
@@ -37,28 +41,28 @@ public function run(InputInterface $input, OutputInterface $output): void
3741
$output->writeln('<info>Created symlink for "assets"</info>');
3842
}
3943

40-
if (!$modx->getObject(modNamespace::class, ['name' => 'sterc-csp'])) {
41-
$namespace = new modNamespace($modx);
44+
if (!$this->modx->getObject(modNamespace::class, ['name' => 'sterc-csp'])) {
45+
$namespace = new modNamespace($this->modx);
4246
$namespace->name = 'sterc-csp';
4347
$namespace->path = '{core_path}components/sterc-csp/';
4448
$namespace->assets_path = '{assets_path}components/sterc-csp/';
4549
$namespace->save();
4650
$output->writeln('<info>Created namespace "sterc-csp"</info>');
4751
}
4852

49-
if (!$modx->getObject(modMenu::class, ['namespace' => 'sterc-csp', 'action' => 'home'])) {
50-
$menu = new modMenu($modx);
53+
if (!$this->modx->getObject(modMenu::class, ['namespace' => 'sterc-csp', 'action' => 'home'])) {
54+
$menu = new modMenu($this->modx);
5155
$menu->namespace = 'sterc-csp';
5256
$menu->action = 'home';
5357
$menu->parent = 'topnav';
5458
$menu->text = 'StercCSP';
55-
$menu->menuindex = $modx->getCount(modMenu::class, ['parent' => 'topnav']);
59+
$menu->menuindex = $this->modx->getCount(modMenu::class, ['parent' => 'topnav']);
5660
$menu->save();
5761
$output->writeln('<info>Created menu "StercCSP"</info>');
5862
}
5963

60-
if (!$plugin = $modx->getObject(modPlugin::class, ['name' => 'StercCSP'])) {
61-
$plugin = new modPlugin($modx);
64+
if (!$plugin = $this->modx->getObject(modPlugin::class, ['name' => 'StercCSP'])) {
65+
$plugin = new modPlugin($this->modx);
6266
$plugin->name = 'StercCSP';
6367
$plugin->plugincode = preg_replace('#^<\?php#', '', file_get_contents($corePath . '/elements/plugin.php'));
6468
$plugin->save();
@@ -71,8 +75,8 @@ public function run(InputInterface $input, OutputInterface $output): void
7175
];
7276
foreach ($pluginEvents as $name) {
7377
$key = ['pluginid' => $plugin->id, 'event' => $name];
74-
if (!$modx->getObject('modPluginEvent', $key)) {
75-
$event = new modPluginEvent($modx);
78+
if (!$this->modx->getObject('modPluginEvent', $key)) {
79+
$event = new modPluginEvent($this->modx);
7680
$event->fromArray($key, '', true, true);
7781
$event->save();
7882
$output->writeln('Added event "' . $name . '" to plugin "StercCSP"');
@@ -86,7 +90,7 @@ public function run(InputInterface $input, OutputInterface $output): void
8690
$output->writeln(explode(PHP_EOL, $res));
8791
}
8892

89-
$modx->runProcessor('system/clearcache');
93+
$this->modx->getCacheManager()->refresh();
9094
$output->writeln('<info>Cleared MODX cache</info>');
9195
}
9296
}

0 commit comments

Comments
 (0)