[1021] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
| 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[1726] | 5 | // | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[1818] | 6 | // | Copyright (C) 2006-2007 Ruben ARNAUD - team@phpwebgallery.net | |
---|
[1021] | 7 | // +-----------------------------------------------------------------------+ |
---|
| 8 | // | branch : BSF (Best So Far) |
---|
| 9 | // | file : $RCSfile$ |
---|
| 10 | // | last update : $Date: 2005-11-26 21:15:50 +0100 (sam., 26 nov. 2005) $ |
---|
| 11 | // | last modifier : $Author: plg $ |
---|
| 12 | // | revision : $Revision: 958 $ |
---|
| 13 | // +-----------------------------------------------------------------------+ |
---|
| 14 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 15 | // | it under the terms of the GNU General Public License as published by | |
---|
| 16 | // | the Free Software Foundation | |
---|
| 17 | // | | |
---|
| 18 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 19 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 20 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 21 | // | General Public License for more details. | |
---|
| 22 | // | | |
---|
| 23 | // | You should have received a copy of the GNU General Public License | |
---|
| 24 | // | along with this program; if not, write to the Free Software | |
---|
| 25 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 26 | // | USA. | |
---|
| 27 | // +-----------------------------------------------------------------------+ |
---|
[1019] | 28 | |
---|
| 29 | // +-----------------------------------------------------------------------+ |
---|
| 30 | // | functions | |
---|
| 31 | // +-----------------------------------------------------------------------+ |
---|
[1021] | 32 | |
---|
[1019] | 33 | /* |
---|
[1021] | 34 | * Returns an array of mail configuration parameters : |
---|
| 35 | * |
---|
| 36 | * - mail_options: see $conf['mail_options'] |
---|
| 37 | * - send_bcc_mail_webmaster: see $conf['send_bcc_mail_webmaster'] |
---|
| 38 | * - email_webmaster: mail corresponding to $conf['webmaster_id'] |
---|
| 39 | * - formated_email_webmaster: the name of webmaster is $conf['gallery_title'] |
---|
| 40 | * - text_footer: PhpWebGallery and version |
---|
| 41 | * |
---|
| 42 | * @return array |
---|
[1019] | 43 | */ |
---|
[1021] | 44 | function get_mail_configuration() |
---|
| 45 | { |
---|
| 46 | global $conf; |
---|
[1019] | 47 | |
---|
[1021] | 48 | $conf_mail = array( |
---|
| 49 | 'mail_options' => $conf['mail_options'], |
---|
[1642] | 50 | 'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'], |
---|
| 51 | 'default_email_format' => $conf['default_email_format'] |
---|
[1021] | 52 | ); |
---|
| 53 | |
---|
| 54 | // we have webmaster id among user list, what's his email address ? |
---|
| 55 | $conf_mail['email_webmaster'] = get_webmaster_mail_address(); |
---|
| 56 | |
---|
| 57 | // name of the webmaster is the title of the gallery |
---|
| 58 | $conf_mail['formated_email_webmaster'] = |
---|
| 59 | format_email($conf['gallery_title'], $conf_mail['email_webmaster']); |
---|
| 60 | |
---|
[1809] | 61 | $conf_mail['boundary_key'] = generate_key(32); |
---|
| 62 | |
---|
[1021] | 63 | return $conf_mail; |
---|
| 64 | } |
---|
| 65 | |
---|
[1019] | 66 | /** |
---|
[1021] | 67 | * Returns an email address with an associated real name |
---|
| 68 | * |
---|
| 69 | * @param string name |
---|
| 70 | * @param string email |
---|
| 71 | */ |
---|
| 72 | function format_email($name, $email) |
---|
| 73 | { |
---|
[1531] | 74 | global $conf; |
---|
[1361] | 75 | |
---|
[1531] | 76 | if ($conf['enabled_format_email']) |
---|
[1021] | 77 | { |
---|
[1531] | 78 | $cvt7b_name = str_translate_to_ascii7bits($name); |
---|
| 79 | |
---|
| 80 | if (strpos($email, '<') === false) |
---|
| 81 | { |
---|
| 82 | return $cvt7b_name.' <'.$email.'>'; |
---|
| 83 | } |
---|
| 84 | else |
---|
| 85 | { |
---|
| 86 | return $cvt7b_name.$email; |
---|
| 87 | } |
---|
[1021] | 88 | } |
---|
| 89 | else |
---|
| 90 | { |
---|
[1531] | 91 | return $email; |
---|
[1021] | 92 | } |
---|
| 93 | } |
---|
[1645] | 94 | |
---|
| 95 | /** |
---|
[1818] | 96 | * Returns an completed array template/theme |
---|
[1809] | 97 | * completed with $conf['default_template'] |
---|
[1645] | 98 | * |
---|
[1809] | 99 | * @params: |
---|
| 100 | * - args: incompleted array of template/theme |
---|
| 101 | * o template: template to use [default $conf['default_template']] |
---|
| 102 | * o theme: template to use [default $conf['default_template']] |
---|
[1645] | 103 | */ |
---|
[1809] | 104 | function get_array_template_theme($args = array()) |
---|
[1645] | 105 | { |
---|
[1809] | 106 | global $conf; |
---|
[1645] | 107 | |
---|
[1809] | 108 | $res = array(); |
---|
| 109 | |
---|
| 110 | if (empty($args['template']) or empty($args['theme'])) |
---|
| 111 | { |
---|
| 112 | list($res['template'], $res['theme']) = explode('/', $conf['default_template']); |
---|
| 113 | } |
---|
[1645] | 114 | |
---|
[1809] | 115 | if (!empty($args['template'])) |
---|
| 116 | { |
---|
| 117 | $res['template'] = $args['template']; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | if (!empty($args['theme'])) |
---|
| 121 | { |
---|
| 122 | $res['theme'] = $args['theme']; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | return $res; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | * Return an new mail template |
---|
| 130 | * |
---|
| 131 | * @params: |
---|
| 132 | * - email_format: mail format |
---|
| 133 | * - args: function params of mail function: |
---|
| 134 | * o template: template to use [default $conf['default_template']] |
---|
| 135 | * o theme: template to use [default $conf['default_template']] |
---|
| 136 | */ |
---|
| 137 | function get_mail_template($email_format, $args = array()) |
---|
| 138 | { |
---|
| 139 | $args = get_array_template_theme($args); |
---|
| 140 | |
---|
| 141 | $mail_template = new Template(PHPWG_ROOT_PATH.'template/'.$args['template'], $args['theme']); |
---|
| 142 | $mail_template->set_rootdir(PHPWG_ROOT_PATH.'template/'.$args['template'].'/mail/'.$email_format); |
---|
| 143 | |
---|
[1645] | 144 | return $mail_template; |
---|
| 145 | } |
---|
[1784] | 146 | |
---|
| 147 | /** |
---|
| 148 | * Return string email format (html or not) |
---|
| 149 | * |
---|
| 150 | * @param string format |
---|
| 151 | */ |
---|
| 152 | function get_str_email_format($is_html) |
---|
| 153 | { |
---|
| 154 | return ($is_html ? 'text/html' : 'text/plain'); |
---|
| 155 | } |
---|
[1021] | 156 | |
---|
| 157 | /** |
---|
[1818] | 158 | * Returns email of all administrator |
---|
| 159 | * |
---|
| 160 | * @return string |
---|
| 161 | */ |
---|
| 162 | function get_administrators_email() |
---|
| 163 | { |
---|
| 164 | global $conf; |
---|
| 165 | |
---|
| 166 | $result = array(); |
---|
| 167 | |
---|
| 168 | $query = ' |
---|
| 169 | select |
---|
| 170 | U.'.$conf['user_fields']['username'].' as username, |
---|
| 171 | U.'.$conf['user_fields']['email'].' as mail_address |
---|
| 172 | from |
---|
| 173 | '.USERS_TABLE.' as U, |
---|
| 174 | '.USER_INFOS_TABLE.' as I |
---|
| 175 | where |
---|
| 176 | I.user_id = U.'.$conf['user_fields']['id'].' and |
---|
[1828] | 177 | I.status in (\'webmaster\', \'admin\') and |
---|
| 178 | I.adviser = \'false\' and |
---|
[1818] | 179 | '.$conf['user_fields']['email'].' is not null |
---|
| 180 | order by |
---|
| 181 | username |
---|
| 182 | '; |
---|
| 183 | |
---|
| 184 | $datas = pwg_query($query); |
---|
| 185 | if (!empty($datas)) |
---|
| 186 | { |
---|
| 187 | while ($admin = mysql_fetch_array($datas)) |
---|
| 188 | { |
---|
| 189 | if (!empty($admin['mail_address'])) |
---|
| 190 | { |
---|
| 191 | array_push($result, format_email($admin['username'], $admin['mail_address'])); |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | return $result; |
---|
| 197 | } |
---|
| 198 | |
---|
[1901] | 199 | /* Return a standard block useful for admin mail */ |
---|
| 200 | function get_block_mail_admin_info() |
---|
| 201 | { |
---|
| 202 | global $user; |
---|
| 203 | |
---|
| 204 | return |
---|
| 205 | "\n" |
---|
| 206 | .'Connected user: '.$user['username']."\n" |
---|
| 207 | .'IP: '.$_SERVER['REMOTE_ADDR']."\n" |
---|
| 208 | .'Browser: '.$_SERVER['HTTP_USER_AGENT']."\n" |
---|
| 209 | ."\n"; |
---|
| 210 | } |
---|
| 211 | |
---|
[1818] | 212 | /** |
---|
[1019] | 213 | * sends an email, using PhpWebGallery specific informations |
---|
[1809] | 214 | * |
---|
| 215 | * @param: |
---|
| 216 | * - to: Receiver, or receivers of the mail. |
---|
| 217 | * - args: function params of mail function: |
---|
[1818] | 218 | * o from: sender [default value webmaster email] |
---|
| 219 | * o Cc: array of carbon copy receivers of the mail. [default value empty] |
---|
| 220 | * o Bcc: array of blind carbon copy receivers of the mail. [default value empty] |
---|
[1809] | 221 | * o subject [default value 'PhpWebGallery'] |
---|
| 222 | * o content: content of mail [default value ''] |
---|
| 223 | * o content_format: format of mail content [default value 'text/plain'] |
---|
| 224 | * o email_format: global mail format [default value $conf_mail['default_email_format']] |
---|
| 225 | * o template: template to use [default $conf['default_template']] |
---|
| 226 | * o theme: template to use [default $conf['default_template']] |
---|
[1642] | 227 | */ |
---|
[1809] | 228 | function pwg_mail($to, $args = array()) |
---|
[1019] | 229 | { |
---|
[1809] | 230 | global $conf, $conf_mail, $lang_info, $page; |
---|
[1019] | 231 | |
---|
[1021] | 232 | if (!isset($conf_mail)) |
---|
| 233 | { |
---|
| 234 | $conf_mail = get_mail_configuration(); |
---|
| 235 | } |
---|
[1642] | 236 | |
---|
[1809] | 237 | if (empty($args['email_format'])) |
---|
[1642] | 238 | { |
---|
[1809] | 239 | $args['email_format'] = $conf_mail['default_email_format']; |
---|
[1642] | 240 | } |
---|
| 241 | |
---|
[1676] | 242 | // Compute root_path in order have complete path |
---|
[1809] | 243 | if ($args['email_format'] == 'text/html') |
---|
[1676] | 244 | { |
---|
| 245 | set_make_full_url(); |
---|
| 246 | } |
---|
| 247 | |
---|
[1021] | 248 | $to = format_email('', $to); |
---|
[1019] | 249 | |
---|
[1809] | 250 | if (empty($args['from'])) |
---|
[1021] | 251 | { |
---|
[1809] | 252 | $args['from'] = $conf_mail['formated_email_webmaster']; |
---|
[1021] | 253 | } |
---|
| 254 | else |
---|
| 255 | { |
---|
[1809] | 256 | $args['from'] = format_email('', $args['from']); |
---|
[1021] | 257 | } |
---|
| 258 | |
---|
[1809] | 259 | if (empty($args['subject'])) |
---|
| 260 | { |
---|
| 261 | $args['subject'] = 'PhpWebGallery'; |
---|
| 262 | } |
---|
| 263 | $cvt7b_subject = str_translate_to_ascii7bits($args['subject']); |
---|
| 264 | |
---|
| 265 | if (!isset($args['content'])) |
---|
| 266 | { |
---|
| 267 | $args['content'] = ''; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | if (empty($args['content_format'])) |
---|
| 271 | { |
---|
| 272 | $args['content_format'] = 'text/plain'; |
---|
| 273 | } |
---|
[1818] | 274 | |
---|
| 275 | if ($conf_mail['send_bcc_mail_webmaster']) |
---|
| 276 | { |
---|
| 277 | $args['Bcc'][] = $conf_mail['formated_email_webmaster']; |
---|
| 278 | } |
---|
[1809] | 279 | |
---|
| 280 | if (($args['content_format'] == 'text/html') and ($args['email_format'] == 'text/plain')) |
---|
| 281 | { |
---|
| 282 | // Todo find function to convert html text to plain text |
---|
| 283 | return false; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | $args = array_merge($args, get_array_template_theme($args)); |
---|
| 287 | |
---|
| 288 | $headers = 'From: '.$args['from']."\n"; |
---|
[1818] | 289 | $headers.= 'Reply-To: '.$args['from']."\n"; |
---|
| 290 | |
---|
| 291 | if (!empty($args['Cc'])) |
---|
| 292 | { |
---|
| 293 | $headers.= 'Cc: '.implode(',', $args['Cc'])."\n"; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | if (!empty($args['Bcc'])) |
---|
| 297 | { |
---|
| 298 | $headers.= 'Bcc: '.implode(',', $args['Bcc'])."\n"; |
---|
| 299 | } |
---|
| 300 | |
---|
[1809] | 301 | $headers.= 'Content-Type: multipart/alternative;'."\n"; |
---|
| 302 | $headers.= ' boundary="---='.$conf_mail['boundary_key'].'";'."\n"; |
---|
| 303 | $headers.= ' reply-type=original'."\n"; |
---|
[1808] | 304 | $headers.= 'MIME-Version: 1.0'."\n"; |
---|
[1532] | 305 | |
---|
[1642] | 306 | $content = ''; |
---|
| 307 | |
---|
[1809] | 308 | if (!isset($conf_mail[$args['email_format']][$lang_info['charset']][$args['template']][$args['theme']])) |
---|
[1642] | 309 | { |
---|
[1784] | 310 | if (!isset($mail_template)) |
---|
[1645] | 311 | { |
---|
[1809] | 312 | $mail_template = get_mail_template($args['email_format']); |
---|
[1784] | 313 | } |
---|
[1644] | 314 | |
---|
[1784] | 315 | $mail_template->set_filename('mail_header', 'header.tpl'); |
---|
| 316 | $mail_template->set_filename('mail_footer', 'footer.tpl'); |
---|
[1642] | 317 | |
---|
[1784] | 318 | $mail_template->assign_vars( |
---|
| 319 | array( |
---|
| 320 | //Header |
---|
[1809] | 321 | 'BOUNDARY_KEY' => $conf_mail['boundary_key'], |
---|
| 322 | 'CONTENT_TYPE' => $args['email_format'], |
---|
| 323 | 'CONTENT_ENCODING' => $lang_info['charset'], |
---|
[1784] | 324 | 'LANG' => $lang_info['code'], |
---|
| 325 | 'DIR' => $lang_info['direction'], |
---|
| 326 | |
---|
| 327 | // Footer |
---|
| 328 | 'GALLERY_URL' => |
---|
| 329 | isset($page['gallery_url']) ? |
---|
| 330 | $page['gallery_url'] : $conf['gallery_url'], |
---|
| 331 | 'GALLERY_TITLE' => |
---|
| 332 | isset($page['gallery_title']) ? |
---|
| 333 | $page['gallery_title'] : $conf['gallery_title'], |
---|
| 334 | 'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '', |
---|
| 335 | 'PHPWG_URL' => PHPWG_URL, |
---|
[1642] | 336 | |
---|
[1784] | 337 | 'TITLE_MAIL' => urlencode(l10n('title_send_mail')), |
---|
| 338 | 'MAIL' => get_webmaster_mail_address() |
---|
| 339 | )); |
---|
| 340 | |
---|
[1809] | 341 | if ($args['email_format'] == 'text/html') |
---|
[1803] | 342 | { |
---|
[1807] | 343 | $old_root = $mail_template->root; |
---|
| 344 | |
---|
| 345 | if (is_file($mail_template->root.'/global-mail-css.tpl')) |
---|
[1803] | 346 | { |
---|
[1807] | 347 | $mail_template->set_filename('global_mail_css', 'global-mail-css.tpl'); |
---|
| 348 | $mail_template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'global_mail_css'); |
---|
[1803] | 349 | } |
---|
[1797] | 350 | |
---|
[1809] | 351 | $mail_template->root = PHPWG_ROOT_PATH.'template/'.$args['template'].'/theme/'.$args['theme']; |
---|
[1807] | 352 | if (is_file($mail_template->root.'/mail-css.tpl')) |
---|
[1803] | 353 | { |
---|
[1807] | 354 | $mail_template->set_filename('mail_css', 'mail-css.tpl'); |
---|
| 355 | $mail_template->assign_var_from_handle('MAIL_CSS', 'mail_css'); |
---|
[1803] | 356 | } |
---|
[1797] | 357 | |
---|
[1803] | 358 | $mail_template->root = PHPWG_ROOT_PATH.'template-common'; |
---|
[1807] | 359 | if (is_file($mail_template->root.'/local-mail-css.tpl')) |
---|
[1803] | 360 | { |
---|
[1807] | 361 | $mail_template->set_filename('local_mail_css', 'local-mail-css.tpl'); |
---|
| 362 | $mail_template->assign_var_from_handle('LOCAL_MAIL_CSS', 'local_mail_css'); |
---|
[1803] | 363 | } |
---|
[1797] | 364 | |
---|
[1803] | 365 | $mail_template->root = $old_root; |
---|
[1797] | 366 | } |
---|
| 367 | |
---|
[1784] | 368 | // what are displayed on the header of each mail ? |
---|
[1809] | 369 | $conf_mail[$args['email_format']] |
---|
[1792] | 370 | [$lang_info['charset']] |
---|
[1809] | 371 | [$args['template']][$args['theme']]['header'] = |
---|
[1792] | 372 | $mail_template->parse('mail_header', true); |
---|
[1784] | 373 | |
---|
| 374 | // what are displayed on the footer of each mail ? |
---|
[1809] | 375 | $conf_mail[$args['email_format']] |
---|
[1792] | 376 | [$lang_info['charset']] |
---|
[1809] | 377 | [$args['template']][$args['theme']]['footer'] = |
---|
[1792] | 378 | $mail_template->parse('mail_footer', true); |
---|
[1642] | 379 | } |
---|
[1809] | 380 | |
---|
| 381 | // Header |
---|
| 382 | $content.= $conf_mail[$args['email_format']] |
---|
[1792] | 383 | [$lang_info['charset']] |
---|
[1809] | 384 | [$args['template']][$args['theme']]['header']; |
---|
[1019] | 385 | |
---|
[1809] | 386 | // Content |
---|
| 387 | if (($args['content_format'] == 'text/plain') and ($args['email_format'] == 'text/html')) |
---|
[1642] | 388 | { |
---|
[1901] | 389 | $content.= '<p>'. |
---|
| 390 | nl2br( |
---|
| 391 | preg_replace("/(http:\/\/)([^\s,]*)/i", |
---|
| 392 | "<a href='$1$2'>$1$2</a>", |
---|
| 393 | htmlentities($args['content']))). |
---|
| 394 | '</p>'; |
---|
[1642] | 395 | } |
---|
| 396 | else |
---|
| 397 | { |
---|
[1809] | 398 | $content.= $args['content']; |
---|
[1642] | 399 | } |
---|
[1809] | 400 | |
---|
| 401 | // Footer |
---|
| 402 | $content.= $conf_mail[$args['email_format']] |
---|
[1792] | 403 | [$lang_info['charset']] |
---|
[1809] | 404 | [$args['template']][$args['theme']]['footer']; |
---|
| 405 | |
---|
| 406 | // Close boundary |
---|
| 407 | $content.= "\n".'-----='.$conf_mail['boundary_key'].'--'."\n"; |
---|
| 408 | |
---|
[1676] | 409 | // Undo Compute root_path in order have complete path |
---|
[1809] | 410 | if ($args['email_format'] == 'text/html') |
---|
[1676] | 411 | { |
---|
| 412 | unset_make_full_url(); |
---|
| 413 | } |
---|
[1642] | 414 | |
---|
[1784] | 415 | /*Testing block |
---|
| 416 | { |
---|
| 417 | global $user; |
---|
| 418 | @mkdir(PHPWG_ROOT_PATH.'testmail'); |
---|
| 419 | $filename = PHPWG_ROOT_PATH.'testmail/mail.'.$user['username']; |
---|
[1809] | 420 | if ($args['content_format'] == 'text/plain') |
---|
[1784] | 421 | { |
---|
| 422 | $filename .= '.txt'; |
---|
| 423 | } |
---|
| 424 | else |
---|
| 425 | { |
---|
| 426 | $filename .= '.html'; |
---|
| 427 | } |
---|
| 428 | $file = fopen($filename, 'w+'); |
---|
| 429 | fwrite($file, $content); |
---|
| 430 | fclose($file); |
---|
| 431 | return true; |
---|
[1792] | 432 | }*/ |
---|
[1784] | 433 | |
---|
[1019] | 434 | if ($conf_mail['mail_options']) |
---|
| 435 | { |
---|
[1531] | 436 | $options = '-f '.$conf_mail['email_webmaster']; |
---|
[1021] | 437 | |
---|
[1361] | 438 | return mail($to, $cvt7b_subject, $content, $headers, $options); |
---|
[1019] | 439 | } |
---|
| 440 | else |
---|
| 441 | { |
---|
[1361] | 442 | return mail($to, $cvt7b_subject, $content, $headers); |
---|
[1019] | 443 | } |
---|
[1021] | 444 | } |
---|
[1105] | 445 | |
---|
[1321] | 446 | ?> |
---|