includes/email.php
changeset 1 fe660c52c48f
child 16 64e0d3d4cf14
equal deleted inserted replaced
0:902822492a68 1:fe660c52c48f
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     5  * Version 1.0 (Banshee)
       
     6  * Copyright (C) 2006-2007 Dan Fuhry
       
     7  *
       
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    12  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    13  * 
       
    14  * The "emailer" class was ported from phpBB 2.0. Copyright (C) 2002-2006 phpBB Group. phpBB is licensed under the GPLv2.
       
    15  */
       
    16  
       
    17 //
       
    18 // The emailer class has support for attaching files, that isn't implemented
       
    19 // in the 2.0 release but we can probable find some way of using it in a future
       
    20 // release
       
    21 //
       
    22 class emailer
       
    23 {
       
    24   var $msg, $subject, $extra_headers;
       
    25   var $addresses, $reply_to, $from;
       
    26   var $use_smtp;
       
    27 
       
    28   var $tpl_msg;
       
    29 
       
    30   function emailer($use_smtp)
       
    31   {
       
    32     $this->reset();
       
    33     $this->use_smtp = $use_smtp;
       
    34     $this->reply_to = $this->from = '';
       
    35   }
       
    36 
       
    37   // Resets all the data (address, template file, etc etc to default
       
    38   function reset()
       
    39   {
       
    40     $this->addresses = array();
       
    41     $this->vars = $this->msg = $this->extra_headers = '';
       
    42   }
       
    43 
       
    44   // Sets an email address to send to
       
    45   function email_address($address)
       
    46   {
       
    47     $this->addresses['to'] = trim($address);
       
    48   }
       
    49 
       
    50   function cc($address)
       
    51   {
       
    52     $this->addresses['cc'][] = trim($address);
       
    53   }
       
    54 
       
    55   function bcc($address)
       
    56   {
       
    57     $this->addresses['bcc'][] = trim($address);
       
    58   }
       
    59 
       
    60   function replyto($address)
       
    61   {
       
    62     $this->reply_to = trim($address);
       
    63   }
       
    64 
       
    65   function from($address)
       
    66   {
       
    67     $this->from = trim($address);
       
    68   }
       
    69 
       
    70   // set up subject for mail
       
    71   function set_subject($subject = '')
       
    72   {
       
    73     $this->subject = trim(preg_replace('#[\n\r]+#s', '', $subject));
       
    74   }
       
    75 
       
    76   // set up extra mail headers
       
    77   function extra_headers($headers)
       
    78   {
       
    79     $this->extra_headers .= trim($headers) . "\n";
       
    80   }
       
    81 
       
    82   function use_template($template_code)
       
    83   {
       
    84     global $db, $session, $paths, $template, $plugins; // Common objects
       
    85     
       
    86     $this->tpl_msg = $template->makeParserText($template_code);
       
    87 
       
    88     return true;
       
    89   }
       
    90 
       
    91   // assign variables
       
    92   function assign_vars($vars)
       
    93   {
       
    94     if ( is_object($this->tpl_msg) )
       
    95     {
       
    96       $this->tpl_msg->assign_vars($vars);
       
    97     }
       
    98     else
       
    99     {
       
   100       die_friendly(GENERAL_ERROR, 'Can\'t set vars, the template is not set');
       
   101     }
       
   102   }
       
   103 
       
   104   // Send the mail out to the recipients set previously in var $this->address
       
   105   function send()
       
   106   {
       
   107     global $db, $session, $paths, $template, $plugins; // Common objects
       
   108     
       
   109     $this->msg = $this->tpl_msg->run();
       
   110     if ( empty($this->msg) )
       
   111     {
       
   112       die_friendly(GENERAL_ERROR, 'Template for e-mail message returned a blank');
       
   113     }
       
   114 
       
   115     // We now try and pull a subject from the email body ... if it exists,
       
   116     // do this here because the subject may contain a variable
       
   117     $drop_header = '';
       
   118     $match = array();
       
   119     if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match))
       
   120     {
       
   121       $this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : 'No Subject');
       
   122       $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
       
   123     }
       
   124     else
       
   125     {
       
   126       $this->subject = (($this->subject != '') ? $this->subject : 'No Subject');
       
   127     }
       
   128 
       
   129     if (preg_match('#^(Charset:(.*?))$#m', $this->msg, $match))
       
   130     {
       
   131       $this->encoding = (trim($match[2]) != '') ? trim($match[2]) : trim('iso-8859-1');
       
   132       $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#');
       
   133     }
       
   134     else
       
   135     {
       
   136       $this->encoding = trim('iso-8859-1');
       
   137     }
       
   138 
       
   139     if ($drop_header != '')
       
   140     {
       
   141       $this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg));
       
   142     }
       
   143 
       
   144     $to = $this->addresses['to'];
       
   145 
       
   146     $cc = (count($this->addresses['cc'])) ? implode(', ', $this->addresses['cc']) : '';
       
   147     $bcc = (count($this->addresses['bcc'])) ? implode(', ', $this->addresses['bcc']) : '';
       
   148 
       
   149     // Build header
       
   150     $this->extra_headers = (($this->reply_to != '') ? "Reply-to: $this->reply_to\n" : '') .
       
   151                            (($this->from != '') ? "From: $this->from\n" : "From: " . getConfig('contact_email') . "\n") .
       
   152                            "Return-Path: " . getConfig('contact_email') .
       
   153                            "\nMessage-ID: <" . md5(uniqid(time())) . "@" . $_SERVER['SERVER_NAME'] . ">\nMIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding .
       
   154                            "\nContent-transfer-encoding: 8bit\nDate: " . date('r', time()) .
       
   155                            "\nX-Priority: 3\nX-MSMail-Priority: Normal\nX-Mailer: PHP\nX-MimeOLE: Produced By Enano CMS\n" .
       
   156                            $this->extra_headers .
       
   157                            (($cc != '') ? "Cc: $cc\n" : '')  .
       
   158                            (($bcc != '') ? "Bcc: $bcc\n" : '');
       
   159     
       
   160     //die('<pre>'.print_r($this,true).'</pre>');
       
   161 
       
   162     // Send message ... removed $this->encode() from subject for time being
       
   163     if ( $this->use_smtp )
       
   164     {
       
   165       $result = smtp_send_email_core($to, $this->subject, $this->msg, $this->extra_headers);
       
   166     }
       
   167     else
       
   168     {
       
   169       $empty_to_header = ($to == '') ? TRUE : FALSE;
       
   170       $to = ($to == '') ? ((getConfig('sendmail_fix')=='1') ? ' ' : 'Undisclosed-recipients:;') : $to;
       
   171   
       
   172       $result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
       
   173       
       
   174       if (!$result && !getConfig('sendmail_fix') && $empty_to_header)
       
   175       {
       
   176         $to = ' ';
       
   177 
       
   178         setConfig('sendmail_fix', '1');
       
   179         
       
   180         $result = @mail($to, $this->subject, preg_replace("#(?<!\r)\n#s", "\n", $this->msg), $this->extra_headers);
       
   181       }
       
   182     }
       
   183 
       
   184     // Did it work?
       
   185     if (!$result || ( $this->use_smtp && $result != 'success' ))
       
   186     {
       
   187       die_friendly(GENERAL_ERROR, 'Failed sending email :: ' . (($this->use_smtp) ? 'SMTP' : 'PHP') . ' :: ' . $result);
       
   188     }
       
   189 
       
   190     return true;
       
   191   }
       
   192 
       
   193   // Encodes the given string for proper display for this encoding ... nabbed 
       
   194   // from php.net and modified. There is an alternative encoding method which 
       
   195   // may produce lesd output but it's questionable as to its worth in this 
       
   196   // scenario IMO
       
   197   function encode($str)
       
   198   {
       
   199     if ($this->encoding == '')
       
   200     {
       
   201       return $str;
       
   202     }
       
   203 
       
   204     // define start delimimter, end delimiter and spacer
       
   205     $end = "?=";
       
   206     $start = "=?$this->encoding?B?";
       
   207     $spacer = "$end\r\n $start";
       
   208 
       
   209     // determine length of encoded text within chunks and ensure length is even
       
   210     $length = 75 - strlen($start) - strlen($end);
       
   211     $length = floor($length / 2) * 2;
       
   212 
       
   213     // encode the string and split it into chunks with spacers after each chunk
       
   214     $str = chunk_split(base64_encode($str), $length, $spacer);
       
   215 
       
   216     // remove trailing spacer and add start and end delimiters
       
   217     $str = preg_replace('#' . preg_quote($spacer, '#') . '$#', '', $str);
       
   218 
       
   219     return $start . $str . $end;
       
   220   }
       
   221 
       
   222   //
       
   223   // Attach files via MIME.
       
   224   //
       
   225   function attachFile($filename, $mimetype = "application/octet-stream", $szFromAddress, $szFilenameToDisplay)
       
   226   {
       
   227     global $lang;
       
   228     $mime_boundary = "--==================_846811060==_";
       
   229 
       
   230     $this->msg = '--' . $mime_boundary . "\nContent-Type: text/plain;\n\tcharset=".'"' . $lang['ENCODING'] . '"'."\n\n" . $this->msg;
       
   231 
       
   232     if ($mime_filename)
       
   233     {
       
   234       $filename = $mime_filename;
       
   235       $encoded = $this->encode_file($filename);
       
   236     }
       
   237 
       
   238     $fd = fopen($filename, "r");
       
   239     $contents = fread($fd, filesize($filename));
       
   240 
       
   241     $this->mimeOut = "--" . $mime_boundary . "\n";
       
   242     $this->mimeOut .= "Content-Type: " . $mimetype . ";\n\tname=".'"'."$szFilenameToDisplay".'"'."\n";
       
   243     $this->mimeOut .= "Content-Transfer-Encoding: quoted-printable\n";
       
   244     $this->mimeOut .= "Content-Disposition: attachment;\n\tfilename=".'"'."$szFilenameToDisplay".'"'."\n\n";
       
   245 
       
   246     if ( $mimetype == "message/rfc822" )
       
   247     {
       
   248       $this->mimeOut .= "From: ".$szFromAddress."\n";
       
   249       $this->mimeOut .= "To: ".$this->emailAddress."\n";
       
   250       $this->mimeOut .= "Date: ".date("D, d M Y H:i:s") . " UT\n";
       
   251       $this->mimeOut .= "Reply-To:".$szFromAddress."\n";
       
   252       $this->mimeOut .= "Subject: ".$this->mailSubject."\n";
       
   253       $this->mimeOut .= "X-Mailer: PHP/".phpversion()."\n";
       
   254       $this->mimeOut .= "MIME-Version: 1.0\n";
       
   255     }
       
   256 
       
   257     $this->mimeOut .= $contents."\n";
       
   258     $this->mimeOut .= "--" . $mime_boundary . "--" . "\n";
       
   259 
       
   260     return $out;
       
   261     // added -- to notify email client attachment is done
       
   262   }
       
   263 
       
   264   function getMimeHeaders($filename, $mime_filename="")
       
   265   {
       
   266     $mime_boundary = "--==================_846811060==_";
       
   267 
       
   268     if ($mime_filename)
       
   269     {
       
   270       $filename = $mime_filename;
       
   271     }
       
   272 
       
   273     $out = "MIME-Version: 1.0\n";
       
   274     $out .= "Content-Type: multipart/mixed;\n\tboundary=".'"'."$mime_boundary".'"'."\n\n";
       
   275     $out .= "This message is in MIME format. Since your mail reader does not understand\n";
       
   276     $out .= "this format, some or all of this message may not be legible.";
       
   277 
       
   278     return $out;
       
   279   }
       
   280 
       
   281   //
       
   282    // Split string by RFC 2045 semantics (76 chars per line, end with \r\n).
       
   283   //
       
   284   function myChunkSplit($str)
       
   285   {
       
   286     $stmp = $str;
       
   287     $len = strlen($stmp);
       
   288     $out = "";
       
   289 
       
   290     while ($len > 0)
       
   291     {
       
   292       if ($len >= 76)
       
   293       {
       
   294         $out .= substr($stmp, 0, 76) . "\r\n";
       
   295         $stmp = substr($stmp, 76);
       
   296         $len = $len - 76;
       
   297       }
       
   298       else
       
   299       {
       
   300         $out .= $stmp . "\r\n";
       
   301         $stmp = "";
       
   302         $len = 0;
       
   303       }
       
   304     }
       
   305     return $out;
       
   306   }
       
   307 
       
   308   //
       
   309    // Split the specified file up into a string and return it
       
   310   //
       
   311   function encode_file($sourcefile)
       
   312   {
       
   313     if (is_readable(@realpath($sourcefile)))
       
   314     {
       
   315       $fd = fopen($sourcefile, "r");
       
   316       $contents = fread($fd, filesize($sourcefile));
       
   317         $encoded = $this->myChunkSplit(base64_encode($contents));
       
   318         fclose($fd);
       
   319     }
       
   320 
       
   321     return $encoded;
       
   322   }
       
   323 
       
   324 } // class emailer
       
   325 
       
   326 
       
   327 /**
       
   328  * This code is copyright (C) 2004 Jim Tucek
       
   329  * PHP version ported from Javascript by Dan Fuhry
       
   330  * All rights reserved.
       
   331  * @link http://www.jracademy.com/~jtucek/email/
       
   332  * @license GNU General Public License v2, permission obtained specifically for Enano
       
   333  */
       
   334 
       
   335 class EmailEncryptor
       
   336 {
       
   337  
       
   338   var $primes = Array(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199);
       
   339   
       
   340   function __construct()
       
   341   {
       
   342     $i = 0;
       
   343     $this->p = 0;
       
   344     $this->q = 0;
       
   345     while($this->p * $this->q < 255 || $this->p == $this->q)
       
   346     {
       
   347       $this->p = $this->primes[mt_rand(0, sizeof($this->primes)-1)];
       
   348       $this->q = $this->primes[mt_rand(0, sizeof($this->primes)-1)];
       
   349     }
       
   350   }
       
   351   
       
   352   function EmailEncryptor()
       
   353   {
       
   354     $this->__construct();
       
   355   }
       
   356   
       
   357   function testAll() {
       
   358     $size = sizeof($this->primes);
       
   359     
       
   360     $allCharacters = "";
       
   361     for($c = 33; $c <= 126; $c++)
       
   362       $allCharacters = $allCharacters . $this->fromCharCode($c);
       
   363     
       
   364     for($i = 0; $i < $size - 1; $i++) {
       
   365       for($j = $i + 1; $j < $size; $j++) {
       
   366         $this->p = $this->primes[$i];
       
   367         $this->q = $this->primes[$j];
       
   368         if($this->p*$this->q < 255)
       
   369           break;
       
   370         $k = $this->makeKey($allCharacters);
       
   371         die('<pre>'.print_r($k, true).'</pre>');
       
   372         $encrypted = $k['X'];
       
   373         $decrypted = $this->goForth($encrypted,$this->p*$this->q,$k['D']);
       
   374         if($decrypted != $allCharacters) {
       
   375           die('Test failed');
       
   376         }
       
   377       }
       
   378     }
       
   379     return 'GOOD';
       
   380   }
       
   381   
       
   382   function charCodeAt($str, $i)
       
   383   {
       
   384     return ord(substr($str, $i, 1));
       
   385   }
       
   386   
       
   387   function fromCharCode($str)
       
   388   {
       
   389     return chr($str);
       
   390   }
       
   391   
       
   392   function MakeArray($l) {
       
   393     $a = Array();
       
   394     $i=0;
       
   395     do {
       
   396       $a[$i]=null;
       
   397       $i++;
       
   398     } while($i < $l);
       
   399     return $a;
       
   400   }
       
   401   
       
   402   function makeKey($addr,$subj = '',$body = '') {
       
   403     $value = "";
       
   404     
       
   405     if($this->p * $this->q < 255)
       
   406     {
       
   407       return("P*Q must be greater than 255! P*Q = " . $this->p*$this->q);
       
   408     }
       
   409     elseif($this->p == $this->q)
       
   410     {
       
   411       return("P cannot be equal to Q!");
       
   412     }
       
   413     elseif($addr == "")
       
   414     {
       
   415       return("You must enter an address to encrypt!");
       
   416     }
       
   417     else
       
   418     {
       
   419       // Make the key
       
   420       $c = 0;
       
   421       $z = ($this->p-1)*($this->q-1);
       
   422       $e = 0;
       
   423       $n = $this->p*$this->q;
       
   424       $d = 0;
       
   425     
       
   426       do {
       
   427         $e++;
       
   428         $d = $this->getKey($this->primes[$e],$z);
       
   429       } while($d==1);
       
   430       $e = $this->primes[$e];
       
   431       
       
   432       // Turn the string into an array of numbers < 255
       
   433       $m = $addr;
       
   434       $emailLength = strlen($m);
       
   435       $justEmail = "";
       
   436       $sep = ( strstr('?', $m) ) ? '&' : '?';
       
   437       if($subj != "") {
       
   438         $m = $m . "{$sep}subject=" . $subj;
       
   439       }
       
   440       $sep = ( strstr($m, '?') ) ? '&' : '?';
       
   441       if($body != "") {
       
   442         $m = $m . "{$sep}body=" . $body;
       
   443       }
       
   444     
       
   445       $length = strlen($m);
       
   446       $theString = $this->MakeArray($length);
       
   447       for($i = 0; $i < $length; $i++) {
       
   448         $theString[$i] = $this->charCodeAt($m, $i);
       
   449       }
       
   450       
       
   451       // Encrypt each of the numbers
       
   452       $theCode = $this->MakeArray($length);
       
   453       $c = "";
       
   454       $temp = 0;
       
   455       for($i = 0; $i < $length; $i++) {
       
   456         if($i != 0)
       
   457           $c .= " ";
       
   458         $temp = $this->myMod($theString[$i],$e,$n);
       
   459         $theCode[$i] = $temp;
       
   460         $c .= $temp;
       
   461         if($i == $emailLength - 1)
       
   462           $justEmail = $c;
       
   463       }
       
   464     }
       
   465     return Array('X'=>$justEmail, 'N'=>$n, 'D'=>$d, 'E'=>$e, 'C'=>$c, 'M'=>$m);
       
   466   }
       
   467     
       
   468   // Finds x^e % y for large values of (x^e)
       
   469   function myMod($x,$e,$y) {
       
   470     if ($e % 2 == 0) {
       
   471       $answer = 1;
       
   472       for($i = 1; $i <= e/2; $i++) {
       
   473         $temp = ($x*$x) % $y;
       
   474         $answer = ($temp*$answer) % $y;
       
   475       }
       
   476     } else {
       
   477       $answer = $x;
       
   478       for($i = 1; $i <= $e/2; $i++) {
       
   479         $temp = ($x*$x) % $y;
       
   480         $answer = ($temp*$answer) % $y;
       
   481       }
       
   482     }
       
   483     return $answer;
       
   484   }
       
   485   
       
   486   
       
   487   function getKey($e,$z) {
       
   488     $A = 1;
       
   489     $B = 0;
       
   490     $C = $z;
       
   491     $F = 0;
       
   492     $G = 1;
       
   493     $bar = $e;    
       
   494     // Euclid's Algorithm:
       
   495     while ($bar != 0) {
       
   496       $foo = floor($C/$bar);
       
   497       $K = $A - $foo * $F;
       
   498       $L = $B - $foo * $G;
       
   499       $M = $C - $foo * $bar;
       
   500       $A = $F;
       
   501       $B = $G;
       
   502       $C = $bar;
       
   503       $F = $K;
       
   504       $G = $L;
       
   505       $bar = $M;
       
   506     }
       
   507     if ($B < 0)
       
   508     {
       
   509       return ($B + $z);
       
   510     }
       
   511     else
       
   512     {
       
   513       return ($B);
       
   514     }
       
   515   }
       
   516   
       
   517   function goForth($c,$n,$d) {
       
   518     $c .= " "; 
       
   519     $length = strlen($c);
       
   520     $number = 0;
       
   521     $bar = 0;
       
   522     $answer = "";
       
   523   
       
   524     for($i = 0; $i < $length; $i++) {
       
   525       $number = 0;
       
   526       $bar = 0;
       
   527       while($this->charCodeAt($c, $i) != 32) { 
       
   528         $number = $number * 10;
       
   529         $number = $number + $this->charCodeAt($c, $i)-48;
       
   530         $i++;
       
   531       }
       
   532       $answer .= $this->fromCharCode($this->decrypt($number,$n,$d));
       
   533     }
       
   534     return $answer;
       
   535   }
       
   536   
       
   537   function decrypt($c,$n,$d) {
       
   538     // Split exponents up
       
   539     if ($d % 2== 0) {
       
   540       $bar = 1;
       
   541       for($i = 1; $i <= $d/2; $i++) {
       
   542         $foo = ($c*$c) % $n;
       
   543         $bar = ($foo*$bar) % $n;
       
   544       }
       
   545     } else {
       
   546       $bar = $c;
       
   547       for($i = 1; $i <= $d/2; $i++) {
       
   548         $foo = ($c*$c) % $n;
       
   549         $bar = ($foo*$bar) % $n;
       
   550       }
       
   551     }
       
   552     return $bar;
       
   553   }
       
   554   
       
   555   function writeOptions() {
       
   556     $size = sizeof($this->primes);
       
   557     for($i = 0; $i < $size; $i++)
       
   558       echo("<option value=".'"'.""+$this->primes[$i]+"".'"'.">"+$this->primes[$i]+"</option>");
       
   559   }
       
   560   
       
   561   function jscode() {
       
   562     return "<script type='text/javascript'>\n// <![CDATA[\nfunction dive(absorption,alchemy,friendship) { absorption += ' '; var file = absorption.length; var sand = 0; var closet = ''; for(var assistant = 0; assistant < file; assistant++) { sand = 0; while(absorption.charCodeAt(assistant) != 32) { sand = sand * 10; sand = sand + absorption.charCodeAt(assistant)-48; assistant++; } closet += String.fromCharCode(say(sand,alchemy,friendship)); } parent.location = 'm'+'a'+'i'+'l'+'t'+'o'+':'+closet; }; function forbid(landing,atmosphere,aviation) { landing += ' '; var kiss = landing.length; var coordinated = 0; for(var day = 0; day < kiss; day++) { coordinated = 0; while(landing.charCodeAt(day) != 32) { coordinated = coordinated * 10; coordinated = coordinated + landing.charCodeAt(day)-48; day++; } document.write(String.fromCharCode(say(coordinated,atmosphere,aviation))); }; }; function say(scene,photograph,fraction) { if (fraction % 2 == 0) { integrity = 1; for(var male = 1; male <= fraction/2; male++) { moon = (scene*scene) % photograph; integrity = (moon*integrity) % photograph; } } else { integrity = scene; for(var night = 1; night <= fraction/2; night++) { moon = (scene*scene) % photograph; integrity = (moon*integrity) % photograph; }; }; return integrity; };\n// ]]>\n</script>";
       
   563   }
       
   564   
       
   565   /**
       
   566    * Wrapper - spits out ready-to-use HTML
       
   567    * @param string $address The e-mail address
       
   568    * @param string $subject The subject of the e-mail. OPTIONAL.
       
   569    * @param string $body The main content of the e-mail. OPTIONAL and doesn't work in many e-mail clients.
       
   570    * @param string $text The text to be shown on the e-mail link. Leave as false to make the e-mail address be shown in the link (but still fully encrypted)
       
   571    */
       
   572   
       
   573   function encryptEmail($address, $subject = '', $body = '', $text = false)
       
   574   {
       
   575     $key = $this->makeKey($address, $subject, $body);
       
   576     if ( $text )
       
   577     {
       
   578       if(preg_match('/^(mailto:)?(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/', $text))
       
   579       {
       
   580         // This is a mailto link and normal obfuscation should be used
       
   581         $text = false;
       
   582       }
       
   583     }
       
   584     $text1 = ( $text ) ? '<script type="text/javascript">document.write(unescape(\''.rawurlencode($text).'\'));</script>' : '<script type=\'text/javascript\'>forbid("'.$key['X'].'",'.$key['N'].','.$key['D'].')</script>';
       
   585     $text2 = ( $text ) ? "$text &lt;".$this->obfuscate_text($this->mask_address($address))."&gt;" : $this->obfuscate_text($this->mask_address($address));
       
   586     $email = '<a href="#" onclick=\'dive("'.$key['C'].'",'.$key['N'].','.$key['D'].'); return false;\' onmouseover="self.status=\'\'; return true;" onmouseout="self.status=\' \'; return true;">'.$text1.'</a><noscript><div style="display: inline">'.$text2.'</div></noscript>';
       
   587     return $email;
       
   588   }
       
   589   
       
   590   /** 
       
   591    * Replace @ symbols with " <AT> " and dots with " <DOT> ".
       
   592    * @param string $email An e-mail address.
       
   593    * @return string
       
   594    */
       
   595    
       
   596   function mask_address($email)
       
   597   {
       
   598     return str_replace('.', ' <DOT> ', str_replace('@', ' <AT> ', $email));
       
   599   }
       
   600   
       
   601   /**
       
   602    * Turn a string of text into hex-encoded HTML entities
       
   603    * @param string $text the text to encode
       
   604    * @return string
       
   605    */
       
   606 
       
   607   function obfuscate_text($text)
       
   608   {
       
   609     $a = enano_str_split($text, 1);
       
   610     $s = '';
       
   611     foreach($a as $k => $c)
       
   612     {
       
   613       $ch = (string)dechex(ord($a[$k]));
       
   614       if(strlen($ch) < 2) $ch = '0' . $ch;
       
   615       $s .= '&#x'.$ch.';';
       
   616     }
       
   617     return $s;
       
   618   }
       
   619 
       
   620 }
       
   621 
       
   622 ?>