r4838 Cacti integration improvements (part of #466)
[racktables] / wwwroot / inc / solutions.php
index d63d5b4..5011b14 100644 (file)
@@ -293,42 +293,59 @@ function proxyCactiRequest ($graph_id)
        $cacti_url = getConfigVar('CACTI_URL');
        $cacti_user = getConfigVar('CACTI_USERNAME');
        $cacti_pass = getConfigVar('CACTI_USERPASS');
-       $url = $cacti_url . "graph_image.php?action=view&local_graph_id=" . $graph_id;
+       $url = $cacti_url . "/graph_image.php?action=view&local_graph_id=" . $graph_id;
        $postvars = "action=login&login_username=" . $cacti_user . "&login_password=" . $cacti_pass;
 
        $session = curl_init();
+//     curl_setopt ($session, CURLOPT_VERBOSE, TRUE);
 
        // Initial options up here so a specific type can override them
        curl_setopt ($session, CURLOPT_FOLLOWLOCATION, FALSE); 
        curl_setopt ($session, CURLOPT_TIMEOUT, 10);
        curl_setopt ($session, CURLOPT_RETURNTRANSFER, TRUE);
-
-       curl_setopt ($session, CURLOPT_HEADER, TRUE);
        curl_setopt ($session, CURLOPT_URL, $url);
-       $headers = curl_exec ($session);        // Initial request to set the cookies
-
-       // Get the cookies from the headers
-       preg_match('/Set-Cookie: ([^;]*)/i', $headers, $cookies);
-       array_shift($cookies);  // Remove 'Set-Cookie: ...' value                       
-       $cookie_header = implode(";", $cookies);
-
-       curl_setopt ($session, CURLOPT_COOKIE, $cookie_header);
-       curl_setopt ($session, CURLOPT_HEADER, FALSE);
-       curl_setopt ($session, CURLOPT_POST, TRUE);
-       curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
 
-       curl_exec ($session);   // POST Login
+       if (isset($_SESSION['CACTICOOKIE'][$cacti_url]))
+               curl_setopt ($session, CURLOPT_COOKIE, $_SESSION['CACTICOOKIE'][$cacti_url]);
 
-       // Make the request
+       // Request the image
        $ret['contents'] = curl_exec ($session);
-       $ret['type'] = "text/plain";
        $ret['type'] = curl_getinfo ($session, CURLINFO_CONTENT_TYPE);
-       $ret['size'] = curl_getinfo ($session, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
+       $ret['size'] = curl_getinfo ($session, CURLINFO_SIZE_DOWNLOAD);
+
+       // Not an image, probably the login page
+       if (preg_match('/^text\/html.*/i', $ret['type'])) {
+               // Request to set the cookies
+               curl_setopt ($session, CURLOPT_HEADER, TRUE);
+               curl_setopt ($session, CURLOPT_COOKIE, "");     // clear the old cookie
+               $headers = curl_exec ($session);
+
+               // Get the cookies from the headers
+               preg_match('/Set-Cookie: ([^;]*)/i', $headers, $cookies);
+               array_shift($cookies);  // Remove 'Set-Cookie: ...' value                       
+               $cookie_header = implode(";", $cookies);
+               $_SESSION['CACTICOOKIE'][$cacti_url] = $cookie_header; // store for later use by this user
+
+               // POST Login
+               curl_setopt ($session, CURLOPT_COOKIE, $cookie_header);
+               curl_setopt ($session, CURLOPT_HEADER, FALSE);
+               curl_setopt ($session, CURLOPT_POST, TRUE);
+               curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars);
+               curl_exec ($session);
+
+               // Request the image
+               curl_setopt ($session, CURLOPT_HTTPGET, TRUE);
+               $ret['contents'] = curl_exec ($session);
+               $ret['type'] = curl_getinfo ($session, CURLINFO_CONTENT_TYPE);
+       }
 
        curl_close ($session);
 
-       header("Content-Type: {$ret['type']}");
-       header("Content-Length: {$ret['size']}");
+       if ($ret['type'] != NULL)
+               header("Content-Type: {$ret['type']}");
+       if ($ret['size'] > 0)
+               header("Content-Length: {$ret['size']}");
+
        echo $ret['contents'];
 }