/** * Parses a validation response from a CAS server to see if the returning CAS request is valid * * @param string $strResults xml or plain text response from cas server * @return bool true if valid, false otherwise * @exception throws exception if cannot parse response or invalid version */ private function isValid() { // values from the request $strTicket = $this->request->getProperty("ticket"); // configuration settings $configCasValidate = $this->registry->getConfig("CAS_VALIDATE", true); // figure out which type of response this is based on the service url $arrURL = explode("/", $configCasValidate); $service = array_pop($arrURL); // now get it! $strUrl = $configCasValidate . "?ticket=" . $strTicket . "&service=" . urlencode($this->validate_url); $strResults = Xerxes_Framework_Parser::request($strUrl); // validate is plain text if ($service == "validate") { $arrMessage = explode("\n", $strResults); if (count($arrMessage) >= 2) { if ($arrMessage[0] == "yes") { return $arrMessage[1]; } } else { throw new Exception("Could not parse CAS validation response."); } } elseif ($service == "serviceValidate" || $service == "proxyValidate") { // these are XML based $objXml = new DOMDocument(); $objXml->loadXML($strResults); $strCasNamespace = "http://www.yale.edu/tp/cas"; $objUser = $objXml->getElementsByTagNameNS($strCasNamespace, "user")->item(0); $objFailure = $objXml->getElementsByTagNameNS($strCasNamespace, "authenticationFailure")->item(0); if ($objUser != null) { if ($objUser->nodeValue != "") { return $objUser->nodeValue; } else { throw new Exception("CAS validation response missing username value"); } } elseif ($objFailure != null) { // see if error, rather than failed authentication if ($objFailure->getAttribute("code") == "INVALID_REQUEST") { throw new Exception("Invalid request to CAS server: " . $objFailure->nodeValue); } } else { throw new Exception("Could not parse CAS validation response."); } } else { throw new Exception("Unsupported CAS version."); } // if we got this far, the request was invalid return false; }
public function doExecute() { $configToken = $this->registry->getConfig("BX_TOKEN", false); if ($configToken != null) { $configBX = $this->registry->getConfig("BX_SERVICE_URL", false, "http://recommender.service.exlibrisgroup.com/service"); $configLinkResolver = $this->registry->getConfig("LINK_RESOLVER_ADDRESS", true); $configSID = $this->registry->getConfig("APPLICATION_SID", false, "calstate.edu:xerxes"); $configMaxRecords = $this->registry->getConfig("BX_MAX_RECORDS", false, "10"); $configMinRelevance = $this->registry->getConfig("BX_MIN_RELEVANCE", false, "0"); $open_url = $this->request->getData("//openurl_kev_co"); $url = $configBX . "/recommender/openurl?token={$configToken}&{$open_url}&res_dat=source=global&threshold={$configMinRelevance}&maxRecords={$configMaxRecords}"; try { $xml = Xerxes_Framework_Parser::request($url, 4); if ($xml == "") { throw new Exception("No response from bx service"); } } catch (Exception $e) { trigger_error("Could not get result from bX service: " . $e->getTraceAsString(), E_USER_WARNING); return 1; } // header("Content-type: text/xml"); echo $xml; exit; $doc = new Xerxes_BxRecord_Document(); $doc->loadXML($xml); $objXml = new DOMDocument(); $objXml->loadXML("<recommendations />"); $results = $doc->records(); $x = 0; if (count($results) > 1) { foreach ($doc->records() as $record) { // first one is the same document if ($x == 0) { $x++; continue; } $objRecord = $objXml->createElement("record"); $objXml->documentElement->appendChild($objRecord); $objImport = $objXml->importNode($record->toXML()->documentElement, true); $objRecord->appendChild($objImport); $strOpenURL = $record->getOpenURL($configLinkResolver, $configSID); $objOpenURL = $objXml->createElement("url_open", Xerxes_Framework_Parser::escapeXML($strOpenURL)); $objRecord->appendChild($objOpenURL); } } $this->request->addDocument($objXml); } return 1; }
/** * Checks the spelling of the supplied query and offers spelling suggestion * * @param string $strQuery supplied query * @param string $strYahooId yahoo access id * @param string $strAltLocation an alternate location for the Yahoo spell check, in case you need to balanace * @return string spelling suggesting */ public function checkSpelling($strQuery, $strYahooId, $strAltLocation = "") { return ""; if ($strAltLocation != "") { $this->url = $strAltLocation . "?appid=" . $strYahooId . "&query=" . urlencode($strQuery); } else { $this->url = "http://api.search.yahoo.com/WebSearchService/V1/spellingSuggestion?appid=" . $strYahooId . "&query=" . urlencode($strQuery); } $strResponse = Xerxes_Framework_Parser::request($this->url); $objSpelling = new DOMDocument(); $objSpelling->loadXML($strResponse); if ($objSpelling->getElementsByTagName("Result")->item(0) != null) { return $objSpelling->getElementsByTagName("Result")->item(0)->nodeValue; } else { return ""; } }
public function doExecute() { $configMemory = $this->registry->getConfig("HARVEST_MEMORY_LIMIT", false, "500M"); ini_set("memory_limit", $configMemory); echo "\n\nSFX INSTITUTIONAL HOLDINGS POPULATION \n\n"; // You can define the export file on sfx as having an instance extension, so // give the client the opportunity to define that here $strInstance = $this->request->getProperty("instance"); if ($strInstance != "") { $strInstance = "-" . $strInstance; } // construct the address to Google Scholar institutional // holdings file on SFX. Either SFX specific config, or // general link resolver config. $configSfx = $this->registry->getConfig("ALTERNATE_FULLTEXT_HARVEST_ADDRESS", false, $this->registry->getConfig("LINK_RESOLVER_ADDRESS", false)); if (!$configSfx) { throw new Exception("Can not run populate action, no link resolver address configured. " . "Need config ALTERNATE_FULLTEXT_HARVEST_ADDRESS or LINK_RESOLVER_ADDRESS."); } // fire-up a transaction with the database $objData = new Xerxes_DataMap(); $objData->beginTransaction(); // clear old data echo " Flushing SFX fulltext table . . . "; $objData->clearFullText(); echo "done.\n"; // try to get the data from sfx $done = false; $x = 0; while ($done == false) { $x++; $strUrl = $configSfx . "/cgi/public/get_file.cgi?file=institutional_holding" . $strInstance . '-.prt' . str_pad($x, 2, '0', STR_PAD_LEFT) . ".xml"; echo " Pulling down SFX inst holding file ({$x}) . . . "; try { $strResponse = Xerxes_Framework_Parser::request($strUrl); $objXml = new SimpleXMLElement($strResponse); } catch (Exception $e) { if ($x == 1) { throw new Exception("cannot get institutional holding file from sfx: '{$strUrl}'. " . "If this is the correct SFX server address, make sure your SFX allows access to " . "institutional holding file from this IP address in config/get_file_restriction.config " . "on SFX server."); } $done = true; } echo "done.\n"; if (!$done) { echo " Processing file . . . "; $objItems = $objXml->xpath("//item[@type != 'other']"); if ($objItems == false) { throw new Exception("could not find items in inst holding file."); } echo "done.\n"; echo " Adding to database . . . "; foreach ($objItems as $objItem) { foreach ($objItem->coverage as $objCoverage) { $objFullText = new Xerxes_Data_Fulltext(); $objFullText->issn = (string) $objItem->issn; $objFullText->issn = str_replace("-", "", $objFullText->issn); $objFullText->title = (string) $objItem->title; $objFullText->title = urlencode($objFullText->title); $objFullText->title = substr(Xerxes_Framework_Parser::strtolower($objFullText->title), 0, 100); $objFullText->startdate = (int) $objCoverage->from->year; $objFullText->enddate = (int) $objCoverage->to->year; if ($objFullText->enddate == 0) { $objFullText->enddate = 9999; } $objFullText->embargo = (int) $objCoverage->embargo->days_not_available; $objFullText->updated = date("YmdHis"); // add it to the database $objData->addFulltext($objFullText); } } echo "done.\n"; } } echo " Commiting changes . . . "; $objData->commit(); echo "done.\n"; return 1; }
public function checkSpelling() { $registry = Xerxes_Framework_Registry::getInstance(); $strAltYahoo = $registry->getConfig("ALTERNATE_YAHOO_LOCATION", false); $configYahooID = $registry->getConfig("YAHOO_ID", false, "calstate"); $spell_return = array(); // we'll return this one for ($x = 0; $x < count($this->query_list); $x++) { $term = $this->query_list[$x]; $url = ""; if ($strAltYahoo != "") { $url = $strAltYahoo; } else { $url = "http://api.search.yahoo.com/WebSearchService/V1/spellingSuggestion"; } $url .= "?appid=" . $configYahooID . "&query=" . urlencode($term->phrase); $strResponse = Xerxes_Framework_Parser::request($url); $objSpelling = new DOMDocument(); $objSpelling->loadXML($strResponse); if ($objSpelling->getElementsByTagName("Result")->item(0) != null) { $term->spell_correct = $objSpelling->getElementsByTagName("Result")->item(0)->nodeValue; $spell_return[$term->id] = $term->spell_correct; } // also put it here so we can return it $this->query_list[$x] = $term; } return $spell_return; }
/** * Fetches and normalize the API data * * @param string $url url of patron dump or pint test * @return array patron data */ private function getContent($url) { $arrRawData = array(); $arrData = array(); // get the data and strip out html tags $strResponse = Xerxes_Framework_Parser::request($url); $strResponse = trim(strip_tags($strResponse)); if ($strResponse == "") { throw new Exception("Could not connect to Innovative Patron API"); } else { // cycle thru each line in the response, splitting each // on the equal sign into an associative array $arrRawData = explode("\n", $strResponse); foreach ($arrRawData as $strLine) { $arrLine = explode("=", $strLine); // strip out the code, leaving just the attribute name $arrLine[0] = preg_replace("/\\[[^\\]]{1,}\\]/", "", $arrLine[0]); $arrData[trim($arrLine[0])] = trim($arrLine[1]); } } return $arrData; }
/** * Fetch the data from Metalib and check for errors * * @param string $url url of the request */ private function getResponse($url, $timeout = null, $retry = 0) { // metalib takes little care to ensure propper encoding of its xml, so we will set // recover to true here in order to allow libxml to recover from errors and continue // processing the document $objXml = new DOMDocument(); $objXml->recover = true; // time out, should only be defined for search-related functions $strResponse = Xerxes_Framework_Parser::request($url, $timeout); if ($strResponse == "") { throw new Exception("Cannot process search at this time."); } $objXml->loadXML($strResponse); // no response? if ($objXml->documentElement == null) { throw new Exception("cannot connect to metalib server"); } // error in response if ($objXml->getElementsByTagName("error_code") != null) { // for easier handling $xml = simplexml_import_dom($objXml->documentElement); foreach ($xml->xpath("//global_error|//local_error") as $error) { $error_code = (int) $error->error_code; $error_text = (string) $error->error_text; // now examine them // metalib session has timed out! if ($error_code == 0151) { // this particular metalib error message is confusing to end-users, so make it more clear $error_text = "The Metalib session has expired"; // also try to re-up the session in the event metalib was restarted or something if ($retry == 0) { $this->session = $this->session(); return $this->getResponse($url, $timeout, 1); } } // these are just warnings if ($error_code == 2114 || $error_code == 6039 || $error_code == 6033 || $error_code == 6034 || $error_code == 6023 || $error_code == 134 || $error_code == 6022) { trigger_error("Metalib warning ({$error_code}): {$error_text}", E_USER_WARNING); } else { throw new Exception("Metalib exception ({$error_code}): {$error_text}"); } } } return $objXml; }