[1021] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[2297] | 3 | // | Piwigo - a PHP based picture gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[5196] | 5 | // | Copyright(C) 2008-2010 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 | */ |
---|
| 33 | function 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 | */ |
---|
| 56 | function 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] | 74 | function 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 | */ |
---|
| 107 | function 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 | */ |
---|
| 137 | function 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] | 163 | function & 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 | */ |
---|
| 180 | function 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 | */ |
---|
| 191 | function 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'); |
---|
[5208] | 226 | load_language('lang', PHPWG_ROOT_PATH.'local/', |
---|
| 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 | */ |
---|
| 245 | function 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 | */ |
---|
| 280 | function pwg_mail_notification_admins($keyargs_subject, $keyargs_content) |
---|
| 281 | { |
---|
[2140] | 282 | // Check arguments |
---|
[2278] | 283 | if |
---|
[2140] | 284 | ( |
---|
| 285 | empty($keyargs_subject) or |
---|
| 286 | empty($keyargs_content) |
---|
| 287 | ) |
---|
| 288 | { |
---|
| 289 | return false; |
---|
| 290 | } |
---|
| 291 | |
---|
[1908] | 292 | global $conf, $user; |
---|
| 293 | $return = true; |
---|
| 294 | |
---|
| 295 | $admins = array(); |
---|
| 296 | |
---|
| 297 | $query = ' |
---|
| 298 | select |
---|
| 299 | U.'.$conf['user_fields']['username'].' as username, |
---|
| 300 | U.'.$conf['user_fields']['email'].' as mail_address |
---|
| 301 | from |
---|
| 302 | '.USERS_TABLE.' as U, |
---|
| 303 | '.USER_INFOS_TABLE.' as I |
---|
| 304 | where |
---|
| 305 | I.user_id = U.'.$conf['user_fields']['id'].' and |
---|
| 306 | I.status in (\'webmaster\', \'admin\') and |
---|
| 307 | I.adviser = \'false\' and |
---|
[2106] | 308 | '.$conf['user_fields']['email'].' is not null and |
---|
[1908] | 309 | I.user_id <> '.$user['id'].' |
---|
| 310 | order by |
---|
| 311 | username |
---|
| 312 | '; |
---|
| 313 | |
---|
| 314 | $datas = pwg_query($query); |
---|
| 315 | if (!empty($datas)) |
---|
| 316 | { |
---|
[4325] | 317 | while ($admin = pwg_db_fetch_assoc($datas)) |
---|
[1908] | 318 | { |
---|
| 319 | if (!empty($admin['mail_address'])) |
---|
| 320 | { |
---|
| 321 | array_push($admins, format_email($admin['username'], $admin['mail_address'])); |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | |
---|
[1914] | 326 | if (count($admins) > 0) |
---|
[2106] | 327 | { |
---|
| 328 | $keyargs_content_admin_info = array |
---|
| 329 | ( |
---|
[4304] | 330 | get_l10n_args('Connected user: %s', stripslashes($user['username'])), |
---|
[2106] | 331 | get_l10n_args('IP: %s', $_SERVER['REMOTE_ADDR']), |
---|
| 332 | get_l10n_args('Browser: %s', $_SERVER['HTTP_USER_AGENT']) |
---|
| 333 | ); |
---|
[1908] | 334 | |
---|
[1926] | 335 | switch_lang_to(get_default_language()); |
---|
[1908] | 336 | |
---|
[1914] | 337 | $return = pwg_mail |
---|
| 338 | ( |
---|
| 339 | '', |
---|
| 340 | array |
---|
[2106] | 341 | ( |
---|
| 342 | 'Bcc' => $admins, |
---|
[1914] | 343 | 'subject' => '['.$conf['gallery_title'].'] '.l10n_args($keyargs_subject), |
---|
[2121] | 344 | 'content' => |
---|
[2106] | 345 | l10n_args($keyargs_content)."\n\n" |
---|
[1914] | 346 | .l10n_args($keyargs_content_admin_info)."\n", |
---|
| 347 | 'content_format' => 'text/plain' |
---|
| 348 | ) |
---|
| 349 | ) and $return; |
---|
| 350 | |
---|
| 351 | switch_lang_back(); |
---|
| 352 | } |
---|
| 353 | |
---|
[1908] | 354 | return $return; |
---|
| 355 | } |
---|
[2106] | 356 | |
---|
[1904] | 357 | /* |
---|
| 358 | * send en email to user's group |
---|
| 359 | * |
---|
[2106] | 360 | * @param: |
---|
| 361 | * - group_id: mail are sent to group with this Id |
---|
| 362 | * - email_format: mail format |
---|
| 363 | * - keyargs_subject: mail subject on l10n_args format |
---|
[1915] | 364 | * - dirname: short name of directory including template |
---|
| 365 | * - tpl_shortname: short template name without extension |
---|
[2106] | 366 | * - assign_vars: array used to assign_vars to mail template |
---|
| 367 | * - language_selected: send mail only to user with this selected language |
---|
[1908] | 368 | * |
---|
| 369 | * @return boolean (Ok or not) |
---|
| 370 | */ |
---|
[2106] | 371 | function pwg_mail_group( |
---|
[2121] | 372 | $group_id, $email_format, $keyargs_subject, |
---|
[2629] | 373 | $tpl_shortname, |
---|
[1915] | 374 | $assign_vars = array(), $language_selected = '') |
---|
[1904] | 375 | { |
---|
[2140] | 376 | // Check arguments |
---|
[2278] | 377 | if |
---|
[2140] | 378 | ( |
---|
| 379 | empty($group_id) or |
---|
| 380 | empty($email_format) or |
---|
| 381 | empty($keyargs_subject) or |
---|
| 382 | empty($tpl_shortname) |
---|
| 383 | ) |
---|
| 384 | { |
---|
| 385 | return false; |
---|
| 386 | } |
---|
| 387 | |
---|
[1904] | 388 | global $conf; |
---|
[1908] | 389 | $return = true; |
---|
[1904] | 390 | |
---|
| 391 | $query = ' |
---|
| 392 | SELECT |
---|
[5123] | 393 | distinct language, theme |
---|
[2121] | 394 | FROM |
---|
| 395 | '.USER_GROUP_TABLE.' as ug |
---|
[1904] | 396 | INNER JOIN '.USERS_TABLE.' as u ON '.$conf['user_fields']['id'].' = ug.user_id |
---|
| 397 | INNER JOIN '.USER_INFOS_TABLE.' as ui ON ui.user_id = ug.user_id |
---|
[2121] | 398 | WHERE |
---|
[1904] | 399 | '.$conf['user_fields']['email'].' IS NOT NULL |
---|
| 400 | AND group_id = '.$group_id; |
---|
| 401 | |
---|
| 402 | if (!empty($language_selected)) |
---|
| 403 | { |
---|
| 404 | $query .= ' |
---|
| 405 | AND language = \''.$language_selected.'\''; |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | $query .= ' |
---|
| 409 | ;'; |
---|
| 410 | |
---|
[2106] | 411 | $result = pwg_query($query); |
---|
[1904] | 412 | |
---|
[4325] | 413 | if (pwg_db_num_rows($result) > 0) |
---|
[2106] | 414 | { |
---|
| 415 | $list = array(); |
---|
[4325] | 416 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[2106] | 417 | { |
---|
| 418 | $list[] = $row; |
---|
| 419 | } |
---|
| 420 | |
---|
[2129] | 421 | foreach ($list as $elem) |
---|
| 422 | { |
---|
| 423 | $query = ' |
---|
[1904] | 424 | SELECT |
---|
| 425 | u.'.$conf['user_fields']['username'].' as username, |
---|
| 426 | u.'.$conf['user_fields']['email'].' as mail_address |
---|
[2121] | 427 | FROM |
---|
| 428 | '.USER_GROUP_TABLE.' as ug |
---|
[1904] | 429 | INNER JOIN '.USERS_TABLE.' as u ON '.$conf['user_fields']['id'].' = ug.user_id |
---|
| 430 | INNER JOIN '.USER_INFOS_TABLE.' as ui ON ui.user_id = ug.user_id |
---|
[2121] | 431 | WHERE |
---|
[1904] | 432 | '.$conf['user_fields']['email'].' IS NOT NULL |
---|
| 433 | AND group_id = '.$group_id.' |
---|
[2106] | 434 | AND language = \''.$elem['language'].'\' |
---|
[5123] | 435 | AND theme = \''.$elem['theme'].'\' |
---|
[1904] | 436 | ;'; |
---|
| 437 | |
---|
[2129] | 438 | $result = pwg_query($query); |
---|
[1904] | 439 | |
---|
[4325] | 440 | if (pwg_db_num_rows($result) > 0) |
---|
[1904] | 441 | { |
---|
[2129] | 442 | $Bcc = array(); |
---|
[4325] | 443 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1904] | 444 | { |
---|
[2129] | 445 | if (!empty($row['mail_address'])) |
---|
| 446 | { |
---|
[4304] | 447 | array_push($Bcc, format_email(stripslashes($row['username']), $row['mail_address'])); |
---|
[2129] | 448 | } |
---|
[1904] | 449 | } |
---|
| 450 | |
---|
[2129] | 451 | if (count($Bcc) > 0) |
---|
| 452 | { |
---|
| 453 | switch_lang_to($elem['language']); |
---|
[1904] | 454 | |
---|
[5123] | 455 | $mail_template = get_mail_template($email_format, $elem['theme']); |
---|
[2629] | 456 | $mail_template->set_filename($tpl_shortname, $tpl_shortname.'.tpl'); |
---|
[1904] | 457 | |
---|
[2278] | 458 | $mail_template->assign( |
---|
[2140] | 459 | trigger_event('mail_group_assign_vars', $assign_vars)); |
---|
| 460 | |
---|
[2129] | 461 | $return = pwg_mail |
---|
[2106] | 462 | ( |
---|
[2129] | 463 | '', |
---|
| 464 | array |
---|
| 465 | ( |
---|
| 466 | 'Bcc' => $Bcc, |
---|
| 467 | 'subject' => l10n_args($keyargs_subject), |
---|
| 468 | 'email_format' => $email_format, |
---|
| 469 | 'content' => $mail_template->parse($tpl_shortname, true), |
---|
| 470 | 'content_format' => $email_format, |
---|
| 471 | 'theme' => $elem['theme'] |
---|
| 472 | ) |
---|
| 473 | ) and $return; |
---|
[1904] | 474 | |
---|
[2129] | 475 | switch_lang_back(); |
---|
| 476 | } |
---|
[1914] | 477 | } |
---|
[1904] | 478 | } |
---|
| 479 | } |
---|
[1908] | 480 | |
---|
| 481 | return $return; |
---|
[1904] | 482 | } |
---|
| 483 | |
---|
| 484 | /* |
---|
[2339] | 485 | * sends an email, using Piwigo specific informations |
---|
[1809] | 486 | * |
---|
| 487 | * @param: |
---|
[2284] | 488 | * - to: receiver(s) of the mail (list separated by comma). |
---|
[1809] | 489 | * - args: function params of mail function: |
---|
[2106] | 490 | * o from: sender [default value webmaster email] |
---|
| 491 | * o Cc: array of carbon copy receivers of the mail. [default value empty] |
---|
| 492 | * o Bcc: array of blind carbon copy receivers of the mail. [default value empty] |
---|
[2339] | 493 | * o subject [default value 'Piwigo'] |
---|
[1809] | 494 | * o content: content of mail [default value ''] |
---|
| 495 | * o content_format: format of mail content [default value 'text/plain'] |
---|
[2106] | 496 | * o email_format: global mail format [default value $conf_mail['default_email_format']] |
---|
[5123] | 497 | * o theme: template to use [default get_default_theme()] |
---|
[1908] | 498 | * |
---|
| 499 | * @return boolean (Ok or not) |
---|
[2106] | 500 | */ |
---|
[1809] | 501 | function pwg_mail($to, $args = array()) |
---|
[1019] | 502 | { |
---|
[1809] | 503 | global $conf, $conf_mail, $lang_info, $page; |
---|
[2106] | 504 | |
---|
| 505 | if (empty($to) and empty($args['Cc']) and empty($args['Bcc'])) |
---|
| 506 | { |
---|
| 507 | return true; |
---|
| 508 | } |
---|
[2121] | 509 | |
---|
[1021] | 510 | if (!isset($conf_mail)) |
---|
| 511 | { |
---|
| 512 | $conf_mail = get_mail_configuration(); |
---|
| 513 | } |
---|
[2106] | 514 | |
---|
| 515 | if (empty($args['email_format'])) |
---|
| 516 | { |
---|
| 517 | $args['email_format'] = $conf_mail['default_email_format']; |
---|
| 518 | } |
---|
| 519 | |
---|
[1676] | 520 | // Compute root_path in order have complete path |
---|
[3938] | 521 | set_make_full_url(); |
---|
[2106] | 522 | |
---|
[1809] | 523 | if (empty($args['from'])) |
---|
[1021] | 524 | { |
---|
[1809] | 525 | $args['from'] = $conf_mail['formated_email_webmaster']; |
---|
[1021] | 526 | } |
---|
| 527 | else |
---|
| 528 | { |
---|
[1809] | 529 | $args['from'] = format_email('', $args['from']); |
---|
[1021] | 530 | } |
---|
| 531 | |
---|
[1809] | 532 | if (empty($args['subject'])) |
---|
| 533 | { |
---|
[2339] | 534 | $args['subject'] = 'Piwigo'; |
---|
[1809] | 535 | } |
---|
[2106] | 536 | // Spring cleaning |
---|
| 537 | $cvt_subject = trim(preg_replace('#[\n\r]+#s', '', $args['subject'])); |
---|
| 538 | // Ascii convertion |
---|
[2121] | 539 | $cvt_subject = encode_mime_header($cvt_subject); |
---|
[1809] | 540 | |
---|
| 541 | if (!isset($args['content'])) |
---|
| 542 | { |
---|
| 543 | $args['content'] = ''; |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | if (empty($args['content_format'])) |
---|
| 547 | { |
---|
| 548 | $args['content_format'] = 'text/plain'; |
---|
| 549 | } |
---|
| 550 | |
---|
[2106] | 551 | if ($conf_mail['send_bcc_mail_webmaster']) |
---|
| 552 | { |
---|
| 553 | $args['Bcc'][] = $conf_mail['formated_email_webmaster']; |
---|
| 554 | } |
---|
| 555 | |
---|
[5123] | 556 | if (empty($args['theme'])) |
---|
| 557 | { |
---|
| 558 | $args['theme'] = get_default_theme(); |
---|
| 559 | } |
---|
[2106] | 560 | |
---|
[1809] | 561 | $headers = 'From: '.$args['from']."\n"; |
---|
[2106] | 562 | $headers.= 'Reply-To: '.$args['from']."\n"; |
---|
[1818] | 563 | |
---|
[2106] | 564 | if (!empty($args['Cc'])) |
---|
| 565 | { |
---|
| 566 | $headers.= 'Cc: '.implode(',', $args['Cc'])."\n"; |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | if (!empty($args['Bcc'])) |
---|
| 570 | { |
---|
| 571 | $headers.= 'Bcc: '.implode(',', $args['Bcc'])."\n"; |
---|
| 572 | } |
---|
| 573 | |
---|
[1809] | 574 | $headers.= 'Content-Type: multipart/alternative;'."\n"; |
---|
| 575 | $headers.= ' boundary="---='.$conf_mail['boundary_key'].'";'."\n"; |
---|
[2106] | 576 | $headers.= ' reply-type=original'."\n"; |
---|
| 577 | $headers.= 'MIME-Version: 1.0'."\n"; |
---|
| 578 | $headers.= 'X-Mailer: Piwigo Mailer'."\n"; |
---|
[1532] | 579 | |
---|
[3938] | 580 | // List on content-type |
---|
| 581 | $content_type_list[] = $args['email_format']; |
---|
| 582 | if (!empty($conf_mail['alternative_email_format'])) |
---|
| 583 | { |
---|
| 584 | $content_type_list[] = $conf_mail['alternative_email_format']; |
---|
| 585 | } |
---|
| 586 | |
---|
[2106] | 587 | $content = ''; |
---|
| 588 | |
---|
[3938] | 589 | foreach (array_unique($content_type_list) as $content_type) |
---|
| 590 | { |
---|
| 591 | // key compose of indexes witch allow ti cache mail data |
---|
[5123] | 592 | $cache_key = $content_type.'-'.$lang_info['code'].'-'.$args['theme']; |
---|
[2129] | 593 | |
---|
[3938] | 594 | if (!isset($conf_mail[$cache_key])) |
---|
[2106] | 595 | { |
---|
[5123] | 596 | if (!isset($conf_mail[$cache_key]['theme'])) |
---|
[3938] | 597 | { |
---|
[5796] | 598 | $conf_mail[$cache_key]['theme'] = get_mail_template($content_type, $args['theme']); |
---|
[3938] | 599 | } |
---|
[2106] | 600 | |
---|
[5123] | 601 | $conf_mail[$cache_key]['theme']->set_filename('mail_header', 'header.tpl'); |
---|
| 602 | $conf_mail[$cache_key]['theme']->set_filename('mail_footer', 'footer.tpl'); |
---|
[2106] | 603 | |
---|
[5123] | 604 | $conf_mail[$cache_key]['theme']->assign( |
---|
[3938] | 605 | array( |
---|
| 606 | //Header |
---|
| 607 | 'BOUNDARY_KEY' => $conf_mail['boundary_key'], |
---|
| 608 | 'CONTENT_TYPE' => $content_type, |
---|
| 609 | 'CONTENT_ENCODING' => get_pwg_charset(), |
---|
[2121] | 610 | |
---|
[3938] | 611 | // Footer |
---|
[6410] | 612 | 'GALLERY_URL' => get_gallery_home_url(), |
---|
[3938] | 613 | 'GALLERY_TITLE' => |
---|
| 614 | isset($page['gallery_title']) ? |
---|
| 615 | $page['gallery_title'] : $conf['gallery_title'], |
---|
| 616 | 'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '', |
---|
| 617 | 'PHPWG_URL' => PHPWG_URL, |
---|
[2106] | 618 | |
---|
[5021] | 619 | 'TITLE_MAIL' => urlencode(l10n('A comment on your site')), |
---|
[3938] | 620 | 'MAIL' => get_webmaster_mail_address() |
---|
| 621 | )); |
---|
[2106] | 622 | |
---|
[3938] | 623 | if ($content_type == 'text/html') |
---|
[2106] | 624 | { |
---|
[5125] | 625 | if ($conf_mail[$cache_key]['theme']->smarty->template_exists('global-mail-css.tpl')) |
---|
[3938] | 626 | { |
---|
[5123] | 627 | $conf_mail[$cache_key]['theme']->set_filename('css', 'global-mail-css.tpl'); |
---|
| 628 | $conf_mail[$cache_key]['theme']->assign_var_from_handle('GLOBAL_MAIL_CSS', 'css'); |
---|
[3938] | 629 | } |
---|
[2106] | 630 | |
---|
[5123] | 631 | $file = PHPWG_ROOT_PATH.'themes/'.$args['theme'].'/mail-css.tpl'; |
---|
[3938] | 632 | if (is_file($file)) |
---|
| 633 | { |
---|
[5123] | 634 | $conf_mail[$cache_key]['theme']->set_filename('css', realpath($file)); |
---|
| 635 | $conf_mail[$cache_key]['theme']->assign_var_from_handle('MAIL_CSS', 'css'); |
---|
[3938] | 636 | } |
---|
[2106] | 637 | } |
---|
| 638 | |
---|
[3938] | 639 | // what are displayed on the header of each mail ? |
---|
| 640 | $conf_mail[$cache_key]['header'] = |
---|
[5123] | 641 | $conf_mail[$cache_key]['theme']->parse('mail_header', true); |
---|
[1784] | 642 | |
---|
[3938] | 643 | // what are displayed on the footer of each mail ? |
---|
| 644 | $conf_mail[$cache_key]['footer'] = |
---|
[5123] | 645 | $conf_mail[$cache_key]['theme']->parse('mail_footer', true); |
---|
[3938] | 646 | } |
---|
[1809] | 647 | |
---|
[3938] | 648 | // Header |
---|
| 649 | $content.= $conf_mail[$cache_key]['header']; |
---|
[1019] | 650 | |
---|
[3938] | 651 | // Content |
---|
| 652 | if (($args['content_format'] == 'text/plain') and ($content_type == 'text/html')) |
---|
| 653 | { |
---|
| 654 | $content.= '<p>'. |
---|
| 655 | nl2br( |
---|
| 656 | preg_replace("/(http:\/\/)([^\s,]*)/i", |
---|
| 657 | "<a href='$1$2' class='thumblnk'>$1$2</a>", |
---|
| 658 | htmlspecialchars($args['content']))). |
---|
| 659 | '</p>'; |
---|
| 660 | } |
---|
| 661 | else if (($args['content_format'] == 'text/html') and ($content_type == 'text/plain')) |
---|
| 662 | { |
---|
| 663 | // convert html text to plain text |
---|
| 664 | $content.= strip_tags($args['content']); |
---|
| 665 | } |
---|
| 666 | else |
---|
| 667 | { |
---|
| 668 | $content.= $args['content']; |
---|
| 669 | } |
---|
[1809] | 670 | |
---|
[3938] | 671 | // Footer |
---|
| 672 | $content.= $conf_mail[$cache_key]['footer']; |
---|
[1809] | 673 | |
---|
| 674 | // Close boundary |
---|
| 675 | $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n"; |
---|
[3938] | 676 | } |
---|
[1809] | 677 | |
---|
[3938] | 678 | //~ // Close boundary |
---|
| 679 | //~ $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n"; |
---|
| 680 | |
---|
[1676] | 681 | // Undo Compute root_path in order have complete path |
---|
[3938] | 682 | unset_make_full_url(); |
---|
[1642] | 683 | |
---|
[2140] | 684 | return |
---|
| 685 | trigger_event('send_mail', |
---|
| 686 | false, /* Result */ |
---|
[2284] | 687 | trigger_event('send_mail_to', get_strict_email_list($to)), |
---|
[2140] | 688 | trigger_event('send_mail_subject', $cvt_subject), |
---|
| 689 | trigger_event('send_mail_content', $content), |
---|
| 690 | trigger_event('send_mail_headers', $headers), |
---|
| 691 | $args |
---|
| 692 | ); |
---|
| 693 | } |
---|
| 694 | |
---|
| 695 | /* |
---|
| 696 | * pwg sendmail |
---|
| 697 | * |
---|
| 698 | * @param: |
---|
| 699 | * - result of other sendmail |
---|
| 700 | * - to: Receiver or receiver(s) of the mail. |
---|
[2339] | 701 | * - subject [default value 'Piwigo'] |
---|
[2140] | 702 | * - content: content of mail |
---|
| 703 | * - headers: headers of mail |
---|
| 704 | * |
---|
| 705 | * @return boolean (Ok or not) |
---|
| 706 | */ |
---|
| 707 | function pwg_send_mail($result, $to, $subject, $content, $headers) |
---|
| 708 | { |
---|
[2280] | 709 | if (!$result) |
---|
| 710 | { |
---|
[2140] | 711 | global $conf_mail; |
---|
| 712 | |
---|
| 713 | if ($conf_mail['use_smtp']) |
---|
| 714 | { |
---|
| 715 | include_once( PHPWG_ROOT_PATH.'include/class_smtp_mail.inc.php' ); |
---|
| 716 | $smtp_mail = new smtp_mail( |
---|
| 717 | $conf_mail['smtp_host'], $conf_mail['smtp_user'], $conf_mail['smtp_password'], |
---|
| 718 | $conf_mail['email_webmaster']); |
---|
| 719 | return $smtp_mail->mail($to, $subject, $content, $headers); |
---|
| 720 | } |
---|
| 721 | else |
---|
| 722 | { |
---|
| 723 | if ($conf_mail['mail_options']) |
---|
| 724 | { |
---|
| 725 | $options = '-f '.$conf_mail['email_webmaster']; |
---|
| 726 | return mail($to, $subject, $content, $headers, $options); |
---|
| 727 | } |
---|
| 728 | else |
---|
| 729 | { |
---|
| 730 | return mail($to, $subject, $content, $headers); |
---|
| 731 | } |
---|
| 732 | } |
---|
[2280] | 733 | } |
---|
| 734 | else |
---|
| 735 | { |
---|
| 736 | return $result; |
---|
| 737 | } |
---|
[2140] | 738 | } |
---|
| 739 | |
---|
[5695] | 740 | function move_ccs_rules_to_body($content) |
---|
| 741 | { |
---|
| 742 | // We search all css rules in style tags |
---|
| 743 | preg_match('#<style>(.*?)</style>#s', $content, $matches); |
---|
| 744 | |
---|
| 745 | if (!empty($matches[1])) |
---|
| 746 | { |
---|
| 747 | preg_match_all('#([^\n]*?)\{(.*?)\}#s', $matches[1], $matches); |
---|
| 748 | |
---|
| 749 | $selectors = array(); |
---|
| 750 | $unknow_selectors = ''; |
---|
| 751 | |
---|
| 752 | foreach ($matches[1] as $key => $value) |
---|
| 753 | { |
---|
| 754 | $selects = explode(',', $value); |
---|
| 755 | $style = trim($matches[2][$key], ' ;'); |
---|
| 756 | |
---|
| 757 | foreach($selects as $select) |
---|
| 758 | { |
---|
| 759 | $select = trim($select); |
---|
| 760 | $selectors[$select][] = $style; |
---|
| 761 | } |
---|
| 762 | } |
---|
[6705] | 763 | |
---|
[5695] | 764 | foreach ($selectors as $selector => $style) |
---|
| 765 | { |
---|
| 766 | if (!preg_match('/^(#|\.|)([A-Za-z0-9_-]*)$/', $selector, $matches)) |
---|
| 767 | { |
---|
[6705] | 768 | $unknow_selectors .= $selector.' {'.implode(";\n", $style).";}\n"; |
---|
[5695] | 769 | } |
---|
| 770 | else switch ($matches[1]) |
---|
| 771 | { |
---|
| 772 | case '#': |
---|
[6705] | 773 | $content = preg_replace('|id="'.$matches[2].'"|', 'id="'.$matches[2].'" style="'.implode(";\n", $style).";\"\n", $content); |
---|
[5695] | 774 | break; |
---|
| 775 | case '.': |
---|
[6705] | 776 | $content = preg_replace('|class="'.$matches[2].'"|', 'class="'.$matches[2].'" style="'.implode(";\n", $style).";\"\n", $content); |
---|
[5695] | 777 | break; |
---|
| 778 | default: |
---|
[6705] | 779 | $content = preg_replace('#<'.$matches[2].'( |>)#', '<'.$matches[2].' style="'.implode(";\n", $style).";\"\n$1", $content); |
---|
[5695] | 780 | break; |
---|
| 781 | } |
---|
| 782 | } |
---|
| 783 | |
---|
| 784 | // Keep unknow tags in page head |
---|
| 785 | if (!empty($unknow_selectors)) |
---|
| 786 | { |
---|
| 787 | $content = preg_replace('#<style>.*?</style>#s', "<style type=\"text/css\">\n$unknow_selectors</style>", $content); |
---|
| 788 | } |
---|
| 789 | else |
---|
| 790 | { |
---|
| 791 | $content = preg_replace('#<style>.*?</style>#s', '', $content); |
---|
| 792 | } |
---|
| 793 | } |
---|
| 794 | return $content; |
---|
| 795 | } |
---|
| 796 | |
---|
[2278] | 797 | /*Testing block*/ |
---|
[6410] | 798 | function pwg_send_mail_test($result, $to, $subject, $content, $headers, $args) |
---|
[2140] | 799 | { |
---|
[2278] | 800 | global $conf, $user, $lang_info; |
---|
| 801 | $dir = $conf['local_data_dir'].'/tmp'; |
---|
[2497] | 802 | if ( mkgetdir( $dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR) ) |
---|
[1784] | 803 | { |
---|
[6410] | 804 | $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'.'.$args['theme'].'-'.date('YmdHis'); |
---|
[2497] | 805 | if ($args['content_format'] == 'text/plain') |
---|
| 806 | { |
---|
| 807 | $filename .= '.txt'; |
---|
| 808 | } |
---|
| 809 | else |
---|
| 810 | { |
---|
| 811 | $filename .= '.html'; |
---|
| 812 | } |
---|
| 813 | $file = fopen($filename, 'w+'); |
---|
| 814 | fwrite($file, $to ."\n"); |
---|
| 815 | fwrite($file, $subject ."\n"); |
---|
| 816 | fwrite($file, $headers); |
---|
| 817 | fwrite($file, $content); |
---|
| 818 | fclose($file); |
---|
[1784] | 819 | } |
---|
[2278] | 820 | return $result; |
---|
[1021] | 821 | } |
---|
[6410] | 822 | if ($conf['debug_mail']) |
---|
| 823 | add_event_handler('send_mail', 'pwg_send_mail_test', EVENT_HANDLER_PRIORITY_NEUTRAL+10, 6); |
---|
[1105] | 824 | |
---|
[2140] | 825 | |
---|
| 826 | add_event_handler('send_mail', 'pwg_send_mail', EVENT_HANDLER_PRIORITY_NEUTRAL, 5); |
---|
[5695] | 827 | add_event_handler('send_mail_content', 'move_ccs_rules_to_body'); |
---|
[2140] | 828 | trigger_action('functions_mail_included'); |
---|
| 829 | |
---|
[1321] | 830 | ?> |
---|