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