1<?php
2/**
3 * This file contains the code for the email-HTTP SOAP gateway 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_Email */
23require_once 'SOAP/Server/Email.php';
24require_once 'SOAP/Transport.php';
25
26/**
27 * SOAP Server Class that implements an email SOAP server.
28 * http://www.pocketsoap.com/specs/smtpbinding/
29 *
30 * This class overrides the default HTTP server, providing the ability to
31 * parse an email message and execute soap calls.  This class DOES NOT pop the
32 * message; the message, complete with headers, must be passed in as a
33 * parameter to the service function call.
34 *
35 * This class calls a provided HTTP SOAP server, forwarding the email request,
36 * then sending the HTTP response out as an email.
37 *
38 * @access   public
39 * @package  SOAP
40 * @author   Shane Caraveo <shane@php.net>
41 */
42class SOAP_Server_Email_Gateway extends SOAP_Server_Email {
43
44    var $gateway = null;
45    var $dump = false;
46
47    function SOAP_Server_Email_Gateway($gateway = '', $send_response = true,
48                                       $dump = false)
49    {
50        parent::SOAP_Server();
51        $this->send_response = $send_response;
52        $this->gateway = $gateway;
53        $this->dump = $dump;
54    }
55
56    function service(&$data, $gateway = '', $endpoint = '',
57                     $send_response = true, $dump = false)
58    {
59        $this->endpoint = $endpoint;
60        $response = '';
61        $useEncoding = 'Mime';
62        $options = array();
63        if (!$gateway) {
64            $gateway = $this->gateway;
65        }
66
67        /* We have a full set of headers, need to find the first blank
68         * line. */
69        $this->_parseEmail($data);
70        if ($this->fault) {
71            $response = $this->fault->message();
72        }
73        if ($this->headers['content-type'] == 'application/dime')
74            $useEncoding = 'DIME';
75
76        /* Call the HTTP Server. */
77        if (!$response) {
78            $soap_transport =& SOAP_Transport::getTransport($gateway, $this->xml_encoding);
79            if ($soap_transport->fault) {
80                $response = $soap_transport->fault->message();
81            }
82        }
83
84        /* Send the message. */
85        if (!$response) {
86            $options['soapaction'] = $this->headers['soapaction'];
87            $options['headers']['Content-Type'] = $this->headers['content-type'];
88
89            $response = $soap_transport->send($data, $options);
90            if (isset($this->headers['mime-version']))
91                $options['headers']['MIME-Version'] = $this->headers['mime-version'];
92
93            if ($soap_transport->fault) {
94                $response = $soap_transport->fault->message();
95            } else {
96                foreach ($soap_transport->transport->attachments as $cid => $body) {
97                    $this->attachments[] = array('body' => $body, 'cid' => $cid, 'encoding' => 'base64');
98                }
99                if (count($this->_attachments)) {
100                    if ($useEncoding == 'Mime') {
101                        $soap_msg = $this->_makeMimeMessage($response);
102                        $options['headers']['MIME-Version'] = '1.0';
103                    } else {
104                        /* Default is DIME. */
105                        $soap_msg = $this->_makeDIMEMessage($response);
106                        $options['headers']['Content-Type'] = 'application/dime';
107                    }
108                    if (PEAR::isError($soap_msg)) {
109                        return $this->_raiseSoapFault($soap_msg);
110                    }
111                    if (is_array($soap_msg)) {
112                        $response = $soap_msg['body'];
113                        if (count($soap_msg['headers'])) {
114                            if (isset($options['headers'])) {
115                                $options['headers'] = array_merge($options['headers'], $soap_msg['headers']);
116                            } else {
117                                $options['headers'] = $soap_msg['headers'];
118                            }
119                        }
120                    }
121                }
122            }
123        }
124
125        if ($this->send_response) {
126            if ($this->dump || $dump) {
127                print $response;
128            } else {
129                $from = array_key_exists('reply-to', $this->headers) ? $this->headers['reply-to'] : $this->headers['from'];
130
131                $soap_transport =& SOAP_Transport::getTransport('mailto:' . $from, $this->response_encoding);
132                $from = $this->endpoint ? $this->endpoint : $this->headers['to'];
133                $headers = array('In-Reply-To' => $this->headers['message-id']);
134                $options = array('from' => $from, 'subject'=> $this->headers['subject'], 'headers' => $headers);
135                $soap_transport->send($response, $options);
136            }
137        }
138    }
139}
140