3
* This file contains the code for the email SOAP server.
7
* LICENSE: This source file is subject to version 2.02 of the PHP license,
8
* that is bundled with this package in the file LICENSE, and is available at
9
* through the world-wide-web at http://www.php.net/license/2_02.txt. If you
10
* did not receive a copy of the PHP license and are unable to obtain it
11
* through the world-wide-web, please send a note to license@php.net so we can
12
* mail you a copy immediately.
14
* @category Web Services
16
* @author Shane Caraveo <Shane@Caraveo.com> Port to PEAR and more
17
* @copyright 2003-2005 The PHP Group
18
* @license http://www.php.net/license/2_02.txt PHP License 2.02
19
* @link http://pear.php.net/package/SOAP
23
require_once 'SOAP/Server.php';
24
require_once 'SOAP/Client.php';
25
require_once 'SOAP/Transport.php';
26
require_once 'Mail/mimeDecode.php';
29
* SOAP Server Class that implements an email SOAP server.
30
* http://www.pocketsoap.com/specs/smtpbinding/
32
* This class overrides the default HTTP server, providing the ability to
33
* parse an email message and execute SOAP calls. This class DOES NOT pop the
34
* message; the message, complete with headers, must be passed in as a
35
* parameter to the service function call.
39
* @author Shane Caraveo <shane@php.net>
41
class SOAP_Server_Email extends SOAP_Server {
43
var $headers = array();
45
function SOAP_Server_Email($send_response = true)
47
parent::SOAP_Server();
48
$this->send_response = $send_response;
52
* Removes HTTP headers from response.
54
* TODO: use PEAR email classes
59
function _parseEmail(&$data)
61
if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $data, $match)) {
63
if (preg_match_all('/^(.*?):\s+(.*)$/m', $match[1], $matches)) {
64
$hc = count($matches[0]);
65
for ($i = 0; $i < $hc; $i++) {
66
$this->headers[strtolower($matches[1][$i])] = trim($matches[2][$i]);
70
if (!stristr($this->headers['content-type'], 'text/xml')) {
71
$this->_raiseSoapFault('Invalid Content Type', '', '', 'Client');
75
if (strcasecmp($this->headers['content-transfer-encoding'], 'base64')==0) {
77
$enctext = preg_replace("/[\r|\n]/", '', $match[2]);
78
$data = base64_decode($enctext);
83
/* If no content, return false. */
84
return strlen($this->request) > 0;
87
$this->_raiseSoapFault('Invalid Email Format', '', '', 'Client');
92
function client(&$data)
94
$attachments = array();
96
/* If neither matches, we'll just try it anyway. */
97
if (stristr($data, 'Content-Type: application/dime')) {
98
$this->_decodeDIMEMessage($data, $this->headers, $attachments);
99
} elseif (stristr($data, 'MIME-Version:')) {
100
/* This is a mime message, let's decode it. */
101
$this->_decodeMimeMessage($data, $this->headers, $attachments);
103
/* The old fallback, but decodeMimeMessage handles things fine. */
104
$this->_parseEmail($data);
107
/* Get the character encoding of the incoming request treat incoming
108
* data as UTF-8 if no encoding set. */
109
if (!$this->soapfault &&
110
!$this->_getContentEncoding($this->headers['content-type'])) {
111
$this->xml_encoding = SOAP_DEFAULT_ENCODING;
112
/* An encoding we don't understand, return a fault. */
113
$this->_raiseSoapFault('Unsupported encoding, use one of ISO-8859-1, US-ASCII, UTF-8', '', '', 'Server');
116
if ($this->soapfault) {
117
return $this->soapfault->getFault();
120
$client =& new SOAP_Client(null);
122
return $client->parseResponse($data, $this->xml_encoding, $this->attachments);
125
function service(&$data, $endpoint = '', $send_response = true,
128
$this->endpoint = $endpoint;
129
$attachments = array();
132
/* If neither matches, we'll just try it anyway. */
133
if (stristr($data, 'Content-Type: application/dime')) {
134
$this->_decodeDIMEMessage($data, $this->headers, $attachments);
135
$useEncoding = 'DIME';
136
} elseif (stristr($data, 'MIME-Version:')) {
137
/* This is a mime message, let's decode it. */
138
$this->_decodeMimeMessage($data, $this->headers, $attachments);
139
$useEncoding = 'Mime';
141
/* The old fallback, but decodeMimeMessage handles things fine. */
142
$this->_parseEmail($data);
145
/* Get the character encoding of the incoming request treat incoming
146
* data as UTF-8 if no encoding set. */
147
if (!$this->_getContentEncoding($this->headers['content-type'])) {
148
$this->xml_encoding = SOAP_DEFAULT_ENCODING;
149
/* An encoding we don't understand, return a fault. */
150
$this->_raiseSoapFault('Unsupported encoding, use one of ISO-8859-1, US-ASCII, UTF-8', '', '', 'Server');
151
$response = $this->getFaultMessage();
154
if ($this->soapfault) {
155
$response = $this->soapfault->message();
157
$soap_msg = $this->parseRequest($data,$attachments);
159
/* Handle Mime or DIME encoding. */
160
/* TODO: DIME Encoding should move to the transport, do it here
161
* for now and for ease of getting it done. */
162
if (count($this->_attachments)) {
163
if ($useEncoding == 'Mime') {
164
$soap_msg = $this->_makeMimeMessage($soap_msg);
166
/* Default is DIME. */
167
$soap_msg = $this->_makeDIMEMessage($soap_msg);
168
$soap_msg['headers']['Content-Type'] = 'application/dime';
170
if (PEAR::isError($soap_msg)) {
171
return $this->raiseSoapFault($soap_msg);
175
if (is_array($soap_msg)) {
176
$response = $soap_msg['body'];
177
if (count($soap_msg['headers'])) {
178
$headers = $soap_msg['headers'];
181
$response = $soap_msg;
185
if ($this->send_response) {
189
$from = array_key_exists('reply-to', $this->headers) ? $this->headers['reply-to'] : $this->headers['from'];
191
$soap_transport =& SOAP_Transport::getTransport('mailto:' . $from, $this->response_encoding);
192
$from = $this->endpoint ? $this->endpoint : $this->headers['to'];
193
$headers['In-Reply-To'] = $this->headers['message-id'];
194
$options = array('from' => $from, 'subject' => $this->headers['subject'], 'headers' => $headers);
195
$soap_transport->send($response, $options);