source: trunk/include/functions_mail.inc.php @ 10214

Last change on this file since 10214 was 9360, checked in by plg, 13 years ago

feature added: ability to avoid sending technical details with pwg_mail_notification_admins

change: pwg_mail_notification_admins forces the format to be text/plain

  • Property svn:eol-style set to LF
File size: 23.0 KB
RevLine 
[1021]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[8728]5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[1019]23
24// +-----------------------------------------------------------------------+
25// |                               functions                               |
26// +-----------------------------------------------------------------------+
[1021]27
[2121]28
29/**
30 * Encodes a string using Q form if required (RFC2045)
31 * mail headers MUST contain only US-ASCII characters
32 */
33function 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;
[2126]48  return '=?'.get_pwg_charset().'?Q?'.$str.'?=';
[2121]49}
50
[1019]51/*
[2284]52 * Returns the name of the mail sender :
53 *
54 * @return string
55 */
56function 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/*
[1021]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']
[2339]70 * - text_footer: Piwigo and version
[1021]71 *
72 * @return array
[1019]73 */
[1021]74function get_mail_configuration()
75{
76  global $conf;
[1019]77
[1021]78  $conf_mail = array(
79    'mail_options' => $conf['mail_options'],
[2106]80    'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'],
81    'default_email_format' => $conf['default_email_format'],
[3938]82    'alternative_email_format' => $conf['alternative_email_format'],
[2106]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']
[1021]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'] =
[2284]94    format_email(get_mail_sender_name(), $conf_mail['email_webmaster']);
[1021]95
[1809]96  $conf_mail['boundary_key'] = generate_key(32);
97
[1021]98  return $conf_mail;
99}
100
[1019]101/**
[1021]102 * Returns an email address with an associated real name
103 *
104 * @param string name
105 * @param string email
106 */
107function format_email($name, $email)
108{
[2280]109  // Spring cleaning
110  $cvt_email = trim(preg_replace('#[\n\r]+#s', '', $email));
[2284]111  $cvt_name = trim(preg_replace('#[\n\r]+#s', '', $name));
[2280]112
[2284]113  if ($cvt_name!="")
[1021]114  {
[2284]115    $cvt_name = encode_mime_header(
116              '"'
117              .addcslashes($cvt_name,'"')
118              .'"');
119    $cvt_name .= ' ';
120  }
[1531]121
[2284]122  if (strpos($cvt_email, '<') === false)
123  {
124    return $cvt_name.'<'.$cvt_email.'>';
[1021]125  }
126  else
127  {
[2284]128    return $cvt_name.$cvt_email;
[1021]129  }
130}
131
[2106]132/**
[2284]133 * Returns an email address list with minimal email string
134 *
135 * @param string with email list (email separated by comma)
136 */
137function 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
[2106]153
154/**
155 * Return an new mail template
156 *
157 * @params:
158 *   - email_format: mail format
159 *   - args: function params of mail function:
[5123]160 *       o template: template to use [default get_default_theme()]
161 *       o theme: template to use [default get_default_theme()]
[2106]162 */
[5123]163function & get_mail_template($email_format, $theme='')
[2106]164{
[5123]165  if (empty($theme))
166  {
167    $theme = get_default_theme();
168  }
[2106]169
[5123]170  $mail_template = new Template(PHPWG_ROOT_PATH.'themes', $theme, 'template/mail/'.$email_format);
171
[2106]172  return $mail_template;
173}
174
175/**
[2121]176 * Return string email format (html or not)
[2106]177 *
178 * @param string format
179 */
180function get_str_email_format($is_html)
181{
182  return ($is_html ? 'text/html' : 'text/plain');
183}
184
[2121]185/*
[1904]186 * Switch language to param language
187 * All entries are push on language stack
188 *
189 * @param string language
190 */
191function switch_lang_to($language)
192{
193  global $switch_lang, $user, $lang, $lang_info;
194
[4908]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']]))
[1904]203  {
[4908]204    $switch_lang['initialisation'] = true;
205    $switch_lang['language'][$user['language']]['lang_info'] = $lang_info;
206    $switch_lang['language'][$user['language']]['lang'] = $lang;
[1904]207  }
208
[4908]209  // Change current infos
210  $switch_lang['stack'][] = $user['language'];
211  $user['language'] = $language;
[1904]212
[4908]213  // Load new data if necessary
214  if (!isset($switch_lang['language'][$language]))
[1904]215  {
[4908]216    // Re-Init language arrays
217    $lang_info = array();
218    $lang  = array();
[1904]219
[4908]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');
[8722]226    load_language('lang', PHPWG_ROOT_PATH.PWG_LOCAL_DIR,
[5208]227      array('language'=>$language, 'no_fallback'=>true, 'local'=>true)
228    );
[1904]229
[4908]230    $switch_lang['language'][$language]['lang_info'] = $lang_info;
231    $switch_lang['language'][$language]['lang'] = $lang;
[1904]232  }
[4908]233  else
234  {
235    $lang_info = $switch_lang['language'][$language]['lang_info'];
236    $lang = $switch_lang['language'][$language]['lang'];
237  }
[1904]238}
239
[2121]240/*
[1904]241 * Switch back language pushed with switch_lang_to function
242 *
243 * @param: none
244 */
245function switch_lang_back()
246{
247  global $switch_lang, $user, $lang, $lang_info;
248
249  if (count($switch_lang['stack']) > 0)
250  {
[4908]251    // Get last value
252    $language = array_pop($switch_lang['stack']);
[1904]253
[4908]254    // Change current infos
255    if (isset($switch_lang['language'][$language]))
[1904]256    {
[4908]257      $lang_info = $switch_lang['language'][$language]['lang_info'];
258      $lang = $switch_lang['language'][$language]['lang'];
[1904]259    }
[2140]260    $user['language'] = $language;
[1904]261  }
262}
[1908]263
264/**
265 * Returns email of all administrator
266 *
267 * @return string
268 */
269/*
[2106]270 * send en notification email to all administrators
[2121]271 * if a administrator is doing action,
[1908]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 */
[9360]280function pwg_mail_notification_admins($keyargs_subject, $keyargs_content, $send_technical_details=true)
[1908]281{
[9360]282  global $conf, $user;
283 
[2140]284  // Check arguments
[9360]285  if (empty($keyargs_subject) or empty($keyargs_content))
[2140]286  {
287    return false;
288  }
289
[1908]290  $return = true;
291
292  $admins = array();
293
294  $query = '
[9360]295SELECT
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;';
[1908]305
306  $datas = pwg_query($query);
307  if (!empty($datas))
308  {
[4325]309    while ($admin = pwg_db_fetch_assoc($datas))
[1908]310    {
311      if (!empty($admin['mail_address']))
312      {
313        array_push($admins, format_email($admin['username'], $admin['mail_address']));
314      }
315    }
316  }
317
[1914]318  if (count($admins) > 0)
[2106]319  {
[9360]320    $keyargs_content_admin_info = array(
[4304]321      get_l10n_args('Connected user: %s', stripslashes($user['username'])),
[2106]322      get_l10n_args('IP: %s', $_SERVER['REMOTE_ADDR']),
323      get_l10n_args('Browser: %s', $_SERVER['HTTP_USER_AGENT'])
[9360]324      );
[1908]325
[1926]326    switch_lang_to(get_default_language());
[1908]327
[9360]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(
[1914]337        'subject' => '['.$conf['gallery_title'].'] '.l10n_args($keyargs_subject),
[9360]338        'content' => $content,
339        'content_format' => 'text/plain',
340        'email_format' => 'text/plain',
341        )
342      );
343   
[1914]344    switch_lang_back();
345  }
346
[1908]347  return $return;
348}
[2106]349
[1904]350/*
351 * send en email to user's group
352 *
[2106]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
[1915]357 *   - dirname: short name of directory including template
358 *   - tpl_shortname: short template name without extension
[2106]359 *   - assign_vars: array used to assign_vars to mail template
360 *   - language_selected: send mail only to user with this selected language
[1908]361 *
362 * @return boolean (Ok or not)
363 */
[2106]364function pwg_mail_group(
[2121]365  $group_id, $email_format, $keyargs_subject,
[2629]366  $tpl_shortname,
[1915]367  $assign_vars = array(), $language_selected = '')
[1904]368{
[2140]369  // Check arguments
[2278]370  if
[2140]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
[1904]381  global $conf;
[1908]382  $return = true;
[1904]383
384  $query = '
385SELECT
[5123]386  distinct language, theme
[2121]387FROM
388  '.USER_GROUP_TABLE.' as ug
[1904]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
[2121]391WHERE
[1904]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
[2106]404  $result = pwg_query($query);
[1904]405
[4325]406  if (pwg_db_num_rows($result) > 0)
[2106]407  {
408    $list = array();
[4325]409    while ($row = pwg_db_fetch_assoc($result))
[2106]410    {
411      $list[] = $row;
412    }
413
[2129]414    foreach ($list as $elem)
415    {
416      $query = '
[1904]417SELECT
418  u.'.$conf['user_fields']['username'].' as username,
419  u.'.$conf['user_fields']['email'].' as mail_address
[2121]420FROM
421  '.USER_GROUP_TABLE.' as ug
[1904]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
[2121]424WHERE
[1904]425        '.$conf['user_fields']['email'].' IS NOT NULL
426    AND group_id = '.$group_id.'
[2106]427    AND language = \''.$elem['language'].'\'
[5123]428    AND theme = \''.$elem['theme'].'\'
[1904]429;';
430
[2129]431      $result = pwg_query($query);
[1904]432
[4325]433      if (pwg_db_num_rows($result) > 0)
[1904]434      {
[2129]435        $Bcc = array();
[4325]436        while ($row = pwg_db_fetch_assoc($result))
[1904]437        {
[2129]438          if (!empty($row['mail_address']))
439          {
[4304]440            array_push($Bcc, format_email(stripslashes($row['username']), $row['mail_address']));
[2129]441          }
[1904]442        }
443
[2129]444        if (count($Bcc) > 0)
445        {
446          switch_lang_to($elem['language']);
[1904]447
[5123]448          $mail_template = get_mail_template($email_format, $elem['theme']);
[2629]449          $mail_template->set_filename($tpl_shortname, $tpl_shortname.'.tpl');
[1904]450
[2278]451          $mail_template->assign(
[2140]452            trigger_event('mail_group_assign_vars', $assign_vars));
453
[2129]454          $return = pwg_mail
[2106]455          (
[2129]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;
[1904]467
[2129]468          switch_lang_back();
469        }
[1914]470      }
[1904]471    }
472  }
[1908]473
474  return $return;
[1904]475}
476
477/*
[2339]478 * sends an email, using Piwigo specific informations
[1809]479 *
480 * @param:
[2284]481 *   - to: receiver(s) of the mail (list separated by comma).
[1809]482 *   - args: function params of mail function:
[2106]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]
[2339]486 *       o subject  [default value 'Piwigo']
[1809]487 *       o content: content of mail    [default value '']
488 *       o content_format: format of mail content  [default value 'text/plain']
[2106]489 *       o email_format: global mail format  [default value $conf_mail['default_email_format']]
[5123]490 *       o theme: template to use [default get_default_theme()]
[1908]491 *
492 * @return boolean (Ok or not)
[2106]493 */
[1809]494function pwg_mail($to, $args = array())
[1019]495{
[1809]496  global $conf, $conf_mail, $lang_info, $page;
[2106]497
498  if (empty($to) and empty($args['Cc']) and empty($args['Bcc']))
499  {
500    return true;
501  }
[2121]502
[1021]503  if (!isset($conf_mail))
504  {
505    $conf_mail = get_mail_configuration();
506  }
[2106]507
508  if (empty($args['email_format']))
509  {
510    $args['email_format'] = $conf_mail['default_email_format'];
511  }
512
[1676]513  // Compute root_path in order have complete path
[3938]514  set_make_full_url();
[2106]515
[1809]516  if (empty($args['from']))
[1021]517  {
[1809]518    $args['from'] = $conf_mail['formated_email_webmaster'];
[1021]519  }
520  else
521  {
[1809]522    $args['from'] = format_email('', $args['from']);
[1021]523  }
524
[1809]525  if (empty($args['subject']))
526  {
[2339]527    $args['subject'] = 'Piwigo';
[1809]528  }
[2106]529  // Spring cleaning
530  $cvt_subject = trim(preg_replace('#[\n\r]+#s', '', $args['subject']));
531  // Ascii convertion
[2121]532  $cvt_subject = encode_mime_header($cvt_subject);
[1809]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
[2106]544  if ($conf_mail['send_bcc_mail_webmaster'])
545  {
546    $args['Bcc'][] = $conf_mail['formated_email_webmaster'];
547  }
548
[5123]549  if (empty($args['theme']))
550  {
551    $args['theme'] = get_default_theme();
552  }
[2106]553
[1809]554  $headers = 'From: '.$args['from']."\n";
[2106]555  $headers.= 'Reply-To: '.$args['from']."\n";
[1818]556
[2106]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
[1809]567  $headers.= 'Content-Type: multipart/alternative;'."\n";
568  $headers.= '  boundary="---='.$conf_mail['boundary_key'].'";'."\n";
[2106]569  $headers.= '  reply-type=original'."\n";
570  $headers.= 'MIME-Version: 1.0'."\n";
571  $headers.= 'X-Mailer: Piwigo Mailer'."\n";
[1532]572
[3938]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
[2106]580  $content = '';
581
[3938]582  foreach (array_unique($content_type_list) as $content_type)
583  {
584    // key compose of indexes witch allow ti cache mail data
[5123]585    $cache_key = $content_type.'-'.$lang_info['code'].'-'.$args['theme'];
[2129]586
[3938]587    if (!isset($conf_mail[$cache_key]))
[2106]588    {
[5123]589      if (!isset($conf_mail[$cache_key]['theme']))
[3938]590      {
[5796]591        $conf_mail[$cache_key]['theme'] = get_mail_template($content_type, $args['theme']);
[3938]592      }
[2106]593
[5123]594      $conf_mail[$cache_key]['theme']->set_filename('mail_header', 'header.tpl');
595      $conf_mail[$cache_key]['theme']->set_filename('mail_footer', 'footer.tpl');
[2106]596
[5123]597      $conf_mail[$cache_key]['theme']->assign(
[3938]598        array(
599          //Header
600          'BOUNDARY_KEY' => $conf_mail['boundary_key'],
601          'CONTENT_TYPE' => $content_type,
602          'CONTENT_ENCODING' => get_pwg_charset(),
[2121]603
[3938]604          // Footer
[6411]605          'GALLERY_URL' => get_gallery_home_url(),
[3938]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,
[2106]611
[5021]612          'TITLE_MAIL' => urlencode(l10n('A comment on your site')),
[3938]613          'MAIL' => get_webmaster_mail_address()
614          ));
[2106]615
[3938]616      if ($content_type == 'text/html')
[2106]617      {
[5125]618        if ($conf_mail[$cache_key]['theme']->smarty->template_exists('global-mail-css.tpl'))
[3938]619        {
[5123]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');
[3938]622        }
[2106]623
[5123]624        $file = PHPWG_ROOT_PATH.'themes/'.$args['theme'].'/mail-css.tpl';
[3938]625        if (is_file($file))
626        {
[5123]627          $conf_mail[$cache_key]['theme']->set_filename('css', realpath($file));
628          $conf_mail[$cache_key]['theme']->assign_var_from_handle('MAIL_CSS', 'css');
[3938]629        }
[2106]630      }
631
[3938]632      // what are displayed on the header of each mail ?
633      $conf_mail[$cache_key]['header'] =
[5123]634        $conf_mail[$cache_key]['theme']->parse('mail_header', true);
[1784]635
[3938]636      // what are displayed on the footer of each mail ?
637      $conf_mail[$cache_key]['footer'] =
[5123]638        $conf_mail[$cache_key]['theme']->parse('mail_footer', true);
[3938]639    }
[1809]640
[3938]641    // Header
642    $content.= $conf_mail[$cache_key]['header'];
[1019]643
[3938]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    }
[1809]663
[3938]664    // Footer
665    $content.= $conf_mail[$cache_key]['footer'];
[1809]666
667  // Close boundary
668  $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n";
[3938]669  }
[1809]670
[3938]671  //~ // Close boundary
672  //~ $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n";
673
[1676]674   // Undo Compute root_path in order have complete path
[3938]675  unset_make_full_url();
[1642]676
[2140]677  return
678    trigger_event('send_mail',
679      false, /* Result */
[2284]680      trigger_event('send_mail_to', get_strict_email_list($to)),
[2140]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.
[2339]694 *   - subject  [default value 'Piwigo']
[2140]695 *   - content: content of mail
696 *   - headers: headers of mail
697 *
698 * @return boolean (Ok or not)
699 */
700function pwg_send_mail($result, $to, $subject, $content, $headers)
701{
[2280]702  if (!$result)
703  {
[2140]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    }
[2280]726  }
727  else
728  {
729    return $result;
730  }
[2140]731}
732
[5695]733function 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    }
[6706]756
[5695]757    foreach ($selectors as $selector => $style)
758    {
759      if (!preg_match('/^(#|\.|)([A-Za-z0-9_-]*)$/', $selector, $matches))
760      {
[6706]761        $unknow_selectors .= $selector.' {'.implode(";\n", $style).";}\n";
[5695]762      }
763      else switch ($matches[1])
764      {
765        case '#':
[6706]766          $content = preg_replace('|id="'.$matches[2].'"|', 'id="'.$matches[2].'" style="'.implode(";\n", $style).";\"\n", $content);
[5695]767          break;
768        case '.':
[6706]769          $content = preg_replace('|class="'.$matches[2].'"|', 'class="'.$matches[2].'" style="'.implode(";\n", $style).";\"\n", $content);
[5695]770          break;
771        default:
[6706]772          $content = preg_replace('#<'.$matches[2].'( |>)#', '<'.$matches[2].' style="'.implode(";\n", $style).";\"\n$1", $content);
[5695]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
[2278]790/*Testing block*/
[6411]791function pwg_send_mail_test($result, $to, $subject, $content, $headers, $args)
[2140]792{
[2278]793    global $conf, $user, $lang_info;
794    $dir = $conf['local_data_dir'].'/tmp';
[2497]795    if ( mkgetdir( $dir,  MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR) )
[1784]796    {
[6411]797      $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'.'.$args['theme'].'-'.date('YmdHis');
[2497]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);
[1784]812    }
[2278]813    return $result;
[1021]814}
[6411]815if ($conf['debug_mail'])
816  add_event_handler('send_mail', 'pwg_send_mail_test', EVENT_HANDLER_PRIORITY_NEUTRAL+10, 6);
[1105]817
[2140]818
819add_event_handler('send_mail', 'pwg_send_mail', EVENT_HANDLER_PRIORITY_NEUTRAL, 5);
[5695]820add_event_handler('send_mail_content', 'move_ccs_rules_to_body');
[2140]821trigger_action('functions_mail_included');
822
[1321]823?>
Note: See TracBrowser for help on using the repository browser.