}
}
-//
-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."<br />\n");
- // Compile the script name
- $script = COMPILE_CODE($script);
- //* DEBUG */ print("SCRIPT=".$script."<br />\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);
//* DEBUG */ print("SCRIPT=".$script."<br />\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."<br />\n");
if ($useProxy) {
// 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("<strong>Request:</strong><pre>".$request."</pre>");
+ $proxyTunnel .= "\r\n";
+ //* DEBUG: */ print("<strong>proxyTunnel=</strong><pre>".$proxyTunnel."</pre>");
// 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
$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("<strong>Request:</strong><pre>".$request."</pre>");
-
- // Initialize array
- $response = array();
// Write request
fputs($fp, $request);
// 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("<strong>Response:</strong><pre>".print_r($response, true)."</pre>");
// Proxy agent found?
'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']))) {
'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) {
'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)];
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);