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