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(); } 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); } } elseif (preg_match('~^HTTP/\\S+\\s+20[01]~i', $response) === 0) { return false; } // 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; } return $data; }
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') { // 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', '*****@*****.**'); if ($ftp->error !== false || !$ftp->passive()) { return false; } // I want that one *points*! $ftp->send_command('RETR ' . $match[6]); // Since passive mode worked (or we would have returned already!) open the connection. $fp = $ftp->connect_passive(); 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(); } elseif (isset($match[1]) && $match[1] == 'http') { // Figure out the origin (Host header, also for keepalive.) $origin = $match[3]; if (!empty($match[5])) { $origin .= ':' . $match[5]; } elseif (!empty($match[2])) { $origin .= ':443'; } if ($keep_alive && $origin == $keep_alive_dom) { $fp = $keep_alive_fp; } if (empty($fp) || feof($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 = $origin; $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"); } else { fwrite($fp, 'POST ' . $match[6] . ' HTTP/1.0' . "\r\n"); } fwrite($fp, 'Host: ' . $origin . "\r\n"); fwrite($fp, 'User-Agent: PHP/SMF' . "\r\n"); if (!empty($_SESSION['webinstall_state']['can_svn']) && strpos($match[6], '-dev') !== false && !empty($_SESSION['webinstall_state']['member_info'])) { // Don't go giving this to the wrong places. if ($origin === 'devel.simplemachines.org:443') { fwrite($fp, 'Authorization: Basic ' . base64_encode(strtolower($_SESSION['webinstall_state']['member_info'][0]) . ':' . $_SESSION['webinstall_state']['member_info'][1]) . "\r\n"); } } if ($keep_alive) { fwrite($fp, 'Connection: Keep-Alive' . "\r\n"); } else { fwrite($fp, 'Connection: close' . "\r\n"); } if (!empty($post_data)) { 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); } else { fwrite($fp, "\r\n"); } $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); } } elseif (preg_match('~^HTTP/\\S+\\s+20[01]~i', $response) === 0) { return false; } // 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; } return $data; }