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
|
* check_session
|
||||||
* Action: Check if a session already exists, if not redirect to login.php
|
* Action: Check if a session already exists, if not redirect to login.php
|
||||||
* Call: check_session ()
|
* Call: check_session ()
|
||||||
* @return String username (e.g. foo@example.com)
|
* @return string username (e.g. foo@example.com)
|
||||||
*/
|
*/
|
||||||
function authentication_get_username()
|
function authentication_get_username()
|
||||||
{
|
{
|
||||||
@ -874,7 +874,7 @@ function encode_header($string, $default_charset = "utf-8")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
# end switch
|
# end switch
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($enc_init) {
|
if ($enc_init) {
|
||||||
|
@ -147,13 +147,22 @@ class VacationHandler extends PFAHandler
|
|||||||
$vacation_data = array(
|
$vacation_data = array(
|
||||||
'active' => db_get_boolean(false),
|
'active' => db_get_boolean(false),
|
||||||
);
|
);
|
||||||
|
|
||||||
$result = db_update('vacation', 'email', $this->username, $vacation_data);
|
$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?)
|
# TODO db_log() call (maybe except if called from set_away?)
|
||||||
/* crap error handling; oh for exceptions... */
|
/* crap error handling; oh for exceptions... */
|
||||||
return true;
|
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.
|
* @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(
|
return array(
|
||||||
'subject' => isset($row['subject']) ? $row['subject'] : null,
|
'subject' => isset($row['subject']) ? $row['subject'] : null,
|
||||||
'body' => isset($row['body']) ? $row['body'] : null,
|
'body' => isset($row['body']) ? $row['body'] : null,
|
||||||
'active' => $boolean,
|
'active' => $boolean,
|
||||||
'interval_time' => isset($row['interval_time']) ? $row['interval_time'] : null,
|
'interval_time' => isset($row['interval_time']) ? $row['interval_time'] : null,
|
||||||
'activeFrom' => isset($row['activefrom']) ? $row['activefrom'] : null,
|
'activeFrom' => isset($row['activefrom']) ? $row['activefrom'] : null,
|
||||||
'activeUntil' => isset($row['activeuntil']) ? $row['activeuntil'] : null
|
'activeUntil' => isset($row['activeuntil']) ? $row['activeuntil'] : null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $subject
|
* @param string $subject
|
||||||
* @param string $body
|
* @param string $body
|
||||||
@ -229,7 +239,7 @@ class VacationHandler extends PFAHandler
|
|||||||
*/
|
*/
|
||||||
public function set_away($subject, $body, $interval_time, $activeFrom, $activeUntil)
|
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)) {
|
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $activeFrom)) {
|
||||||
$activeFrom .= ' 00:00:00';
|
$activeFrom .= ' 00:00:00';
|
||||||
@ -253,7 +263,7 @@ class VacationHandler extends PFAHandler
|
|||||||
'activeuntil' => $activeUntil,
|
'activeuntil' => $activeUntil,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (! db_pgsql()) {
|
if (!db_pgsql()) {
|
||||||
$vacation_data['cache'] = ''; # leftover from 2.2
|
$vacation_data['cache'] = ''; # leftover from 2.2
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -281,6 +291,7 @@ class VacationHandler extends PFAHandler
|
|||||||
$handler = new AliasHandler();
|
$handler = new AliasHandler();
|
||||||
|
|
||||||
if (!$handler->init($this->id)) {
|
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
|
# print_r($handler->errormsg); # TODO: error handling
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -290,14 +301,14 @@ class VacationHandler extends PFAHandler
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (!$handler->set($values)) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
# TODO: supress logging in AliasHandler if called from VacationHandler (VacationHandler should log itsself)
|
# TODO: supress logging in AliasHandler if called from VacationHandler (VacationHandler should log itsself)
|
||||||
|
|
||||||
if (!$handler->save()) {
|
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;
|
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