From: <dj...@us...> - 2012-01-22 16:14:28
|
Revision: 8776 http://xoops.svn.sourceforge.net/xoops/?rev=8776&view=rev Author: djculex Date: 2012-01-22 16:14:22 +0000 (Sun, 22 Jan 2012) Log Message: ----------- Found a nice function to retrieve remote content will attempt to use the fopen method first, then curl, then socket Modified Paths: -------------- XoopsModules/smallworld/trunk/smallworld/class/adminclass.php Modified: XoopsModules/smallworld/trunk/smallworld/class/adminclass.php =================================================================== --- XoopsModules/smallworld/trunk/smallworld/class/adminclass.php 2012-01-22 15:11:08 UTC (rev 8775) +++ XoopsModules/smallworld/trunk/smallworld/class/adminclass.php 2012-01-22 16:14:22 UTC (rev 8776) @@ -230,7 +230,7 @@ $rt = ''; $url = "http://www.culex.dk/updates/smallworld_version.csv"; - $fileC = file_get_contents($url); + $fileC = $this->fetchURL($url,array('fopen', 'curl', 'socket')); $read = explode(";", $fileC); $upd_img = $pathIcon16.'/on.png'; @@ -257,6 +257,86 @@ return $rt; } + // Fetch content of comma separated text file + // will attempt to use the fopen method first, then curl, then socket + // If fail all; return nothing + // On succes; Return string + function fetchURL($url, $methods = array('fopen', 'curl', 'socket')) { + /** + * December 21st 2010, Mathew Tinsley (ti...@ti...) + * http://tinsology.net + * + * To the extent possible under law, Mathew Tinsley has waived all copyright and related or + * neighboring rights to this work. There's absolutely no warranty. + */ + if(gettype($methods) == 'string') + $methods = array($methods); + elseif(!is_array($methods)) + return false; + foreach($methods as $method) + { + switch($method) + { + case 'fopen': + //uses file_get_contents in place of fopen + //allow_url_fopen must still be enabled + if(ini_get('allow_url_fopen')) + { + $contents = file_get_contents($url); + if($contents !== false) + return $contents; + } + break; + case 'curl': + if(function_exists('curl_init')) + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HEADER, 0); + return curl_exec($ch); + } + break; + case 'socket': + //make sure the url contains a protocol, otherwise $parts['host'] won't be set + if(strpos($url, 'http://') !== 0 && strpos($url, 'https://') !== 0) + $url = 'http://' . $url; + $parts = parse_url($url); + if($parts['scheme'] == 'https') + { + $target = 'ssl://' . $parts['host']; + $port = isset($parts['port']) ? $parts['port'] : 443; + } + else + { + $target = $parts['host']; + $port = isset($parts['port']) ? $parts['port'] : 80; + } + $page = isset($parts['path']) ? $parts['path'] : ''; + $page .= isset($parts['query']) ? '?' . $parts['query'] : ''; + $page .= isset($parts['fragment']) ? '#' . $parts['fragment'] : ''; + $page = ($page == '') ? '/' : $page; + if($fp = fsockopen($target, $port, $errno, $errstr, 15)) + { + $headers = "GET $page HTTP/1.1\r\n"; + $headers .= "Host: {$parts['host']}\r\n"; + $headers .= "Connection: Close\r\n\r\n"; + if(fwrite($fp, $headers)) + { + $resp = ''; + //while not eof and an error does not occur when calling fgets + while(!feof($fp) && ($curr = fgets($fp, 128)) !== false) + $resp .= $curr; + if(isset($curr) && $curr !== false) + return substr(strstr($resp, "\r\n\r\n"), 3); + } + fclose($fp); + } + break; + } + } + return false; + } + // flatten multidimentional arrays to one dimentional function flatten($array) { $return = array(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |