Skip to content

Commit

Permalink
minor code style cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
onlime committed Feb 27, 2022
1 parent 8d1e29e commit 022f236
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 108 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# CHANGELOG

## 1.0.7 (UNRELEASED)
## 1.0.x (UNRELEASED)

## 1.0.7 (2022-02-27)

- Added config option `trottle.ignoreExceptions` to ignore exceptions while throttling, allowing fail safety if e.g. db server goes away.
- Added a limit to `explode()` to prevent the truncation of IPv6 addresses in the x-meta-client header. #7 by @skysky6
- Pushed minimum required PHP version to 7.3
- Minor code cleanup

## 1.0.6 (2020-07-24)

Expand Down
10 changes: 5 additions & 5 deletions app/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ConfigLoader
/**
* @var StdClass
*/
protected $_conf;
protected $conf;

/**
* Constructor
Expand All @@ -23,7 +23,7 @@ public function __construct()
$globalConfig = APP_ROOT . '/config.ini';
$extraConfigs = [
APP_ROOT . '/config.local.ini',
APP_ROOT . '/config.private.ini'
APP_ROOT . '/config.private.ini',
];

// load global config
Expand All @@ -39,7 +39,7 @@ public function __construct()
}
}

$this->_conf = $this->arrayToObject($config);
$this->conf = $this->arrayToObject($config);

$this->init();
}
Expand All @@ -51,7 +51,7 @@ public function init()
{
// assure the default timezone is set
if (!ini_get('date.timezone')) {
date_default_timezone_set($this->_conf->global->defaultTZ);
date_default_timezone_set($this->conf->global->defaultTZ);
}
}

Expand All @@ -62,7 +62,7 @@ public function init()
*/
public function getConfig()
{
return $this->_conf;
return $this->conf;
}

/**
Expand Down
62 changes: 22 additions & 40 deletions app/SendmailThrottle.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
require_once 'StdinMailParser.php';
require_once 'ConfigLoader.php';

/**
* Sendmail Wrapper by Onlime GmbH webhosting services
Expand All @@ -16,48 +15,31 @@ class SendmailThrottle extends StdinMailParser
const STATUS_BLOCKED = 3;
const STATUS_EXCEPTION = 4;

/**
* @var StdClass
*/
protected $_conf;

/**
* @var PDO
*/
protected $_pdo;

/**
* Constructor
*/
public function __construct()
{
// load configuration
$configLoader = new ConfigLoader();
$this->_conf = $configLoader->getConfig();

parent::__construct();
}
protected $pdo;

/**
* Destructor
* close the PDO database connection
*/
public function __destruct()
{
$this->_pdo = null;
$this->pdo = null;
}

/**
* Create PDO database connection
*
* @throws PDOException
*/
protected function _connect()
protected function connect()
{
$this->_pdo = new PDO(
$this->_conf->db->dsn,
$this->_conf->db->user,
$this->_conf->db->pass
$this->pdo = new PDO(
$this->conf->db->dsn,
$this->conf->db->user,
$this->conf->db->pass
);
}

Expand All @@ -77,13 +59,13 @@ public function run($username, $rcptCount)
{
try {
// connect to DB
$this->_connect();
$this->connect();

// default status code: success
$status = self::STATUS_OK;

$sql = 'SELECT * FROM throttle WHERE username = :username';
$stmt = $this->_pdo->prepare($sql);
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':username', $username);
$stmt->execute();
$throttle = $stmt->fetchObject();
Expand Down Expand Up @@ -114,7 +96,7 @@ public function run($username, $rcptCount)
$sql = 'UPDATE throttle SET updated_ts = NOW(), count_cur = :countCur, count_tot = :countTot,
rcpt_cur = :rcptCur, rcpt_tot = :rcptTot, status = :status
WHERE username = :username';
$stmt = $this->_pdo->prepare($sql);
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':countCur', $countCur, PDO::PARAM_INT);
$stmt->bindParam(':countTot', $countTot, PDO::PARAM_INT);
$stmt->bindParam(':rcptCur' , $rcptCur , PDO::PARAM_INT);
Expand All @@ -129,30 +111,30 @@ public function run($username, $rcptCount)
$status = self::STATUS_BLOCKED;
}
} else {
$countMax = $this->_conf->throttle->countMax;
$countMax = $this->conf->throttle->countMax;
$countCur = 1;
$countTot = 1;
$rcptMax = $this->_conf->throttle->rcptMax;
$rcptMax = $this->conf->throttle->rcptMax;
$rcptCur = $rcptCount;
$rcptTot = $rcptCount;

$sql = 'INSERT INTO throttle (updated_ts, username, count_max, rcpt_max, rcpt_cur, rcpt_tot)
VALUES (NOW(), :username, :countMax, :rcptMax, :rcptCur, :rcptTot)';
$stmt = $this->_pdo->prepare($sql);
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':countMax', $countMax, PDO::PARAM_INT);
$stmt->bindParam(':rcptMax' , $rcptMax , PDO::PARAM_INT);
$stmt->bindParam(':rcptCur' , $rcptCur , PDO::PARAM_INT);
$stmt->bindParam(':rcptTot' , $rcptTot , PDO::PARAM_INT);
$stmt->execute();
$id = $this->_pdo->lastInsertId();
$id = $this->pdo->lastInsertId();
}

// syslogging
$syslogMsg = sprintf('%s: user=%s (%s:%s), rcpts=%s, status=%s, command=%s, ' .
'count_max=%s, count_cur=%s, count_tot=%s, ' .
'rcpt_max=%s, rcpt_cur=%s, rcpt_tot=%s',
$this->_conf->throttle->syslogPrefix,
$this->conf->throttle->syslogPrefix,
$username,
$_SERVER['SUDO_UID'],
$_SERVER['SUDO_GID'],
Expand All @@ -177,19 +159,19 @@ public function run($username, $rcptCount)
// Do not report on status code 2, as the admin only wants to get notified once!
// Also, he is never interested in blocked accounts (status code 3).
mail(
$this->_conf->global->adminTo,
$this->_conf->throttle->adminSubject,
$this->conf->global->adminTo,
$this->conf->throttle->adminSubject,
$syslogMsg,
"From: " . $this->_conf->global->adminFrom
"From: " . $this->conf->global->adminFrom
);
}

// write all meta information to db messages log
$this->_logMessage($id, $username, $rcptCount, $status);
$this->logMessage($id, $username, $rcptCount, $status);

return $status;
} catch (PDOException $e) {
syslog(LOG_WARNING, sprintf('%s: PDOException: %s', $this->_conf->throttle->syslogPrefix, $e->getMessage()));
syslog(LOG_WARNING, sprintf('%s: PDOException: %s', $this->conf->throttle->syslogPrefix, $e->getMessage()));
return self::STATUS_EXCEPTION;
}
}
Expand All @@ -203,7 +185,7 @@ public function run($username, $rcptCount)
* @param int $rcptCount
* @param int $status
*/
protected function _logMessage($throttleId, $username, $rcptCount, $status)
protected function logMessage($throttleId, $username, $rcptCount, $status)
{
$headerArr = $this->getParsedHeaderArr();
$from = mb_decode_mimeheader($headerArr['from'] ?? null);
Expand All @@ -216,7 +198,7 @@ protected function _logMessage($throttleId, $username, $rcptCount, $status)
cc_addr, bcc_addr, subject, site, client, sender_host, script)
VALUES (:throttleId, :username, :uid, :gid, :rcptCount, :status, :msgid, :fromAddr, :toAddr,
:ccAddr, :bccAddr, :subject, :site, :client, SUBSTRING_INDEX(USER(), '@', -1), :script)";
$stmt = $this->_pdo->prepare($sql);
$stmt = $this->pdo->prepare($sql);
$stmt->bindParam(':throttleId', $throttleId);
$stmt->bindParam(':username' , $username);
$stmt->bindParam(':uid' , $_SERVER['SUDO_UID']);
Expand Down
42 changes: 12 additions & 30 deletions app/SendmailWrapper.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
require_once 'StdinMailParser.php';
require_once 'ConfigLoader.php';
require_once 'SendmailThrottle.php';

