mirror of
https://github.com/postfixadmin/postfixadmin.git
synced 2024-09-19 11:12:15 +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:
parent
f3ce7e232d
commit
519c934ed3
@ -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()
|
||||
{
|
||||
|
@ -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.
|
||||
*/
|
||||
@ -219,6 +228,7 @@ class VacationHandler extends PFAHandler
|
||||
'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';
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
36
scripts/examples/vacation-cron.php
Normal file
36
scripts/examples/vacation-cron.php
Normal 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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user