diff options
Diffstat (limited to 'include/functions_mail.inc.php')
-rw-r--r-- | include/functions_mail.inc.php | 1032 |
1 files changed, 583 insertions, 449 deletions
diff --git a/include/functions_mail.inc.php b/include/functions_mail.inc.php index 7ad0b1853..20dd9e610 100644 --- a/include/functions_mail.inc.php +++ b/include/functions_mail.inc.php @@ -2,7 +2,7 @@ // +-----------------------------------------------------------------------+ // | Piwigo - a PHP based photo gallery | // +-----------------------------------------------------------------------+ -// | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org | +// | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | // +-----------------------------------------------------------------------+ @@ -21,59 +21,47 @@ // | USA. | // +-----------------------------------------------------------------------+ -// +-----------------------------------------------------------------------+ -// | functions | -// +-----------------------------------------------------------------------+ +/** + * @package functions\mail + */ /** - * Encodes a string using Q form if required (RFC2045) - * mail headers MUST contain only US-ASCII characters + * Returns the name of the mail sender + * + * @return string */ -function encode_mime_header($str) +function get_mail_sender_name() { - $x = preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches); - if ($x==0) - { - return $str; - } - // Replace every high ascii, control =, ? and _ characters - $str = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e', - "'='.sprintf('%02X', ord('\\1'))", $str); - - // Replace every spaces to _ (more readable than =20) - $str = str_replace(" ", "_", $str); + global $conf; - global $lang_info; - return '=?'.get_pwg_charset().'?Q?'.$str.'?='; + return (empty($conf['mail_sender_name']) ? $conf['gallery_title'] : $conf['mail_sender_name']); } -/* - * Returns the name of the mail sender : +/** + * Returns the email of the mail sender * + * @since 2.6 * @return string */ -function get_mail_sender_name() +function get_mail_sender_email() { global $conf; - return (empty($conf['mail_sender_name']) ? $conf['gallery_title'] : $conf['mail_sender_name']); + return (empty($conf['mail_sender_email']) ? get_webmaster_mail_address() : $conf['mail_sender_email']); } -/* - * Returns an array of mail configuration parameters : - * - * - mail_options +/** + * Returns an array of mail configuration parameters. * - send_bcc_mail_webmaster - * - default_email_format - * - alternative_email_format + * - mail_allow_html * - use_smtp * - smtp_host * - smtp_user * - smtp_password - * - boundary_key + * - smtp_secure * - email_webmaster - * - formated_email_webmaster + * - name_webmaster * * @return array */ @@ -82,46 +70,39 @@ function get_mail_configuration() global $conf; $conf_mail = array( - 'mail_options' => $conf['mail_options'], 'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'], - 'default_email_format' => $conf['default_email_format'], - 'alternative_email_format' => $conf['alternative_email_format'], + 'mail_allow_html' => $conf['mail_allow_html'], + 'mail_theme' => $conf['mail_theme'], 'use_smtp' => !empty($conf['smtp_host']), 'smtp_host' => $conf['smtp_host'], 'smtp_user' => $conf['smtp_user'], 'smtp_password' => $conf['smtp_password'], - 'boundary_key' => generate_key(32), + 'smtp_secure' => $conf['smtp_secure'], + 'email_webmaster' => get_mail_sender_email(), + 'name_webmaster' => get_mail_sender_name(), ); - // we have webmaster id among user list, what's his email address ? - $conf_mail['email_webmaster'] = get_webmaster_mail_address(); - - // name of the webmaster is the title of the gallery - $conf_mail['formated_email_webmaster'] = - format_email(get_mail_sender_name(), $conf_mail['email_webmaster']); - return $conf_mail; } /** - * Returns an email address with an associated real name + * Returns an email address with an associated real name. + * Can return either: + * - email@domain.com + * - name <email@domain.com> * - * @param string name - * @param string email + * @param string $name + * @param string $email + * @return string */ function format_email($name, $email) { - // Spring cleaning $cvt_email = trim(preg_replace('#[\n\r]+#s', '', $email)); $cvt_name = trim(preg_replace('#[\n\r]+#s', '', $name)); if ($cvt_name!="") { - $cvt_name = encode_mime_header( - '"' - .addcslashes($cvt_name,'"') - .'"'); - $cvt_name .= ' '; + $cvt_name = '"'.addcslashes($cvt_name,'"').'"'.' '; } if (strpos($cvt_email, '<') === false) @@ -135,14 +116,118 @@ function format_email($name, $email) } /** - * Returns an email address list with minimal email string + * Returns the email and the name from a formatted address. + * @since 2.6 + * + * @param string|string[] $input - if is an array must contain email[, name] + * @return array email, name + */ +function unformat_email($input) +{ + if (is_array($input)) + { + if (!isset($input['name'])) + { + $input['name'] = ''; + } + return $input; + } + + if (preg_match('/(.*)<(.*)>.*/', $input, $matches)) + { + return array( + 'email' => trim($matches[2]), + 'name' => trim($matches[1]), + ); + } + else + { + return array( + 'email' => trim($input), + 'name' => '', + ); + } +} + +/** + * Return a clean array of hashmaps (email, name) removing duplicates. + * It accepts various inputs: + * - comma separated list + * - array of emails + * - single hashmap (email[, name]) + * - array of incomplete hashmaps + * @since 2.6 + * + * @param mixed $data + * @return string[][] + */ +function get_clean_recipients_list($data) +{ + if (empty($data)) + { + return array(); + } + else if (is_array($data)) + { + $values = array_values($data); + if (!is_array($values[0])) + { + $keys = array_keys($data); + if (is_int($keys[0])) + { // simple array of emails + foreach ($data as &$item) + { + $item = array( + 'email' => trim($item), + 'name' => '', + ); + } + unset($item); + } + else + { // hashmap of one recipient + $data = array(unformat_email($data)); + } + } + else + { // array of hashmaps + $data = array_map('unformat_email', $data); + } + } + else + { + $data = explode(',', $data); + $data = array_map('unformat_email', $data); + } + + $existing = array(); + foreach ($data as $i => $entry) + { + if (isset($existing[ $entry['email'] ])) + { + unset($data[$i]); + } + else + { + $existing[ $entry['email'] ] = true; + } + } + + return array_values($data); +} + +/** + * Returns an email address list with minimal email string. + * @deprecated 2.6 * - * @param string with email list (email separated by comma) + * @param string $email_list - comma separated + * @return string */ function get_strict_email_list($email_list) { $result = array(); $list = explode(',', $email_list); + foreach ($list as $email) { if (strpos($email, '<') !== false) @@ -155,44 +240,38 @@ function get_strict_email_list($email_list) return implode(',', array_unique($result)); } - /** - * Return an new mail template + * Return an new mail template. * - * @param string email_format: mail format, text/html or text/plain - * @param string theme: theme to use [default get_default_theme()] + * @param string $email_format - text/html or text/plain + * @return Template */ -function & get_mail_template($email_format, $theme='') +function &get_mail_template($email_format) { - if (empty($theme)) - { - $theme = get_default_theme(); - } - - $mail_template = new Template(PHPWG_ROOT_PATH.'themes', $theme, 'template/mail/'.$email_format); - - return $mail_template; + $template = new Template(PHPWG_ROOT_PATH.'themes', 'default', 'template/mail/'.$email_format); + return $template; } /** - * Return string email format (text/html or text/plain) + * Return string email format (text/html or text/plain). * - * @param string format + * @param bool $is_html + * @return string */ function get_str_email_format($is_html) { return ($is_html ? 'text/html' : 'text/plain'); } -/* - * Switch language to param language +/** + * Switch language to specified language. * All entries are push on language stack * - * @param string language + * @param string $language */ function switch_lang_to($language) { - global $switch_lang, $user, $lang, $lang_info; + global $switch_lang, $user, $lang, $lang_info, $language_files; // explanation of switch_lang // $switch_lang['language'] contains data of language @@ -224,6 +303,15 @@ function switch_lang_to($language) // No test admin because script is checked admin (user selected no) // Translations are in admin file too load_language('admin.lang', '', array('language'=>$language) ); + + // Reload all plugins files (see load_language declaration) + if (!empty($language_files)) + { + foreach ($language_files as $dirname => $files) + foreach ($files as $filename) + load_language($filename, $dirname, array('language'=>$language) ); + } + trigger_action('loading_lang'); load_language('lang', PHPWG_ROOT_PATH.PWG_LOCAL_DIR, array('language'=>$language, 'no_fallback'=>true, 'local'=>true) @@ -239,10 +327,9 @@ function switch_lang_to($language) } } -/* - * Switch back language pushed with switch_lang_to function - * - * @param: none +/** + * Switch back language pushed with switch_lang_to() function. + * @see switch_lang_to() */ function switch_lang_back() { @@ -264,118 +351,126 @@ function switch_lang_back() } /** - * Returns email of all administrator + * Send a notification email to all administrators. + * current user (if admin) is not notified * - * @return string + * @param string|array $subject + * @param string|array $content + * @param boolean $send_technical_details - send user IP and browser + * @return boolean */ -/* - * send en notification email to all administrators - * if a administrator is doing action, - * he's be removed to email list - * - * @param: - * - keyargs_subject: mail subject on l10n_args format - * - keyargs_content: mail content on l10n_args format - * - send_technical_details: send user IP and browser +function pwg_mail_notification_admins($subject, $content, $send_technical_details=true) +{ + if (empty($subject) or empty($content)) + { + return false; + } + + global $conf, $user; + + if (is_array($subject) or is_array($content)) + { + switch_lang_to(get_default_language()); + + if (is_array($subject)) + { + $subject = l10n_args($subject); + } + if (is_array($content)) + { + $content = l10n_args($content); + } + + switch_lang_back(); + } + + $tpl_vars = array(); + if ($send_technical_details) + { + $tpl_vars['TECHNICAL'] = array( + 'username' => stripslashes($user['username']), + 'ip' => $_SERVER['REMOTE_ADDR'], + 'user_agent' => $_SERVER['HTTP_USER_AGENT'], + ); + } + + return pwg_mail_admins( + array( + 'subject' => '['. $conf['gallery_title'] .'] '. $subject, + 'mail_title' => $conf['gallery_title'], + 'mail_subtitle' => $subject, + 'content' => $content, + 'content_format' => 'text/plain', + ), + array( + 'filename' => 'notification_admin', + 'assign' => $tpl_vars, + ) + ); +} + +/** + * Send a email to all administrators. + * current user (if admin) is excluded + * @see pwg_mail() + * @since 2.6 * - * @return boolean (Ok or not) + * @param array $args - as in pwg_mail() + * @param array $tpl - as in pwg_mail() + * @return boolean */ -function pwg_mail_notification_admins($keyargs_subject, $keyargs_content, $send_technical_details=true) +function pwg_mail_admins($args=array(), $tpl=array()) { - global $conf, $user; - - // Check arguments - if (empty($keyargs_subject) or empty($keyargs_content)) + if (empty($args['content']) and empty($tpl)) { return false; } + global $conf, $user; $return = true; - $admins = array(); - + // get admins (except ourself) $query = ' SELECT - u.'.$conf['user_fields']['username'].' AS username, - u.'.$conf['user_fields']['email'].' AS mail_address + u.'.$conf['user_fields']['username'].' AS name, + u.'.$conf['user_fields']['email'].' AS email FROM '.USERS_TABLE.' AS u - JOIN '.USER_INFOS_TABLE.' AS i ON i.user_id = u.'.$conf['user_fields']['id'].' + JOIN '.USER_INFOS_TABLE.' AS i + ON i.user_id = u.'.$conf['user_fields']['id'].' WHERE i.status in (\'webmaster\', \'admin\') - AND '.$conf['user_fields']['email'].' IS NOT NULL + AND u.'.$conf['user_fields']['email'].' IS NOT NULL AND i.user_id <> '.$user['id'].' ORDER BY username ;'; + $admins = array_from_query($query); - $datas = pwg_query($query); - if (!empty($datas)) + if (empty($admins)) { - while ($admin = pwg_db_fetch_assoc($datas)) - { - if (!empty($admin['mail_address'])) - { - array_push($admins, format_email($admin['username'], $admin['mail_address'])); - } - } + return $return; } - if (count($admins) > 0) - { - switch_lang_to(get_default_language()); + switch_lang_to(get_default_language()); - $content = l10n_args($keyargs_content)."\n"; - if ($send_technical_details) - { - $keyargs_content_admin_info = array( - get_l10n_args('Connected user: %s', stripslashes($user['username'])), - get_l10n_args('IP: %s', $_SERVER['REMOTE_ADDR']), - get_l10n_args('Browser: %s', $_SERVER['HTTP_USER_AGENT']) - ); - - $content.= "\n".l10n_args($keyargs_content_admin_info)."\n"; - } + $return = pwg_mail($admins, $args, $tpl); - $return = pwg_mail( - implode(', ', $admins), - array( - 'subject' => '['.$conf['gallery_title'].'] '.l10n_args($keyargs_subject), - 'content' => $content, - 'content_format' => 'text/plain', - 'email_format' => 'text/plain', - ) - ); - - switch_lang_back(); - } + switch_lang_back(); return $return; } -/* - * send en email to user's group - * - * @param: - * - group_id: mail are sent to group with this Id - * - email_format: mail format - * - keyargs_subject: mail subject on l10n_args format - * - tpl_shortname: short template name without extension - * - assign_vars: array used to assign_vars to mail template - * - language_selected: send mail only to user with this selected language +/** + * Send an email to a group. + * @see pwg_mail() * - * @return boolean (Ok or not) + * @param int $group_id + * @param array $args - as in pwg_mail() + * o language_selected: filters users of the group by language [default value empty] + * @param array $tpl - as in pwg_mail() + * @return boolean */ -function pwg_mail_group( - $group_id, $email_format, $keyargs_subject, - $tpl_shortname, - $assign_vars = array(), $language_selected = '') -{ - // Check arguments - if - ( - empty($group_id) or - empty($email_format) or - empty($keyargs_subject) or - empty($tpl_shortname) - ) +function pwg_mail_group($group_id, $args=array(), $tpl=array()) +{ + if (empty($group_id) or ( empty($args['content']) and empty($tpl) )) { return false; } @@ -383,117 +478,93 @@ function pwg_mail_group( global $conf; $return = true; + // get distinct languages of targeted users $query = ' -SELECT - distinct language, theme -FROM - '.USER_GROUP_TABLE.' as ug - INNER JOIN '.USERS_TABLE.' as u ON '.$conf['user_fields']['id'].' = ug.user_id - INNER JOIN '.USER_INFOS_TABLE.' as ui ON ui.user_id = ug.user_id -WHERE - '.$conf['user_fields']['email'].' IS NOT NULL - AND group_id = '.$group_id; - - if (!empty($language_selected)) +SELECT DISTINCT language + FROM '.USER_GROUP_TABLE.' AS ug + INNER JOIN '.USERS_TABLE.' AS u + ON '.$conf['user_fields']['id'].' = ug.user_id + INNER JOIN '.USER_INFOS_TABLE.' AS ui + ON ui.user_id = ug.user_id + WHERE group_id = '.$group_id.' + AND '.$conf['user_fields']['email'].' <> ""'; + if (!empty($args['language_selected'])) { $query .= ' - AND language = \''.$language_selected.'\''; + AND language = \''.$args['language_selected'].'\''; } $query .= ' ;'; + $languages = array_from_query($query, 'language'); - $result = pwg_query($query); - - if (pwg_db_num_rows($result) > 0) + if (empty($languages)) { - $list = array(); - while ($row = pwg_db_fetch_assoc($result)) - { - $list[] = $row; - } + return $return; + } - foreach ($list as $elem) - { - $query = ' + foreach ($languages as $language) + { + // get subset of users in this group for a specific language + $query = ' SELECT - u.'.$conf['user_fields']['username'].' as username, - u.'.$conf['user_fields']['email'].' as mail_address -FROM - '.USER_GROUP_TABLE.' as ug - INNER JOIN '.USERS_TABLE.' as u ON '.$conf['user_fields']['id'].' = ug.user_id - INNER JOIN '.USER_INFOS_TABLE.' as ui ON ui.user_id = ug.user_id -WHERE - '.$conf['user_fields']['email'].' IS NOT NULL - AND group_id = '.$group_id.' - AND language = \''.$elem['language'].'\' - AND theme = \''.$elem['theme'].'\' + u.'.$conf['user_fields']['username'].' AS name, + u.'.$conf['user_fields']['email'].' AS email + FROM '.USER_GROUP_TABLE.' AS ug + INNER JOIN '.USERS_TABLE.' AS u + ON '.$conf['user_fields']['id'].' = ug.user_id + INNER JOIN '.USER_INFOS_TABLE.' AS ui + ON ui.user_id = ug.user_id + WHERE group_id = '.$group_id.' + AND '.$conf['user_fields']['email'].' <> "" + AND language = \''.$language.'\' ;'; + $users = array_from_query($query); - $result = pwg_query($query); + if (empty($users)) + { + continue; + } - if (pwg_db_num_rows($result) > 0) - { - $Bcc = array(); - while ($row = pwg_db_fetch_assoc($result)) - { - if (!empty($row['mail_address'])) - { - array_push($Bcc, format_email(stripslashes($row['username']), $row['mail_address'])); - } - } + switch_lang_to($language); - if (count($Bcc) > 0) - { - switch_lang_to($elem['language']); - - $mail_template = get_mail_template($email_format, $elem['theme']); - $mail_template->set_filename($tpl_shortname, $tpl_shortname.'.tpl'); - - $mail_template->assign( - trigger_event('mail_group_assign_vars', $assign_vars)); - - $return = pwg_mail - ( - '', - array - ( - 'Bcc' => $Bcc, - 'subject' => l10n_args($keyargs_subject), - 'email_format' => $email_format, - 'content' => $mail_template->parse($tpl_shortname, true), - 'content_format' => $email_format, - 'theme' => $elem['theme'] - ) - ) and $return; + $return&= pwg_mail(null, + array_merge( + $args, + array('Bcc' => $users) + ), + $tpl + ); - switch_lang_back(); - } - } - } + switch_lang_back(); } return $return; } -/* - * sends an email, using Piwigo specific informations +/** + * Sends an email, using Piwigo specific informations. * - * @param: - * - to: receiver(s) of the mail (list separated by comma). - * - args: function params of mail function: + * @param string|array $to + * @param array $args * o from: sender [default value webmaster email] * o Cc: array of carbon copy receivers of the mail. [default value empty] * o Bcc: array of blind carbon copy receivers of the mail. [default value empty] - * o subject [default value 'Piwigo'] - * o content: content of mail [default value ''] - * o content_format: format of mail content [default value 'text/plain'] - * o email_format: global mail format [default value $conf_mail['default_email_format']] - * o theme: template to use [default get_default_theme()] + * o subject [default value 'Piwigo'] + * o content: content of mail [default value ''] + * o content_format: format of mail content [default value 'text/plain'] + * o email_format: global mail format [default value $conf_mail['default_email_format']] + * o theme: theme to use [default value $conf_mail['mail_theme']] + * o mail_title: main title of the mail [default value $conf['gallery_title']] + * o mail_subtitle: subtitle of the mail [default value subject] + * @param array $tpl - use these options to define a custom content template file + * o filename + * o dirname (optional) + * o assign (optional) * - * @return boolean (Ok or not) + * @return boolean */ -function pwg_mail($to, $args = array()) +function pwg_mail($to, $args=array(), $tpl=array()) { global $conf, $conf_mail, $lang_info, $page; @@ -507,224 +578,316 @@ function pwg_mail($to, $args = array()) $conf_mail = get_mail_configuration(); } - if (empty($args['email_format'])) + include_once(PHPWG_ROOT_PATH.'include/phpmailer/class.phpmailer.php'); + + $mail = new PHPMailer; + + foreach (get_clean_recipients_list($to) as $recipient) { - $args['email_format'] = $conf_mail['default_email_format']; + $mail->addAddress($recipient['email'], $recipient['name']); } + $mail->WordWrap = 76; + $mail->CharSet = 'UTF-8'; + // Compute root_path in order have complete path set_make_full_url(); if (empty($args['from'])) { - $args['from'] = $conf_mail['formated_email_webmaster']; + $from = array( + 'email' => $conf_mail['email_webmaster'], + 'name' => $conf_mail['name_webmaster'], + ); } else { - $args['from'] = format_email('', $args['from']); + $from = unformat_email($args['from']); } + $mail->setFrom($from['email'], $from['name']); + $mail->addReplyTo($from['email'], $from['name']); + // Subject if (empty($args['subject'])) { $args['subject'] = 'Piwigo'; } - // Spring cleaning - $cvt_subject = trim(preg_replace('#[\n\r]+#s', '', $args['subject'])); - // Ascii convertion - $cvt_subject = encode_mime_header($cvt_subject); + $args['subject'] = trim(preg_replace('#[\n\r]+#s', '', $args['subject'])); + $mail->Subject = $args['subject']; - if (!isset($args['content'])) + // Cc + if (!empty($args['Cc'])) { - $args['content'] = ''; + foreach (get_clean_recipients_list($args['Cc']) as $recipient) + { + $mail->addCC($recipient['email'], $recipient['name']); + } } - if (empty($args['content_format'])) + // Bcc + $Bcc = get_clean_recipients_list(@$args['Bcc']); + if ($conf_mail['send_bcc_mail_webmaster']) { - $args['content_format'] = 'text/plain'; + $Bcc[] = array( + 'email' => get_webmaster_mail_address(), + 'name' => '', + ); } - - if ($conf_mail['send_bcc_mail_webmaster']) + if (!empty($Bcc)) { - $args['Bcc'][] = $conf_mail['formated_email_webmaster']; + foreach ($Bcc as $recipient) + { + $mail->addBCC($recipient['email'], $recipient['name']); + } } - if (empty($args['theme'])) + // theme + if (empty($args['theme']) or !in_array($args['theme'], array('clear','dark'))) { - $args['theme'] = get_default_theme(); + $args['theme'] = $conf_mail['mail_theme']; } - $headers = 'From: '.$args['from']."\n"; - $headers.= 'Reply-To: '.$args['from']."\n"; - - if (!empty($args['Cc'])) + // content + if (!isset($args['content'])) { - $headers.= 'Cc: '.implode(',', $args['Cc'])."\n"; + $args['content'] = ''; } - - if (!empty($args['Bcc'])) + + // try to decompose subject like "[....] ...." + if (!isset($args['mail_title']) and !isset($args['mail_subtitle'])) { - $headers.= 'Bcc: '.implode(',', $args['Bcc'])."\n"; + if (preg_match('#^\[(.*)\](.*)$#', $args['subject'], $matches)) + { + $args['mail_title'] = $matches[1]; + $args['mail_subtitle'] = $matches[2]; + } + } + if (!isset($args['mail_title'])) + { + $args['mail_title'] = $conf['gallery_title']; + } + if (!isset($args['mail_subtitle'])) + { + $args['mail_subtitle'] = $args['subject']; } - $headers.= 'Content-Type: multipart/alternative;'."\n"; - $headers.= ' boundary="---='.$conf_mail['boundary_key'].'";'."\n"; - $headers.= ' reply-type=original'."\n"; - $headers.= 'MIME-Version: 1.0'."\n"; - $headers.= 'X-Mailer: Piwigo Mailer'."\n"; - - // List on content-type - $content_type_list[] = $args['email_format']; - if (!empty($conf_mail['alternative_email_format'])) + // content type + if (empty($args['content_format'])) { - $content_type_list[] = $conf_mail['alternative_email_format']; + $args['content_format'] = 'text/plain'; } - $content = ''; + $content_type_list = array(); + if ($conf_mail['mail_allow_html'] and @$args['email_format'] != 'text/plain') + { + $content_type_list[] = 'text/html'; + } + $content_type_list[] = 'text/plain'; - foreach (array_unique($content_type_list) as $content_type) + $contents = array(); + foreach ($content_type_list as $content_type) { - // key compose of indexes witch allow ti cache mail data - $cache_key = $content_type.'-'.$lang_info['code'].'-'.$args['theme']; + // key compose of indexes witch allow to cache mail data + $cache_key = $content_type.'-'.$lang_info['code']; if (!isset($conf_mail[$cache_key])) { + // instanciate a new Template if (!isset($conf_mail[$cache_key]['theme'])) { - $conf_mail[$cache_key]['theme'] = get_mail_template($content_type, $args['theme']); + $conf_mail[$cache_key]['theme'] = get_mail_template($content_type); + trigger_action('before_parse_mail_template', $cache_key, $content_type); } + $template = &$conf_mail[$cache_key]['theme']; - $conf_mail[$cache_key]['theme']->set_filename('mail_header', 'header.tpl'); - $conf_mail[$cache_key]['theme']->set_filename('mail_footer', 'footer.tpl'); + $template->set_filename('mail_header', 'header.tpl'); + $template->set_filename('mail_footer', 'footer.tpl'); - $conf_mail[$cache_key]['theme']->assign( + $template->assign( array( - //Header - 'BOUNDARY_KEY' => $conf_mail['boundary_key'], - 'CONTENT_TYPE' => $content_type, - 'CONTENT_ENCODING' => get_pwg_charset(), - - // Footer 'GALLERY_URL' => get_gallery_home_url(), - 'GALLERY_TITLE' => - isset($page['gallery_title']) ? - $page['gallery_title'] : $conf['gallery_title'], + 'GALLERY_TITLE' => isset($page['gallery_title']) ? $page['gallery_title'] : $conf['gallery_title'], 'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '', - 'PHPWG_URL' => PHPWG_URL, - - 'TITLE_MAIL' => urlencode(l10n('A comment on your site')), - 'MAIL' => get_webmaster_mail_address() - )); + 'PHPWG_URL' => defined('PHPWG_URL') ? PHPWG_URL : '', + 'CONTENT_ENCODING' => get_pwg_charset(), + 'CONTACT_MAIL' => $conf_mail['email_webmaster'], + ) + ); if ($content_type == 'text/html') { - if ($conf_mail[$cache_key]['theme']->smarty->template_exists('global-mail-css.tpl')) + if ($template->smarty->templateExists('global-mail-css.tpl')) { - $conf_mail[$cache_key]['theme']->set_filename('css', 'global-mail-css.tpl'); - $conf_mail[$cache_key]['theme']->assign_var_from_handle('GLOBAL_MAIL_CSS', 'css'); + $template->set_filename('global-css', 'global-mail-css.tpl'); + $template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'global-css'); } - $file = PHPWG_ROOT_PATH.'themes/'.$args['theme'].'/mail-css.tpl'; - if (is_file($file)) + if ($template->smarty->templateExists('mail-css-'. $args['theme'] .'.tpl')) { - $conf_mail[$cache_key]['theme']->set_filename('css', realpath($file)); - $conf_mail[$cache_key]['theme']->assign_var_from_handle('MAIL_CSS', 'css'); + $template->set_filename('css', 'mail-css-'. $args['theme'] .'.tpl'); + $template->assign_var_from_handle('MAIL_CSS', 'css'); } } - - // what are displayed on the header of each mail ? - $conf_mail[$cache_key]['header'] = - $conf_mail[$cache_key]['theme']->parse('mail_header', true); - - // what are displayed on the footer of each mail ? - $conf_mail[$cache_key]['footer'] = - $conf_mail[$cache_key]['theme']->parse('mail_footer', true); } + + $template = &$conf_mail[$cache_key]['theme']; + $template->assign( + array( + 'MAIL_TITLE' => $args['mail_title'], + 'MAIL_SUBTITLE' => $args['mail_subtitle'], + ) + ); // Header - $content.= $conf_mail[$cache_key]['header']; + $contents[$content_type] = $template->parse('mail_header', true); // Content - if (($args['content_format'] == 'text/plain') and ($content_type == 'text/html')) + // Stored in a temp variable, if a content template is used it will be assigned + // to the $CONTENT template variable, otherwise it will be appened to the mail + if ($args['content_format'] == 'text/plain' and $content_type == 'text/html') { - $content.= '<p>'. - nl2br( - preg_replace("/(http:\/\/)([^\s,]*)/i", - "<a href='$1$2' class='thumblnk'>$1$2</a>", - htmlspecialchars($args['content']))). - '</p>'; + // convert plain text to html + $mail_content = + '<p>'. + nl2br( + preg_replace( + '/(https?:\/\/([-\w\.]+[-\w])+(:\d+)?(\/([\w\/_\.\#-]*(\?\S+)?[^\.\s])?)?)/i', + '<a href="$1">$1</a>', + htmlspecialchars($args['content']) + ) + ). + '</p>'; } - else if (($args['content_format'] == 'text/html') and ($content_type == 'text/plain')) + else if ($args['content_format'] == 'text/html' and $content_type == 'text/plain') { // convert html text to plain text - $content.= strip_tags($args['content']); + $mail_content = strip_tags($args['content']); + } + else + { + $mail_content = $args['content']; + } + + // Runtime template + if (isset($tpl['filename'])) + { + if (isset($tpl['dirname'])) + { + $template->set_template_dir($tpl['dirname'] .'/'. $content_type); + } + if ($template->smarty->templateExists($tpl['filename'] .'.tpl')) + { + $template->set_filename($tpl['filename'], $tpl['filename'] .'.tpl'); + if (!empty($tpl['assign'])) + { + $template->assign($tpl['assign']); + } + $template->assign('CONTENT', $mail_content); + $contents[$content_type].= $template->parse($tpl['filename'], true); + } + else + { + $contents[$content_type].= $mail_content; + } } else { - $content.= $args['content']; + $contents[$content_type].= $mail_content; } // Footer - $content.= $conf_mail[$cache_key]['footer']; + $contents[$content_type].= $template->parse('mail_footer', true); + } + + // Undo Compute root_path in order have complete path + unset_make_full_url(); + + // Send content to PHPMailer + if (isset($contents['text/html'])) + { + $mail->isHTML(true); + $mail->Body = move_css_to_body($contents['text/html']); + + if (isset($contents['text/plain'])) + { + $mail->AltBody = $contents['text/plain']; + } + } + else + { + $mail->isHTML(false); + $mail->Body = $contents['text/plain']; + } + + if ($conf_mail['use_smtp']) + { + // now we need to split port number + if (strpos($conf_mail['smtp_host'], ':') !== false) + { + list($smtp_host, $smtp_port) = explode(':', $conf_mail['smtp_host']); + } + else + { + $smtp_host = $conf_mail['smtp_host']; + $smtp_port = 25; + } - // Close boundary - $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n"; + $mail->IsSMTP(); + + // enables SMTP debug information (for testing) 2 - debug, 0 - no message + $mail->SMTPDebug = 0; + + $mail->Host = $smtp_host; + $mail->Port = $smtp_port; + + if (!empty($conf_mail['smtp_secure']) and in_array($conf_mail['smtp_secure'], array('ssl', 'tls'))) + { + $mail->SMTPSecure = $conf_mail['smtp_secure']; + } + + if (!empty($conf_mail['smtp_user'])) + { + $mail->SMTPAuth = true; + $mail->Username = $conf_mail['smtp_user']; + $mail->Password = $conf_mail['smtp_password']; + } } - //~ // Close boundary - //~ $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n"; + $ret = true; + $pre_result = trigger_event('before_send_mail', true, $to, $args, $mail); - // Undo Compute root_path in order have complete path - unset_make_full_url(); + if ($pre_result) + { + $ret = $mail->send(); + if (!$ret and (!ini_get('display_errors') or is_admin())) + { + trigger_error('Mailer Error: ' . $mail->ErrorInfo, E_USER_WARNING); + } + if ($conf['debug_mail']) + { + pwg_send_mail_test($ret, $mail, $args); + } + } - return - trigger_event('send_mail', - false, /* Result */ - trigger_event('send_mail_to', get_strict_email_list($to)), - trigger_event('send_mail_subject', $cvt_subject), - trigger_event('send_mail_content', $content), - trigger_event('send_mail_headers', $headers), - $args - ); + return $ret; } -/* - * pwg sendmail - * - * @param: - * - result of other sendmail - * - to: Receiver or receiver(s) of the mail. - * - subject [default value 'Piwigo'] - * - content: content of mail - * - headers: headers of mail - * - * @return boolean (Ok or not) +/** + * @deprecated 2.6 */ function pwg_send_mail($result, $to, $subject, $content, $headers) { + if (is_admin()) + { + trigger_error('pwg_send_mail function is deprecated', E_USER_NOTICE); + } + if (!$result) { - global $conf_mail; - - if ($conf_mail['use_smtp']) - { - include_once( PHPWG_ROOT_PATH.'include/class_smtp_mail.inc.php' ); - $smtp_mail = new smtp_mail( - $conf_mail['smtp_host'], $conf_mail['smtp_user'], $conf_mail['smtp_password'], - $conf_mail['email_webmaster']); - return $smtp_mail->mail($to, $subject, $content, $headers); - } - else - { - if ($conf_mail['mail_options']) - { - $options = '-f '.$conf_mail['email_webmaster']; - return mail($to, $subject, $content, $headers, $options); - } - else - { - return mail($to, $subject, $content, $headers); - } - } + return pwg_mail($to, array( + 'content' => $content, + 'subject' => $subject, + )); } else { @@ -732,94 +895,65 @@ function pwg_send_mail($result, $to, $subject, $content, $headers) } } -function move_ccs_rules_to_body($content) +/** + * Moves CSS rules contained in the <style> tag to inline CSS. + * Used for compatibility with Gmail and such clients + * @since 2.6 + * + * @param string $content + * @return string + */ +function move_css_to_body($content) { - // We search all css rules in style tags - preg_match('#<style>(.*?)</style>#s', $content, $matches); - - if (!empty($matches[1])) - { - preg_match_all('#([^\n]*?)\{(.*?)\}#s', $matches[1], $matches); + include_once(PHPWG_ROOT_PATH.'include/emogrifier.class.php'); - $selectors = array(); - $unknow_selectors = ''; + // disable DOM warnings + $e_state = libxml_use_internal_errors(true); - foreach ($matches[1] as $key => $value) - { - $selects = explode(',', $value); - $style = trim($matches[2][$key], ' ;'); + $e = new Emogrifier($content); + // $e->preserveStyleTag = true; + $content = $e->emogrify(); - foreach($selects as $select) - { - $select = trim($select); - $selectors[$select][] = $style; - } - } + libxml_clear_errors(); + libxml_use_internal_errors($e_state); - foreach ($selectors as $selector => $style) - { - if (!preg_match('/^(#|\.|)([A-Za-z0-9_-]*)$/', $selector, $matches)) - { - $unknow_selectors .= $selector.' {'.implode(";\n", $style).";}\n"; - } - else switch ($matches[1]) - { - case '#': - $content = preg_replace('|id="'.$matches[2].'"|', 'id="'.$matches[2].'" style="'.implode(";\n", $style).";\"\n", $content); - break; - case '.': - $content = preg_replace('|class="'.$matches[2].'"|', 'class="'.$matches[2].'" style="'.implode(";\n", $style).";\"\n", $content); - break; - default: - $content = preg_replace('#<'.$matches[2].'( |>)#', '<'.$matches[2].' style="'.implode(";\n", $style).";\"\n$1", $content); - break; - } - } + return $content; +} - // Keep unknow tags in page head - if (!empty($unknow_selectors)) +/** + * Saves a copy of the mail if _data/tmp. + * + * @param boolean $success + * @param PHPMailer $mail + * @param array $args + */ +function pwg_send_mail_test($success, $mail, $args) +{ + global $conf, $user, $lang_info; + + $dir = PHPWG_ROOT_PATH.$conf['data_location'].'tmp'; + if (mkgetdir($dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR)) + { + $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'-'.date('YmdHis').($success ? '' : '.ERROR'); + if ($args['content_format'] == 'text/plain') { - $content = preg_replace('#<style>.*?</style>#s', "<style type=\"text/css\">\n$unknow_selectors</style>", $content); + $filename .= '.txt'; } else { - $content = preg_replace('#<style>.*?</style>#s', '', $content); + $filename .= '.html'; } - } - return $content; -} - -/*Testing block*/ -function pwg_send_mail_test($result, $to, $subject, $content, $headers, $args) -{ - global $conf, $user, $lang_info; - $dir = PHPWG_ROOT_PATH.$conf['data_location'].'tmp'; - if ( mkgetdir( $dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR) ) + + $file = fopen($filename, 'w+'); + if (!$success) { - $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'.'.$args['theme'].'-'.date('YmdHis'); - if ($args['content_format'] == 'text/plain') - { - $filename .= '.txt'; - } - else - { - $filename .= '.html'; - } - $file = fopen($filename, 'w+'); - fwrite($file, $to ."\n"); - fwrite($file, $subject ."\n"); - fwrite($file, $headers); - fwrite($file, $content); - fclose($file); + fwrite($file, "ERROR: " . $mail->ErrorInfo . "\n\n"); } - return $result; + fwrite($file, $mail->getSentMIMEMessage()); + fclose($file); + } } -if ($conf['debug_mail']) - add_event_handler('send_mail', 'pwg_send_mail_test', EVENT_HANDLER_PRIORITY_NEUTRAL+10, 6); - -add_event_handler('send_mail', 'pwg_send_mail', EVENT_HANDLER_PRIORITY_NEUTRAL, 5); -add_event_handler('send_mail_content', 'move_ccs_rules_to_body'); trigger_action('functions_mail_included'); -?> +?>
\ No newline at end of file |