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