2008-01-30 20:33:16 +00:00
|
|
|
|
<?php
|
2006-12-02 04:36:16 +00:00
|
|
|
|
/*
|
|
|
|
|
+ ----------------------------------------------------------------------------+
|
|
|
|
|
| e107 website system
|
|
|
|
|
|
|
|
|
|
|
| <EFBFBD>Steve Dunstan 2001-2002
|
|
|
|
|
| http://e107.org
|
|
|
|
|
| jalist@e107.org
|
|
|
|
|
|
|
|
|
|
|
| Released under the terms and conditions of the
|
|
|
|
|
| GNU General Public License (http://gnu.org).
|
|
|
|
|
|
|
|
|
|
|
| $Source: /cvs_backup/e107_0.8/e107_handlers/xml_class.php,v $
|
2008-03-20 23:05:21 +00:00
|
|
|
|
| $Revision: 1.7 $
|
|
|
|
|
| $Date: 2008-03-20 23:05:21 $
|
|
|
|
|
| $Author: e107steved $
|
2006-12-02 04:36:16 +00:00
|
|
|
|
+----------------------------------------------------------------------------+
|
|
|
|
|
*/
|
|
|
|
|
|
2008-03-20 23:05:21 +00:00
|
|
|
|
/*
|
|
|
|
|
Update 20.3.08:
|
|
|
|
|
1. Configurable timeout in call
|
|
|
|
|
2. Sets socket timeout if possible on socket-based protocols
|
|
|
|
|
*/
|
2008-01-20 04:46:35 +00:00
|
|
|
|
class xmlClass
|
|
|
|
|
{
|
2006-12-02 04:36:16 +00:00
|
|
|
|
var $xmlFileContents;
|
|
|
|
|
|
2008-03-20 23:05:21 +00:00
|
|
|
|
function getRemoteFile($address, $timeout=10)
|
2006-12-02 04:36:16 +00:00
|
|
|
|
{
|
2008-03-20 23:05:21 +00:00
|
|
|
|
// Could do something like: if ($timeout <= 0) $timeout = $pref['get_remote_timeout']; here
|
|
|
|
|
$timeout = min($timeout,120);
|
|
|
|
|
$timeout = max($timeout,3);
|
|
|
|
|
|
|
|
|
|
$address = str_replace(array("\r","\n", "\t"),'',$address); // May be paranoia, but streaky thought it might be a good idea
|
|
|
|
|
// ... and there shouldn't be unprintable characters in the URL anyway
|
|
|
|
|
|
2008-01-20 04:46:35 +00:00
|
|
|
|
if(function_exists('file_get_contents'))
|
|
|
|
|
{
|
2008-03-20 23:05:21 +00:00
|
|
|
|
$old_timeout = e107_ini_set('default_socket_timeout', $timeout);
|
|
|
|
|
$data = file_get_contents(urlencode($address));
|
|
|
|
|
e107_ini_set('default_socket_timeout', $old_timeout);
|
|
|
|
|
if ($data)
|
|
|
|
|
{
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-12-02 04:36:16 +00:00
|
|
|
|
if(function_exists("curl_init"))
|
|
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
|
$cu = curl_init ();
|
2006-12-02 04:36:16 +00:00
|
|
|
|
curl_setopt($cu, CURLOPT_URL, $address);
|
|
|
|
|
curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
|
curl_setopt ($cu, CURLOPT_HEADER, 0);
|
2008-03-20 23:05:21 +00:00
|
|
|
|
curl_setopt ($cu, CURLOPT_TIMEOUT, $timeout);
|
2008-01-20 04:46:35 +00:00
|
|
|
|
$this->xmlFileContents = curl_exec($cu);
|
2006-12-02 04:36:16 +00:00
|
|
|
|
if (curl_error($cu))
|
|
|
|
|
{
|
|
|
|
|
$this -> error = "Error: ".curl_errno($cu).", ".curl_error($cu);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
curl_close ($cu);
|
2008-01-20 04:46:35 +00:00
|
|
|
|
return $this->xmlFileContents;
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(ini_get("allow_url_fopen"))
|
|
|
|
|
{
|
2008-03-20 23:05:21 +00:00
|
|
|
|
$old_timeout = e107_ini_set('default_socket_timeout', $timeout);
|
|
|
|
|
$remote = @fopen ($address, "r");
|
|
|
|
|
e107_ini_set('default_socket_timeout', $old_timeout);
|
|
|
|
|
if(!$remote)
|
|
|
|
|
{
|
|
|
|
|
$this -> error = "Unable to open remote XML file.";
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-03-20 23:05:21 +00:00
|
|
|
|
$tmp = parse_url($address);
|
|
|
|
|
if(!$remote = fsockopen ($tmp['host'], 80 ,$errno, $errstr, $timeout))
|
|
|
|
|
{
|
|
|
|
|
$this -> error = "Unable to open remote XML file.";
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
socket_set_timeout($remote, $timeout);
|
|
|
|
|
fputs($remote, "GET ".urlencode($address)." HTTP/1.0\r\n\r\n");
|
|
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this -> xmlFileContents = "";
|
|
|
|
|
while (!feof($remote))
|
|
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
|
$this->xmlFileContents .= fgets ($remote, 4096);
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
|
|
|
|
fclose ($remote);
|
2008-01-20 04:46:35 +00:00
|
|
|
|
return $this->xmlFileContents;
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-20 23:05:21 +00:00
|
|
|
|
|
|
|
|
|
|
2008-01-20 04:46:35 +00:00
|
|
|
|
function parseXml($xml='')
|
2006-12-02 04:36:16 +00:00
|
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
|
if($xml == '' && $this->xmlFileContents)
|
2006-12-02 04:36:16 +00:00
|
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
|
$xml = $this->xmlFileContents;
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
|
if(!$xml)
|
2006-12-02 04:36:16 +00:00
|
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
|
return false;
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-01-20 04:46:35 +00:00
|
|
|
|
$xml = simplexml_load_string($xml);
|
|
|
|
|
if(is_object($xml))
|
2006-12-02 04:36:16 +00:00
|
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
|
$xml = (array)$xml;
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
|
$xml = $this->xml_convert_to_array($xml);
|
|
|
|
|
return $xml;
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-01-20 04:46:35 +00:00
|
|
|
|
function xml_convert_to_array($xml)
|
2006-12-02 04:36:16 +00:00
|
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
|
if(is_array($xml))
|
2006-12-02 04:36:16 +00:00
|
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
|
foreach($xml as $k => $v)
|
|
|
|
|
{
|
|
|
|
|
if(is_object($v))
|
|
|
|
|
{
|
|
|
|
|
$v = (array)$v;
|
|
|
|
|
}
|
|
|
|
|
$xml[$k] = $this->xml_convert_to_array($v);
|
|
|
|
|
}
|
|
|
|
|
if(count($xml) == 1 && isset($xml[0]))
|
|
|
|
|
{
|
|
|
|
|
$xml = $xml[0];
|
|
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
|
return $xml;
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-01 14:11:27 +00:00
|
|
|
|
function loadXMLfile($fname='', $parse = false, $replace_constants = false)
|
2006-12-02 04:36:16 +00:00
|
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
|
|
|
|
|
|
if($fname == '')
|
2006-12-02 04:36:16 +00:00
|
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
|
return false;
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
|
$xml = false;
|
2006-12-02 04:36:16 +00:00
|
|
|
|
|
2008-03-20 23:05:21 +00:00
|
|
|
|
if(strpos($filename, '://') !== false)
|
2006-12-02 04:36:16 +00:00
|
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
|
$this->getRemoteFile($fname);
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
|
else
|
2006-12-02 04:36:16 +00:00
|
|
|
|
{
|
2008-01-20 04:46:35 +00:00
|
|
|
|
if($xml = file_get_contents($fname))
|
|
|
|
|
{
|
|
|
|
|
$this->xmlFileContents = $xml;
|
|
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
|
if($this->xmlFileContents)
|
2006-12-02 04:36:16 +00:00
|
|
|
|
{
|
2008-02-01 14:11:27 +00:00
|
|
|
|
if($replace_constants == true)
|
|
|
|
|
{
|
|
|
|
|
global $tp;
|
|
|
|
|
if(!is_object($tp))
|
|
|
|
|
{
|
|
|
|
|
require_once('e_parse_class.php');
|
|
|
|
|
$tp = new e_parse;
|
|
|
|
|
}
|
|
|
|
|
$this->xmlFileContents = $tp->replaceConstants($this->xmlFileContents, '', true);
|
|
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
|
if($parse == true)
|
|
|
|
|
{
|
|
|
|
|
return $this->parseXML();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return $this->xmlFileContents;
|
|
|
|
|
}
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
2008-01-20 04:46:35 +00:00
|
|
|
|
return false;
|
2006-12-02 04:36:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-01-30 20:33:16 +00:00
|
|
|
|
}
|
|
|
|
|
?>
|