1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-02 12:48:26 +02:00

Save memory by stripping comments; improve timeouts on getting remote files

This commit is contained in:
e107steved
2008-12-02 20:14:47 +00:00
parent 3d61d07e8c
commit de9f0a2158

View File

@@ -11,8 +11,8 @@
| GNU General Public License (http://gnu.org). | GNU General Public License (http://gnu.org).
| |
| $Source: /cvs_backup/e107_0.8/e107_handlers/xml_class.php,v $ | $Source: /cvs_backup/e107_0.8/e107_handlers/xml_class.php,v $
| $Revision: 1.8 $ | $Revision: 1.9 $
| $Date: 2008-08-25 10:46:33 $ | $Date: 2008-12-02 20:14:47 $
| $Author: e107steved $ | $Author: e107steved $
+----------------------------------------------------------------------------+ +----------------------------------------------------------------------------+
*/ */
@@ -21,7 +21,7 @@ class xmlClass
{ {
var $xmlFileContents; var $xmlFileContents;
var $filter; // Optional filter for loaded XML var $filter; // Optional filter for loaded XML
// Set to FALSE if not enabled (default position) // Set to FALSE if not enabled (default on initialisation)
// Otherwise mirrors the required subset of the loaded XML - set a field FALSE to accept all // Otherwise mirrors the required subset of the loaded XML - set a field FALSE to accept all
// ...elements lower down the tree. e.g.: // ...elements lower down the tree. e.g.:
// $filter = array( // $filter = array(
@@ -29,6 +29,7 @@ class xmlClass
// 'administration' => FALSE, // 'administration' => FALSE,
// 'management' => array('install' => FALSE) // 'management' => array('install' => FALSE)
// ); // );
var $stripComments; // Set true to strip all mention of comments from the returned array (default); FALSE to return comment markers
// Constructor - set defaults // Constructor - set defaults
@@ -36,6 +37,7 @@ class xmlClass
{ {
$this->xmlFileContents = ''; $this->xmlFileContents = '';
$this->filter = FALSE; $this->filter = FALSE;
$this->stripComments = TRUE; // Don't usually want comments back
} }
function getRemoteFile($address, $timeout=10) function getRemoteFile($address, $timeout=10)
@@ -50,8 +52,9 @@ class xmlClass
if(function_exists('file_get_contents')) if(function_exists('file_get_contents'))
{ {
$old_timeout = e107_ini_set('default_socket_timeout', $timeout); $old_timeout = e107_ini_set('default_socket_timeout', $timeout);
$data = file_get_contents(urlencode($address)); // $data = file_get_contents(urlencode($address));
e107_ini_set('default_socket_timeout', $old_timeout); $data = file_get_contents(htmlspecialchars($address)); // PHP manual says to use urlencode() - but this seems to work better
if ($old_timeout !== FALSE) { e107_ini_set('default_socket_timeout', $old_timeout); }
if ($data) if ($data)
{ {
return $data; return $data;
@@ -60,7 +63,7 @@ class xmlClass
if(function_exists("curl_init")) if(function_exists("curl_init"))
{ {
$cu = curl_init (); $cu = curl_init();
curl_setopt($cu, CURLOPT_URL, $address); curl_setopt($cu, CURLOPT_URL, $address);
curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1); curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($cu, CURLOPT_HEADER, 0); curl_setopt ($cu, CURLOPT_HEADER, 0);
@@ -68,30 +71,30 @@ class xmlClass
$this->xmlFileContents = curl_exec($cu); $this->xmlFileContents = curl_exec($cu);
if (curl_error($cu)) if (curl_error($cu))
{ {
$this -> error = "Error: ".curl_errno($cu).", ".curl_error($cu); $this -> error = "Curl error: ".curl_errno($cu).", ".curl_error($cu);
return FALSE; return FALSE;
} }
curl_close ($cu); curl_close($cu);
return $this->xmlFileContents; return $this->xmlFileContents;
} }
if(ini_get("allow_url_fopen")) if(ini_get("allow_url_fopen"))
{ {
$old_timeout = e107_ini_set('default_socket_timeout', $timeout); $old_timeout = e107_ini_set('default_socket_timeout', $timeout);
$remote = @fopen ($address, "r"); $remote = @fopen($address, "r");
e107_ini_set('default_socket_timeout', $old_timeout);
if(!$remote) if(!$remote)
{ {
$this -> error = "Unable to open remote XML file."; $this -> error = "fopen: Unable to open remote XML file.";
return FALSE; return FALSE;
} }
} }
else else
{ {
$old_timeout = $timeout;
$tmp = parse_url($address); $tmp = parse_url($address);
if(!$remote = fsockopen ($tmp['host'], 80 ,$errno, $errstr, $timeout)) if(!$remote = fsockopen ($tmp['host'], 80 ,$errno, $errstr, $timeout))
{ {
$this -> error = "Unable to open remote XML file."; $this -> error = "Sockets: Unable to open remote XML file.";
return FALSE; return FALSE;
} }
else else
@@ -107,6 +110,10 @@ class xmlClass
$this->xmlFileContents .= fgets ($remote, 4096); $this->xmlFileContents .= fgets ($remote, 4096);
} }
fclose ($remote); fclose ($remote);
if ($old_timeout != $timeout)
{
if ($old_timeout !== FALSE) { e107_ini_set('default_socket_timeout', $old_timeout); }
}
return $this->xmlFileContents; return $this->xmlFileContents;
} }
@@ -128,16 +135,21 @@ class xmlClass
{ {
$xml = (array)$xml; $xml = (array)$xml;
} }
$xml = $this->xml_convert_to_array($xml, $this->filter); $xml = $this->xml_convert_to_array($xml, $this->filter, $this->stripComments);
return $xml; return $xml;
} }
function xml_convert_to_array($xml, $localFilter = FALSE) function xml_convert_to_array($xml, $localFilter = FALSE, $stripComments = TRUE)
{ {
if(is_array($xml)) if(is_array($xml))
{ {
foreach($xml as $k => $v) foreach($xml as $k => $v)
{ {
if ($stripComments && ($k === 'comment'))
{
unset($xml[$k]);
continue;
}
$enabled = FALSE; $enabled = FALSE;
if ($localFilter === FALSE) if ($localFilter === FALSE)
{ {
@@ -155,7 +167,7 @@ class xmlClass
{ {
$v = (array)$v; $v = (array)$v;
} }
$xml[$k] = $this->xml_convert_to_array($v, $onFilter); $xml[$k] = $this->xml_convert_to_array($v, $onFilter, $stripComments);
} }
else else
{ {