Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
PluginValidator
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 8
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isPluginId
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 isPluginName
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 isDisplayName
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 isCheckUrl
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 isDescription
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 isFreePluginId
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 isFreePluginName
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\App\Models\Domain;
6
7use PeServer\Core\I18n;
8use PeServer\Core\Uuid;
9use PeServer\Core\TrueKeeper;
10use PeServer\Core\Environment;
11use PeServer\Core\Mvc\Validator;
12use PeServer\Core\Text;
13use PeServer\Core\Database\DatabaseContext;
14use PeServer\Core\Mvc\IValidationReceiver;
15use PeServer\App\Models\Domain\ValidatorBase;
16use PeServer\App\Models\Dao\Entities\PluginsEntityDao;
17
18class PluginValidator extends ValidatorBase
19{
20    private const PLUGIN_NAME_RANGE_MIN = 4;
21    private const PLUGIN_NAME_RANGE_MAX = 64;
22    private const PLUGIN_DISPLAY_NAME_LENGTH = 500;
23    private const PLUGIN_DESCRIPTION_LENGTH = 1000;
24
25    private const LOCALHOST_PATTERN = '/https?:\/\/(\w*:\\w*@)?((localhost)|(127\.0\.0\.1))\b/';
26
27    public function __construct(IValidationReceiver $receiver, Validator $validator, private Environment $environment)
28    {
29        parent::__construct($receiver, $validator);
30    }
31
32    public function isPluginId(string $key, ?string $value): bool
33    {
34        if ($this->validator->isNotWhiteSpace($key, $value)) {
35            $trueKeeper = new TrueKeeper();
36
37            if (!Uuid::isGuid($value)) {
38                $trueKeeper->state = false;
39                $this->receiver->receiveErrorMessage($key, I18n::message('error/illegal_plugin_id'));
40            }
41
42            return $trueKeeper->state;
43        }
44
45        return false;
46    }
47
48    public function isPluginName(string $key, ?string $value): bool
49    {
50        if ($this->validator->isNotWhiteSpace($key, $value)) {
51            $trueKeeper = new TrueKeeper();
52
53            $trueKeeper->state = $this->validator->inRange($key, self::PLUGIN_NAME_RANGE_MIN, self::PLUGIN_NAME_RANGE_MAX, $value);
54            $trueKeeper->state = $this->validator->isMatch($key, '/^[a-zA-Z0-9\-_!=\(\)\[\]\.]+$/', $value);
55
56            return $trueKeeper->state;
57        }
58
59        return false;
60    }
61
62    public function isDisplayName(string $key, ?string $value): bool
63    {
64        if ($this->validator->isNotWhiteSpace($key, $value)) {
65            $trueKeeper = new TrueKeeper();
66
67            $trueKeeper->state = $this->validator->inLength($key, self::PLUGIN_DISPLAY_NAME_LENGTH, $value);
68
69            return $trueKeeper->state;
70        }
71
72        return false;
73    }
74
75    public function isCheckUrl(string $key, ?string $value): bool
76    {
77        if ($this->validator->isNotWhiteSpace($key, $value)) {
78            $trueKeeper = new TrueKeeper();
79
80            $trueKeeper->state = $this->isWebsite($key, $value);
81
82            if ($this->environment->isProduction()) {
83                // チェック用URLなのでワッケ分からんURLの登録は禁止する(検証環境はいい)
84                $trueKeeper->state = $this->validator->isNotMatch($key, self::LOCALHOST_PATTERN, $value);
85            }
86
87            return $trueKeeper->state;
88        }
89
90        return false;
91    }
92
93    public function isDescription(string $key, ?string $value): bool
94    {
95        if (!Text::isNullOrWhiteSpace($value)) {
96            $trueKeeper = new TrueKeeper();
97
98            $trueKeeper->state = $this->validator->inLength($key, self::PLUGIN_DESCRIPTION_LENGTH, $value);
99
100            return $trueKeeper->state;
101        }
102
103        return true;
104    }
105
106
107    public function isFreePluginId(DatabaseContext $database, string $key, string $pluginId): bool
108    {
109        $pluginsEntityDao = new PluginsEntityDao($database);
110
111        if ($pluginsEntityDao->selectExistsPluginId($pluginId)) {
112            $this->receiver->receiveErrorMessage($key, I18n::message('error/unusable_plugin_id'));
113            return false;
114        }
115
116        return true;
117    }
118
119    public function isFreePluginName(DatabaseContext $database, string $key, string $pluginName): bool
120    {
121        $pluginsEntityDao = new PluginsEntityDao($database);
122
123        if ($pluginsEntityDao->selectExistsPluginName($pluginName)) {
124            $this->receiver->receiveErrorMessage($key, I18n::message('error/unusable_plugin_name'));
125            return false;
126        }
127
128        return true;
129    }
130}