@ -119,6 +119,76 @@ class EBSCOAPI {
$this->config = $config;
}
/**
* Setter / Getter for authentication token.
*
* @param string The authentication token
*
* @return string or none
*
* @access public
*/
public function authenticationToken(){
$autocompleteTokenTimeOut = time();
$authenticationTimeout = 0;
//$authenticationTimeout;
// $autocompleteUrl = '';
// $token = '';
// $timeout = '';
// $autoToken = '';
// //$autocompleteTokenTimeOut = '';
// //$autocompleteTokenTimeOut;
// $autocompleteTokenTimeOut = 0;
// $autocompleteCustId ='';
if (isset($_SESSION["authenticationToken"])){
$token = $_SESSION["authenticationToken"];
$authenticationTimeout = $_SESSION["authenticationTimeout"]-600;
$timeout = $_SESSION["authenticationTimeout"];
$autocompleteUrl = $_SESSION["autocompleteUrl"];
$autoToken = $_SESSION["autocompleteToken"];
$autocompleteTokenTimeOut = $_SESSION["autocompleteTokenTimeOut"];
$autocompleteCustId = $_SESSION["autocompleteCustId"];
}else{
$result = $this->apiAuthenticationToken();
$_SESSION["authenticationToken"]= $result['authenticationToken'];
$_SESSION["authenticationTimeout"]= $result['authenticationTimeout'];
$_SESSION['autocompleteUrl'] = $result['autocompleteUrl'];
$_SESSION['autocompleteToken'] = $result['autocompleteToken'];
$_SESSION["autocompleteTokenTimeOut"]= $result['autocompleteTokenTimeOut'];
$_SESSION['autocompleteCustId'] = $result['autocompleteCustId'];
}
if(time()-$autocompleteTokenTimeOut >= $authenticationTimeout){
$result = $this->apiAuthenticationToken();
$_SESSION["authenticationToken"]= $result['authenticationToken'];
$_SESSION["authenticationTimeout"]= $result['authenticationTimeout'];
$_SESSION['autocompleteUrl'] = $result['autocompleteUrl'];
$_SESSION['autocompleteToken'] = $result['autocompleteToken'];
$_SESSION["autocompleteTokenTimeOut"]= $result['autocompleteTokenTimeOut'];
$_SESSION['autocompleteCustId'] = $result['autocompleteCustId'];
$result = array(
'authenticationToken' => $token,
'authenticationTimeout' => $timeout,
'autocompleteUrl' => $autocompleteUrl,
'autocompleteToken' => $autoToken,
'autocompleteTokenTimeOut' => $autocompleteTokenTimeOut,
'autocompleteCustId' => $autocompleteCustId
);
return $result['authenticationToken'];
}else{
return $this->token;
}
}
/**
* Setter / Getter for session token.
*
@ -203,7 +273,8 @@ class EBSCOAPI {
* @access protected
*/
protected function request($action, $params = NULL, $attempts = 5) {
$sessionToken = $this->sessionToken();
//$authenticationToken = $this->authenticationToken();
$sessionToken = $this->apiSessionToken();//$this->sessionToken();
// If authentication token is missing then the session token is missing too, so get both tokens
// If session token is missing then the authentication token may be invalid, so get both tokens.
@ -220,8 +291,7 @@ class EBSCOAPI {
// Any change of the isGuest should request a new session
// (and don't terminate the current request if there was an error during the session request
// since it's not that important)
$storedGuest = $this->readSession('isGuest');
if ($storedGuest !== '' & & $storedGuest != $this->connector()->isGuest()) {
if ($this->isGuest() != $this->connector()->isGuest()) {
$this->apiSessionToken();
}
@ -249,11 +319,8 @@ class EBSCOAPI {
}
break;
case EBSCOConnector::EDS_SESSION_TOKEN_MISSING:
case EBSCOConnector::EDS_SESSION_TOKEN_INVALID:
$this->deleteSession('sessionToken');
$this->sessionToken = NULL;
$result = $this->apiSessionToken();
$result = $this->apiAuthenticationAndSessionToken();
if ($this->isError($result)) {
// Any error should terminate the request immediately
// in order to prevent infinite recursion.
@ -303,7 +370,6 @@ class EBSCOAPI {
//$headers = array(
// 'x-authenticationToken: '.$this->authenticationToken(),
//);
$headers = [];
$response = $this->connector()->requestSessionToken($headers);
// Raise the exception so that any code running this method should exit immediately.
@ -314,7 +380,6 @@ class EBSCOAPI {
$result = $this->response($response)->result();
if (is_string($result)) {
$this->sessionToken($result);
$this->writeSession('isGuest', $this->connector()->isGuest());
return $result;
}
else {
@ -709,68 +774,36 @@ class EBSCOAPI {
}
/**
* How long (seconds) EDS values are cached for.
*
* EDS sessions time out after a period of inactivity, so keep this short.
* An expired token is also detected and replaced by request().
*/
const CACHE_LIFETIME = 900;
/**
* Builds the cache ID for a key, scoped to the EDS profile in use.
* Store the given object into session.
*
* @param string $key
* The key used for the value.
*
* @return string
* The cache ID.
*/
protected function cacheId($key) {
$profile = $this->config['profile'] ?? '';
return 'roblib_search_eds:' . $profile . ':' . $key;
}
/**
* Store the given value in the Drupal cache.
* The key used for reading the value.
* @param object $value
* The object stored in session.
*
* EDS tokens are obtained via IP authentication of the web server, so they
* are shared by all visitors rather than stored in each user's PHP session
* ($_SESSION is not persisted by Drupal and would start sessions for
* anonymous users, defeating the page cache).
* @return none
*
* @param string $key
* The key used for reading the value.
* @param mixed $value
* The value to store.
* @access protected
*/
protected function writeSession($key, $value) {
if (!empty($key) & & !empty($value)) {
\Drupal::cache()->set($this->cacheId($key), $value, time() + self::CACHE_LIFETIME) ;
$_SESSION['EBSCO'][$key] = $value;
}
}
/**
* Read the value having the given key from the Drupal cache .
* Read from session the object having the given key .
*
* @param string $key
* The key used for reading the value .
* The key used for reading the object .
*
* @return mixed
* The cached value, or an empty string if there is none.
*/
protected function readSession($key) {
$cache = \Drupal::cache()->get($this->cacheId($key));
return $cache ? $cache->data : '';
}
/**
* Remove the value having the given key from the Drupal cache.
* @return object
*
* @param string $key
* The key of the value to remove.
* @access protected
*/
protected function deleteSession($key) {
\Drupal::cache()->delete($this->cacheId($key));
protected function readSession($key) {
$value = isset($_SESSION['EBSCO'][$key]) ? $_SESSION['EBSCO'][$key] : '';
return $value;
}
/**
@ -783,7 +816,7 @@ class EBSCOAPI {
* @access protected
*/
protected function isError($object) {
return $object instanceof EBSCOException ;
return is_a($object, 'EBSCOException') ;
}
}