Parcourir la source

Add config for module Test

Now in the configuration you can set external form referrers.
Use a translator to read the comments in the test.default.php file.
Visman il y a 1 an
Parent
commit
f2c9679e30
4 fichiers modifiés avec 85 ajouts et 3 suppressions
  1. 39 2
      app/Core/Test.php
  2. 20 0
      app/Models/Pages/Admin/Update.php
  3. 4 1
      app/config/main.dist.php
  4. 22 0
      app/config/test.default.php

+ 39 - 2
app/Core/Test.php

@@ -15,8 +15,19 @@ use ForkBB\Core\Validator;
 
 class Test
 {
-    public function __construct(protected Container $c)
+    protected array $config = [];
+
+    public function __construct(string $path, protected Container $c)
     {
+        if (! \is_file($path)) {
+            throw new RuntimeException('File not found');
+        }
+
+        if (! \is_readable($path)) {
+            throw new RuntimeException('File can not be read');
+        }
+
+        $this->config = include $path;
     }
 
     public function beforeValidation(Validator $v): Validator
@@ -86,7 +97,33 @@ class Test
             $ref = \strstr($ref, '#', true) ?: $ref;
 
             if ($ref !== $_SERVER['HTTP_REFERER']) {
-                $index += 3;
+                $inc = 3;
+
+                if (
+                    ! empty($this->config['referers'])
+                    && \is_array($this->config['referers'])
+                ) {
+                    foreach ($this->config['referers'] as $ref) {
+                        if (false === \strpos($ref, '*')) {
+                            if ($ref === $_SERVER['HTTP_REFERER']) {
+                                $inc = 0;
+
+                                break;
+                            }
+                        } else {
+                            $ref = \preg_quote($ref, '%');
+                            $ref = \str_replace('\\*', '.*?', $ref);
+
+                            if (\preg_match("%^{$ref}$%D", $_SERVER['HTTP_REFERER'])) {
+                                $inc = 0;
+
+                                break;
+                            }
+                        }
+                    }
+                }
+
+                $index += $inc;
             }
         }
 

+ 20 - 0
app/Models/Pages/Admin/Update.php

@@ -831,4 +831,24 @@ class Update extends Admin
 
         return null;
     }
+
+    /**
+     * rev.65 to rev.66
+     */
+    protected function stageNumber65(array $args): ?int
+    {
+        $coreConfig = new CoreConfig($this->configFile);
+
+        $coreConfig->add(
+            'shared=>Test',
+            [
+                'class'  => '\\ForkBB\\Core\\Test::class',
+                'config' => '\'%DIR_CONFIG%/test.default.php\'',
+            ]
+        );
+
+        $coreConfig->save();
+
+        return null;
+    }
 }

+ 4 - 1
app/config/main.dist.php

@@ -123,7 +123,10 @@ return [
             'eol'   => '%EOL%',
         ],
         'Func'      => \ForkBB\Core\Func::class,
-        'Test'      => \ForkBB\Core\Test::class,
+        'Test'      => [
+            'class'  => \ForkBB\Core\Test::class,
+            'config' => '%DIR_CONFIG%/test.default.php',
+        ],
         'NormEmail' => \MioVisman\NormEmail\NormEmail::class,
         'Log'       => [
             'class'  => \ForkBB\Core\Log::class,

+ 22 - 0
app/config/test.default.php

@@ -0,0 +1,22 @@
+<?php
+/**
+ * This file is part of the ForkBB <https://github.com/forkbb>.
+ *
+ * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
+ * @license   The MIT License (MIT)
+ */
+
+declare(strict_types=1);
+
+// переименуйте этот файл в test.php
+// в файле main.php замените '%DIR_CONFIG%/test.default.php' на '%DIR_CONFIG%/test.php'
+// после этого вносите свои изменения
+
+return [
+    // Список url (не относящихся к страницам движка) источников отправки форм
+    // символ звездочки (*) заменяет любое количество символов
+    'referers' => [
+        // 'http://localhost/',  // <-- точное соотвествие странице http://localhost/ (не https)
+        // 'http://localhost/*', // <-- любые страницы с сайта http://localhost/ (не https)
+    ],
+];