Browse Source

fixed session issues on for eds and minor changes

2.x-ebsco
Paul Pound 12 years ago
parent
commit
86ef54b274
  1. 2
      targets/cufts/theme/roblib-search-cufts.tpl.php
  2. 172
      targets/eds/includes/rest/EBSCOAPI.php
  3. 7
      targets/eds/roblib_search_eds.module
  4. 2
      targets/eds/theme/roblib-search-eds.tpl.php
  5. 14
      targets/evergreen/js/evergreen_results.js
  6. 19
      targets/evergreen/roblib_search_evergreen.module
  7. 2
      targets/evergreen/theme/roblib-search-evergreen.tpl.php

2
targets/cufts/theme/roblib-search-cufts.tpl.php

@ -18,7 +18,7 @@
?>
<div class ="roblib-search-content cufts" id="roblib-search-content-cufts">
<img src="<?php print $spinner_path; ?>"/>
<img src="<?php print (empty($spinner_path) ? ' ' : $spinner_path); ?>"/>
</div>
<div class ="roblib-search-more">Search all Journals</div>

172
targets/eds/includes/rest/EBSCOAPI.php

@ -1,6 +1,5 @@
<?php
/**
* EBSCO API class
*
@ -8,19 +7,17 @@
*
*/
/**
* EBSCO API class
*/
class EBSCOAPI
{
class EBSCOAPI {
/**
* The authentication token used for API transactions
* @global string
*/
private $authenticationToken;
/**
* The session token for API transactions
* @global string
@ -32,7 +29,6 @@ class EBSCOAPI
* @global object EBSCOConnector
*/
private $connector;
private $config;
public function __construct($config) {
@ -48,8 +44,7 @@ class EBSCOAPI
* @return EBSCOConnector object
* @access public
*/
public function connector($config)
{
public function connector($config) {
if (empty($this->connector)) {
$this->connector = new EBSCOConnector($config);
}
@ -57,7 +52,6 @@ class EBSCOAPI
return $this->connector;
}
/**
* Create a new EBSCOResponse object
*
@ -66,13 +60,11 @@ class EBSCOAPI
* @return EBSCOResponse object
* @access public
*/
public function response($response)
{
public function response($response) {
$responseObj = new EBSCOResponse($response);
return $responseObj;
}
/**
* Request authentication and session tokens, then send the API request.
* Retry the request if authentication errors occur
@ -85,19 +77,18 @@ class EBSCOAPI
* @return array An associative array with results.
* @access protected
*/
protected function request($action, $params = null, $attempts = 3)
{
protected function request($action, $params = null, $attempts = 3) {
try {
$authenticationToken = $this->getAuthToken();
$sessionToken = $this->getSessionToken($authenticationToken);
if(empty($authenticationToken)){
$authenticationToken = $this -> getAuthToken();
if (empty($authenticationToken)) {
$authenticationToken = $this->getAuthToken();
}
if(empty($sessionToken)){
$sessionToken = $this -> getSessionToken($authenticationToken,'y');
if (empty($sessionToken)) {
$sessionToken = $this->getSessionToken($authenticationToken, 'y');
}
$headers = array(
@ -109,14 +100,14 @@ class EBSCOAPI
$result = $this->response($response)->result();
$results = $result;
return $results;
} catch(EBSCOException $e) {
} catch (EBSCOException $e) {
try {
// Retry the request if there were authentication errors
$code = $e->getCode();
switch ($code) {
case EBSCOConnector::EDS_AUTH_TOKEN_INVALID:
$authenticationToken = $this->getAuthToken();
$sessionToken = $this ->getSessionToken($authenticationToken);
$sessionToken = $this->getSessionToken($authenticationToken);
$headers = array(
'x-authenticationToken: ' . $authenticationToken,
'x-sessionToken: ' . $sessionToken
@ -126,7 +117,7 @@ class EBSCOAPI
}
break;
case EBSCOConnector::EDS_SESSION_TOKEN_INVALID:
$sessionToken = $this ->getSessionToken($authenticationToken,'y');
$sessionToken = $this->getSessionToken($authenticationToken, 'y');
$headers = array(
'x-authenticationToken: ' . $authenticationToken,
'x-sessionToken: ' . $sessionToken
@ -142,13 +133,13 @@ class EBSCOAPI
return $result;
break;
}
} catch(Exception $e) {
} catch (Exception $e) {
$result = array(
'error' => $e->getMessage()
);
return $result;
}
} catch(Exception $e) {
} catch (Exception $e) {
$result = array(
'error' => $e->getMessage()
);
@ -156,16 +147,22 @@ class EBSCOAPI
}
}
private function writeLockFile(){
$tokenFile = fopen("edstoken.txt","w+");
private function writeTokenFile() {
$tokenFile = fopen("edstoken.txt", "w+");
$result = $this->apiAuthenticationToken();
fwrite($tokenFile, $result['authenticationToken']."\n");
fwrite($tokenFile, $result['authenticationTimeout']."\n");
fwrite($tokenFile, $result['authenticationToken'] . "\n");
fwrite($tokenFile, $result['authenticationTimeout'] . "\n");
fwrite($tokenFile, $result['authenticationTimeStamp']);
fclose($tokenFile);
return $result['authenticationToken'];
}
private function writeLockFile() {
$lockFile = fopen("edslock.txt", "w+");
fwrite($lockFile, 'lock');
fclose($lockFile);
}
/*
* Get authentication token from appication scop
* Check authToen's expiration
@ -175,32 +172,40 @@ class EBSCOAPI
*
* @access public
*/
public function getAuthToken(){
$lockFile = fopen("edslock.txt","r");
$tokenFile =fopen("edstoken.txt","r");
if(empty($lockFile) || empty($tokenFile)){
return $this->writeLockFile();
}
while(!feof($tokenFile)){
$authToken = rtrim(fgets($tokenFile),"\n");
$timeout = fgets($tokenFile)-600;
public function getAuthToken() {
$lockFile = fopen("edslock.txt", "r");
if (empty($lockFile)) {
$this->writeLockFile();
$lockFile = fopen("edslock.txt", "r");
}
$tokenFile = fopen("edstoken.txt", "r");
if (empty($tokenFile)) {
$this->writetokenFile();
$tokenFile = fopen("edstoken.txt", "r");
}
while (!feof($tokenFile)) {
$authToken = rtrim(fgets($tokenFile), "\n");
$timeout = fgets($tokenFile) - 600;
$timestamp = fgets($tokenFile);
}
fclose($tokenFile);
if(time()-$timestamp>=$timeout){
if (time() - $timestamp >= $timeout) {
// Lock check.
if(flock($lockFile, LOCK_EX)){
$tokenFile = fopen("edstoken.txt","w+");
if (flock($lockFile, LOCK_EX)) {
$tokenFile = fopen("edstoken.txt", "w+");
$result = $this->apiAuthenticationToken();
fwrite($tokenFile, $result['authenticationToken']."\n");
fwrite($tokenFile, $result['authenticationTimeout']."\n");
fwrite($tokenFile, $result['authenticationToken'] . "\n");
fwrite($tokenFile, $result['authenticationTimeout'] . "\n");
fwrite($tokenFile, $result['authenticationTimeStamp']);
fclose($tokenFile);
return $result['authenticationToken'];
}else{
}
else {
return $authToken;
}
}else{
}
else {
return $authToken;
}
fclose($lockFile);
@ -213,8 +218,7 @@ class EBSCOAPI
*
* @access public
*/
public function apiAuthenticationToken()
{
public function apiAuthenticationToken() {
$response = $this->connector->requestAuthenticationToken();
$result = $this->response($response)->result();
return $result;
@ -228,42 +232,37 @@ class EBSCOAPI
* @param Authentication token, Profile
* @access public
*/
public function getSessionToken($authenToken, $invalid='n'){
public function getSessionToken($authenToken, $invalid = 'n') {
$token = '';
// Check user's login status
if(isset($_COOKIE['login'])){
/* if(isset($_COOKIE['login'])){
if($invalid=='y'){
$profile = $_SESSION['sessionToken']['profile'];
$sessionToken = $this->apiSessionToken($authenToken, $profile,'n');
$_SESSION['sessionToken']=$sessionToken;
}
$token = $_SESSION['sessionToken']['sessionToken'];
}
else if(isset($_COOKIE['Guest'])){
if($invalid=='y'){
} */
//else
if (isset($_COOKIE['Guest'])) {
if ($invalid == 'y') {
$profile = $_SESSION['sessionToken']['profile'];
$sessionToken = $this->apiSessionToken($authenToken, $profile,'y');
$_SESSION['sessionToken']=$sessionToken;
if (empty($profile)) {
$profile = $_COOKIE['Guest'];
}
$sessionToken = $this->apiSessionToken($authenToken, $profile, 'y');
$_SESSION['sessionToken'] = $sessionToken;
}
$token = $_SESSION['sessionToken']['sessionToken'];
}else{
//$xml ="Config.xml";
//$dom = new DOMDocument();
//$dom->load($xml);
//$EDSCredentials = $dom ->getElementsByTagName('EDSCredentials')->item(0);
//$users = $EDSCredentials -> getElementsByTagName('User');
}
else {
$profileId = $this->config['profile'];
//foreach($users as $user){
// $userType = $user->getElementsByTagName('ClientUser')->item(0)->nodeValue;
// if($userType == 'guest'){
// $profileId = $user -> getElementsByTagName('EDSProfile')->item(0)->nodeValue;
// break;
// }
//}
$sessionToken = $this->apiSessionToken($authenToken, $profileId,'y');
$sessionToken = $this->apiSessionToken($authenToken, $profileId, 'y');
$_SESSION['profile'] = $profileId;
$_SESSION['sessionToken']=$sessionToken;
$_SESSION['sessionToken'] = $sessionToken;
setcookie("Guest", $profileId, 0);
$token = $sessionToken['sessionToken'];
}
@ -277,17 +276,16 @@ class EBSCOAPI
*
* @access public
*/
public function apiSessionToken($authenToken, $profile, $guest)
{
public function apiSessionToken($authenToken, $profile, $guest) {
// Add authentication tokens to headers
$headers = array(
'x-authenticationToken: ' . $authenToken
);
$response = $this->connector($this->config)->requestSessionToken($headers, $profile,$guest);
$response = $this->connector($this->config)->requestSessionToken($headers, $profile, $guest);
$result = $this->response($response)->result();
$token = array(
'sessionToken'=>$result,
'sessionToken' => $result,
'profile' => $profile
);
return $token;
@ -300,14 +298,14 @@ class EBSCOAPI
*
* @access public
*/
public function apiEndSessionToken($authenToken, $sessionToken){
public function apiEndSessionToken($authenToken, $sessionToken) {
// Add authentication tokens to headers
$headers = array(
'x-authenticationToken: '.$authenToken
'x-authenticationToken: ' . $authenToken
);
$this -> connector($this->config)->requestEndSessionToken($headers, $sessionToken);
$this->connector($this->config)->requestEndSessionToken($headers, $sessionToken);
}
/**
@ -325,7 +323,6 @@ class EBSCOAPI
return $results;
}
/**
* Wrapper for retrieve API call
*
@ -336,8 +333,7 @@ class EBSCOAPI
* @return array An associative array of data
* @access public
*/
public function apiRetrieve($an, $db, $term)
{
public function apiRetrieve($an, $db, $term) {
// Add the HTTP query params
$params = array(
'an' => $an,
@ -349,27 +345,27 @@ class EBSCOAPI
return $result;
}
/**
* Wrapper for info API call
*
* @return array An associative array of data
* @access public
*/
public function getInfo()
{
if(isset($_SESSION['info'])){
public function getInfo() {
if (isset($_SESSION['info'])) {
$InfoArray = $_SESSION['info'];
$timestamp = $InfoArray['timestamp'];
if(time()-$timestamp>=3600){
if (time() - $timestamp >= 3600) {
// Get new Info for the profile
$InfoArray = $this->apiInfo();
$_SESSION['info'] = $InfoArray;
$info = $InfoArray['Info'];
}else{
}
else {
$info = $InfoArray['Info'];
}
}else{
}
else {
// Get new Info for the profile
$InfoArray = $this->apiInfo();
$_SESSION['info'] = $InfoArray;
@ -378,14 +374,16 @@ class EBSCOAPI
return $info;
}
public function apiInfo(){
public function apiInfo() {
$response = $this->request('Info','');
$response = $this->request('Info', '');
$Info = array(
'Info' => $response,
'timestamp'=>time()
'timestamp' => time()
);
return $Info;
}
}
?>

7
targets/eds/roblib_search_eds.module

@ -41,7 +41,7 @@ function roblib_search_eds_menu() {
$items['admin/roblib_search/eds_search'] = array(
'title' => 'EDS search Target configuration',
'description' => 'Configuration for the Roblib evergreen search target',
'description' => 'Configuration for the Roblib eds search target',
'page callback' => 'drupal_get_form',
'page arguments' => array('roblib_search_eds_config_form'),
'access arguments' => array('access administration pages'),
@ -216,11 +216,6 @@ function roblib_search_eds_get_results($query = NULL) {
}
$config = roblib_search_eds_build_config_arr();
/*$config['user'] = variable_get('roblib_search_eds_user', 'edsusername');
$config['pass'] = variable_get('roblib_search_eds_pass', 'edspassword');
$config['profile'] = variable_get('roblib_search_eds_profile', 'edsapi');
$config['auth_url'] = variable_get('roblib_search_eds_auth_url', 'https://eds-api.ebscohost.com/Authservice/rest');
$config['rest_url'] = variable_get('roblib_search_eds_rest_url', 'http://eds-api.ebscohost.com/edsapi/rest');*/
$eds_api = new EBSCOAPI($config);
$number_per_page = variable_get('roblib_search_eds_num_results', '5');
$query = urlencode($query);

2
targets/eds/theme/roblib-search-eds.tpl.php

@ -18,7 +18,7 @@
?>
<div class ="roblib-search-content eds" id="roblib-search-content-eds">
<img src="<?php print $spinner_path; ?>"/>
<img src="<?php print (empty($spinner_path) ? ' ' : $spinner_path); ?>"/>
</div>
<div class ="roblib-search-more" id="roblib-search-eds-more"></div>

14
targets/evergreen/js/evergreen_results.js

@ -30,6 +30,13 @@ Drupal.behaviors.roblib_search_evergreen = {
})
items.push('</div>')
}
if(typeof val.electronic_holdings !== 'undefined'){
items.push('<div class="evergreen-holdings">');
jQuery.each(val.electronic_holdings, function(key2, val2){
roblibEvergreenAddElectronicHoldings(val2, items);
})
items.push('</div>')
}
items.push('</div>');
});
}
@ -41,9 +48,10 @@ Drupal.behaviors.roblib_search_evergreen = {
}
function roblibEvergreenAddHoldings(holdings, items){
items.push('<div class="evergreen-holdings-item">' + holdings.call_number + ' ' + holdings.availability + '</div>');
items.push('<div class="evergreen-holdings-item">' + holdings.call_number + ' - ' + holdings.location +' (' + holdings.availability + ')</div>');
}
function roblibEvergreenAddElectronicHoldings(holdings, items){
items.push('<div class="evergreen-holdings-item"><a href="' + holdings.url + '">' + holdings.label + '</div>');
}

19
targets/evergreen/roblib_search_evergreen.module

@ -233,6 +233,7 @@ function roblib_search_evergreen_parse_results($results, $query) {
roblib_search_evergreen_update_array($output, $index, 'date', $record->xpath('marcxml:datafield[@tag="260"]/marcxml:subfield[@code="c"]'));
roblib_search_evergreen_update_array($output, $index, 'id', $record->xpath('marcxml:datafield[@tag="901"]/marcxml:subfield[@code="c"]'));
roblib_search_evergreen_holdings($output, $index, $record->xpath('marcxml:datafield[@tag="852"]'));
roblib_search_evergreen_electronic_holdings($output, $index, $record->xpath('marcxml:datafield[@tag="856"]'));
$id = $output[$index]['id'];
$output[$index++]['url'] = variable_get('roblib_search_evergreen_url', 'http://137.149.200.52')
. variable_get('roblib_search_evergreen_detail_suffix', '/opac/en-CA/skin/default/xml/rdetail.xml?r=')
@ -249,7 +250,23 @@ function roblib_search_evergreen_holdings(&$arr, $index, $xpath_result){
$call_number = $xml->xpath('marcxml:subfield[@code="c"]');
$arr[$index]['holdings'][$holding_index]['call_number'] = (string)$call_number[0];
$availability = $xml->xpath('marcxml:subfield[@code="n"]');
$arr[$index]['holdings'][$holding_index++]['availability'] = (string)$availability[0];
$arr[$index]['holdings'][$holding_index]['availability'] = (string)$availability[0];
$location = $xml->xpath('marcxml:subfield[@code="a"]');
$arr[$index]['holdings'][$holding_index++]['location'] = (string)$location[0];
}
}
function roblib_search_evergreen_electronic_holdings(&$arr, $index, $xpath_result){
$holding_index = 0;
foreach($xpath_result as $xml){
$xml->registerXPathNamespace('marcxml', 'http://www.loc.gov/MARC21/slim');
$label = $xml->xpath('marcxml:subfield[@code="y"]');
$label = (string)$label[0];
$url = $xml->xpath('marcxml:subfield[@code="u"]');
$url = (string)$url[0];
$arr[$index]['electronic_holdings'][$holding_index]['url'] = $url;
$arr[$index]['electronic_holdings'][$holding_index++]['label'] = $label;
}
}

2
targets/evergreen/theme/roblib-search-evergreen.tpl.php

@ -18,7 +18,7 @@
?>
<div class ="roblib-search-content evergreen" id="roblib-search-content-evergreen">
<img src="<?php print $spinner_path; ?>"/>
<img src="<?php print (empty($spinner_path) ? ' ' : $spinner_path); ?>"/>
</div>
<div class ="roblib-search-more" id="roblib-search-evergreen-more">Search all Results</div>
Loading…
Cancel
Save