Add new PlSubIterator
[platal.git] / classes / plmailer.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 require_once('smarty/libs/Smarty.class.php');
23
24 /** Classe de mail avec corps en templates.
25 */
26 class PlMail extends Smarty
27 {
28 private $tpl;
29 private $mailer = null;
30
31 function __construct($tpl)
32 {
33 global $globals;
34 $this->tpl = $tpl;
35 $this->caching = false;
36 $this->compile_check = true;
37
38 $this->template_dir = $globals->spoolroot . "/templates/";
39 $this->compile_dir = $globals->spoolroot . "/spool/mails_c/";
40 $this->config_dir = $globals->spoolroot . "/configs/";
41 array_unshift($this->plugins_dir, $globals->spoolroot."/plugins/");
42
43 $this->register_outputfilter(Array($this, 'mail_format'));
44 $this->register_function('from', Array($this, 'setFrom'));
45 $this->register_function('to', Array($this, 'addTo'));
46 $this->register_function('cc', Array($this, 'addCc'));
47 $this->register_function('bcc', Array($this, 'addBcc'));
48 $this->register_function('subject', Array($this, 'setSubject'));
49 $this->register_function('add_header', Array($this, 'addHeader'));
50 $this->assign_by_ref('globals', $globals);
51 }
52
53 public static function &get(&$mailer, $tpl)
54 {
55 static $plmail;
56 if (!isset($plmail) || $plmail->tpl != $tpl) {
57 $plmail = new PlMail($tpl);
58 }
59 $plmail->mailer =& $mailer;
60 return $plmail;
61 }
62
63 public function run($version)
64 {
65 $this->assign('mail_part', $version);
66 $text = $this->fetch($this->tpl);
67 if ($version == 'text') {
68 return wordwrap($text, 78);
69 }
70 return $text;
71 }
72
73 /** used to remove the empty lines due to {from ...}, {to ...} ... functions */
74 static public function mail_format($output, &$smarty)
75 {
76 return "\n".trim($output)."\n";
77 }
78
79 static protected function format_addr(&$params)
80 {
81 if (isset($params['full'])) {
82 return $params['full'];
83 } elseif (empty($params['text'])) {
84 return $params['addr'];
85 } else {
86 return $params['text'].' <'.$params['addr'].'>';
87 }
88 }
89
90 /** template function : from.
91 * {from full=...} for an already formatted address
92 * {from addr=... [text=...]} else
93 */
94 public function setFrom($params, &$smarty)
95 {
96 $smarty->mailer->setFrom(PlMail::format_addr($params));
97 }
98
99 /** template function : to.
100 * {to full=...} for an already formatted address
101 * {to addr=... [text=...]} else
102 */
103 public function addTo($params, &$smarty)
104 {
105 $smarty->mailer->addTo(PlMail::format_addr($params));
106 }
107
108 /** template function : cc.
109 * {cc full=...} for an already formatted address
110 * {cc addr=... [text=...]} else
111 */
112 public function addCc($params, &$smarty)
113 {
114 $smarty->mailer->addCc(PlMail::format_addr($params));
115 }
116
117 /** template function : bcc.
118 * {bcc full=...} for an already formatted address
119 * {bcc addr=... [text=...]} else
120 */
121 public function addBcc($params, &$smarty)
122 {
123 $smarty->mailer->addBcc(PlMail::format_addr($params));
124 }
125
126 /** template function : subject.
127 * {subject text=...}
128 */
129 public function setSubject($params, &$smarty)
130 {
131 $smarty->mailer->setSubject($params['text']);
132 }
133
134 /** template function : add_header.
135 * {add_header name=... value=...}
136 */
137 public function addHeader($params, &$smarty)
138 {
139 $smarty->mailer->addHeader($params['name'], $params['value']);
140 }
141 }
142
143 require_once('Mail.php');
144 require_once('Mail/mime.php');
145
146 /** Class for sending inline or multipart-emails.
147 * Based on Diogenes' HermesMailer
148 */
149 class PlMailer extends Mail_Mime {
150
151 private $mail;
152 private $page = null;
153 private $charset;
154 private $wiki = null;
155
156 function __construct($tpl = null, $charset = "UTF-8")
157 {
158 $this->charset = $charset;
159 $this->Mail_Mime("\n");
160 $this->mail = Mail::factory('sendmail', Array('sendmail_args' => '-oi'));
161 if (!is_null($tpl)) {
162 $this->page =& PlMail::get($this, $tpl);
163 }
164 }
165
166 static private function formatUser(PlUser $user)
167 {
168 return '"' . $user->fullName() . '" <' . $user->bestEmail() . '>';
169 }
170
171 /**
172 * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant
173 */
174 private function correct_emails($email)
175 {
176 if ($email instanceof PlUser) {
177 $email = self::formatUser($email);
178 }
179 return preg_replace('!(^|, *)([^<"]+?) *(<[^>]*>)!u', '\1"\2" \3', $email);
180 }
181
182 public function addTo($email)
183 {
184 $email = $this->correct_emails($email);
185 if (isset($this->_headers['To'])) {
186 $this->_headers['To'] .= ", $email";
187 } else {
188 $this->_headers['To'] = $email;
189 }
190 }
191
192 public function addCc($email)
193 {
194 return parent::addCc($this->correct_emails($email));
195 }
196
197 public function addBcc($email)
198 {
199 return parent::addBcc($this->correct_emails($email));
200 }
201
202 public function setFrom($email)
203 {
204 return parent::setFrom($this->correct_emails($email));
205 }
206
207 static function encodeQP($char)
208 {
209 return sprintf('=%02X', ord($char));
210 }
211
212 public function setSubject($subject)
213 {
214 if (!preg_match('/^[\x20-\x7e]*$/', $subject)) {
215 $subject = '=?UTF-8?Q?' . preg_replace('/[^\x21-\x3C\x3e-\x7e]/e', 'PlMailer::encodeQP("\0")', $subject)
216 . '?=';
217 }
218 return parent::setSubject($subject);
219 }
220
221 public function addHeader($hdr,$val)
222 {
223 switch($hdr) {
224 case 'From':
225 $this->setFrom($val);
226 break;
227
228 case 'To':
229 unset($this->_headers[$hdr]);
230 $this->addTo($val);
231 break;
232
233 case 'Cc':
234 unset($this->_headers[$hdr]);
235 $this->addCc($val);
236 break;
237
238 case 'Bcc':
239 unset($this->_headers[$hdr]);
240 $this->addBcc($val);
241 break;
242
243 default:
244 $this->headers(Array($hdr=>$val));
245 }
246 }
247
248 public function addUploadAttachment(PlUpload &$upload, $name)
249 {
250 $encoding = $upload->isType('text') ? 'quoted-printable' : 'base64';
251 $this->addAttachment($upload->getContents(), $upload->contentType(), $name, false, $encoding);
252 }
253
254 public function assign($var, $value)
255 {
256 if (!is_null($this->page)) {
257 $this->page->assign($var, $value);
258 }
259 }
260
261 public function assign_by_ref($var, &$value)
262 {
263 if (!is_null($this->page)) {
264 $this->page->assign_by_ref($var, $value);
265 }
266 }
267
268 public function register_modifier($var, $callback)
269 {
270 if (!is_null($this->page)) {
271 $this->page->register_modifier($var, $callback);
272 }
273 }
274
275 public function register_function($var, $callback)
276 {
277 if (!is_null($this->page)) {
278 $this->page->register_function($var, $callback);
279 }
280 }
281
282 public function setWikiBody($wiki)
283 {
284 $this->wiki = $wiki;
285 }
286
287 private function processPage($with_html = true)
288 {
289 if (!is_null($this->page)) {
290 global $globals;
291 if (!($globals->debug & DEBUG_SMARTY)) {
292 $level = error_reporting(0);
293 }
294 $this->page->assign_by_ref('globals', $globals);
295 $this->page->run('head'); // process page headers
296 $this->wiki = trim($this->page->run('wiki')); // get wiki
297 if (!$this->wiki) {
298 $this->setTxtBody($this->page->run('text'));
299 if ($with_html) {
300 $html = trim($this->page->run('html'));
301 if (!empty($html)) {
302 $this->setHtmlBody($html);
303 }
304 }
305 }
306 if (!($globals->debug & DEBUG_SMARTY)) {
307 error_reporting($level);
308 }
309 }
310 if ($this->wiki) {
311 $this->setTxtBody(MiniWiki::WikiToText($this->wiki, false, 0, 78));
312 if ($with_html) {
313 $this->setHtmlBody('<html><body>' . MiniWiki::WikiToHtml($this->wiki, true) . '</body></html>');
314 }
315 }
316 }
317
318 public function sendTo(PlUser &$user)
319 {
320 $this->addTo($user);
321 $this->assign_by_ref('user', $user);
322 return $this->send($user->isEmailFormatHtml());
323 }
324
325 public function send($with_html = true)
326 {
327 $this->processPage($with_html);
328 if (S::user()) {
329 $this->addHeader('X-Org-Mail', S::user()->forlifeEmail());
330 }
331 $addrs = Array();
332 foreach (Array('To', 'Cc', 'Bcc') as $hdr) {
333 if (isset($this->_headers[$hdr])) {
334 require_once 'Mail/RFC822.php';
335 $parsed = @Mail_RFC822::parseAddressList($this->_headers[$hdr]);
336 if (is_array($parsed)) {
337 $addrs = array_merge($addrs, $parsed);
338 }
339 }
340 }
341 if (empty($addrs)) {
342 return false;
343 }
344
345 $dests = Array();
346 foreach ($addrs as $a) {
347 $dests[] = "{$a->mailbox}@{$a->host}";
348 }
349
350 // Support for a "catch-all" email address, to be used by developers.
351 // This mode can only be activated when the working copy is in restricted
352 // mode, to ensure that production plat/al copies are never affected.
353 global $globals;
354 if ($globals->email_catchall && $globals->core->restricted_platal) {
355 require_once 'Mail/RFC822.php';
356 if (@Mail_RFC822::isValidInetAddress($globals->email_catchall)) {
357 $dests = array($globals->email_catchall);
358 }
359 }
360
361 // very important to do it in THIS order very precisely.
362 $body = $this->get(array('text_charset' => $this->charset,
363 'text_encoding' => '8bit',
364 'html_charset' => $this->charset,
365 'head_charset' => $this->charset));
366 $hdrs = $this->headers();
367 if (empty($hdrs['From'])) {
368 trigger_error('Empty "From", mail not sent', E_USER_WARNING);
369 return false;
370 }
371 return $this->mail->send($dests, $hdrs, $body);
372 }
373 }
374
375 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
376 ?>