| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* This file contains the code for the email SOAP server.
|
|
|
4 |
*
|
|
|
5 |
* PHP versions 4 and 5
|
|
|
6 |
*
|
|
|
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.
|
|
|
13 |
*
|
|
|
14 |
* @category Web Services
|
|
|
15 |
* @package SOAP
|
|
|
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
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/** SOAP_Server */
|
|
|
23 |
require_once 'SOAP/Server.php';
|
|
|
24 |
require_once 'SOAP/Client.php';
|
|
|
25 |
require_once 'SOAP/Transport.php';
|
|
|
26 |
require_once 'Mail/mimeDecode.php';
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* SOAP Server Class that implements an email SOAP server.
|
|
|
30 |
* http://www.pocketsoap.com/specs/smtpbinding/
|
|
|
31 |
*
|
|
|
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.
|
|
|
36 |
*
|
|
|
37 |
* @access public
|
|
|
38 |
* @package SOAP
|
|
|
39 |
* @author Shane Caraveo <shane@php.net>
|
|
|
40 |
*/
|
|
|
41 |
class SOAP_Server_Email extends SOAP_Server {
|
|
|
42 |
|
|
|
43 |
var $headers = array();
|
|
|
44 |
|
|
|
45 |
function SOAP_Server_Email($send_response = true)
|
|
|
46 |
{
|
|
|
47 |
parent::SOAP_Server();
|
|
|
48 |
$this->send_response = $send_response;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Removes HTTP headers from response.
|
|
|
53 |
*
|
|
|
54 |
* TODO: use PEAR email classes
|
|
|
55 |
*
|
|
|
56 |
* @return boolean
|
|
|
57 |
* @access private
|
|
|
58 |
*/
|
|
|
59 |
function _parseEmail(&$data)
|
|
|
60 |
{
|
|
|
61 |
if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $data, $match)) {
|
|
|
62 |
|
|
|
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]);
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
if (!stristr($this->headers['content-type'], 'text/xml')) {
|
|
|
71 |
$this->_raiseSoapFault('Invalid Content Type', '', '', 'Client');
|
|
|
72 |
return false;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
if (strcasecmp($this->headers['content-transfer-encoding'], 'base64')==0) {
|
|
|
76 |
/* Unfold lines. */
|
|
|
77 |
$enctext = preg_replace("/[\r|\n]/", '', $match[2]);
|
|
|
78 |
$data = base64_decode($enctext);
|
|
|
79 |
} else {
|
|
|
80 |
$data = $match[2];
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/* If no content, return false. */
|
|
|
84 |
return strlen($this->request) > 0;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
$this->_raiseSoapFault('Invalid Email Format', '', '', 'Client');
|
|
|
88 |
|
|
|
89 |
return false;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
function client(&$data)
|
|
|
93 |
{
|
|
|
94 |
$attachments = array();
|
|
|
95 |
|
|
|
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);
|
|
|
102 |
} else {
|
|
|
103 |
/* The old fallback, but decodeMimeMessage handles things fine. */
|
|
|
104 |
$this->_parseEmail($data);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
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');
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if ($this->soapfault) {
|
|
|
117 |
return $this->soapfault->getFault();
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
$client =& new SOAP_Client(null);
|
|
|
121 |
|
|
|
122 |
return $client->parseResponse($data, $this->xml_encoding, $this->attachments);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
function service(&$data, $endpoint = '', $send_response = true,
|
|
|
126 |
$dump = false)
|
|
|
127 |
{
|
|
|
128 |
$this->endpoint = $endpoint;
|
|
|
129 |
$attachments = array();
|
|
|
130 |
$headers = array();
|
|
|
131 |
|
|
|
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';
|
|
|
140 |
} else {
|
|
|
141 |
/* The old fallback, but decodeMimeMessage handles things fine. */
|
|
|
142 |
$this->_parseEmail($data);
|
|
|
143 |
}
|
|
|
144 |
|
|
|
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();
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
if ($this->soapfault) {
|
|
|
155 |
$response = $this->soapfault->message();
|
|
|
156 |
} else {
|
|
|
157 |
$soap_msg = $this->parseRequest($data,$attachments);
|
|
|
158 |
|
|
|
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);
|
|
|
165 |
} else {
|
|
|
166 |
/* Default is DIME. */
|
|
|
167 |
$soap_msg = $this->_makeDIMEMessage($soap_msg);
|
|
|
168 |
$soap_msg['headers']['Content-Type'] = 'application/dime';
|
|
|
169 |
}
|
|
|
170 |
if (PEAR::isError($soap_msg)) {
|
|
|
171 |
return $this->raiseSoapFault($soap_msg);
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
if (is_array($soap_msg)) {
|
|
|
176 |
$response = $soap_msg['body'];
|
|
|
177 |
if (count($soap_msg['headers'])) {
|
|
|
178 |
$headers = $soap_msg['headers'];
|
|
|
179 |
}
|
|
|
180 |
} else {
|
|
|
181 |
$response = $soap_msg;
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
if ($this->send_response) {
|
|
|
186 |
if ($dump) {
|
|
|
187 |
print $response;
|
|
|
188 |
} else {
|
|
|
189 |
$from = array_key_exists('reply-to', $this->headers) ? $this->headers['reply-to'] : $this->headers['from'];
|
|
|
190 |
|
|
|
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);
|
|
|
196 |
}
|
|
|
197 |
}
|
|
|
198 |
}
|
|
|
199 |
}
|