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/File.php (5 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\engines\EngineInterface;
7
use carono\janitor\helpers\ArrayHelper;
8
9
/**
10
 * Class File
11
 *
12
 * @package carono\janitor
13
 */
14
class File
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $filePath;
20
    /**
21
     * @var array
22
     */
23
    protected $movieNames = [];
24
    /**
25
     * @var EngineAbstract
26
     */
27
    protected $engine;
28
    /**
29
     * @var string
30
     */
31
    protected $lastRequest;
32
33
    /**
34
     * @return string
35
     */
36
    public function getParentFolder()
37
    {
38
        return basename($this->getFolder());
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getFolder()
45
    {
46
        return (string)pathinfo($this->filePath, PATHINFO_DIRNAME);
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getFileName()
53
    {
54
        return (string)pathinfo($this->filePath, PATHINFO_FILENAME);
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getFilePath()
61
    {
62
        return $this->filePath;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getFileExtension()
69
    {
70
        return (string)pathinfo($this->filePath, PATHINFO_EXTENSION);
71
    }
72
73
    /**
74
     * @return array
75
     */
76
    protected function getStoredSerials()
77
    {
78
        return $this->getCacheValue('serial-names') ?: [];
79
    }
80
81
    /**
82
     * @return mixed
83
     */
84
    public function getStoredSerialData()
85
    {
86
        if ($this->isFilm()) {
87
            return [];
88
        }
89
        return ArrayHelper::getValue($this->getStoredSerials(), $this->getParentFolder(), []);
90
    }
91
92
    /**
93
     *
94
     */
95
    public function restoreSerialData()
96
    {
97
        if ($data = $this->getStoredSerialData()) {
98
            $this->movieNames = $data['movieNames'];
99
        }
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    public function getStoredSerialName()
106
    {
107
        if ($data = $this->getStoredSerialData()) {
108
            return $data['newName'];
109
        }
110
        return false;
111
    }
112
113
    /**
114
     * @param $newName
115
     */
116
    public function storeSerialName($newName)
117
    {
118
        $title = $this->getReformFilmName($newName);
119
        if ($this->isSerial() && !$this->getStoredSerialData()) {
120
            $serials = $this->getStoredSerials();
121
            $data = [
122
                'title' => $title,
123
                'newName' => $newName,
124
                'movieNames' => $this->movieNames
125
            ];
126
            $serials[$this->getParentFolder()] = $data;
127
            $this->setCacheValue('serial-names', $serials);
128
        }
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    public function isFilm()
135
    {
136
        return !$this->getSeasonNumber() && !$this->getEpisodeNumber();
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getSeasonNumber() 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...
Bug Best Practice introduced by
The expression $this->getEpisodeNumber() 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...
137
    }
138
139
    /**
140
     * @return bool
141
     */
142
    public function isSerial()
143
    {
144
        return !$this->isFilm();
145
    }
146
147
    /**
148
     * @param null $request
149
     * @return string[]
150
     */
151
    public function searchFilmNames($request = null)
152
    {
153
        $search = $request ?: $this->filePath;
154
        if (!isset($this->movieNames[$search])) {
155
            $this->movieNames[$search] = $this->engine->getTitles($search, $this);
156
        }
157
        $this->lastRequest = $request;
158
        return $this->movieNames[$search];
159
    }
160
161
    /**
162
     * @param $newName
163
     * @return string
164
     */
165
    public function getReformFilePath($newName)
166
    {
167
        return $this->getFolder() . DIRECTORY_SEPARATOR . $this->getReformFileName($newName);
168
    }
169
170
    /**
171
     * @param $newName
172
     * @return string
173
     */
174
    public function getReformFileName($newName)
175
    {
176
        return $this->getReformFilmName($newName) . '.' . $this->getFileExtension();
177
    }
178
179
    /**
180
     * @param $newName
181
     * @return string
182
     */
183
    public function getReformFilmName($newName)
184
    {
185
        return $this->engine->reformFilmName($this, $newName, $this->lastRequest);
186
    }
187
188
    /**
189
     * @return string
190
     */
191
    public function getSuffix()
192
    {
193
        return Cli::getEpisodeName($this->getSeasonNumber(), $this->getEpisodeNumber());
0 ignored issues
show
It seems like $this->getSeasonNumber() targeting carono\janitor\File::getSeasonNumber() can also be of type integer; however, carono\janitor\Cli::getEpisodeName() does only seem to accept null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
It seems like $this->getEpisodeNumber() targeting carono\janitor\File::getEpisodeNumber() can also be of type integer; however, carono\janitor\Cli::getEpisodeName() does only seem to accept null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
194
    }
195
196
    /**
197
     * @return int|null
198
     */
199
    public function getEpisodeNumber()
200
    {
201
        if (!$episode = $this->engine->parseSerialEpisodeNumber($this->filePath)) {
202
            return $this->getSeasonNumber() ? 1 : null;
203
        }
204
        return $episode;
205
    }
206
207
    /**
208
     * @return int|null
209
     */
210
    public function getSeasonNumber()
211
    {
212
        return $this->engine->parseSerialSeason($this->filePath) ?: $this->engine->parseSerialSeason($this->getFolder());
213
    }
214
215
    protected function setCacheValue($key, $data)
216
    {
217
        $class = $this->engine;
218
        $class::setCacheValue($key, $data);
219
    }
220
221
    protected function getCacheValue($key)
222
    {
223
        $class = $this->engine;
224
        return $class::getCacheValue($key);
225
    }
226
227
    /**
228
     * @return bool
229
     */
230
    public function wasRenamed()
231
    {
232
        $key = 'store-file:' . md5($this->filePath);
233
        return (bool)$this->getCacheValue($key);
234
    }
235
236
    public function storeRename()
237
    {
238
        $key = 'store-file:' . md5($this->filePath);
239
        $this->setCacheValue($key, 1);
240
    }
241
242
    /**
243
     * @param $newName
244
     */
245
    public function renameFile($newName)
246
    {
247
        if (!rename($this->filePath, $newPath = $this->getReformFilePath($newName))) {
248
            dir("Fail rename $this->filePath, to $newPath");
249
        }
250
        $this->filePath = $newPath;
251
        $this->storeRename();
252
        $this->storeSerialName($newName);
253
    }
254
255
    /**
256
     * File constructor.
257
     *
258
     * @param $filePath
259
     * @throws \Exception
260
     */
261
    public function __construct(EngineInterface $engine, $filePath)
262
    {
263
        $this->engine = $engine;
0 ignored issues
show
Documentation Bug introduced by
$engine is of type object<carono\janitor\engines\EngineInterface>, but the property $engine was declared to be of type object<carono\janitor\engines\EngineAbstract>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
264
        $this->filePath = $filePath;
265
        if (!file_exists($filePath)) {
266
            throw new \Exception("File $filePath not found");
267
        }
268
    }
269
}