| 1 | <?php |
|---|
| 2 | // +-----------------------------------------------------------------------+ |
|---|
| 3 | // | Piwigo - a PHP based picture gallery | |
|---|
| 4 | // +-----------------------------------------------------------------------+ |
|---|
| 5 | // | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org | |
|---|
| 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 | // +-----------------------------------------------------------------------+ |
|---|
| 23 | |
|---|
| 24 | // +-----------------------------------------------------------------------+ |
|---|
| 25 | // | functions | |
|---|
| 26 | // +-----------------------------------------------------------------------+ |
|---|
| 27 | |
|---|
| 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; |
|---|
| 48 | return '=?'.get_pwg_charset().'?Q?'.$str.'?='; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | /* |
|---|
| 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 | /* |
|---|
| 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'] |
|---|
| 70 | * - text_footer: Piwigo and version |
|---|
| 71 | * |
|---|
| 72 | * @return array |
|---|
| 73 | */ |
|---|
| 74 | function get_mail_configuration() |
|---|
| 75 | { |
|---|
| 76 | global $conf; |
|---|
| 77 | |
|---|
| 78 | $conf_mail = array( |
|---|
| 79 | 'mail_options' => $conf['mail_options'], |
|---|
| 80 | 'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'], |
|---|
| 81 | 'default_email_format' => $conf['default_email_format'], |
|---|
| 82 | 'alternative_email_format' => $conf['alternative_email_format'], |
|---|
| 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'] |
|---|
| 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'] = |
|---|
| 94 | format_email(get_mail_sender_name(), $conf_mail['email_webmaster']); |
|---|
| 95 | |
|---|
| 96 | $conf_mail['boundary_key'] = generate_key(32); |
|---|
| 97 | |
|---|
| 98 | return $conf_mail; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /** |
|---|
| 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 | { |
|---|
| 109 | // Spring cleaning |
|---|
| 110 | $cvt_email = trim(preg_replace('#[\n\r]+#s', '', $email)); |
|---|
| 111 | $cvt_name = trim(preg_replace('#[\n\r]+#s', '', $name)); |
|---|
| 112 | |
|---|
| 113 | if ($cvt_name!="") |
|---|
| 114 | { |
|---|
| 115 | $cvt_name = encode_mime_header( |
|---|
| 116 | '"' |
|---|
| 117 | .addcslashes($cvt_name,'"') |
|---|
| 118 | .'"'); |
|---|
| 119 | $cvt_name .= ' '; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | if (strpos($cvt_email, '<') === false) |
|---|
| 123 | { |
|---|
| 124 | return $cvt_name.'<'.$cvt_email.'>'; |
|---|
| 125 | } |
|---|
| 126 | else |
|---|
| 127 | { |
|---|
| 128 | return $cvt_name.$cvt_email; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | /** |
|---|
| 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 | |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * Return an new mail template |
|---|
| 156 | * |
|---|
| 157 | * @params: |
|---|
| 158 | * - email_format: mail format |
|---|
| 159 | * - args: function params of mail function: |
|---|
| 160 | * o template: template to use [default get_default_theme()] |
|---|
| 161 | * o theme: template to use [default get_default_theme()] |
|---|
| 162 | */ |
|---|
| 163 | function & get_mail_template($email_format, $theme='') |
|---|
| 164 | { |
|---|
| 165 | if (empty($theme)) |
|---|
| 166 | { |
|---|
| 167 | $theme = get_default_theme(); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | $mail_template = new Template(PHPWG_ROOT_PATH.'themes', $theme, 'template/mail/'.$email_format); |
|---|
| 171 | |
|---|
| 172 | return $mail_template; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | /** |
|---|
| 176 | * Return string email format (html or not) |
|---|
| 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 | |
|---|
| 185 | /* |
|---|
| 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 | |
|---|
| 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']])) |
|---|
| 203 | { |
|---|
| 204 | $switch_lang['initialisation'] = true; |
|---|
| 205 | $switch_lang['language'][$user['language']]['lang_info'] = $lang_info; |
|---|
| 206 | $switch_lang['language'][$user['language']]['lang'] = $lang; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | // Change current infos |
|---|
| 210 | $switch_lang['stack'][] = $user['language']; |
|---|
| 211 | $user['language'] = $language; |
|---|
| 212 | |
|---|
| 213 | // Load new data if necessary |
|---|
| 214 | if (!isset($switch_lang['language'][$language])) |
|---|
| 215 | { |
|---|
| 216 | // Re-Init language arrays |
|---|
| 217 | $lang_info = array(); |
|---|
| 218 | $lang = array(); |
|---|
| 219 | |
|---|
| 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'); |
|---|
| 226 | load_language('lang', PHPWG_ROOT_PATH.'local/', |
|---|
| 227 | array('language'=>$language, 'no_fallback'=>true, 'local'=>true) |
|---|
| 228 | ); |
|---|
| 229 | |
|---|
| 230 | $switch_lang['language'][$language]['lang_info'] = $lang_info; |
|---|
| 231 | $switch_lang['language'][$language]['lang'] = $lang; |
|---|
| 232 | } |
|---|
| 233 | else |
|---|
| 234 | { |
|---|
| 235 | $lang_info = $switch_lang['language'][$language]['lang_info']; |
|---|
| 236 | $lang = $switch_lang['language'][$language]['lang']; |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | /* |
|---|
| 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 | { |
|---|
| 251 | // Get last value |
|---|
| 252 | $language = array_pop($switch_lang['stack']); |
|---|
| 253 | |
|---|
| 254 | // Change current infos |
|---|
| 255 | if (isset($switch_lang['language'][$language])) |
|---|
| 256 | { |
|---|
| 257 | $lang_info = $switch_lang['language'][$language]['lang_info']; |
|---|
| 258 | $lang = $switch_lang['language'][$language]['lang']; |
|---|
| 259 | } |
|---|
| 260 | $user['language'] = $language; |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | /** |
|---|
| 265 | * Returns email of all administrator |
|---|
| 266 | * |
|---|
| 267 | * @return string |
|---|
| 268 | */ |
|---|
| 269 | /* |
|---|
| 270 | * send en notification email to all administrators |
|---|
| 271 | * if a administrator is doing action, |
|---|
| 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 | { |
|---|
| 282 | // Check arguments |
|---|
| 283 | if |
|---|
| 284 | ( |
|---|
| 285 | empty($keyargs_subject) or |
|---|
| 286 | empty($keyargs_content) |
|---|
| 287 | ) |
|---|
| 288 | { |
|---|
| 289 | return false; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 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 |
|---|
| 308 | '.$conf['user_fields']['email'].' is not null and |
|---|
| 309 | I.user_id <> '.$user['id'].' |
|---|
| 310 | order by |
|---|
| 311 | username |
|---|
| 312 | '; |
|---|
| 313 | |
|---|
| 314 | $datas = pwg_query($query); |
|---|
| 315 | if (!empty($datas)) |
|---|
| 316 | { |
|---|
| 317 | while ($admin = pwg_db_fetch_assoc($datas)) |
|---|
| 318 | { |
|---|
| 319 | if (!empty($admin['mail_address'])) |
|---|
| 320 | { |
|---|
| 321 | array_push($admins, format_email($admin['username'], $admin['mail_address'])); |
|---|
| 322 | } |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | if (count($admins) > 0) |
|---|
| 327 | { |
|---|
| 328 | $keyargs_content_admin_info = array |
|---|
| 329 | ( |
|---|
| 330 | get_l10n_args('Connected user: %s', stripslashes($user['username'])), |
|---|
| 331 | get_l10n_args('IP: %s', $_SERVER['REMOTE_ADDR']), |
|---|
| 332 | get_l10n_args('Browser: %s', $_SERVER['HTTP_USER_AGENT']) |
|---|
| 333 | ); |
|---|
| 334 | |
|---|
| 335 | switch_lang_to(get_default_language()); |
|---|
| 336 | |
|---|
| 337 | $return = pwg_mail |
|---|
| 338 | ( |
|---|
| 339 | '', |
|---|
| 340 | array |
|---|
| 341 | ( |
|---|
| 342 | 'Bcc' => $admins, |
|---|
| 343 | 'subject' => '['.$conf['gallery_title'].'] '.l10n_args($keyargs_subject), |
|---|
| 344 | 'content' => |
|---|
| 345 | l10n_args($keyargs_content)."\n\n" |
|---|
| 346 | .l10n_args($keyargs_content_admin_info)."\n", |
|---|
| 347 | 'content_format' => 'text/plain' |
|---|
| 348 | ) |
|---|
| 349 | ) and $return; |
|---|
| 350 | |
|---|
| 351 | switch_lang_back(); |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | return $return; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | /* |
|---|
| 358 | * send en email to user's group |
|---|
| 359 | * |
|---|
| 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 |
|---|
| 364 | * - dirname: short name of directory including template |
|---|
| 365 | * - tpl_shortname: short template name without extension |
|---|
| 366 | * - assign_vars: array used to assign_vars to mail template |
|---|
| 367 | * - language_selected: send mail only to user with this selected language |
|---|
| 368 | * |
|---|
| 369 | * @return boolean (Ok or not) |
|---|
| 370 | */ |
|---|
| 371 | function pwg_mail_group( |
|---|
| 372 | $group_id, $email_format, $keyargs_subject, |
|---|
| 373 | $tpl_shortname, |
|---|
| 374 | $assign_vars = array(), $language_selected = '') |
|---|
| 375 | { |
|---|
| 376 | // Check arguments |
|---|
| 377 | if |
|---|
| 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 | |
|---|
| 388 | global $conf; |
|---|
| 389 | $return = true; |
|---|
| 390 | |
|---|
| 391 | $query = ' |
|---|
| 392 | SELECT |
|---|
| 393 | distinct language, theme |
|---|
| 394 | FROM |
|---|
| 395 | '.USER_GROUP_TABLE.' as ug |
|---|
| 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 |
|---|
| 398 | WHERE |
|---|
| 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 | |
|---|
| 411 | $result = pwg_query($query); |
|---|
| 412 | |
|---|
| 413 | if (pwg_db_num_rows($result) > 0) |
|---|
| 414 | { |
|---|
| 415 | $list = array(); |
|---|
| 416 | while ($row = pwg_db_fetch_assoc($result)) |
|---|
| 417 | { |
|---|
| 418 | $list[] = $row; |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | foreach ($list as $elem) |
|---|
| 422 | { |
|---|
| 423 | $query = ' |
|---|
| 424 | SELECT |
|---|
| 425 | u.'.$conf['user_fields']['username'].' as username, |
|---|
| 426 | u.'.$conf['user_fields']['email'].' as mail_address |
|---|
| 427 | FROM |
|---|
| 428 | '.USER_GROUP_TABLE.' as ug |
|---|
| 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 |
|---|
| 431 | WHERE |
|---|
| 432 | '.$conf['user_fields']['email'].' IS NOT NULL |
|---|
| 433 | AND group_id = '.$group_id.' |
|---|
| 434 | AND language = \''.$elem['language'].'\' |
|---|
| 435 | AND theme = \''.$elem['theme'].'\' |
|---|
| 436 | ;'; |
|---|
| 437 | |
|---|
| 438 | $result = pwg_query($query); |
|---|
| 439 | |
|---|
| 440 | if (pwg_db_num_rows($result) > 0) |
|---|
| 441 | { |
|---|
| 442 | $Bcc = array(); |
|---|
| 443 | while ($row = pwg_db_fetch_assoc($result)) |
|---|
| 444 | { |
|---|
| 445 | if (!empty($row['mail_address'])) |
|---|
| 446 | { |
|---|
| 447 | array_push($Bcc, format_email(stripslashes($row['username']), $row['mail_address'])); |
|---|
| 448 | } |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | if (count($Bcc) > 0) |
|---|
| 452 | { |
|---|
| 453 | switch_lang_to($elem['language']); |
|---|
| 454 | |
|---|
| 455 | $mail_template = get_mail_template($email_format, $elem['theme']); |
|---|
| 456 | $mail_template->set_filename($tpl_shortname, $tpl_shortname.'.tpl'); |
|---|
| 457 | |
|---|
| 458 | $mail_template->assign( |
|---|
| 459 | trigger_event('mail_group_assign_vars', $assign_vars)); |
|---|
| 460 | |
|---|
| 461 | $return = pwg_mail |
|---|
| 462 | ( |
|---|
| 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; |
|---|
| 474 | |
|---|
| 475 | switch_lang_back(); |
|---|
| 476 | } |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | return $return; |
|---|
| 482 | } |
|---|
| 483 | |
|---|
| 484 | /* |
|---|
| 485 | * sends an email, using Piwigo specific informations |
|---|
| 486 | * |
|---|
| 487 | * @param: |
|---|
| 488 | * - to: receiver(s) of the mail (list separated by comma). |
|---|
| 489 | * - args: function params of mail function: |
|---|
| 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] |
|---|
| 493 | * o subject [default value 'Piwigo'] |
|---|
| 494 | * o content: content of mail [default value ''] |
|---|
| 495 | * o content_format: format of mail content [default value 'text/plain'] |
|---|
| 496 | * o email_format: global mail format [default value $conf_mail['default_email_format']] |
|---|
| 497 | * o theme: template to use [default get_default_theme()] |
|---|
| 498 | * |
|---|
| 499 | * @return boolean (Ok or not) |
|---|
| 500 | */ |
|---|
| 501 | function pwg_mail($to, $args = array()) |
|---|
| 502 | { |
|---|
| 503 | global $conf, $conf_mail, $lang_info, $page; |
|---|
| 504 | |
|---|
| 505 | if (empty($to) and empty($args['Cc']) and empty($args['Bcc'])) |
|---|
| 506 | { |
|---|
| 507 | return true; |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | if (!isset($conf_mail)) |
|---|
| 511 | { |
|---|
| 512 | $conf_mail = get_mail_configuration(); |
|---|
| 513 | } |
|---|
| 514 | |
|---|
| 515 | if (empty($args['email_format'])) |
|---|
| 516 | { |
|---|
| 517 | $args['email_format'] = $conf_mail['default_email_format']; |
|---|
| 518 | } |
|---|
| 519 | |
|---|
| 520 | // Compute root_path in order have complete path |
|---|
| 521 | set_make_full_url(); |
|---|
| 522 | |
|---|
| 523 | if (empty($args['from'])) |
|---|
| 524 | { |
|---|
| 525 | $args['from'] = $conf_mail['formated_email_webmaster']; |
|---|
| 526 | } |
|---|
| 527 | else |
|---|
| 528 | { |
|---|
| 529 | $args['from'] = format_email('', $args['from']); |
|---|
| 530 | } |
|---|
| 531 | |
|---|
| 532 | if (empty($args['subject'])) |
|---|
| 533 | { |
|---|
| 534 | $args['subject'] = 'Piwigo'; |
|---|
| 535 | } |
|---|
| 536 | // Spring cleaning |
|---|
| 537 | $cvt_subject = trim(preg_replace('#[\n\r]+#s', '', $args['subject'])); |
|---|
| 538 | // Ascii convertion |
|---|
| 539 | $cvt_subject = encode_mime_header($cvt_subject); |
|---|
| 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 | |
|---|
| 551 | if ($conf_mail['send_bcc_mail_webmaster']) |
|---|
| 552 | { |
|---|
| 553 | $args['Bcc'][] = $conf_mail['formated_email_webmaster']; |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | if (empty($args['theme'])) |
|---|
| 557 | { |
|---|
| 558 | $args['theme'] = get_default_theme(); |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | $headers = 'From: '.$args['from']."\n"; |
|---|
| 562 | $headers.= 'Reply-To: '.$args['from']."\n"; |
|---|
| 563 | |
|---|
| 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 | |
|---|
| 574 | $headers.= 'Content-Type: multipart/alternative;'."\n"; |
|---|
| 575 | $headers.= ' boundary="---='.$conf_mail['boundary_key'].'";'."\n"; |
|---|
| 576 | $headers.= ' reply-type=original'."\n"; |
|---|
| 577 | $headers.= 'MIME-Version: 1.0'."\n"; |
|---|
| 578 | $headers.= 'X-Mailer: Piwigo Mailer'."\n"; |
|---|
| 579 | |
|---|
| 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 | |
|---|
| 587 | $content = ''; |
|---|
| 588 | |
|---|
| 589 | foreach (array_unique($content_type_list) as $content_type) |
|---|
| 590 | { |
|---|
| 591 | // key compose of indexes witch allow ti cache mail data |
|---|
| 592 | $cache_key = $content_type.'-'.$lang_info['code'].'-'.$args['theme']; |
|---|
| 593 | |
|---|
| 594 | if (!isset($conf_mail[$cache_key])) |
|---|
| 595 | { |
|---|
| 596 | if (!isset($conf_mail[$cache_key]['theme'])) |
|---|
| 597 | { |
|---|
| 598 | $conf_mail[$cache_key]['theme'] = get_mail_template($content_type, $args['theme']); |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | $conf_mail[$cache_key]['theme']->set_filename('mail_header', 'header.tpl'); |
|---|
| 602 | $conf_mail[$cache_key]['theme']->set_filename('mail_footer', 'footer.tpl'); |
|---|
| 603 | |
|---|
| 604 | $conf_mail[$cache_key]['theme']->assign( |
|---|
| 605 | array( |
|---|
| 606 | //Header |
|---|
| 607 | 'BOUNDARY_KEY' => $conf_mail['boundary_key'], |
|---|
| 608 | 'CONTENT_TYPE' => $content_type, |
|---|
| 609 | 'CONTENT_ENCODING' => get_pwg_charset(), |
|---|
| 610 | |
|---|
| 611 | // Footer |
|---|
| 612 | 'GALLERY_URL' => get_gallery_home_url(), |
|---|
| 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, |
|---|
| 618 | |
|---|
| 619 | 'TITLE_MAIL' => urlencode(l10n('A comment on your site')), |
|---|
| 620 | 'MAIL' => get_webmaster_mail_address() |
|---|
| 621 | )); |
|---|
| 622 | |
|---|
| 623 | if ($content_type == 'text/html') |
|---|
| 624 | { |
|---|
| 625 | if ($conf_mail[$cache_key]['theme']->smarty->template_exists('global-mail-css.tpl')) |
|---|
| 626 | { |
|---|
| 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'); |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | $file = PHPWG_ROOT_PATH.'themes/'.$args['theme'].'/mail-css.tpl'; |
|---|
| 632 | if (is_file($file)) |
|---|
| 633 | { |
|---|
| 634 | $conf_mail[$cache_key]['theme']->set_filename('css', realpath($file)); |
|---|
| 635 | $conf_mail[$cache_key]['theme']->assign_var_from_handle('MAIL_CSS', 'css'); |
|---|
| 636 | } |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | // what are displayed on the header of each mail ? |
|---|
| 640 | $conf_mail[$cache_key]['header'] = |
|---|
| 641 | $conf_mail[$cache_key]['theme']->parse('mail_header', true); |
|---|
| 642 | |
|---|
| 643 | // what are displayed on the footer of each mail ? |
|---|
| 644 | $conf_mail[$cache_key]['footer'] = |
|---|
| 645 | $conf_mail[$cache_key]['theme']->parse('mail_footer', true); |
|---|
| 646 | } |
|---|
| 647 | |
|---|
| 648 | // Header |
|---|
| 649 | $content.= $conf_mail[$cache_key]['header']; |
|---|
| 650 | |
|---|
| 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 | } |
|---|
| 670 | |
|---|
| 671 | // Footer |
|---|
| 672 | $content.= $conf_mail[$cache_key]['footer']; |
|---|
| 673 | |
|---|
| 674 | // Close boundary |
|---|
| 675 | $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n"; |
|---|
| 676 | } |
|---|
| 677 | |
|---|
| 678 | //~ // Close boundary |
|---|
| 679 | //~ $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n"; |
|---|
| 680 | |
|---|
| 681 | // Undo Compute root_path in order have complete path |
|---|
| 682 | unset_make_full_url(); |
|---|
| 683 | |
|---|
| 684 | return |
|---|
| 685 | trigger_event('send_mail', |
|---|
| 686 | false, /* Result */ |
|---|
| 687 | trigger_event('send_mail_to', get_strict_email_list($to)), |
|---|
| 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. |
|---|
| 701 | * - subject [default value 'Piwigo'] |
|---|
| 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 | { |
|---|
| 709 | if (!$result) |
|---|
| 710 | { |
|---|
| 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 | } |
|---|
| 733 | } |
|---|
| 734 | else |
|---|
| 735 | { |
|---|
| 736 | return $result; |
|---|
| 737 | } |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 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 | } |
|---|
| 763 | foreach ($selectors as $selector => $style) |
|---|
| 764 | { |
|---|
| 765 | if (!preg_match('/^(#|\.|)([A-Za-z0-9_-]*)$/', $selector, $matches)) |
|---|
| 766 | { |
|---|
| 767 | $unknow_selectors .= $selector.' {'.implode('; ', $style).";}\n"; |
|---|
| 768 | } |
|---|
| 769 | else switch ($matches[1]) |
|---|
| 770 | { |
|---|
| 771 | case '#': |
|---|
| 772 | $content = preg_replace('|id="'.$matches[2].'"|', 'id="'.$matches[2].'" style="'.implode('; ', $style).';"', $content); |
|---|
| 773 | break; |
|---|
| 774 | case '.': |
|---|
| 775 | $content = preg_replace('|class="'.$matches[2].'"|', 'class="'.$matches[2].'" style="'.implode('; ', $style).';"', $content); |
|---|
| 776 | break; |
|---|
| 777 | default: |
|---|
| 778 | $content = preg_replace('#<'.$matches[2].'( |>)#', '<'.$matches[2].' style="'.implode('; ', $style).';"$1', $content); |
|---|
| 779 | break; |
|---|
| 780 | } |
|---|
| 781 | } |
|---|
| 782 | |
|---|
| 783 | // Keep unknow tags in page head |
|---|
| 784 | if (!empty($unknow_selectors)) |
|---|
| 785 | { |
|---|
| 786 | $content = preg_replace('#<style>.*?</style>#s', "<style type=\"text/css\">\n$unknow_selectors</style>", $content); |
|---|
| 787 | } |
|---|
| 788 | else |
|---|
| 789 | { |
|---|
| 790 | $content = preg_replace('#<style>.*?</style>#s', '', $content); |
|---|
| 791 | } |
|---|
| 792 | } |
|---|
| 793 | return $content; |
|---|
| 794 | } |
|---|
| 795 | |
|---|
| 796 | /*Testing block*/ |
|---|
| 797 | function pwg_send_mail_test($result, $to, $subject, $content, $headers, $args) |
|---|
| 798 | { |
|---|
| 799 | global $conf, $user, $lang_info; |
|---|
| 800 | $dir = $conf['local_data_dir'].'/tmp'; |
|---|
| 801 | if ( mkgetdir( $dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR) ) |
|---|
| 802 | { |
|---|
| 803 | $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'.'.$args['theme'].'-'.date('YmdHis'); |
|---|
| 804 | if ($args['content_format'] == 'text/plain') |
|---|
| 805 | { |
|---|
| 806 | $filename .= '.txt'; |
|---|
| 807 | } |
|---|
| 808 | else |
|---|
| 809 | { |
|---|
| 810 | $filename .= '.html'; |
|---|
| 811 | } |
|---|
| 812 | $file = fopen($filename, 'w+'); |
|---|
| 813 | fwrite($file, $to ."\n"); |
|---|
| 814 | fwrite($file, $subject ."\n"); |
|---|
| 815 | fwrite($file, $headers); |
|---|
| 816 | fwrite($file, $content); |
|---|
| 817 | fclose($file); |
|---|
| 818 | } |
|---|
| 819 | return $result; |
|---|
| 820 | } |
|---|
| 821 | if ($conf['debug_mail']) |
|---|
| 822 | add_event_handler('send_mail', 'pwg_send_mail_test', EVENT_HANDLER_PRIORITY_NEUTRAL+10, 6); |
|---|
| 823 | |
|---|
| 824 | |
|---|
| 825 | add_event_handler('send_mail', 'pwg_send_mail', EVENT_HANDLER_PRIORITY_NEUTRAL, 5); |
|---|
| 826 | add_event_handler('send_mail_content', 'move_ccs_rules_to_body'); |
|---|
| 827 | trigger_action('functions_mail_included'); |
|---|
| 828 | |
|---|
| 829 | ?> |
|---|