Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * This file contains the code for an SMTP transport layer.
4
 *
5
 * This code is still a rough and untested draft.
6
 * TODO:
7
 *  switch to pear mail stuff
8
 *  smtp authentication
9
 *  smtp ssl support
10
 *  ability to define smtp options (encoding, from, etc.)
11
 *
12
 * PHP versions 4 and 5
13
 *
14
 * LICENSE: This source file is subject to version 2.02 of the PHP license,
15
 * that is bundled with this package in the file LICENSE, and is available at
16
 * through the world-wide-web at http://www.php.net/license/2_02.txt.  If you
17
 * did not receive a copy of the PHP license and are unable to obtain it
18
 * through the world-wide-web, please send a note to license@php.net so we can
19
 * mail you a copy immediately.
20
 *
21
 * @category   Web Services
22
 * @package    SOAP
23
 * @author     Shane Caraveo <Shane@Caraveo.com>
24
 * @author     Jan Schneider <jan@horde.org>
25
 * @copyright  2003-2006 The PHP Group
26
 * @license    http://www.php.net/license/2_02.txt  PHP License 2.02
27
 * @link       http://pear.php.net/package/SOAP
28
 */
29
 
30
require_once 'SOAP/Transport.php';
31
require_once 'Mail/smtp.php';
32
 
33
/**
34
 * SMTP Transport for SOAP
35
 *
36
 * Implements SOAP-SMTP as defined at
37
 * http://www.pocketsoap.com/specs/smtpbinding/
38
 *
39
 * @todo use PEAR smtp and Mime classes
40
 *
41
 * @access public
42
 * @package SOAP
43
 * @author Shane Caraveo <shane@php.net>
44
 * @author Jan Schneider <jan@horde.org>
45
 */
