Issues (8)

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.

Api/Helper/File.php (3 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
namespace Werkspot\BingAdsApiBundle\Api\Helper;
3
4
use Exception;
5
use GuzzleHttp\ClientInterface;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Finder\Finder;
8
use Werkspot\BingAdsApiBundle\Api\Exceptions\FileNotCopiedException;
9
use Werkspot\BingAdsApiBundle\Api\Exceptions\NoFileDestinationException;
10
use Werkspot\BingAdsApiBundle\Guzzle\Exceptions\CurlException;
11
use Werkspot\BingAdsApiBundle\Guzzle\Exceptions\HttpStatusCodeException;
12
use ZipArchive;
13
14
class File
15
{
16
    /**
17
     * @var ClientInterface
18
     */
19
    private $guzzleClient;
20
21
    /**
22
     * @var Filesystem
23 27
     */
24
    private $filesystem;
25 27
26 27
    /**
27 27
     * @var Finder
28
     */
29
    private $finder;
30
31
    /**
32
     * @var ZipArchive
33
     */
34
    private $zipArchive;
35
36
    public function __construct(ClientInterface $guzzleClient, Filesystem $fileSystem, Finder $finder)
37 2
    {
38
        $this->guzzleClient = $guzzleClient;
39 2
        $this->filesystem = $fileSystem;
40 2
        $this->finder = $finder;
41 1
        $this->zipArchive = new ZipArchive();
42
    }
43 1
44 1
    /**
45 1
     * @param $source
46 1
     * @param null|string $destination
47 1
     *
48 1
     * @throws NoFileDestinationException|FileNotCopiedException
49
     *
50
     * @return string
51
     */
52 1
    public function copyFile($source, $destination = null)
53 1
    {
54
        if (preg_match('/^http(s?):\/\//', $source)) {
55
            if ($destination === null) {
56 1
                throw new NoFileDestinationException();
57
            }
58
            $destination = $this->download($source, $destination);
59
        } else {
60
            if ($destination !== null) {
61
                $this->filesystem->copy($source, $destination);
62
            } else {
63
                $destination = $source;
64
            }
65
        }
66
67
        if (!$this->filesystem->exists($destination)) {
68
            throw new FileNotCopiedException();
69 2
        }
70
71 2
        return $destination;
72
    }
73 2
74
    /**
75
     * @param string $file
76
     *
77
     * @return bool
78
     */
79
    public function isHealthyZipFile($file)
80
    {
81
        $zipStatus = $this->zipArchive->open($file, ZipArchive::CHECKCONS);
82
        if ($zipStatus === ZipArchive::ER_OK || $zipStatus === true) {
83
            $this->zipArchive->close();
84
            $status = true;
85 2
        } else {
86
            $status = false;
87 2
        }
88 2
89 2
        return $status;
90 1
    }
91
92 1
    /**
93 1
     * @param string $url
94 1
     * @param string $destination
95 1
     *
96 1
     * @throws CurlException
97 1
     * @throws HttpStatusCodeException
98 1
     * @throws \Exception
99 1
     *
100 1
     * @return string
101 1
     */
102
    public function download($url, $destination)
103 1
    {
104
        $this->guzzleClient->request('GET', $url, ['sink' => $destination]);
105
106
        return $destination;
107
    }
108
109
    /**
110
     * @param string $file zipFile we want to open
111
     * @param null|string $extractTo
112
     * @param true|bool $delete
113
     *
114
     * @throws Exception
115
     *
116
     * @return array
117
     */
118
    public function unZip($file, $extractTo = null, $delete = true)
119
    {
120
        $zipDir = ($extractTo) ? $extractTo : dirname($file);
121
122
        if ($this->zipArchive->open($file) !== true) {
123
            throw new Exception("Could not open file {$file}");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $file instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
124
        }
125
        $files = [];
126
        for ($i = 0; $i < $this->zipArchive->numFiles; ++$i) {
127
            $stat = $this->zipArchive->statIndex($i);
128
            $files[] = "{$zipDir}/{$stat['name']}";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $zipDir instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $stat instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
129
        }
130
        $this->zipArchive->extractTo($zipDir);
131
        $this->zipArchive->close();
132
        if ($delete) {
133
            $this->filesystem->remove($file);
134
        }
135
136
        return $files;
137
    }
138
139
    /**
140
     * @param string $file
141
     * @return string[]
142
     */
143
    public function readFileLinesIntoArray($file)
144
    {
145
        return file($file);
146
    }
147
148
    /**
149
     * @param string[] $lines
150
     * @param string $file
151
     */
152
    public function writeLinesToFile($lines, $file)
153
    {
154
        $fp = fopen($file, 'w');
155
        fwrite($fp, implode('', $lines));
156
        fclose($fp);
157
    }
158
159
    /**
160
     * @param string|string[] $cache
161
     */
162
    public function clearCache($cache)
163
    {
164
        if (is_array($cache)) {
165
            foreach ($cache as $file) {
166
                $this->removeFile($file);
167
            }
168
        } elseif (is_dir($cache)) {
169
            foreach ($this->finder->files()->in($cache) as $file) {
170
                $this->removeFile($file);
171
            }
172
        } else {
173
            $this->removeFile($cache);
174
        }
175
    }
176
177
    /**
178
     * @param string $file
179
     */
180
    private function removeFile($file)
181
    {
182
        $this->filesystem->remove($file);
183
    }
184
185
    /**
186
     * @param string[] $files
187
     * @param string $target
188
     */
189
    public function moveFirstFile(array $files, $target)
190
    {
191
        $this->filesystem->rename($files[0], $target);
192
    }
193
194
    public function createDirIfNotExists($path)
195
    {
196
        if (!$this->filesystem->exists($path)) {
197
            $this->filesystem->mkdir($path, 0700);
198
        }
199
    }
200
}
201