home *** CD-ROM | disk | FTP | other *** search
/ PC Basics 53 / PC Basics Issue 53.iso / Software / Internet / Invboard.exe / PC Basics 53 / Invboard / upload / sources / lib / emailer.php < prev    next >
Encoding:
PHP Script  |  2002-06-12  |  10.0 KB  |  292 lines

  1. <?php
  2.  
  3. /*
  4. +--------------------------------------------------------------------------
  5. |   IBFORUMS v1
  6. |   ========================================
  7. |   by Matthew Mecham and David Baxter
  8. |   (c) 2001,2002 IBForums
  9. |   http://www.ibforums.com
  10. |   ========================================
  11. |   Web: http://www.ibforums.com
  12. |   Email: phpboards@ibforums.com
  13. |   Licence Info: phpib-licence@ibforums.com
  14. +---------------------------------------------------------------------------
  15. |
  16. |   > Sending email module
  17. |   > Module written by Matt Mecham
  18. |   > Date started: 26th February 2002
  19. |
  20. |    > Module Version Number: 1.0.0
  21. +--------------------------------------------------------------------------
  22. |
  23. |   QUOTE OF THE MODULE: (Taken from "Shrek" (c) Dreamworks Pictures)
  24. |   --------------------
  25. |    DONKEY: We can stay up late, swap manly stories and in the morning,  
  26. |           I'm making waffles!
  27. |
  28. +-------------------------------------------------------------------------- 
  29. */
  30.  
  31. // This module is fairly basic, more functionality is expected in future
  32. // versions (such as MIME attachments, SMTP stuff, etc)
  33.  
  34.  
  35. class emailer {
  36.  
  37.  
  38.     var $from         = "";
  39.     var $to           = "";
  40.     var $subject      = "";
  41.     var $message      = "";
  42.     var $header       = "";
  43.     var $footer       = "";
  44.     var $template     = "";
  45.     var $error        = "";
  46.     var $parts        = array();
  47.     var $bcc          = "";
  48.     var $mail_headers = array();
  49.     var $multipart    = "";
  50.     var $boundry      = "";
  51.     
  52.     var $temp_dump = 0;
  53.     
  54.     function emailer() {
  55.         global $ibforums;
  56.         
  57.         // Assign $from as the admin out email address, this can be
  58.         // over-riden at any time.
  59.         
  60.         $this->from = $ibforums->vars['email_out'];
  61.         
  62.         // Temporarily assign $header and $footer, this can be over-riden
  63.         // also
  64.         
  65.         $this->header = $ibforums->vars['email_header'];
  66.         $this->footer = $ibforums->vars['email_footer'];
  67.         $this->boundry = "----=_NextPart_000_0022_01C1BD6C.D0C0F9F0";  //"b".md5(uniqid(time()));
  68.         
  69.     }
  70.     
  71.     function add_attachment($data = "", $name = "", $ctype='application/octet-stream') {
  72.     
  73.         $this->parts[] = array( 'ctype'  => $ctype,
  74.                                 'data'   => $data,
  75.                                 'encode' => 'base64',
  76.                                 'name'   => $name
  77.                               );
  78.                              
  79.     }
  80.     
  81.     function build_headers() {
  82.     
  83.         $this->mail_headers  = "From: ".$this->from. "\n";
  84.         $this->mail_headers .= "X-Priority: 3\n";
  85.         $this->mail_headers .= "X-Mailer: IBForums PHP Mailer\n";
  86.         
  87.         if (is_array( $this->bcc ) )
  88.         {
  89.             $this->mail_headers .= "BCC: ".implode( "," , $this->bcc ) . "\n";
  90.         }
  91.         
  92.         if ( count ($this->parts) > 0 )
  93.         {
  94.             
  95.             $this->mail_headers .= "MIME-Version: 1.0\n";
  96.             $this->mail_headers .= "Content-Type: multipart/mixed;\n\tboundary=\"".$this->boundry."\"\n\nThis is a MIME encoded message.\n\n--".$this->boundry;
  97.             $this->mail_headers .= "\nContent-Type: text/plain;\n\tcharset=\"iso-8859-1\"\nContent-Transfer-Encoding: quoted-printable\n\n".$this->message."\n\n--".$this->boundry;
  98.             $this->mail_headers .= $this->build_multipart();
  99.             
  100.             $this->message = "";
  101.         }
  102.     
  103.     }
  104.     
  105.     function encode_attachment($part) {
  106.         
  107.         $msg = chunk_split(base64_encode($part['data']));
  108.         
  109.         return "Content-Type: ".$part['ctype']. ($part['name'] ? ";\n\tname =\"".$part['name']."\"" : "").
  110.               "\nContent-Transfer-Encoding: ".$part['encode']."\nContent-Disposition: attachment;\n\tfilename=\"".$part['name']."\"\n\n".$msg."\n";
  111.         
  112.     }
  113.     
  114.     function build_multipart() {
  115.     
  116.         $multipart = "";
  117.         
  118.         for ($i = sizeof($this->parts) - 1 ; $i >= 0 ; $i--)
  119.         {
  120.             $multipart .= "\n".$this->encode_attachment($this->parts[$i]) . "--".$this->boundry;
  121.         }
  122.         
  123.         return $multipart . "--\n";
  124.         
  125.     }
  126.     
  127.     
  128.     //+--------------------------------------------------------------------------
  129.     // send_mail:
  130.     // Physically sends the email
  131.     //+--------------------------------------------------------------------------
  132.     
  133.     function send_mail() {
  134.         
  135.         $this->to   = preg_replace( "/[ \t]+/" , " "  , $this->to );
  136.         $this->from = preg_replace( "/[ \t]+/" , " "  , $this->from );
  137.         
  138.         $this->to   = preg_replace( "/,,/"     , ","  , $this->to );
  139.         $this->from = preg_replace( "/,,/"     , ","  , $this->from );
  140.         
  141.         $this->to     = preg_replace( "#\#\[\]'\"\(\):;/\$!ú%\^&\*\{\}#" , "", $this->to  );
  142.         $this->from   = preg_replace( "#\#\[\]'\"\(\):;/\$!ú%\^&\*\{\}#" , "", $this->from);
  143.         
  144.         $this->subject = $this->clean_message($this->subject);
  145.         
  146.         $this->build_headers();
  147.         
  148.         if ( ($this->from) and ($this->subject) )
  149.         {
  150.         
  151.             if ($this->temp_dump == 1)
  152.             {
  153.                 $blah = $this->to."\n".$this->from."\n".$this->subject."\n".$this->mail_headers."\n\n".$this->message;
  154.                 
  155.                 $pathy = '/Library/WebServer/Documents/mail/'.date("js.F.Y.h:i.A"); // OS X rules!
  156.                 $fh = fopen ($pathy, 'w');
  157.                 fputs ($fh, $blah, strlen($blah) );
  158.                 fclose($fh);
  159.             }
  160.             else
  161.             {
  162.                 if ( ! @mail( $this->to, $this->subject, $this->message, $this->mail_headers ) )
  163.                 {
  164.                     $this->fatal_error("Could not sent the email", "Failed at 'mail' command");
  165.                 }
  166.             }
  167.         }
  168.         else
  169.         {
  170.             return FALSE;
  171.         }
  172.     }
  173.     
  174.     
  175.     //+--------------------------------------------------------------------------
  176.     // get_template:
  177.     // Queries the database, and stores the template we wish to use in memory
  178.     //+--------------------------------------------------------------------------
  179.  
  180.     function get_template($name="", $language="en") {
  181.         global $IB, $DB;
  182.         
  183.         if ($name == "")
  184.         {
  185.             $this->error++;
  186.             $this->fatal_error("A valid email template ID was not passed to the email library during template parsing", "");
  187.         }
  188.         
  189.         if (! file_exists("./lang/$language/email_content.php") )
  190.         {
  191.             require "./lang/en/email_content.php";
  192.         }
  193.         else
  194.         {
  195.             require "./lang/$language/email_content.php";
  196.         }
  197.         
  198.         if (! isset($EMAIL[ $name ]) ) {
  199.             $this->fatal_error("Could not find an email template with an ID of '$name'", "");
  200.         }
  201.         $this->template = $EMAIL[ $name ];
  202.     }
  203.         
  204.     //+--------------------------------------------------------------------------
  205.     // build_message:
  206.     // Swops template tags into the corresponding string held in $words array.
  207.     // Also joins header and footer to message and cleans the message for sending
  208.     //+--------------------------------------------------------------------------
  209.         
  210.     function build_message($words) {
  211.         global $ibforums;
  212.         
  213.         if ($this->template == "") {
  214.             $this->error++;
  215.             $this->fatal_error("Could not build the email message, no template assigned", "Make sure a template is assigned first.");
  216.         }
  217.         
  218.         $this->message = $this->template;
  219.         
  220.         // Add some default words
  221.         
  222.         $words['BOARD_ADDRESS'] = $ibforums->vars['board_url'] . '/index.' . $ibforums->vars['php_ext'];
  223.         $words['WEB_ADDRESS']   = $ibforums->vars['home_url'];
  224.         $words['BOARD_NAME']    = $ibforums->vars['board_name'];
  225.         $words['SIGNATURE']     = $ibforums->vars['signature'];
  226.         
  227.         // Swop the words
  228.         
  229.         $this->message = preg_replace( "/<#(.+?)#>/e", "\$words[\\1]", $this->message );
  230.         
  231.         $this->message = $this->clean_message( $this->message );
  232.         
  233.     }
  234.  
  235.     
  236.     //+--------------------------------------------------------------------------
  237.     // clean_message: (Mainly used internally)
  238.     // Ensures that \n and <br> are converted into CRLF (\r\n)
  239.     // Also unconverts some iB_CODE.
  240.     //+--------------------------------------------------------------------------
  241.     
  242.     function clean_message($message = "" ) {
  243.     
  244.         $message = preg_replace( "#<b>(.+?)</b>#" , "\\1", $message );
  245.         $message = preg_replace( "#<i>(.+?)</i>#" , "\\1", $message );
  246.         $message = preg_replace( "#<s>(.+?)</s>#" , "--\\1--", $message );
  247.         $message = preg_replace( "#<u>(.+?)</u>#" , "-\\1-"  , $message );
  248.         
  249.         $message = preg_replace( "#<!--emo&(.+?)-->.+?<!--endemo-->#", "\\1" , $message );
  250.         
  251.         $message = preg_replace( "#<!--c1-->(.+?)<!--ec1-->#", "\n\n------------ CODE SAMPLE ----------\n"  , $message );
  252.         $message = preg_replace( "#<!--c2-->(.+?)<!--ec2-->#", "\n-----------------------------------\n\n"  , $message );
  253.         
  254.         $message = preg_replace( "#<!--QuoteBegin-->(.+?)<!--QuoteEBegin-->#"                       , "\n\n------------ QUOTE ----------\n" , $message );
  255.         $message = preg_replace( "#<!--QuoteBegin--(.+?)\+(.+?)-->(.+?)<!--QuoteEBegin-->#"         , "\n\n------------ QUOTE ----------\n" , $message );
  256.         $message = preg_replace( "#<!--QuoteEnd-->(.+?)<!--QuoteEEnd-->#"                           , "\n-----------------------------\n\n" , $message );
  257.         
  258.         $message = preg_replace( "#<!--Flash (.+?)-->.+?<!--End Flash-->#e"                         , "(FLASH MOVIE)" , $message );
  259.         $message = preg_replace( "#<img src=[\"'](\S+?)['\"].+?".">#"                                  , "(IMAGE: \\1)"   , $message );
  260.         $message = preg_replace( "#<a href=[\"'](http|https|ftp|news)://(\S+?)['\"].+?".">(.+?)</a>#"  , "(URL: \\1)"     , $message );
  261.         $message = preg_replace( "#<a href=[\"']mailto:(.+?)['\"]>(.+?)</a>#"                       , "(EMAIL: \\2)"   , $message );
  262.         
  263.         $message = preg_replace( "#<!--sql-->(.+?)<!--sql1-->(.+?)<!--sql2-->(.+?)<!--sql3-->#e"    , "\n\n--------------- SQL -----------\n\\2\n----------------\n\n", $message);
  264.         $message = preg_replace( "#<!--html-->(.+?)<!--html1-->(.+?)<!--html2-->(.+?)<!--html3-->#e", "\n\n-------------- HTML -----------\n\\2\n----------------\n\n", $message);
  265.         
  266.         $message = preg_replace( "#<!--EDIT\|.+?\|.+?-->#" , "" , $message );
  267.         
  268.         $message = preg_replace( "#<.+?".">#" , "" , $message );
  269.         
  270.         $message = preg_replace( "/(<br>|\n)/" , "\r\n", $message );
  271.         
  272.         $message = str_replace( """, "\"", $message );
  273.         $message = str_replace( "\", "\\", $message );
  274.         $message = str_replace( "$", "\$", $message );
  275.         $message = str_replace( "!" , "!", $message );
  276.         $message = str_replace( "'" , "'", $message );
  277.         $message = str_replace( "<"  , "<", $message );
  278.         $message = str_replace( ">"  , ">", $message );
  279.         $message = str_replace( "&" , "&", $message );
  280.         
  281.         return $message;
  282.     }
  283.     
  284.     function fatal_error($msg, $help="")
  285.     {
  286.         echo("<h1>Mail Error!</h1><br><b>$msg</b><br>$help");
  287.         exit();
  288.     }
  289.  
  290. }
  291.  
  292. ?>