46
class SOAP_Transport_SMTP extends SOAP_Transport
47
{
48
    var $credentials = '';
49
    var $timeout = 4; // connect timeout
50
    var $host = '127.0.0.1';
51
    var $port = 25;
52
    var $auth = null;
53
 
54
    /**
55
     * SOAP_Transport_SMTP Constructor
56
     *
57
     * @param string $url  mailto: address.
58
     *
59
     * @access public
60
     */
61
    function SOAP_Transport_SMTP($url, $encoding = 'US-ASCII')
62
    {
63
        parent::SOAP_Base('SMTP');
64
        $this->encoding = $encoding;
65
        $this->urlparts = @parse_url($url);
66
        $this->url = $url;
67
    }
68
 
69
    /**
70
     * Sends and receives SOAP data.
71
     *
72
     * @access public
73
     *
74
     * @param string  Outgoing SOAP data.
75
     * @param array   Options.
76
     *
77
     * @return string|SOAP_Fault
78
     */
79
    function send($msg, $options = array())
80
    {
81
        $this->fault = null;
82
        $this->incoming_payload = '';
83
        $this->outgoing_payload = $msg;
84
        if (!$this->_validateUrl()) {
85
            return $this->fault;
86
        }
87
        if (!$options || !isset($options['from'])) {
88
            return $this->_raiseSoapFault('No From: address to send message with');
89
        }
90
 
91
        if (isset($options['host'])) $this->host = $options['host'];
92
        if (isset($options['port'])) $this->port = $options['port'];
93
        if (isset($options['auth'])) $this->auth = $options['auth'];
94
        if (isset($options['username'])) $this->username = $options['username'];
95
        if (isset($options['password'])) $this->password = $options['password'];
96
 
97
        $headers = array();
98
        $headers['From'] = $options['from'];
99
        $headers['X-Mailer'] = $this->_userAgent;
100
        $headers['MIME-Version'] = '1.0';
101
        $headers['Message-ID'] = md5(time()) . '.soap@' . $this->host;
102
        $headers['To'] = $this->urlparts['path'];
103
        if (isset($options['soapaction'])) {
104
            $headers['Soapaction'] = "\"{$options['soapaction']}\"";
105
        }
106
 
107
        if (isset($options['headers']))
108
            $headers = array_merge($headers, $options['headers']);
109
 
110
        // If the content type is already set, we assume that MIME encoding is
111
        // already done.
112
        if (isset($headers['Content-Type'])) {
113
            $out = $msg;
114
        } else {
115
            // Do a simple inline MIME encoding.
116
            $headers['Content-Disposition'] = 'inline';
117
            $headers['Content-Type'] = "text/xml; charset=\"$this->encoding\"";
118
            if (isset($options['transfer-encoding'])) {
119
                if (strcasecmp($options['transfer-encoding'], 'quoted-printable') == 0) {
120
                    $headers['Content-Transfer-Encoding'] = $options['transfer-encoding'];
121
                    $out = $msg;
122
                } elseif (strcasecmp($options['transfer-encoding'],'base64') == 0) {
123
                    $headers['Content-Transfer-Encoding'] = 'base64';
124
                    $out = chunk_split(base64_encode($msg), 76, "\n");
125
                } else {
126
                    return $this->_raiseSoapFault("Invalid Transfer Encoding: {$options['transfer-encoding']}");
127
                }
128
            } else {
129
                // Default to base64.
130
                $headers['Content-Transfer-Encoding'] = 'base64';
131
                $out = chunk_split(base64_encode($msg));
132
            }
133
        }
134
 
135
        $headers['Subject'] = isset($options['subject']) ? $options['subject'] : 'SOAP Message';
136
 
137
        foreach ($headers as $key => $value) {
138
            $header_text .= "$key: $value\n";
139
        }
140
        $this->outgoing_payload = $header_text . "\r\n" . $this->outgoing_payload;
141
 
142
        $mailer_params = array(
143
            'host' => $this->host,
144
            'port' => $this->port,
145
            'username' => $this->username,
146
            'password' => $this->password,
147
            'auth' => $this->auth
148
        );
149
        $mailer = new Mail_smtp($mailer_params);
150
        $result = $mailer->send($this->urlparts['path'], $headers, $out);
151
        if (!PEAR::isError($result)) {
152
            $val = new SOAP_Value('Message-ID', 'string', $headers['Message-ID']);
153
        } else {
154
            $sval[] = new SOAP_Value('faultcode', 'QName', SOAP_BASE::SOAPENVPrefix().':Client');
155
            $sval[] = new SOAP_Value('faultstring', 'string', "couldn't send SMTP message to {$this->urlparts['path']}");
156
            $val = new SOAP_Value('Fault', 'Struct', $sval);
157
        }
158
 
159
        $methodValue = new SOAP_Value('Response', 'Struct', array($val));
160
 
161
        $this->incoming_payload = $this->makeEnvelope($methodValue,
162
                                                      $this->headers,
163
                                                      $this->encoding);
164
 
165
        return $this->incoming_payload;
166
    }
167
 
168
    /**
169
     * Sets data for HTTP authentication, creates Authorization header.
170
     *
171
     * @param string $username  Username.
172
     * @param string $password  Response data, minus HTTP headers.
173
     *
174
     * @access public
175
     */
176
    function setCredentials($username, $password)
177
    {
178
        $this->username = $username;
179
        $this->password = $password;
180
    }
181
 
182
    /**
183
     * Validates url data passed to constructor.
184
     *
185
     * @return boolean
186
     * @access private
187
     */
188
    function _validateUrl()
189
    {
190
        if (!is_array($this->urlparts)) {
191
            $this->_raiseSoapFault("Unable to parse URL $this->url");
192
            return false;
193
        }
194
        if (!isset($this->urlparts['scheme']) ||
195
            strcasecmp($this->urlparts['scheme'], 'mailto') != 0) {
196
                $this->_raiseSoapFault("Unable to parse URL $this->url");
197
                return false;
198
        }
199
        if (!isset($this->urlparts['path'])) {
200
            $this->_raiseSoapFault("Unable to parse URL $this->url");
201
            return false;
202
        }
203
        return true;
204
    }
205
 
206
}