1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2012 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 | /** |
---|
155 | * Return an new mail template |
---|
156 | * |
---|
157 | * @params: |
---|
158 | * - email_format: mail format |
---|
159 | * - args: function params of mail function: |
---|
160 | * o template: template to use [default get_default_theme()] |
---|
161 | * o theme: template to use [default get_default_theme()] |
---|
162 | */ |
---|
163 | function & get_mail_template($email_format, $theme='') |
---|
164 | { |
---|
165 | if (empty($theme)) |
---|
166 | { |
---|
167 | $theme = get_default_theme(); |
---|
168 | } |
---|
169 | |
---|
170 | $mail_template = new Template(PHPWG_ROOT_PATH.'themes', $theme, 'template/mail/'.$email_format); |
---|
171 | |
---|
172 | return $mail_template; |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * Return string email format (html or not) |
---|
177 | * |
---|
178 | * @param string format |
---|
179 | */ |
---|
180 | function get_str_email_format($is_html) |
---|
181 | { |
---|
182 | return ($is_html ? 'text/html' : 'text/plain'); |
---|
183 | } |
---|
184 | |
---|
185 | /* |
---|
186 | * Switch language to param language |
---|
187 | * All entries are push on language stack |
---|
188 | * |
---|
189 | * @param string language |
---|
190 | */ |
---|
191 | function switch_lang_to($language) |
---|
192 | { |
---|
193 | global $switch_lang, $user, $lang, $lang_info; |
---|
194 | |
---|
195 | // explanation of switch_lang |
---|
196 | // $switch_lang['language'] contains data of language |
---|
197 | // $switch_lang['stack'] contains stack LIFO |
---|
198 | // $switch_lang['initialisation'] allow to know if it's first call |
---|
199 | |
---|
200 | // Treatment with current user |
---|
201 | // Language of current user is saved (it's considered OK on firt call) |
---|
202 | if (!isset($switch_lang['initialisation']) and !isset($switch_lang['language'][$user['language']])) |
---|
203 | { |
---|
204 | $switch_lang['initialisation'] = true; |
---|
205 | $switch_lang['language'][$user['language']]['lang_info'] = $lang_info; |
---|
206 | $switch_lang['language'][$user['language']]['lang'] = $lang; |
---|
207 | } |
---|
208 | |
---|
209 | // Change current infos |
---|
210 | $switch_lang['stack'][] = $user['language']; |
---|
211 | $user['language'] = $language; |
---|
212 | |
---|
213 | // Load new data if necessary |
---|
214 | if (!isset($switch_lang['language'][$language])) |
---|
215 | { |
---|
216 | // Re-Init language arrays |
---|
217 | $lang_info = array(); |
---|
218 | $lang = array(); |
---|
219 | |
---|
220 | // language files |
---|
221 | load_language('common.lang', '', array('language'=>$language) ); |
---|
222 | // No test admin because script is checked admin (user selected no) |
---|
223 | // Translations are in admin file too |
---|
224 | load_language('admin.lang', '', array('language'=>$language) ); |
---|
225 | trigger_action('loading_lang'); |
---|
226 | load_language('lang', PHPWG_ROOT_PATH.PWG_LOCAL_DIR, |
---|
227 | array('language'=>$language, 'no_fallback'=>true, 'local'=>true) |
---|
228 | ); |
---|
229 | |
---|
230 | $switch_lang['language'][$language]['lang_info'] = $lang_info; |
---|
231 | $switch_lang['language'][$language]['lang'] = $lang; |
---|
232 | } |
---|
233 | else |
---|
234 | { |
---|
235 | $lang_info = $switch_lang['language'][$language]['lang_info']; |
---|
236 | $lang = $switch_lang['language'][$language]['lang']; |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | /* |
---|
241 | * Switch back language pushed with switch_lang_to function |
---|
242 | * |
---|
243 | * @param: none |
---|
244 | */ |
---|
245 | function switch_lang_back() |
---|
246 | { |
---|
247 | global $switch_lang, $user, $lang, $lang_info; |
---|
248 | |
---|
249 | if (count($switch_lang['stack']) > 0) |
---|
250 | { |
---|
251 | // Get last value |
---|
252 | $language = array_pop($switch_lang['stack']); |
---|
253 | |
---|
254 | // Change current infos |
---|
255 | if (isset($switch_lang['language'][$language])) |
---|
256 | { |
---|
257 | $lang_info = $switch_lang['language'][$language]['lang_info']; |
---|
258 | $lang = $switch_lang['language'][$language]['lang']; |
---|
259 | } |
---|
260 | $user['language'] = $language; |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | /** |
---|
265 | * Returns email of all administrator |
---|
266 | * |
---|
267 | * @return string |
---|
268 | */ |
---|
269 | /* |
---|
270 | * send en notification email to all administrators |
---|
271 | * if a administrator is doing action, |
---|
272 | * he's be removed to email list |
---|
273 | * |
---|
274 | * @param: |
---|
275 | * - keyargs_subject: mail subject on l10n_args format |
---|
276 | * - keyargs_content: mail content on l10n_args format |
---|
277 | * |
---|
278 | * @return boolean (Ok or not) |
---|
279 | */ |
---|
280 | function pwg_mail_notification_admins($keyargs_subject, $keyargs_content, $send_technical_details=true) |
---|
281 | { |
---|
282 | global $conf, $user; |
---|
283 | |
---|
284 | // Check arguments |
---|
285 | if (empty($keyargs_subject) or empty($keyargs_content)) |
---|
286 | { |
---|
287 | return false; |
---|
288 | } |
---|
289 | |
---|
290 | $return = true; |
---|
291 | |
---|
292 | $admins = array(); |
---|
293 | |
---|
294 | $query = ' |
---|
295 | SELECT |
---|
296 | u.'.$conf['user_fields']['username'].' AS username, |
---|
297 | u.'.$conf['user_fields']['email'].' AS mail_address |
---|
298 | FROM '.USERS_TABLE.' AS u |
---|
299 | JOIN '.USER_INFOS_TABLE.' AS i ON i.user_id = u.'.$conf['user_fields']['id'].' |
---|
300 | WHERE i.status in (\'webmaster\', \'admin\') |
---|
301 | AND '.$conf['user_fields']['email'].' IS NOT NULL |
---|
302 | AND i.user_id <> '.$user['id'].' |
---|
303 | ORDER BY username |
---|
304 | ;'; |
---|
305 | |
---|
306 | $datas = pwg_query($query); |
---|
307 | if (!empty($datas)) |
---|
308 | { |
---|
309 | while ($admin = pwg_db_fetch_assoc($datas)) |
---|
310 | { |
---|
311 | if (!empty($admin['mail_address'])) |
---|
312 | { |
---|
313 | array_push($admins, format_email($admin['username'], $admin['mail_address'])); |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | if (count($admins) > 0) |
---|
319 | { |
---|
320 | $keyargs_content_admin_info = array( |
---|
321 | get_l10n_args('Connected user: %s', stripslashes($user['username'])), |
---|
322 | get_l10n_args('IP: %s', $_SERVER['REMOTE_ADDR']), |
---|
323 | get_l10n_args('Browser: %s', $_SERVER['HTTP_USER_AGENT']) |
---|
324 | ); |
---|
325 | |
---|
326 | switch_lang_to(get_default_language()); |
---|
327 | |
---|
328 | $content = l10n_args($keyargs_content)."\n"; |
---|
329 | if ($send_technical_details) |
---|
330 | { |
---|
331 | $content.= "\n".l10n_args($keyargs_content_admin_info)."\n"; |
---|
332 | } |
---|
333 | |
---|
334 | $return = pwg_mail( |
---|
335 | implode(', ', $admins), |
---|
336 | array( |
---|
337 | 'subject' => '['.$conf['gallery_title'].'] '.l10n_args($keyargs_subject), |
---|
338 | 'content' => $content, |
---|
339 | 'content_format' => 'text/plain', |
---|
340 | 'email_format' => 'text/plain', |
---|
341 | ) |
---|
342 | ); |
---|
343 | |
---|
344 | switch_lang_back(); |
---|
345 | } |
---|
346 | |
---|
347 | return $return; |
---|
348 | } |
---|
349 | |
---|
350 | /* |
---|
351 | * send en email to user's group |
---|
352 | * |
---|
353 | * @param: |
---|
354 | * - group_id: mail are sent to group with this Id |
---|
355 | * - email_format: mail format |
---|
356 | * - keyargs_subject: mail subject on l10n_args format |
---|
357 | * - dirname: short name of directory including template |
---|
358 | * - tpl_shortname: short template name without extension |
---|
359 | * - assign_vars: array used to assign_vars to mail template |
---|
360 | * - language_selected: send mail only to user with this selected language |
---|
361 | * |
---|
362 | * @return boolean (Ok or not) |
---|
363 | */ |
---|
364 | function pwg_mail_group( |
---|
365 | $group_id, $email_format, $keyargs_subject, |
---|
366 | $tpl_shortname, |
---|
367 | $assign_vars = array(), $language_selected = '') |
---|
368 | { |
---|
369 | // Check arguments |
---|
370 | if |
---|
371 | ( |
---|
372 | empty($group_id) or |
---|
373 | empty($email_format) or |
---|
374 | empty($keyargs_subject) or |
---|
375 | empty($tpl_shortname) |
---|
376 | ) |
---|
377 | { |
---|
378 | return false; |
---|
379 | } |
---|
380 | |
---|
381 | global $conf; |
---|
382 | $return = true; |
---|
383 | |
---|
384 | $query = ' |
---|
385 | SELECT |
---|
386 | distinct language, theme |
---|
387 | FROM |
---|
388 | '.USER_GROUP_TABLE.' as ug |
---|
389 | INNER JOIN '.USERS_TABLE.' as u ON '.$conf['user_fields']['id'].' = ug.user_id |
---|
390 | INNER JOIN '.USER_INFOS_TABLE.' as ui ON ui.user_id = ug.user_id |
---|
391 | WHERE |
---|
392 | '.$conf['user_fields']['email'].' IS NOT NULL |
---|
393 | AND group_id = '.$group_id; |
---|
394 | |
---|
395 | if (!empty($language_selected)) |
---|
396 | { |
---|
397 | $query .= ' |
---|
398 | AND language = \''.$language_selected.'\''; |
---|
399 | } |
---|
400 | |
---|
401 | $query .= ' |
---|
402 | ;'; |
---|
403 | |
---|
404 | $result = pwg_query($query); |
---|
405 | |
---|
406 | if (pwg_db_num_rows($result) > 0) |
---|
407 | { |
---|
408 | $list = array(); |
---|
409 | while ($row = pwg_db_fetch_assoc($result)) |
---|
410 | { |
---|
411 | $list[] = $row; |
---|
412 | } |
---|
413 | |
---|
414 | foreach ($list as $elem) |
---|
415 | { |
---|
416 | $query = ' |
---|
417 | SELECT |
---|
418 | u.'.$conf['user_fields']['username'].' as username, |
---|
419 | u.'.$conf['user_fields']['email'].' as mail_address |
---|
420 | FROM |
---|
421 | '.USER_GROUP_TABLE.' as ug |
---|
422 | INNER JOIN '.USERS_TABLE.' as u ON '.$conf['user_fields']['id'].' = ug.user_id |
---|
423 | INNER JOIN '.USER_INFOS_TABLE.' as ui ON ui.user_id = ug.user_id |
---|
424 | WHERE |
---|
425 | '.$conf['user_fields']['email'].' IS NOT NULL |
---|
426 | AND group_id = '.$group_id.' |
---|
427 | AND language = \''.$elem['language'].'\' |
---|
428 | AND theme = \''.$elem['theme'].'\' |
---|
429 | ;'; |
---|
430 | |
---|
431 | $result = pwg_query($query); |
---|
432 | |
---|
433 | if (pwg_db_num_rows($result) > 0) |
---|
434 | { |
---|
435 | $Bcc = array(); |
---|
436 | while ($row = pwg_db_fetch_assoc($result)) |
---|
437 | { |
---|
438 | if (!empty($row['mail_address'])) |
---|
439 | { |
---|
440 | array_push($Bcc, format_email(stripslashes($row['username']), $row['mail_address'])); |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | if (count($Bcc) > 0) |
---|
445 | { |
---|
446 | switch_lang_to($elem['language']); |
---|
447 | |
---|
448 | $mail_template = get_mail_template($email_format, $elem['theme']); |
---|
449 | $mail_template->set_filename($tpl_shortname, $tpl_shortname.'.tpl'); |
---|
450 | |
---|
451 | $mail_template->assign( |
---|
452 | trigger_event('mail_group_assign_vars', $assign_vars)); |
---|
453 | |
---|
454 | $return = pwg_mail |
---|
455 | ( |
---|
456 | '', |
---|
457 | array |
---|
458 | ( |
---|
459 | 'Bcc' => $Bcc, |
---|
460 | 'subject' => l10n_args($keyargs_subject), |
---|
461 | 'email_format' => $email_format, |
---|
462 | 'content' => $mail_template->parse($tpl_shortname, true), |
---|
463 | 'content_format' => $email_format, |
---|
464 | 'theme' => $elem['theme'] |
---|
465 | ) |
---|
466 | ) and $return; |
---|
467 | |
---|
468 | switch_lang_back(); |
---|
469 | } |
---|
470 | } |
---|
471 | } |
---|
472 | } |
---|
473 | |
---|
474 | return $return; |
---|
475 | } |
---|
476 | |
---|
477 | /* |
---|
478 | * sends an email, using Piwigo specific informations |
---|
479 | * |
---|
480 | * @param: |
---|
481 | * - to: receiver(s) of the mail (list separated by comma). |
---|
482 | * - args: function params of mail function: |
---|
483 | * o from: sender [default value webmaster email] |
---|
484 | * o Cc: array of carbon copy receivers of the mail. [default value empty] |
---|
485 | * o Bcc: array of blind carbon copy receivers of the mail. [default value empty] |
---|
486 | * o subject [default value 'Piwigo'] |
---|
487 | * o content: content of mail [default value ''] |
---|
488 | * o content_format: format of mail content [default value 'text/plain'] |
---|
489 | * o email_format: global mail format [default value $conf_mail['default_email_format']] |
---|
490 | * o theme: template to use [default get_default_theme()] |
---|
491 | * |
---|
492 | * @return boolean (Ok or not) |
---|
493 | */ |
---|
494 | function pwg_mail($to, $args = array()) |
---|
495 | { |
---|
496 | global $conf, $conf_mail, $lang_info, $page; |
---|
497 | |
---|
498 | if (empty($to) and empty($args['Cc']) and empty($args['Bcc'])) |
---|
499 | { |
---|
500 | return true; |
---|
501 | } |
---|
502 | |
---|
503 | if (!isset($conf_mail)) |
---|
504 | { |
---|
505 | $conf_mail = get_mail_configuration(); |
---|
506 | } |
---|
507 | |
---|
508 | if (empty($args['email_format'])) |
---|
509 | { |
---|
510 | $args['email_format'] = $conf_mail['default_email_format']; |
---|
511 | } |
---|
512 | |
---|
513 | // Compute root_path in order have complete path |
---|
514 | set_make_full_url(); |
---|
515 | |
---|
516 | if (empty($args['from'])) |
---|
517 | { |
---|
518 | $args['from'] = $conf_mail['formated_email_webmaster']; |
---|
519 | } |
---|
520 | else |
---|
521 | { |
---|
522 | $args['from'] = format_email('', $args['from']); |
---|
523 | } |
---|
524 | |
---|
525 | if (empty($args['subject'])) |
---|
526 | { |
---|
527 | $args['subject'] = 'Piwigo'; |
---|
528 | } |
---|
529 | // Spring cleaning |
---|
530 | $cvt_subject = trim(preg_replace('#[\n\r]+#s', '', $args['subject'])); |
---|
531 | // Ascii convertion |
---|
532 | $cvt_subject = encode_mime_header($cvt_subject); |
---|
533 | |
---|
534 | if (!isset($args['content'])) |
---|
535 | { |
---|
536 | $args['content'] = ''; |
---|
537 | } |
---|
538 | |
---|
539 | if (empty($args['content_format'])) |
---|
540 | { |
---|
541 | $args['content_format'] = 'text/plain'; |
---|
542 | } |
---|
543 | |
---|
544 | if ($conf_mail['send_bcc_mail_webmaster']) |
---|
545 | { |
---|
546 | $args['Bcc'][] = $conf_mail['formated_email_webmaster']; |
---|
547 | } |
---|
548 | |
---|
549 | if (empty($args['theme'])) |
---|
550 | { |
---|
551 | $args['theme'] = get_default_theme(); |
---|
552 | } |
---|
553 | |
---|
554 | $headers = 'From: '.$args['from']."\n"; |
---|
555 | $headers.= 'Reply-To: '.$args['from']."\n"; |
---|
556 | |
---|
557 | if (!empty($args['Cc'])) |
---|
558 | { |
---|
559 | $headers.= 'Cc: '.implode(',', $args['Cc'])."\n"; |
---|
560 | } |
---|
561 | |
---|
562 | if (!empty($args['Bcc'])) |
---|
563 | { |
---|
564 | $headers.= 'Bcc: '.implode(',', $args['Bcc'])."\n"; |
---|
565 | } |
---|
566 | |
---|
567 | $headers.= 'Content-Type: multipart/alternative;'."\n"; |
---|
568 | $headers.= ' boundary="---='.$conf_mail['boundary_key'].'";'."\n"; |
---|
569 | $headers.= ' reply-type=original'."\n"; |
---|
570 | $headers.= 'MIME-Version: 1.0'."\n"; |
---|
571 | $headers.= 'X-Mailer: Piwigo Mailer'."\n"; |
---|
572 | |
---|
573 | // List on content-type |
---|
574 | $content_type_list[] = $args['email_format']; |
---|
575 | if (!empty($conf_mail['alternative_email_format'])) |
---|
576 | { |
---|
577 | $content_type_list[] = $conf_mail['alternative_email_format']; |
---|
578 | } |
---|
579 | |
---|
580 | $content = ''; |
---|
581 | |
---|
582 | foreach (array_unique($content_type_list) as $content_type) |
---|
583 | { |
---|
584 | // key compose of indexes witch allow ti cache mail data |
---|
585 | $cache_key = $content_type.'-'.$lang_info['code'].'-'.$args['theme']; |
---|
586 | |
---|
587 | if (!isset($conf_mail[$cache_key])) |
---|
588 | { |
---|
589 | if (!isset($conf_mail[$cache_key]['theme'])) |
---|
590 | { |
---|
591 | $conf_mail[$cache_key]['theme'] = get_mail_template($content_type, $args['theme']); |
---|
592 | } |
---|
593 | |
---|
594 | $conf_mail[$cache_key]['theme']->set_filename('mail_header', 'header.tpl'); |
---|
595 | $conf_mail[$cache_key]['theme']->set_filename('mail_footer', 'footer.tpl'); |
---|
596 | |
---|
597 | $conf_mail[$cache_key]['theme']->assign( |
---|
598 | array( |
---|
599 | //Header |
---|
600 | 'BOUNDARY_KEY' => $conf_mail['boundary_key'], |
---|
601 | 'CONTENT_TYPE' => $content_type, |
---|
602 | 'CONTENT_ENCODING' => get_pwg_charset(), |
---|
603 | |
---|
604 | // Footer |
---|
605 | 'GALLERY_URL' => get_gallery_home_url(), |
---|
606 | 'GALLERY_TITLE' => |
---|
607 | isset($page['gallery_title']) ? |
---|
608 | $page['gallery_title'] : $conf['gallery_title'], |
---|
609 | 'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '', |
---|
610 | 'PHPWG_URL' => PHPWG_URL, |
---|
611 | |
---|
612 | 'TITLE_MAIL' => urlencode(l10n('A comment on your site')), |
---|
613 | 'MAIL' => get_webmaster_mail_address() |
---|
614 | )); |
---|
615 | |
---|
616 | if ($content_type == 'text/html') |
---|
617 | { |
---|
618 | if ($conf_mail[$cache_key]['theme']->smarty->template_exists('global-mail-css.tpl')) |
---|
619 | { |
---|
620 | $conf_mail[$cache_key]['theme']->set_filename('css', 'global-mail-css.tpl'); |
---|
621 | $conf_mail[$cache_key]['theme']->assign_var_from_handle('GLOBAL_MAIL_CSS', 'css'); |
---|
622 | } |
---|
623 | |
---|
624 | $file = PHPWG_ROOT_PATH.'themes/'.$args['theme'].'/mail-css.tpl'; |
---|
625 | if (is_file($file)) |
---|
626 | { |
---|
627 | $conf_mail[$cache_key]['theme']->set_filename('css', realpath($file)); |
---|
628 | $conf_mail[$cache_key]['theme']->assign_var_from_handle('MAIL_CSS', 'css'); |
---|
629 | } |
---|
630 | } |
---|
631 | |
---|
632 | // what are displayed on the header of each mail ? |
---|
633 | $conf_mail[$cache_key]['header'] = |
---|
634 | $conf_mail[$cache_key]['theme']->parse('mail_header', true); |
---|
635 | |
---|
636 | // what are displayed on the footer of each mail ? |
---|
637 | $conf_mail[$cache_key]['footer'] = |
---|
638 | $conf_mail[$cache_key]['theme']->parse('mail_footer', true); |
---|
639 | } |
---|
640 | |
---|
641 | // Header |
---|
642 | $content.= $conf_mail[$cache_key]['header']; |
---|
643 | |
---|
644 | // Content |
---|
645 | if (($args['content_format'] == 'text/plain') and ($content_type == 'text/html')) |
---|
646 | { |
---|
647 | $content.= '<p>'. |
---|
648 | nl2br( |
---|
649 | preg_replace("/(http:\/\/)([^\s,]*)/i", |
---|
650 | "<a href='$1$2' class='thumblnk'>$1$2</a>", |
---|
651 | htmlspecialchars($args['content']))). |
---|
652 | '</p>'; |
---|
653 | } |
---|
654 | else if (($args['content_format'] == 'text/html') and ($content_type == 'text/plain')) |
---|
655 | { |
---|
656 | // convert html text to plain text |
---|
657 | $content.= strip_tags($args['content']); |
---|
658 | } |
---|
659 | else |
---|
660 | { |
---|
661 | $content.= $args['content']; |
---|
662 | } |
---|
663 | |
---|
664 | // Footer |
---|
665 | $content.= $conf_mail[$cache_key]['footer']; |
---|
666 | |
---|
667 | // Close boundary |
---|
668 | $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n"; |
---|
669 | } |
---|
670 | |
---|
671 | //~ // Close boundary |
---|
672 | //~ $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n"; |
---|
673 | |
---|
674 | // Undo Compute root_path in order have complete path |
---|
675 | unset_make_full_url(); |
---|
676 | |
---|
677 | return |
---|
678 | trigger_event('send_mail', |
---|
679 | false, /* Result */ |
---|
680 | trigger_event('send_mail_to', get_strict_email_list($to)), |
---|
681 | trigger_event('send_mail_subject', $cvt_subject), |
---|
682 | trigger_event('send_mail_content', $content), |
---|
683 | trigger_event('send_mail_headers', $headers), |
---|
684 | $args |
---|
685 | ); |
---|
686 | } |
---|
687 | |
---|
688 | /* |
---|
689 | * pwg sendmail |
---|
690 | * |
---|
691 | * @param: |
---|
692 | * - result of other sendmail |
---|
693 | * - to: Receiver or receiver(s) of the mail. |
---|
694 | * - subject [default value 'Piwigo'] |
---|
695 | * - content: content of mail |
---|
696 | * - headers: headers of mail |
---|
697 | * |
---|
698 | * @return boolean (Ok or not) |
---|
699 | */ |
---|
700 | function pwg_send_mail($result, $to, $subject, $content, $headers) |
---|
701 | { |
---|
702 | if (!$result) |
---|
703 | { |
---|
704 | global $conf_mail; |
---|
705 | |
---|
706 | if ($conf_mail['use_smtp']) |
---|
707 | { |
---|
708 | include_once( PHPWG_ROOT_PATH.'include/class_smtp_mail.inc.php' ); |
---|
709 | $smtp_mail = new smtp_mail( |
---|
710 | $conf_mail['smtp_host'], $conf_mail['smtp_user'], $conf_mail['smtp_password'], |
---|
711 | $conf_mail['email_webmaster']); |
---|
712 | return $smtp_mail->mail($to, $subject, $content, $headers); |
---|
713 | } |
---|
714 | else |
---|
715 | { |
---|
716 | if ($conf_mail['mail_options']) |
---|
717 | { |
---|
718 | $options = '-f '.$conf_mail['email_webmaster']; |
---|
719 | return mail($to, $subject, $content, $headers, $options); |
---|
720 | } |
---|
721 | else |
---|
722 | { |
---|
723 | return mail($to, $subject, $content, $headers); |
---|
724 | } |
---|
725 | } |
---|
726 | } |
---|
727 | else |
---|
728 | { |
---|
729 | return $result; |
---|
730 | } |
---|
731 | } |
---|
732 | |
---|
733 | function move_ccs_rules_to_body($content) |
---|
734 | { |
---|
735 | // We search all css rules in style tags |
---|
736 | preg_match('#<style>(.*?)</style>#s', $content, $matches); |
---|
737 | |
---|
738 | if (!empty($matches[1])) |
---|
739 | { |
---|
740 | preg_match_all('#([^\n]*?)\{(.*?)\}#s', $matches[1], $matches); |
---|
741 | |
---|
742 | $selectors = array(); |
---|
743 | $unknow_selectors = ''; |
---|
744 | |
---|
745 | foreach ($matches[1] as $key => $value) |
---|
746 | { |
---|
747 | $selects = explode(',', $value); |
---|
748 | $style = trim($matches[2][$key], ' ;'); |
---|
749 | |
---|
750 | foreach($selects as $select) |
---|
751 | { |
---|
752 | $select = trim($select); |
---|
753 | $selectors[$select][] = $style; |
---|
754 | } |
---|
755 | } |
---|
756 | |
---|
757 | foreach ($selectors as $selector => $style) |
---|
758 | { |
---|
759 | if (!preg_match('/^(#|\.|)([A-Za-z0-9_-]*)$/', $selector, $matches)) |
---|
760 | { |
---|
761 | $unknow_selectors .= $selector.' {'.implode(";\n", $style).";}\n"; |
---|
762 | } |
---|
763 | else switch ($matches[1]) |
---|
764 | { |
---|
765 | case '#': |
---|
766 | $content = preg_replace('|id="'.$matches[2].'"|', 'id="'.$matches[2].'" style="'.implode(";\n", $style).";\"\n", $content); |
---|
767 | break; |
---|
768 | case '.': |
---|
769 | $content = preg_replace('|class="'.$matches[2].'"|', 'class="'.$matches[2].'" style="'.implode(";\n", $style).";\"\n", $content); |
---|
770 | break; |
---|
771 | default: |
---|
772 | $content = preg_replace('#<'.$matches[2].'( |>)#', '<'.$matches[2].' style="'.implode(";\n", $style).";\"\n$1", $content); |
---|
773 | break; |
---|
774 | } |
---|
775 | } |
---|
776 | |
---|
777 | // Keep unknow tags in page head |
---|
778 | if (!empty($unknow_selectors)) |
---|
779 | { |
---|
780 | $content = preg_replace('#<style>.*?</style>#s', "<style type=\"text/css\">\n$unknow_selectors</style>", $content); |
---|
781 | } |
---|
782 | else |
---|
783 | { |
---|
784 | $content = preg_replace('#<style>.*?</style>#s', '', $content); |
---|
785 | } |
---|
786 | } |
---|
787 | return $content; |
---|
788 | } |
---|
789 | |
---|
790 | /*Testing block*/ |
---|
791 | function pwg_send_mail_test($result, $to, $subject, $content, $headers, $args) |
---|
792 | { |
---|
793 | global $conf, $user, $lang_info; |
---|
794 | $dir = PHPWG_ROOT_PATH.$conf['data_location'].'tmp'; |
---|
795 | if ( mkgetdir( $dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR) ) |
---|
796 | { |
---|
797 | $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'.'.$args['theme'].'-'.date('YmdHis'); |
---|
798 | if ($args['content_format'] == 'text/plain') |
---|
799 | { |
---|
800 | $filename .= '.txt'; |
---|
801 | } |
---|
802 | else |
---|
803 | { |
---|
804 | $filename .= '.html'; |
---|
805 | } |
---|
806 | $file = fopen($filename, 'w+'); |
---|
807 | fwrite($file, $to ."\n"); |
---|
808 | fwrite($file, $subject ."\n"); |
---|
809 | fwrite($file, $headers); |
---|
810 | fwrite($file, $content); |
---|
811 | fclose($file); |
---|
812 | } |
---|
813 | return $result; |
---|
814 | } |
---|
815 | if ($conf['debug_mail']) |
---|
816 | add_event_handler('send_mail', 'pwg_send_mail_test', EVENT_HANDLER_PRIORITY_NEUTRAL+10, 6); |
---|
817 | |
---|
818 | |
---|
819 | add_event_handler('send_mail', 'pwg_send_mail', EVENT_HANDLER_PRIORITY_NEUTRAL, 5); |
---|
820 | add_event_handler('send_mail_content', 'move_ccs_rules_to_body'); |
---|
821 | trigger_action('functions_mail_included'); |
---|
822 | |
---|
823 | ?> |
---|