LSsearch: fix/improve handling session data

- avoid PHP warning about unset key
- make purge methods accepting more specific object type/context
- store cache in dedicated sub-array to avoid purging params when 
purging cache
- fix strange problem about not preserved params due to too large purges 
in some contexts
This commit is contained in:
Benjamin Renard 2023-05-24 20:13:02 +02:00
parent cc9942019f
commit c98e31b7c6
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -156,7 +156,7 @@ class LSsearch extends LSlog_staticLoggerClass {
} }
} }
else { else {
$this -> purgeParams($LSobject); $this -> purgeParams($LSobject, $context);
$this -> loadDefaultParameters(); $this -> loadDefaultParameters();
} }
@ -214,7 +214,14 @@ class LSsearch extends LSlog_staticLoggerClass {
* @return boolean True if params has been loaded from session or False * @return boolean True if params has been loaded from session or False
*/ */
private function loadParamsFromSession() { private function loadParamsFromSession() {
if (isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context]) && is_array($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context])) { if (
isset($_SESSION['LSsession'])
&& isset($_SESSION['LSsession']['LSsearch'])
&& isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject])
&& isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'])
&& isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context])
&& is_array($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context])
) {
self :: log_debug('Load params from session for context '.$this -> context); self :: log_debug('Load params from session for context '.$this -> context);
$params = $_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context]; $params = $_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context];
@ -236,15 +243,28 @@ class LSsearch extends LSlog_staticLoggerClass {
* @return void * @return void
*/ */
private function saveParamsInSession() { private function saveParamsInSession() {
self :: log_debug('Save context params session '.$this -> context); self :: log_debug(
'saveParamsInSession(".$this -> LSobject."): Save context params session '.$this -> context);
$params = $this -> params; $params = $this -> params;
if ($params['filter'] instanceof Net_LDAP2_Filter) { if ($params['filter'] instanceof Net_LDAP2_Filter) {
$params['filter'] = $params['filter'] -> asString(); $params['filter'] = $params['filter'] -> asString();
} }
if (!isset($_SESSION['LSsession']))
$_SESSION['LSsession'] = array();
if (!isset($_SESSION['LSsession']['LSsearch']))
$_SESSION['LSsession']['LSsearch'] = array();
if (!isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]))
$_SESSION['LSsession']['LSsearch'][$this -> LSobject] = array();
if (!isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params']))
$_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'] = array();
if (!isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context]))
$_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context] = array();
foreach ($params as $param => $value) { foreach ($params as $param => $value) {
if ( !isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context][$param]) || $_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context][$param]!=$value) { if (
self :: log_trace("$param => ".varDump($value)); !isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context][$param])
|| $_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context][$param] != $value
) {
self :: log_trace("saveParamsInSession(".$this -> LSobject."): $param => ".varDump($value));
$_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context][$param]=$value; $_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context][$param]=$value;
} }
} }
@ -253,30 +273,61 @@ class LSsearch extends LSlog_staticLoggerClass {
/** /**
* Purge parameters in session * Purge parameters in session
* *
* @param string $LSobject The LSobject type * @param string $LSobject The LdapObject type
* @param string|null $context The context of the search (optional, default: null)
* *
* @return void * @return void
*/ */
public static function purgeParams($LSobject) { public static function purgeParams($LSobject, $context=null) {
unset($_SESSION['LSsession']['LSsearch'][$LSobject]['params']); if (!isset($_SESSION['LSsession'])) return;
if (!isset($_SESSION['LSsession']['LSsearch'])) return;
if (!isset($_SESSION['LSsession']['LSsearch'][$LSobject])) return;
if (!isset($_SESSION['LSsession']['LSsearch'][$LSobject]['params'])) return;
if ($context) {
if (isset($_SESSION['LSsession']['LSsearch'][$LSobject]['params'][$context]))
unset($_SESSION['LSsession']['LSsearch'][$LSobject]['params'][$context]);
}
else
unset($_SESSION['LSsession']['LSsearch'][$LSobject]['params']);
} }
/** /**
* Purge cache * Purge cache
* *
* @param string $LSobject The LdapObject type
* @param string|null $hash The hash of the search (optional, default: null)
*
* @return void * @return void
*/ */
public static function purgeCache($LSobject) { public static function purgeCache($LSobject, $hash=null) {
unset($_SESSION['LSsession']['LSsearch'][$LSobject]); if (!isset($_SESSION['LSsession'])) return;
if (!isset($_SESSION['LSsession']['LSsearch'])) return;
if (!isset($_SESSION['LSsession']['LSsearch'][$LSobject])) return;
if (!isset($_SESSION['LSsession']['LSsearch'][$LSobject]['cache'])) return;
if ($hash) {
if (isset($_SESSION['LSsession']['LSsearch'][$LSobject]['cache'][$hash]))
unset($_SESSION['LSsession']['LSsearch'][$LSobject]['cache'][$hash]);
}
else
unset($_SESSION['LSsession']['LSsearch'][$LSobject]['cache']);
} }
/** /**
* Purge session * Purge session
* *
* @param string $LSobject The LdapObject type
*
* @return void * @return void
*/ */
private function purgeSession() { private function purgeSession($LSobject=null) {
unset($_SESSION['LSsession']['LSsearch']); if (!isset($_SESSION['LSsession'])) return;
if (!isset($_SESSION['LSsession']['LSsearch'])) return;
if ($LSobject) {
if (isset($_SESSION['LSsession']['LSsearch'][$LSobject]))
unset($_SESSION['LSsession']['LSsearch'][$LSobject]);
}
else
unset($_SESSION['LSsession']['LSsearch']);
} }
/** /**
@ -1121,7 +1172,15 @@ class LSsearch extends LSlog_staticLoggerClass {
if ($this -> cacheIsEnabled()) { if ($this -> cacheIsEnabled()) {
$hash = $this->getHash(); $hash = $this->getHash();
self :: log_trace("addResultToCache(): Save result in cache with hash '$hash'."); self :: log_trace("addResultToCache(): Save result in cache with hash '$hash'.");
$_SESSION['LSsession']['LSsearch'][$this -> LSobject][$hash]=$this->result; if (!isset($_SESSION['LSsession']))
$_SESSION['LSsession'] = array();
if (!isset($_SESSION['LSsession']['LSsearch']))
$_SESSION['LSsession']['LSsearch'] = array();
if (!isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]))
$_SESSION['LSsession']['LSsearch'][$this -> LSobject] = array();
if (!isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['cache']))
$_SESSION['LSsession']['LSsearch'][$this -> LSobject]['cache'] = array();
$_SESSION['LSsession']['LSsearch'][$this -> LSobject]['cache'][$hash] = $this->result;
} }
else else
self :: log_trace('addResultToCache(): cache is disabled.'); self :: log_trace('addResultToCache(): cache is disabled.');
@ -1134,10 +1193,16 @@ class LSsearch extends LSlog_staticLoggerClass {
**/ **/
private function getResultFromCache() { private function getResultFromCache() {
if ($this -> cacheIsEnabled()) { if ($this -> cacheIsEnabled()) {
$hash=$this->getHash(); $hash = $this->getHash();
if (isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject][$hash])) { if (
isset($_SESSION['LSsession'])
&& isset($_SESSION['LSsession']['LSsearch'])
&& isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject])
&& isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['cache'])
&& isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['cache'][$hash])
) {
self :: log_trace('getResultFromCache(): result found in cache.'); self :: log_trace('getResultFromCache(): result found in cache.');
return $_SESSION['LSsession']['LSsearch'][$this -> LSobject][$hash]; return $_SESSION['LSsession']['LSsearch'][$this -> LSobject]['cache'][$hash];
} }
self :: log_trace('getResultFromCache(): result not found in cache.'); self :: log_trace('getResultFromCache(): result not found in cache.');
} }