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