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
22
require_once 'SOAP/Server.php';
23
require_once 'SOAP/Client.php';
24
require_once 'SOAP/Transport.php';
25
require_once 'Mail/mimeDecode.php';
28
* SOAP Server Class that implements an email SOAP server.
29
* http://www.pocketsoap.com/specs/smtpbinding/
31
* This class overrides the default HTTP server, providing the ability to
32
* parse an email message and execute SOAP calls. This class DOES NOT pop the
33
* message; the message, complete with headers, must be passed in as a
34
* parameter to the service function call.
38
* @author Shane Caraveo <shane@php.net>
40
class SOAP_Server_Email extends SOAP_Server {
42
var $headers = array();
44
function SOAP_Server_Email($send_response = true)
46
parent::SOAP_Server();
47
$this->send_response = $send_response;
51
* Removes HTTP headers from response.
53
* TODO: use PEAR email classes
58
function _parseEmail(&$data)
60
if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $data, $match)) {
62
if (preg_match_all('/^(.*?):\s+(.*)$/m', $match[1], $matches)) {
63
$hc = count($matches[0]);
64
for ($i = 0; $i < $hc; $i++) {
65
$this->headers[strtolower($matches[1][$i])] = trim($matches[2][$i]);
69
if (!stristr($this->headers['content-type'], 'text/xml')) {
70
$this->_raiseSoapFault('Invalid Content Type', '', '', 'Client');
74
if (strcasecmp($this->headers['content-transfer-encoding'], 'base64')==0) {
76
$enctext = preg_replace("/[\r|\n]/", '', $match[2]);
77
$data = base64_decode($enctext);
82
/* If no content, return false. */
83
return strlen($this->request) > 0;
86
$this->_raiseSoapFault('Invalid Email Format', '', '', 'Client');
91
function client(&$data)
93
$attachments = array();
95
/* If neither matches, we'll just try it anyway. */
96
if (stristr($data, 'Content-Type: application/dime')) {
97
$this->_decodeDIMEMessage($data, $this->headers, $attachments);
98
} elseif (stristr($data, 'MIME-Version:')) {
99
/* This is a mime message, let's decode it. */
100
$this->_decodeMimeMessage($data, $this->headers, $attachments);
102
/* The old fallback, but decodeMimeMessage handles things fine. */
103
$this->_parseEmail($data);
106
/* Get the character encoding of the incoming request treat incoming
107
* data as UTF-8 if no encoding set. */
108
if (!$this->soapfault &&
109
!$this->_getContentEncoding($this->headers['content-type'])) {
110
$this->xml_encoding = SOAP_DEFAULT_ENCODING;
111
/* An encoding we don't understand, return a fault. */
112
$this->_raiseSoapFault('Unsupported encoding, use one of ISO-8859-1, US-ASCII, UTF-8', '', '', 'Server');
115
if ($this->soapfault) {
116
return $this->soapfault->getFault();
119
$client =& new SOAP_Client(null);
121
return $client->parseResponse($data, $this->xml_encoding, $this->attachments);
124
function service(&$data, $endpoint = '', $send_response = true,
127
$this->endpoint = $endpoint;
128
$attachments = array();
131
/* If neither matches, we'll just try it anyway. */
132
if (stristr($data, 'Content-Type: application/dime')) {
133
$this->_decodeDIMEMessage($data, $this->headers, $attachments);
134
$useEncoding = 'DIME';
135
} elseif (stristr($data, 'MIME-Version:')) {
136
/* This is a mime message, let's decode it. */
137
$this->_decodeMimeMessage($data, $this->headers, $attachments);
138
$useEncoding = 'Mime';
140
/* The old fallback, but decodeMimeMessage handles things fine. */
141
$this->_parseEmail($data);
144
/* Get the character encoding of the incoming request treat incoming
145
* data as UTF-8 if no encoding set. */
146
if (!$this->_getContentEncoding($this->headers['content-type'])) {
147
$this->xml_encoding = SOAP_DEFAULT_ENCODING;
148
/* An encoding we don't understand, return a fault. */
149
$this->_raiseSoapFault('Unsupported encoding, use one of ISO-8859-1, US-ASCII, UTF-8', '', '', 'Server');
150
$response = $this->getFaultMessage();
153
if ($this->soapfault) {
154
$response = $this->soapfault->message();
156
$soap_msg = $this->parseRequest($data,$attachments);
158
/* Handle Mime or DIME encoding. */
159
/* TODO: DIME Encoding should move to the transport, do it here
160
* for now and for ease of getting it done. */
161
if (count($this->_attachments)) {
162
if ($useEncoding == 'Mime') {
163
$soap_msg = $this->_makeMimeMessage($soap_msg);
165
/* Default is DIME. */
166
$soap_msg = $this->_makeDIMEMessage($soap_msg);
167
$soap_msg['headers']['Content-Type'] = 'application/dime';
169
if (PEAR::isError($soap_msg)) {
170
return $this->raiseSoapFault($soap_msg);
174
if (is_array($soap_msg)) {
175
$response = $soap_msg['body'];
176
if (count($soap_msg['headers'])) {
177
$headers = $soap_msg['headers'];
180
$response = $soap_msg;
184
if ($this->send_response) {
188
$from = array_key_exists('reply-to', $this->headers) ? $this->headers['reply-to'] : $this->headers['from'];
190
$soap_transport =& SOAP_Transport::getTransport('mailto:' . $from, $this->response_encoding);
191
$from = $this->endpoint ? $this->endpoint : $this->headers['to'];
192
$headers['In-Reply-To'] = $this->headers['message-id'];
193
$options = array('from' => $from, 'subject' => $this->headers['subject'], 'headers' => $headers);
194
$soap_transport->send($response, $options);