['label' => 'All', 'where' => ''], 'starred' => ['label' => 'Starred', 'icon' => 'fa-star', 'where' => 'certificates.certificate_favorite = 1'], 'soon' => ['label' => 'Expiring soon', 'icon' => 'fa-hourglass-half', 'where' => certificateSoonSql(), 'title' => 'In the next ' . CERTIFICATE_SOON_DAYS . " days - or 20 for a short-lived one, which should have renewed itself by then"], 'expired' => ['label' => 'Expired', 'icon' => 'fa-calendar-times', 'where' => 'certificates.certificate_expire < CURDATE()'], 'problems' => ['label' => 'Problems', 'icon' => 'fa-exclamation-triangle', 'where' => certificateProblemSql(), 'title' => "The last check found it serving one for another name, an untrusted or expired one - or nothing answered"], 'unchecked' => ['label' => 'Not checked', 'icon' => 'fa-eye-slash', 'where' => 'certificates.certificate_check_status IS NULL', 'title' => 'No host to check, or not checked yet'], ]; $show = isset($shows[$_GET['show'] ?? '']) ? ($_GET['show'] ?? '') : ''; $show_query = $shows[$show]['where'] ? 'AND ' . $shows[$show]['where'] : ''; $expire_days = $_GET['expire_days'] ?? ''; $expire_query = ''; if ($expire_days === 'expired') { $expire_query = 'AND certificates.certificate_expire < CURDATE()'; } elseif (intval($expire_days) > 0) { $expire_query = 'AND certificates.certificate_expire BETWEEN CURDATE() AND CURDATE() + INTERVAL ' . intval($expire_days) . ' DAY'; } // Only these sort - filter_header.php will hand through any lowercase word, and // an unknown column takes the page down with a mysqli exception $sortable_columns = ['certificate_name' => 'certificates.certificate_name', 'certificate_domain' => 'certificates.certificate_domain', 'certificate_issued_by' => 'certificates.certificate_issued_by', 'certificate_expire' => 'certificates.certificate_expire', 'certificate_checked_at' => 'certificates.certificate_checked_at', 'client_name' => 'client_name', 'certificate_created_at' => 'certificates.certificate_created_at']; if (!isset($sortable_columns[$sort])) { $sort = 'certificate_name'; } // Certificates without an expiry (or never checked) go last whichever way it is sorted $sort_sql = (in_array($sort, ['certificate_expire', 'certificate_checked_at'], true) ? "{$sortable_columns[$sort]} IS NULL, " : '') . $sortable_columns[$sort] . " $order"; [$certificate_columns, $certificate_joins] = certificateSelectSql(); $search_query = "AND (certificates.certificate_name LIKE '%$q%' OR certificates.certificate_domain LIKE '%$q%' OR certificates.certificate_description LIKE '%$q%' OR certificates.certificate_issued_by LIKE '%$q%' OR certificates.certificate_names LIKE '%$q%' OR certificates.certificate_fingerprint LIKE '%$q%' OR certificates.certificate_notes LIKE '%$q%' OR domains.domain_name LIKE '%$q%' OR client_name LIKE '%$q%')"; $scope_query = clientScopeSql('certificate_client_id'); $filters = "$archive_query $issuer_query $search_query $expire_query $scope_query $client_query"; $sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS $certificate_columns FROM certificates $certificate_joins WHERE $filters $show_query ORDER BY certificates.certificate_favorite DESC, $sort_sql, certificates.certificate_name ASC LIMIT $record_from, $record_to"); $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); $certificates = []; while ($row = mysqli_fetch_assoc($sql)) { $certificates[intval($row['certificate_id'])] = $row; } // How many of each, for the pills - everything in scope, before the pill itself $count_sql = []; foreach ($shows as $key => $s) { $count_sql[] = $key === '' ? 'COUNT(*)' : "COALESCE(SUM({$s['where']}), 0)"; } $row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT " . implode(', ', $count_sql) . " FROM certificates $certificate_joins WHERE $filters")); $counts = []; foreach (array_keys($shows) as $i => $key) { $counts[$key] = intval($row[$i]); } $filter_url = function ($changes) { $params = array_merge($_GET, $changes); unset($params['page']); return '?' . http_build_query(array_filter($params, function ($v) { return $v !== '' && $v !== null; })); }; // Issuers for the filter - the ones on a certificate in this scope $sql_issuers_filter = mysqli_query($mysqli, "SELECT certificates.certificate_issued_by, COUNT(*) AS issued FROM certificates $certificate_joins WHERE $archive_query $scope_query $client_query AND certificates.certificate_issued_by <> '' GROUP BY certificates.certificate_issued_by ORDER BY certificates.certificate_issued_by ASC"); // What the export starts from - the pill, as the export's own filters $export_extra = [ 'expire_days' => $show === 'expired' ? 'expired' : ($show === 'soon' ? CERTIFICATE_SOON_DAYS : ''), 'problems' => $show === 'problems' ? 1 : '', ]; ?>