Skip to content

Commit

Permalink
#2, #3: Use session variable, create log
Browse files Browse the repository at this point in the history
Update the processing to simply use a session-variable to note that the
processing has run and create/update a log of the plugin's processing.
  • Loading branch information
lat9 committed Aug 7, 2017
1 parent d281dc1 commit f8e356e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
54 changes: 46 additions & 8 deletions YOUR_ADMIN/includes/init_includes/init_log_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// Part of the Log Manager plugin, created by lat9 (lat9@vinosdefrutastropicales.com)
// Copyright (c) 2017, Vinos de Frutas Tropicales.
//
// -----
// If the plugin's configuration settings (in Configuration->Logging) haven't been set, set them now!
//
if (!defined('LOG_MANAGER_KEEP_DAYS')) {
$next_sort = $db->Execute(
"SELECT MAX(sort_order) as max_sort
Expand All @@ -23,12 +26,20 @@
( 'Log Manager: Logs to Keep', 'LOG_MANAGER_KEEP_THESE', 'zcInstall', 'Enter a comma-separated list of name-prefixes for any log-files that you want to <b><i>keep</i></b>, regardless of their age.<br /><br />The values you enter are <em>case-sensitive</em>, i.e. <em>zcInstall</em> is different than <em>zcinstall</em>. The default setting (<code>zcInstall</code>) results in any file matching <code>/logs/zcInstall*.log</code> being kept regardless of its creation date.<br />', 10, $sort_order2, now(), NULL, NULL)"
);
} else {
// -----
// If we're not currently on the login page, but the "referring page" is the login page, continue to see if there are logs
// to be removed ... if so configured.
//
if ($current_page != 'login.php' && isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], zen_href_link(FILENAME_LOGIN, '', 'SSL')) === 0 && ((int)LOG_MANAGER_KEEP_DAYS) > 0) {
define('LOG_MANAGER_KEEP_DAYS', '0');
define('LOG_MANAGER_KEEP_THESE', 'zcInstall');
}

// -----
// If the plugin is enabled and current session is associated with a logged-in admin and the plugin's processing hasn't been run
// yet for this session ... manage those logs!
//
if (isset($_SESSION['admin_id'])) {
if (((int)LOG_MANAGER_KEEP_DAYS) > 0 && !isset($_SESSION['log_managed'])) {
// -----
// Build up a string-to-match (via preg_match) for the .log files that should be "kept". That string
// contains the log-file name prefixes.
//
$match_string = '';
if (LOG_MANAGER_KEEP_THESE != '') {
$logs_to_keep = explode(',', str_replace(' ', '', LOG_MANAGER_KEEP_THESE));
Expand All @@ -38,15 +49,31 @@
$match_string = '/^' . $logs_to_keep[0] . '.*$/';
}
}

// -----
// Determine the keep-until date (some number of days **prior to** today).
//
$keep_until = strtotime('-' . LOG_MANAGER_KEEP_DAYS . ' day');
$keep_until_date = date(DATE_FORMAT . ' H:m:i', $keep_until);

// -----
// Loop through all files present in the /logs directory ...
//
$files_removed = 0;
if ($dir = dir(DIR_FS_LOGS)) {
while (($current_file = $dir->read()) !== false) {
// -----
// ... looking for a ".log" file with a name that isn't in the "keep-these" list ...
//
if (strpos($current_file, '.log') !== false && $match_string != '' && !preg_match($match_string, $current_file)) {
// -----
// ... that was created prior to the keep-until date ...
//
$modified = filemtime(DIR_FS_LOGS . DIRECTORY_SEPARATOR . $current_file);
if ($modified !== false && $modified < $keep_until) {
// -----
// ... and remove it.
//
// I'm not a big fan of inhibiting error-reports, but for this case it's possible that multiple
// admins have "hit" the site concurrently and are all causing this script to run.
//
Expand All @@ -57,8 +84,19 @@
}
$dir->close();
}

// -----
// If one or more files was removed, let the admin know (via message) and log the removal action.
//
if ($files_removed != 0) {
$messageStack->add(sprintf(LOG_MANAGER_FILES_MESSAGE_FORMAT, $files_removed, $keep_until_date), 'success');
$logMessage = sprintf(LOG_MANAGER_FILES_MESSAGE_FORMAT, $files_removed, $keep_until_date);
$messageStack->add($logMessage, 'success');
error_log(date(PHP_DATE_TIME_FORMAT) . ', ' . $_SESSION['admin_id'] . ": $logMessage" . PHP_EOL, 3, DIR_FS_LOGS . '/log_manager_removal.log');
}
}
}

// -----
// Note that the plugin's processing has been run for the current admin session.
//
$_SESSION['log_managed'] = true;
}
10 changes: 7 additions & 3 deletions docs/log_manager/readme.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

<body>
<h1>Log Manager for Zen Cart 1.5.1 (and later)</h1>
<h3>Version 1.1.0</h3>
<h3>Version 1.2.0</h3>
<p>Current Support Thread at Zen Cart Forums: <a href="https://www.zen-cart.com/showthread.php?221939-Log-Manager-Support-Thread">https://www.zen-cart.com/showthread.php?221939-Log-Manager-Support-Thread</a></p>

<div class="tab-container" id="outer-container">
Expand All @@ -76,7 +76,7 @@ <h3>Version 1.1.0</h3>
<h2>What it does</h2>
<p>Logs are good things, providing information that allows an after-the-fact analysis if something goes wrong &hellip; but they can start filling up your store's <code class="no-pad">/logs</code> directory if you don't keep a watch on them!</p>
<p>The <em>Log Manager</em> enables you to control how long you keep those <code class="no-pad">.log</code> files, inspecting each file's last-modified date, and then automatically removing (i.e. deleting) any files that are past their &quot;expiration&quot;. There's also a setting to enable you to identify logs that you want to keep (e.g. <em>zcInstall</em> logs).</p>
<p>Once configured, <em>Log Manager</em> inspects your store's <code class="no-pad">/logs</code> directory, starting with v1.1.0, each time an admin successfully logs in to see if there are any candidates for removal &hellip; and, if found, removes them.</p>
<p>Once configured, <em>Log Manager</em> inspects your store's <code class="no-pad">/logs</code> directory, starting with v1.1.0, each time an admin successfully logs in to see if there are any candidates for removal &hellip; and, if found, removes them. Starting with v1.2.0, the plugin also creates and updates the file <code class="no-pad">/logs/log_manager_removal.log</code> to record its processing.</p>
</div>

<div id="installation">
Expand Down Expand Up @@ -140,8 +140,12 @@ <h2>What it does</h2>

<div id="changes">
<ul>
<li>v1.0.0, 2017-02-27, Initial release.</li>
<li>v1.2.0, 2017-08-07:<ul>
<li>CHANGE: Log removal check moved to a $_SESSION variable for the currently-logged-in admin.</li>
<li>CHANGE: Create/update /logs/log_manager_removal.log each time one or more files are removed.</li>
</ul></li>
<li>v1.1.0, 2017-06-28, Log removal occurs now upon successful admin login.</li>
<li>v1.0.0, 2017-02-27, Initial release.</li>
</ul>
</div>
</div>
Expand Down

0 comments on commit f8e356e

Please sign in to comment.