Issues (5)

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/Providers/Provider.php (1 issue)

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 Irazasyed\VideoDownloader\Providers;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\RequestException;
7
use GuzzleHttp\Promise;
8
use GuzzleHttp\Promise\PromiseInterface;
9
use GuzzleHttp\RequestOptions;
10
use Irazasyed\VideoDownloader\Exceptions\VideoDownloaderException;
11
12
/**
13
 * Abstract Provider.
14
 */
15
abstract class Provider implements ProviderInterface
16
{
17
    /**
18
     * @var Client
19
     */
20
    protected $client;
21
22
    /**
23
     * @var PromiseInterface[]
24
     */
25
    private static $promises = [];
26
27
    /**
28
     * @var string
29
     */
30
    protected $body;
31
32
    /**
33
     * VideoLinkGenerator constructor.
34
     *
35
     * @param Client|null $client
36
     */
37
    public function __construct(Client $client = null)
38
    {
39
        $this->client = $client ?: new Client();
40
    }
41
42
    /**
43
     * Unwrap Promises.
44
     */
45
    public function __destruct()
46
    {
47
        Promise\unwrap(self::$promises);
48
    }
49
50
    /**
51
     * @param $url
52
     */
53
    abstract public function getVideoInfo($url);
54
55
    /**
56
     * Sets HTTP client.
57
     *
58
     * @param $client
59
     *
60
     * @return $this
61
     */
62
    public function setClient($client)
63
    {
64
        $this->client = $client;
65
66
        return $this;
67
    }
68
69
    /**
70
     * Gets HTTP client for internal class use.
71
     *
72
     * @return Client
73
     */
74
    public function getClient()
75
    {
76
        return $this->client;
77
    }
78
79
    /**
80
     * Returns Raw Response Body.
81
     *
82
     * @return string
83
     */
84
    public function getBody()
85
    {
86
        return $this->body;
87
    }
88
89
    /**
90
     * Get Page Source Code.
91
     *
92
     * @param $url
93
     *
94
     * @throws VideoDownloaderException
95
     */
96
    public function getSourceCode($url)
97
    {
98
        $response = $this->httpRequest($url);
99
100
        $status = $response->getStatusCode();
101
102
        if ($status === 200) {
103
            return $this->body = $response->getBody();
104
        }
105
106
        throw new VideoDownloaderException('Something went wrong, HTTP Status Code Returned: '.$status);
107
    }
108
109
    /**
110
     * Download remote file from server
111
     * and save it locally using HTTP Client.
112
     *
113
     * @param string $url            The URL to Remote File to Download.
114
     * @param string $dstFilename    Destination Filename (Accepts File Path too).
115
     * @param bool   $isAsyncRequest
116
     *
117
     * @return string
118
     */
119
    public function download($url, $dstFilename, $isAsyncRequest = false)
120
    {
121
        $baseDir = dirname($dstFilename);
122
        if (!is_writable($baseDir)) {
123
            @mkdir($baseDir, 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
124
        }
125
126
        $this->httpRequest($url, ['sink' => $dstFilename], $isAsyncRequest);
127
128
        return ['file_path' => $dstFilename];
129
    }
130
131
    /**
132
     * Make a HTTP Request.
133
     *
134
     * @param            $url
135
     * @param array      $options
136
     * @param bool|false $isAsyncRequest
137
     *
138
     * @return mixed
139
     */
140
    private function httpRequest($url, array $options = [], $isAsyncRequest = false)
141
    {
142
        if ($url == null || trim($url) == '') {
143
            return 'URL was invalid.';
144
        }
145
146
        $options = $this->getOptions($this->defaultHeaders(), $options, $isAsyncRequest);
147
148
        try {
149
            $response = $this->client->getAsync($url, $options);
150
151
            if ($isAsyncRequest) {
152
                self::$promises[] = $response;
153
            } else {
154
                $response = $response->wait();
155
            }
156
        } catch (RequestException $e) {
157
            return 'There was an error while processing the request';
158
        }
159
160
        return $response;
161
    }
162
163
    /**
164
     * Prepares and returns request options.
165
     *
166
     * @param array $headers
167
     * @param       $options
168
     * @param       $isAsyncRequest
169
     *
170
     * @return array
171
     */
172
    private function getOptions(array $headers, $options = [], $isAsyncRequest = false)
173
    {
174
        $default_options = [
175
            RequestOptions::HEADERS     => $headers,
176
            RequestOptions::SYNCHRONOUS => !$isAsyncRequest,
177
        ];
178
179
        return array_merge($default_options, $options);
180
    }
181
182
    /**
183
     * Returns Default Headers for HTTP Client.
184
     *
185
     * @return array
186
     */
187
    protected function defaultHeaders()
188
    {
189
        return [
190
            'User-Agent'      => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36',
191
            'Accept-Language' => 'en-US,en;q=0.8,sr;q=0.6,pt;q=0.4',
192
        ];
193
    }
194
195
    /**
196
     * Decode Unicode Sequences.
197
     *
198
     * @param $str
199
     *
200
     * @return mixed
201
     */
202
    protected function decodeUnicode($str)
203
    {
204
        return preg_replace_callback(
205
            '/\\\\u([0-9a-f]{4})/i',
206
            [$this, 'replace_unicode_escape_sequence'],
207
            $str
208
        );
209
    }
210
211
    /**
212
     * Cleanup string to readible text.
213
     *
214
     * @param string $str
215
     *
216
     * @return string
217
     */
218
    protected function cleanStr($str)
219
    {
220
        return html_entity_decode(strip_tags($str), ENT_QUOTES, 'UTF-8');
221
    }
222
223
    /**
224
     * @param $uni
225
     *
226
     * @return bool|mixed|string
227
     */
228
    protected function replace_unicode_escape_sequence($uni)
229
    {
230
        return mb_convert_encoding(pack('H*', $uni[1]), 'UTF-8', 'UCS-2BE');
231
    }
232
}
233