1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** This file is part of KCFinder project |
4
|
|
|
* |
5
|
|
|
* @desc Helper class for downloading URLs |
6
|
|
|
* @package KCFinder |
7
|
|
|
* @version 3.12 |
8
|
|
|
* @author Pavel Tzonkov <[email protected]> |
9
|
|
|
* @copyright 2010-2014 KCFinder Project |
10
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 |
11
|
|
|
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3 |
12
|
|
|
* @link http://kcfinder.sunhater.com |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace kcfinder; |
16
|
|
|
|
17
|
|
|
class phpGet |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
public static $methods = ['curl', 'fopen', 'http', 'socket']; |
21
|
|
|
public static $urlExpr = '/^([a-z]+):\/\/((([\p{L}\d\-]+\.)+[\p{L}]{1,4})(\:(\d{1,6}))?(\/.*)*)?$/u'; |
22
|
|
|
public static $socketExpr = '/^[A-Z]+\/\d+(\.\d+)\s+\d+\s+OK\s*([a-zA-Z0-9\-]+\:\s*[^\n]*\n)*\s*([a-f0-9]+\r?\n)?(.*)$/s'; |
23
|
|
|
|
24
|
|
|
public static function get($url, $file = null, $method = null) |
25
|
|
|
{ |
26
|
|
|
if (true === $file) { |
27
|
|
|
$file = basename($url); |
28
|
|
|
} |
29
|
|
|
if (null !== $file) { |
30
|
|
|
if (is_dir($file)) { |
31
|
|
|
$file = rtrim($file, '/') . '/' . basename($url); |
32
|
|
|
} |
33
|
|
|
$exists = file_exists($file); |
34
|
|
|
if (!@touch($file)) { |
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
if (!$exists) { |
38
|
|
|
@unlink($file); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (in_array($method, self::$methods, true)) { |
43
|
|
|
$check = "check_$method"; |
44
|
|
|
$get = "get_$method"; |
45
|
|
|
if (self::$check()) { |
46
|
|
|
$content = self::$get($url); |
47
|
|
|
} else { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
} else { |
51
|
|
|
foreach (self::$methods as $m) { |
52
|
|
|
$check = "check_$m"; |
53
|
|
|
$get = "get_$m"; |
54
|
|
|
if (self::$check()) { |
55
|
|
|
$content = self::$get($url); |
56
|
|
|
if (((true !== $method) && ('all' != strtolower($method))) |
57
|
|
|
|| (false !== $content)) { |
58
|
|
|
break; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
if (!isset($content)) { |
63
|
|
|
return false; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return (null !== $file) ? @file_put_contents($file, $content) : $content; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function get_fopen($url) |
71
|
|
|
{ |
72
|
|
|
return @file_get_contents($url); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function get_curl($url) |
76
|
|
|
{ |
77
|
|
|
return ((false !== ($curl = @curl_init($url))) |
78
|
|
|
&& (@ob_start() || (@curl_close($curl) && false)) |
|
|
|
|
79
|
|
|
&& (@curl_exec($curl) || (@curl_close($curl) && false)) |
|
|
|
|
80
|
|
|
&& ((false !== ($content = @ob_get_clean())) || (@curl_close($curl) && false)) |
|
|
|
|
81
|
|
|
&& (@curl_close($curl) || true)) ? $content : false; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public static function get_http($url) |
85
|
|
|
{ |
86
|
|
|
return ((false !== ($content = @http_get($url))) |
87
|
|
|
&& ((preg_match(self::$socketExpr, $content, $match) |
88
|
|
|
&& false !== ($content = $match[4])) |
89
|
|
|
|| true)) ? $content : false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public static function get_socket($url) |
93
|
|
|
{ |
94
|
|
|
if (!preg_match(self::$urlExpr, $url, $match)) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$protocol = $match[1]; |
99
|
|
|
$host = $match[3]; |
100
|
|
|
$port = strlen($match[6]) ? $match[6] : 80; |
101
|
|
|
$path = strlen($match[7]) ? $match[7] : '/'; |
102
|
|
|
|
103
|
|
|
$cmd = "GET $path " . strtoupper($protocol) . "/1.1\r\n" . "Host: $host\r\n" . "Connection: Close\r\n\r\n"; |
104
|
|
|
|
105
|
|
|
if ((false !== ($socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) |
106
|
|
|
&& (false !== @socket_connect($socket, $host, $port)) |
107
|
|
|
&& (false !== @socket_write($socket, $cmd, strlen($cmd))) |
108
|
|
|
&& (false !== ($content = @socket_read($socket, 2048)))) { |
109
|
|
|
do { |
110
|
|
|
$piece = @socket_read($socket, 2048); |
111
|
|
|
$content .= $piece; |
112
|
|
|
} while ($piece); |
113
|
|
|
|
114
|
|
|
$content = preg_match(self::$socketExpr, $content, $match) ? $match[4] : false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (isset($socket) && is_resource($socket)) { |
118
|
|
|
@socket_close($socket); |
|
|
|
|
119
|
|
|
} else { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return isset($content) ? $content : false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private static function check_fopen() |
127
|
|
|
{ |
128
|
|
|
return ini_get('allow_url_fopen') |
129
|
|
|
&& function_exists('file_get_contents'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private static function check_curl() |
133
|
|
|
{ |
134
|
|
|
return function_exists('curl_init') |
135
|
|
|
&& function_exists('curl_exec') |
136
|
|
|
&& function_exists('curl_close') |
137
|
|
|
&& function_exists('ob_start') |
138
|
|
|
&& function_exists('ob_get_clean'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private static function check_http() |
142
|
|
|
{ |
143
|
|
|
return function_exists('http_get'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private static function check_socket() |
147
|
|
|
{ |
148
|
|
|
return function_exists('socket_create') |
149
|
|
|
&& function_exists('socket_connect') |
150
|
|
|
&& function_exists('socket_write') |
151
|
|
|
&& function_exists('socket_read') |
152
|
|
|
&& function_exists('socket_close'); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: