Skip to content

Commit 022f236

Browse files
committed
minor code style cleanup
1 parent 8d1e29e commit 022f236

File tree

8 files changed

+72
-108
lines changed

8 files changed

+72
-108
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# CHANGELOG
22

3-
## 1.0.7 (UNRELEASED)
3+
## 1.0.x (UNRELEASED)
4+
5+
## 1.0.7 (2022-02-27)
46

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

712
## 1.0.6 (2020-07-24)
813

app/ConfigLoader.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ConfigLoader
1313
/**
1414
* @var StdClass
1515
*/
16-
protected $_conf;
16+
protected $conf;
1717

1818
/**
1919
* Constructor
@@ -23,7 +23,7 @@ public function __construct()
2323
$globalConfig = APP_ROOT . '/config.ini';
2424
$extraConfigs = [
2525
APP_ROOT . '/config.local.ini',
26-
APP_ROOT . '/config.private.ini'
26+
APP_ROOT . '/config.private.ini',
2727
];
2828

2929
// load global config
@@ -39,7 +39,7 @@ public function __construct()
3939
}
4040
}
4141

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

4444
$this->init();
4545
}
@@ -51,7 +51,7 @@ public function init()
5151
{
5252
// assure the default timezone is set
5353
if (!ini_get('date.timezone')) {
54-
date_default_timezone_set($this->_conf->global->defaultTZ);
54+
date_default_timezone_set($this->conf->global->defaultTZ);
5555
}
5656
}
5757

@@ -62,7 +62,7 @@ public function init()
6262
*/
6363
public function getConfig()
6464
{
65-
return $this->_conf;
65+
return $this->conf;
6666
}
6767

6868
/**

app/SendmailThrottle.php

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
require_once 'StdinMailParser.php';
3-
require_once 'ConfigLoader.php';
43

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

19-
/**
20-
* @var StdClass
21-
*/
22-
protected $_conf;
23-
2418
/**
2519
* @var PDO
2620
*/
27-
protected $_pdo;
28-
29-
/**
30-
* Constructor
31-
*/
32-
public function __construct()
33-
{
34-
// load configuration
35-
$configLoader = new ConfigLoader();
36-
$this->_conf = $configLoader->getConfig();
37-
38-
parent::__construct();
39-
}
21+
protected $pdo;
4022

4123
/**
4224
* Destructor
4325
* close the PDO database connection
4426
*/
4527
public function __destruct()
4628
{
47-
$this->_pdo = null;
29+
$this->pdo = null;
4830
}
4931

5032
/**
5133
* Create PDO database connection
5234
*
5335
* @throws PDOException
5436
*/
55-
protected function _connect()
37+
protected function connect()
5638
{
57-
$this->_pdo = new PDO(
58-
$this->_conf->db->dsn,
59-
$this->_conf->db->user,
60-
$this->_conf->db->pass
39+
$this->pdo = new PDO(
40+
$this->conf->db->dsn,
41+
$this->conf->db->user,
42+
$this->conf->db->pass
6143
);
6244
}
6345

@@ -77,13 +59,13 @@ public function run($username, $rcptCount)
7759
{
7860
try {
7961
// connect to DB
80-
$this->_connect();
62+
$this->connect();
8163

8264
// default status code: success
8365
$status = self::STATUS_OK;
8466

8567
$sql = 'SELECT * FROM throttle WHERE username = :username';
86-
$stmt = $this->_pdo->prepare($sql);
68+
$stmt = $this->pdo->prepare($sql);
8769
$stmt->bindParam(':username', $username);
8870
$stmt->execute();
8971
$throttle = $stmt->fetchObject();
@@ -114,7 +96,7 @@ public function run($username, $rcptCount)
11496
$sql = 'UPDATE throttle SET updated_ts = NOW(), count_cur = :countCur, count_tot = :countTot,
11597
rcpt_cur = :rcptCur, rcpt_tot = :rcptTot, status = :status
11698
WHERE username = :username';
117-
$stmt = $this->_pdo->prepare($sql);
99+
$stmt = $this->pdo->prepare($sql);
118100
$stmt->bindParam(':countCur', $countCur, PDO::PARAM_INT);
119101
$stmt->bindParam(':countTot', $countTot, PDO::PARAM_INT);
120102
$stmt->bindParam(':rcptCur' , $rcptCur , PDO::PARAM_INT);
@@ -129,30 +111,30 @@ public function run($username, $rcptCount)
129111
$status = self::STATUS_BLOCKED;
130112
}
131113
} else {
132-
$countMax = $this->_conf->throttle->countMax;
114+
$countMax = $this->conf->throttle->countMax;
133115
$countCur = 1;
134116
$countTot = 1;
135-
$rcptMax = $this->_conf->throttle->rcptMax;
117+
$rcptMax = $this->conf->throttle->rcptMax;
136118
$rcptCur = $rcptCount;
137119
$rcptTot = $rcptCount;
138120

139121
$sql = 'INSERT INTO throttle (updated_ts, username, count_max, rcpt_max, rcpt_cur, rcpt_tot)
140122
VALUES (NOW(), :username, :countMax, :rcptMax, :rcptCur, :rcptTot)';
141-
$stmt = $this->_pdo->prepare($sql);
123+
$stmt = $this->pdo->prepare($sql);
142124
$stmt->bindParam(':username', $username);
143125
$stmt->bindParam(':countMax', $countMax, PDO::PARAM_INT);
144126
$stmt->bindParam(':rcptMax' , $rcptMax , PDO::PARAM_INT);
145127
$stmt->bindParam(':rcptCur' , $rcptCur , PDO::PARAM_INT);
146128
$stmt->bindParam(':rcptTot' , $rcptTot , PDO::PARAM_INT);
147129
$stmt->execute();
148-
$id = $this->_pdo->lastInsertId();
130+
$id = $this->pdo->lastInsertId();
149131
}
150132

151133
// syslogging
152134
$syslogMsg = sprintf('%s: user=%s (%s:%s), rcpts=%s, status=%s, command=%s, ' .
153135
'count_max=%s, count_cur=%s, count_tot=%s, ' .
154136
'rcpt_max=%s, rcpt_cur=%s, rcpt_tot=%s',
155-
$this->_conf->throttle->syslogPrefix,
137+
$this->conf->throttle->syslogPrefix,
156138
$username,
157139
$_SERVER['SUDO_UID'],
158140
$_SERVER['SUDO_GID'],
@@ -177,19 +159,19 @@ public function run($username, $rcptCount)
177159
// Do not report on status code 2, as the admin only wants to get notified once!
178160
// Also, he is never interested in blocked accounts (status code 3).
179161
mail(
180-
$this->_conf->global->adminTo,
181-
$this->_conf->throttle->adminSubject,
162+
$this->conf->global->adminTo,
163+
$this->conf->throttle->adminSubject,
182164
$syslogMsg,
183-
"From: " . $this->_conf->global->adminFrom
165+
"From: " . $this->conf->global->adminFrom
184166
);
185167
}
186168

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

190172
return $status;
191173
} catch (PDOException $e) {
192-
syslog(LOG_WARNING, sprintf('%s: PDOException: %s', $this->_conf->throttle->syslogPrefix, $e->getMessage()));
174+
syslog(LOG_WARNING, sprintf('%s: PDOException: %s', $this->conf->throttle->syslogPrefix, $e->getMessage()));
193175
return self::STATUS_EXCEPTION;
194176
}
195177
}
@@ -203,7 +185,7 @@ public function run($username, $rcptCount)
203185
* @param int $rcptCount
204186
* @param int $status
205187
*/
206-
protected function _logMessage($throttleId, $username, $rcptCount, $status)
188+
protected function logMessage($throttleId, $username, $rcptCount, $status)
207189
{
208190
$headerArr = $this->getParsedHeaderArr();
209191
$from = mb_decode_mimeheader($headerArr['from'] ?? null);
@@ -216,7 +198,7 @@ protected function _logMessage($throttleId, $username, $rcptCount, $status)
216198
cc_addr, bcc_addr, subject, site, client, sender_host, script)
217199
VALUES (:throttleId, :username, :uid, :gid, :rcptCount, :status, :msgid, :fromAddr, :toAddr,
218200
:ccAddr, :bccAddr, :subject, :site, :client, SUBSTRING_INDEX(USER(), '@', -1), :script)";
219-
$stmt = $this->_pdo->prepare($sql);
201+
$stmt = $this->pdo->prepare($sql);
220202
$stmt->bindParam(':throttleId', $throttleId);
221203
$stmt->bindParam(':username' , $username);
222204
$stmt->bindParam(':uid' , $_SERVER['SUDO_UID']);

