[ Index ]

PHP Cross Reference of Nuke-Evolution v2.0.5

title

Body

[close]

/includes/ -> emailer.php (source)

   1  <?php
   2  /*=======================================================================
   3   Nuke-Evolution Basic: Enhanced PHP-Nuke Web Portal System
   4   =======================================================================*/
   5  
   6  /***************************************************************************
   7                                  emailer.php
   8                               -------------------
   9      begin                : Sunday Aug. 12, 2001
  10      copyright            : (C) 2001 The phpBB Group
  11      email                : support@phpbb.com
  12  
  13      Id: emailer.php,v 1.15.2.34 2003/07/26 11:41:35 acydburn Exp
  14  
  15  ***************************************************************************/
  16  
  17  /***************************************************************************
  18  * phpbb2 forums port version 2.0.5 (c) 2003 - Nuke Cops (http://nukecops.com)
  19  *
  20  * Ported by Nuke Cops to phpbb2 standalone 2.0.5 Test
  21  * and debugging completed by the Elite Nukers and site members.
  22  *
  23  * You run this package at your sole risk. Nuke Cops and affiliates cannot
  24  * be held liable if anything goes wrong. You are advised to test this
  25  * package on a development system. Backup everything before implementing
  26  * in a production environment. If something goes wrong, you can always
  27  * backout and restore your backups.
  28  *
  29  * Installing and running this also means you agree to the terms of the AUP
  30  * found at Nuke Cops.
  31  *
  32  * This is version 2.0.5 of the phpbb2 forum port for PHP-Nuke. Work is based
  33  * on Tom Nitzschner's forum port version 2.0.6. Tom's 2.0.6 port was based
  34  * on the phpbb2 standalone version 2.0.3. Our version 2.0.5 from Nuke Cops is
  35  * now reflecting phpbb2 standalone 2.0.5 that fixes some bugs and the
  36  * invalid_session error message.
  37  ***************************************************************************/
  38  
  39  /***************************************************************************
  40   *   This file is part of the phpBB2 port to Nuke 6.0 (c) copyright 2002
  41   *   by Tom Nitzschner (tom@toms-home.com)
  42   *   http://bbtonuke.sourceforge.net (or http://www.toms-home.com)
  43   *
  44   *   As always, make a backup before messing with anything. All code
  45   *   release by me is considered sample code only. It may be fully
  46   *   functual, but you use it at your own risk, if you break it,
  47   *   you get to fix it too. No waranty is given or implied.
  48   *
  49   *   Please post all questions/request about this port on http://bbtonuke.sourceforge.net first,
  50   *   then on my site. All original header code and copyright messages will be maintained
  51   *   to give credit where credit is due. If you modify this, the only requirement is
  52   *   that you also maintain all original copyright messages. All my work is released
  53   *   under the GNU GENERAL PUBLIC LICENSE. Please see the README for more information.
  54   *
  55   ***************************************************************************/
  56  
  57  /***************************************************************************
  58   *
  59   *   This program is free software; you can redistribute it and/or modify
  60   *   it under the terms of the GNU General Public License as published by
  61   *   the Free Software Foundation; either version 2 of the License, or
  62   *   (at your option) any later version.
  63   *
  64   ***************************************************************************/
  65  
  66  /*****[CHANGES]**********************************************************
  67  -=[Mod]=-
  68        Custom mass PM                           v1.4.7       07/04/2005
  69   ************************************************************************/
  70  
  71  if (!defined('IN_PHPBB'))
  72  {
  73      die('Hacking attempt');
  74  }
  75  
  76  //
  77  // The emailer class has support for attaching files, that isn't implemented
  78  // in the 2.0 release but we can probable find some way of using it in a future
  79  // release
  80  //
  81  class emailer
  82  {
  83          var $msg, $subject, $extra_headers;
  84          var $addresses, $reply_to, $from;
  85          var $use_smtp;
  86  
  87          var $tpl_msg = array();
  88  
  89          function emailer($use_smtp)
  90          {
  91                  $this->reset();
  92                  $this->use_smtp = $use_smtp;
  93          $this->reply_to = $this->from = '';
  94      }
  95  
  96      // Resets all the data (address, template file, etc etc to default
  97      function reset()
  98      {
  99          $this->addresses = array();
 100          $this->vars = $this->msg = $this->extra_headers = '';
 101      }
 102  
 103      // Sets an email address to send to
 104      function email_address($address)
 105      {
 106          $this->addresses['to'] = trim($address);
 107      }
 108  
 109      function cc($address)
 110      {
 111          $this->addresses['cc'][] = trim($address);
 112      }
 113  
 114      function bcc($address)
 115      {
 116          $this->addresses['bcc'][] = trim($address);
 117      }
 118  
 119      function replyto($address)
 120      {
 121          $this->reply_to = trim($address);
 122          }
 123  
 124          function from($address)
 125          {
 126                  $this->from = trim($address);
 127          }
 128  
 129          // set up subject for mail
 130          function set_subject($subject = '')
 131          {
 132                  $this->subject = trim(preg_replace('#[\n\r]+#s', '', $subject));
 133          }
 134  
 135          // set up extra mail headers
 136          function extra_headers($headers)
 137          {
 138                  $this->extra_headers .= trim($headers) . "\n";
 139          }
 140  
 141          function use_template($template_file, $template_lang = '')
 142          {
 143                  global $board_config, $phpbb_root_path;
 144  
 145                  if (trim($template_file) == '')
 146                  {
 147                          message_die(GENERAL_ERROR, 'No template file set', '', __LINE__, __FILE__);
 148                  }
 149  
 150                  if (trim($template_lang) == '')
 151                  {
 152                          $template_lang = $board_config['default_lang'];
 153                  }
 154  
 155                  if (empty($this->tpl_msg[$template_lang . $template_file]))
 156                  {
 157                          $tpl_file = $phpbb_root_path . 'language/lang_' . $template_lang . '/email/' . $template_file . '.tpl';
 158  
 159                          if (!@file_exists(@phpbb_realpath($tpl_file)))
 160                          {
 161                                  $tpl_file = $phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/email/' . $template_file . '.tpl';
 162  
 163                                  if (!@file_exists(@phpbb_realpath($tpl_file)))
 164                                  {
 165                                          message_die(GENERAL_ERROR, 'Could not find email template file :: ' . $template_file, '', __LINE__, __FILE__);
 166                                  }
 167                          }
 168  
 169                          if (!($fd = @fopen($tpl_file, 'r')))
 170                          {
 171                                  message_die(GENERAL_ERROR, 'Failed opening template file :: ' . $tpl_file, '', __LINE__, __FILE__);
 172                          }
 173  
 174                          $this->tpl_msg[$template_lang . $template_file] = fread($fd, filesize($tpl_file));
 175                          fclose($fd);
 176                  }
 177  
 178                  $this->msg = $this->tpl_msg[$template_lang . $template_file];
 179  
 180                  return true;
 181          }
 182  
 183          // assign variables
 184          function assign_vars($vars)
 185          {
 186                  $this->vars = (empty($this->vars)) ? $vars : $this->vars . $vars;
 187          }
 188  
 189          // Send the mail out to the recipients set previously in var $this->address
 190  /*****[BEGIN]******************************************
 191   [ Mod:     Custom mass PM                     v1.4.7 ]
 192   ******************************************************/
 193          function send($error_level=0)
 194  /*****[END]********************************************
 195   [ Mod:     Custom mass PM                     v1.4.7 ]
 196   ******************************************************/
 197          {
 198                  global $board_config, $lang, $phpEx, $phpbb_root_path, $db, $cache;
 199  
 200              // Escape all quotes, else the eval will fail.
 201                  $this->msg = str_replace ("'", "\'", $this->msg);
 202                  $this->msg = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->msg);
 203  
 204                  // Set vars
 205                  reset ($this->vars);
 206                  while (list($key, $val) = each($this->vars))
 207                  {
 208                          $$key = $val;
 209                  }
 210  
 211                  eval("\$this->msg = '$this->msg';");
 212  
 213                  // Clear vars
 214                  reset ($this->vars);
 215                  while (list($key, $val) = each($this->vars))
 216                  {
 217                          unset($$key);
 218                  }
 219  
 220                  // We now try and pull a subject from the email body ... if it exists,
 221                  // do this here because the subject may contain a variable
 222                  $drop_header = '';
 223                  $match = array();
 224                  if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
 225                  {
 226                          $this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : 'No Subject');
 227                          $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
 228                  }
 229                  else
 230                  {
 231                          $this->subject = (($this->subject != '') ? $this->subject : 'No Subject');
 232                  }
 233  
 234                  if (preg_match('#^(Charset:(.*?))$#m', $this->msg, $match))
 235                  {
 236                          $this->encoding = (trim($match[2]) != '') ? trim($match[2]) : trim($lang['ENCODING']);
 237                          $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
 238                  }
 239                  else
 240                  {
 241                          $this->encoding = trim($lang['ENCODING']);
 242                  }
 243  
 244                  if ($drop_header != '')
 245                  {
 246                          $this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
 247                  }
 248  
 249          $to = $this->addresses['to'];
 250  
 251          $cc = (count($this->addresses['cc'])) ? implode(', ', $this->addresses['cc']) : '';
 252          $bcc = (count($this->addresses['bcc'])) ? implode(', ', $this->addresses['bcc']) : '';
 253  
 254          // Build header
 255          $this->extra_headers = (($this->reply_to != '') ? "Reply-to: $this->reply_to\n" : '') . (($this->from != '') ? "From: $this->from\n" : "From: " . $board_config['board_email'] . "\n") . "Return-Path: " . $board_config['board_email'] . "\nMessage-ID: <" . md5(uniqid(time())) . "@" . $board_config['server_name'] . ">\nMIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . "\nContent-transfer-encoding: 8bit\nDate: " . date('r', time()) . "\nX-Priority: 3\nX-MSMail-Priority: Normal\nX-Mailer: PHP\nX-MimeOLE: Produced By phpBB2\n" . $this->extra_headers . (($cc != '') ? "Cc: $cc\n" : '')  . (($bcc != '') ? "Bcc: $bcc\n" : '');
 256  
 257                  // Send message ... removed $this->encode() from subject for time being
 258                  if ( $this->use_smtp )
 259                  {
 260                          if ( !defined('SMTP_INCLUDED') )
 261                          {
 262                                  include(dirname(__FILE__)."/smtp.php");
 263                          }
 264  
 265                          $result = smtpmail($to, $this->subject, $this->msg, $this->extra_headers);
 266                  }
 267                  else
 268                  {
 269              $empty_to_header = ($to == '') ? TRUE : FALSE;
 270              $to = ($to == '') ? (($board_config['sendmail_fix']) ? ' ' : 'Undisclosed-recipients:;') : $to;
 271                          $result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
 272  
 273                          if (!$result && !$board_config['sendmail_fix'] && $empty_to_header)
 274                          {
 275                                  $to = ' ';
 276  
 277                                  $sql = "UPDATE " . CONFIG_TABLE . "
 278                                          SET config_value = '1'
 279                                          WHERE config_name = 'sendmail_fix'";
 280                                  if (!$db->sql_query($sql))
 281                                  {
 282                                          message_die(GENERAL_ERROR, 'Unable to update config table', '', __LINE__, __FILE__, $sql);
 283                                  }
 284  /*****['BEGIN']******************************************
 285   [ Base:    Caching System                     v3.0.0 ]
 286   ******************************************************/
 287                                  $cache->delete('board_config', 'config');
 288  /*****['END']********************************************
 289   [ Base:    Caching System                     v3.0.0 ]
 290   ******************************************************/
 291                                  $board_config['sendmail_fix'] = 1;
 292                                  $result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
 293                          }
 294                  }
 295  
 296                  // Did it work?
 297  /*****[BEGIN]******************************************
 298   [ Mod:     Custom mass PM                     v1.4.7 ]
 299   ******************************************************/
 300                  if (!$result && !$error_level)
 301  /*****[END]********************************************
 302   [ Mod:     Custom mass PM                     v1.4.7 ]
 303   ******************************************************/
 304                  {
 305                          message_die(GENERAL_ERROR, 'Failed sending email :: ' . (($this->use_smtp) ? 'SMTP' : 'PHP') . ' :: ' . $result, '', __LINE__, __FILE__);
 306                  }
 307  
 308  /*****[BEGIN]******************************************
 309   [ Mod:     Custom mass PM                     v1.4.7 ]
 310   ******************************************************/
 311                  return $result;
 312  /*****[END]********************************************
 313   [ Mod:     Custom mass PM                     v1.4.7 ]
 314   ******************************************************/
 315          }
 316  
 317          // Encodes the given string for proper display for this encoding ... nabbed
 318          // from php.net and modified. There is an alternative encoding method which
 319          // may produce lesd output but it's questionable as to its worth in this
 320          // scenario IMO
 321          function encode($str)
 322          {
 323                  if ($this->encoding == '')
 324                  {
 325                          return $str;
 326                  }
 327  
 328                  // define start delimimter, end delimiter and spacer
 329                  $end = "?=";
 330                  $start = "=?$this->encoding?B?";
 331                  $spacer = "$end\r\n $start";
 332  
 333                  // determine length of encoded text within chunks and ensure length is even
 334                  $length = 75 - strlen($start) - strlen($end);
 335                  $length = floor($length / 2) * 2;
 336  
 337                  // encode the string and split it into chunks with spacers after each chunk
 338                  $str = chunk_split(base64_encode($str), $length, $spacer);
 339  
 340                  // remove trailing spacer and add start and end delimiters
 341                  $str = preg_replace('#' . preg_quote($spacer, '#') . '$#', '', $str);
 342  
 343                  return $start . $str . $end;
 344          }
 345  
 346          //
 347          // Attach files via MIME.
 348          //
 349          function attachFile($filename, $mimetype = "application/octet-stream", $szFromAddress, $szFilenameToDisplay)
 350          {
 351                  global $lang;
 352                  $mime_boundary = "--==================_846811060==_";
 353  
 354                  $this->msg = '--' . $mime_boundary . "\nContent-Type: text/plain;\n\tcharset=\"" . $lang['ENCODING'] . "\"\n\n" . $this->msg;
 355  
 356                  if ($mime_filename)
 357                  {
 358                          $filename = $mime_filename;
 359                          $encoded = $this->encode_file($filename);
 360                  }
 361  
 362                  $fd = fopen($filename, "r");
 363                  $contents = fread($fd, filesize($filename));
 364  
 365                  $this->mimeOut = "--" . $mime_boundary . "\n";
 366                  $this->mimeOut .= "Content-Type: " . $mimetype . ";\n\tname=\"$szFilenameToDisplay\"\n";
 367                  $this->mimeOut .= "Content-Transfer-Encoding: quoted-printable\n";
 368                  $this->mimeOut .= "Content-Disposition: attachment;\n\tfilename=\"$szFilenameToDisplay\"\n\n";
 369  
 370                  if ( $mimetype == "message/rfc822" )
 371                  {
 372                          $this->mimeOut .= "From: ".$szFromAddress."\n";
 373                          $this->mimeOut .= "To: ".$this->emailAddress."\n";
 374                          $this->mimeOut .= "Date: ".date("D, d M Y H:i:s") . " UT\n";
 375                          $this->mimeOut .= "Reply-To:".$szFromAddress."\n";
 376                          $this->mimeOut .= "Subject: ".$this->mailSubject."\n";
 377                          $this->mimeOut .= "X-Mailer: PHP/".phpversion()."\n";
 378                          $this->mimeOut .= "MIME-Version: 1.0\n";
 379                  }
 380  
 381                  $this->mimeOut .= $contents."\n";
 382                  $this->mimeOut .= "--" . $mime_boundary . "--" . "\n";
 383  
 384                  return $out;
 385                  // added -- to notify email client attachment is done
 386          }
 387  
 388          function getMimeHeaders($filename, $mime_filename="")
 389          {
 390                  $mime_boundary = "--==================_846811060==_";
 391  
 392                  if ($mime_filename)
 393                  {
 394                          $filename = $mime_filename;
 395                  }
 396  
 397                  $out = "MIME-Version: 1.0\n";
 398                  $out .= "Content-Type: multipart/mixed;\n\tboundary=\"$mime_boundary\"\n\n";
 399                  $out .= "This message is in MIME format. Since your mail reader does not understand\n";
 400                  $out .= "this format, some or all of this message may not be legible.";
 401  
 402                  return $out;
 403          }
 404  
 405          //
 406          // Split string by RFC 2045 semantics (76 chars per line, end with \r\n).
 407          //
 408          function myChunkSplit($str)
 409          {
 410                  $stmp = $str;
 411                  $len = strlen($stmp);
 412                  $out = "";
 413  
 414                  while ($len > 0)
 415                  {
 416                          if ($len >= 76)
 417                          {
 418                                  $out .= substr($stmp, 0, 76) . "\r\n";
 419                                  $stmp = substr($stmp, 76);
 420                                  $len = $len - 76;
 421                          }
 422                          else
 423                          {
 424                                  $out .= $stmp . "\r\n";
 425                                  $stmp = "";
 426                                  $len = 0;
 427                          }
 428                  }
 429                  return $out;
 430          }
 431  
 432          //
 433          // Split the specified file up into a string and return it
 434          //
 435          function encode_file($sourcefile)
 436          {
 437                  if (is_readable(phpbb_realpath($sourcefile)))
 438                  {
 439                          $fd = fopen($sourcefile, "r");
 440                          $contents = fread($fd, filesize($sourcefile));
 441                $encoded = $this->myChunkSplit(base64_encode($contents));
 442                fclose($fd);
 443                  }
 444  
 445                  return $encoded;
 446          }
 447  
 448  } // class emailer
 449  
 450  ?>


Generated: Wed Jun 6 11:38:01 2007 Cross-referenced by PHPXref 0.7