/** * Make Request * * Makes a request to the Voyager Restful API * * @param array $hierarchy Array of key-value pairs to embed in the URL path of * the request (set value to false to inject a non-paired value). * @param array $params A keyed array of query data * @param string $mode The http request method to use (Default of GET) * @param string $xml An optional XML string to send to the API * * @return obj A Simple XML Object loaded with the xml data returned by the API * @access protected */ protected function makeRequest($hierarchy, $params = false, $mode = "GET", $xml = false) { // Build Url Base $urlParams = "http://{$this->ws_host}:{$this->ws_port}/{$this->ws_app}"; // Add Hierarchy foreach ($hierarchy as $key => $value) { $hierarchyString[] = $value !== false ? urlencode($key) . "/" . urlencode($value) : urlencode($key); } // Add Params foreach ($params as $key => $param) { $queryString[] = $key . "=" . urlencode($param); } // Build Hierarchy $urlParams .= "/" . implode("/", $hierarchyString); // Build Params if (isset($queryString)) { $urlParams .= "?" . implode("&", $queryString); } if ($mode == 'GET' && isset($this->getCache[$urlParams])) { return $this->getCache[$urlParams]; } // Create Proxy Request $client = new Proxy_Request($urlParams); if ($this->sessionId) { $client->addCookie('JSESSIONID', $this->sessionId); } // Select Method if ($mode == "POST") { $client->setMethod(HTTP_REQUEST_METHOD_POST); if ($xml) { $client->addRawPostData($xml); } } else { if ($mode == "PUT") { $client->setMethod(HTTP_REQUEST_METHOD_PUT); $client->addRawPostData($xml); } else { if ($mode == "DELETE") { $client->setMethod(HTTP_REQUEST_METHOD_DELETE); } else { $client->setMethod(HTTP_REQUEST_METHOD_GET); } } } // Send Request and Retrieve Response $startTime = microtime(true); if (PEAR::isError($client->sendRequest())) { error_log("VoyagerRestful: failed to send request to {$urlParams}"); return false; } $code = $client->getResponseCode(); if ($code >= 400) { error_log("VoyagerRestful: HTTP Request failed with error code {$code}. Request url: {$urlParams}, response: " . $client->getResponseBody()); } $cookies = $client->getResponseCookies(); if ($cookies) { foreach ($cookies as $cookie) { if ($cookie['name'] == 'JSESSIONID') { $this->sessionId = $cookie['value']; } } } $xmlResponse = $client->getResponseBody(); $this->debugLog('[' . round(microtime(true) - $startTime, 4) . "s] {$this->sessionId} {$mode} request {$urlParams}, body:\n{$xml}\nResults:\n{$xmlResponse}"); $oldLibXML = libxml_use_internal_errors(); libxml_use_internal_errors(true); $simpleXML = simplexml_load_string($xmlResponse); libxml_use_internal_errors($oldLibXML); if ($simpleXML === false) { $logger = new Logger(); $error = libxml_get_last_error(); error_log('VoyagerRestful: Failed to parse response XML: ' . $error->message . ", response:\n" . $xmlResponse); $logger->log('Failed to parse response XML: ' . $error->message . ", response:\n" . $xmlResponse, PEAR_LOG_ERR); $this->debugLog('Failed to parse response XML: ' . $error->message . ", response:\n" . $xmlResponse); return false; } if ($mode == 'GET') { $this->getCache[$urlParams] = $simpleXML; } return $simpleXML; }
/** * Process incoming parameters and display the page. * * @return void * @access public */ public function launch() { global $interface; global $configArray; if (isset($_REQUEST['proxyitem'])) { $url = $configArray['OpenURL']['url']; // Strip main directory $url = substr($url, 0, strrpos($url, '/')); $url .= $_REQUEST['proxyitem']; $request = new Proxy_Request(); $request->setMethod(HTTP_REQUEST_METHOD_GET); $request->setURL($url); $request->addHeader('X-Forwarded-For', $_SERVER['REMOTE_ADDR']); if (isset($configArray['OpenURL']['language'][$interface->lang])) { $request->addCookie('user-Profile', '%2B%2B%2B' . $configArray['OpenURL']['language'][$interface->lang]); } $result = $request->sendRequest(); if (PEAR::isError($result)) { die($result->getMessage() . "\n"); } foreach ($request->getResponseHeader() as $name => $value) { if (strcasecmp($name, 'content-type') == 0 || strcasecmp($name, 'content-length') == 0) { header("{$name}: {$value}"); } } echo $request->getResponseBody(); exit; } header('Content-type: text/html; charset=UTF-8'); header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1 header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past if (!isset($_REQUEST['openurl']) || !$_REQUEST['openurl']) { die("Missing parameter 'openurl'"); return; } $url = $configArray['OpenURL']['url']; $baseURL = substr($url, 0, strrpos($url, '/')); $proxyURL = $configArray['Site']['url'] . '/AJAX/SFXMenu?action=SFXMenu&proxyitem='; if (substr($_REQUEST['openurl'], 0, 1) != '?') { $url .= '?'; } $url .= $_REQUEST['openurl']; $request = new Proxy_Request(); $request->setMethod(HTTP_REQUEST_METHOD_GET); $request->setURL($url); $request->addHeader('X-Forwarded-For', $_SERVER['REMOTE_ADDR']); if (isset($configArray['OpenURL']['language'][$interface->lang])) { $request->addCookie('user-Profile', '%2B%2B%2B' . $configArray['OpenURL']['language'][$interface->lang]); } // Perform request and die on error $result = $request->sendRequest(); if (PEAR::isError($result)) { die($result->getMessage() . "\n"); } $html = new simple_html_dom(); $html->load($request->getResponseBody()); echo <<<EOF <html> <head> EOF; // Get style sheets and scripts foreach ($html->find('head link') as $link) { if (substr($link->href, 0, 1) == '/') { $link->href = $proxyURL . urlencode($link->href); } elseif (strcasecmp(substr($link->href, 0, strlen($baseURL)), $baseURL) == 0) { $link->href = $proxyURL . urlencode(substr($link->href, strlen($baseURL))); } echo "{$link}\n"; } foreach ($html->find('head script') as $script) { if (substr($script->src, 0, 1) == '/' || substr($script->src, 0, 1) == '.') { $script->src = $proxyURL . urlencode($script->src); } else { $src = parse_url($script->src); // proxify only if not secure url if (strcasecmp($src['scheme'], 'http') == 0) { $script->src = $proxyURL . urlencode($src['path']); } } echo "{$script}\n"; } echo <<<EOF <script type="text/javascript"> function openWindow(obj, form_name) { var form = \$("form[name=" + form_name + "]"); var url = form.attr('action'); var params = ''; form.find("input[type=hidden]").each(function() { if (params) { params += '&'; } params += encodeURIComponent(\$(this).attr('name')) + '=' + encodeURIComponent(\$(this).attr('value')); }); var win = window.open(); win.location = url + '?' + params; } </script> </head> <body> EOF; $container = $html->find('#basic_target_list_container', 0); if (!$container) { $container = $html->find('#advanced_target_list_container', 0); } if ($container) { // We have some actual items to display $table = $container->parent(); foreach ($table->find('img') as $img) { if (substr($img->src, 0, 1) == '/') { $img->src = $proxyURL . urlencode($img->src); } } foreach ($table->find('form') as $form) { if (substr($form->action, 0, 1) == '/') { $form->action = $baseURL . $form->action; } } echo $table; } echo <<<EOF </body> </html> EOF; }