]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/Mail.php
some extlibs that were missing from 0.6.0
[quix0rs-gnu-social.git] / extlib / Mail.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 // | Author: Chuck Hagenbuch <chuck@horde.org>                            |
17 // +----------------------------------------------------------------------+
18 //
19 // $Id: Mail.php,v 1.17 2006/09/15 03:41:18 jon Exp $
20
21 require_once 'PEAR.php';
22
23 /**
24  * PEAR's Mail:: interface. Defines the interface for implementing
25  * mailers under the PEAR hierarchy, and provides supporting functions
26  * useful in multiple mailer backends.
27  *
28  * @access public
29  * @version $Revision: 1.17 $
30  * @package Mail
31  */
32 class Mail
33 {
34     /**
35      * Line terminator used for separating header lines.
36      * @var string
37      */
38     var $sep = "\r\n";
39
40     /**
41      * Provides an interface for generating Mail:: objects of various
42      * types
43      *
44      * @param string $driver The kind of Mail:: object to instantiate.
45      * @param array  $params The parameters to pass to the Mail:: object.
46      * @return object Mail a instance of the driver class or if fails a PEAR Error
47      * @access public
48      */
49     function &factory($driver, $params = array())
50     {
51         $driver = strtolower($driver);
52         @include_once 'Mail/' . $driver . '.php';
53         $class = 'Mail_' . $driver;
54         if (class_exists($class)) {
55             $mailer = new $class($params);
56             return $mailer;
57         } else {
58             return PEAR::raiseError('Unable to find class for driver ' . $driver);
59         }
60     }
61
62     /**
63      * Implements Mail::send() function using php's built-in mail()
64      * command.
65      *
66      * @param mixed $recipients Either a comma-seperated list of recipients
67      *              (RFC822 compliant), or an array of recipients,
68      *              each RFC822 valid. This may contain recipients not
69      *              specified in the headers, for Bcc:, resending
70      *              messages, etc.
71      *
72      * @param array $headers The array of headers to send with the mail, in an
73      *              associative array, where the array key is the
74      *              header name (ie, 'Subject'), and the array value
75      *              is the header value (ie, 'test'). The header
76      *              produced from those values would be 'Subject:
77      *              test'.
78      *
79      * @param string $body The full text of the message body, including any
80      *               Mime parts, etc.
81      *
82      * @return mixed Returns true on success, or a PEAR_Error
83      *               containing a descriptive error message on
84      *               failure.
85      * @access public
86      * @deprecated use Mail_mail::send instead
87      */
88     function send($recipients, $headers, $body)
89     {
90         $this->_sanitizeHeaders($headers);
91
92         // if we're passed an array of recipients, implode it.
93         if (is_array($recipients)) {
94             $recipients = implode(', ', $recipients);
95         }
96
97         // get the Subject out of the headers array so that we can
98         // pass it as a seperate argument to mail().
99         $subject = '';
100         if (isset($headers['Subject'])) {
101             $subject = $headers['Subject'];
102             unset($headers['Subject']);
103         }
104
105         // flatten the headers out.
106         list(,$text_headers) = Mail::prepareHeaders($headers);
107
108         return mail($recipients, $subject, $body, $text_headers);
109
110     }
111
112     /**
113      * Sanitize an array of mail headers by removing any additional header
114      * strings present in a legitimate header's value.  The goal of this
115      * filter is to prevent mail injection attacks.
116      *
117      * @param array $headers The associative array of headers to sanitize.
118      *
119      * @access private
120      */
121     function _sanitizeHeaders(&$headers)
122     {
123         foreach ($headers as $key => $value) {
124             $headers[$key] =
125                 preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i',
126                              null, $value);
127         }
128     }
129
130     /**
131      * Take an array of mail headers and return a string containing
132      * text usable in sending a message.
133      *
134      * @param array $headers The array of headers to prepare, in an associative
135      *              array, where the array key is the header name (ie,
136      *              'Subject'), and the array value is the header
137      *              value (ie, 'test'). The header produced from those
138      *              values would be 'Subject: test'.
139      *
140      * @return mixed Returns false if it encounters a bad address,
141      *               otherwise returns an array containing two
142      *               elements: Any From: address found in the headers,
143      *               and the plain text version of the headers.
144      * @access private
145      */
146     function prepareHeaders($headers)
147     {
148         $lines = array();
149         $from = null;
150
151         foreach ($headers as $key => $value) {
152             if (strcasecmp($key, 'From') === 0) {
153                 include_once 'Mail/RFC822.php';
154                 $parser = &new Mail_RFC822();
155                 $addresses = $parser->parseAddressList($value, 'localhost', false);
156                 if (PEAR::isError($addresses)) {
157                     return $addresses;
158                 }
159
160                 $from = $addresses[0]->mailbox . '@' . $addresses[0]->host;
161
162                 // Reject envelope From: addresses with spaces.
163                 if (strstr($from, ' ')) {
164                     return false;
165                 }
166
167                 $lines[] = $key . ': ' . $value;
168             } elseif (strcasecmp($key, 'Received') === 0) {
169                 $received = array();
170                 if (is_array($value)) {
171                     foreach ($value as $line) {
172                         $received[] = $key . ': ' . $line;
173                     }
174                 }
175                 else {
176                     $received[] = $key . ': ' . $value;
177                 }
178                 // Put Received: headers at the top.  Spam detectors often
179                 // flag messages with Received: headers after the Subject:
180                 // as spam.
181                 $lines = array_merge($received, $lines);
182             } else {
183                 // If $value is an array (i.e., a list of addresses), convert
184                 // it to a comma-delimited string of its elements (addresses).
185                 if (is_array($value)) {
186                     $value = implode(', ', $value);
187                 }
188                 $lines[] = $key . ': ' . $value;
189             }
190         }
191
192         return array($from, join($this->sep, $lines));
193     }
194
195     /**
196      * Take a set of recipients and parse them, returning an array of
197      * bare addresses (forward paths) that can be passed to sendmail
198      * or an smtp server with the rcpt to: command.
199      *
200      * @param mixed Either a comma-seperated list of recipients
201      *              (RFC822 compliant), or an array of recipients,
202      *              each RFC822 valid.
203      *
204      * @return mixed An array of forward paths (bare addresses) or a PEAR_Error
205      *               object if the address list could not be parsed.
206      * @access private
207      */
208     function parseRecipients($recipients)
209     {
210         include_once 'Mail/RFC822.php';
211
212         // if we're passed an array, assume addresses are valid and
213         // implode them before parsing.
214         if (is_array($recipients)) {
215             $recipients = implode(', ', $recipients);
216         }
217
218         // Parse recipients, leaving out all personal info. This is
219         // for smtp recipients, etc. All relevant personal information
220         // should already be in the headers.
221         $addresses = Mail_RFC822::parseAddressList($recipients, 'localhost', false);
222
223         // If parseAddressList() returned a PEAR_Error object, just return it.
224         if (PEAR::isError($addresses)) {
225             return $addresses;
226         }
227
228         $recipients = array();
229         if (is_array($addresses)) {
230             foreach ($addresses as $ob) {
231                 $recipients[] = $ob->mailbox . '@' . $ob->host;
232             }
233         }
234
235         return $recipients;
236     }
237
238 }