Skip to content

Commit cd1bb4b

Browse files
Remove session usage for monitor filters, use cookies only
- Removed all $_SESSION reads/writes in _monitor_filters.php - Removed zm_session_start() and session_write_close() calls - Updated all filter value retrieval to use cookies only via getFilterFromCookie() - Updated Group::get_group_dropdown() to use cookies instead of session - Filter persistence now entirely client-side via cookies - Simplified code by removing session management complexity Co-authored-by: connortechnology <925519+connortechnology@users.noreply.github.com>
1 parent a0f9194 commit cd1bb4b

File tree

2 files changed

+46
-52
lines changed

2 files changed

+46
-52
lines changed

web/includes/Group.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,21 @@ public function MonitorIds( ) {
6868
}
6969

7070
public static function get_group_dropdown( $view = null ) {
71-
$selected_group_id = 0;
72-
if ( isset($_REQUEST['groups']) ) {
73-
$selected_group_id = $group_id = $_SESSION['groups'] = $_REQUEST['groups'];
74-
} else if ( isset( $_SESSION['groups'] ) ) {
75-
$selected_group_id = $group_id = $_SESSION['groups'];
76-
} else if ( isset($_REQUEST['filtering']) ) {
77-
zm_session_start();
78-
unset($_SESSION['groups']);
79-
session_write_close();
71+
// Get selected value from cookie
72+
$selectedValue = null;
73+
if (isset($_COOKIE['zmFilter_GroupId'])) {
74+
$cookieValue = $_COOKIE['zmFilter_GroupId'];
75+
if ($cookieValue && $cookieValue !== '') {
76+
// Try to decode JSON for array values
77+
$decoded = json_decode($cookieValue, true);
78+
$selectedValue = ($decoded !== null) ? $decoded : $cookieValue;
79+
}
8080
}
8181

8282
// Use monitorFilterOnChange on console view for AJAX refresh, submitThisForm elsewhere
8383
$onChangeFunction = ($view == 'console') ? 'monitorFilterOnChange' : 'submitThisForm';
8484

85-
return htmlSelect('GroupId[]', Group::get_dropdown_options(), isset($_SESSION['GroupId'])?$_SESSION['GroupId']:null, array(
85+
return htmlSelect('GroupId[]', Group::get_dropdown_options(), $selectedValue, array(
8686
'data-on-change' => $onChangeFunction,
8787
'class'=>'chosen',
8888
'multiple'=>'multiple',

web/skins/classic/views/_monitor_filters.php

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function addFilterSelect($name, $options) {
2323
// Use monitorFilterOnChange on console view for AJAX refresh, submitThisForm elsewhere
2424
$onChangeFunction = ($view == 'console') ? 'monitorFilterOnChange' : 'submitThisForm';
2525

26-
// Get selected value from cookie first, then fallback to session
26+
// Get selected value from cookie only
2727
$selectedValue = '';
2828
if (isset($_COOKIE['zmFilter_'.$name])) {
2929
$cookieValue = $_COOKIE['zmFilter_'.$name];
@@ -32,8 +32,6 @@ function addFilterSelect($name, $options) {
3232
$decoded = json_decode($cookieValue, true);
3333
$selectedValue = ($decoded !== null) ? $decoded : $cookieValue;
3434
}
35-
} else if (isset($_SESSION[$name])) {
36-
$selectedValue = $_SESSION[$name];
3735
}
3836

3937
$html = '<span class="term '.$name.'Filter"><label>'.translate($name).'</label>';
@@ -75,30 +73,19 @@ function buildMonitorsFilters() {
7573
// Use monitorFilterOnChange on console view for AJAX refresh, submitThisForm elsewhere
7674
$onChangeFunction = ($view == 'console') ? 'monitorFilterOnChange' : 'submitThisForm';
7775

78-
zm_session_start();
79-
foreach (array('GroupId','Capturing','Analysing','Recording','ServerId','StorageId','Status','MonitorId','MonitorName','Source') as $var) {
80-
if (isset($_REQUEST[$var])) {
81-
if ($_REQUEST[$var] != '') {
82-
$_SESSION[$var] = $_REQUEST[$var];
83-
} else {
84-
unset($_SESSION[$var]);
85-
}
86-
} else if (isset($_REQUEST['filtering'])) {
87-
unset($_SESSION[$var]);
88-
} else if (!isset($_SESSION[$var])) {
89-
// Restore from cookie if session doesn't have a value
90-
$cookieName = 'zmFilter_'.$var;
91-
if (isset($_COOKIE[$cookieName])) {
92-
$cookieValue = $_COOKIE[$cookieName];
93-
if ($cookieValue && $cookieValue !== '') {
94-
// Try to decode JSON for array values
95-
$decoded = json_decode($cookieValue, true);
96-
$_SESSION[$var] = ($decoded !== null) ? $decoded : $cookieValue;
97-
}
76+
// Helper function to get filter value from cookie
77+
function getFilterFromCookie($var) {
78+
$cookieName = 'zmFilter_'.$var;
79+
if (isset($_COOKIE[$cookieName])) {
80+
$cookieValue = $_COOKIE[$cookieName];
81+
if ($cookieValue && $cookieValue !== '') {
82+
// Try to decode JSON for array values
83+
$decoded = json_decode($cookieValue, true);
84+
return ($decoded !== null) ? $decoded : $cookieValue;
9885
}
9986
}
87+
return null;
10088
}
101-
session_write_close();
10289

10390
$storage_areas = ZM\Storage::find();
10491
$StorageById = array();
@@ -130,7 +117,7 @@ function buildMonitorsFilters() {
130117
$html .= '<span class="term" id="groupControl"><label>'. translate('Group') .'</label>';
131118
$html .= '<span class="term-value-wrapper">';
132119
# This will end up with the group_id of the deepest selection
133-
$group_id = isset($_SESSION['GroupId']) ? $_SESSION['GroupId'] : null;
120+
$group_id = getFilterFromCookie('GroupId');
134121
$html .= ZM\Group::get_group_dropdown($view);
135122
$groupSql = ZM\Group::get_group_sql($group_id);
136123
$html .= addButtonResetForFilterSelect('GroupId[]');
@@ -139,8 +126,10 @@ function buildMonitorsFilters() {
139126
}
140127
}
141128

142-
$selected_monitor_ids = isset($_SESSION['MonitorId']) ? $_SESSION['MonitorId'] : array();
143-
if ( !is_array($selected_monitor_ids) ) {
129+
$selected_monitor_ids = getFilterFromCookie('MonitorId');
130+
if (!$selected_monitor_ids) {
131+
$selected_monitor_ids = array();
132+
} else if (!is_array($selected_monitor_ids)) {
144133
$selected_monitor_ids = array($selected_monitor_ids);
145134
}
146135

@@ -150,13 +139,14 @@ function buildMonitorsFilters() {
150139
if ( $groupSql )
151140
$conditions[] = $groupSql;
152141
foreach ( array('ServerId','StorageId','Status','Capturing','Analysing','Recording') as $filter ) {
153-
if ( isset($_SESSION[$filter]) ) {
154-
if ( is_array($_SESSION[$filter]) ) {
155-
$conditions[] = '`'.$filter . '` IN ('.implode(',', array_map(function(){return '?';}, $_SESSION[$filter])). ')';
156-
$values = array_merge($values, $_SESSION[$filter]);
142+
$filterValue = getFilterFromCookie($filter);
143+
if ( $filterValue ) {
144+
if ( is_array($filterValue) ) {
145+
$conditions[] = '`'.$filter . '` IN ('.implode(',', array_map(function(){return '?';}, $filterValue)).')';
146+
$values = array_merge($values, $filterValue);
157147
} else {
158148
$conditions[] = '`'.$filter . '`=?';
159-
$values[] = $_SESSION[$filter];
149+
$values[] = $filterValue;
160150
}
161151
}
162152
} # end foreach filter
@@ -167,9 +157,10 @@ function buildMonitorsFilters() {
167157
$values = array_merge($values, $ids);
168158
}
169159

160+
$monitorNameValue = getFilterFromCookie('MonitorName');
170161
$html .= '<span class="term MonitorNameFilter"><label>'.translate('Name').'</label>';
171162
$html .= '<span class="term-value-wrapper">';
172-
$html .= '<input type="text" name="MonitorName" value="'.(isset($_SESSION['MonitorName'])?validHtmlStr($_SESSION['MonitorName']):'').'" placeholder="'.translate('text or regular expression').'"/></span>';
163+
$html .= '<input type="text" name="MonitorName" value="'.($monitorNameValue ? validHtmlStr($monitorNameValue) : '').'" placeholder="'.translate('text or regular expression').'"/></span>';
173164
$html .= '</span>'.PHP_EOL;
174165

175166
$html .= addFilterSelect('Capturing', array('None'=>translate('None'), 'Always'=>translate('Always'), 'OnDemand'=>translate('On Demand')));
@@ -180,7 +171,7 @@ function buildMonitorsFilters() {
180171
$html .= '<span class="term ServerFilter"><label>'. translate('Server').'</label>';
181172
$html .= '<span class="term-value-wrapper">';
182173
$html .= htmlSelect('ServerId[]', $ServersById,
183-
(isset($_SESSION['ServerId'])?$_SESSION['ServerId']:''),
174+
getFilterFromCookie('ServerId') ?: '',
184175
array(
185176
'data-on-change'=>$onChangeFunction,
186177
'class'=>'chosen',
@@ -197,7 +188,7 @@ function buildMonitorsFilters() {
197188
$html .= '<span class="term StorageFilter"><label>'.translate('Storage').'</label>';
198189
$html .= '<span class="term-value-wrapper">';
199190
$html .= htmlSelect('StorageId[]', $StorageById,
200-
(isset($_SESSION['StorageId'])?$_SESSION['StorageId']:''),
191+
getFilterFromCookie('StorageId') ?: '',
201192
array(
202193
'data-on-change'=>$onChangeFunction,
203194
'class'=>'chosen',
@@ -218,7 +209,7 @@ function buildMonitorsFilters() {
218209
);
219210
$html .= '<span class="term-value-wrapper">';
220211
$html .= htmlSelect( 'Status[]', $status_options,
221-
( isset($_SESSION['Status']) ? $_SESSION['Status'] : '' ),
212+
getFilterFromCookie('Status') ?: '',
222213
array(
223214
'data-on-change'=>$onChangeFunction,
224215
'class'=>'chosen',
@@ -229,9 +220,10 @@ function buildMonitorsFilters() {
229220
$html .= '</span>';
230221
$html .= '</span>';
231222

223+
$sourceValue = getFilterFromCookie('Source');
232224
$html .= '<span class="term SourceFilter"><label>'.translate('Source').'</label>';
233225
$html .= '<span class="term-value-wrapper">';
234-
$html .= '<input type="text" name="Source" value="'.(isset($_SESSION['Source'])?validHtmlStr($_SESSION['Source']):'').'" placeholder="'.translate('text or regular expression').'"/>';
226+
$html .= '<input type="text" name="Source" value="'.($sourceValue ? validHtmlStr($sourceValue) : '').'" placeholder="'.translate('text or regular expression').'"/>';
235227
$html .= '</span>';
236228
$html .= '</span>';
237229

@@ -276,11 +268,12 @@ function buildMonitorsFilters() {
276268
continue;
277269
}
278270

279-
if ( isset($_SESSION['MonitorName']) ) {
271+
$monitorNameFilter = getFilterFromCookie('MonitorName');
272+
if ( $monitorNameFilter ) {
280273
$Monitor = new ZM\Monitor($monitors[$i]);
281274
ini_set('track_errors', 'on');
282275
$php_errormsg = '';
283-
$regexp = $_SESSION['MonitorName'];
276+
$regexp = $monitorNameFilter;
284277
if (!strpos($regexp, '/')) $regexp = '/'.$regexp.'/i';
285278

286279
@preg_match($regexp, '');
@@ -293,18 +286,19 @@ function buildMonitorsFilters() {
293286
}
294287
}
295288

296-
if ( isset($_SESSION['Source']) ) {
289+
$sourceFilter = getFilterFromCookie('Source');
290+
if ( $sourceFilter ) {
297291
$Monitor = new ZM\Monitor($monitors[$i]);
298292
ini_set('track_errors', 'on');
299293
$php_errormsg = '';
300-
$regexp = $_SESSION['Source'];
294+
$regexp = $sourceFilter;
301295

302296
if (!preg_match("/^\/.+\/[a-z]*$/i",$regexp))
303297
$regexp = '/'.$regexp.'/i';
304298

305299
@preg_match($regexp, '');
306300
if ( $php_errormsg ) {
307-
ZM\Warning($_SESSION['Source'].' is not a valid search string');
301+
ZM\Warning($sourceFilter.' is not a valid search string');
308302
} else {
309303
ZM\Debug("Using $regexp for source");
310304
if ( !preg_match($regexp, $Monitor->Source()) ) {

0 commit comments

Comments
 (0)