-
Notifications
You must be signed in to change notification settings - Fork 6
/
SMTP_Server_Relay_Session.php
181 lines (136 loc) · 3.43 KB
/
SMTP_Server_Relay_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
<?php
/**
* SMTP_Server_Relay_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_Relay_Session {
var $log;
var $index;
var $socket;
var $file_name;
var $to;
var $from;
var $host;
var $date;
var $headers = array();
function SMTP_Server_Relay_Session($pos) {
$this->log = new SMTP_Server_Log();
$this->index = new SMTP_Server_Index();
$this->socket = new SMTP_Server_Socket();
$rec = $this->index->get($pos);
$this->file_name = $rec[0];
$this->to = $rec[1];
$this->from = $rec[2];
$this->date = $rec[3];
}
function resolve($address) {
list($user, $domain) = $this->split_address($address);
$hosts = array();
$weight = array();
$prefer = 0;
getmxrr($domain, $hosts, $weight);
for($i = 0; $i < count($hosts); $i++) {
$this->log->msg(SMTP_DEBUG, "HOST #".$i." for '".$domain."' is '".$hosts[$i]."' with the weight '".$weight[$i]."'");
if($weight[$i] < $weight[$prefer]) {
$prefer = $i;
}
}
if(count($hosts) <= 0) {
return false;
}
$this->log->msg(SMTP_DEBUG, "PREFER HOST INDEX $prefer");
$this->host = $hosts[$prefer];
}
function extract_address($header) {
$matches = array();
if(preg_match("/([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}/ix", $header, $matches) <= 0) {
return false;
}
return $matches[0];
}
function split_address($address) {
return explode('@', $address);
}
function scan_headers() {
if($file = fopen($this->file_name, 'r')) {
while(trim($line = fgets($file)) != '') {
if(substr($line, 0, 3) == 'To:') {
$this->to = $this->extract_address($line);
}
if(substr($line, 0, 5) == 'From:') {
$this->from = $this->extract_address($line);
}
$this->headers[] = $line;
}
fclose($file);
}
}
function send($to) {
$this->socket->connect($this->host);
while(true) {
if(!$this->HELO()) break;
if(!$this->MAIL()) break;
if(!$this->RCPT($to)) break;
if(!$this->DATA()) break;
if(!$this->QUIT()) break;
break;
}
$this->socket->close();
}
function HELO() {
$this->socket->write("HELO");
if(substr($this->socket->read(), 0, 1) != '2') {
return false;
}
return true;
}
function MAIL() {
$this->socket->write("MAIL FROM: <".$this->from.">");
if(substr($this->socket->read(), 0, 1) != '2') {
return false;
}
return true;
}
function RCPT($to) {
$this->socket->write("RCPT TO: <".$to.">");
if(substr($this->socket->read(), 0, 1) != '2') {
return false;
}
return true;
}
function DATA() {
$this->socket->write("DATA");
if(substr($this->socket->read(), 0, 1) != '3') {
return false;
}
$this->socket->write_file($this->file_name);
if(substr($this->socket->read(), 0, 1) != '2') {
return false;
}
return true;
}
function QUIT() {
$this->socket->write("QUIT");
$this->socket->read();
return true;
}
function remove() {
unlink($this->file_name);
}
function run() {
if(!file_exists($this->file_name))
return false;
for($i = 0; $i < count($this->to); $i++) {
$this->resolve($this->to[$i]);
$this->send($this->to[$i]);
}
$this->remove();
$this->log->msg(SMTP_DEBUG, "RELAYED MESSAGE TO '".join(',',$this->to)."' FROM '".$this->from."'");
}
}
?>