source: extensions/piwigopress/PiwigoPress_get.php @ 4193

Last change on this file since 4193 was 4125, checked in by vdigital, 14 years ago

[PiwigoPress] = 1.04 =

  • Alternate pwg_get_contents (file_get_contents, fsockopen, ...)
  • cURL is coming soon
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line 
1<?php
2if (defined('PHPWG_ROOT_PATH')) return; /* Avoid direct usage under Piwigo */
3if (!defined('PWGP_NAME')) return; /* Avoid unpredicted access */
4//error_reporting(E_ALL);
5if (!isset($pwg_mode)) $pwg_mode = ''; // Remind which process is working well
6$pwg_prev_host = ''; // Remind last requested gallery
7
8function 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';
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        $return["stat"] = 'failed';
59        $pwg_mode = 'failed';
60        return serialize($return);
61
62// Not active cURL right now
63        if ( $pwg_mode == 'fs' ) $pwg_mode = 'ch';
64// 3 - Sometime another solution: curl_exec
65// See http://fr2.php.net/manual/en/curl.installation.php
66  if (function_exists('curl_init') and $pwg_mode == 'ch') {
67                $ch = @curl_init();
68                @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
69                @curl_setopt($ch, CURLOPT_URL, $url);
70                @curl_setopt($ch, CURLOPT_HEADER, 1);
71                @curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
72                @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
73                @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
74                $value = @curl_exec($ch);
75                $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE);
76                $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
77                @curl_close($value);
78                if ($value !== false and $status >= 200 and $status < 400)
79                                $value = substr($value, $header_length);
80        }
81
82        // No other solutions
83        $return["stat"] = 'failed';
84        echo '<br/>- pwg_get_contents: failed on file_get, fsockopen and cURL processes<br/>';
85        $pwg_mode = 'failed';
86        return serialize($return);
87}
88?>
Note: See TracBrowser for help on using the repository browser.