@ -119,76 +119,6 @@ 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.
*
@ -273,8 +203,7 @@ class EBSCOAPI {
* @access protected
*/
protected function request($action, $params = NULL, $attempts = 5) {
//$authenticationToken = $this->authenticationToken();
$sessionToken = $this->apiSessionToken();//$this->sessionToken();
$sessionToken = $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.
@ -291,7 +220,8 @@ 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)
if ($this->isGuest() != $this->connector()->isGuest()) {
$storedGuest = $this->readSession('isGuest');
if ($storedGuest !== '' & & $storedGuest != $this->connector()->isGuest()) {
$this->apiSessionToken();
}
@ -319,8 +249,11 @@ class EBSCOAPI {
}
break;
case EBSCOConnector::EDS_SESSION_TOKEN_MISSING:
case EBSCOConnector::EDS_SESSION_TOKEN_INVALID:
$result = $this->apiAuthenticationAndSessionToken();
$this->deleteSession('sessionToken');
$this->sessionToken = NULL;
$result = $this->apiSessionToken();
if ($this->isError($result)) {
// Any error should terminate the request immediately
// in order to prevent infinite recursion.
@ -370,6 +303,7 @@ 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.
@ -380,6 +314,7 @@ class EBSCOAPI {
$result = $this->response($response)->result();
if (is_string($result)) {
$this->sessionToken($result);
$this->writeSession('isGuest', $this->connector()->isGuest());
return $result;
}
else {
@ -774,36 +709,68 @@ class EBSCOAPI {
}
/**
* Store the given object into session.
* 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.
*
* @param string $key
* The key used for reading the value.
* @param object $value
* The object stored in session.
* The key used for the value.
*
* @return none
* @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.
*
* @access protected
* 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).
*
* @param string $key
* The key used for reading the value.
* @param mixed $value
* The value to store.
*/
protected function writeSession($key, $value) {
if (!empty($key) & & !empty($value)) {
$_SESSION['EBSCO'][$key] = $value;
\Drupal::cache()->set($this->cacheId($key), $value, time() + self::CACHE_LIFETIME) ;
}
}
/**
* Read from session the object having the given key .
* Read the value having the given key from the Drupal cache .
*
* @param string $key
* The key used for reading the object.
*
* @return object
* The key used for reading the value.
*
* @access protected
* @return mixed
* The cached value, or an empty string if there is none.
*/
protected function readSession($key) {
$value = isset($_SESSION['EBSCO'][$key]) ? $_SESSION['EBSCO'][$key] : '';
return $value;
$cache = \Drupal::cache()->get($this->cacheId($key));
return $cache ? $cache->data : '';
}
/**
* Remove the value having the given key from the Drupal cache.
*
* @param string $key
* The key of the value to remove.
*/
protected function deleteSession($key) {
\Drupal::cache()->delete($this->cacheId($key));
}
/**
@ -816,7 +783,7 @@ class EBSCOAPI {
* @access protected
*/
protected function isError($object) {
return is_a($object, 'EBSCOException') ;
return $object instanceof EBSCOException ;
}
}