From f50cf1e7f174a1547055ef772a3a2b12415bc73b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Tue, 9 Sep 2008 11:03:08 +0000 Subject: [PATCH] MXCHANGE_OPEN() rewritten to GET_URL(), POST_URL() and SEND_RAW_REQUEST() --- inc/functions.php | 147 ++++++++++++++++++------- inc/libs/wernis_functions.php | 19 ++-- inc/modules/admin/admin-inc.php | 2 +- inc/modules/admin/what-extensions.php | 2 +- inc/modules/admin/what-theme_check.php | 2 +- inc/modules/admin/what-updates.php | 2 +- 6 files changed, 122 insertions(+), 52 deletions(-) diff --git a/inc/functions.php b/inc/functions.php index 0f4f995d6d..29421f3922 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -1652,25 +1652,12 @@ function ADD_EMAIL_NAV($PAGES, $offset, $show_form, $colspan, $return=false) { } } -// -function MXCHANGE_OPEN ($script) { - global $_CONFIG; - // Default is not to use proxy - $useProxy = true; - - // Are proxy settins set? - if ((!empty($_CONFIG['proxy_host'])) && ($_CONFIG['proxy_port'] > 0)) { - // Then use it - $useProxy = true; - } - - //* DEBUG */ print("SCRIPT=".$script."
\n"); - // Compile the script name - $script = COMPILE_CODE($script); - //* DEBUG */ print("SCRIPT=".$script."
\n"); - +// Extract host from script name +function EXTRACT_HOST (&$script) { // Use default SERVER_URL by default... ;) So? $url = SERVER_URL; + + // Is this URL valid? if (substr($script, 0, 7) == "http://") { // Use the hostname from script URL as new hostname $url = substr($script, 7); @@ -1696,6 +1683,85 @@ function MXCHANGE_OPEN ($script) { //* DEBUG */ print("SCRIPT=".$script."
\n"); if (substr($script, 0, 1) == "/") $script = substr($script, 1); + // Return host name + return $host; +} + +// Send a GET request +function GET_URL ($script) { + // Compile the script name + $script = COMPILE_CODE($script); + + // Extract host name from script + $host = EXTRACT_HOST($script); + + // Generate GET request header + $request = "GET /" . trim($script) . " HTTP/1.1\r\n"; + $request .= "Host: " . $host . "\r\n"; + $request .= "Referer: " . URL . "/admin.php\r\n"; + $request .= "User-Agent: " . TITLE . "/" . FULL_VERSION . "\r\n"; + $request .= "Content-Type: text/plain\r\n"; + $request .= "Cache-Control: no-cache\r\n"; + $request .= "Connection: Close\r\n\r\n"; + + // Send the raw request + $response = SEND_RAW_REQUEST($host, $request); + + // Return the result to the caller function + return $response; +} + +// Send a POST request +function POST_URL ($script, $postData) { + // Is postData an array? + if (!is_array($postData)) { + // Abort here + return array("", "", ""); + } // END - if + + // Compile the script name + $script = COMPILE_CODE($script); + + // Extract host name from script + $host = EXTRACT_HOST($script); + + // Construct request + $data = http_build_query($postData, '', '&'); + + // Generate POST request header + $request = "POST /" . trim($script) . " HTTP/1.1\r\n"; + $request .= "Host: " . $host . "\r\n"; + $request .= "Referer: " . URL . "/admin.php\r\n"; + $request .= "User-Agent: " . TITLE . "/" . FULL_VERSION . "\r\n"; + $request .= "Content-type: application/x-www-form-urlencoded\r\n"; + $request .= "Content-length: " . strlen($data) . "\r\n"; + $request .= "Cache-Control: no-cache\r\n"; + $request .= "Connection: Close\r\n\r\n"; + $request .= $data; + + // Send the raw request + $response = SEND_RAW_REQUEST($host, $request); + + // Return the result to the caller function + return $response; +} + +// Sends a raw request to another host +function SEND_RAW_REQUEST ($host, $request) { + global $_CONFIG; + + // Initialize array + $response = array("", "", ""); + + // Default is not to use proxy + $useProxy = false; + + // Are proxy settins set? + if ((!empty($_CONFIG['proxy_host'])) && ($_CONFIG['proxy_port'] > 0)) { + // Then use it + $useProxy = true; + } // END - if + // Open connection //* DEBUG */ die("SCRIPT=".$script."
\n"); if ($useProxy) { @@ -1707,33 +1773,33 @@ function MXCHANGE_OPEN ($script) { // Is there a link? if (!is_resource($fp)) { // Failed! - return array("", "", ""); + return $response; } // END - if // Do we use proxy? if ($useProxy) { // Generate CONNECT request header - $request = "CONNECT ".$host.":80 HTTP/1.1\r\n"; - $request .= "Host: ".$host."\r\n"; + $proxyTunnel = "CONNECT ".$host.":80 HTTP/1.1\r\n"; + $proxyTunnel .= "Host: ".$host."\r\n"; // Use login data to proxy? (username at least!) if (!empty($_CONFIG['proxy_username'])) { // Add it as well $encodedAuth = base64_encode(COMPILE_CODE($_CONFIG['proxy_username']).":".COMPILE_CODE($_CONFIG['proxy_password'])); - $request .= "Proxy-Authorization: Basic ".$encodedAuth."\r\n"; + $proxyTunnel .= "Proxy-Authorization: Basic ".$encodedAuth."\r\n"; } // END - if // Add last new-line - $request .= "\r\n"; - //* DEBUG: */ print("Request:
".$request."
"); + $proxyTunnel .= "\r\n"; + //* DEBUG: */ print("proxyTunnel=
".$proxyTunnel."
"); // Write request - fputs($fp, $request); + fputs($fp, $proxyTunnel); // Got response? if (feof($fp)) { // No response received - return array("", "", ""); + return $response; } // END - if // Read the first line @@ -1741,22 +1807,9 @@ function MXCHANGE_OPEN ($script) { $respArray = explode(" ", $resp); if ((strtolower($respArray[0]) !== "http/1.0") || ($respArray[1] != "200")) { // Invalid response! - return array("", "", ""); + return $response; } // END - if } // END - if - - // Generate GET request header - $request = "GET /".trim($script)." HTTP/1.1\r\n"; - $request .= "Host: ".$host."\r\n"; - $request .= "Referer: ".URL."/admin.php\r\n"; - $request .= "User-Agent: ".TITLE."/".FULL_VERSION."\r\n"; - $request .= "Content-Type: text/plain\r\n"; - $request .= "Cache-Control: no-cache\r\n"; - $request .= "Connection: Close\r\n\r\n"; - //* DEBUG: */ print("Request:
".$request."
"); - - // Initialize array - $response = array(); // Write request fputs($fp, $request); @@ -1769,6 +1822,22 @@ function MXCHANGE_OPEN ($script) { // Close socket fclose($fp); + // Skip first empty lines + $resp = $response; + foreach ($resp as $idx => $line) { + // Trim space away + $line = trim($line); + + // Is this line empty? + if (empty($line)) { + // Then remove it + array_shift($response); + } else { + // Abort on first non-empty line + break; + } + } // END - foreach + //* DEBUG: */ print("Response:
".print_r($response, true)."
"); // Proxy agent found? diff --git a/inc/libs/wernis_functions.php b/inc/libs/wernis_functions.php index e665277234..4114845e9c 100644 --- a/inc/libs/wernis_functions.php +++ b/inc/libs/wernis_functions.php @@ -82,7 +82,7 @@ function WERNIS_SEND_REQUEST ($scriptName, $requestData = array()) { 'status' => "failed_general", 'message' => WERNIS_API_REQUEST_DATA_INVALID ); - } + } // END - if // Is the API id and MD5 hash there? if ((empty($_CONFIG['wernis_api_id'])) || (empty($_CONFIG['wernis_api_md5']))) { @@ -91,16 +91,17 @@ function WERNIS_SEND_REQUEST ($scriptName, $requestData = array()) { 'status' => "failed_general", 'message' => WERNIS_API_REQUEST_DATA_MISSING ); - } + } // END - if + + // Add more request data + $requestData['api_id'] = bigintval($_CONFIG['wernis_api_id']); + $requestData['api_key'] = $_CONFIG['wernis_api_key']; // Construct the request string - $requestString = $_CONFIG['wernis_api_url'] . $scriptName."?api_id=".$_CONFIG['wernis_api_id']."&api_key=".$_CONFIG['wernis_api_md5']; - foreach ($requestData as $key=>$value) { - $requestString .= "&".$key."=".$value; - } + $requestString = $_CONFIG['wernis_api_url'] . $scriptName; // Get the raw response from the lower function - $response = MXCHANGE_OPEN($requestString); + $response = POST_URL($requestString, $requestData); // Check the response header if all is fine if (strpos($response[0], "200") === false) { @@ -109,7 +110,7 @@ function WERNIS_SEND_REQUEST ($scriptName, $requestData = array()) { 'status' => "request_error", 'message' => sprintf(WERNIS_API_REQUEST_ERROR, $response[0]) ); - } + } // END - if // All (maybe) fine so remove the response header from server $response = $response[(count($response) - 1)]; @@ -118,7 +119,7 @@ function WERNIS_SEND_REQUEST ($scriptName, $requestData = array()) { if (substr($response, 0, 1) == "&") { // Remove the leading & (which can be used in Flash) $response = substr($response, 1); - } + } // END - if // Bring back the response $data = explode("=", $response); diff --git a/inc/modules/admin/admin-inc.php b/inc/modules/admin/admin-inc.php index 6b22d9ba5d..0534befa51 100644 --- a/inc/modules/admin/admin-inc.php +++ b/inc/modules/admin/admin-inc.php @@ -902,7 +902,7 @@ function ADMIN_TEST_PROXY_SETTINGS ($settingsArray) { $_CONFIG = array_merge($_CONFIG, $settingsArray); // Now get the test URL - $content = MXCHANGE_OPEN("check-updates2.php"); + $content = GET_URL("check-updates2.php"); // Is the first line with "200 OK"? $valid = eregi("200 OK", $content[0]); diff --git a/inc/modules/admin/what-extensions.php b/inc/modules/admin/what-extensions.php index 3d47fee1d9..80b0cd03e7 100644 --- a/inc/modules/admin/what-extensions.php +++ b/inc/modules/admin/what-extensions.php @@ -337,7 +337,7 @@ case "register": // Register new extension case "search": // Search for new extensions on our server // Get response from our server - $response = MXCHANGE_OPEN("extensions.php"); + $response = GET_URL("extensions.php"); // Are extensions found? if (($response[sizeof($response) - 1] == "[EOF]") && ($response[0] != "[EOF]")) diff --git a/inc/modules/admin/what-theme_check.php b/inc/modules/admin/what-theme_check.php index 62eca499ee..acef17b946 100644 --- a/inc/modules/admin/what-theme_check.php +++ b/inc/modules/admin/what-theme_check.php @@ -47,7 +47,7 @@ $mode = "overview"; $SEL = "0"; // Get response from our server -$response = MXCHANGE_OPEN("check-themes.php"); +$response = GET_URL("check-themes.php"); // Are theme_check found? if (($response[sizeof($response) - 1] == "[EOF]") && ($response[0] != "[EOF]")) diff --git a/inc/modules/admin/what-updates.php b/inc/modules/admin/what-updates.php index c273061574..c0db3f864d 100644 --- a/inc/modules/admin/what-updates.php +++ b/inc/modules/admin/what-updates.php @@ -44,7 +44,7 @@ ADD_DESCR("admin", basename(__FILE__)); $ONLINE['code'] = "???"; // Get response from our server in an array -$response = MXCHANGE_OPEN("check-updates2.php"); +$response = GET_URL("check-updates2.php"); if (empty($response[0]) && empty($response[1]) && empty($response[2])) { // Error! -- 2.39.2