forked from logical-and/postfix_php_policy_service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostfix-util.php
112 lines (103 loc) · 2.39 KB
/
postfix-util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace {
/**
* Fetch args from CLI and return fetched args array
* @return array Sample:
* <pre>Array
(
[request] => smtpd_access_policy
[protocol_state] => MAIL
[protocol_name] => ESMTP
[client_address] => 91.124.206.200
[client_name] => 200-206-124-91.pool.ukrtel.net
[reverse_client_name] => 200-206-124-91.pool.ukrtel.net
[helo_name] => [192.168.1.3]
[sender] => [email protected]
[recipient] =>
[recipient_count] => 0
[queue_id] =>
[instance] =>
[size] => 1732
[etrn_domain] =>
[stress] =>
[sasl_method] => LOGIN
[sasl_username] => test.redmine
[sasl_sender] =>
[ccert_subject] =>
[ccert_issuer] =>
[ccert_fingerprint] =>
[ccert_pubkey_fingerprint] =>
[encryption_protocol] =>
[encryption_cipher] =>
[encryption_keysize] => 0
)
* </pre>
*/
function fetchArgs()
{
$args = [];
while ($line = trim(fgets(STDIN)))
{
list($key, $value) = explode('=', $line);
$args[$key] = $value;
}
return $args;
}
define('ACTION_ALLOW', 'dunno');
define('ACTION_REJECT', 'reject');
/**
* Send the result and stop script work
*
* @param $action
* @param string $message
*/
function sendResult($action, $message = '')
{
echo "action=$action" . (trim($message) ? (' ' . trim($message)) : '') . "\n\n";
exit(0);
}
/**
* Parse hash and return result
* @param $path
* @param array $bindings
* @param bool $hashKey
* @return array Example:
* <pre> Array
(
[[email protected]] => Array
(
[email] => [email protected]
[account] => test.redmine
)
)</pre><br/>
* OR <br/>
* <pre> Array
(
[0] => Array
(
[email] => [email protected]
[account] => test.redmine
)
)</pre><br/>
*/
function parseHash($path, array $bindings, $hashKey = FALSE)
{
if ($hashKey AND !in_array($hashKey, $bindings)) throw new \InvalidArgumentException('Bindings must contain a key!');
if (!file_exists($path)) throw new \RuntimeException("File \"$path\"not exists!");
$parsed = [];
$contents = file_get_contents($path);
foreach (preg_split('#[\n\r]+#', $contents) as $line)
{
$parsedLine = preg_split('#[\s]{1,}#', $line);
if (empty($parsedLine[0])) continue; // empty string
$parsedRow = [];
foreach ($bindings as $index => $key)
{
if (!empty($parsedLine[$index])) $parsedRow[$key] = trim($parsedLine[$index]);
}
if (!$hashKey) $parsed[] = $parsedRow;
else $parsed[$parsedRow[$hashKey]] = $parsedRow;
}
return $parsed;
}
}