1 | <?php |
---|
2 | defined('FLICKR_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | /** |
---|
5 | * test if a download method is available |
---|
6 | * @return: bool |
---|
7 | */ |
---|
8 | if (!function_exists('test_remote_download')) |
---|
9 | { |
---|
10 | function test_remote_download() |
---|
11 | { |
---|
12 | return function_exists('curl_init') || ini_get('allow_url_fopen'); |
---|
13 | } |
---|
14 | } |
---|
15 | |
---|
16 | /** |
---|
17 | * download a remote file |
---|
18 | * - needs cURL or allow_url_fopen |
---|
19 | * - take care of SSL urls |
---|
20 | * |
---|
21 | * @param: string source url |
---|
22 | * @param: mixed destination file (if true, file content is returned) |
---|
23 | */ |
---|
24 | if (!function_exists('download_remote_file')) |
---|
25 | { |
---|
26 | function download_remote_file($src, $dest) |
---|
27 | { |
---|
28 | if (empty($src)) |
---|
29 | { |
---|
30 | return false; |
---|
31 | } |
---|
32 | |
---|
33 | $return = ($dest === true) ? true : false; |
---|
34 | |
---|
35 | /* curl */ |
---|
36 | if (function_exists('curl_init')) |
---|
37 | { |
---|
38 | if (!$return) |
---|
39 | { |
---|
40 | $newf = fopen($dest, "wb"); |
---|
41 | } |
---|
42 | $ch = curl_init(); |
---|
43 | |
---|
44 | curl_setopt($ch, CURLOPT_URL, $src); |
---|
45 | curl_setopt($ch, CURLOPT_HEADER, false); |
---|
46 | curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-language: en")); |
---|
47 | curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'); |
---|
48 | curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
---|
49 | if (!ini_get('safe_mode')) |
---|
50 | { |
---|
51 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
---|
52 | curl_setopt($ch, CURLOPT_MAXREDIRS, 1); |
---|
53 | } |
---|
54 | if (strpos($src, 'https://') !== false) |
---|
55 | { |
---|
56 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
---|
57 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
---|
58 | } |
---|
59 | if (!$return) |
---|
60 | { |
---|
61 | curl_setopt($ch, CURLOPT_FILE, $newf); |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
---|
66 | } |
---|
67 | |
---|
68 | $out = curl_exec($ch); |
---|
69 | curl_close($ch); |
---|
70 | |
---|
71 | if ($out === false) |
---|
72 | { |
---|
73 | return 'file_error'; |
---|
74 | } |
---|
75 | else if (!$return) |
---|
76 | { |
---|
77 | fclose($newf); |
---|
78 | return true; |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | return $out; |
---|
83 | } |
---|
84 | } |
---|
85 | /* file get content */ |
---|
86 | else if (ini_get('allow_url_fopen')) |
---|
87 | { |
---|
88 | if (strpos($src, 'https://') !== false and !extension_loaded('openssl')) |
---|
89 | { |
---|
90 | return false; |
---|
91 | } |
---|
92 | |
---|
93 | $opts = array( |
---|
94 | 'http' => array( |
---|
95 | 'method' => "GET", |
---|
96 | 'user_agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)', |
---|
97 | 'header' => "Accept-language: en", |
---|
98 | ) |
---|
99 | ); |
---|
100 | |
---|
101 | $context = stream_context_create($opts); |
---|
102 | |
---|
103 | if (($file = file_get_contents($src, false, $context)) === false) |
---|
104 | { |
---|
105 | return 'file_error'; |
---|
106 | } |
---|
107 | |
---|
108 | if (!$return) |
---|
109 | { |
---|
110 | file_put_contents($dest, $file); |
---|
111 | return true; |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | return $file; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | return false; |
---|
120 | } |
---|
121 | } |
---|