Improved protection from mass bot registrations.
[timetracker.git] / WEB-INF / lib / pear / Mail.php
1 <?php
2 /**
3  * PEAR's Mail:: interface.
4  *
5  * PHP version 5
6  *
7  * LICENSE:
8  *
9  * Copyright (c) 2002-2007, Richard Heyes
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  *
16  * o Redistributions of source code must retain the above copyright
17  *   notice, this list of conditions and the following disclaimer.
18  * o Redistributions in binary form must reproduce the above copyright
19  *   notice, this list of conditions and the following disclaimer in the
20  *   documentation and/or other materials provided with the distribution.
21  * o The names of the authors may not be used to endorse or promote
22  *   products derived from this software without specific prior written
23  *   permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @category    Mail
38  * @package     Mail
39  * @author      Chuck Hagenbuch <chuck@horde.org>
40  * @copyright   1997-2010 Chuck Hagenbuch
41  * @license     http://opensource.org/licenses/bsd-license.php New BSD License
42  * @version     CVS: $Id$
43  * @link        http://pear.php.net/package/Mail/
44  */
45
46 require_once 'PEAR.php';
47
48 /**
49  * PEAR's Mail:: interface. Defines the interface for implementing
50  * mailers under the PEAR hierarchy, and provides supporting functions
51  * useful in multiple mailer backends.
52  *
53  * @version $Revision$
54  * @package Mail
55  */
56 class Mail
57 {
58     /**
59      * Line terminator used for separating header lines.
60      * @var string
61      */
62     public $sep = "\r\n";
63
64     /**
65      * Provides an interface for generating Mail:: objects of various
66      * types
67      *
68      * @param string $driver The kind of Mail:: object to instantiate.
69      * @param array  $params The parameters to pass to the Mail:: object.
70      *
71      * @return object Mail a instance of the driver class or if fails a PEAR Error
72      */
73     public static function factory($driver, $params = array())
74     {
75         $driver = strtolower($driver);
76         @include_once 'Mail/' . $driver . '.php';
77         $class = 'Mail_' . $driver;
78         if (class_exists($class)) {
79             $mailer = new $class($params);
80             return $mailer;
81         } else {
82             return PEAR::raiseError('Unable to find class for driver ' . $driver);
83         }
84     }
85
86     /**
87      * Implements Mail::send() function using php's built-in mail()
88      * command.
89      *
90      * @param mixed $recipients Either a comma-seperated list of recipients
91      *              (RFC822 compliant), or an array of recipients,
92      *              each RFC822 valid. This may contain recipients not
93      *              specified in the headers, for Bcc:, resending
94      *              messages, etc.
95      *
96      * @param array $headers The array of headers to send with the mail, in an
97      *              associative array, where the array key is the
98      *              header name (ie, 'Subject'), and the array value
99      *              is the header value (ie, 'test'). The header
100      *              produced from those values would be 'Subject:
101      *              test'.
102      *
103      * @param string $body The full text of the message body, including any
104      *               Mime parts, etc.
105      *
106      * @return mixed Returns true on success, or a PEAR_Error
107      *               containing a descriptive error message on
108      *               failure.
109      *
110      * @deprecated use Mail_mail::send instead
111      */
112     public function send($recipients, $headers, $body)
113     {
114         if (!is_array($headers)) {
115             return PEAR::raiseError('$headers must be an array');
116         }
117
118         $result = $this->_sanitizeHeaders($headers);
119         if (is_a($result, 'PEAR_Error')) {
120             return $result;
121         }
122
123         // if we're passed an array of recipients, implode it.
124         if (is_array($recipients)) {
125             $recipients = implode(', ', $recipients);
126         }
127
128         // get the Subject out of the headers array so that we can
129         // pass it as a seperate argument to mail().
130         $subject = '';
131         if (isset($headers['Subject'])) {
132             $subject = $headers['Subject'];
133             unset($headers['Subject']);
134         }
135
136         // flatten the headers out.
137         list(, $text_headers) = Mail::prepareHeaders($headers);
138
139         return mail($recipients, $subject, $body, $text_headers);
140     }
141
142     /**
143      * Sanitize an array of mail headers by removing any additional header
144      * strings present in a legitimate header's value.  The goal of this
145      * filter is to prevent mail injection attacks.
146      *
147      * @param array $headers The associative array of headers to sanitize.
148      */
149     protected function _sanitizeHeaders(&$headers)
150     {
151         foreach ($headers as $key => $value) {
152             $headers[$key] =
153                 preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i',
154                              null, $value);
155         }
156     }
157
158     /**
159      * Take an array of mail headers and return a string containing
160      * text usable in sending a message.
161      *
162      * @param array $headers The array of headers to prepare, in an associative
163      *              array, where the array key is the header name (ie,
164      *              'Subject'), and the array value is the header
165      *              value (ie, 'test'). The header produced from those
166      *              values would be 'Subject: test'.
167      *
168      * @return mixed Returns false if it encounters a bad address,
169      *               otherwise returns an array containing two
170      *               elements: Any From: address found in the headers,
171      *               and the plain text version of the headers.
172      */
173     protected function prepareHeaders($headers)
174     {
175         $lines = array();
176         $from = null;
177
178         foreach ($headers as $key => $value) {
179             if (strcasecmp($key, 'From') === 0) {
180                 include_once 'Mail/RFC822.php';
181                 $parser = new Mail_RFC822();
182                 $addresses = $parser->parseAddressList($value, 'localhost', false);
183                 if (is_a($addresses, 'PEAR_Error')) {
184                     return $addresses;
185                 }
186
187                 $from = $addresses[0]->mailbox . '@' . $addresses[0]->host;
188
189                 // Reject envelope From: addresses with spaces.
190                 if (strstr($from, ' ')) {
191                     return false;
192                 }
193
194                 $lines[] = $key . ': ' . $value;
195             } elseif (strcasecmp($key, 'Received') === 0) {
196                 $received = array();
197                 if (is_array($value)) {
198                     foreach ($value as $line) {
199                         $received[] = $key . ': ' . $line;
200                     }
201                 }
202                 else {
203                     $received[] = $key . ': ' . $value;
204                 }
205                 // Put Received: headers at the top.  Spam detectors often
206                 // flag messages with Received: headers after the Subject:
207                 // as spam.
208                 $lines = array_merge($received, $lines);
209             } else {
210                 // If $value is an array (i.e., a list of addresses), convert
211                 // it to a comma-delimited string of its elements (addresses).
212                 if (is_array($value)) {
213                     $value = implode(', ', $value);
214                 }
215                 $lines[] = $key . ': ' . $value;
216             }
217         }
218
219         return array($from, join($this->sep, $lines));
220     }
221
222     /**
223      * Take a set of recipients and parse them, returning an array of
224      * bare addresses (forward paths) that can be passed to sendmail
225      * or an smtp server with the rcpt to: command.
226      *
227      * @param mixed Either a comma-seperated list of recipients
228      *              (RFC822 compliant), or an array of recipients,
229      *              each RFC822 valid.
230      *
231      * @return mixed An array of forward paths (bare addresses) or a PEAR_Error
232      *               object if the address list could not be parsed.
233      */
234     protected function parseRecipients($recipients)
235     {
236         include_once 'Mail/RFC822.php';
237
238         // if we're passed an array, assume addresses are valid and
239         // implode them before parsing.
240         if (is_array($recipients)) {
241             $recipients = implode(', ', $recipients);
242         }
243
244         // Parse recipients, leaving out all personal info. This is
245         // for smtp recipients, etc. All relevant personal information
246         // should already be in the headers.
247         $Mail_RFC822 = new Mail_RFC822();
248         $addresses = $Mail_RFC822->parseAddressList($recipients, 'localhost', false);
249
250         // If parseAddressList() returned a PEAR_Error object, just return it.
251         if (is_a($addresses, 'PEAR_Error')) {
252             return $addresses;
253         }
254
255         $recipients = array();
256         if (is_array($addresses)) {
257             foreach ($addresses as $ob) {
258                 $recipients[] = $ob->mailbox . '@' . $ob->host;
259             }
260         }
261
262         return $recipients;
263     }
264
265 }