Changeset 2881 for branches/2.0/admin/include/functions.php
- Timestamp:
- Nov 15, 2008, 10:11:58 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.0/admin/include/functions.php
r2776 r2881 1923 1923 } 1924 1924 1925 /** 1926 * Retrieve data from external URL 1927 * 1928 * @param string $src: URL 1929 * @param global $dest: can be a file ressource or string 1930 * @return bool 1931 */ 1932 function fetchRemote($src, &$dest, $user_agent='Piwigo', $step=0) 1933 { 1934 is_resource($dest) or $dest = ''; 1935 1936 // Try curl to read remote file 1937 if (function_exists('curl_init')) 1938 { 1939 $ch = @curl_init(); 1940 @curl_setopt($ch, CURLOPT_URL, $src); 1941 @curl_setopt($ch, CURLOPT_HEADER, 0); 1942 @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 1943 is_resource($dest) ? 1944 @curl_setopt($ch, CURLOPT_FILE, $dest): 1945 @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 1946 $content = @curl_exec($ch); 1947 @curl_close($ch); 1948 if ($content !== false) 1949 { 1950 is_resource($dest) or $dest = $content; 1951 return true; 1952 } 1953 } 1954 1955 // Try file_get_contents to read remote file 1956 if (ini_get('allow_url_fopen')) 1957 { 1958 $content = @file_get_contents($src); 1959 if ($content !== false) 1960 { 1961 is_resource($dest) ? @fwrite($dest, $content) : $dest = $content; 1962 return true; 1963 } 1964 } 1965 1966 // Try fsockopen to read remote file 1967 if ($step > 3) 1968 { 1969 return false; 1970 } 1971 1972 $src = parse_url($src); 1973 $host = $src['host']; 1974 $path = isset($src['path']) ? $src['path'] : '/'; 1975 $path .= isset($src['query']) ? '?'.$src['query'] : ''; 1976 1977 if (($s = @fsockopen($host,80,$errno,$errstr,5)) === false) 1978 { 1979 return false; 1980 } 1981 1982 fwrite($s, 1983 "GET ".$path." HTTP/1.0\r\n" 1984 ."Host: ".$host."\r\n" 1985 ."User-Agent: ".$user_agent."\r\n" 1986 ."Accept: */*\r\n" 1987 ."\r\n" 1988 ); 1989 1990 $i = 0; 1991 $in_content = false; 1992 while (!feof($s)) 1993 { 1994 $line = fgets($s); 1995 1996 if (rtrim($line,"\r\n") == '' && !$in_content) 1997 { 1998 $in_content = true; 1999 $i++; 2000 continue; 2001 } 2002 if ($i == 0) 2003 { 2004 if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/',rtrim($line,"\r\n"), $m)) 2005 { 2006 fclose($s); 2007 return false; 2008 } 2009 $status = (integer) $m[2]; 2010 if ($status < 200 || $status >= 400) 2011 { 2012 fclose($s); 2013 return false; 2014 } 2015 } 2016 if (!$in_content) 2017 { 2018 if (preg_match('/Location:\s+?(.+)$/',rtrim($line,"\r\n"),$m)) 2019 { 2020 fclose($s); 2021 return fetchRemote(trim($m[1]),$dest,$user_agent,$step+1); 2022 } 2023 $i++; 2024 continue; 2025 } 2026 is_resource($dest) ? @fwrite($dest, $line) : $dest .= $line; 2027 $i++; 2028 } 2029 fclose($s); 2030 return true; 2031 } 2032 1925 2033 ?>
Note: See TracChangeset
for help on using the changeset viewer.