Issues (21)

Security Analysis    no request data  

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/DirectoryConverter.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 Spatie\Php7to5;
4
5
use Spatie\Php7to5\Exceptions\InvalidParameter;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Finder\Finder;
8
9
class DirectoryConverter
10
{
11
    /** @var string */
12
    protected $sourceDirectory;
13
    /** @var string */
14
    protected $copyNonPhpFiles = true;
15
    /** @var bool */
16
    protected $cleanDestinationDirectory = false;
17
    /** @var string[] */
18
    protected $extensions;
19
    /** @var null|string[] */
20
    protected $excludes;
21
    protected $logger;
22
23
    /**
24
     * DirectoryConverter constructor.
25
     *
26
     * @param string $sourceDirectory
27
     * @param string[] $extensions
28
     * @param string[]|null $excludes
29
     *
30
     * @throws \Spatie\Php7to5\Exceptions\InvalidParameter
31
     */
32
    public function __construct($sourceDirectory, array $extensions, array $excludes = null)
33
    {
34
        if (!file_exists($sourceDirectory)) {
35
            throw InvalidParameter::directoryDoesNotExist($sourceDirectory);
36
        }
37
38
        $this->sourceDirectory = $sourceDirectory;
39
        $this->extensions = array_map('mb_strtolower', $extensions);
40
        $this->excludes = $excludes;
41
    }
42
43
    public function setLogger(OutputInterface $output)
44
    {
45
        $this->logger = $output;
46
    }
47
48
    public function log($sourceItem, $target)
49
    {
50
        if (is_null($this->logger)) {
51
            return;
52
        }
53
        $targetRealPath = realpath($target);
54
55
        $this->logger->writeln("<comment>Converting {$sourceItem} to {$targetRealPath}...</comment>");
56
    }
57
58
    /**
59
     * @return $this
60
     */
61
    public function alsoCopyNonPhpFiles()
62
    {
63
        $this->copyNonPhpFiles = true;
0 ignored issues
show
Documentation Bug introduced by
The property $copyNonPhpFiles was declared of type string, but true is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return $this
70
     */
71
    public function cleanDestinationDirectory()
72
    {
73
        $this->cleanDestinationDirectory = true;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return $this
80
     */
81
    public function doNotCopyNonPhpFiles()
82
    {
83
        $this->copyNonPhpFiles = false;
0 ignored issues
show
Documentation Bug introduced by
The property $copyNonPhpFiles was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
84
85
        return $this;
86
    }
87
88
    /**
89
     * @param string $destinationDirectory
90
     *
91
     * @throws \Spatie\Php7to5\Exceptions\InvalidParameter
92
     */
93
    public function savePhp5FilesTo($destinationDirectory)
94
    {
95
        if ($destinationDirectory === '') {
96
            throw InvalidParameter::directoryIsRequired();
97
        }
98
99
        if($this->cleanDestinationDirectory){
100
            $this->removeDirectory($destinationDirectory);
101
        }
102
103
        $this->copyDirectory($this->sourceDirectory, $destinationDirectory);
104
    }
105
106
    /**
107
     * @param string $sourceDirectory
108
     * @param string $destinationDirectory
109
     */
110
    protected function copyDirectory($sourceDirectory, $destinationDirectory)
111
    {
112
        if (!is_dir($destinationDirectory)) {
113
            mkdir($destinationDirectory);
114
        }
115
116
        $finder = new Finder();
117
        $finder->in($sourceDirectory);
118
        if (!$this->copyNonPhpFiles) {
119
            foreach ($this->extensions as $extension) {
120
                $finder->name('*.'.$extension);
121
            }
122
        }
123
124
        if ($this->excludes) {
125
            foreach ($this->excludes as $exclude) {
126
                $finder->notPath('/^'.preg_quote($exclude, '/').'/');
127
            }
128
        }
129
130
        foreach ($finder as $item) {
131
            $target = $destinationDirectory.'/'.$item->getRelativePathname();
132
133
            if ($item->isFile()) {
134
                $isPhpFile = $this->isPhpFile($target);
135
                if ($isPhpFile || $this->copyNonPhpFiles) {
136
                    $targetDir = dirname($target);
137
                    if ($targetDir && !is_dir($targetDir)) {
138
                        mkdir($targetDir, 0755, true);
139
                    }
140
                    copy($item->getRealPath(), $target);
141
142
                    $this->log($item->getRelativePath(), $target);
143
144
                    if ($isPhpFile) {
145
                        $this->convertToPhp5($target);
146
                    }
147
                }
148
            }
149
        }
150
    }
151
152
    /**
153
     * @param string $path
154
     */
155
    protected function removeDirectory($path)
156
    {
157
        if (PHP_OS === 'Windows') {
158
            $command = 'rd /s /q %s';
159
        } else {
160
            $command = 'rm -rf %s';
161
        }
162
163
        exec(sprintf($command, escapeshellarg($path)));
164
    }
165
166
    /**
167
     * @param string $filePath
168
     */
169
    protected function convertToPhp5($filePath)
170
    {
171
        $converter = new Converter($filePath);
172
173
        $converter->saveAsPhp5($filePath);
174
    }
175
176
    /**
177
     * @param string $filePath
178
     *
179
     * @return bool
180
     */
181
    protected function isPhpFile($filePath)
182
    {
183
        return in_array(strtolower(pathinfo($filePath, PATHINFO_EXTENSION)), $this->extensions, true);
184
    }
185
}
186