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/Cli.php (2 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\engines\EngineAbstract;
6
use carono\janitor\helpers\Console;
7
use carono\janitor\helpers\FileHelper;
8
9
/**
10
 * Class Cli
11
 *
12
 * @package carono\janitor
13
 */
14
class Cli
15
{
16
    public static $renamedFile = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'renamed.json';
17
    public static $cacheFile = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'cache.json';
18
    public static $temporaryDir = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmp';
19
20
    protected static function getFileTypeMessage(File $file)
21
    {
22
        $message = 'File type: ';
23
        if ($file->isFilm()) {
24
            $message .= 'Movie';
25
        } else {
26
            $message .= 'Serial, Season ' . $file->getSeasonNumber() . ' Episode ' . $file->getEpisodeNumber();
27
        }
28
        return $message;
29
    }
30
31
    protected static function out($text = '', $newLine = true)
32
    {
33
        echo $text . ($newLine ? "\n" : '');
34
    }
35
36
    /**
37
     * @param $dir
38
     */
39
    public static function reformFiles($dir)
40
    {
41
        /**
42
         * @var File $file
43
         */
44
        $files = static::getFiles($dir);
45
        $renameAll = false;
46
        $fileModels = array_map(function ($path) {
47
            return new File(static::getEngine(), $path);
0 ignored issues
show
It seems like static::getEngine() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
48
        }, $files);
49
        $i = 0;
50
        $selectedName = null;
0 ignored issues
show
$selectedName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
        $request = null;
52
        while ($file = array_shift($fileModels)) {
53
            $value = null;
54
            if ($file->wasRenamed()) {
55
                echo "{$file->getFilePath()} SKIP\n";
56
                continue;
57
            }
58
59
            $options = [
60
                'r' => 'Confirm rename',
61
                's' => 'Skip file',
62
                'c' => 'Custom name for search',
63
                'n' => 'Next title',
64
                'i' => 'Ignore this file'
65
            ];
66
67
            if ($file->isSerial()) {
68
                $options['a'] = 'All rename for serial';
69
            }
70
71
            $selectedName = $file->getStoredSerialName();
72
73
            if (!$renameAll || !$file->getStoredSerialName()) {
74
                $renameAll = false;
75
                $names = $file->searchFilmNames($request);
76
                Console::clearScreen();
77
78
                if ($file->getStoredSerialName()) {
79
                    $file->restoreSerialData();
80
                    if (($i = array_search($selectedName, $names)) === false) {
81
                        $i = [];
82
                    }
83
                } else {
84
                    $selectedName = $names[$i];
85
                }
86
87
                $reformedFileName = $file->getReformFileName($selectedName);
88
89
                Console::selectBox('Select correct title:', $names, $i);
90
                self::out();
91
                self::out("Original file: {$file->getFileName()}");
92
                self::out("New file name: {$reformedFileName}");
93
                if ($file->isSerial()) {
94
                    self::out('Files in directory: ' . count(FileHelper::findFiles($file->getFolder())));
95
                }
96
                self::out();
97
                $value = Console::select('What do?', $options);
98
99
            } else {
100
                echo $file->getFilePath() . ' => ' . $file->getReformFilmName($selectedName) . ": Renamed\n";
101
            }
102
103
            if ($value === 'i') {
104
                $file->storeRename();
105
                continue;
106
            }
107
108
            if ($value === 'n') {
109
                $i++;
110
                if (!isset($names[$i])) {
111
                    $i = 0;
112
                }
113
            }
114
115
            if ($value === 'a') {
116
                $renameAll = true;
117
            }
118
119
            if ($value === 'r' || $value === 'a' || $renameAll) {
120
                $file->renameFile($selectedName);
121
                $i = 0;
122
                continue;
123
            }
124
125
            if ($value === 'c') {
126
                $request = Console::prompt('Write film name for searching:');
127
            }
128
129
            array_unshift($fileModels, $file);
130
        }
131
    }
132
133
    /**
134
     * @param $dir
135
     */
136
    public static function reform($dir)
137
    {
138
        static::reformFiles(realpath($dir));
139
    }
140
141
    /**
142
     * @param $name
143
     * @param $season
144
     * @return string
145
     */
146
    public static function getSerialFolderName($name, $season)
147
    {
148
        $name = pathinfo($name, PATHINFO_FILENAME);
149
        return $name . ' сезон ' . $season;
150
    }
151
152
    /**
153
     * @param $dir
154
     * @return array
155
     */
156
    protected static function getFiles($dir)
157
    {
158
        return FileHelper::findFiles($dir);
159
    }
160
161
    /**
162
     * @param $name
163
     * @return bool
164
     */
165
    public static function extractYear($name)
166
    {
167
        if (preg_match_all('/(\d{4})/', $name, $m)) {
168
            foreach ($m[0] as $year) {
169
                if ($year > 1900 && $year <= date('Y')) {
170
                    return $year;
171
                }
172
            }
173
        }
174
        return false;
175
    }
176
177
    /**
178
     * @param null $season
179
     * @param null $episode
180
     * @return string
181
     */
182
    public static function getEpisodeName($season = null, $episode = null)
183
    {
184
        $season = str_pad($season, 2, '0', STR_PAD_LEFT);
185
        $episode = str_pad($episode, 2, '0', STR_PAD_LEFT);
186
        return "s{$season}e{$episode}";
187
    }
188
189
    /**
190
     * @return EngineAbstract
191
     */
192
    public static function getEngine()
193
    {
194
        $class = getenv('SEARCH_ENGINE');
195
        if (!class_exists($class)) {
196
            die("Class $class not found\n");
197
        }
198
        return new $class;
199
    }
200
}