Issues (50)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/JanitorCommand.php (6 issues)

Upgrade to new PHP Analysis Engine

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
This use statement conflicts with another class in this namespace, carono\janitor\CLI.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
Bug Best Practice introduced by
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 ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

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
Loading history...
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
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
}