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