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

Last change on this file since 19655 was 19225, checked in by mistic100, 12 years ago

clean some function desc

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