1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ExternalResource.php |
4
|
|
|
* @author Revin Roman http://phptime.ru |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace rmrevin\yii\module\File\resources; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ExternalResource |
11
|
|
|
* @package rmrevin\yii\module\File\resources |
12
|
|
|
*/ |
13
|
|
|
class ExternalResource extends AbstractResource implements ResourceInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $temp; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $source; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $source |
24
|
|
|
*/ |
25
|
2 |
|
public function setSource($source) |
26
|
|
|
{ |
27
|
2 |
|
$this->source = $source; |
28
|
2 |
|
$this->temp = $this->copyTempResource(); |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
1 |
|
public function getName() |
35
|
|
|
{ |
36
|
1 |
|
return basename($this->source); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return int |
41
|
|
|
*/ |
42
|
1 |
|
public function getSize() |
43
|
|
|
{ |
44
|
1 |
|
return filesize($this->temp); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return string |
49
|
|
|
* @throws \yii\base\InvalidConfigException |
50
|
|
|
*/ |
51
|
1 |
|
public function getMime() |
52
|
|
|
{ |
53
|
1 |
|
return \yii\helpers\FileHelper::getMimeType($this->temp); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
1 |
|
public function getTemp() |
60
|
|
|
{ |
61
|
1 |
|
return $this->temp; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string|false |
66
|
|
|
*/ |
67
|
2 |
|
private function copyTempResource() |
68
|
|
|
{ |
69
|
2 |
|
if (false === $this->checkAvailable()) { |
70
|
1 |
|
throw new \RuntimeException('External resource not available'); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
$url = parse_url($this->source); |
74
|
1 |
|
$ext = pathinfo($url['path'], PATHINFO_EXTENSION); |
75
|
1 |
|
$temp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('file-') . '.' . $ext; |
76
|
|
|
|
77
|
1 |
|
$result = copy($this->source, $temp); |
78
|
1 |
|
@chmod($temp, 0664); |
|
|
|
|
79
|
|
|
|
80
|
1 |
|
return true === $result ? $temp : false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
2 |
|
private function checkAvailable() |
87
|
|
|
{ |
88
|
2 |
|
$result = false; |
89
|
|
|
|
90
|
2 |
|
$ch = curl_init($this->source); |
91
|
2 |
|
curl_setopt_array($ch, [ |
92
|
2 |
|
CURLOPT_HEADER => true, |
93
|
2 |
|
CURLOPT_FOLLOWLOCATION => true, |
94
|
2 |
|
CURLOPT_RETURNTRANSFER => true, |
95
|
2 |
|
CURLOPT_NOBODY => true, |
96
|
2 |
|
CURLOPT_TIMEOUT => 30, |
97
|
2 |
|
CURLOPT_SSL_VERIFYHOST => false, |
98
|
2 |
|
CURLOPT_SSL_VERIFYPEER => false, |
99
|
2 |
|
]); |
100
|
|
|
|
101
|
2 |
|
curl_exec($ch); |
102
|
|
|
|
103
|
2 |
|
if (!curl_errno($ch)) { |
104
|
1 |
|
$info = curl_getinfo($ch); |
105
|
|
|
|
106
|
1 |
|
if ($info['http_code'] === 200) { |
107
|
1 |
|
$result = true; |
108
|
1 |
|
} |
109
|
1 |
|
} |
110
|
|
|
|
111
|
2 |
|
curl_close($ch); |
112
|
|
|
|
113
|
2 |
|
return $result; |
114
|
|
|
} |
115
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: