[1021] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[26461] | 5 | // | Copyright(C) 2008-2014 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 | |
---|
[25344] | 24 | /** |
---|
[25550] | 25 | * @package functions\mail |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | /** |
---|
[25344] | 30 | * Returns the name of the mail sender |
---|
[25550] | 31 | * |
---|
[2284] | 32 | * @return string |
---|
| 33 | */ |
---|
| 34 | function get_mail_sender_name() |
---|
| 35 | { |
---|
| 36 | global $conf; |
---|
| 37 | |
---|
| 38 | return (empty($conf['mail_sender_name']) ? $conf['gallery_title'] : $conf['mail_sender_name']); |
---|
| 39 | } |
---|
| 40 | |
---|
[25344] | 41 | /** |
---|
| 42 | * Returns the email of the mail sender |
---|
[25550] | 43 | * |
---|
[25344] | 44 | * @since 2.6 |
---|
| 45 | * @return string |
---|
| 46 | */ |
---|
| 47 | function get_mail_sender_email() |
---|
| 48 | { |
---|
| 49 | global $conf; |
---|
| 50 | |
---|
| 51 | return (empty($conf['mail_sender_email']) ? get_webmaster_mail_address() : $conf['mail_sender_email']); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /** |
---|
[25550] | 55 | * Returns an array of mail configuration parameters. |
---|
[19225] | 56 | * - send_bcc_mail_webmaster |
---|
[25357] | 57 | * - mail_allow_html |
---|
[19225] | 58 | * - use_smtp |
---|
| 59 | * - smtp_host |
---|
| 60 | * - smtp_user |
---|
| 61 | * - smtp_password |
---|
[25344] | 62 | * - smtp_secure |
---|
[19225] | 63 | * - email_webmaster |
---|
[25344] | 64 | * - name_webmaster |
---|
[1021] | 65 | * |
---|
| 66 | * @return array |
---|
[1019] | 67 | */ |
---|
[1021] | 68 | function get_mail_configuration() |
---|
| 69 | { |
---|
| 70 | global $conf; |
---|
[1019] | 71 | |
---|
[1021] | 72 | $conf_mail = array( |
---|
[2106] | 73 | 'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'], |
---|
[25357] | 74 | 'mail_allow_html' => $conf['mail_allow_html'], |
---|
[25344] | 75 | 'mail_theme' => $conf['mail_theme'], |
---|
[2106] | 76 | 'use_smtp' => !empty($conf['smtp_host']), |
---|
| 77 | 'smtp_host' => $conf['smtp_host'], |
---|
| 78 | 'smtp_user' => $conf['smtp_user'], |
---|
[19225] | 79 | 'smtp_password' => $conf['smtp_password'], |
---|
[24951] | 80 | 'smtp_secure' => $conf['smtp_secure'], |
---|
[25344] | 81 | 'email_webmaster' => get_mail_sender_email(), |
---|
| 82 | 'name_webmaster' => get_mail_sender_name(), |
---|
[1021] | 83 | ); |
---|
| 84 | |
---|
| 85 | return $conf_mail; |
---|
| 86 | } |
---|
| 87 | |
---|
[1019] | 88 | /** |
---|
[25550] | 89 | * Returns an email address with an associated real name. |
---|
| 90 | * Can return either: |
---|
| 91 | * - email@domain.com |
---|
| 92 | * - name <email@domain.com> |
---|
| 93 | * |
---|
| 94 | * @param string $name |
---|
| 95 | * @param string $email |
---|
| 96 | * @return string |
---|
[1021] | 97 | */ |
---|
| 98 | function format_email($name, $email) |
---|
| 99 | { |
---|
[2280] | 100 | $cvt_email = trim(preg_replace('#[\n\r]+#s', '', $email)); |
---|
[2284] | 101 | $cvt_name = trim(preg_replace('#[\n\r]+#s', '', $name)); |
---|
[2280] | 102 | |
---|
[2284] | 103 | if ($cvt_name!="") |
---|
[1021] | 104 | { |
---|
[24951] | 105 | $cvt_name = '"'.addcslashes($cvt_name,'"').'"'.' '; |
---|
[2284] | 106 | } |
---|
[1531] | 107 | |
---|
[2284] | 108 | if (strpos($cvt_email, '<') === false) |
---|
| 109 | { |
---|
| 110 | return $cvt_name.'<'.$cvt_email.'>'; |
---|
[1021] | 111 | } |
---|
| 112 | else |
---|
| 113 | { |
---|
[2284] | 114 | return $cvt_name.$cvt_email; |
---|
[1021] | 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
[2106] | 118 | /** |
---|
[25550] | 119 | * Returns the email and the name from a formatted address. |
---|
[25344] | 120 | * @since 2.6 |
---|
[25550] | 121 | * |
---|
| 122 | * @param string|string[] $input - if is an array must contain email[, name] |
---|
| 123 | * @return array email, name |
---|
[25344] | 124 | */ |
---|
| 125 | function unformat_email($input) |
---|
| 126 | { |
---|
| 127 | if (is_array($input)) |
---|
| 128 | { |
---|
[25361] | 129 | if (!isset($input['name'])) |
---|
| 130 | { |
---|
| 131 | $input['name'] = ''; |
---|
| 132 | } |
---|
[25344] | 133 | return $input; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if (preg_match('/(.*)<(.*)>.*/', $input, $matches)) |
---|
| 137 | { |
---|
| 138 | return array( |
---|
| 139 | 'email' => trim($matches[2]), |
---|
| 140 | 'name' => trim($matches[1]), |
---|
| 141 | ); |
---|
| 142 | } |
---|
| 143 | else |
---|
| 144 | { |
---|
| 145 | return array( |
---|
| 146 | 'email' => trim($input), |
---|
| 147 | 'name' => '', |
---|
| 148 | ); |
---|
| 149 | } |
---|
| 150 | } |
---|
[25361] | 151 | |
---|
| 152 | /** |
---|
[25550] | 153 | * Return a clean array of hashmaps (email, name) removing duplicates. |
---|
| 154 | * It accepts various inputs: |
---|
| 155 | * - comma separated list |
---|
| 156 | * - array of emails |
---|
| 157 | * - single hashmap (email[, name]) |
---|
| 158 | * - array of incomplete hashmaps |
---|
[25361] | 159 | * @since 2.6 |
---|
| 160 | * |
---|
| 161 | * @param mixed $data |
---|
| 162 | * @return string[][] |
---|
| 163 | */ |
---|
| 164 | function get_clean_recipients_list($data) |
---|
| 165 | { |
---|
| 166 | if (empty($data)) |
---|
| 167 | { |
---|
| 168 | return array(); |
---|
| 169 | } |
---|
| 170 | else if (is_array($data)) |
---|
| 171 | { |
---|
| 172 | $values = array_values($data); |
---|
| 173 | if (!is_array($values[0])) |
---|
| 174 | { |
---|
| 175 | $keys = array_keys($data); |
---|
| 176 | if (is_int($keys[0])) |
---|
| 177 | { // simple array of emails |
---|
| 178 | foreach ($data as &$item) |
---|
| 179 | { |
---|
| 180 | $item = array( |
---|
[25550] | 181 | 'email' => trim($item), |
---|
[25361] | 182 | 'name' => '', |
---|
| 183 | ); |
---|
| 184 | } |
---|
| 185 | unset($item); |
---|
| 186 | } |
---|
| 187 | else |
---|
| 188 | { // hashmap of one recipient |
---|
| 189 | $data = array(unformat_email($data)); |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | else |
---|
| 193 | { // array of hashmaps |
---|
| 194 | $data = array_map('unformat_email', $data); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | else |
---|
| 198 | { |
---|
| 199 | $data = explode(',', $data); |
---|
| 200 | $data = array_map('unformat_email', $data); |
---|
| 201 | } |
---|
[25550] | 202 | |
---|
| 203 | $existing = array(); |
---|
| 204 | foreach ($data as $i => $entry) |
---|
| 205 | { |
---|
[25563] | 206 | if (isset($existing[ $entry['email'] ])) |
---|
[25550] | 207 | { |
---|
| 208 | unset($data[$i]); |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | { |
---|
| 212 | $existing[ $entry['email'] ] = true; |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | return array_values($data); |
---|
[25361] | 217 | } |
---|
[25344] | 218 | |
---|
| 219 | /** |
---|
[25550] | 220 | * Returns an email address list with minimal email string. |
---|
| 221 | * @deprecated 2.6 |
---|
| 222 | * |
---|
[25344] | 223 | * @param string $email_list - comma separated |
---|
| 224 | * @return string |
---|
[2284] | 225 | */ |
---|
| 226 | function get_strict_email_list($email_list) |
---|
| 227 | { |
---|
| 228 | $result = array(); |
---|
| 229 | $list = explode(',', $email_list); |
---|
[25344] | 230 | |
---|
[2284] | 231 | foreach ($list as $email) |
---|
| 232 | { |
---|
| 233 | if (strpos($email, '<') !== false) |
---|
| 234 | { |
---|
| 235 | $email = preg_replace('/.*<(.*)>.*/i', '$1', $email); |
---|
| 236 | } |
---|
| 237 | $result[] = trim($email); |
---|
| 238 | } |
---|
| 239 | |
---|
[19225] | 240 | return implode(',', array_unique($result)); |
---|
[2284] | 241 | } |
---|
| 242 | |
---|
[2106] | 243 | /** |
---|
[25550] | 244 | * Return an new mail template. |
---|
| 245 | * |
---|
[25344] | 246 | * @param string $email_format - text/html or text/plain |
---|
| 247 | * @return Template |
---|
[2106] | 248 | */ |
---|
[25344] | 249 | function &get_mail_template($email_format) |
---|
[2106] | 250 | { |
---|
[25344] | 251 | $template = new Template(PHPWG_ROOT_PATH.'themes', 'default', 'template/mail/'.$email_format); |
---|
| 252 | return $template; |
---|
[2106] | 253 | } |
---|
| 254 | |
---|
| 255 | /** |
---|
[25550] | 256 | * Return string email format (text/html or text/plain). |
---|
| 257 | * |
---|
| 258 | * @param bool $is_html |
---|
[25360] | 259 | * @return string |
---|
| 260 | */ |
---|
| 261 | function get_str_email_format($is_html) |
---|
| 262 | { |
---|
| 263 | return ($is_html ? 'text/html' : 'text/plain'); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | /** |
---|
[25550] | 267 | * Switch language to specified language. |
---|
[1904] | 268 | * All entries are push on language stack |
---|
[25550] | 269 | * |
---|
[25344] | 270 | * @param string $language |
---|
[1904] | 271 | */ |
---|
| 272 | function switch_lang_to($language) |
---|
| 273 | { |
---|
[23823] | 274 | global $switch_lang, $user, $lang, $lang_info, $language_files; |
---|
[1904] | 275 | |
---|
[4908] | 276 | // explanation of switch_lang |
---|
| 277 | // $switch_lang['language'] contains data of language |
---|
| 278 | // $switch_lang['stack'] contains stack LIFO |
---|
| 279 | // $switch_lang['initialisation'] allow to know if it's first call |
---|
| 280 | |
---|
| 281 | // Treatment with current user |
---|
| 282 | // Language of current user is saved (it's considered OK on firt call) |
---|
| 283 | if (!isset($switch_lang['initialisation']) and !isset($switch_lang['language'][$user['language']])) |
---|
[1904] | 284 | { |
---|
[4908] | 285 | $switch_lang['initialisation'] = true; |
---|
| 286 | $switch_lang['language'][$user['language']]['lang_info'] = $lang_info; |
---|
| 287 | $switch_lang['language'][$user['language']]['lang'] = $lang; |
---|
[1904] | 288 | } |
---|
| 289 | |
---|
[4908] | 290 | // Change current infos |
---|
| 291 | $switch_lang['stack'][] = $user['language']; |
---|
| 292 | $user['language'] = $language; |
---|
[1904] | 293 | |
---|
[4908] | 294 | // Load new data if necessary |
---|
| 295 | if (!isset($switch_lang['language'][$language])) |
---|
[1904] | 296 | { |
---|
[4908] | 297 | // Re-Init language arrays |
---|
| 298 | $lang_info = array(); |
---|
| 299 | $lang = array(); |
---|
[1904] | 300 | |
---|
[4908] | 301 | // language files |
---|
| 302 | load_language('common.lang', '', array('language'=>$language) ); |
---|
| 303 | // No test admin because script is checked admin (user selected no) |
---|
| 304 | // Translations are in admin file too |
---|
| 305 | load_language('admin.lang', '', array('language'=>$language) ); |
---|
[23823] | 306 | |
---|
[25344] | 307 | // Reload all plugins files (see load_language declaration) |
---|
[23823] | 308 | if (!empty($language_files)) |
---|
| 309 | { |
---|
| 310 | foreach ($language_files as $dirname => $files) |
---|
| 311 | foreach ($files as $filename) |
---|
| 312 | load_language($filename, $dirname, array('language'=>$language) ); |
---|
| 313 | } |
---|
| 314 | |
---|
[4908] | 315 | trigger_action('loading_lang'); |
---|
[8722] | 316 | load_language('lang', PHPWG_ROOT_PATH.PWG_LOCAL_DIR, |
---|
[5208] | 317 | array('language'=>$language, 'no_fallback'=>true, 'local'=>true) |
---|
| 318 | ); |
---|
[1904] | 319 | |
---|
[4908] | 320 | $switch_lang['language'][$language]['lang_info'] = $lang_info; |
---|
| 321 | $switch_lang['language'][$language]['lang'] = $lang; |
---|
[1904] | 322 | } |
---|
[4908] | 323 | else |
---|
| 324 | { |
---|
| 325 | $lang_info = $switch_lang['language'][$language]['lang_info']; |
---|
| 326 | $lang = $switch_lang['language'][$language]['lang']; |
---|
| 327 | } |
---|
[1904] | 328 | } |
---|
| 329 | |
---|
[25344] | 330 | /** |
---|
[25550] | 331 | * Switch back language pushed with switch_lang_to() function. |
---|
| 332 | * @see switch_lang_to() |
---|
[1904] | 333 | */ |
---|
| 334 | function switch_lang_back() |
---|
| 335 | { |
---|
| 336 | global $switch_lang, $user, $lang, $lang_info; |
---|
| 337 | |
---|
| 338 | if (count($switch_lang['stack']) > 0) |
---|
| 339 | { |
---|
[4908] | 340 | // Get last value |
---|
| 341 | $language = array_pop($switch_lang['stack']); |
---|
[1904] | 342 | |
---|
[4908] | 343 | // Change current infos |
---|
| 344 | if (isset($switch_lang['language'][$language])) |
---|
[1904] | 345 | { |
---|
[4908] | 346 | $lang_info = $switch_lang['language'][$language]['lang_info']; |
---|
| 347 | $lang = $switch_lang['language'][$language]['lang']; |
---|
[1904] | 348 | } |
---|
[2140] | 349 | $user['language'] = $language; |
---|
[1904] | 350 | } |
---|
| 351 | } |
---|
[1908] | 352 | |
---|
| 353 | /** |
---|
[25550] | 354 | * Send a notification email to all administrators. |
---|
[25357] | 355 | * current user (if admin) is not notified |
---|
[25550] | 356 | * |
---|
[25360] | 357 | * @param string|array $subject |
---|
| 358 | * @param string|array $content |
---|
[25357] | 359 | * @param boolean $send_technical_details - send user IP and browser |
---|
| 360 | * @return boolean |
---|
[1908] | 361 | */ |
---|
[25357] | 362 | function pwg_mail_notification_admins($subject, $content, $send_technical_details=true) |
---|
[1908] | 363 | { |
---|
[25357] | 364 | if (empty($subject) or empty($content)) |
---|
| 365 | { |
---|
| 366 | return false; |
---|
| 367 | } |
---|
[25360] | 368 | |
---|
| 369 | global $conf, $user; |
---|
| 370 | |
---|
| 371 | if (is_array($subject) or is_array($content)) |
---|
[25357] | 372 | { |
---|
[25360] | 373 | switch_lang_to(get_default_language()); |
---|
| 374 | |
---|
| 375 | if (is_array($subject)) |
---|
| 376 | { |
---|
| 377 | $subject = l10n_args($subject); |
---|
| 378 | } |
---|
| 379 | if (is_array($content)) |
---|
| 380 | { |
---|
| 381 | $content = l10n_args($content); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | switch_lang_back(); |
---|
[25357] | 385 | } |
---|
| 386 | |
---|
| 387 | $tpl_vars = array(); |
---|
| 388 | if ($send_technical_details) |
---|
[2140] | 389 | { |
---|
[25360] | 390 | $tpl_vars['TECHNICAL'] = array( |
---|
| 391 | 'username' => stripslashes($user['username']), |
---|
| 392 | 'ip' => $_SERVER['REMOTE_ADDR'], |
---|
| 393 | 'user_agent' => $_SERVER['HTTP_USER_AGENT'], |
---|
| 394 | ); |
---|
[25357] | 395 | } |
---|
| 396 | |
---|
| 397 | return pwg_mail_admins( |
---|
| 398 | array( |
---|
| 399 | 'subject' => '['. $conf['gallery_title'] .'] '. $subject, |
---|
| 400 | 'mail_title' => $conf['gallery_title'], |
---|
| 401 | 'mail_subtitle' => $subject, |
---|
| 402 | 'content' => $content, |
---|
| 403 | 'content_format' => 'text/plain', |
---|
| 404 | ), |
---|
| 405 | array( |
---|
| 406 | 'filename' => 'notification_admin', |
---|
| 407 | 'assign' => $tpl_vars, |
---|
| 408 | ) |
---|
| 409 | ); |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | /** |
---|
[25550] | 413 | * Send a email to all administrators. |
---|
[25357] | 414 | * current user (if admin) is excluded |
---|
| 415 | * @see pwg_mail() |
---|
[25358] | 416 | * @since 2.6 |
---|
[25357] | 417 | * |
---|
| 418 | * @param array $args - as in pwg_mail() |
---|
| 419 | * @param array $tpl - as in pwg_mail() |
---|
| 420 | * @return boolean |
---|
| 421 | */ |
---|
| 422 | function pwg_mail_admins($args=array(), $tpl=array()) |
---|
| 423 | { |
---|
| 424 | if (empty($args['content']) and empty($tpl)) |
---|
| 425 | { |
---|
[2140] | 426 | return false; |
---|
| 427 | } |
---|
| 428 | |
---|
[25357] | 429 | global $conf, $user; |
---|
[1908] | 430 | $return = true; |
---|
| 431 | |
---|
[25357] | 432 | // get admins (except ourself) |
---|
[1908] | 433 | $query = ' |
---|
[9360] | 434 | SELECT |
---|
[25357] | 435 | u.'.$conf['user_fields']['username'].' AS name, |
---|
| 436 | u.'.$conf['user_fields']['email'].' AS email |
---|
[9360] | 437 | FROM '.USERS_TABLE.' AS u |
---|
[25357] | 438 | JOIN '.USER_INFOS_TABLE.' AS i |
---|
| 439 | ON i.user_id = u.'.$conf['user_fields']['id'].' |
---|
[9360] | 440 | WHERE i.status in (\'webmaster\', \'admin\') |
---|
[25357] | 441 | AND u.'.$conf['user_fields']['email'].' IS NOT NULL |
---|
[9360] | 442 | AND i.user_id <> '.$user['id'].' |
---|
[27997] | 443 | ORDER BY name |
---|
[9360] | 444 | ;'; |
---|
[25357] | 445 | $admins = array_from_query($query); |
---|
[1908] | 446 | |
---|
[25357] | 447 | if (empty($admins)) |
---|
[1908] | 448 | { |
---|
[25357] | 449 | return $return; |
---|
[1908] | 450 | } |
---|
| 451 | |
---|
[25357] | 452 | switch_lang_to(get_default_language()); |
---|
[1908] | 453 | |
---|
[25357] | 454 | $return = pwg_mail($admins, $args, $tpl); |
---|
[9360] | 455 | |
---|
[25357] | 456 | switch_lang_back(); |
---|
[1914] | 457 | |
---|
[1908] | 458 | return $return; |
---|
| 459 | } |
---|
[2106] | 460 | |
---|
[25357] | 461 | /** |
---|
[25550] | 462 | * Send an email to a group. |
---|
[25357] | 463 | * @see pwg_mail() |
---|
[1904] | 464 | * |
---|
[25357] | 465 | * @param int $group_id |
---|
| 466 | * @param array $args - as in pwg_mail() |
---|
[25550] | 467 | * o language_selected: filters users of the group by language [default value empty] |
---|
[25357] | 468 | * @param array $tpl - as in pwg_mail() |
---|
| 469 | * @return boolean |
---|
[1908] | 470 | */ |
---|
[25357] | 471 | function pwg_mail_group($group_id, $args=array(), $tpl=array()) |
---|
| 472 | { |
---|
| 473 | if (empty($group_id) or ( empty($args['content']) and empty($tpl) )) |
---|
[2140] | 474 | { |
---|
| 475 | return false; |
---|
| 476 | } |
---|
| 477 | |
---|
[1904] | 478 | global $conf; |
---|
[1908] | 479 | $return = true; |
---|
[1904] | 480 | |
---|
[25357] | 481 | // get distinct languages of targeted users |
---|
[1904] | 482 | $query = ' |
---|
[25357] | 483 | SELECT DISTINCT language |
---|
| 484 | FROM '.USER_GROUP_TABLE.' AS ug |
---|
| 485 | INNER JOIN '.USERS_TABLE.' AS u |
---|
| 486 | ON '.$conf['user_fields']['id'].' = ug.user_id |
---|
| 487 | INNER JOIN '.USER_INFOS_TABLE.' AS ui |
---|
| 488 | ON ui.user_id = ug.user_id |
---|
| 489 | WHERE group_id = '.$group_id.' |
---|
| 490 | AND '.$conf['user_fields']['email'].' <> ""'; |
---|
| 491 | if (!empty($args['language_selected'])) |
---|
[1904] | 492 | { |
---|
| 493 | $query .= ' |
---|
[25357] | 494 | AND language = \''.$args['language_selected'].'\''; |
---|
[1904] | 495 | } |
---|
| 496 | |
---|
| 497 | $query .= ' |
---|
| 498 | ;'; |
---|
[25357] | 499 | $languages = array_from_query($query, 'language'); |
---|
[1904] | 500 | |
---|
[25357] | 501 | if (empty($languages)) |
---|
| 502 | { |
---|
| 503 | return $return; |
---|
| 504 | } |
---|
[1904] | 505 | |
---|
[25357] | 506 | foreach ($languages as $language) |
---|
[2106] | 507 | { |
---|
[25357] | 508 | // get subset of users in this group for a specific language |
---|
| 509 | $query = ' |
---|
[1904] | 510 | SELECT |
---|
[25357] | 511 | u.'.$conf['user_fields']['username'].' AS name, |
---|
| 512 | u.'.$conf['user_fields']['email'].' AS email |
---|
| 513 | FROM '.USER_GROUP_TABLE.' AS ug |
---|
| 514 | INNER JOIN '.USERS_TABLE.' AS u |
---|
| 515 | ON '.$conf['user_fields']['id'].' = ug.user_id |
---|
| 516 | INNER JOIN '.USER_INFOS_TABLE.' AS ui |
---|
| 517 | ON ui.user_id = ug.user_id |
---|
| 518 | WHERE group_id = '.$group_id.' |
---|
| 519 | AND '.$conf['user_fields']['email'].' <> "" |
---|
| 520 | AND language = \''.$language.'\' |
---|
[1904] | 521 | ;'; |
---|
[25357] | 522 | $users = array_from_query($query); |
---|
[1904] | 523 | |
---|
[25357] | 524 | if (empty($users)) |
---|
| 525 | { |
---|
| 526 | continue; |
---|
| 527 | } |
---|
[1904] | 528 | |
---|
[25357] | 529 | switch_lang_to($language); |
---|
[1904] | 530 | |
---|
[25357] | 531 | $return&= pwg_mail(null, |
---|
| 532 | array_merge( |
---|
| 533 | $args, |
---|
| 534 | array('Bcc' => $users) |
---|
| 535 | ), |
---|
| 536 | $tpl |
---|
| 537 | ); |
---|
[1904] | 538 | |
---|
[25357] | 539 | switch_lang_back(); |
---|
[1904] | 540 | } |
---|
[1908] | 541 | |
---|
| 542 | return $return; |
---|
[1904] | 543 | } |
---|
| 544 | |
---|
[25344] | 545 | /** |
---|
[25550] | 546 | * Sends an email, using Piwigo specific informations. |
---|
[1809] | 547 | * |
---|
[25550] | 548 | * @param string|array $to |
---|
[25344] | 549 | * @param array $args |
---|
[2106] | 550 | * o from: sender [default value webmaster email] |
---|
| 551 | * o Cc: array of carbon copy receivers of the mail. [default value empty] |
---|
| 552 | * o Bcc: array of blind carbon copy receivers of the mail. [default value empty] |
---|
[25357] | 553 | * o subject [default value 'Piwigo'] |
---|
| 554 | * o content: content of mail [default value ''] |
---|
| 555 | * o content_format: format of mail content [default value 'text/plain'] |
---|
| 556 | * o email_format: global mail format [default value $conf_mail['default_email_format']] |
---|
[25344] | 557 | * o theme: theme to use [default value $conf_mail['mail_theme']] |
---|
| 558 | * o mail_title: main title of the mail [default value $conf['gallery_title']] |
---|
| 559 | * o mail_subtitle: subtitle of the mail [default value subject] |
---|
[25357] | 560 | * @param array $tpl - use these options to define a custom content template file |
---|
| 561 | * o filename |
---|
| 562 | * o dirname (optional) |
---|
| 563 | * o assign (optional) |
---|
[1908] | 564 | * |
---|
[25344] | 565 | * @return boolean |
---|
[2106] | 566 | */ |
---|
[25357] | 567 | function pwg_mail($to, $args=array(), $tpl=array()) |
---|
[1019] | 568 | { |
---|
[1809] | 569 | global $conf, $conf_mail, $lang_info, $page; |
---|
[2106] | 570 | |
---|
| 571 | if (empty($to) and empty($args['Cc']) and empty($args['Bcc'])) |
---|
| 572 | { |
---|
| 573 | return true; |
---|
| 574 | } |
---|
[2121] | 575 | |
---|
[1021] | 576 | if (!isset($conf_mail)) |
---|
| 577 | { |
---|
| 578 | $conf_mail = get_mail_configuration(); |
---|
| 579 | } |
---|
[2106] | 580 | |
---|
[24952] | 581 | include_once(PHPWG_ROOT_PATH.'include/phpmailer/class.phpmailer.php'); |
---|
[24951] | 582 | |
---|
| 583 | $mail = new PHPMailer; |
---|
| 584 | |
---|
[25361] | 585 | foreach (get_clean_recipients_list($to) as $recipient) |
---|
[2106] | 586 | { |
---|
[25344] | 587 | $mail->addAddress($recipient['email'], $recipient['name']); |
---|
[2106] | 588 | } |
---|
[25344] | 589 | |
---|
[24951] | 590 | $mail->WordWrap = 76; |
---|
| 591 | $mail->CharSet = 'UTF-8'; |
---|
| 592 | |
---|
[1676] | 593 | // Compute root_path in order have complete path |
---|
[3938] | 594 | set_make_full_url(); |
---|
[2106] | 595 | |
---|
[1809] | 596 | if (empty($args['from'])) |
---|
[1021] | 597 | { |
---|
[25344] | 598 | $from = array( |
---|
| 599 | 'email' => $conf_mail['email_webmaster'], |
---|
| 600 | 'name' => $conf_mail['name_webmaster'], |
---|
| 601 | ); |
---|
[1021] | 602 | } |
---|
| 603 | else |
---|
| 604 | { |
---|
[25344] | 605 | $from = unformat_email($args['from']); |
---|
[1021] | 606 | } |
---|
[25344] | 607 | $mail->setFrom($from['email'], $from['name']); |
---|
| 608 | $mail->addReplyTo($from['email'], $from['name']); |
---|
[1021] | 609 | |
---|
[24951] | 610 | // Subject |
---|
[1809] | 611 | if (empty($args['subject'])) |
---|
| 612 | { |
---|
[2339] | 613 | $args['subject'] = 'Piwigo'; |
---|
[1809] | 614 | } |
---|
[24951] | 615 | $args['subject'] = trim(preg_replace('#[\n\r]+#s', '', $args['subject'])); |
---|
| 616 | $mail->Subject = $args['subject']; |
---|
| 617 | |
---|
| 618 | // Cc |
---|
| 619 | if (!empty($args['Cc'])) |
---|
[1809] | 620 | { |
---|
[25361] | 621 | foreach (get_clean_recipients_list($args['Cc']) as $recipient) |
---|
[25344] | 622 | { |
---|
[25361] | 623 | $mail->addCC($recipient['email'], $recipient['name']); |
---|
[25344] | 624 | } |
---|
[1809] | 625 | } |
---|
| 626 | |
---|
[24951] | 627 | // Bcc |
---|
[25361] | 628 | $Bcc = get_clean_recipients_list(@$args['Bcc']); |
---|
[24951] | 629 | if ($conf_mail['send_bcc_mail_webmaster']) |
---|
[1809] | 630 | { |
---|
[25550] | 631 | $Bcc[] = array( |
---|
| 632 | 'email' => get_webmaster_mail_address(), |
---|
| 633 | 'name' => '', |
---|
| 634 | ); |
---|
[1809] | 635 | } |
---|
[25361] | 636 | if (!empty($Bcc)) |
---|
[2106] | 637 | { |
---|
[25361] | 638 | foreach ($Bcc as $recipient) |
---|
[24951] | 639 | { |
---|
[25361] | 640 | $mail->addBCC($recipient['email'], $recipient['name']); |
---|
[24951] | 641 | } |
---|
[2106] | 642 | } |
---|
| 643 | |
---|
[25344] | 644 | // theme |
---|
| 645 | if (empty($args['theme']) or !in_array($args['theme'], array('clear','dark'))) |
---|
[5123] | 646 | { |
---|
[25344] | 647 | $args['theme'] = $conf_mail['mail_theme']; |
---|
[5123] | 648 | } |
---|
[2106] | 649 | |
---|
[25344] | 650 | // content |
---|
[24951] | 651 | if (!isset($args['content'])) |
---|
| 652 | { |
---|
| 653 | $args['content'] = ''; |
---|
| 654 | } |
---|
[25357] | 655 | |
---|
| 656 | // try to decompose subject like "[....] ...." |
---|
| 657 | if (!isset($args['mail_title']) and !isset($args['mail_subtitle'])) |
---|
| 658 | { |
---|
| 659 | if (preg_match('#^\[(.*)\](.*)$#', $args['subject'], $matches)) |
---|
| 660 | { |
---|
| 661 | $args['mail_title'] = $matches[1]; |
---|
| 662 | $args['mail_subtitle'] = $matches[2]; |
---|
| 663 | } |
---|
| 664 | } |
---|
[25344] | 665 | if (!isset($args['mail_title'])) |
---|
| 666 | { |
---|
| 667 | $args['mail_title'] = $conf['gallery_title']; |
---|
| 668 | } |
---|
| 669 | if (!isset($args['mail_subtitle'])) |
---|
| 670 | { |
---|
| 671 | $args['mail_subtitle'] = $args['subject']; |
---|
| 672 | } |
---|
[1818] | 673 | |
---|
[25344] | 674 | // content type |
---|
[24951] | 675 | if (empty($args['content_format'])) |
---|
[2106] | 676 | { |
---|
[24951] | 677 | $args['content_format'] = 'text/plain'; |
---|
[2106] | 678 | } |
---|
| 679 | |
---|
[25344] | 680 | $content_type_list = array(); |
---|
[25357] | 681 | if ($conf_mail['mail_allow_html'] and @$args['email_format'] != 'text/plain') |
---|
[2106] | 682 | { |
---|
[25344] | 683 | $content_type_list[] = 'text/html'; |
---|
[2106] | 684 | } |
---|
[25344] | 685 | $content_type_list[] = 'text/plain'; |
---|
[2106] | 686 | |
---|
[24951] | 687 | $contents = array(); |
---|
[25344] | 688 | foreach ($content_type_list as $content_type) |
---|
[3938] | 689 | { |
---|
[25344] | 690 | // key compose of indexes witch allow to cache mail data |
---|
| 691 | $cache_key = $content_type.'-'.$lang_info['code']; |
---|
[2129] | 692 | |
---|
[3938] | 693 | if (!isset($conf_mail[$cache_key])) |
---|
[2106] | 694 | { |
---|
[25344] | 695 | // instanciate a new Template |
---|
[5123] | 696 | if (!isset($conf_mail[$cache_key]['theme'])) |
---|
[3938] | 697 | { |
---|
[25344] | 698 | $conf_mail[$cache_key]['theme'] = get_mail_template($content_type); |
---|
| 699 | trigger_action('before_parse_mail_template', $cache_key, $content_type); |
---|
[3938] | 700 | } |
---|
[25357] | 701 | $template = &$conf_mail[$cache_key]['theme']; |
---|
[2106] | 702 | |
---|
[25357] | 703 | $template->set_filename('mail_header', 'header.tpl'); |
---|
| 704 | $template->set_filename('mail_footer', 'footer.tpl'); |
---|
[2106] | 705 | |
---|
[25357] | 706 | $template->assign( |
---|
[3938] | 707 | array( |
---|
[6411] | 708 | 'GALLERY_URL' => get_gallery_home_url(), |
---|
[24951] | 709 | 'GALLERY_TITLE' => isset($page['gallery_title']) ? $page['gallery_title'] : $conf['gallery_title'], |
---|
[3938] | 710 | 'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '', |
---|
[25344] | 711 | 'PHPWG_URL' => defined('PHPWG_URL') ? PHPWG_URL : '', |
---|
| 712 | 'CONTENT_ENCODING' => get_pwg_charset(), |
---|
| 713 | 'CONTACT_MAIL' => $conf_mail['email_webmaster'], |
---|
[24951] | 714 | ) |
---|
| 715 | ); |
---|
[2106] | 716 | |
---|
[3938] | 717 | if ($content_type == 'text/html') |
---|
[2106] | 718 | { |
---|
[25384] | 719 | if ($template->smarty->templateExists('global-mail-css.tpl')) |
---|
[3938] | 720 | { |
---|
[25372] | 721 | $template->set_filename('global-css', 'global-mail-css.tpl'); |
---|
| 722 | $template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'global-css'); |
---|
[3938] | 723 | } |
---|
[2106] | 724 | |
---|
[25384] | 725 | if ($template->smarty->templateExists('mail-css-'. $args['theme'] .'.tpl')) |
---|
[3938] | 726 | { |
---|
[25357] | 727 | $template->set_filename('css', 'mail-css-'. $args['theme'] .'.tpl'); |
---|
| 728 | $template->assign_var_from_handle('MAIL_CSS', 'css'); |
---|
[3938] | 729 | } |
---|
[2106] | 730 | } |
---|
[3938] | 731 | } |
---|
[25358] | 732 | |
---|
| 733 | $template = &$conf_mail[$cache_key]['theme']; |
---|
| 734 | $template->assign( |
---|
| 735 | array( |
---|
| 736 | 'MAIL_TITLE' => $args['mail_title'], |
---|
| 737 | 'MAIL_SUBTITLE' => $args['mail_subtitle'], |
---|
| 738 | ) |
---|
| 739 | ); |
---|
[1809] | 740 | |
---|
[3938] | 741 | // Header |
---|
[25358] | 742 | $contents[$content_type] = $template->parse('mail_header', true); |
---|
[1019] | 743 | |
---|
[3938] | 744 | // Content |
---|
[25357] | 745 | // Stored in a temp variable, if a content template is used it will be assigned |
---|
| 746 | // to the $CONTENT template variable, otherwise it will be appened to the mail |
---|
[25344] | 747 | if ($args['content_format'] == 'text/plain' and $content_type == 'text/html') |
---|
[3938] | 748 | { |
---|
[25344] | 749 | // convert plain text to html |
---|
[25357] | 750 | $mail_content = |
---|
[25344] | 751 | '<p>'. |
---|
| 752 | nl2br( |
---|
| 753 | preg_replace( |
---|
| 754 | '/(https?:\/\/([-\w\.]+[-\w])+(:\d+)?(\/([\w\/_\.\#-]*(\?\S+)?[^\.\s])?)?)/i', |
---|
| 755 | '<a href="$1">$1</a>', |
---|
| 756 | htmlspecialchars($args['content']) |
---|
| 757 | ) |
---|
| 758 | ). |
---|
| 759 | '</p>'; |
---|
[3938] | 760 | } |
---|
[25344] | 761 | else if ($args['content_format'] == 'text/html' and $content_type == 'text/plain') |
---|
[3938] | 762 | { |
---|
| 763 | // convert html text to plain text |
---|
[25357] | 764 | $mail_content = strip_tags($args['content']); |
---|
[3938] | 765 | } |
---|
| 766 | else |
---|
| 767 | { |
---|
[25357] | 768 | $mail_content = $args['content']; |
---|
[3938] | 769 | } |
---|
[1809] | 770 | |
---|
[25357] | 771 | // Runtime template |
---|
| 772 | if (isset($tpl['filename'])) |
---|
| 773 | { |
---|
| 774 | if (isset($tpl['dirname'])) |
---|
| 775 | { |
---|
| 776 | $template->set_template_dir($tpl['dirname'] .'/'. $content_type); |
---|
| 777 | } |
---|
[25384] | 778 | if ($template->smarty->templateExists($tpl['filename'] .'.tpl')) |
---|
[25357] | 779 | { |
---|
| 780 | $template->set_filename($tpl['filename'], $tpl['filename'] .'.tpl'); |
---|
| 781 | if (!empty($tpl['assign'])) |
---|
| 782 | { |
---|
| 783 | $template->assign($tpl['assign']); |
---|
| 784 | } |
---|
| 785 | $template->assign('CONTENT', $mail_content); |
---|
| 786 | $contents[$content_type].= $template->parse($tpl['filename'], true); |
---|
| 787 | } |
---|
| 788 | else |
---|
| 789 | { |
---|
| 790 | $contents[$content_type].= $mail_content; |
---|
| 791 | } |
---|
| 792 | } |
---|
| 793 | else |
---|
| 794 | { |
---|
| 795 | $contents[$content_type].= $mail_content; |
---|
| 796 | } |
---|
| 797 | |
---|
[3938] | 798 | // Footer |
---|
[25358] | 799 | $contents[$content_type].= $template->parse('mail_footer', true); |
---|
[24951] | 800 | } |
---|
[1809] | 801 | |
---|
[24951] | 802 | // Undo Compute root_path in order have complete path |
---|
| 803 | unset_make_full_url(); |
---|
| 804 | |
---|
[25344] | 805 | // Send content to PHPMailer |
---|
[24951] | 806 | if (isset($contents['text/html'])) |
---|
| 807 | { |
---|
[25344] | 808 | $mail->isHTML(true); |
---|
| 809 | $mail->Body = move_css_to_body($contents['text/html']); |
---|
[24951] | 810 | |
---|
| 811 | if (isset($contents['text/plain'])) |
---|
| 812 | { |
---|
| 813 | $mail->AltBody = $contents['text/plain']; |
---|
| 814 | } |
---|
[3938] | 815 | } |
---|
[24951] | 816 | else |
---|
| 817 | { |
---|
| 818 | $mail->isHTML(false); |
---|
| 819 | $mail->Body = $contents['text/plain']; |
---|
| 820 | } |
---|
[1809] | 821 | |
---|
[24951] | 822 | if ($conf_mail['use_smtp']) |
---|
| 823 | { |
---|
| 824 | // now we need to split port number |
---|
| 825 | if (strpos($conf_mail['smtp_host'], ':') !== false) |
---|
| 826 | { |
---|
| 827 | list($smtp_host, $smtp_port) = explode(':', $conf_mail['smtp_host']); |
---|
| 828 | } |
---|
| 829 | else |
---|
| 830 | { |
---|
| 831 | $smtp_host = $conf_mail['smtp_host']; |
---|
| 832 | $smtp_port = 25; |
---|
| 833 | } |
---|
[3938] | 834 | |
---|
[24951] | 835 | $mail->IsSMTP(); |
---|
[1642] | 836 | |
---|
[24951] | 837 | // enables SMTP debug information (for testing) 2 - debug, 0 - no message |
---|
| 838 | $mail->SMTPDebug = 0; |
---|
| 839 | |
---|
| 840 | $mail->Host = $smtp_host; |
---|
| 841 | $mail->Port = $smtp_port; |
---|
| 842 | |
---|
| 843 | if (!empty($conf_mail['smtp_secure']) and in_array($conf_mail['smtp_secure'], array('ssl', 'tls'))) |
---|
| 844 | { |
---|
| 845 | $mail->SMTPSecure = $conf_mail['smtp_secure']; |
---|
| 846 | } |
---|
| 847 | |
---|
| 848 | if (!empty($conf_mail['smtp_user'])) |
---|
| 849 | { |
---|
| 850 | $mail->SMTPAuth = true; |
---|
| 851 | $mail->Username = $conf_mail['smtp_user']; |
---|
| 852 | $mail->Password = $conf_mail['smtp_password']; |
---|
| 853 | } |
---|
| 854 | } |
---|
[24966] | 855 | |
---|
[25344] | 856 | $ret = true; |
---|
| 857 | $pre_result = trigger_event('before_send_mail', true, $to, $args, $mail); |
---|
| 858 | |
---|
| 859 | if ($pre_result) |
---|
[24951] | 860 | { |
---|
[25344] | 861 | $ret = $mail->send(); |
---|
[25550] | 862 | if (!$ret and (!ini_get('display_errors') or is_admin())) |
---|
[25344] | 863 | { |
---|
| 864 | trigger_error('Mailer Error: ' . $mail->ErrorInfo, E_USER_WARNING); |
---|
| 865 | } |
---|
[25384] | 866 | if ($conf['debug_mail']) |
---|
| 867 | { |
---|
| 868 | pwg_send_mail_test($ret, $mail, $args); |
---|
| 869 | } |
---|
[24951] | 870 | } |
---|
[25344] | 871 | |
---|
[24966] | 872 | return $ret; |
---|
[2140] | 873 | } |
---|
| 874 | |
---|
[25344] | 875 | /** |
---|
| 876 | * @deprecated 2.6 |
---|
[2140] | 877 | */ |
---|
[25344] | 878 | function pwg_send_mail($result, $to, $subject, $content, $headers) |
---|
[2140] | 879 | { |
---|
[25357] | 880 | if (is_admin()) |
---|
| 881 | { |
---|
| 882 | trigger_error('pwg_send_mail function is deprecated', E_USER_NOTICE); |
---|
| 883 | } |
---|
[25344] | 884 | |
---|
[2280] | 885 | if (!$result) |
---|
| 886 | { |
---|
[25344] | 887 | return pwg_mail($to, array( |
---|
| 888 | 'content' => $content, |
---|
| 889 | 'subject' => $subject, |
---|
| 890 | )); |
---|
[2280] | 891 | } |
---|
| 892 | else |
---|
| 893 | { |
---|
| 894 | return $result; |
---|
| 895 | } |
---|
[2140] | 896 | } |
---|
| 897 | |
---|
[25344] | 898 | /** |
---|
[25550] | 899 | * Moves CSS rules contained in the <style> tag to inline CSS. |
---|
| 900 | * Used for compatibility with Gmail and such clients |
---|
[25344] | 901 | * @since 2.6 |
---|
[25550] | 902 | * |
---|
[25344] | 903 | * @param string $content |
---|
| 904 | * @return string |
---|
| 905 | */ |
---|
| 906 | function move_css_to_body($content) |
---|
| 907 | { |
---|
| 908 | include_once(PHPWG_ROOT_PATH.'include/emogrifier.class.php'); |
---|
[5695] | 909 | |
---|
[25344] | 910 | $e = new Emogrifier($content); |
---|
[27403] | 911 | return $e->emogrify(); |
---|
[25344] | 912 | } |
---|
[5695] | 913 | |
---|
[25344] | 914 | /** |
---|
[25550] | 915 | * Saves a copy of the mail if _data/tmp. |
---|
| 916 | * |
---|
| 917 | * @param boolean $success |
---|
| 918 | * @param PHPMailer $mail |
---|
[25344] | 919 | * @param array $args |
---|
| 920 | */ |
---|
[25384] | 921 | function pwg_send_mail_test($success, $mail, $args) |
---|
[25344] | 922 | { |
---|
| 923 | global $conf, $user, $lang_info; |
---|
| 924 | |
---|
| 925 | $dir = PHPWG_ROOT_PATH.$conf['data_location'].'tmp'; |
---|
| 926 | if (mkgetdir($dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR)) |
---|
| 927 | { |
---|
[25384] | 928 | $filename = $dir.'/mail.'.stripslashes($user['username']).'.'.$lang_info['code'].'-'.date('YmdHis').($success ? '' : '.ERROR'); |
---|
[25344] | 929 | if ($args['content_format'] == 'text/plain') |
---|
[5695] | 930 | { |
---|
[25344] | 931 | $filename .= '.txt'; |
---|
[5695] | 932 | } |
---|
| 933 | else |
---|
| 934 | { |
---|
[25344] | 935 | $filename .= '.html'; |
---|
[5695] | 936 | } |
---|
[25344] | 937 | |
---|
| 938 | $file = fopen($filename, 'w+'); |
---|
[25384] | 939 | if (!$success) |
---|
| 940 | { |
---|
| 941 | fwrite($file, "ERROR: " . $mail->ErrorInfo . "\n\n"); |
---|
| 942 | } |
---|
| 943 | fwrite($file, $mail->getSentMIMEMessage()); |
---|
[25344] | 944 | fclose($file); |
---|
[5695] | 945 | } |
---|
| 946 | } |
---|
| 947 | |
---|
[2140] | 948 | trigger_action('functions_mail_included'); |
---|
| 949 | |
---|
[25344] | 950 | ?> |
---|