1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2014 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 | * @package functions\mail |
---|
26 | */ |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | * Returns the name of the mail sender |
---|
31 | * |
---|
32 | * @return string |
---|
33 | */ |
---|
34 | function get_mail_sender_name() |
---|
35 | { |
---|
36 | global $conf; |
---|
37 | |
---|
38 | return (empty($conf['mail_sender_name']) ? $conf['gallery_title'] : $conf['mail_sender_name']); |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * Returns the email of the mail sender |
---|
43 | * |
---|
44 | * @since 2.6 |
---|
45 | * @return string |
---|
46 | */ |
---|
47 | function get_mail_sender_email() |
---|
48 | { |
---|
49 | global $conf; |
---|
50 | |
---|
51 | return (empty($conf['mail_sender_email']) ? get_webmaster_mail_address() : $conf['mail_sender_email']); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Returns an array of mail configuration parameters. |
---|
56 | * - send_bcc_mail_webmaster |
---|
57 | * - mail_allow_html |
---|
58 | * - use_smtp |
---|
59 | * - smtp_host |
---|
60 | * - smtp_user |
---|
61 | * - smtp_password |
---|
62 | * - smtp_secure |
---|
63 | * - email_webmaster |
---|
64 | * - name_webmaster |
---|
65 | * |
---|
66 | * @return array |
---|
67 | */ |
---|
68 | function get_mail_configuration() |
---|
69 | { |
---|
70 | global $conf; |
---|
71 | |
---|
72 | $conf_mail = array( |
---|
73 | 'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'], |
---|
74 | 'mail_allow_html' => $conf['mail_allow_html'], |
---|
75 | 'mail_theme' => $conf['mail_theme'], |
---|
76 | 'use_smtp' => !empty($conf['smtp_host']), |
---|
77 | 'smtp_host' => $conf['smtp_host'], |
---|
78 | 'smtp_user' => $conf['smtp_user'], |
---|
79 | 'smtp_password' => $conf['smtp_password'], |
---|
80 | 'smtp_secure' => $conf['smtp_secure'], |
---|
81 | 'email_webmaster' => get_mail_sender_email(), |
---|
82 | 'name_webmaster' => get_mail_sender_name(), |
---|
83 | ); |
---|
84 | |
---|
85 | return $conf_mail; |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Returns an email address with an associated real name. |
---|
90 | * Can return either: |
---|
91 | * - email@domain.com |
---|
92 | * - name <email@domain.com> |
---|
93 | * |
---|
94 | * @param string $name |
---|
95 | * @param string $email |
---|
96 | * @return string |
---|
97 | */ |
---|
98 | function format_email($name, $email) |
---|
99 | { |
---|
100 | $cvt_email = trim(preg_replace('#[\n\r]+#s', '', $email)); |
---|
101 | $cvt_name = trim(preg_replace('#[\n\r]+#s', '', $name)); |
---|
102 | |
---|
103 | if ($cvt_name!="") |
---|
104 | { |
---|
105 | $cvt_name = '"'.addcslashes($cvt_name,'"').'"'.' '; |
---|
106 | } |
---|
107 | |
---|
108 | if (strpos($cvt_email, '<') === false) |
---|
109 | { |
---|
110 | return $cvt_name.'<'.$cvt_email.'>'; |
---|
111 | } |
---|
112 | else |
---|
113 | { |
---|
114 | return $cvt_name.$cvt_email; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * Returns the email and the name from a formatted address. |
---|
120 | * @since 2.6 |
---|
121 | * |
---|
122 | * @param string|string[] $input - if is an array must contain email[, name] |
---|
123 | * @return array email, name |
---|
124 | */ |
---|
125 | function unformat_email($input) |
---|
126 | { |
---|
127 | if (is_array($input)) |
---|
128 | { |
---|
129 | if (!isset($input['name'])) |
---|
130 | { |
---|
131 | $input['name'] = ''; |
---|
132 | } |
---|
133 | return $input; |
---|
134 | } |
---|
135 | |
---|
136 | if (preg_match('/(.*)<(.*)>.*/', $input, $matches)) |
---|
137 | { |
---|
138 | return array( |
---|
139 | 'email' => trim($matches[2]), |
---|
140 | 'name' => trim($matches[1]), |
---|
141 | ); |
---|
142 | } |
---|
143 | else |
---|
144 | { |
---|
145 | return array( |
---|
146 | 'email' => trim($input), |
---|
147 | 'name' => '', |
---|
148 | ); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Return a clean array of hashmaps (email, name) removing duplicates. |
---|
154 | * It accepts various inputs: |
---|
155 | * - comma separated list |
---|
156 | * - array of emails |
---|
157 | * - single hashmap (email[, name]) |
---|
158 | * - array of incomplete hashmaps |
---|
159 | * @since 2.6 |
---|
160 | * |
---|
161 | * @param mixed $data |
---|
162 | * @return string[][] |
---|
163 | */ |
---|
164 | function get_clean_recipients_list($data) |
---|
165 | { |
---|
166 | if (empty($data)) |
---|
167 | { |
---|
168 | return array(); |
---|
169 | } |
---|
170 | else if (is_array($data)) |
---|
171 | { |
---|
172 | $values = array_values($data); |
---|
173 | if (!is_array($values[0])) |
---|
174 | { |
---|
175 | $keys = array_keys($data); |
---|
176 | if (is_int($keys[0])) |
---|
177 | { // simple array of emails |
---|
178 | foreach ($data as &$item) |
---|
179 | { |
---|
180 | $item = array( |
---|
181 | 'email' => trim($item), |
---|
182 | 'name' => '', |
---|
183 | ); |
---|
184 | } |
---|
185 | unset($item); |
---|
186 | } |
---|
187 | else |
---|
188 | { // hashmap of one recipient |
---|
189 | $data = array(unformat_email($data)); |
---|
190 | } |
---|
191 | } |
---|
192 | else |
---|
193 | { // array of hashmaps |
---|
194 | $data = array_map('unformat_email', $data); |
---|
195 | } |
---|
196 | } |
---|
197 | else |
---|
198 | { |
---|
199 | $data = explode(',', $data); |
---|
200 | $data = array_map('unformat_email', $data); |
---|
201 | } |
---|
202 | |
---|
203 | $existing = array(); |
---|
204 | foreach ($data as $i => $entry) |
---|
205 | { |
---|
206 | if (isset($existing[ $entry['email'] ])) |
---|
207 | { |
---|
208 | unset($data[$i]); |
---|
209 | } |
---|
210 | else |
---|
211 | { |
---|
212 | $existing[ $entry['email'] ] = true; |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | return array_values($data); |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * Returns an email address list with minimal email string. |
---|
221 | * @deprecated 2.6 |
---|
222 | * |
---|
223 | * @param string $email_list - comma separated |
---|
224 | * @return string |
---|
225 | */ |
---|
226 | function get_strict_email_list($email_list) |
---|
227 | { |
---|
228 | $result = array(); |
---|
229 | $list = explode(',', $email_list); |
---|
230 | |
---|
231 | foreach ($list as $email) |
---|
232 | { |
---|
233 | if (strpos($email, '<') !== false) |
---|
234 | { |
---|
235 | $email = preg_replace('/.*<(.*)>.*/i', '$1', $email); |
---|
236 | } |
---|
237 | $result[] = trim($email); |
---|
238 | } |
---|
239 | |
---|
240 | return implode(',', array_unique($result)); |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | * Return an new mail template. |
---|
245 | * |
---|
246 | * @param string $email_format - text/html or text/plain |
---|
247 | * @return Template |
---|
248 | */ |
---|
249 | function &get_mail_template($email_format) |
---|
250 | { |
---|
251 | $template = new Template(PHPWG_ROOT_PATH.'themes', 'default', 'template/mail/'.$email_format); |
---|
252 | return $template; |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * Return string email format (text/html or text/plain). |
---|
257 | * |
---|
258 | * @param bool $is_html |
---|
259 | * @return string |
---|
260 | */ |
---|
261 | function get_str_email_format($is_html) |
---|
262 | { |
---|
263 | return ($is_html ? 'text/html' : 'text/plain'); |
---|
264 | } |
---|
265 | |
---|
266 | /** |
---|
267 | * Switch language to specified language. |
---|
268 | * All entries are push on language stack |
---|
269 | * |
---|
270 | * @param string $language |
---|
271 | */ |
---|
272 | function switch_lang_to($language) |
---|
273 | { |
---|
274 | global $switch_lang, $user, $lang, $lang_info, $language_files; |
---|
275 | |
---|
276 | // explanation of switch_lang |
---|
277 | // $switch_lang['language'] contains data of language |
---|
278 | // $switch_lang['stack'] contains stack LIFO |
---|
279 | // $switch_lang['initialisation'] allow to know if it's first call |
---|
280 | |
---|
281 | // Treatment with current user |
---|
282 | // Language of current user is saved (it's considered OK on firt call) |
---|
283 | if (!isset($switch_lang['initialisation']) and !isset($switch_lang['language'][$user['language']])) |
---|
284 | { |
---|
285 | $switch_lang['initialisation'] = true; |
---|
286 | $switch_lang['language'][$user['language']]['lang_info'] = $lang_info; |
---|
287 | $switch_lang['language'][$user['language']]['lang'] = $lang; |
---|
288 | } |
---|
289 | |
---|
290 | // Change current infos |
---|
291 | $switch_lang['stack'][] = $user['language']; |
---|
292 | $user['language'] = $language; |
---|
293 | |
---|
294 | // Load new data if necessary |
---|
295 | if (!isset($switch_lang['language'][$language])) |
---|
296 | { |
---|
297 | // Re-Init language arrays |
---|
298 | $lang_info = array(); |
---|
299 | $lang = array(); |
---|
300 | |
---|
301 | // language files |
---|
302 | load_language('common.lang', '', array('language'=>$language) ); |
---|
303 | // No test admin because script is checked admin (user selected no) |
---|
304 | // Translations are in admin file too |
---|
305 | load_language('admin.lang', '', array('language'=>$language) ); |
---|
306 | |
---|
307 | // Reload all plugins files (see load_language declaration) |
---|
308 | if (!empty($language_files)) |
---|
309 | { |
---|
310 | foreach ($language_files as $dirname => $files) |
---|
311 | foreach ($files as $filename) |
---|
312 | load_language($filename, $dirname, array('language'=>$language) ); |
---|
313 | } |
---|
314 | |
---|
315 | trigger_action('loading_lang'); |
---|
316 | load_language('lang', PHPWG_ROOT_PATH.PWG_LOCAL_DIR, |
---|
317 | array('language'=>$language, 'no_fallback'=>true, 'local'=>true) |
---|
318 | ); |
---|
319 | |
---|
320 | $switch_lang['language'][$language]['lang_info'] = $lang_info; |
---|
321 | $switch_lang['language'][$language]['lang'] = $lang; |
---|
322 | } |
---|
323 | else |
---|
324 | { |
---|
325 | $lang_info = $switch_lang['language'][$language]['lang_info']; |
---|
326 | $lang = $switch_lang['language'][$language]['lang']; |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | /** |
---|
331 | * Switch back language pushed with switch_lang_to() function. |
---|
332 | * @see switch_lang_to() |
---|
333 | */ |
---|
334 | function switch_lang_back() |
---|
335 | { |
---|
336 | global $switch_lang, $user, $lang, $lang_info; |
---|
337 | |
---|
338 | if (count($switch_lang['stack']) > 0) |
---|
339 | { |
---|
340 | // Get last value |
---|
341 | $language = array_pop($switch_lang['stack']); |
---|
342 | |
---|
343 | // Change current infos |
---|
344 | if (isset($switch_lang['language'][$language])) |
---|
345 | { |
---|
346 | $lang_info = $switch_lang['language'][$language]['lang_info']; |
---|
347 | $lang = $switch_lang['language'][$language]['lang']; |
---|
348 | } |
---|
349 | $user['language'] = $language; |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | /** |
---|
354 | * Send a notification email to all administrators. |
---|
355 | * current user (if admin) is not notified |
---|
356 | * |
---|
357 | * @param string|array $subject |
---|
358 | * @param string|array $content |
---|
359 | * @param boolean $send_technical_details - send user IP and browser |
---|
360 | * @return boolean |
---|
361 | */ |
---|
362 | function pwg_mail_notification_admins($subject, $content, $send_technical_details=true) |
---|
363 | { |
---|
364 | if (empty($subject) or empty($content)) |
---|
365 | { |
---|
366 | return false; |
---|
367 | } |
---|
368 | |
---|
369 | global $conf, $user; |
---|
370 | |
---|
371 | if (is_array($subject) or is_array($content)) |
---|
372 | { |
---|
373 | switch_lang_to(get_default_language()); |
---|
374 | |
---|
375 | if (is_array($subject)) |
---|
376 | { |
---|
377 | $subject = l10n_args($subject); |
---|
378 | } |
---|
379 | if (is_array($content)) |
---|
380 | { |
---|
381 | $content = l10n_args($content); |
---|
382 | } |
---|
383 | |
---|
384 | switch_lang_back(); |
---|
385 | } |
---|
386 | |
---|
387 | $tpl_vars = array(); |
---|
388 | if ($send_technical_details) |
---|
389 | { |
---|
390 | $tpl_vars['TECHNICAL'] = array( |
---|
391 | 'username' => stripslashes($user['username']), |
---|
392 | 'ip' => $_SERVER['REMOTE_ADDR'], |
---|
393 | 'user_agent' => $_SERVER['HTTP_USER_AGENT'], |
---|
394 | ); |
---|
395 | } |
---|
396 | |
---|
397 | return pwg_mail_admins( |
---|
398 | array( |
---|
399 | 'subject' => '['. $conf['gallery_title'] .'] '. $subject, |
---|
400 | 'mail_title' => $conf['gallery_title'], |
---|
401 | 'mail_subtitle' => $subject, |
---|
402 | 'content' => $content, |
---|
403 | 'content_format' => 'text/plain', |
---|
404 | ), |
---|
405 | array( |
---|
406 | 'filename' => 'notification_admin', |
---|
407 | 'assign' => $tpl_vars, |
---|
408 | ) |
---|
409 | ); |
---|
410 | } |
---|
411 | |
---|
412 | /** |
---|
413 | * Send a email to all administrators. |
---|
414 | * current user (if admin) is excluded |
---|
415 | * @see pwg_mail() |
---|
416 | * @since 2.6 |
---|
417 | * |
---|
418 | * @param array $args - as in pwg_mail() |
---|
419 | * @param array $tpl - as in pwg_mail() |
---|
420 | * @return boolean |
---|
421 | */ |
---|
422 | function pwg_mail_admins($args=array(), $tpl=array()) |
---|
423 | { |
---|
424 | if (empty($args['content']) and empty($tpl)) |
---|
425 | { |
---|
426 | return false; |
---|
427 | } |
---|
428 | |
---|
429 | global $conf, $user; |
---|
430 | $return = true; |
---|
431 | |
---|
432 | // get admins (except ourself) |
---|
433 | $query = ' |
---|
434 | SELECT |
---|
435 | u.'.$conf['user_fields']['username'].' AS name, |
---|
436 | u.'.$conf['user_fields']['email'].' AS email |
---|
437 | FROM '.USERS_TABLE.' AS u |
---|
438 | JOIN '.USER_INFOS_TABLE.' AS i |
---|
439 | ON i.user_id = u.'.$conf['user_fields']['id'].' |
---|
440 | WHERE i.status in (\'webmaster\', \'admin\') |
---|
441 | AND u.'.$conf['user_fields']['email'].' IS NOT NULL |
---|
442 | AND i.user_id <> '.$user['id'].' |
---|
443 | ORDER BY name |
---|
444 | ;'; |
---|
445 | $admins = array_from_query($query); |
---|
446 | |
---|
447 | if (empty($admins)) |
---|
448 | { |
---|
449 | return $return; |
---|
450 | } |
---|
451 | |
---|
452 | switch_lang_to(get_default_language()); |
---|
453 | |
---|
454 | $return = pwg_mail($admins, $args, $tpl); |
---|
455 | |
---|
456 | switch_lang_back(); |
---|
457 | |
---|
458 | return $return; |
---|
459 | } |
---|
460 | |
---|
461 | /** |
---|
462 | * Send an email to a group. |
---|
463 | * @see pwg_mail() |
---|
464 | * |
---|
465 | * @param int $group_id |
---|
466 | * @param array $args - as in pwg_mail() |
---|
467 | * o language_selected: filters users of the group by language [default value empty] |
---|
468 | * @param array $tpl - as in pwg_mail() |
---|
469 | * @return boolean |
---|
470 | */ |
---|
471 | function pwg_mail_group($group_id, $args=array(), $tpl=array()) |
---|
472 | { |
---|
473 | if (empty($group_id) or ( empty($args['content']) and empty($tpl) )) |
---|
474 | { |
---|
475 | return false; |
---|
476 | } |
---|
477 | |
---|
478 | global $conf; |
---|
479 | $return = true; |
---|
480 | |
---|
481 | // get distinct languages of targeted users |
---|
482 | $query = ' |
---|
483 | SELECT DISTINCT language |
---|
484 | FROM '.USER_GROUP_TABLE.' AS ug |
---|
485 | INNER JOIN '.USERS_TABLE.' AS u |
---|
486 | ON '.$conf['user_fields']['id'].' = ug.user_id |
---|
487 | INNER JOIN '.USER_INFOS_TABLE.' AS ui |
---|
488 | ON ui.user_id = ug.user_id |
---|
489 | WHERE group_id = '.$group_id.' |
---|
490 | AND '.$conf['user_fields']['email'].' <> ""'; |
---|
491 | if (!empty($args['language_selected'])) |
---|
492 | { |
---|
493 | $query .= ' |
---|
494 | AND language = \''.$args['language_selected'].'\''; |
---|
495 | } |
---|
496 | |
---|
497 | $query .= ' |
---|
498 | ;'; |
---|
499 | $languages = array_from_query($query, 'language'); |
---|
500 | |
---|
501 | if (empty($languages)) |
---|
502 | { |
---|
503 | return $return; |
---|
504 | } |
---|
505 | |
---|
506 | foreach ($languages as $language) |
---|
507 | { |
---|
508 | // get subset of users in this group for a specific language |
---|
509 | $query = ' |
---|
510 | SELECT |
---|
511 | u.'.$conf['user_fields']['username'].' AS name, |
---|
512 | u.'.$conf['user_fields']['email'].' AS email |
---|
513 | FROM '.USER_GROUP_TABLE.' AS ug |
---|
514 | INNER JOIN '.USERS_TABLE.' AS u |
---|
515 | ON '.$conf['user_fields']['id'].' = ug.user_id |
---|
516 | INNER JOIN '.USER_INFOS_TABLE.' AS ui |
---|
517 | ON ui.user_id = ug.user_id |
---|
518 | WHERE group_id = '.$group_id.' |
---|
519 | AND '.$conf['user_fields']['email'].' <> "" |
---|
520 | AND language = \''.$language.'\' |
---|
521 | ;'; |
---|
522 | $users = array_from_query($query); |
---|
523 | |
---|
524 | if (empty($users)) |
---|
525 | { |
---|
526 | continue; |
---|
527 | } |
---|
528 | |
---|
529 | switch_lang_to($language); |
---|
530 | |
---|
531 | $return&= pwg_mail(null, |
---|
532 | array_merge( |
---|
533 | $args, |
---|
534 | array('Bcc' => $users) |
---|
535 | ), |
---|
536 | $tpl |
---|
537 | ); |
---|
538 | |
---|
539 | switch_lang_back(); |
---|
540 | } |
---|
541 | |
---|
542 | return $return; |
---|
543 | } |
---|
544 | |
---|
545 | /** |
---|
546 | * Sends an email, using Piwigo specific informations. |
---|
547 | * |
---|
548 | * @param string|array $to |
---|
549 | * @param array $args |
---|
550 | * o from: sender [default value webmaster email] |
---|
551 | * o Cc: array of carbon copy receivers of the mail. [default value empty] |
---|
552 | * o Bcc: array of blind carbon copy receivers of the mail. [default value empty] |
---|
553 | * o subject [default value 'Piwigo'] |
---|
554 | * o content: content of mail [default value ''] |
---|
555 | * o content_format: format of mail content [default value 'text/plain'] |
---|
556 | * o email_format: global mail format [default value $conf_mail['default_email_format']] |
---|
557 | * o theme: theme to use [default value $conf_mail['mail_theme']] |
---|
558 | * o mail_title: main title of the mail [default value $conf['gallery_title']] |
---|
559 | * o mail_subtitle: subtitle of the mail [default value subject] |
---|
560 | * @param array $tpl - use these options to define a custom content template file |
---|
561 | * o filename |
---|
562 | * o dirname (optional) |
---|
563 | * o assign (optional) |
---|
564 | * |
---|
565 | * @return boolean |
---|
566 | */ |
---|
567 | function pwg_mail($to, $args=array(), $tpl=array()) |
---|
568 | { |
---|
569 | global $conf, $conf_mail, $lang_info, $page; |
---|
570 | |
---|
571 | if (empty($to) and empty($args['Cc']) and empty($args['Bcc'])) |
---|
572 | { |
---|
573 | return true; |
---|
574 | } |
---|
575 | |
---|
576 | if (!isset($conf_mail)) |
---|
577 | { |
---|
578 | $conf_mail = get_mail_configuration(); |
---|
579 | } |
---|
580 | |
---|
581 | include_once(PHPWG_ROOT_PATH.'include/phpmailer/class.phpmailer.php'); |
---|
582 | |
---|
583 | $mail = new PHPMailer; |
---|
584 | |
---|
585 | foreach (get_clean_recipients_list($to) as $recipient) |
---|
586 | { |
---|
587 | $mail->addAddress($recipient['email'], $recipient['name']); |
---|
588 | } |
---|
589 | |
---|
590 | $mail->WordWrap = 76; |
---|
591 | $mail->CharSet = 'UTF-8'; |
---|
592 | |
---|
593 | // Compute root_path in order have complete path |
---|
594 | set_make_full_url(); |
---|
595 | |
---|
596 | if (empty($args['from'])) |
---|
597 | { |
---|
598 | $from = array( |
---|
599 | 'email' => $conf_mail['email_webmaster'], |
---|
600 | 'name' => $conf_mail['name_webmaster'], |
---|
601 | ); |
---|
602 | } |
---|
603 | else |
---|
604 | { |
---|
605 | $from = unformat_email($args['from']); |
---|
606 | } |
---|
607 | $mail->setFrom($from['email'], $from['name']); |
---|
608 | $mail->addReplyTo($from['email'], $from['name']); |
---|
609 | |
---|
610 | // Subject |
---|
611 | if (empty($args['subject'])) |
---|
612 | { |
---|
613 | $args['subject'] = 'Piwigo'; |
---|
614 | } |
---|
615 | $args['subject'] = trim(preg_replace('#[\n\r]+#s', '', $args['subject'])); |
---|
616 | $mail->Subject = $args['subject']; |
---|
617 | |
---|
618 | // Cc |
---|
619 | if (!empty($args['Cc'])) |
---|
620 | { |
---|
621 | foreach (get_clean_recipients_list($args['Cc']) as $recipient) |
---|
622 | { |
---|
623 | $mail->addCC($recipient['email'], $recipient['name']); |
---|
624 | } |
---|
625 | } |
---|
626 | |
---|
627 | // Bcc |
---|
628 | $Bcc = get_clean_recipients_list(@$args['Bcc']); |
---|
629 | if ($conf_mail['send_bcc_mail_webmaster']) |
---|
630 | { |
---|
631 | $Bcc[] = array( |
---|
632 | 'email' => get_webmaster_mail_address(), |
---|
633 | 'name' => '', |
---|
634 | ); |
---|
635 | } |
---|
636 | if (!empty($Bcc)) |
---|
637 | { |
---|
638 | foreach ($Bcc as $recipient) |
---|
639 | { |
---|
640 | $mail->addBCC($recipient['email'], $recipient['name']); |
---|
641 | } |
---|
642 | } |
---|
643 | |
---|
644 | // theme |
---|
645 | if (empty($args['theme']) or !in_array($args['theme'], array('clear','dark'))) |
---|
646 | { |
---|
647 | $args['theme'] = $conf_mail['mail_theme']; |
---|
648 | } |
---|
649 | |
---|
650 | // content |
---|
651 | if (!isset($args['content'])) |
---|
652 | { |
---|
653 | $args['content'] = ''; |
---|
654 | } |
---|
655 | |
---|
656 | // try to decompose subject like "[....] ...." |
---|
657 | if (!isset($args['mail_title']) and !isset($args['mail_subtitle'])) |
---|
658 | { |
---|
659 | if (preg_match('#^\[(.*)\](.*)$#', $args['subject'], $matches)) |
---|
660 | { |
---|
661 | $args['mail_title'] = $matches[1]; |
---|
662 | $args['mail_subtitle'] = $matches[2]; |
---|
663 | } |
---|
664 | } |
---|
665 | if (!isset($args['mail_title'])) |
---|
666 | { |
---|
667 | $args['mail_title'] = $conf['gallery_title']; |
---|
668 | } |
---|
669 | if (!isset($args['mail_subtitle'])) |
---|
670 | { |
---|
671 | $args['mail_subtitle'] = $args['subject']; |
---|
672 | } |
---|
673 | |
---|
674 | // content type |
---|
675 | if (empty($args['content_format'])) |
---|
676 | { |
---|
677 | $args['content_format'] = 'text/plain'; |
---|
678 | } |
---|
679 | |
---|
680 | $content_type_list = array(); |
---|
681 | if ($conf_mail['mail_allow_html'] and @$args['email_format'] != 'text/plain') |
---|
682 | { |
---|
683 | $content_type_list[] = 'text/html'; |
---|
684 | } |
---|
685 | $content_type_list[] = 'text/plain'; |
---|
686 | |
---|
687 | $contents = array(); |
---|
688 | foreach ($content_type_list as $content_type) |
---|
689 | { |
---|
690 | // key compose of indexes witch allow to cache mail data |
---|
691 | $cache_key = $content_type.'-'.$lang_info['code']; |
---|
692 | |
---|
693 | if (!isset($conf_mail[$cache_key])) |
---|
694 | { |
---|
695 | // instanciate a new Template |
---|
696 | if (!isset($conf_mail[$cache_key]['theme'])) |
---|
697 | { |
---|
698 | $conf_mail[$cache_key]['theme'] = get_mail_template($content_type); |
---|
699 | trigger_action('before_parse_mail_template', $cache_key, $content_type); |
---|
700 | } |
---|
701 | $template = &$conf_mail[$cache_key]['theme']; |
---|
702 | |
---|
703 | $template->set_filename('mail_header', 'header.tpl'); |
---|
704 | $template->set_filename('mail_footer', 'footer.tpl'); |
---|
705 | |
---|
706 | $template->assign( |
---|
707 | array( |
---|
708 | 'GALLERY_URL' => get_gallery_home_url(), |
---|
709 | 'GALLERY_TITLE' => isset($page['gallery_title']) ? $page['gallery_title'] : $conf['gallery_title'], |
---|
710 | 'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '', |
---|
711 | 'PHPWG_URL' => defined('PHPWG_URL') ? PHPWG_URL : '', |
---|
712 | 'CONTENT_ENCODING' => get_pwg_charset(), |
---|
713 | 'CONTACT_MAIL' => $conf_mail['email_webmaster'], |
---|
714 | ) |
---|
715 | ); |
---|
716 | |
---|
717 | if ($content_type == 'text/html') |
---|
718 | { |
---|
719 | if ($template->smarty->templateExists('global-mail-css.tpl')) |
---|
720 | { |
---|
721 | $template->set_filename('global-css', 'global-mail-css.tpl'); |
---|
722 | $template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'global-css'); |
---|
723 | } |
---|
724 | |
---|
725 | if ($template->smarty->templateExists('mail-css-'. $args['theme'] .'.tpl')) |
---|
726 | { |
---|
727 | $template->set_filename('css', 'mail-css-'. $args['theme'] .'.tpl'); |
---|
728 | $template->assign_var_from_handle('MAIL_CSS', 'css'); |
---|
729 | } |
---|
730 | } |
---|
731 | } |
---|
732 | |
---|
733 | $template = &$conf_mail[$cache_key]['theme']; |
---|
734 | $template->assign( |
---|
735 | array( |
---|
736 | 'MAIL_TITLE' => $args['mail_title'], |
---|
737 | 'MAIL_SUBTITLE' => $args['mail_subtitle'], |
---|
738 | ) |
---|
739 | ); |
---|
740 | |
---|
741 | // Header |
---|
742 | $contents[$content_type] = $template->parse('mail_header', true); |
---|
743 | |
---|
744 | // Content |
---|
745 | // Stored in a temp variable, if a content template is used it will be assigned |
---|
746 | // to the $CONTENT template variable, otherwise it will be appened to the mail |
---|
747 | if ($args['content_format'] == 'text/plain' and $content_type == 'text/html') |
---|
748 | { |
---|
749 | // convert plain text to html |
---|
750 | $mail_content = |
---|
751 | '<p>'. |
---|
752 | nl2br( |
---|
753 | preg_replace( |
---|
754 | '/(https?:\/\/([-\w\.]+[-\w])+(:\d+)?(\/([\w\/_\.\#-]*(\?\S+)?[^\.\s])?)?)/i', |
---|
755 | '<a href="$1">$1</a>', |
---|
756 | htmlspecialchars($args['content']) |
---|
757 | ) |
---|
758 | ). |
---|
759 | '</p>'; |
---|
760 | } |
---|
761 | else if ($args['content_format'] == 'text/html' and $content_type == 'text/plain') |
---|
762 | { |
---|
763 | // convert html text to plain text |
---|
764 | $mail_content = strip_tags($args['content']); |
---|
765 | } |
---|
766 | else |
---|
767 | { |
---|
768 | $mail_content = $args['content']; |
---|
769 | } |
---|
770 | |
---|
771 | // Runtime template |
---|
772 | if (isset($tpl['filename'])) |
---|
773 | { |
---|
774 | if (isset($tpl['dirname'])) |
---|
775 | { |
---|
776 | $template->set_template_dir($tpl['dirname'] .'/'. $content_type); |
---|
777 | } |
---|
778 | if ($template->smarty->templateExists($tpl['filename'] .'.tpl')) |
---|
779 | { |
---|
780 | $template->set_filename($tpl['filename'], $tpl['filename'] .'.tpl'); |
---|
781 | if (!empty($tpl['assign'])) |
---|
782 | { |
---|
783 | $template->assign($tpl['assign']); |
---|
784 | } |
---|
785 | $template->assign('CONTENT', $mail_content); |
---|
786 | $contents[$content_type].= $template->parse($tpl['filename'], true); |
---|
787 | } |
---|
788 | else |
---|
789 | { |
---|
790 | $contents[$content_type].= $mail_content; |
---|
791 | } |
---|
792 | } |
---|
793 | else |
---|
794 | { |
---|
795 | $contents[$content_type].= $mail_content; |
---|
796 | } |
---|
797 | |
---|
798 | // Footer |
---|
799 | $contents[$content_type].= $template->parse('mail_footer', true); |
---|
800 | } |
---|
801 | |
---|
802 | // Undo Compute root_path in order have complete path |
---|
803 | unset_make_full_url(); |
---|
804 | |
---|
805 | // Send content to PHPMailer |
---|
806 | if (isset($contents['text/html'])) |
---|
807 | { |
---|
808 | $mail->isHTML(true); |
---|
809 | $mail->Body = move_css_to_body($contents['text/html']); |
---|
810 | |
---|
811 | if (isset($contents['text/plain'])) |
---|
812 | { |
---|
813 | $mail->AltBody = $contents['text/plain']; |
---|
814 | } |
---|
815 | } |
---|
816 | else |
---|
817 | { |
---|
818 | $mail->isHTML(false); |
---|
819 | $mail->Body = $contents['text/plain']; |
---|
820 | } |
---|
821 | |
---|
822 | if ($conf_mail['use_smtp']) |
---|
823 | { |
---|
824 | // now we need to split port number |
---|
825 | if (strpos($conf_mail['smtp_host'], ':') !== false) |
---|
826 | { |
---|
827 | list($smtp_host, $smtp_port) = explode(':', $conf_mail['smtp_host']); |
---|
828 | } |
---|
829 | else |
---|
830 | { |
---|
831 | $smtp_host = $conf_mail['smtp_host']; |
---|
832 | $smtp_port = 25; |
---|
833 | } |
---|
834 | |
---|
835 | $mail->IsSMTP(); |
---|
836 | |
---|
837 | // enables SMTP debug information (for testing) 2 - debug, 0 - no message |
---|
838 | $mail->SMTPDebug = 0; |
---|
839 | |
---|
840 | $mail->Host = $smtp_host; |
---|
841 | $mail->Port = $smtp_port; |
---|
842 | |
---|
843 | if (!empty($conf_mail['smtp_secure']) and in_array($conf_mail['smtp_secure'], array('ssl', 'tls'))) |
---|
844 | { |
---|
845 | $mail->SMTPSecure = $conf_mail['smtp_secure']; |
---|
846 | } |
---|
847 | |
---|
848 | if (!empty($conf_mail['smtp_user'])) |
---|
849 | { |
---|
850 | $mail->SMTPAuth = true; |
---|
851 | $mail->Username = $conf_mail['smtp_user']; |
---|
852 | $mail->Password = $conf_mail['smtp_password']; |
---|
853 | } |
---|
854 | } |
---|
855 | |
---|
856 | $ret = true; |
---|
857 | $pre_result = trigger_event('before_send_mail', true, $to, $args, $mail); |
---|
858 | |
---|
859 | if ($pre_result) |
---|
860 | { |
---|
861 | $ret = $mail->send(); |
---|
862 | if (!$ret and (!ini_get('display_errors') or is_admin())) |
---|
863 | { |
---|
864 | trigger_error('Mailer Error: ' . $mail->ErrorInfo, E_USER_WARNING); |
---|
865 | } |
---|
866 | if ($conf['debug_mail']) |
---|
867 | { |
---|
868 | pwg_send_mail_test($ret, $mail, $args); |
---|
869 | } |
---|
870 | } |
---|
871 | |
---|
872 | return $ret; |
---|
873 | } |
---|
874 | |
---|
875 | /** |
---|
876 | * @deprecated 2.6 |
---|
877 | */ |
---|
878 | function pwg_send_mail($result, $to, $subject, $content, $headers) |
---|
879 | { |
---|
880 | if (is_admin()) |
---|
881 | { |
---|
882 | trigger_error('pwg_send_mail function is deprecated', E_USER_NOTICE); |
---|
883 | } |
---|
884 | |
---|
885 | if (!$result) |
---|
886 | { |
---|
887 | return pwg_mail($to, array( |
---|
888 | 'content' => $content, |
---|
889 | 'subject' => $subject, |
---|
890 | )); |
---|
891 | } |
---|
892 | else |
---|
893 | { |
---|
894 | return $result; |
---|
895 | } |
---|
896 | } |
---|
897 | |
---|
898 | /** |
---|
899 | * Moves CSS rules contained in the <style> tag to inline CSS. |
---|
900 | * Used for compatibility with Gmail and such clients |
---|
901 | * @since 2.6 |
---|
902 | * |
---|
903 | * @param string $content |
---|
904 | * @return string |
---|
905 | */ |
---|
906 | function move_css_to_body($content) |
---|
907 | { |
---|
908 | include_once(PHPWG_ROOT_PATH.'include/emogrifier.class.php'); |
---|
909 | |
---|
910 | $e = new Emogrifier($content); |
---|
911 | return $e->emogrify(); |
---|
912 | } |
---|
913 | |
---|
914 | /** |
---|
915 | * Saves a copy of the mail if _data/tmp. |
---|
916 | * |
---|
917 | * @param boolean $success |
---|
918 | * @param PHPMailer $mail |
---|
919 | * @param array $args |
---|
920 | */ |
---|
921 | function pwg_send_mail_test($success, $mail, $args) |
---|
922 | { |
---|
923 | global $conf, $user, $lang_info; |
---|
924 | |
---|
925 | $dir = PHPWG_ROOT_PATH.$conf['data_location'].'tmp'; |
---|
926 | if (mkgetdir($dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR)) |
---|
927 | { |
---|
928 | $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'-'.date('YmdHis').($success ? '' : '.ERROR'); |
---|
929 | if ($args['content_format'] == 'text/plain') |
---|
930 | { |
---|
931 | $filename .= '.txt'; |
---|
932 | } |
---|
933 | else |
---|
934 | { |
---|
935 | $filename .= '.html'; |
---|
936 | } |
---|
937 | |
---|
938 | $file = fopen($filename, 'w+'); |
---|
939 | if (!$success) |
---|
940 | { |
---|
941 | fwrite($file, "ERROR: " . $mail->ErrorInfo . "\n\n"); |
---|
942 | } |
---|
943 | fwrite($file, $mail->getSentMIMEMessage()); |
---|
944 | fclose($file); |
---|
945 | } |
---|
946 | } |
---|
947 | |
---|
948 | trigger_action('functions_mail_included'); |
---|
949 | |
---|
950 | ?> |
---|