diff --git a/CHANGELOG.txt b/CHANGELOG.txt index c1263220..178b160a 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,6 +1,10 @@ FlightPath 7.x Changelog ======================== +2026-11-04 + Revised the watchdog screen (admin/config/watchdog) to use the FAPI as well as + new filters for searching by date range. + 2026-10-30 Added global setting for Alerts to either enable or disable sending emails on new diff --git a/includes/render.inc b/includes/render.inc index 77b51822..a0f355d3 100644 --- a/includes/render.inc +++ b/includes/render.inc @@ -220,6 +220,20 @@ function fp_get_form($form_id, $params = array()) { call_user_func_array($module . '_form_alter', array(&$form, $form_id)); } + // Form properties (that begin with #) are metadata, not renderable elements, and should not be + // passed through the element weight sorter. + $form_properties = array(); + $form_elements = array(); + foreach ($form as $key => $element) { + if (is_string($key) && substr($key, 0, 1) === '#') { + $form_properties[$key] = $element; + } + else { + $form_elements[$key] = $element; + } + } + $form = $form_elements; + // Okay, now the fun part. Re-order the elements by weight. Lighter weights // should float to the top. Elements w/o a weight listed are assumed to have a weight of 0. // Unfortunately we cannot use uasort, as it re-orders our indexes when weights are identical. @@ -269,7 +283,7 @@ function fp_get_form($form_id, $params = array()) { // Okay, we should now be good to go! - return $new_form; + return $form_properties + $new_form; } diff --git a/modules/admin/admin.module b/modules/admin/admin.module index 310584da..27165842 100644 --- a/modules/admin/admin.module +++ b/modules/admin/admin.module @@ -1731,97 +1731,69 @@ function admin_display_watchdog() { $rtn .= "

View recent watchdog entries

