home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / unitednuke / unitednuke.exe / html / includes / emailer.php < prev    next >
PHP Script  |  2004-01-10  |  17KB  |  409 lines

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