Using XML Parser without allow url fopen

After many hours of frustration with the pear module XML_RSS (which depends on XML_Parser), I finally figured out that Dreamhost turns off allow_url_fopen for good security reasons.

They have, thankfully, allowed libcurl connections. Alas, XML_Parser (which is where the http open was failing) doesn't fallback to libcurl, so I had to hack it.

Starting at line 698 inside the setInputFile function of Parser.php, this is what I now have: if (eregi('^(http|ftp)://', substr($file, 0, 10))) { if (!ini_get('allow_url_fopen')) { ## time to do the curl hack $ch = curl_init($file); $fp = tmpfile; curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp);

$file = $tempFile;

# return $this->raiseError('Remote files cannot be parsed, as safe mode is enabled.', XML_PARSER_ERROR_REMOTE); }       }

If you are using the later version of the XML_Parser From http://pear.php.net/package/XML_Parser You may notice that there is not even 698 lines, They changed some of their error coding. Ver 1.2.7 will look like this. Starting at line 379
 * 1) An UPDATE ############################

if (eregi('^(http|ftp)://', substr($file, 0, 10))) { if (!ini_get('allow_url_fopen')) { ## time to do the curl hack $ch = curl_init($file); $fp = tmpfile; curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp);

$file = $tempFile;

# return $this->raiseError('Remote files cannot be parsed, as safe mode is enabled.', XML_PARSER_ERROR_REMOTE); }       }        $fp = @fopen($file, 'rb'); if (is_resource($fp)) { $this->fp = $fp; return $fp; }       return $this->raiseError('File could not be opened.', XML_PARSER_ERROR_FILE_NOT_READABLE); }

Though I've got many years of experience hacking php, I do not work for Dreamhost, nor do I get anything for posting on their wiki. With that said, If you have questions or need help getting this (or PHP's PEAR on Dreamhost) to work, feel free to email me at [mailto:mccorkle@devteam.org McCorkle@devteam.org].

For a PHP-based XML, RSS 1.0, RSS 2.0, and Atom feed parser see the Pear XML Feed Parser 

Another way
Look for the setInputFile function of Parser.php (XML folder) and change it as follows function setInputFile($file) {        /**         * check, if file is a remote file */       if (eregi('^(http|ftp)://', substr($file, 0, 10))) { if (!ini_get('allow_url_fopen')) { //tiempo para hacer el hack del curl $ch = curl_init; curl_setopt($ch, CURLOPT_URL, $file); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $fp=curl_exec($ch);

//return $this->raiseError('Remote files cannot be parsed, as safe mode is enabled.', XML_PARSER_ERROR_REMOTE); }       }

//$fp = @fopen($file, 'rb'); //if (is_resource($fp)) { //$this->fp = $fp; //return $fp; //}

if(empty($fp)==false) {         $this->fp = $fp; return $fp; }

return $this->raiseError('File could not be opened.', XML_PARSER_ERROR_FILE_NOT_READABLE); }