forked from jasonjohnson/php-smtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SMTP_Server_Socket.php
143 lines (120 loc) · 3.13 KB
/
SMTP_Server_Socket.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
<?php
/**
* SMTP_Server_Socket
*
* @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_Socket {
var $log;
var $socket;
var $remote_address;
/**
* Constructor with an optional argument of a socket resource
*
* @param resource $socket A socket resource as returned by socket_create() or socket_accept()
*/
function SMTP_Server_Socket($socket = null) {
$this->log = new SMTP_Server_Log();
$this->socket = $socket;
$this->remote_address = '';
if(!$this->socket) {
$this->socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
}
}
/**
* Connects to the the specified $host on $port
*
* @param string $host The host to connect to
* @param int $port The port on the host to use, defaults to 25
*/
function connect($host, $port = 25) {
if(!socket_connect($this->socket, $host, $port)) {
$this->log->msg(SMTP_ERROR, "Could not connect to '".$host."' on port '".$port."'");
}
}
/**
* Binds the socket to a host and port
*
* @param string $host The host to bind the socket to
* @param int $port The port on the host to use
*/
function bind($host, $port) {
while(!socket_bind($this->socket, $host, $port)) {
$this->log->msg(SMTP_NOTICE, "Binding...");
sleep(5);
}
$this->log->msg(SMTP_NOTICE, "Bound!");
}
/**
* Cause the socket to listen for incoming connections
*/
function listen() {
$this->log->msg(SMTP_NOTICE, "Listening...");
socket_listen($this->socket);
}
/**
* Block and accept an incoming connection
*
* @return SMTP_Server_Socket
*/
function accept() {
$remote = socket_accept($this->socket);
if(!socket_getpeername($remote, $this->remote_address)) {
$this->log->msg(SMTP_WARNING, "Could not determine remote address");
}
$this->log->msg(SMTP_DEBUG, "Accepted connection from '".$this->remote_address."'");
return new SMTP_Server_Socket($remote);
}
/**
* Returns the address of the remote client or server
*
* @return string
*/
function remote_address() {
return $this->remote_address;
}
/**
* Writes the supplied buffer to the socket
*
* @param string $buffer The buffer to be written to the socket
*/
function write($buffer, $raw = false) {
$this->log->msg(SMTP_DEBUG, ">>> $buffer");
socket_write($this->socket, ($raw?$buffer:($buffer."\r\n")));
}
/**
* Writes an entire file to the socket
*
* @param string $path The absolute or relative path of the file
*/
function write_file($path) {
if($file = fopen($path, 'r')) {
while(!feof($file)) {
$this->write(fgets($file), true);
}
fclose($file);
}
}
/**
* Reads and returns a default length of bytes from the socket
*
* @return string
*/
function read() {
$buffer = socket_read($this->socket, SMTP_CHUNK_SIZE);
$this->log->msg(SMTP_DEBUG, "<<< $buffer");
return $buffer;
}
/**
* Closes the socket
*/
function close() {
$this->log->msg(SMTP_DEBUG, "Closing socket connection");
socket_close($this->socket);
}
}
?>