This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace carono\janitor; |
||
4 | |||
5 | use carono\janitor\Cli as JanitorCli; |
||
6 | use carono\janitor\helpers\ArrayHelper; |
||
7 | use carono\janitor\helpers\Console; |
||
8 | use carono\janitor\helpers\FileHelper; |
||
9 | use carono\janitor\helpers\Inflector; |
||
10 | use Dotenv\Dotenv; |
||
11 | use splitbrain\phpcli\CLI; |
||
0 ignored issues
–
show
|
|||
12 | use splitbrain\phpcli\Options; |
||
13 | |||
14 | class JanitorCommand extends CLI |
||
15 | { |
||
16 | const CMD_FIXTURE = 'fixture'; |
||
17 | const CMD_CLEAR_CACHE = 'clear-cache'; |
||
18 | const CMD_SET_ENGINE_OPTIONS = 'set-engine-options'; |
||
19 | const CMD_SET_OPTION = 'set-option'; |
||
20 | const CMD_RESET_ENV = 'reset-env'; |
||
21 | |||
22 | protected static function getEnvFile() |
||
23 | { |
||
24 | $home = ArrayHelper::getValue($_SERVER, 'HOMEPATH', ArrayHelper::getValue($_SERVER, 'HOME')); |
||
25 | return $home . DIRECTORY_SEPARATOR . 'film-janitor' . DIRECTORY_SEPARATOR . '.env'; |
||
26 | } |
||
27 | |||
28 | protected static function refreshEnv() |
||
29 | { |
||
30 | $dotenv = Dotenv::create(dirname(static::getEnvFile())); |
||
31 | $dotenv->overload(); |
||
32 | $dotenv->required(['SEARCH_ENGINE']); |
||
33 | unset($dotenv); |
||
34 | } |
||
35 | |||
36 | public function __construct($autocatch = true) |
||
37 | { |
||
38 | parent::__construct($autocatch); |
||
39 | |||
40 | if (!file_exists(static::getEnvFile())) { |
||
41 | $this->cmdResetEnv(new Options); |
||
42 | } |
||
43 | static::refreshEnv(); |
||
44 | } |
||
45 | |||
46 | protected function setup(Options $options) |
||
47 | { |
||
48 | $options->setHelp(''); |
||
49 | $options->registerOption('directory', 'Directory for refactor films', 'd', true); |
||
50 | |||
51 | $options->registerCommand(static::CMD_FIXTURE, 'Create video files for testing'); |
||
52 | $options->registerCommand(static::CMD_CLEAR_CACHE, 'Clearing cache'); |
||
53 | $options->registerCommand(static::CMD_SET_ENGINE_OPTIONS, 'Set engine options'); |
||
54 | $options->registerCommand(static::CMD_RESET_ENV, 'Reset env options'); |
||
55 | } |
||
56 | |||
57 | protected function setEnvOption($option, $value) |
||
58 | { |
||
59 | $value = addcslashes($value, '"\\'); |
||
60 | $env = static::getEnvFile(); |
||
61 | $envContent = file_get_contents($env); |
||
62 | $option = preg_quote($option, '/'); |
||
63 | $data = $option . '="' . $value . '"'; |
||
64 | $newEnvContent = preg_replace("/^ ?$option ?=.+$/m", str_replace('\\', '\\\\', $data), $envContent, -1, $c); |
||
65 | if (!$c) { |
||
0 ignored issues
–
show
The expression
$c of type integer|null is loosely compared to false ; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.
In PHP, under loose comparison (like For 0 == false // true
0 == null // true
123 == false // false
123 == null // false
// It is often better to use strict comparison
0 === false // false
0 === null // false
![]() |
|||
66 | $data = $envContent ? "\n" . $data : $data; |
||
67 | file_put_contents($env, $data, FILE_APPEND); |
||
68 | } else { |
||
69 | file_put_contents($env, $newEnvContent); |
||
70 | } |
||
71 | static::refreshEnv(); |
||
72 | } |
||
73 | |||
74 | public function cmdRefactoring($dir) |
||
75 | { |
||
76 | foreach (JanitorCli::getEngine()->getRequiredEnvironmentOptions() as $option => $description) { |
||
77 | if (!getenv($option)) { |
||
78 | $value = Console::prompt($description); |
||
79 | $this->setEnvOption($option, $value); |
||
80 | } |
||
81 | } |
||
82 | $realPath = realpath($dir); |
||
83 | if (!dir($realPath)) { |
||
84 | die("Dir $dir not found\n"); |
||
85 | } |
||
86 | if (Console::confirm('Refactor ' . realpath($dir) . '?')) { |
||
87 | JanitorCli::reform($dir); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param Options $options |
||
93 | */ |
||
94 | public function cmdFixture(Options $options) |
||
0 ignored issues
–
show
|
|||
95 | { |
||
96 | $appDir = dirname(__DIR__); |
||
97 | $prefix = ''; |
||
98 | $videoDir = $appDir . DIRECTORY_SEPARATOR . 'video'; |
||
99 | $files = explode("\n", trim(file_get_contents($appDir . DIRECTORY_SEPARATOR . 'fixture.txt'))); |
||
100 | FileHelper::removeDirectory($videoDir); |
||
101 | if (!mkdir($videoDir) && !is_dir($videoDir)) { |
||
102 | throw new \RuntimeException(sprintf('Directory "%s" was not created', 'video')); |
||
103 | } |
||
104 | foreach ($files as $file) { |
||
105 | $file = trim($file); |
||
106 | $fileName = mb_substr($file, mb_strlen($prefix, 'UTF-8'), null, 'UTF-8'); |
||
107 | $baseName = basename($fileName); |
||
108 | $dir = $appDir . '\\video' . (strpos($fileName, '\\') ? '\\' . dirname($fileName) : ''); |
||
109 | if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) { |
||
110 | throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir)); |
||
111 | } |
||
112 | $fullFilePath = $dir . '\\' . $baseName; |
||
113 | echo $fullFilePath . "\n"; |
||
114 | file_put_contents($fullFilePath, $baseName); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param Options $options |
||
120 | */ |
||
121 | public function cmdClearCache(Options $options) |
||
0 ignored issues
–
show
|
|||
122 | { |
||
123 | \carono\janitor\Cli::getEngine()->clearCache(); |
||
124 | echo "Clearing\n"; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param Options $options |
||
129 | */ |
||
130 | public function cmdSetEngineOptions(Options $options) |
||
0 ignored issues
–
show
|
|||
131 | { |
||
132 | |||
133 | foreach (JanitorCli::getEngine()->getRequiredEnvironmentOptions() as $option => $description) { |
||
134 | $value = getenv($option); |
||
135 | $value = Console::prompt($description, ['default' => $value]); |
||
136 | $this->setEnvOption($option, $value); |
||
137 | } |
||
138 | |||
139 | foreach (JanitorCli::getEngine()->getOptions() as $option => $item) { |
||
140 | $default = JanitorCli::getEngine()->getOptionDefaultValue($option); |
||
141 | $description = JanitorCli::getEngine()->getOptionDescription($option); |
||
142 | $key = 'ENGINE_OPTION_' . $option; |
||
143 | |||
144 | $value = getenv($key) !== null ? getenv($key) : $default; |
||
145 | $value = Console::prompt($description, ['default' => $value]); |
||
146 | $this->setEnvOption($key, $value); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param Options $options |
||
152 | */ |
||
153 | public function cmdResetEnv(Options $options) |
||
0 ignored issues
–
show
|
|||
154 | { |
||
155 | if (Console::confirm('Reset env to default')) { |
||
156 | $env = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '.env'; |
||
157 | if (!is_dir(dirname(static::getEnvFile()))) { |
||
158 | FileHelper::createDirectory(dirname(static::getEnvFile())); |
||
159 | } |
||
160 | copy($env . '.example', static::getEnvFile()); |
||
161 | Console::output('Env reverted to default'); |
||
162 | static::refreshEnv(); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param Options $options |
||
168 | */ |
||
169 | protected function main(Options $options) |
||
170 | { |
||
171 | if ($dir = $options->getOpt('directory')) { |
||
172 | $this->cmdRefactoring($dir); |
||
173 | exit; |
||
174 | } |
||
175 | |||
176 | $cmd = $options->getCmd(); |
||
177 | $method = 'cmd' . Inflector::camelize($cmd); |
||
178 | if ($cmd && method_exists($this, $method)) { |
||
179 | $this->$method($options); |
||
180 | exit; |
||
181 | } |
||
182 | |||
183 | echo $options->help(); |
||
184 | } |
||
185 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: