-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.php
195 lines (165 loc) · 6.22 KB
/
handler.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/php
<?php
/**********************************************************************************************************
* PaperCut Email-To-Print Handler for Postfix V1.1 *
* Recieve email from a specific TO address and *
* Rewrite TO header, storing in a local mailbox *
* Based on: https://thecodingmachine.io/triggering-a-php-script-when-your-postfix-server-receives-a-mail *
**********************************************************************************************************/
/// CONFIG SECTION \\\
// Should we output the last received email to a file
$debug_file = false;
$debug_output_file = "/tmp/lastemail";
$log_file = "/var/log/emailtoprint.log";
// Accepted TO addresses
// All other emails will be dropped
$accepted_to = array(
);
// Target TO addresses
// We will round-robin over these to cause load-balancing across the print queues
$desired_to = array(
);
// Target Local Mailbox
// The path to the local mailbox file we will deliver this to
// This is the mailbox file the IMAP/POP server will serve to PaperCut
$mailbox = "/var/mail/etpmailbox";
/// SCRIPT \\\
// Error Handling
function error_handler($errno, $errstr, $errfile, $errline)
{
if(!(error_reporting() & $errno)) return false;
// Log the error in a panic file
try
{
$log_file = fopen("/var/log/emailtoprint.error", "w");
fwrite($log_file, $errstr);
fclose($log_file);
}
catch( Exception $e )
{
}
// Return an error the MTA can handle
echo "The MTA was unable to deliver your message. Please contact [email protected]";
die();
}
set_error_handler("error_handler");
ini_set('display_errors', 0);
// Open the log file
$log_file = fopen($log_file, "a");
fwrite($log_file, "---\nExecuting PaperCut Email-To-Print script at ".date("Y-m-d H:i:s")."\n");
// Read the email from STDIN
fwrite($log_file, "Reading email from STDIN\n");
$email = "";
$email_file = fopen("php://stdin", "r");
while( !feof($email_file) )
{
$line = fread($email_file, 1024);
$email .= $line;
}
fclose($email_file);
fwrite($log_file, "Email Data read: ".strlen($email)."\n");
if( $debug_file === true && strlen($debug_output_file) >= 1 )
{
// Write the debug file if enabled
$debug_output = fopen($debug_output_file, "w");
fwrite($debug_output, $email);
fclose($debug_output);
}
// Put sender addres in logfile
$from_header_regex_name = preg_match('/^From:.*<(.*@.*)>$/im', $email, $match_from_email);
$from_header_regex = preg_match('/^From:[\s]*([^<@]*[^\n]*)$/im', $email, $match_from_email2);
if( $from_header_regex_name === 1 ) fwrite($log_file, "Sender Address: ".$match_from_email[1]."\n");
else if( $from_header_regex === 1 ) fwrite($log_file, "Sender Address: ".$match_from_email2[1]."\n");
else fwrite($log_file, "Could not determine sender address\n");
// Check the TO is one of the valid emails
fwrite($log_file, "Validating TO Header against configuration\n");
$to_header_regex_name = preg_match('/^To:.*<(.*@.*)>$/im', $email, $match_name);
$to_header_regex = preg_match('/^To:[\s]*([^<@]*[^\n]*)$/im', $email, $match_email);
if( $to_header_regex_name !== 1 && $to_header_regex !== 1 )
{
fwrite($log_file, "Email rejected - Unable to find the TO header in the email!\n");
fclose($log_file);
exit();
}
$to_header_addresses = array();
if( count($match_name) == 2 )
{
foreach( explode(",", $match_name[1]) as $key => $value )
{
$valid_email = strtolower(filter_var($value, FILTER_VALIDATE_EMAIL));
if( !in_array($valid_email, $to_header_addresses) )
{
$to_header_addresses[] = $valid_email;
}
}
}
if( count($match_email) == 2 )
{
foreach( explode(",", $match_email[1]) as $key => $value )
{
$valid_email = strtolower(filter_var($value, FILTER_VALIDATE_EMAIL));
if( !in_array($valid_email, $to_header_addresses) )
{
$to_header_addresses[] = $valid_email;
}
}
}
fwrite($log_file, "Emails found in To header:\n");
foreach( $to_header_addresses as $key => $value )
{
fwrite($log_file, $value."\n");
}
// Validate against the configuration
$found_valid_email = false;
foreach( $to_header_addresses as $key => $value )
{
if( in_array($value, $accepted_to) === true )
{
$found_valid_email = true;
break;
}
}
if( $found_valid_email === true )
{
fwrite($log_file, "Email accepted - At least one TO address contained within configuration\n");
}
else
{
fwrite($log_file, "Email rejected - None of the TO addresses exist in the configuration\n");
file_put_contents("/tmp/rejectedmail.log", $email, FILE_APPEND);
fclose($log_file);
exit();
}
// Get the next round-robin entry
$indexing_file_path = "/home/cop28etp/.indexer";
$current_position = 0;
$current_position_file = @file_get_contents($indexing_file_path, false);
if( $current_position_file !== false )
{
$current_position = intval($current_position_file);
}
fwrite($log_file, "Current position is: ".$current_position."\n");
// Calculate the next entry
$new_position = ($current_position >= (count($desired_to)-1)) ? 0 : $current_position + 1;
fwrite($log_file, "New position is: ".$new_position."\n");
@file_put_contents($indexing_file_path, $new_position);
// Update the To header
$this_address = $desired_to[$current_position];
fwrite($log_file, "Replacing TO header with address: ".$this_address."\n");
$new_header = sprintf("To: %s\n", $this_address);
fwrite($log_file, "Replacement header: ".$new_header."\n");
$email = preg_replace('/To:(.*)\n/im', $new_header, $email);
// Add email to user spool file
fwrite($log_file, "Storing mail in target spoolfile: ".$mailbox."\n");
file_put_contents($mailbox, $email, FILE_APPEND);
// Close log file
fwrite($log_file, "---\nLog closed at ".date("Y-m-d H:i:s")."\n");
fclose($log_file);
?>