SOAP
[ class tree: SOAP ] [ index: SOAP ] [ all elements ]

Source for file Email.php

Documentation is available at Email.php

  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.php,v 1.14 2005/05/03 21:12:45 chagenbu Exp $
  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.             $useEncoding 'DIME';
  99.         elseif (stristr($data'MIME-Version:')) {
  100.             /* This is a mime message, let's decode it. */
  101.             $this->_decodeMimeMessage($data$this->headers$attachments);
  102.             $useEncoding 'Mime';
  103.         else {
  104.             /* The old fallback, but decodeMimeMessage handles things fine. */
  105.             $this->_parseEmail($data);
  106.         }
  107.  
  108.         /* Get the character encoding of the incoming request treat incoming
  109.          * data as UTF-8 if no encoding set. */
  110.         if (!$this->soapfault &&
  111.             !$this->_getContentEncoding($this->headers['content-type'])) {
  112.             $this->xml_encoding = SOAP_DEFAULT_ENCODING;
  113.             /* An encoding we don't understand, return a fault. */
  114.             $this->_raiseSoapFault('Unsupported encoding, use one of ISO-8859-1, US-ASCII, UTF-8''''''Server');
  115.         }
  116.  
  117.         if ($this->soapfault{
  118.             return $this->soapfault->getFault();
  119.         }
  120.  
  121.         $client =new SOAP_Client(null);
  122.  
  123.         return $client->__parse($data$this->xml_encoding$this->attachments);
  124.     }
  125.  
  126.     function service(&$data$endpoint ''$send_response = true,
  127.                      $dump = false)
  128.     {
  129.         $this->endpoint = $endpoint;
  130.         $attachments = array();
  131.         $headers = array();
  132.  
  133.         /* If neither matches, we'll just try it anyway. */
  134.         if (stristr($data'Content-Type: application/dime')) {
  135.             $this->_decodeDIMEMessage($data$this->headers$attachments);
  136.             $useEncoding 'DIME';
  137.         elseif (stristr($data'MIME-Version:')) {
  138.             /* This is a mime message, let's decode it. */
  139.             $this->_decodeMimeMessage($data$this->headers$attachments);
  140.             $useEncoding 'Mime';
  141.         else {
  142.             /* The old fallback, but decodeMimeMessage handles things fine. */
  143.             $this->_parseEmail($data);
  144.         }
  145.  
  146.         /* Get the character encoding of the incoming request treat incoming
  147.          * data as UTF-8 if no encoding set. */
  148.         if (!$response &&
  149.             !$this->_getContentEncoding($this->headers['content-type'])) {
  150.             $this->xml_encoding = SOAP_DEFAULT_ENCODING;
  151.             /* An encoding we don't understand, return a fault. */
  152.             $this->_raiseSoapFault('Unsupported encoding, use one of ISO-8859-1, US-ASCII, UTF-8''''''Server');
  153.             $response $this->getFaultMessage();
  154.         }
  155.  
  156.         if ($this->soapfault{
  157.             $response $this->soapfault->message();
  158.         else {
  159.             $soap_msg $this->parseRequest($data,$attachments);
  160.  
  161.             /* Handle Mime or DIME encoding. */
  162.             /* TODO: DIME Encoding should move to the transport, do it here
  163.              * for now and for ease of getting it done. */
  164.             if (count($this->__attachments)) {
  165.                 if ($useEncoding == 'Mime'{
  166.                     $soap_msg $this->_makeMimeMessage($soap_msg);
  167.                 else {
  168.                     /* Default is DIME. */
  169.                     $soap_msg $this->_makeDIMEMessage($soap_msg);
  170.                     $header['Content-Type''application/dime';
  171.                 }
  172.                 if (PEAR::isError($soap_msg)) {
  173.                     return $this->raiseSoapFault($soap_msg);
  174.                 }
  175.             }
  176.  
  177.             if (is_array($soap_msg)) {
  178.                 $response $soap_msg['body'];
  179.                 if (count($soap_msg['headers'])) {
  180.                     $headers $soap_msg['headers'];
  181.                 }
  182.             else {
  183.                 $response $soap_msg;
  184.             }
  185.         }
  186.  
  187.         if ($this->send_response{
  188.             if ($dump{
  189.                 print $response;
  190.             else {
  191.                 $from array_key_exists('reply-to'$this->headers$this->headers['reply-to'$this->headers['from'];
  192.  
  193.                 $soap_transport =SOAP_Transport::getTransport('mailto:' $from$this->response_encoding);
  194.                 $from $this->endpoint ? $this->endpoint : $this->headers['to'];
  195.                 $headers['In-Reply-To'$this->headers['message-id'];
  196.                 $options = array('from' => $from'subject' => $this->headers['subject']'headers' => $headers);
  197.                 $soap_transport->send($response$options);
  198.             }
  199.         }
  200.     }
  201. }

Documentation generated on Mon, 11 Mar 2019 14:20:03 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.