/**
Expand All @@ -11,23 +10,6 @@
*/
class SendmailWrapper extends StdinMailParser
{
/**
* @var StdClass
*/
protected $_conf;

/**
* Constructor
*/
public function __construct()
{
// load configuration
$configLoader = new ConfigLoader();
$this->_conf = $configLoader->getConfig();

parent::__construct();
}

/**
* Run sendmail wrapper
*
Expand All @@ -38,12 +20,12 @@ public function run()
$status = SendmailThrottle::STATUS_OK;

// get config variables
$sendmailCmd = $this->_conf->wrapper->sendmailCmd;
$throttleCmd = $this->_conf->wrapper->throttleCmd;
$throttleOn = (bool) $this->_conf->wrapper->throttleOn;
$xHeaderPrefix = $this->_conf->wrapper->xHeaderPrefix;
$defaultHost = $this->_conf->wrapper->defaultHost;
$ignoreExceptions = (bool) $this->_conf->throttle->ignoreExceptions;
$sendmailCmd = $this->conf->wrapper->sendmailCmd;
$throttleCmd = $this->conf->wrapper->throttleCmd;
$throttleOn = (bool) $this->conf->wrapper->throttleOn;
$xHeaderPrefix = $this->conf->wrapper->xHeaderPrefix;
$defaultHost = $this->conf->wrapper->defaultHost;
$ignoreExceptions = (bool) $this->conf->throttle->ignoreExceptions;

// generate an RFC-compliant Message-ID
// RFC 2822 (http://www.faqs.org/rfcs/rfc2822.html)
Expand Down Expand Up @@ -80,7 +62,7 @@ public function run()
'subject' => @$headerArr['subject'],
'site' => @$_SERVER["HTTP_HOST"],
'client' => @$_SERVER["REMOTE_ADDR"],
'script' => getenv('SCRIPT_FILENAME')
'script' => getenv('SCRIPT_FILENAME'),
];

// throttling
Expand Down Expand Up @@ -116,7 +98,7 @@ public function run()
// message logging to syslog
$syslogMsg = sprintf(
'%s: uid=%s, msgid=%s, from=%s, to="%s", cc="%s", bcc="%s", subject="%s", site=%s, client=%s, script=%s, throttleStatus=%s',
$this->_conf->wrapper->syslogPrefix,
$this->conf->wrapper->syslogPrefix,
$messageInfo['uid'],
$messageInfo['msgid'],
$messageInfo['from'],
Expand Down Expand Up @@ -153,9 +135,9 @@ public function run()
// Force adding envelope sender address (sendmail -r/-f parameters)
// For security reasons, we check if the Return-Path or From email addresses
// are valid, prior to passing them to -r.
if (preg_match('/^\-r/', $allArgs) || false !== strstr($allArgs, ' -r')) {
if (preg_match('/^-r/', $allArgs) || false !== strstr($allArgs, ' -r')) {
// -r parameter was found, no changes
} elseif (preg_match('/^\-f/', $allArgs) || false !== strstr($allArgs, ' -f')) {
} elseif (preg_match('/^-f/', $allArgs) || false !== strstr($allArgs, ' -f')) {
// -f parameter was found, no changes
} else {
// use Return-Path as -r parameter
Expand Down Expand Up @@ -199,11 +181,11 @@ public function stripStrLength($value, $limit = null, $suffix = null)
{
if ($limit === null) {
// load limit from configuration
$limit = $this->_conf->syslog->stringLengthLimit;
$limit = $this->conf->syslog->stringLengthLimit;
}
if ($suffix === null) {
// load suffix from configuration
$suffix = $this->_conf->syslog->stringCutSuffix;
$suffix = $this->conf->syslog->stringCutSuffix;
}

$strLen = mb_strlen($value);
Expand Down
Loading

0 comments on commit 022f236

Please sign in to comment.