[17312] | 1 | <?php |
---|
[26180] | 2 | defined('FLICKR_PATH') or die('Hacking attempt!'); |
---|
[17312] | 3 | |
---|
| 4 | /** |
---|
[17476] | 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 | /** |
---|
[17312] | 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 | } |
---|
[26180] | 32 | |
---|
[17312] | 33 | $return = ($dest === true) ? true : false; |
---|
[26180] | 34 | |
---|
[17312] | 35 | /* curl */ |
---|
| 36 | if (function_exists('curl_init')) |
---|
| 37 | { |
---|
| 38 | if (!$return) |
---|
| 39 | { |
---|
| 40 | $newf = fopen($dest, "wb"); |
---|
| 41 | } |
---|
| 42 | $ch = curl_init(); |
---|
[26180] | 43 | |
---|
[17312] | 44 | curl_setopt($ch, CURLOPT_URL, $src); |
---|
[17476] | 45 | curl_setopt($ch, CURLOPT_HEADER, false); |
---|
[17312] | 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)'); |
---|
[17476] | 48 | curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
---|
[21288] | 49 | if (!ini_get('safe_mode')) |
---|
| 50 | { |
---|
| 51 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
---|
| 52 | curl_setopt($ch, CURLOPT_MAXREDIRS, 1); |
---|
| 53 | } |
---|
[17312] | 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 | } |
---|
[26180] | 67 | |
---|
[17476] | 68 | $out = curl_exec($ch); |
---|
| 69 | curl_close($ch); |
---|
[26180] | 70 | |
---|
[17476] | 71 | if ($out === false) |
---|
[17312] | 72 | { |
---|
| 73 | return 'file_error'; |
---|
| 74 | } |
---|
[17476] | 75 | else if (!$return) |
---|
[17312] | 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 | } |
---|
[26180] | 92 | |
---|
[17312] | 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); |
---|
[26180] | 102 | |
---|
[17312] | 103 | if (($file = file_get_contents($src, false, $context)) === false) |
---|
| 104 | { |
---|
| 105 | return 'file_error'; |
---|
| 106 | } |
---|
[26180] | 107 | |
---|
[17312] | 108 | if (!$return) |
---|
| 109 | { |
---|
| 110 | file_put_contents($dest, $file); |
---|
| 111 | return true; |
---|
| 112 | } |
---|
| 113 | else |
---|
| 114 | { |
---|
| 115 | return $file; |
---|
| 116 | } |
---|
| 117 | } |
---|
[26180] | 118 | |
---|
[17312] | 119 | return false; |
---|
| 120 | } |
---|
| 121 | } |
---|