b98794ca |
1 | <?php |
2 | /*************************************************************************** |
5ddeb07c |
3 | * Copyright (C) 2003-2007 Polytechnique.org * |
b98794ca |
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 | |
b98794ca |
24 | /** Classe de mail avec corps en templates. |
25 | */ |
26 | class PlMail extends Smarty |
27 | { |
28 | private $tpl; |
29 | private $mailer = null; |
30 | |
b2192733 |
31 | function __construct($tpl) |
b98794ca |
32 | { |
33 | global $globals; |
34 | $this->tpl = $tpl; |
b98794ca |
35 | $this->caching = false; |
36 | $this->compile_check = true; |
37 | |
38 | $this->template_dir = $globals->spoolroot . "/templates/"; |
d635addc |
39 | $this->compile_dir = $globals->spoolroot . "/spool/mails_c/"; |
b98794ca |
40 | $this->config_dir = $globals->spoolroot . "/configs/"; |
eaf30d86 |
41 | |
b98794ca |
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')); |
11f44323 |
49 | $this->register_function('add_header', Array($this, 'addHeader')); |
b98794ca |
50 | } |
51 | |
b2192733 |
52 | public static function &get(&$mailer, $tpl) |
53 | { |
54 | static $plmail; |
55 | if (!isset($plmail) || $plmail->tpl != $tpl) { |
56 | $plmail = new PlMail($tpl); |
57 | } |
58 | $plmail->mailer =& $mailer; |
59 | return $plmail; |
60 | } |
61 | |
5b21237d |
62 | public function run($version) |
b98794ca |
63 | { |
5b21237d |
64 | $this->assign('mail_part', $version); |
a2446af5 |
65 | $text = $this->fetch($this->tpl); |
7cbf5d4b |
66 | if ($version == 'text') { |
67 | return wordwrap($text, 78); |
68 | } |
a2446af5 |
69 | return $text; |
b98794ca |
70 | } |
71 | |
b98794ca |
72 | /** used to remove the empty lines due to {from ...}, {to ...} ... functions */ |
5bc6e9e6 |
73 | static public function mail_format($output, &$smarty) |
b98794ca |
74 | { |
7cbf5d4b |
75 | return "\n".trim($output)."\n"; |
b98794ca |
76 | } |
77 | |
5bc6e9e6 |
78 | static protected function format_addr(&$params) |
b98794ca |
79 | { |
80 | if (isset($params['full'])) { |
81 | return $params['full']; |
82 | } elseif (empty($params['text'])) { |
83 | return $params['addr']; |
84 | } else { |
85 | return $params['text'].' <'.$params['addr'].'>'; |
86 | } |
87 | } |
88 | |
b98794ca |
89 | /** template function : from. |
90 | * {from full=...} for an already formatted address |
91 | * {from addr=... [text=...]} else |
92 | */ |
5bc6e9e6 |
93 | public function setFrom($params, &$smarty) |
b98794ca |
94 | { |
5bc6e9e6 |
95 | $smarty->mailer->setFrom(PlMail::format_addr($params)); |
b98794ca |
96 | } |
97 | |
b98794ca |
98 | /** template function : to. |
99 | * {to full=...} for an already formatted address |
100 | * {to addr=... [text=...]} else |
101 | */ |
5bc6e9e6 |
102 | public function addTo($params, &$smarty) |
b98794ca |
103 | { |
5bc6e9e6 |
104 | $smarty->mailer->addTo(PlMail::format_addr($params)); |
b98794ca |
105 | } |
106 | |
b98794ca |
107 | /** template function : cc. |
108 | * {cc full=...} for an already formatted address |
109 | * {cc addr=... [text=...]} else |
110 | */ |
5bc6e9e6 |
111 | public function addCc($params, &$smarty) |
b98794ca |
112 | { |
5bc6e9e6 |
113 | $smarty->mailer->addCc(PlMail::format_addr($params)); |
b98794ca |
114 | } |
115 | |
b98794ca |
116 | /** template function : bcc. |
117 | * {bcc full=...} for an already formatted address |
118 | * {bcc addr=... [text=...]} else |
119 | */ |
5bc6e9e6 |
120 | public function addBcc($params, &$smarty) |
b98794ca |
121 | { |
5bc6e9e6 |
122 | $smarty->mailer->addBcc(PlMail::format_addr($params)); |
b98794ca |
123 | } |
124 | |
b98794ca |
125 | /** template function : subject. |
eaf30d86 |
126 | * {subject text=...} |
b98794ca |
127 | */ |
5bc6e9e6 |
128 | public function setSubject($params, &$smarty) |
b98794ca |
129 | { |
130 | $smarty->mailer->setSubject($params['text']); |
131 | } |
132 | |
11f44323 |
133 | /** template function : add_header. |
134 | * {add_header name=... value=...} |
135 | */ |
5bc6e9e6 |
136 | public function addHeader($params, &$smarty) |
11f44323 |
137 | { |
138 | $smarty->mailer->addHeader($params['name'], $params['value']); |
139 | } |
b98794ca |
140 | } |
b98794ca |
141 | |
142 | require_once('Mail.php'); |
143 | require_once('Mail/mime.php'); |
144 | |
b98794ca |
145 | /** Class for sending inline or multipart-emails. |
5bc6e9e6 |
146 | * Based on Diogenes' HermesMailer |
b98794ca |
147 | */ |
148 | class PlMailer extends Mail_Mime { |
149 | |
150 | private $mail; |
151 | private $page = null; |
152 | private $charset; |
5b21237d |
153 | private $wiki = null; |
b98794ca |
154 | |
a7de4ef7 |
155 | function __construct($tpl = null, $charset = "UTF-8") |
b98794ca |
156 | { |
157 | $this->charset = $charset; |
158 | $this->Mail_Mime("\n"); |
a14159bf |
159 | $this->mail = Mail::factory('sendmail', Array('sendmail_args' => '-oi')); |
b98794ca |
160 | if (!is_null($tpl)) { |
b2192733 |
161 | $this->page =& PlMail::get($this, $tpl); |
b98794ca |
162 | } |
163 | } |
164 | |
b98794ca |
165 | /** |
166 | * converts all : Foo Bar Baz <quux@foobar.org> into "Foo Bar Baz" <quux@foobar.org> which is RFC compliant |
167 | */ |
b98794ca |
168 | private function correct_emails($email) |
169 | { |
a14159bf |
170 | return preg_replace('!(^|, *)([^<"]+?) *(<[^>]*>)!u', '\1"\2" \3', $email); |
b98794ca |
171 | } |
172 | |
5bc6e9e6 |
173 | public function addTo($email) |
b98794ca |
174 | { |
175 | $email = $this->correct_emails($email); |
176 | if (isset($this->_headers['To'])) { |
177 | $this->_headers['To'] .= ", $email"; |
178 | } else { |
179 | $this->_headers['To'] = $email; |
180 | } |
181 | } |
182 | |
5bc6e9e6 |
183 | public function addCc($email) |
b98794ca |
184 | { |
185 | return parent::addCc($this->correct_emails($email)); |
186 | } |
187 | |
5bc6e9e6 |
188 | public function addBcc($email) |
b98794ca |
189 | { |
190 | return parent::addBcc($this->correct_emails($email)); |
191 | } |
192 | |
5bc6e9e6 |
193 | public function setFrom($email) |
b98794ca |
194 | { |
195 | return parent::setFrom($this->correct_emails($email)); |
196 | } |
197 | |
5bc6e9e6 |
198 | public function addHeader($hdr,$val) |
b98794ca |
199 | { |
200 | switch($hdr) { |
201 | case 'From': |
202 | $this->setFrom($val); |
203 | break; |
204 | |
205 | case 'To': |
206 | unset($this->_headers[$hdr]); |
207 | $this->addTo($val); |
208 | break; |
209 | |
210 | case 'Cc': |
211 | unset($this->_headers[$hdr]); |
212 | $this->addCc($val); |
213 | break; |
214 | |
215 | case 'Bcc': |
216 | unset($this->_headers[$hdr]); |
217 | $this->addBcc($val); |
218 | break; |
219 | |
220 | default: |
221 | $this->headers(Array($hdr=>$val)); |
222 | } |
223 | } |
224 | |
da419622 |
225 | public function addUploadAttachment(PlUpload &$upload, $name) |
226 | { |
227 | $encoding = $upload->isType('text') ? 'quoted-printable' : 'base64'; |
228 | $this->addAttachment($upload->getContents(), $upload->contentType(), $name, false, $encoding); |
229 | } |
230 | |
5bc6e9e6 |
231 | public function assign($var, $value) |
b98794ca |
232 | { |
233 | if (!is_null($this->page)) { |
234 | $this->page->assign($var, $value); |
235 | } |
236 | } |
eaf30d86 |
237 | |
5bc6e9e6 |
238 | public function assign_by_ref($var, &$value) |
11f44323 |
239 | { |
240 | if (!is_null($this->page)) { |
241 | $this->page->assign_by_ref($var, $value); |
242 | } |
243 | } |
244 | |
5bc6e9e6 |
245 | public function register_modifier($var, $callback) |
11f44323 |
246 | { |
247 | if (!is_null($this->page)) { |
248 | $this->page->register_modifier($var, $callback); |
249 | } |
250 | } |
eaf30d86 |
251 | |
5bc6e9e6 |
252 | public function register_function($var, $callback) |
11f44323 |
253 | { |
254 | if (!is_null($this->page)) { |
255 | $this->page->register_function($var, $callback); |
256 | } |
257 | } |
5b21237d |
258 | |
259 | public function setWikiBody($wiki) |
260 | { |
261 | $this->wiki = $wiki; |
262 | } |
ef42a9d6 |
263 | |
11f44323 |
264 | private function processPage($with_html = true) |
b98794ca |
265 | { |
266 | if (!is_null($this->page)) { |
5b21237d |
267 | $level = error_reporting(0); |
268 | $this->page->run('head'); // process page headers |
269 | $this->wiki = trim($this->page->run('wiki')); // get wiki |
270 | if (!$this->wiki) { |
271 | $this->setTxtBody($this->page->run('text')); |
272 | if ($with_html) { |
273 | $html = trim($this->page->run('html')); |
274 | if (!empty($html)) { |
275 | $this->setHtmlBody($html); |
276 | } |
11f44323 |
277 | } |
b98794ca |
278 | } |
5b21237d |
279 | error_reporting($level); |
280 | } |
281 | if ($this->wiki) { |
282 | $this->setTxtBody(MiniWiki::WikiToText($this->wiki, true, 0, 78)); |
283 | if ($with_html) { |
284 | $this->setHtmlBody(MiniWiki::WikiToHtml($this->wiki, true)); |
285 | } |
b98794ca |
286 | } |
287 | } |
288 | |
5bc6e9e6 |
289 | public function send($with_html = true) |
b98794ca |
290 | { |
11f44323 |
291 | $this->processPage($with_html); |
b98794ca |
292 | if (S::v('forlife')) { |
315fa6ae |
293 | global $globals; |
294 | $this->addHeader('X-Org-Mail', S::v('forlife') . '@' . $globals->mail->domain); |
b98794ca |
295 | } |
296 | $addrs = Array(); |
297 | foreach(Array('To', 'Cc', 'Bcc') as $hdr) { |
298 | if(isset($this->_headers[$hdr])) { |
299 | require_once 'Mail/RFC822.php'; |
c77fed74 |
300 | $parsed = @Mail_RFC822::parseAddressList($this->_headers[$hdr]); |
301 | if (is_array($parsed)) { |
302 | $addrs = array_merge($addrs, $parsed); |
303 | } |
b98794ca |
304 | } |
305 | } |
306 | if(empty($addrs)) { |
307 | return false; |
308 | } |
eaf30d86 |
309 | |
b98794ca |
310 | $dests = Array(); |
311 | foreach($addrs as $a) { |
312 | $dests[] = "{$a->mailbox}@{$a->host}"; |
313 | } |
eaf30d86 |
314 | |
b98794ca |
315 | // very important to do it in THIS order very precisely. |
316 | $body = $this->get(array('text_charset' => $this->charset, |
d77ad560 |
317 | 'text_encoding' => '8bit', |
b98794ca |
318 | 'html_charset' => $this->charset, |
319 | 'head_charset' => $this->charset)); |
320 | $hdrs = $this->headers(); |
434cd6dd |
321 | if (empty($hdrs['From'])) { |
322 | trigger_error('Empty "From", mail not sent', E_USER_WARNING); |
323 | return false; |
324 | } |
b98794ca |
325 | return $this->mail->send($dests, $hdrs, $body); |
326 | } |
b98794ca |
327 | } |
328 | |
a7de4ef7 |
329 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
b98794ca |
330 | ?> |