1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | file : $Id: functions_mail.inc.php 2139 2007-10-16 20:45:00Z rub $ |
---|
8 | // | last update : $Date: 2007-10-16 20:45:00 +0000 (Tue, 16 Oct 2007) $ |
---|
9 | // | last modifier : $Author: rub $ |
---|
10 | // | revision : $Revision: 2139 $ |
---|
11 | // +-----------------------------------------------------------------------+ |
---|
12 | // | This program is free software; you can redistribute it and/or modify | |
---|
13 | // | it under the terms of the GNU General Public License as published by | |
---|
14 | // | the Free Software Foundation | |
---|
15 | // | | |
---|
16 | // | This program is distributed in the hope that it will be useful, but | |
---|
17 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
18 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
19 | // | General Public License for more details. | |
---|
20 | // | | |
---|
21 | // | You should have received a copy of the GNU General Public License | |
---|
22 | // | along with this program; if not, write to the Free Software | |
---|
23 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
24 | // | USA. | |
---|
25 | // +-----------------------------------------------------------------------+ |
---|
26 | |
---|
27 | // +-----------------------------------------------------------------------+ |
---|
28 | // | functions | |
---|
29 | // +-----------------------------------------------------------------------+ |
---|
30 | |
---|
31 | |
---|
32 | /** |
---|
33 | * Encodes a string using Q form if required (RFC2045) |
---|
34 | * mail headers MUST contain only US-ASCII characters |
---|
35 | */ |
---|
36 | function encode_mime_header($str) |
---|
37 | { |
---|
38 | $x = preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches); |
---|
39 | if ($x==0) |
---|
40 | { |
---|
41 | return $str; |
---|
42 | } |
---|
43 | // Replace every high ascii, control =, ? and _ characters |
---|
44 | $str = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e', |
---|
45 | "'='.sprintf('%02X', ord('\\1'))", $str); |
---|
46 | |
---|
47 | // Replace every spaces to _ (more readable than =20) |
---|
48 | $str = str_replace(" ", "_", $str); |
---|
49 | |
---|
50 | global $lang_info; |
---|
51 | return '=?'.$lang_info['charset'].'?Q?'.$str.'?='; |
---|
52 | } |
---|
53 | |
---|
54 | /* |
---|
55 | * Returns an array of mail configuration parameters : |
---|
56 | * |
---|
57 | * - mail_options: see $conf['mail_options'] |
---|
58 | * - send_bcc_mail_webmaster: see $conf['send_bcc_mail_webmaster'] |
---|
59 | * - email_webmaster: mail corresponding to $conf['webmaster_id'] |
---|
60 | * - formated_email_webmaster: the name of webmaster is $conf['gallery_title'] |
---|
61 | * - text_footer: PhpWebGallery and version |
---|
62 | * |
---|
63 | * @return array |
---|
64 | */ |
---|
65 | function get_mail_configuration() |
---|
66 | { |
---|
67 | global $conf; |
---|
68 | |
---|
69 | $conf_mail = array( |
---|
70 | 'mail_options' => $conf['mail_options'], |
---|
71 | 'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'], |
---|
72 | 'default_email_format' => $conf['default_email_format'], |
---|
73 | 'use_smtp' => !empty($conf['smtp_host']), |
---|
74 | 'smtp_host' => $conf['smtp_host'], |
---|
75 | 'smtp_user' => $conf['smtp_user'], |
---|
76 | 'smtp_password' => $conf['smtp_password'] |
---|
77 | ); |
---|
78 | |
---|
79 | // we have webmaster id among user list, what's his email address ? |
---|
80 | $conf_mail['email_webmaster'] = get_webmaster_mail_address(); |
---|
81 | |
---|
82 | // name of the webmaster is the title of the gallery |
---|
83 | $conf_mail['formated_email_webmaster'] = |
---|
84 | format_email($conf['gallery_title'], $conf_mail['email_webmaster']); |
---|
85 | |
---|
86 | $conf_mail['boundary_key'] = generate_key(32); |
---|
87 | |
---|
88 | return $conf_mail; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Returns an email address with an associated real name |
---|
93 | * |
---|
94 | * @param string name |
---|
95 | * @param string email |
---|
96 | */ |
---|
97 | function format_email($name, $email) |
---|
98 | { |
---|
99 | global $conf; |
---|
100 | |
---|
101 | if ($conf['enabled_format_email']) |
---|
102 | { |
---|
103 | // Spring cleaning |
---|
104 | $cvt_name = trim(preg_replace('#[\n\r]+#s', '', $name)); |
---|
105 | $cvt_email = trim(preg_replace('#[\n\r]+#s', '', $email)); |
---|
106 | |
---|
107 | if ($cvt_name!="") |
---|
108 | { |
---|
109 | $cvt_name = encode_mime_header( |
---|
110 | '"' |
---|
111 | .addcslashes($cvt_name,'"') |
---|
112 | .'"'); |
---|
113 | $cvt_name .= ' '; |
---|
114 | } |
---|
115 | |
---|
116 | if (strpos($cvt_email, '<') === false) |
---|
117 | { |
---|
118 | return $cvt_name.'<'.$cvt_email.'>'; |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | return $cvt_name.$cvt_email; |
---|
123 | } |
---|
124 | } |
---|
125 | else |
---|
126 | { |
---|
127 | return $cvt_email; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Returns an completed array template/theme |
---|
133 | * completed with get_default_template() |
---|
134 | * |
---|
135 | * @params: |
---|
136 | * - args: incompleted array of template/theme |
---|
137 | * o template: template to use [default get_default_template()] |
---|
138 | * o theme: template to use [default get_default_template()] |
---|
139 | */ |
---|
140 | function get_array_template_theme($args = array()) |
---|
141 | { |
---|
142 | global $conf; |
---|
143 | |
---|
144 | $res = array(); |
---|
145 | |
---|
146 | if (empty($args['template']) or empty($args['theme'])) |
---|
147 | { |
---|
148 | list($res['template'], $res['theme']) = explode('/', get_default_template()); |
---|
149 | } |
---|
150 | |
---|
151 | if (!empty($args['template'])) |
---|
152 | { |
---|
153 | $res['template'] = $args['template']; |
---|
154 | } |
---|
155 | |
---|
156 | if (!empty($args['theme'])) |
---|
157 | { |
---|
158 | $res['theme'] = $args['theme']; |
---|
159 | } |
---|
160 | |
---|
161 | return $res; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Return an new mail template |
---|
166 | * |
---|
167 | * @params: |
---|
168 | * - email_format: mail format |
---|
169 | * - args: function params of mail function: |
---|
170 | * o template: template to use [default get_default_template()] |
---|
171 | * o theme: template to use [default get_default_template()] |
---|
172 | */ |
---|
173 | function get_mail_template($email_format, $args = array()) |
---|
174 | { |
---|
175 | $args = get_array_template_theme($args); |
---|
176 | |
---|
177 | $mail_template = new Template(PHPWG_ROOT_PATH.'template/'.$args['template'], $args['theme']); |
---|
178 | $mail_template->set_rootdir(PHPWG_ROOT_PATH.'template/'.$args['template'].'/mail/'.$email_format); |
---|
179 | |
---|
180 | return $mail_template; |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * Return string email format (html or not) |
---|
185 | * |
---|
186 | * @param string format |
---|
187 | */ |
---|
188 | function get_str_email_format($is_html) |
---|
189 | { |
---|
190 | return ($is_html ? 'text/html' : 'text/plain'); |
---|
191 | } |
---|
192 | |
---|
193 | /* |
---|
194 | * Switch language to param language |
---|
195 | * All entries are push on language stack |
---|
196 | * |
---|
197 | * @param string language |
---|
198 | */ |
---|
199 | function switch_lang_to($language) |
---|
200 | { |
---|
201 | global $switch_lang, $user, $lang, $lang_info; |
---|
202 | |
---|
203 | if (count($switch_lang['stack']) == 0) |
---|
204 | { |
---|
205 | $prev_language = $user['language']; |
---|
206 | } |
---|
207 | else |
---|
208 | { |
---|
209 | $prev_language = end($switch_lang['stack']); |
---|
210 | } |
---|
211 | |
---|
212 | $switch_lang['stack'][] = $language; |
---|
213 | |
---|
214 | if ($prev_language != $language) |
---|
215 | { |
---|
216 | if (!isset($switch_lang['language'][$prev_language])) |
---|
217 | { |
---|
218 | $switch_lang[$prev_language]['lang_info'] = $lang_info; |
---|
219 | $switch_lang[$prev_language]['lang'] = $lang; |
---|
220 | } |
---|
221 | |
---|
222 | if (!isset($switch_lang['language'][$language])) |
---|
223 | { |
---|
224 | // Re-Init language arrays |
---|
225 | $lang_info = array(); |
---|
226 | $lang = array(); |
---|
227 | |
---|
228 | // language files |
---|
229 | include(get_language_filepath('common.lang.php', '', $language)); |
---|
230 | // No test admin because script is checked admin (user selected no) |
---|
231 | // Translations are in admin file too |
---|
232 | include(get_language_filepath('admin.lang.php', '', $language)); |
---|
233 | trigger_action('loading_lang'); |
---|
234 | @include(get_language_filepath('local.lang.php', '', $language)); |
---|
235 | |
---|
236 | $switch_lang[$language]['lang_info'] = $lang_info; |
---|
237 | $switch_lang[$language]['lang'] = $lang; |
---|
238 | } |
---|
239 | else |
---|
240 | { |
---|
241 | $lang_info = $switch_lang[$language]['lang_info']; |
---|
242 | $lang = $switch_lang[$language]['lang']; |
---|
243 | } |
---|
244 | |
---|
245 | $user['language'] = $language; |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | /* |
---|
250 | * Switch back language pushed with switch_lang_to function |
---|
251 | * |
---|
252 | * @param: none |
---|
253 | */ |
---|
254 | function switch_lang_back() |
---|
255 | { |
---|
256 | global $switch_lang, $user, $lang, $lang_info; |
---|
257 | |
---|
258 | $last_language = array_pop($switch_lang['stack']); |
---|
259 | |
---|
260 | if (count($switch_lang['stack']) > 0) |
---|
261 | { |
---|
262 | $language = end($switch_lang['stack']); |
---|
263 | } |
---|
264 | else |
---|
265 | { |
---|
266 | $language = $user['language']; |
---|
267 | } |
---|
268 | |
---|
269 | if ($last_language != $language) |
---|
270 | { |
---|
271 | if (!isset($switch_lang['language'][$language])) |
---|
272 | { |
---|
273 | $lang_info = $switch_lang[$language]['lang_info']; |
---|
274 | $lang = $switch_lang[$language]['lang']; |
---|
275 | } |
---|
276 | $user['language'] = $language; |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | * Returns email of all administrator |
---|
282 | * |
---|
283 | * @return string |
---|
284 | */ |
---|
285 | /* |
---|
286 | * send en notification email to all administrators |
---|
287 | * if a administrator is doing action, |
---|
288 | * he's be removed to email list |
---|
289 | * |
---|
290 | * @param: |
---|
291 | * - keyargs_subject: mail subject on l10n_args format |
---|
292 | * - keyargs_content: mail content on l10n_args format |
---|
293 | * |
---|
294 | * @return boolean (Ok or not) |
---|
295 | */ |
---|
296 | function pwg_mail_notification_admins($keyargs_subject, $keyargs_content) |
---|
297 | { |
---|
298 | // Check arguments |
---|
299 | if |
---|
300 | ( |
---|
301 | empty($keyargs_subject) or |
---|
302 | empty($keyargs_content) |
---|
303 | ) |
---|
304 | { |
---|
305 | return false; |
---|
306 | } |
---|
307 | |
---|
308 | global $conf, $user; |
---|
309 | $return = true; |
---|
310 | |
---|
311 | $admins = array(); |
---|
312 | |
---|
313 | $query = ' |
---|
314 | select |
---|
315 | U.'.$conf['user_fields']['username'].' as username, |
---|
316 | U.'.$conf['user_fields']['email'].' as mail_address |
---|
317 | from |
---|
318 | '.USERS_TABLE.' as U, |
---|
319 | '.USER_INFOS_TABLE.' as I |
---|
320 | where |
---|
321 | I.user_id = U.'.$conf['user_fields']['id'].' and |
---|
322 | I.status in (\'webmaster\', \'admin\') and |
---|
323 | I.adviser = \'false\' and |
---|
324 | '.$conf['user_fields']['email'].' is not null and |
---|
325 | I.user_id <> '.$user['id'].' |
---|
326 | order by |
---|
327 | username |
---|
328 | '; |
---|
329 | |
---|
330 | $datas = pwg_query($query); |
---|
331 | if (!empty($datas)) |
---|
332 | { |
---|
333 | while ($admin = mysql_fetch_array($datas)) |
---|
334 | { |
---|
335 | if (!empty($admin['mail_address'])) |
---|
336 | { |
---|
337 | array_push($admins, format_email($admin['username'], $admin['mail_address'])); |
---|
338 | } |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | if (count($admins) > 0) |
---|
343 | { |
---|
344 | $keyargs_content_admin_info = array |
---|
345 | ( |
---|
346 | get_l10n_args('Connected user: %s', $user['username']), |
---|
347 | get_l10n_args('IP: %s', $_SERVER['REMOTE_ADDR']), |
---|
348 | get_l10n_args('Browser: %s', $_SERVER['HTTP_USER_AGENT']) |
---|
349 | ); |
---|
350 | |
---|
351 | switch_lang_to(get_default_language()); |
---|
352 | |
---|
353 | $return = pwg_mail |
---|
354 | ( |
---|
355 | '', |
---|
356 | array |
---|
357 | ( |
---|
358 | 'Bcc' => $admins, |
---|
359 | 'subject' => '['.$conf['gallery_title'].'] '.l10n_args($keyargs_subject), |
---|
360 | 'content' => |
---|
361 | l10n_args($keyargs_content)."\n\n" |
---|
362 | .l10n_args($keyargs_content_admin_info)."\n", |
---|
363 | 'content_format' => 'text/plain' |
---|
364 | ) |
---|
365 | ) and $return; |
---|
366 | |
---|
367 | switch_lang_back(); |
---|
368 | } |
---|
369 | |
---|
370 | return $return; |
---|
371 | } |
---|
372 | |
---|
373 | /* |
---|
374 | * send en email to user's group |
---|
375 | * |
---|
376 | * @param: |
---|
377 | * - group_id: mail are sent to group with this Id |
---|
378 | * - email_format: mail format |
---|
379 | * - keyargs_subject: mail subject on l10n_args format |
---|
380 | * - dirname: short name of directory including template |
---|
381 | * - tpl_shortname: short template name without extension |
---|
382 | * - assign_vars: array used to assign_vars to mail template |
---|
383 | * - language_selected: send mail only to user with this selected language |
---|
384 | * |
---|
385 | * @return boolean (Ok or not) |
---|
386 | */ |
---|
387 | function pwg_mail_group( |
---|
388 | $group_id, $email_format, $keyargs_subject, |
---|
389 | $dirname, $tpl_shortname, |
---|
390 | $assign_vars = array(), $language_selected = '') |
---|
391 | { |
---|
392 | // Check arguments |
---|
393 | if |
---|
394 | ( |
---|
395 | empty($group_id) or |
---|
396 | empty($email_format) or |
---|
397 | empty($keyargs_subject) or |
---|
398 | empty($tpl_shortname) |
---|
399 | ) |
---|
400 | { |
---|
401 | return false; |
---|
402 | } |
---|
403 | |
---|
404 | global $conf; |
---|
405 | $return = true; |
---|
406 | |
---|
407 | $query = ' |
---|
408 | SELECT |
---|
409 | distinct language, template |
---|
410 | FROM |
---|
411 | '.USER_GROUP_TABLE.' as ug |
---|
412 | INNER JOIN '.USERS_TABLE.' as u ON '.$conf['user_fields']['id'].' = ug.user_id |
---|
413 | INNER JOIN '.USER_INFOS_TABLE.' as ui ON ui.user_id = ug.user_id |
---|
414 | WHERE |
---|
415 | '.$conf['user_fields']['email'].' IS NOT NULL |
---|
416 | AND group_id = '.$group_id; |
---|
417 | |
---|
418 | if (!empty($language_selected)) |
---|
419 | { |
---|
420 | $query .= ' |
---|
421 | AND language = \''.$language_selected.'\''; |
---|
422 | } |
---|
423 | |
---|
424 | $query .= ' |
---|
425 | ;'; |
---|
426 | |
---|
427 | $result = pwg_query($query); |
---|
428 | |
---|
429 | if (mysql_num_rows($result) > 0) |
---|
430 | { |
---|
431 | $list = array(); |
---|
432 | while ($row = mysql_fetch_array($result)) |
---|
433 | { |
---|
434 | $row['template_theme'] = $row['template']; |
---|
435 | list($row['template'], $row['theme']) = explode('/', $row['template_theme']); |
---|
436 | $list[] = $row; |
---|
437 | } |
---|
438 | |
---|
439 | foreach ($list as $elem) |
---|
440 | { |
---|
441 | $query = ' |
---|
442 | SELECT |
---|
443 | u.'.$conf['user_fields']['username'].' as username, |
---|
444 | u.'.$conf['user_fields']['email'].' as mail_address |
---|
445 | FROM |
---|
446 | '.USER_GROUP_TABLE.' as ug |
---|
447 | INNER JOIN '.USERS_TABLE.' as u ON '.$conf['user_fields']['id'].' = ug.user_id |
---|
448 | INNER JOIN '.USER_INFOS_TABLE.' as ui ON ui.user_id = ug.user_id |
---|
449 | WHERE |
---|
450 | '.$conf['user_fields']['email'].' IS NOT NULL |
---|
451 | AND group_id = '.$group_id.' |
---|
452 | AND language = \''.$elem['language'].'\' |
---|
453 | AND template = \''.$elem['template_theme'].'\' |
---|
454 | ;'; |
---|
455 | |
---|
456 | $result = pwg_query($query); |
---|
457 | |
---|
458 | if (mysql_num_rows($result) > 0) |
---|
459 | { |
---|
460 | $Bcc = array(); |
---|
461 | while ($row = mysql_fetch_array($result)) |
---|
462 | { |
---|
463 | if (!empty($row['mail_address'])) |
---|
464 | { |
---|
465 | array_push($Bcc, format_email($row['username'], $row['mail_address'])); |
---|
466 | } |
---|
467 | } |
---|
468 | |
---|
469 | if (count($Bcc) > 0) |
---|
470 | { |
---|
471 | switch_lang_to($elem['language']); |
---|
472 | |
---|
473 | $mail_template = get_mail_template($email_format, $elem); |
---|
474 | $mail_template->set_filename($tpl_shortname, |
---|
475 | (empty($dirname) ? '' : $dirname.'/').$tpl_shortname.'.tpl'); |
---|
476 | |
---|
477 | $mail_template->assign_vars( |
---|
478 | trigger_event('mail_group_assign_vars', $assign_vars)); |
---|
479 | |
---|
480 | $return = pwg_mail |
---|
481 | ( |
---|
482 | '', |
---|
483 | array |
---|
484 | ( |
---|
485 | 'Bcc' => $Bcc, |
---|
486 | 'subject' => l10n_args($keyargs_subject), |
---|
487 | 'email_format' => $email_format, |
---|
488 | 'content' => $mail_template->parse($tpl_shortname, true), |
---|
489 | 'content_format' => $email_format, |
---|
490 | 'template' => $elem['template'], |
---|
491 | 'theme' => $elem['theme'] |
---|
492 | ) |
---|
493 | ) and $return; |
---|
494 | |
---|
495 | switch_lang_back(); |
---|
496 | } |
---|
497 | } |
---|
498 | } |
---|
499 | } |
---|
500 | |
---|
501 | return $return; |
---|
502 | } |
---|
503 | |
---|
504 | /* |
---|
505 | * sends an email, using PhpWebGallery specific informations |
---|
506 | * |
---|
507 | * @param: |
---|
508 | * - to: receiver(s) of the mail. |
---|
509 | * - args: function params of mail function: |
---|
510 | * o from: sender [default value webmaster email] |
---|
511 | * o Cc: array of carbon copy receivers of the mail. [default value empty] |
---|
512 | * o Bcc: array of blind carbon copy receivers of the mail. [default value empty] |
---|
513 | * o subject [default value 'PhpWebGallery'] |
---|
514 | * o content: content of mail [default value ''] |
---|
515 | * o content_format: format of mail content [default value 'text/plain'] |
---|
516 | * o email_format: global mail format [default value $conf_mail['default_email_format']] |
---|
517 | * o template: template to use [default get_default_template()] |
---|
518 | * o theme: template to use [default get_default_template()] |
---|
519 | * |
---|
520 | * @return boolean (Ok or not) |
---|
521 | */ |
---|
522 | function pwg_mail($to, $args = array()) |
---|
523 | { |
---|
524 | global $conf, $conf_mail, $lang_info, $page; |
---|
525 | |
---|
526 | if (empty($to) and empty($args['Cc']) and empty($args['Bcc'])) |
---|
527 | { |
---|
528 | return true; |
---|
529 | } |
---|
530 | |
---|
531 | if (!isset($conf_mail)) |
---|
532 | { |
---|
533 | $conf_mail = get_mail_configuration(); |
---|
534 | } |
---|
535 | |
---|
536 | if (empty($args['email_format'])) |
---|
537 | { |
---|
538 | $args['email_format'] = $conf_mail['default_email_format']; |
---|
539 | } |
---|
540 | |
---|
541 | // Compute root_path in order have complete path |
---|
542 | if ($args['email_format'] == 'text/html') |
---|
543 | { |
---|
544 | set_make_full_url(); |
---|
545 | } |
---|
546 | |
---|
547 | if (!empty($to)) |
---|
548 | { |
---|
549 | $to = format_email('', $to); |
---|
550 | } |
---|
551 | |
---|
552 | if (empty($args['from'])) |
---|
553 | { |
---|
554 | $args['from'] = $conf_mail['formated_email_webmaster']; |
---|
555 | } |
---|
556 | else |
---|
557 | { |
---|
558 | $args['from'] = format_email('', $args['from']); |
---|
559 | } |
---|
560 | |
---|
561 | if (empty($args['subject'])) |
---|
562 | { |
---|
563 | $args['subject'] = 'PhpWebGallery'; |
---|
564 | } |
---|
565 | // Spring cleaning |
---|
566 | $cvt_subject = trim(preg_replace('#[\n\r]+#s', '', $args['subject'])); |
---|
567 | // Ascii convertion |
---|
568 | $cvt_subject = encode_mime_header($cvt_subject); |
---|
569 | |
---|
570 | if (!isset($args['content'])) |
---|
571 | { |
---|
572 | $args['content'] = ''; |
---|
573 | } |
---|
574 | |
---|
575 | if (empty($args['content_format'])) |
---|
576 | { |
---|
577 | $args['content_format'] = 'text/plain'; |
---|
578 | } |
---|
579 | |
---|
580 | if ($conf_mail['send_bcc_mail_webmaster']) |
---|
581 | { |
---|
582 | $args['Bcc'][] = $conf_mail['formated_email_webmaster']; |
---|
583 | } |
---|
584 | |
---|
585 | if (($args['content_format'] == 'text/html') and ($args['email_format'] == 'text/plain')) |
---|
586 | { |
---|
587 | // Todo find function to convert html text to plain text |
---|
588 | return false; |
---|
589 | } |
---|
590 | |
---|
591 | $args = array_merge($args, get_array_template_theme($args)); |
---|
592 | |
---|
593 | $headers = 'From: '.$args['from']."\n"; |
---|
594 | $headers.= 'Reply-To: '.$args['from']."\n"; |
---|
595 | if (empty($to)) |
---|
596 | { |
---|
597 | $headers.= 'To: undisclosed-recipients: ;'."\n"; |
---|
598 | } |
---|
599 | |
---|
600 | if (!empty($args['Cc'])) |
---|
601 | { |
---|
602 | $headers.= 'Cc: '.implode(',', $args['Cc'])."\n"; |
---|
603 | } |
---|
604 | |
---|
605 | if (!empty($args['Bcc'])) |
---|
606 | { |
---|
607 | $headers.= 'Bcc: '.implode(',', $args['Bcc'])."\n"; |
---|
608 | } |
---|
609 | |
---|
610 | $headers.= 'Content-Type: multipart/alternative;'."\n"; |
---|
611 | $headers.= ' boundary="---='.$conf_mail['boundary_key'].'";'."\n"; |
---|
612 | $headers.= ' reply-type=original'."\n"; |
---|
613 | $headers.= 'MIME-Version: 1.0'."\n"; |
---|
614 | $headers.= 'X-Mailer: Piwigo Mailer'."\n"; |
---|
615 | |
---|
616 | $content = ''; |
---|
617 | |
---|
618 | if (!isset($conf_mail[$args['email_format']][$lang_info['code']][$lang_info['charset']][$args['template']][$args['theme']])) |
---|
619 | { |
---|
620 | if (!isset($mail_template)) |
---|
621 | { |
---|
622 | $mail_template = get_mail_template($args['email_format']); |
---|
623 | } |
---|
624 | |
---|
625 | $mail_template->set_filename('mail_header', 'header.tpl'); |
---|
626 | $mail_template->set_filename('mail_footer', 'footer.tpl'); |
---|
627 | |
---|
628 | $mail_template->assign_vars( |
---|
629 | array( |
---|
630 | //Header |
---|
631 | 'BOUNDARY_KEY' => $conf_mail['boundary_key'], |
---|
632 | 'CONTENT_TYPE' => $args['email_format'], |
---|
633 | 'CONTENT_ENCODING' => $lang_info['charset'], |
---|
634 | 'LANG' => $lang_info['code'], |
---|
635 | 'DIR' => $lang_info['direction'], |
---|
636 | |
---|
637 | // Footer |
---|
638 | 'GALLERY_URL' => |
---|
639 | isset($page['gallery_url']) ? |
---|
640 | $page['gallery_url'] : $conf['gallery_url'], |
---|
641 | 'GALLERY_TITLE' => |
---|
642 | isset($page['gallery_title']) ? |
---|
643 | $page['gallery_title'] : $conf['gallery_title'], |
---|
644 | 'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '', |
---|
645 | 'PHPWG_URL' => PHPWG_URL, |
---|
646 | |
---|
647 | 'TITLE_MAIL' => urlencode(l10n('title_send_mail')), |
---|
648 | 'MAIL' => get_webmaster_mail_address() |
---|
649 | )); |
---|
650 | |
---|
651 | if ($args['email_format'] == 'text/html') |
---|
652 | { |
---|
653 | $old_root = $mail_template->root; |
---|
654 | |
---|
655 | if (is_file($mail_template->root.'/global-mail-css.tpl')) |
---|
656 | { |
---|
657 | $mail_template->set_filename('global_mail_css', 'global-mail-css.tpl'); |
---|
658 | $mail_template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'global_mail_css'); |
---|
659 | } |
---|
660 | |
---|
661 | $mail_template->root = PHPWG_ROOT_PATH.'template/'.$args['template'].'/theme/'.$args['theme']; |
---|
662 | if (is_file($mail_template->root.'/mail-css.tpl')) |
---|
663 | { |
---|
664 | $mail_template->set_filename('mail_css', 'mail-css.tpl'); |
---|
665 | $mail_template->assign_var_from_handle('MAIL_CSS', 'mail_css'); |
---|
666 | } |
---|
667 | |
---|
668 | $mail_template->root = PHPWG_ROOT_PATH.'template-common'; |
---|
669 | if (is_file($mail_template->root.'/local-mail-css.tpl')) |
---|
670 | { |
---|
671 | $mail_template->set_filename('local_mail_css', 'local-mail-css.tpl'); |
---|
672 | $mail_template->assign_var_from_handle('LOCAL_MAIL_CSS', 'local_mail_css'); |
---|
673 | } |
---|
674 | |
---|
675 | $mail_template->root = $old_root; |
---|
676 | } |
---|
677 | |
---|
678 | // what are displayed on the header of each mail ? |
---|
679 | $conf_mail[$args['email_format']] |
---|
680 | [$lang_info['code']][$lang_info['charset']] |
---|
681 | [$args['template']][$args['theme']]['header'] = |
---|
682 | $mail_template->parse('mail_header', true); |
---|
683 | |
---|
684 | // what are displayed on the footer of each mail ? |
---|
685 | $conf_mail[$args['email_format']] |
---|
686 | [$lang_info['code']][$lang_info['charset']] |
---|
687 | [$args['template']][$args['theme']]['footer'] = |
---|
688 | $mail_template->parse('mail_footer', true); |
---|
689 | } |
---|
690 | |
---|
691 | // Header |
---|
692 | $content.= $conf_mail[$args['email_format']] |
---|
693 | [$lang_info['code']][$lang_info['charset']] |
---|
694 | [$args['template']][$args['theme']]['header']; |
---|
695 | |
---|
696 | // Content |
---|
697 | if (($args['content_format'] == 'text/plain') and ($args['email_format'] == 'text/html')) |
---|
698 | { |
---|
699 | $content.= '<p>'. |
---|
700 | nl2br( |
---|
701 | preg_replace("/(http:\/\/)([^\s,]*)/i", |
---|
702 | "<a href='$1$2'>$1$2</a>", |
---|
703 | htmlentities($args['content']))). |
---|
704 | '</p>'; |
---|
705 | } |
---|
706 | else |
---|
707 | { |
---|
708 | $content.= $args['content']; |
---|
709 | } |
---|
710 | |
---|
711 | // Footer |
---|
712 | $content.= $conf_mail[$args['email_format']] |
---|
713 | [$lang_info['code']][$lang_info['charset']] |
---|
714 | [$args['template']][$args['theme']]['footer']; |
---|
715 | |
---|
716 | // Close boundary |
---|
717 | $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n"; |
---|
718 | |
---|
719 | // Undo Compute root_path in order have complete path |
---|
720 | if ($args['email_format'] == 'text/html') |
---|
721 | { |
---|
722 | unset_make_full_url(); |
---|
723 | } |
---|
724 | |
---|
725 | return |
---|
726 | trigger_event('send_mail', |
---|
727 | false, /* Result */ |
---|
728 | trigger_event('send_mail_to', $to), |
---|
729 | trigger_event('send_mail_subject', $cvt_subject), |
---|
730 | trigger_event('send_mail_content', $content), |
---|
731 | trigger_event('send_mail_headers', $headers), |
---|
732 | $args |
---|
733 | ); |
---|
734 | } |
---|
735 | |
---|
736 | /* |
---|
737 | * pwg sendmail |
---|
738 | * |
---|
739 | * @param: |
---|
740 | * - result of other sendmail |
---|
741 | * - to: Receiver or receiver(s) of the mail. |
---|
742 | * - subject [default value 'PhpWebGallery'] |
---|
743 | * - content: content of mail |
---|
744 | * - headers: headers of mail |
---|
745 | * |
---|
746 | * @return boolean (Ok or not) |
---|
747 | */ |
---|
748 | function pwg_send_mail($result, $to, $subject, $content, $headers) |
---|
749 | { |
---|
750 | if (!$result) |
---|
751 | { |
---|
752 | global $conf_mail; |
---|
753 | |
---|
754 | if ($conf_mail['use_smtp']) |
---|
755 | { |
---|
756 | include_once( PHPWG_ROOT_PATH.'include/class_smtp_mail.inc.php' ); |
---|
757 | $smtp_mail = new smtp_mail( |
---|
758 | $conf_mail['smtp_host'], $conf_mail['smtp_user'], $conf_mail['smtp_password'], |
---|
759 | $conf_mail['email_webmaster']); |
---|
760 | return $smtp_mail->mail($to, $subject, $content, $headers); |
---|
761 | } |
---|
762 | else |
---|
763 | { |
---|
764 | if ($conf_mail['mail_options']) |
---|
765 | { |
---|
766 | $options = '-f '.$conf_mail['email_webmaster']; |
---|
767 | return mail($to, $subject, $content, $headers, $options); |
---|
768 | } |
---|
769 | else |
---|
770 | { |
---|
771 | return mail($to, $subject, $content, $headers); |
---|
772 | } |
---|
773 | } |
---|
774 | } |
---|
775 | } |
---|
776 | |
---|
777 | /*Testing block |
---|
778 | function pwg_send_mail_test($result, $to, $subject, $content, $headers, $args) |
---|
779 | { |
---|
780 | global $user, $lang_info; |
---|
781 | @mkdir(PHPWG_ROOT_PATH.'testmail'); |
---|
782 | $filename = PHPWG_ROOT_PATH.'testmail/mail.'.$user['username'].'.'.$lang_info['code'].'.'.$args['template'].'.'.$args['theme']; |
---|
783 | if ($args['content_format'] == 'text/plain') |
---|
784 | { |
---|
785 | $filename .= '.txt'; |
---|
786 | } |
---|
787 | else |
---|
788 | { |
---|
789 | $filename .= '.html'; |
---|
790 | } |
---|
791 | $file = fopen($filename, 'w+'); |
---|
792 | fwrite($file, $to); |
---|
793 | fwrite($file, $subject); |
---|
794 | fwrite($file, $headers); |
---|
795 | fwrite($file, $content); |
---|
796 | fclose($file); |
---|
797 | return true; |
---|
798 | } |
---|
799 | add_event_handler('send_mail', 'pwg_send_mail_test', 0, 6);*/ |
---|
800 | |
---|
801 | |
---|
802 | add_event_handler('send_mail', 'pwg_send_mail', EVENT_HANDLER_PRIORITY_NEUTRAL, 5); |
---|
803 | trigger_action('functions_mail_included'); |
---|
804 | |
---|
805 | ?> |
---|