"; $severity_array = array( - 5 => "notice", - 1 => "alert", - 3 => "error", - 7 => "debug", + WATCHDOG_NOTICE => "notice", + WATCHDOG_ALERT => "alert", + WATCHDOG_ERROR => "error", + WATCHDOG_DEBUG => "debug", ); - $results_per_page = 50; + $limit = 50; + $type_line = $severity_line = $date_line = ""; - - $type_line = $severity_line = $limit_start = ""; - - @$sev_filter = $_GET["sev_filter"]; - @$type_filter = $_GET["type_filter"]; - @$page = intval($_GET["page"]); - - $limit_start = 0; - if ($page > 0) { - $limit_start = $results_per_page * ($page - 1); + $sev_filter = $_GET["sev_filter"] ?? ""; + if (!is_string($sev_filter)) { + $sev_filter = ""; } - - - - // Filters: - $rtn .= "
- "; - - $rtn .= "Filter by: - - Type: - - "; - - - $rtn .= " - - Severity: - - "; - - - - - $rtn .= ""; - // Hack if clean URLs are not enabled: - $rtn .= ""; - - $rtn .= "
"; - - + $end_datetime = $_GET['end_datetime'] ?? ''; + $end_timestamp = admin_watchdog_datetime_to_timestamp($end_datetime); + if ($end_timestamp === FALSE) { + $end_datetime = ''; + } + $page = $_GET["page"] ?? 0; + if (!is_scalar($page)) { + $page = 0; + } + $page = intval($page); + $rtn .= fp_render_form("admin_watchdog_filter_form"); $params = array(); - if (@$_GET["type_filter"] != "") { - //$type_line = " AND type = '" . mysql_real_escape_string($_GET["type_filter"]) . "' "; + if ($type_filter != "") { $type_line = " AND `type` = :type "; - $params[":type"] = $_GET["type_filter"]; + $params[":type"] = $type_filter; } - if (@$_GET["sev_filter"] != "") { - //$severity_line = " AND severity = '" . mysql_real_escape_string($_GET["sev_filter"]) . "' "; + if ($sev_filter != "") { $severity_line = " AND severity = :sev "; - $params[":sev"] = $_GET["sev_filter"]; + $params[":sev"] = $sev_filter; } + if ($start_timestamp !== FALSE) { + $date_line .= " AND a.timestamp >= :start_timestamp "; + $params[":start_timestamp"] = $start_timestamp; + } - $pol = "even"; + if ($end_timestamp !== FALSE) { + $date_line .= " AND a.timestamp <= :end_timestamp "; + $params[":end_timestamp"] = $end_timestamp; + } + $pol = "even"; $table_headers = array(); $table_headers[] = array("label" => "ID", "field" => "wid"); @@ -1838,20 +1810,18 @@ function admin_display_watchdog() { // Draw our our table headers, with links.... $rtn .= theme_table_header_sortable($table_headers); - - // Get our order by clause based on selected table header, if any. $order_by = theme_table_header_sortable_order_by($table_headers); $res = pager_query("SELECT * FROM watchdog a WHERE 1 $type_line $severity_line - $order_by", $params, 50, 0, "SELECT COUNT(wid) FROM watchdog a + $date_line + $order_by", $params, $limit, 0, "SELECT COUNT(wid) FROM watchdog a WHERE 1 $type_line $severity_line - $order_by", $params); - + $date_line"); while ($cur = db_fetch_object($res)) { $pol = ($pol == "even") ? "odd" : "even"; @@ -1870,15 +1840,12 @@ function admin_display_watchdog() { $type = trim($type) . "..."; } - - $severity = $severity_array[$cur->severity]; $when = format_date(convert_time($cur->timestamp), "short"); - $rtn .= " - " . l($cur->wid, "admin/config/watchdog/$cur->wid", "sev_filter=$sev_filter&type_filter=$type_filter&page=$page") . " + " . l($cur->wid, "admin/config/watchdog/$cur->wid", fp_query_string_encode(array('sev_filter' => $sev_filter, 'type_filter' => $type_filter, 'start_datetime' => $start_datetime, 'end_datetime' => $end_datetime, 'page' => $page))) . " $when $type $msg @@ -1891,22 +1858,220 @@ function admin_display_watchdog() { } - $rtn .= ""; + // Display the pager that was generated by the pager_query above! + $rtn .= theme_pager(array(), $limit); + return $rtn; - // Display the pager that was generated by the pager_query above! - $rtn .= theme_pager(); +} // admin display watchdog +/** + * Build the watchdog filter form. + * + * @return array + * A FlightPath form array. + */ +function admin_watchdog_filter_form() { + $form = array(); + $type_filter = $_GET['type_filter'] ?? ''; + if (!is_string($type_filter)) { + $type_filter = ''; + } - return $rtn; + $sev_filter = $_GET['sev_filter'] ?? ''; + if (!is_string($sev_filter)) { + $sev_filter = ''; + } -} // admin display watchdog + $start_datetime = $_GET['start_datetime'] ?? ''; + if (admin_watchdog_datetime_to_timestamp($start_datetime) === FALSE) { + $start_datetime = ''; + } + $end_datetime = $_GET['end_datetime'] ?? ''; + if (admin_watchdog_datetime_to_timestamp($end_datetime) === FALSE) { + $end_datetime = ''; + } + $type_options = array('' => t('- Any -')); + $res = db_query("SELECT DISTINCT(type) AS type + FROM watchdog + ORDER BY type"); + while ($cur = db_fetch_object($res)) { + $type_options[$cur->type] = $cur->type; + } + + $form['#submit_method'] = 'GET'; + $form['#attributes'] = array( + 'class' => 'watchdog-filters', + ); + $form['type_filter'] = array( + 'type' => 'select', + 'label' => t('Type:'), + 'options' => $type_options, + 'value' => $type_filter, + 'hide_please_select' => TRUE, + 'inline' => TRUE, + 'weight' => 10, + ); + + $form['sev_filter'] = array( + 'type' => 'select', + 'label' => t('Severity:'), + 'options' => array( + '' => t('- Any -'), + WATCHDOG_NOTICE => t('notice'), + WATCHDOG_ALERT => t('alert'), + WATCHDOG_ERROR => t('error'), + WATCHDOG_DEBUG => t('debug'), + ), + + 'value' => $sev_filter, + 'hide_please_select' => TRUE, + 'inline' => TRUE, + 'attributes' => array( + 'class' => 'watchdog-severity-filter', + ), + 'weight' => 20, + ); + + $form['start_datetime'] = array( + 'type' => 'datetime-local', + 'label' => t('Start:'), + 'value' => $start_datetime, + 'inline' => TRUE, + 'weight' => 30, + ); + + $form['end_datetime'] = array( + 'type' => 'datetime-local', + 'label' => t('End:'), + 'value' => $end_datetime, + 'inline' => TRUE, + 'attributes' => array( + 'class' => 'watchdog-end-datetime', + ), + 'weight' => 40, + ); + + $form['apply_btn'] = array( + 'type' => 'submit', + 'value' => t('Apply'), + 'inline' => TRUE, + 'weight' => 100, + ); + + $form['reset_btn'] = array( + 'type' => 'submit', + 'value' => t('Reset'), + 'inline' => TRUE, + 'weight' => 110, + ); + + return $form; +} + +/** + * Validate a watchdog filter form submission. + * + * @param array $form + * The form definition. + * @param array $form_state + * The submitted form state. + */ +function admin_watchdog_filter_form_validate($form, &$form_state) { + foreach (array('type_filter', 'sev_filter') as $name) { + $value = $form_state['values'][$name] ?? ''; + if (!is_string($value) || !array_key_exists($value, $form[$name]['options'])) { + form_error($name, t('Please select a valid @label filter.', array('@label' => strtolower($form[$name]['label'])))); + } + } + + $start_value = $form_state['values']['start_datetime'] ?? ''; + $end_value = $form_state['values']['end_datetime'] ?? ''; + $start_timestamp = admin_watchdog_datetime_to_timestamp($start_value); + $end_timestamp = admin_watchdog_datetime_to_timestamp($end_value); + + if ($start_value !== '' && $start_timestamp === FALSE) { + form_error('start_datetime', t('Please enter a valid start date and time.')); + } + if ($end_value !== '' && $end_timestamp === FALSE) { + form_error('end_datetime', t('Please enter a valid end date and time.')); + } + if ($start_timestamp !== FALSE && $end_timestamp !== FALSE && $start_timestamp > $end_timestamp) { + form_error('end_datetime', t('The end date and time must not be before the start date and time.')); + } +} + +/** + * Redirect a watchdog filter submission to the filtered list. + * + * @param array $form + * The form definition. + * @param array $form_state + * The submitted form state. + */ +function admin_watchdog_filter_form_submit(&$form, &$form_state) { + $query = array(); + $values = $form_state['values']; + + if (!empty($values['apply_btn'])) { + if ($values['type_filter'] != '') { + $query['type_filter'] = $values['type_filter']; + } + if ($values['sev_filter'] != '') { + $query['sev_filter'] = $values['sev_filter']; + } + if ($values['start_datetime'] != '') { + $query['start_datetime'] = $values['start_datetime']; + } + if ($values['end_datetime'] != '') { + $query['end_datetime'] = $values['end_datetime']; + } + } + + $form['#redirect'] = array( + 'path' => 'admin/config/watchdog', + 'query' => fp_query_string_encode($query), + ); +} + + + + + + + +/** + * Convert a local datetime filter value to a watchdog timestamp. + * + * @param mixed $value + * A datetime-local value. + * + * @return int|false + * The Unix timestamp, or FALSE when the value is blank or invalid. + */ +function admin_watchdog_datetime_to_timestamp($value) { + if (!is_string($value) || $value === '') { + return FALSE; + } + + $timezone = new DateTimeZone(fp_get_user_timezone()); + $datetime = DateTime::createFromFormat('!Y-m-d\TH:i', $value, $timezone); + $errors = DateTime::getLastErrors(); + if ($datetime === FALSE || ($errors !== FALSE && ($errors['warning_count'] > 0 || $errors['error_count'] > 0))) { + return FALSE; + } + if ($datetime->format('Y-m-d\TH:i') !== $value) { + return FALSE; + } + + return $datetime->getTimestamp(); +} diff --git a/modules/admin/css/admin.css b/modules/admin/css/admin.css index fac4ac41..7cdabb36 100644 --- a/modules/admin/css/admin.css +++ b/modules/admin/css/admin.css @@ -194,8 +194,33 @@ table.admin-display-main-table td.system-configuration-left padding-top: 15px; } -.watchdog-filters .severity-filter { - padding-left: 15px; +.watchdog-filters .element-wrapper--watchdog-severity-filter { + padding-left: 5px; +} + +.watchdog-filters .element-wrapper--watchdog-end-datetime { + padding-left: 5px; +} + +.watchdog-filters > .element-type-submit { + margin-left: 5px; +} + +/* Keep the watchdog filters compact without changing global form controls. */ +.watchdog-filters .element-type-select, +.watchdog-filters .element-type-datetime-local, +.watchdog-filters .element-type-submit { + display: inline-block; + margin-bottom: 0; + width: auto; +} + +.watchdog-filters .element-type-select select, +.watchdog-filters .element-type-datetime-local input, +.watchdog-filters .element-type-submit input { + font-size: 0.9em; + padding: 0.4rem 0.8rem; + width: auto; } /* diff --git a/modules/system/system.module b/modules/system/system.module index b2dc1cbd..bafe2751 100644 --- a/modules/system/system.module +++ b/modules/system/system.module @@ -2777,8 +2777,10 @@ function system_handle_form_submit() { if (!is_string($callback)) $callback = ''; if (!is_string($form_type)) $form_type = ''; - watchdog('system', "handle_form_submit callback:$callback, form_type:$form_type", array()); - + if ($callback !== 'admin_watchdog_filter_form') { + watchdog('system', "handle_form_submit callback:$callback, form_type:$form_type", array()); + } + $form = array(); $form_include = @$_REQUEST["form_include"]; @@ -2876,22 +2878,32 @@ function system_handle_form_submit() { } // Actually get the form now. $form = fp_get_form($callback, $form_params); + + // Read submitted element values from the request method declared by the form. + $submitted_values = $_POST; + if (strtoupper($form['#submit_method'] ?? 'POST') === 'GET') { + $submitted_values = $_GET; + } + foreach ($form as $name => $element) { + if (is_string($name) && substr($name, 0, 1) === '#') { + continue; + } // Save to our $values array, but we don't care about markup. if (@$element["type"] != "" && @$element["type"] != "markup" && @$element["type"] != "markup_no_wrappers") { - $values[$name] = @$_POST[$name]; + $values[$name] = @$submitted_values[$name]; // Save to safe_values, too, if this is not a password field. if (@$element["type"] != "password") { - $safe_values[$name] = @$_POST[$name]; + $safe_values[$name] = @$submitted_values[$name]; } - // If this is a checkbox, and we have any value in the POST, it should + // If this is a checkbox, and we have any submitted value, it should // be saved as boolean TRUE if (isset($element["type"]) && $element["type"] == "checkbox") { - if (isset($_POST[$name]) && $_POST[$name] === "1") { + if (isset($submitted_values[$name]) && $submitted_values[$name] === "1") { $values[$name] = TRUE; } else { @@ -2916,7 +2928,7 @@ function system_handle_form_submit() { } - // Do we need to alter the value from the POST? + // Do we need to alter the submitted value? // If this element is a fieldset or cfieldset, it may contain other elements. We should get // those values too. @@ -2925,17 +2937,17 @@ function system_handle_form_submit() { foreach ($element["elements"][$k] as $cname => $celement) { // Save to our $values array, but we don't care about markup. if (@$celement["type"] != "" && @$celement["type"] != "markup" && @$element["type"] != "markup_no_wrappers") { - $values[$cname] = @$_POST[$cname]; + $values[$cname] = @$submitted_values[$cname]; // Save to save_values, too, if this is not a password field. if (@$celement["type"] != "password") { - $safe_values[$cname] = @$_POST[$cname]; + $safe_values[$cname] = @$submitted_values[$cname]; } - // If this is a checkbox, and we have any value in the POST, it should + // If this is a checkbox, and we have any submitted value, it should // be saved as boolean TRUE if (isset($celement["type"]) && $celement["type"] == "checkbox") { - if (isset($_POST[$cname]) && $_POST[$cname] === "1") { + if (isset($submitted_values[$cname]) && $submitted_values[$cname] === "1") { $values[$cname] = TRUE; } else { @@ -3000,7 +3012,7 @@ function system_handle_form_submit() { $_SESSION["fp_form_submissions"][$callback]["values"] = $safe_values; } - $form_state = array("values" => $values, "POST" => $_POST); + $form_state = array("values" => $values, "POST" => $_POST, 'GET' => $_GET); // Let's pass this through our default form validator (mainly to check for required fields // which do not have values entered) diff --git a/tests/Modules/AdminTest.php b/tests/Modules/AdminTest.php index 63887214..10b6fb68 100644 --- a/tests/Modules/AdminTest.php +++ b/tests/Modules/AdminTest.php @@ -78,6 +78,83 @@ public function testPermissionDefinesAdministrationCapabilities(): void { $this->assertSame('Access administrative console', $permissions['can_access_admin']['title']); } + /** + * Confirms the watchdog filter uses a GET Form API submission and reflects + * its URL-based filter values. + */ + public function testWatchdogFilterFormUsesGetValues(): void { + $getWasArray = is_array($_GET); + $originalGet = $getWasArray ? $_GET : NULL; + if (!$getWasArray) $_GET = array(); + try { + $_GET['type_filter'] = 'admin'; + $_GET['sev_filter'] = (string) WATCHDOG_ERROR; + $_GET['start_datetime'] = '2026-11-01T08:30'; + $_GET['end_datetime'] = '2026-11-02T17:15'; + + $form = fp_get_form('admin_watchdog_filter_form'); + + $this->assertSame('GET', $form['#submit_method']); + $this->assertSame('admin', $form['type_filter']['value']); + $this->assertSame((string) WATCHDOG_ERROR, $form['sev_filter']['value']); + $this->assertSame('datetime-local', $form['start_datetime']['type']); + $this->assertSame('2026-11-01T08:30', $form['start_datetime']['value']); + $this->assertSame('2026-11-02T17:15', $form['end_datetime']['value']); + $this->assertSame('submit', $form['apply_btn']['type']); + $this->assertSame('submit', $form['reset_btn']['type']); + } + finally { + if ($getWasArray) $_GET = $originalGet; else unset($_GET); + } + } + + /** + * Confirms watchdog filter submissions redirect to a canonical list URL. + */ + public function testWatchdogFilterFormSubmitBuildsFilterRedirect(): void { + $form = array(); + $formState = array( + 'values' => array( + 'apply_btn' => 'Apply', + 'reset_btn' => '', + 'type_filter' => 'admin', + 'sev_filter' => (string) WATCHDOG_ERROR, + 'start_datetime' => '2026-11-01T08:30', + 'end_datetime' => '2026-11-02T17:15', + ), + ); + + admin_watchdog_filter_form_submit($form, $formState); + + $this->assertSame( + array( + 'path' => 'admin/config/watchdog', + 'query' => 'type_filter=admin&sev_filter=' . WATCHDOG_ERROR . '&start_datetime=2026-11-01T08%3A30&end_datetime=2026-11-02T17%3A15', + ), + $form['#redirect'] + ); + + $formState['values']['apply_btn'] = ''; + $formState['values']['reset_btn'] = 'Reset'; + admin_watchdog_filter_form_submit($form, $formState); + + $this->assertSame('', $form['#redirect']['query']); + } + + /** + * Confirms watchdog date filter values must be valid local datetimes. + */ + public function testWatchdogDatetimeFilterValidation(): void { + $start = admin_watchdog_datetime_to_timestamp('2026-11-01T08:30'); + $end = admin_watchdog_datetime_to_timestamp('2026-11-01T09:30'); + + $this->assertIsInt($start); + $this->assertIsInt($end); + $this->assertGreaterThan($start, $end); + $this->assertFalse(admin_watchdog_datetime_to_timestamp('2026-11-01 08:30')); + $this->assertFalse(admin_watchdog_datetime_to_timestamp('not a datetime')); + } + /** * Confirms Admin's large-list difference helper retains duplicate source * values while excluding every value already present in the comparison list.