Skip to content

Commit

Permalink
#5: Deferring processing until admin's logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
lat9 committed Oct 18, 2023
1 parent f8e356e commit 798a60d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 58 deletions.
6 changes: 3 additions & 3 deletions YOUR_ADMIN/includes/auto_loaders/config.log_manager.php
@@ -1,9 +1,9 @@
<?php
// -----
// Part of the Log Manager plugin, created by lat9 (lat9@vinosdefrutastropicales.com)
// Copyright (c) 2017, Vinos de Frutas Tropicales.
// Copyright (c) 2017-2023, Vinos de Frutas Tropicales.
//
$autoLoadConfig[200][] = array (
$autoLoadConfig[200][] = [
'autoType' => 'init_script',
'loadFile' => 'init_log_manager.php'
);
];
66 changes: 31 additions & 35 deletions YOUR_ADMIN/includes/init_includes/init_log_manager.php
@@ -1,61 +1,57 @@
<?php
// -----
// Part of the Log Manager plugin, created by lat9 (lat9@vinosdefrutastropicales.com)
// Copyright (c) 2017, Vinos de Frutas Tropicales.
// Copyright (c) 2017-2023, 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
FROM " . TABLE_CONFIGURATION . "
WHERE configuration_group_id = 10",
false,
false,
0,
true
);
$sort_order = $next_sort->fields['max_sort'] + 1;
$sort_order2 = $sort_order + 1;
$db->Execute(
"INSERT INTO " . TABLE_CONFIGURATION . "
( configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added, use_function, set_function )
VALUES
( 'Log Manager: Days to Keep', 'LOG_MANAGER_KEEP_DAYS', '0', 'Enter the maximum number of <em>days</em> to keep any file with a <code>.log</code> file extension in your store\'s <b>logs</b> directory.<br /><br />If the value you enter is non-zero, then any files created prior to that relative date will be <b>permanently removed</b> from your store\'s file-system.<br />', 10, $sort_order, now(), NULL, NULL),
( '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)"
);
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'])) {
if (isset($_SESSION['admin_id']) && !isset($_SESSION['log_managed'])) {
// -----
// 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->ExecuteNoCache(
"SELECT MAX(sort_order) as max_sort
FROM " . TABLE_CONFIGURATION . "
WHERE configuration_group_id = 10",
);
$sort_order = $next_sort->fields['max_sort'] + 1;
$sort_order2 = $sort_order + 1;
$db->Execute(
"INSERT INTO " . TABLE_CONFIGURATION . "
(configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added, use_function, set_function)
VALUES
('Log Manager: Days to Keep', 'LOG_MANAGER_KEEP_DAYS', '0', 'Enter the maximum number of <em>days</em> to keep any file with a <code>.log</code> file extension in your store\'s <b>logs</b> directory.<br><br>If the value you enter is non-zero, then any files created prior to that relative date will be <b>permanently removed</b> from your store\'s file-system.<br>', 10, $sort_order, now(), NULL, NULL),
('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)"
);
define('LOG_MANAGER_KEEP_DAYS', '0');
define('LOG_MANAGER_KEEP_THESE', 'zcInstall');
}

if (((int)LOG_MANAGER_KEEP_DAYS) > 0) {
// -----
// 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 != '') {
if (LOG_MANAGER_KEEP_THESE !== '') {
$logs_to_keep = explode(',', str_replace(' ', '', LOG_MANAGER_KEEP_THESE));
if (count($logs_to_keep) > 1) {
$match_string = '/^(' . implode('|', $logs_to_keep) . ').*$/';
} else {
$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 ...
//
Expand Down Expand Up @@ -84,7 +80,7 @@
}
$dir->close();
}

// -----
// If one or more files was removed, let the admin know (via message) and log the removal action.
//
Expand All @@ -94,7 +90,7 @@
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.
//
Expand Down
41 changes: 21 additions & 20 deletions docs/log_manager/readme.html
@@ -1,14 +1,13 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="../vinos_favicon.ico" type="image/x-icon" />
<link href="http://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<title>Zen Cart Log Manager</title>

<style type="text/css">
<!--
<style>
a, a:active, a:visited { color: #006666; text-decoration: none; }
a:hover { color:#00CC99; text-decoration: underline; }
BODY, P, H1, H2, H3, H4, H5, H6, LI, TD, DD, DT, pre, code, img, table, #outer-container {
Expand Down Expand Up @@ -48,21 +47,20 @@
.tab a.active { font-weight: bold; }
.tab-container .panel-container { background: #fff; border: solid #666 1px; padding: 10px; border-radius: 0 4px 4px 4px; }
.panel-container { margin-bottom: 10px; }
-->
</style>
<link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" />
<script src="../jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="../jquery.easytabs.min.js" type="text/javascript"></script>
<script type="text/javascript">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" />
<script src="../jquery-1.7.1.min.js"></script>
<script src="../jquery.easytabs.min.js"></script>
<script>
$(document).ready( function() {
$('#tab-container').easytabs();
});
</script>
</head>

<body>
<h1>Log Manager for Zen Cart 1.5.1 (and later)</h1>
<h3>Version 1.2.0</h3>
<h1>Log Manager for Zen Cart 1.5.7 (and later)</h1>
<h3>Version 1.3.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 @@ -74,9 +72,9 @@ <h3>Version 1.2.0</h3>
<div class="panel-container">
<div id="overview">
<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. 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>
<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 <var>/logs</var> 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 <var>.log</var> 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 <var>/logs</var> 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 <var>/logs/log_manager_removal.log</var> to record its processing.</p>
</div>

<div id="installation">
Expand All @@ -89,7 +87,7 @@ <h2>What it does</h2>
</ul>
<div class="panel-container">
<div id="install">
<p>There are <b>no</b> core-file overwrites required by <em>Log Manager</em>. Rename the <code class="no-pad">YOUR_ADMIN</code> folder to match your Zen Cart admin's folder name and then copy the plugin's files to your store's file-system:</p>
<p>There are <b>no</b> core-file overwrites required by <em>Log Manager</em>. Rename the <var>YOUR_ADMIN</var> folder to match your Zen Cart admin's folder name and then copy the plugin's files to your store's file-system:</p>
<ol>
<li>/YOUR_ADMIN/includes/auto_loaders/config.log_manager.php</li>
<li>/YOUR_ADMIN/includes/init_includes/init_log_manager.php</li>
Expand All @@ -107,13 +105,13 @@ <h2>What it does</h2>
</tr>
<tr>
<td>Log Manager: Files to Keep</td>
<td>Identify the <b><i>case-sensitive</i></b> starting characters of any log-files that you want to <b>keep</b>, regardless of their age, using a comma-separated list. The default (<code class="no-pad">zcInstall</code>) keeps any files that match <code class="no-pad">/logs/zcInstall*.log</code>.</td>
<td>Identify the <b><i>case-sensitive</i></b> starting characters of any log-files that you want to <b>keep</b>, regardless of their age, using a comma-separated list. The default (<var>zcInstall</var>) keeps any files that match <var>/logs/zcInstall*.log</var>.</td>
</tr>
</table>
</div>

<div id="remove">
<p>First, delete the files that were copied to your zen-cart installation's admin directories. Restore your backup up of then use your admin's <em>Tools-&gt;Run SQL Patches</em> to run the file <code class="no-pad">/docs/log_manager/uninstall/log_manager_uninstall.sql</code>.</p>
<p>First, delete the files that were copied to your zen-cart installation's admin directories. Restore your backup up of then use your admin's <em>Tools-&gt;Run SQL Patches</em> to run the file <var>/docs/log_manager/uninstall/log_manager_uninstall.sql</var>.</p>
</div>

</div>
Expand All @@ -140,6 +138,9 @@ <h2>What it does</h2>

<div id="changes">
<ul>
<li>v1.3.0, 2023-10-18:<ul>
<li>CHANGE: Defer all processing until an admin has logged in.</li>
</ul></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>
Expand All @@ -154,7 +155,7 @@ <h2>What it does</h2>
</div>
</div>

<script type="text/javascript">
<script>
$('#outer-container, #inner-container2, #inner-container3').easytabs();

$( '.side-link' ).click( function() {
Expand Down

0 comments on commit 798a60d

Please sign in to comment.