app/SendmailWrapper.php

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
require_once 'StdinMailParser.php';
3-
require_once 'ConfigLoader.php';
43
require_once 'SendmailThrottle.php';
54

65
/**
@@ -11,23 +10,6 @@
1110
*/
1211
class SendmailWrapper extends StdinMailParser
1312
{
14-
/**
15-
* @var StdClass
16-
*/
17-
protected $_conf;
18-
19-
/**
20-
* Constructor
21-
*/
22-
public function __construct()
23-
{
24-
// load configuration
25-
$configLoader = new ConfigLoader();
26-
$this->_conf = $configLoader->getConfig();
27-
28-
parent::__construct();
29-
}
30-
3113
/**
3214
* Run sendmail wrapper
3315
*
@@ -38,12 +20,12 @@ public function run()
3820
$status = SendmailThrottle::STATUS_OK;
3921

4022
// get config variables
41-
$sendmailCmd = $this->_conf->wrapper->sendmailCmd;
42-
$throttleCmd = $this->_conf->wrapper->throttleCmd;
43-
$throttleOn = (bool) $this->_conf->wrapper->throttleOn;
44-
$xHeaderPrefix = $this->_conf->wrapper->xHeaderPrefix;
45-
$defaultHost = $this->_conf->wrapper->defaultHost;
46-
$ignoreExceptions = (bool) $this->_conf->throttle->ignoreExceptions;
23+
$sendmailCmd = $this->conf->wrapper->sendmailCmd;
24+
$throttleCmd = $this->conf->wrapper->throttleCmd;
25+
$throttleOn = (bool) $this->conf->wrapper->throttleOn;
26+
$xHeaderPrefix = $this->conf->wrapper->xHeaderPrefix;
27+
$defaultHost = $this->conf->wrapper->defaultHost;
28+
$ignoreExceptions = (bool) $this->conf->throttle->ignoreExceptions;
4729

4830
// generate an RFC-compliant Message-ID
4931
// RFC 2822 (http://www.faqs.org/rfcs/rfc2822.html)
@@ -80,7 +62,7 @@ public function run()
8062
'subject' => @$headerArr['subject'],
8163
'site' => @$_SERVER["HTTP_HOST"],
8264
'client' => @$_SERVER["REMOTE_ADDR"],
83-
'script' => getenv('SCRIPT_FILENAME')
65+
'script' => getenv('SCRIPT_FILENAME'),
8466
];
8567

8668
// throttling
@@ -116,7 +98,7 @@ public function run()
11698
// message logging to syslog
11799
$syslogMsg = sprintf(
118100
'%s: uid=%s, msgid=%s, from=%s, to="%s", cc="%s", bcc="%s", subject="%s", site=%s, client=%s, script=%s, throttleStatus=%s',
119-
$this->_conf->wrapper->syslogPrefix,
101+
$this->conf->wrapper->syslogPrefix,
120102
$messageInfo['uid'],
121103
$messageInfo['msgid'],
122104
$messageInfo['from'],
@@ -153,9 +135,9 @@ public function run()
153135
// Force adding envelope sender address (sendmail -r/-f parameters)
154136
// For security reasons, we check if the Return-Path or From email addresses
155137
// are valid, prior to passing them to -r.
156-
if (preg_match('/^\-r/', $allArgs) || false !== strstr($allArgs, ' -r')) {
138+
if (preg_match('/^-r/', $allArgs) || false !== strstr($allArgs, ' -r')) {
157139
// -r parameter was found, no changes
158-
} elseif (preg_match('/^\-f/', $allArgs) || false !== strstr($allArgs, ' -f')) {
140+
} elseif (preg_match('/^-f/', $allArgs) || false !== strstr($allArgs, ' -f')) {
159141
// -f parameter was found, no changes
160142
} else {
161143
// use Return-Path as -r parameter
@@ -199,11 +181,11 @@ public function stripStrLength($value, $limit = null, $suffix = null)
199181
{
200182
if ($limit === null) {
201183
// load limit from configuration
202-
$limit = $this->_conf->syslog->stringLengthLimit;
184+
$limit = $this->conf->syslog->stringLengthLimit;
203185
}
204186
if ($suffix === null) {
205187
// load suffix from configuration
206-
$suffix = $this->_conf->syslog->stringCutSuffix;
188+
$suffix = $this->conf->syslog->stringCutSuffix;
207189
}
208190

209191
$strLen = mb_strlen($value);

0 commit comments

Comments
 (0)