Issues (100)

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.

Url/UrlBuilder.php (1 issue)

Languages

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
 * This file is part of the IrishDan\ResponsiveImageBundle package.
4
 *
5
 * (c) Daniel Byrne <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source
8
 * code.
9
 */
10
11
namespace IrishDan\ResponsiveImageBundle\Url;
12
13
use IrishDan\ResponsiveImageBundle\FileSystem\PrimaryFileSystemWrapper;
14
use League\Flysystem\AdapterInterface;
15
use Psr\Log\InvalidArgumentException;
16
17
/**
18
 * Class UrlBuilder
19
 *
20
 * @package IrishDan\ResponsiveImageBundle\Url
21
 */
22
class UrlBuilder
23
{
24
    /**
25
     * @var \League\Flysystem\FilesystemInterface|null
26
     */
27
    private $fileSystem;
28
    /**
29
     * @var array|null
30
     */
31
    private $config;
32
    /**
33
     * @var array
34
     */
35
    private $adapterUrlEncoders = [];
36
    /**
37
     * @var array
38
     */
39
    private $adapterUrlMappings = [];
40
41
    /**
42
     * @param string $adapterType
43
     *
44
     * @return array
45
     */
46
    public function getAdapterUrlMappings($adapterType = '')
47
    {
48
        if (empty($adapterType)) {
49
            return $this->adapterUrlMappings;
50
        }
51
52
        return empty($this->adapterUrlMappings[$adapterType]) ? null : $this->adapterUrlMappings[$adapterType];
53
    }
54
55
    public function setAdapterUrlMappings($adapterType, $data)
56
    {
57
        if (!is_string($adapterType)) {
58
            throw new InvalidArgumentException(
59
                'Adapter type must be a string'
60
            );
61
        }
62
63
        $this->adapterUrlMappings[$adapterType] = $data;
64
    }
65
66
    /**
67
     * UrlBuilder constructor.
68
     *
69
     * @param PrimaryFileSystemWrapper|null $PrimaryFileSystemWrapper
70
     * @param array|null                    $config
71
     */
72
    public function __construct(PrimaryFileSystemWrapper $PrimaryFileSystemWrapper = null, array $config = null)
73
    {
74
        if (!empty($PrimaryFileSystemWrapper)) {
75
            $this->fileSystem = $PrimaryFileSystemWrapper->getFileSystem();
76
        }
77
        $this->config = $config;
78
    }
79
80
    /**
81
     * @param                     $key
82
     * @param UrlEncoderInterface $encoder
83
     */
84
    public function setAdapterUrlEncoder($key, UrlEncoderInterface $encoder)
85
    {
86
        $this->adapterUrlEncoders[$key] = $encoder;
87
    }
88
89
    /**
90
     * @param        $relativeFilePath
91
     * @param array  $urlData
92
     *
93
     * @return string
94
     * @internal param string $adapterUrlData
95
     */
96
    public function filePublicUrl($relativeFilePath, array $urlData = [])
97
    {
98
        // @TODO: $urlData could be collected when
99
        // Either the data needed to build the url is passed in adapterUrlData
100
        // Or it should be derived from the Adapter
101
102
        // Build path from the provided data if it exists
103
        if (!empty($urlData)) {
104
            return $this->formatAsUrl($urlData, $relativeFilePath);
105
        }
106
107
        // Build the url from any mappings provided.
108
        $adapterType = $this->getAdapterTypeFromFilesystem();
109
        if (!empty($this->getAdapterUrlMappings($adapterType))) {
110
            return $this->formatAsUrl($urlData, $relativeFilePath);
111
        }
112
113
        // Build the path from adapter.
114
        // Most adaptors don't have any direct method to do this.
115
        $urlData = $this->getUrlFromFileSystem();
116
        if (!empty($urlData)) {
117
            return $this->formatAsUrl($urlData, $relativeFilePath);
118
        }
119
120
        // Use a fallback from config.
121
        if (!empty($this->config['default_url'])) {
122
            return $this->formatAsUrl($this->config['default_url'], $relativeFilePath);
123
        }
124
125
        // If all of the above methods fail just return the path.
126
        // perhaps it being generated elsewhere!!
127
128
        return $relativeFilePath;
129
    }
130
131
    private function getAdapterTypeFromFilesystem()
132
    {
133
        $adapter = $this->fileSystem->getAdapter();
134
135
        return $this->getAdapterType($adapter);
136
    }
137
138
    /**
139
     * @param $base
140
     * @param $path
141
     *
142
     * @return string
143
     */
144
    protected function formatAsUrl($base, $path)
145
    {
146
        // @TODO: $base could also be an array.
147
148
        $url = $base . '/' . trim($path, '/');
149
150
        // Check it the protocol is included.
151
        $urlBits = explode('://', $url);
152
        if ($urlBits[0] == 'http' || $urlBits[0] == 'https') {
153
            $urlPrefix = $urlBits[0] . '://';
154
            $urlPath   = $urlBits[1];
155
        }
156
        else {
157
            $urlPrefix = '/';
158
            $urlPath   = $url;
159
        }
160
161
        // Format the url part to ensure its correct.
162
        $urlParts = explode('/', $urlPath);
163
        foreach ($urlParts as $index => $part) {
164
            $part = trim($part);
165
            if (empty($part)) {
166
                unset($urlParts[$index]);
167
            }
168
        }
169
        $urlPath = implode('/', $urlParts);
170
171
        return $urlPrefix . $urlPath;
172
    }
173
174
    /**
175
     * @return string
176
     * @internal param array $data
177
     *
178
     */
179
    protected function getUrlFromFileSystem()
180
    {
181
        $path        = false;
182
        $adapter     = $this->fileSystem->getAdapter();
183
        $adapterType = $this->getAdapterType($adapter);
184
185
        if (!empty($this->adapterUrlEncoders[$adapterType])) {
186
            $encoder = $this->adapterUrlEncoders[$adapterType];
187
            $path    = $encoder->getUrl($adapter, $this->config);
188
        }
189
190
        return $path;
191
    }
192
193
    /**
194
     * @param AdapterInterface $adapter
195
     *
196
     * @return mixed
197
     */
198 View Code Duplication
    protected function getAdapterType(AdapterInterface $adapter)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
    {
200
        $class          = get_class($adapter);
201
        $namespaceArray = explode("\\", $class);
202
203
        return array_pop($namespaceArray);
204
    }
205
}