-
Notifications
You must be signed in to change notification settings - Fork 6
/
SMTP_Server_Session.php
224 lines (177 loc) · 4.61 KB
/
SMTP_Server_Session.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* SMTP_Server_Session
*
* @author Jason Johnson <jason@php-smtp.org>
* @copyright Copyright (c) 2008, Jason Johnson
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @version 1.0
* @package php-smtp
*/
class SMTP_Server_Session {
var $log;
var $index;
var $socket;
var $buffer;
var $date;
var $to;
var $from;
var $domains;
var $complete;
var $api;
var $is_authenticated;
var $is_local_account;
function SMTP_Server_Session($socket) {
global $api;
$this->log = new SMTP_Server_Log();
$this->index = new SMTP_Server_Index();
$this->socket = $socket;
$this->date = time();
$this->to = array();
$this->from = array();
$this->complete = false;
$this->api = &$api;
$this->domains = explode(',', SMTP_VALID_DOMAINS);
$this->is_authenticated = false;
$this->is_local_account = false;
}
/**
* Enters a loop to read and process incoming commands, exits when $this->complete is true
*/
function run() {
$this->socket->write(SMTP_220);
while($this->buffer = $this->socket->read()) {
$cmd = substr($this->buffer, 0, 4);
$arg = trim(substr($this->buffer, 5));
$this->dispatch($cmd, $arg);
if($this->complete) {
break;
}
}
$this->socket->write(SMTP_221);
$this->socket->close();
}
function dispatch($cmd, $arg) {
switch($cmd) {
case 'HELO': $this->HELO($arg); break;
case 'EHLO': $this->EHLO($arg); break;
case 'AUTH': $this->AUTH($arg); break;
case 'MAIL': $this->MAIL($arg); break;
case 'RCPT': $this->RCPT($arg); break;
case 'DATA': $this->DATA($arg); break;
case 'RSET': $this->RSET($arg); break;
case 'HELP': $this->HELP($arg); break;
case 'QUIT': $this->QUIT($arg); break;
default: $this->NOT_IMPLEMENTED();
}
}
function EHLO($arg) {
$this->socket->write('250-This host supports a few commands');
$this->socket->write('250-AUTH PLAIN CRAM-MD5');
$this->socket->write('250 HELP');
}
function HELO($arg) {
$this->socket->write(SMTP_250);
}
function AUTH($arg) {
if(substr($arg, 0, 8) == 'CRAM-MD5') {
$this->AUTH_CRAM_MD5();
}
if(substr($arg, 0, 5) == 'PLAIN') {
$this->AUTH_PLAIN(substr($arg, 6));
}
if(substr($arg, 0, 5) == 'LOGIN') {
$this->AUTH_LOGIN();
}
}
function AUTH_CRAM_MD5() {
$this->socket->write(SMTP_504);
}
function AUTH_LOGIN() {
$this->socket->write(SMTP_504);
}
function AUTH_PLAIN($arg) {
list($auth_id, $user_id, $password) = explode(chr(0),base64_decode($arg));
if($this->api->hook(SMTP_API_AUTH, $auth_id, $user_id, $password)) {
$this->is_authenticated = true;
$this->socket->write(SMTP_235);
} else {
$this->is_authenticated = false;
$this->socket->write(SMTP_550);
}
}
function MAIL($arg) {
$arg = trim($arg, 'FfRrOoMm: <>');
$arr = explode('@', $arg);
$this->from['user'] = $arr[0];
$this->from['domain'] = $arr[1];
$this->socket->write(SMTP_250);
}
function RCPT($arg) {
$arg = trim($arg, 'TtOo: <>');
$arr = explode('@', $arg);
$to = array('user' => $arr[0], 'domain' => $arr[1]);
if(!in_array($to['domain'], $this->domains) && !$this->is_authenticated) {
$this->socket->write(SMTP_550);
return;
}
$this->to[] = $to;
$this->socket->write(SMTP_250);
}
function DATA($arg) {
if(!$this->to) {
$this->socket->write(SMTP_503);
return;
}
$this->socket->write(SMTP_354);
if($this->is_authenticated) {
$file = SMTP_OUTBOUND;
} else {
$file = SMTP_INBOUND;
$file .= $this->to['domain'].DIRECTORY_SEPARATOR;
$file .= $this->to['user'].DIRECTORY_SEPARATOR;
}
// If the path does not exist, create it recursively
if(!file_exists($file)) {
mkdir($file, 0700, true);
}
$file .= $this->date."@".$this->to[0]['user']."@".$this->to[0]['domain'];
$this->index->write($file, $this->to, $this->from, $this->date);
$size = 0;
$size_exceeded = false;
if($msg = fopen($file, 'w')) {
while($this->buffer = $this->socket->read()) {
$size += SMTP_CHUNK_SIZE;
if($size > SMTP_MAX_SIZE) {
$size_exceeded = true;
break;
}
fwrite($msg, $this->buffer);
if(substr($this->buffer, -5) == "\r\n.\r\n") {
break;
}
}
fclose($msg);
}
if(!$size_exceeded) {
$this->socket->write(SMTP_250);
} else {
$this->socket->write(SMTP_552);
}
$this->complete = true;
}
function RSET($arg) {
$this->socket->write(SMTP_250);
}
function HELP($arg) {
$this->socket->write(SMTP_250);
}
function QUIT($arg) {
$this->socket->write(SMTP_221);
$this->socket->close();
}
function NOT_IMPLEMENTED() {
$this->socket->write(SMTP_502);
}
}
?>