Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Commit

Permalink
Added new option to delete old log entries.
Browse files Browse the repository at this point in the history
  • Loading branch information
lx-s committed Aug 13, 2020
1 parent 343684b commit dc1c7ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
5 changes: 4 additions & 1 deletion config.sample.php
Expand Up @@ -22,7 +22,10 @@
// see contents of translations directory
define('CCL_LANG', 'en');

define('ENTRY_LOG_DAYS', 3 * 7); // Last 3 weeks
// Last 3 weeks
define('ENTRY_LOG_DAYS', 3 * 7);
// When set to false, entries oder than ENTRY_LOG_DAYS are deleted
define('KEEP_OLD_ENTRIES', false);

/* If the user array has at least one entry, this site
can only be accessed by using proper credentials.
Expand Down
27 changes: 24 additions & 3 deletions index.php
Expand Up @@ -24,14 +24,34 @@ function add_entry($who)
if ($stmt->execute() === true) {
$added = true;
} else {
$errors[] = _tr('sql.error.insert').': <code>'.get_sql_error($db_).'</code>';
$errors[] = _tr('sql.error.insert').': <code>'.get_sql_error($stmt).'</code>';
}
$stmt->closeCursor();
}

return $added;
}

function delete_old_entries()
{
if (defined('KEEP_OLD_ENTRIES') && KEEP_OLD_ENTRIES === false) {
global $db_;
global $errors;
$sql = 'DELETE FROM '.DB_TABLE_PREFIX.'contact_log'
.' WHERE time < NOW() - INTERVAL :days DAY';
$stmt = $db_->prepare($sql);
if ($stmt === false) {
$errors[] = _tr('sql.error.prepare').': <code>'.get_sql_error($db_).'</code>';
} else {
$stmt->bindValue(':days', ENTRY_LOG_DAYS, \PDO::PARAM_INT);
if ($stmt->execute() === false) {
$errors[] = _tr('sql.error.query').': <code>'.get_sql_error($stmt).'</code>';
}
$stmt->closeCursor();
}
}
}

function get_entries($lastDays)
{
global $db_;
Expand All @@ -45,9 +65,9 @@ function get_entries($lastDays)
if ($stmt === false) {
$errors[] = _tr('sql.error.prepare').': <code>'.get_sql_error($db_).'</code>';
} else {
$stmt->bindValue(':days', $lastDays);
$stmt->bindValue(':days', $lastDays, \PDO::PARAM_INT);
if ($stmt->execute() === false || ($results = $stmt->fetchAll(\PDO::FETCH_ASSOC)) === false) {
$errors[] = _tr('sql.error.query').': <code>'.get_sql_error($db_).'</code>';
$errors[] = _tr('sql.error.query').': <code>'.get_sql_error($stmt).'</code>';
}
$stmt->closeCursor();
}
Expand All @@ -66,6 +86,7 @@ function get_entries($lastDays)
}
}

delete_old_entries();
$entries = get_entries(ENTRY_LOG_DAYS);

?><!doctype html>
Expand Down

0 comments on commit dc1c7ad

Please sign in to comment.