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