Need help with a script and allow_URL_fopen

software development

#1

I am having problems getting a WordPress plugin to work because of allow_URL_fopen being turned off. Can a PHP guru take a look at the following function and see if you can figure out what is causing the problem and how to change it so it will work on DreamHost? I know part of it needs to be changed to

<?php $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, 'http://example.com'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); echo $file_contents; ?>Note: $file is a URL

[code]<?
/* Function that reads file from the cache if it is younger than a certain age and otherwise grabs a fresh copy of it from remote source */
function cached_get_file_contents ($file, $file_mode, $cache_timeout = 3600, $cache_path = ‘…/’, $debug = false)
{
clearstatcache(); // Clear file stats so we get an accurate file creation time for cache files

if (function_exists(‘sha1’)) // Prefer SHA1 because collision space is much larger than MD5, but MD5 will work too
{
$cache_filename = $cache_path . “/” . sha1($file) .".cached"; // Path to cached file
}
else
{
$cache_filename = $cache_path . “/” . md5($file) .".cached"; // Path to cached file
}

if ($debug)
{
print “local_cache creation_time =” . @filemtime($cache_filename) . " actual time = " . time() . " timeout = " . $timeout_seconds ."\n";
}

if (( @file_exists($cache_filename) && (( @filemtime($cache_filename) + $cache_timeout) > ( time())))) // If file exists and is young enough then do nothing because it will return file contents of cache
{
if ($debug)
{
print “using cached file ($cache_filename)\n”;
}
}
else // If file doesn’t exist or is too old get a fresh copy from remote source
{
if ($debug)
{
print “cacheing file ($file) to local ($cache_filename)\n”;
}

$f = @fopen($file,“r”); // Open remote source file
if (!$f) // Remote file couldn’t be opened
{

return @file_get_contents ($cache_filename); // Return the file contents form the cache
}

$f2 = @fopen($cache_filename,“w+”); // Open local cache file
while ($r = @fread($f,8192)) // Loop through remote source file
{
@fwrite($f2,$r); // Write to the local cache
}
@fclose($f2);
@fclose($f);
}

return file_get_contents ($cache_filename); // Return the file contents from the cache
}
?>[/code]


#2

I’ve tried to rewrite your script to use CURL. I haven’t tested the script, so please remember to backup the source file.

Replace:

$f = @fopen($file,"r"); // Open remote source file if (!$f) // Remote file couldn't be opened { return @file_get_contents ($cache_filename); // Return the file contents form the cache } $f2 = @fopen($cache_filename,"w+"); // Open local cache file while ($r = @fread($f,8192)) // Loop through remote source file { @fwrite($f2,$r); // Write to the local cache } @fclose($f2); @fclose($f);
With:

[code]$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $file);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

// $f = @fopen($file,“r”); // Open remote source file
if (!$file_contents) // Remote file couldn’t be opened
{
return @file_get_contents ($cache_filename); // Return the file contents form the cache
} else {
$f2 = @fopen($cache_filename,“w+”); // Open local cache file
@fwrite($f2, $file_contents); // Write to the local cache
@fclose($f2);
return $file_contents;
}
// while ($r = @fread($f,8192)) // Loop through remote source file
// {
// @fwrite($f2,$r); // Write to the local cache
// }
// @fclose($f2);
// @fclose($f);
[/code]


[color=#000000]DreamHost Promo Code[/color]: [color=#CC0000]Save $50[/color] with the promo code DREAMTEAM