root/trunk/include/functions_mail.inc.php @ 1912

Revision 1912, 18.4 KB (checked in by rub, 6 years ago)

Update svn properties (svn:eol-style and svn:keywords)

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