Edit File: log_output.php
<?php // log_output.php // This file expects GET or POST parameters: q (search query) and page (page number) $logFile = '/usr/local/cptools/logs/whm_cptools_activity.log'; // Ensure the log directory exists $logDir = dirname($logFile); if (!is_dir($logDir)) { if (!mkdir($logDir, 0755, true)) { echo "<p>Failed to create log directory.</p>"; exit; } } // Ensure the log file exists; if not, create an empty file. if (!file_exists($logFile)) { if (false === file_put_contents($logFile, "")) { echo "<p>Log file not found and could not be created.</p>"; exit; } } // Read the log file and parse each line $lines = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $parsedLogs = []; foreach ($lines as $line) { // Expected format: [YYYY-MM-DD HH:MM:SS] ==> domain ==> action ==> user $parts = explode("==>", $line); if (count($parts) === 4) { $timestamp = trim($parts[0], " []"); $domain = trim($parts[1]); $action = trim($parts[2]); $logUser = trim($parts[3]); $timeUnix = strtotime($timestamp); $parsedLogs[] = [ 'timestamp' => $timestamp, 'timeUnix' => $timeUnix, 'domain' => $domain, 'action' => $action, 'logUser' => $logUser ]; } } // Sort logs descending by timestamp $timestamps = array_column($parsedLogs, 'timeUnix'); array_multisort($timestamps, SORT_DESC, $parsedLogs); // Limit to only the 100 latest records for display $parsedLogs = array_slice($parsedLogs, 0, 100); // Apply search filtering if a query is provided (search within these 100 records) $searchQuery = isset($_REQUEST['q']) ? trim($_REQUEST['q']) : ''; if ($searchQuery !== '') { $parsedLogs = array_filter($parsedLogs, function($log) use ($searchQuery) { return (stripos($log['domain'], $searchQuery) !== false || stripos($log['action'], $searchQuery) !== false || stripos($log['logUser'], $searchQuery) !== false); }); } // For non-root users, filter logs so they see only their own records (if REMOTE_USER is set) $currentUser = getenv("REMOTE_USER") ?: 'Unknown User'; if ($currentUser !== "root") { $parsedLogs = array_filter($parsedLogs, function($log) use ($currentUser) { return $log['logUser'] === $currentUser; }); } $noRecordsFound = empty($parsedLogs); // Set pagination parameters (for example, 10 records per page) $perPage = 10; $totalRecords = count($parsedLogs); $totalPages = $totalRecords > 0 ? ceil($totalRecords / $perPage) : 1; $page = isset($_REQUEST['page']) && is_numeric($_REQUEST['page']) ? intval($_REQUEST['page']) : 1; if ($page < 1) { $page = 1; } if ($page > $totalPages) { $page = $totalPages; } $paginatedLogs = $totalRecords > 0 ? array_slice($parsedLogs, ($page - 1) * $perPage, $perPage) : []; if ($noRecordsFound) { echo '<p style="margin-top:20px;color:red; font-size:20px;font-weight:bolder;">No activity logs available.</p>'; } else { foreach ($paginatedLogs as $log) { echo '<div class="log-entry">'; echo '[' . htmlspecialchars($log['timestamp']) . '] : ' ; if ($currentUser === "root") { echo ' <em><b>' . htmlspecialchars($log['logUser']) . '</b></em> '; } echo htmlspecialchars($log['action']) . ' ' . htmlspecialchars($log['domain']); echo '</div>'; } // Generate pagination links if more than one page exists if ($totalRecords > 0 && $totalPages > 1) { echo '<div class="pagination">'; for ($i = 1; $i <= $totalPages; $i++) { $activeClass = ($i == $page) ? "active" : ""; echo "<a class='$activeClass' href='javascript:void(0)' onclick='loadLogs($i)'>$i</a>"; } echo '</div>'; } } ?> <script> function loadLogs(page) { var query = $("#searchInput").val(); $("#logOutput").load("log_output_proxy.php", { q: query, page: page }); } </script>