MXCHANGE_OPEN() rewritten to GET_URL(), POST_URL() and SEND_RAW_REQUEST()
[mailer.git] / inc / functions.php
index 0f4f995d6d2124d2ba368fc2164604d3d6b702fd..29421f39225739df8e8d4c591273093cbe8535e8 100644 (file)
@@ -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."<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);
@@ -1696,6 +1683,85 @@ function MXCHANGE_OPEN ($script) {
        //* 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) {
@@ -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("<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
@@ -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("<strong>Request:</strong><pre>".$request."</pre>");
-
-       // 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("<strong>Response:</strong><pre>".print_r($response, true)."</pre>");
 
        // Proxy agent found?