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