1 | <?php |
---|
2 | if (defined('PHPWG_ROOT_PATH')) return; /* Avoid direct usage under Piwigo */ |
---|
3 | if (!defined('PWGP_NAME')) return; /* Avoid unpredicted access */ |
---|
4 | //error_reporting(E_ALL); |
---|
5 | if (!isset($pwg_mode)) $pwg_mode = ''; // Remind which process is working well |
---|
6 | $pwg_prev_host = ''; // Remind last requested gallery |
---|
7 | |
---|
8 | function pwg_get_contents($url, $mode='') { |
---|
9 | |
---|
10 | global $pwg_mode, $pwg_prev_host; |
---|
11 | $timeout = 5; // will be a parameter (only for the socket) |
---|
12 | |
---|
13 | $host = (strtolower(substr($url,0,7)) == 'http://') ? substr($url,7) : $url; |
---|
14 | $host = (strtolower(substr($host,0,8)) == 'https://') ? substr($host,8) : $host; |
---|
15 | $doc = substr($host, strpos($host, '/')); |
---|
16 | $host = substr($host, 0, strpos($host, '/')); |
---|
17 | |
---|
18 | if ($pwg_prev_host != $host) $pwg_mode = ''; // What was possible with one website could be different with another |
---|
19 | $pwg_prev_host = $host; |
---|
20 | $mode = $pwg_mode; |
---|
21 | // $mode = 'ch'; // Forcing a test '' all, 'fs' fsockopen, 'ch' cURL |
---|
22 | |
---|
23 | // 1 - The simplest solution: file_get_contents |
---|
24 | // Contraint: php.ini |
---|
25 | // ; Whether to allow the treatment of URLs (like http:// or ftp://) as files. |
---|
26 | // allow_url_fopen = On |
---|
27 | if ( $mode == '' ) { |
---|
28 | if ( true === (bool) ini_get('allow_url_fopen') ) { |
---|
29 | $value = file_get_contents($url); |
---|
30 | if ( $value !== false) { |
---|
31 | return $value; |
---|
32 | } |
---|
33 | } |
---|
34 | } |
---|
35 | if ( $mode == '' ) $mode = 'fs'; |
---|
36 | if ( $pwg_mode == '' ) $pwg_mode = 'fs'; // Remind it |
---|
37 | // 2 - Often accepted access: fsockopen |
---|
38 | if ($mode == 'fs') { |
---|
39 | $fs = fsockopen($host, 80, $errno, $errstr, $timeout); |
---|
40 | if ( $fs !== false ) { |
---|
41 | fwrite($fs, 'GET ' . $doc . " HTTP/1.1\r\n"); |
---|
42 | fwrite($fs, 'Host: ' . $host . "\r\n"); |
---|
43 | fwrite($fs, "Connection: Close\r\n\r\n"); |
---|
44 | stream_set_blocking($fs, TRUE); |
---|
45 | stream_set_timeout($fs,$timeout); // Again the $timeout on the get |
---|
46 | $info = stream_get_meta_data($fs); |
---|
47 | $value = ''; |
---|
48 | while ((!feof($fs)) && (!$info['timed_out'])) { |
---|
49 | $value .= fgets($fs, 4096); |
---|
50 | $info = stream_get_meta_data($fs); |
---|
51 | flush(); |
---|
52 | } |
---|
53 | $value = substr($value, strpos($value,'a:2:{s:4:"stat";')); |
---|
54 | // echo '<br/>-fs- ('. $value . ') <br/>'; |
---|
55 | if ( $info['timed_out'] === false ) return $value; |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | if ( $pwg_mode == 'fs' ) $pwg_mode = 'ch'; // Remind it |
---|
60 | // 3 - Sometime another solution: curl_exec |
---|
61 | // See http://fr2.php.net/manual/en/curl.installation.php |
---|
62 | if (function_exists('curl_init') and $pwg_mode == 'ch') { |
---|
63 | $ch = @curl_init(); |
---|
64 | @curl_setopt($ch, CURLOPT_URL, $url); |
---|
65 | @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); |
---|
66 | @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); |
---|
67 | @curl_setopt($ch, CURLOPT_HEADER, 1); |
---|
68 | @curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0'); |
---|
69 | @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
---|
70 | @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
---|
71 | $value = @curl_exec($ch); |
---|
72 | $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
---|
73 | $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE); |
---|
74 | @curl_close($value); |
---|
75 | if ($value !== false and $status >= 200 and $status < 400) { |
---|
76 | $value = substr($value, $header_length); |
---|
77 | // echo '<br/>-ch- ('. $value . ') <br/>'; |
---|
78 | return $value; |
---|
79 | } |
---|
80 | else $pwg_mode = 'failed'; // Sorry but remind it as well |
---|
81 | } |
---|
82 | |
---|
83 | // No other solutions |
---|
84 | $return["stat"] = 'failed'; |
---|
85 | // echo '<br/>- pwg_get_contents: failed on file_get, fsockopen and cURL processes<br/>'; |
---|
86 | return serialize($return); |
---|
87 | } |
---|
88 | ?> |
---|