1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\Skylab\Provider; |
4
|
|
|
|
5
|
|
|
use Cilex\Application; |
6
|
|
|
use Kunstmaan\Skylab\Exceptions\AccessDeniedException; |
7
|
|
|
|
8
|
|
|
class RemoteProvider extends AbstractProvider |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Registers services on the given app. |
13
|
|
|
* |
14
|
|
|
* @param Application $app An Application instance |
15
|
|
|
*/ |
16
|
|
|
public function register(Application $app) |
17
|
|
|
{ |
18
|
|
|
$app['remote'] = $this; |
19
|
|
|
$this->app = $app; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param $url |
24
|
|
|
* @param string $contentType |
|
|
|
|
25
|
|
|
* @param string $filename |
|
|
|
|
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function curl($url, $contentType = null, $filename = null, $cacheTimeInSeconds = 0, $username = null, $password = null) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$ch = curl_init(); |
31
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
32
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false); |
33
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
34
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
35
|
|
|
curl_setopt($ch, CURLOPT_REFERER, $url); |
36
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
37
|
|
|
if($username && $password){ |
38
|
|
|
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); |
39
|
|
|
} |
40
|
|
|
if ($contentType) { |
|
|
|
|
41
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: " . $contentType)); |
42
|
|
|
} |
43
|
|
|
$tmpfile = $this->setDownloadHeaders($filename, $ch); |
44
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, "Skylab " . Application::VERSION . " (https://github.com/Kunstmaan/skylab)"); |
45
|
|
|
$result = curl_exec($ch); |
46
|
|
|
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
47
|
|
|
if($http_status == 403) { |
48
|
|
|
throw new AccessDeniedException(); |
49
|
|
|
} |
50
|
|
|
curl_close($ch); |
51
|
|
|
if ($filename) { |
|
|
|
|
52
|
|
|
$this->closeFile($tmpfile); |
53
|
|
|
} else { |
54
|
|
|
return $result; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string|null $filename |
62
|
|
|
* @param resource $ch |
63
|
|
|
*/ |
64
|
|
|
private function setDownloadHeaders($filename, $ch) |
65
|
|
|
{ |
66
|
|
|
if ($filename) { |
|
|
|
|
67
|
|
|
$tempFP = fopen($filename, 'w+'); |
68
|
|
|
curl_setopt($ch, CURLOPT_FILE, $tempFP); |
69
|
|
|
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); |
70
|
|
|
|
71
|
|
|
return $tempFP; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function closeFile($tempFP) |
78
|
|
|
{ |
79
|
|
|
if (!$tempFP) { |
80
|
|
|
fclose($tempFP); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.