Codice PHP:
<?php
error_reporting(99223372036854775807);
function fetch_web_data($url, $post_data = '', $keep_alive = false, $redirection_level = 0)
{
global $webmaster_email;
static $keep_alive_dom = null, $keep_alive_fp = null;
preg_match('~^(http|ftp)(s)?://([^/:]+)(:(\d+))?(.+)$~', $url, $match);
// An FTP url. We should try connecting and RETRieving it...
if (empty($match[1]))
return false;
elseif ($match[1] == 'ftp')
{
// Include the file containing the ftp_connection class.
loadClassFile('Class-Package.php');
// Establish a connection and attempt to enable passive mode.
$ftp = new ftp_connection(($match[2] ? 'ssl://' : '') . $match[3], empty($match[5]) ? 21 : $match[5], 'anonymous', $webmaster_email);
if ($ftp->error !== false || !$ftp->passive())
return false;
// I want that one *points*!
fwrite($ftp->connection, 'RETR ' . $match[6] . "\r\n");
// Since passive mode worked (or we would have returned already!) open the connection.
$fp = @fsockopen($ftp->pasv['ip'], $ftp->pasv['port'], $err, $err, 5);
if (!$fp)
return false;
// The server should now say something in acknowledgement.
$ftp->check_response(150);
$data = '';
while (!feof($fp))
$data .= fread($fp, 4096);
fclose($fp);
// All done, right? Good.
$ftp->check_response(226);
$ftp->close();
}
// This is more likely; a standard HTTP URL.
elseif (isset($match[1]) && $match[1] == 'http')
{
if ($keep_alive && $match[3] == $keep_alive_dom)
$fp = $keep_alive_fp;
if (empty($fp))
{
// Open the socket on the port we want...
$fp = @fsockopen(($match[2] ? 'ssl://' : '') . $match[3], empty($match[5]) ? ($match[2] ? 443 : 80) : $match[5], $err, $err, 5);
if (!$fp)
return false;
}
if ($keep_alive)
{
$keep_alive_dom = $match[3];
$keep_alive_fp = $fp;
}
// I want this, from there, and I'm not going to be bothering you for more (probably.)
if (empty($post_data))
{
fwrite($fp, 'GET ' . $match[6] . ' HTTP/1.0' . "\r\n");
fwrite($fp, 'Host: ' . $match[3] . (empty($match[5]) ? ($match[2] ? ':443' : '') : ':' . $match[5]) . "\r\n");
fwrite($fp, 'User-Agent: PHP/SMF' . "\r\n");
if ($keep_alive)
fwrite($fp, 'Connection: Keep-Alive' . "\r\n\r\n");
else
fwrite($fp, 'Connection: close' . "\r\n\r\n");
}
else
{
fwrite($fp, 'POST ' . $match[6] . ' HTTP/1.0' . "\r\n");
fwrite($fp, 'Host: ' . $match[3] . (empty($match[5]) ? ($match[2] ? ':443' : '') : ':' . $match[5]) . "\r\n");
fwrite($fp, 'User-Agent: PHP/SMF' . "\r\n");
if ($keep_alive)
fwrite($fp, 'Connection: Keep-Alive' . "\r\n");
else
fwrite($fp, 'Connection: close' . "\r\n");
fwrite($fp, 'Content-Type: application/x-www-form-urlencoded' . "\r\n");
fwrite($fp, 'Content-Length: ' . strlen($post_data) . "\r\n\r\n");
fwrite($fp, $post_data);
}
$response = fgets($fp, 768);
// Redirect in case this location is permanently or temporarily moved.
if ($redirection_level < 3 && preg_match('~^HTTP/\S+\s+30[127]~i', $response) === 1)
{
$header = '';
$location = '';
while (!feof($fp) && trim($header = fgets($fp, 4096)) != '')
if (strpos($header, 'Location:') !== false)
$location = trim(substr($header, strpos($header, ':') + 1));
if (empty($location))
return false;
else
{
if (!$keep_alive)
fclose($fp);
return fetch_web_data($location, $post_data, $keep_alive, $redirection_level + 1);
}
}
// Make sure we get a 200 OK.
elseif (preg_match('~^HTTP/\S+\s+20[01]~i', $response) === 0)
return false; //il false dovrebbe essere questo il problema, ma ripeto è SMF che sbaglia con la funzione scheduled_fetchSMfiles()
// Skip the headers...
while (!feof($fp) && trim($header = fgets($fp, 4096)) != '')
{
if (preg_match('~content-length:\s*(\d+)~i', $header, $match) != 0)
$content_length = $match[1];
elseif (preg_match('~connection:\s*close~i', $header) != 0)
{
$keep_alive_dom = null;
$keep_alive = false;
}
continue;
}
$data = '';
if (isset($content_length))
{
while (!feof($fp) && strlen($data) < $content_length)
$data .= fread($fp, $content_length - strlen($data));
}
else
{
while (!feof($fp))
$data .= fread($fp, 4096);
}
if (!$keep_alive)
fclose($fp);
}
else
{
// Umm, this shouldn't happen?
trigger_error('fetch_web_data(): Bad URL', E_USER_NOTICE);
$data = false; // qui non arriva
}
return $data;
}
var_dump(fetch_web_data('https://www.simplemachines.org/smf/current-version.js?version=SMF+2.1.1'));
?>