0
0
mirror of https://github.com/postfixadmin/postfixadmin.git synced 2024-09-19 03:02:14 +02:00

Add minimal cron job to remove old vacation alias records - see https://github.com/postfixadmin/postfixadmin/issues/832

This commit is contained in:
David Goodwin 2024-05-31 19:25:53 +01:00
parent f3ce7e232d
commit 519c934ed3
No known key found for this signature in database
3 changed files with 55 additions and 8 deletions

View File

@ -38,7 +38,7 @@ function authentication_mfa_incomplete()
* check_session
* Action: Check if a session already exists, if not redirect to login.php
* Call: check_session ()
* @return String username (e.g. foo@example.com)
* @return string username (e.g. foo@example.com)
*/
function authentication_get_username()
{
@ -874,7 +874,7 @@ function encode_header($string, $default_charset = "utf-8")
}
}
break;
# end switch
# end switch
}
}
if ($enc_init) {

View File

@ -147,13 +147,22 @@ class VacationHandler extends PFAHandler
$vacation_data = array(
'active' => db_get_boolean(false),
);
$result = db_update('vacation', 'email', $this->username, $vacation_data);
$result = db_delete('vacation_notification', 'on_vacation', $this->username);
// check for error?
$this->removeVacationNotifications();
# TODO db_log() call (maybe except if called from set_away?)
/* crap error handling; oh for exceptions... */
return true;
}
private function removeVacationNotifications()
{
$result = db_delete('vacation_notification', 'on_vacation', $this->username);
}
/**
* @return boolean true indicates this server supports vacation messages, and users are able to change their own.
*/
@ -213,12 +222,13 @@ class VacationHandler extends PFAHandler
return array(
'subject' => isset($row['subject']) ? $row['subject'] : null,
'body' => isset($row['body']) ? $row['body'] : null,
'active' => $boolean,
'active' => $boolean,
'interval_time' => isset($row['interval_time']) ? $row['interval_time'] : null,
'activeFrom' => isset($row['activefrom']) ? $row['activefrom'] : null,
'activeUntil' => isset($row['activeuntil']) ? $row['activeuntil'] : null
);
}
/**
* @param string $subject
* @param string $body
@ -229,7 +239,7 @@ class VacationHandler extends PFAHandler
*/
public function set_away($subject, $body, $interval_time, $activeFrom, $activeUntil)
{
$this->remove(); // clean out any notifications that might already have been sent.
$this->removeVacationNotifications(); // clean out any notifications that might already have been sent.
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $activeFrom)) {
$activeFrom .= ' 00:00:00';
@ -253,7 +263,7 @@ class VacationHandler extends PFAHandler
'activeuntil' => $activeUntil,
);
if (! db_pgsql()) {
if (!db_pgsql()) {
$vacation_data['cache'] = ''; # leftover from 2.2
}
@ -281,6 +291,7 @@ class VacationHandler extends PFAHandler
$handler = new AliasHandler();
if (!$handler->init($this->id)) {
error_log("failed to initialise AliashHandler for {$this->id} : " . json_encode($handler->errormsg));
# print_r($handler->errormsg); # TODO: error handling
return false;
}
@ -290,14 +301,14 @@ class VacationHandler extends PFAHandler
);
if (!$handler->set($values)) {
# print_r($handler->errormsg); # TODO: error handling
error_log("Failed to set vacation with values " . json_encode($values) . " - error: " . json_encode($handler->errormsg)); # TODO: error handling
return false;
}
# TODO: supress logging in AliasHandler if called from VacationHandler (VacationHandler should log itsself)
if (!$handler->save()) {
print_r($handler->errormsg); # TODO: error handling
error_log("Failed to save vacation record with values " . json_Encode($values) . " - error : " . json_encode($handler->errormsg)); # TODO: error handling
return false;
}

View File

@ -0,0 +1,36 @@
#!/bin/env php
<?php
/**
* This script is intended to be run through cron.
* It should look through the postfixadmin vacation table for vacation entries that should no longer be active. If it
* finds one that's expired, it should update the alias record to remove the autoreply alias, as well as deactivating the vacation entry.
* @see https://github.com/postfixadmin/postfixadmin/issues/832
*
* How you link it into your cron routine is up to you - you 'could' have an entry in /etc/cron.hourly/postfixadmin that contains something like :
*
* #!/bin/bash
* cd /path/to/postfixadmin/scripts/examples
* php vacation-cron.php
*
*
* might work.
*/
require_once(__DIR__ . '/../../public/common.php');
define('POSTFIXADMIN_CLI', 1);
$table_vacation = table_by_key('vacation');
$vacations_that_need_deactivating = db_query_all("SELECT * FROM $table_vacation WHERE activeuntil <= NOW() AND active = 1 ");
foreach ($vacations_that_need_deactivating as $row) {
try {
$vh = new VacationHandler($row['email']);
error_log(__FILE__ . " - I need to disable the postfixadmin vacation stuff for : {$row['email']} as it should end at {$row['activeuntil']}");
$vh->remove();
} catch (\Exception $e) {
error_log(__FILE__ . " - failed to remove postfixadmin vacation settings for user." . $e->getMessage());
}
}