Email_Gateway.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Shane Caraveo <Shane@Caraveo.com> Port to PEAR and more |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Email_Gateway.php,v 1.9 2003/05/18 00:57:40 shane Exp $
  20. //
  21. require_once 'SOAP/Server/Email.php';
  22. require_once 'SOAP/Transport.php';
  23. /**
  24. * SOAP_Server_Email
  25. * SOAP Server Class
  26. *
  27. * implements Email SOAP Server
  28. * http://www.pocketsoap.com/specs/smtpbinding/
  29. *
  30. * class overrides the default HTTP server, providing the ability
  31. * to parse an email message and execute soap calls.
  32. * this class DOES NOT pop the message, the message, complete
  33. * with headers, must be passed in as a parameter to the service
  34. * function call
  35. *
  36. * This class calls a provided HTTP SOAP server, forwarding
  37. * the email request, then sending the HTTP response out as an
  38. * email
  39. *
  40. * @access public
  41. * @version $Id: Email_Gateway.php,v 1.9 2003/05/18 00:57:40 shane Exp $
  42. * @package SOAP::Server
  43. * @author Shane Caraveo <shane@php.net>
  44. */
  45. class SOAP_Server_Email_Gateway extends SOAP_Server_Email
  46. {
  47. var $gateway = NULL;
  48. var $dump = FALSE;
  49. function SOAP_Server_Email_Gateway($gateway = '', $send_response = TRUE, $dump=FALSE)
  50. {
  51. parent::SOAP_Server();
  52. $this->send_response = $send_response;
  53. $this->gateway = $gateway;
  54. $this->dump = $dump;
  55. }
  56. function service(&$data, $gateway='', $endpoint = '', $send_response = TRUE, $dump = FALSE)
  57. {
  58. $this->endpoint = $endpoint;
  59. $response = '';
  60. $useEncoding='Mime';
  61. $options = array();
  62. if (!$gateway) $gateway = $this->gateway;
  63. // we have a full set of headers, need to find the first blank line
  64. $this->_parseEmail($data);
  65. if ($this->fault) {
  66. $response = $this->fault->message();
  67. }
  68. if ($this->headers['content-type']=='application/dime')
  69. $useEncoding='DIME';
  70. # call the HTTP Server
  71. if (!$response) {
  72. $soap_transport =& SOAP_Transport::getTransport($gateway, $this->xml_encoding);
  73. if ($soap_transport->fault) {
  74. $response = $soap_transport->fault->message();
  75. }
  76. }
  77. // send the message
  78. if (!$response) {
  79. $options['soapaction'] = $this->headers['soapaction'];
  80. $options['headers']['Content-Type'] = $this->headers['content-type'];
  81. $response = $soap_transport->send($data, $options);
  82. if (isset($this->headers['mime-version']))
  83. $options['headers']['MIME-Version'] = $this->headers['mime-version'];
  84. if ($soap_transport->fault) {
  85. $response = $soap_transport->fault->message();
  86. } else {
  87. foreach ($soap_transport->transport->attachments as $cid=>$body) {
  88. $this->attachments[] = array('body' => $body, 'cid' => $cid, 'encoding' => 'base64');
  89. }
  90. if (count($this->__attachments)) {
  91. if ($useEncoding == 'Mime') {
  92. $soap_msg = $this->_makeMimeMessage($response);
  93. $options['headers']['MIME-Version'] = '1.0';
  94. } else {
  95. // default is dime
  96. $soap_msg = $this->_makeDIMEMessage($response);
  97. $options['headers']['Content-Type'] = 'application/dime';
  98. }
  99. if (PEAR::isError($soap_msg)) {
  100. return $this->_raiseSoapFault($soap_msg);
  101. }
  102. if (is_array($soap_msg)) {
  103. $response = $soap_msg['body'];
  104. if (count($soap_msg['headers'])) {
  105. if (isset($options['headers'])) {
  106. $options['headers'] = array_merge($options['headers'],$soap_msg['headers']);
  107. } else {
  108. $options['headers'] = $soap_msg['headers'];
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }
  115. if ($this->send_response) {
  116. if ($this->dump || $dump) {
  117. print $response;
  118. } else {
  119. $from = array_key_exists('reply-to',$this->headers) ? $this->headers['reply-to']:$this->headers['from'];
  120. # XXX what if no from?????
  121. $soap_transport =& SOAP_Transport::getTransport('mailto:'.$from, $this->response_encoding);
  122. $from = $this->endpoint ? $this->endpoint : $this->headers['to'];
  123. $headers = array('In-Reply-To'=>$this->headers['message-id']);
  124. $options = array('from' => $from, 'subject'=> $this->headers['subject'], 'headers' => $headers);
  125. $soap_transport->send($response, $options);
  126. }
  127. }
  128. }
  129. }
  130. ?>