diff -r de56132c008d -r bdac73ed481e includes/email.php --- a/includes/email.php Sun Mar 28 21:49:26 2010 -0400 +++ b/includes/email.php Sun Mar 28 23:10:46 2010 -0400 @@ -20,305 +20,305 @@ // class emailer { - var $msg, $subject, $extra_headers; - var $addresses, $reply_to, $from; - var $use_smtp; + var $msg, $subject, $extra_headers; + var $addresses, $reply_to, $from; + var $use_smtp; - var $tpl_msg; + var $tpl_msg; - function __construct($use_smtp) - { - $this->reset(); - $this->use_smtp = $use_smtp; - $this->reply_to = $this->from = ''; - } + function __construct($use_smtp) + { + $this->reset(); + $this->use_smtp = $use_smtp; + $this->reply_to = $this->from = ''; + } - // Resets all the data (address, template file, etc etc to default - function reset() - { - $this->addresses = array(); - $this->vars = $this->msg = $this->extra_headers = ''; - } + // Resets all the data (address, template file, etc etc to default + function reset() + { + $this->addresses = array(); + $this->vars = $this->msg = $this->extra_headers = ''; + } - // Sets an email address to send to - function email_address($address) - { - $this->addresses['to'] = trim($address); - } + // Sets an email address to send to + function email_address($address) + { + $this->addresses['to'] = trim($address); + } - function cc($address) - { - $this->addresses['cc'][] = trim($address); - } + function cc($address) + { + $this->addresses['cc'][] = trim($address); + } - function bcc($address) - { - $this->addresses['bcc'][] = trim($address); - } + function bcc($address) + { + $this->addresses['bcc'][] = trim($address); + } - function replyto($address) - { - $this->reply_to = trim($address); - } + function replyto($address) + { + $this->reply_to = trim($address); + } - function from($address) - { - $this->from = trim($address); - } + function from($address) + { + $this->from = trim($address); + } - // set up subject for mail - function set_subject($subject = '') - { - $this->subject = trim(preg_replace('#[\n\r]+#s', '', $subject)); - } + // set up subject for mail + function set_subject($subject = '') + { + $this->subject = trim(preg_replace('#[\n\r]+#s', '', $subject)); + } - // set up extra mail headers - function extra_headers($headers) - { - $this->extra_headers .= trim($headers) . "\n"; - } + // set up extra mail headers + function extra_headers($headers) + { + $this->extra_headers .= trim($headers) . "\n"; + } - function use_template($template_code) - { - global $db, $session, $paths, $template, $plugins; // Common objects - - $this->tpl_msg = $template->makeParserText($template_code); + function use_template($template_code) + { + global $db, $session, $paths, $template, $plugins; // Common objects + + $this->tpl_msg = $template->makeParserText($template_code); - return true; - } + return true; + } - // assign variables - function assign_vars($vars) - { - if ( is_object($this->tpl_msg) ) - { - $this->tpl_msg->assign_vars($vars); - } - else - { - die_friendly(GENERAL_ERROR, 'Can\'t set vars, the template is not set'); - } - } + // assign variables + function assign_vars($vars) + { + if ( is_object($this->tpl_msg) ) + { + $this->tpl_msg->assign_vars($vars); + } + else + { + die_friendly(GENERAL_ERROR, 'Can\'t set vars, the template is not set'); + } + } - // Send the mail out to the recipients set previously in var $this->address - function send() - { - global $db, $session, $paths, $template, $plugins; // Common objects - - $this->msg = $this->tpl_msg->run(); - if ( empty($this->msg) ) - { - die_friendly(GENERAL_ERROR, 'Template for e-mail message returned a blank'); - } + // Send the mail out to the recipients set previously in var $this->address + function send() + { + global $db, $session, $paths, $template, $plugins; // Common objects + + $this->msg = $this->tpl_msg->run(); + if ( empty($this->msg) ) + { + die_friendly(GENERAL_ERROR, 'Template for e-mail message returned a blank'); + } - // We now try and pull a subject from the email body ... if it exists, - // do this here because the subject may contain a variable - $drop_header = ''; - $match = array(); - if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match)) - { - $this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : 'No Subject'); - $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#'); - } - else - { - $this->subject = (($this->subject != '') ? $this->subject : 'No Subject'); - } + // We now try and pull a subject from the email body ... if it exists, + // do this here because the subject may contain a variable + $drop_header = ''; + $match = array(); + if (preg_match('#^(Subject:(.*?))$#m', $this->msg, $match)) + { + $this->subject = (trim($match[2]) != '') ? trim($match[2]) : (($this->subject != '') ? $this->subject : 'No Subject'); + $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#'); + } + else + { + $this->subject = (($this->subject != '') ? $this->subject : 'No Subject'); + } - if (preg_match('#^(Charset:(.*?))$#m', $this->msg, $match)) - { - $this->encoding = (trim($match[2]) != '') ? trim($match[2]) : trim('iso-8859-1'); - $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#'); - } - else - { - $this->encoding = trim('iso-8859-1'); - } + if (preg_match('#^(Charset:(.*?))$#m', $this->msg, $match)) + { + $this->encoding = (trim($match[2]) != '') ? trim($match[2]) : trim('iso-8859-1'); + $drop_header .= '[\r\n]*?' . preg_quote($match[1], '#'); + } + else + { + $this->encoding = trim('iso-8859-1'); + } - if ($drop_header != '') - { - $this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg)); - } + if ($drop_header != '') + { + $this->msg = trim(preg_replace('#' . $drop_header . '#s', '', $this->msg)); + } - $to = $this->addresses['to']; + $to = $this->addresses['to']; - $cc = (count($this->addresses['cc'])) ? implode(', ', $this->addresses['cc']) : ''; - $bcc = (count($this->addresses['bcc'])) ? implode(', ', $this->addresses['bcc']) : ''; + $cc = (count($this->addresses['cc'])) ? implode(', ', $this->addresses['cc']) : ''; + $bcc = (count($this->addresses['bcc'])) ? implode(', ', $this->addresses['bcc']) : ''; - // Build header - $this->extra_headers = (($this->reply_to != '') ? "Reply-to: $this->reply_to\n" : '') . - (($this->from != '') ? "From: $this->from\n" : "From: " . getConfig('contact_email') . "\n") . - "Return-Path: " . getConfig('contact_email') . - "\nMessage-ID: <" . md5(uniqid(time())) . "@" . $_SERVER['SERVER_NAME'] . ">\nMIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . - "\nContent-transfer-encoding: 8bit\nDate: " . enano_date('r', time()) . - "\nX-Priority: 3\nX-MSMail-Priority: Normal\nX-Mailer: PHP\nX-MimeOLE: Produced By Enano CMS\n" . - $this->extra_headers . - (($cc != '') ? "Cc: $cc\n" : '') . - (($bcc != '') ? "Bcc: $bcc\n" : ''); - - //die('
'.print_r($this,true).'
'); + // Build header + $this->extra_headers = (($this->reply_to != '') ? "Reply-to: $this->reply_to\n" : '') . + (($this->from != '') ? "From: $this->from\n" : "From: " . getConfig('contact_email') . "\n") . + "Return-Path: " . getConfig('contact_email') . + "\nMessage-ID: <" . md5(uniqid(time())) . "@" . $_SERVER['SERVER_NAME'] . ">\nMIME-Version: 1.0\nContent-type: text/plain; charset=" . $this->encoding . + "\nContent-transfer-encoding: 8bit\nDate: " . enano_date('r', time()) . + "\nX-Priority: 3\nX-MSMail-Priority: Normal\nX-Mailer: PHP\nX-MimeOLE: Produced By Enano CMS\n" . + $this->extra_headers . + (($cc != '') ? "Cc: $cc\n" : '') . + (($bcc != '') ? "Bcc: $bcc\n" : ''); + + //die('
'.print_r($this,true).'
'); - // Send message ... removed $this->encode() from subject for time being - if ( $this->use_smtp ) - { - $result = smtp_send_email_core($to, $this->subject, $this->msg, $this->extra_headers); - } - else - { - $empty_to_header = ($to == '') ? TRUE : FALSE; - $to = ($to == '') ? ((getConfig('sendmail_fix')=='1') ? ' ' : 'Undisclosed-recipients:;') : $to; - - $result = @mail($to, $this->subject, preg_replace("#(?msg), $this->extra_headers); - - if (!$result && !getConfig('sendmail_fix') && $empty_to_header) - { - $to = ' '; + // Send message ... removed $this->encode() from subject for time being + if ( $this->use_smtp ) + { + $result = smtp_send_email_core($to, $this->subject, $this->msg, $this->extra_headers); + } + else + { + $empty_to_header = ($to == '') ? TRUE : FALSE; + $to = ($to == '') ? ((getConfig('sendmail_fix')=='1') ? ' ' : 'Undisclosed-recipients:;') : $to; + + $result = @mail($to, $this->subject, preg_replace("#(?msg), $this->extra_headers); + + if (!$result && !getConfig('sendmail_fix') && $empty_to_header) + { + $to = ' '; - setConfig('sendmail_fix', '1'); - - $result = @mail($to, $this->subject, preg_replace("#(?msg), $this->extra_headers); - } - } + setConfig('sendmail_fix', '1'); + + $result = @mail($to, $this->subject, preg_replace("#(?msg), $this->extra_headers); + } + } - // Did it work? - if (!$result || ( $this->use_smtp && $result != 'success' )) - { - die_friendly(GENERAL_ERROR, 'Failed sending email :: ' . (($this->use_smtp) ? 'SMTP' : 'PHP') . ' :: ' . $result); - } + // Did it work? + if (!$result || ( $this->use_smtp && $result != 'success' )) + { + die_friendly(GENERAL_ERROR, 'Failed sending email :: ' . (($this->use_smtp) ? 'SMTP' : 'PHP') . ' :: ' . $result); + } - return true; - } + return true; + } - // Encodes the given string for proper display for this encoding ... nabbed - // from php.net and modified. There is an alternative encoding method which - // may produce lesd output but it's questionable as to its worth in this - // scenario IMO - function encode($str) - { - if ($this->encoding == '') - { - return $str; - } + // Encodes the given string for proper display for this encoding ... nabbed + // from php.net and modified. There is an alternative encoding method which + // may produce lesd output but it's questionable as to its worth in this + // scenario IMO + function encode($str) + { + if ($this->encoding == '') + { + return $str; + } - // define start delimimter, end delimiter and spacer - $end = "?="; - $start = "=?$this->encoding?B?"; - $spacer = "$end\r\n $start"; + // define start delimimter, end delimiter and spacer + $end = "?="; + $start = "=?$this->encoding?B?"; + $spacer = "$end\r\n $start"; - // determine length of encoded text within chunks and ensure length is even - $length = 75 - strlen($start) - strlen($end); - $length = floor($length / 2) * 2; + // determine length of encoded text within chunks and ensure length is even + $length = 75 - strlen($start) - strlen($end); + $length = floor($length / 2) * 2; - // encode the string and split it into chunks with spacers after each chunk - $str = chunk_split(base64_encode($str), $length, $spacer); + // encode the string and split it into chunks with spacers after each chunk + $str = chunk_split(base64_encode($str), $length, $spacer); - // remove trailing spacer and add start and end delimiters - $str = preg_replace('#' . preg_quote($spacer, '#') . '$#', '', $str); + // remove trailing spacer and add start and end delimiters + $str = preg_replace('#' . preg_quote($spacer, '#') . '$#', '', $str); - return $start . $str . $end; - } + return $start . $str . $end; + } - // - // Attach files via MIME. - // - function attachFile($filename, $mimetype = "application/octet-stream", $szFromAddress, $szFilenameToDisplay) - { - global $lang; - $mime_boundary = "--==================_846811060==_"; + // + // Attach files via MIME. + // + function attachFile($filename, $mimetype = "application/octet-stream", $szFromAddress, $szFilenameToDisplay) + { + global $lang; + $mime_boundary = "--==================_846811060==_"; - $this->msg = '--' . $mime_boundary . "\nContent-Type: text/plain;\n\tcharset=".'"' . $lang['ENCODING'] . '"'."\n\n" . $this->msg; + $this->msg = '--' . $mime_boundary . "\nContent-Type: text/plain;\n\tcharset=".'"' . $lang['ENCODING'] . '"'."\n\n" . $this->msg; - if ($mime_filename) - { - $filename = $mime_filename; - $encoded = $this->encode_file($filename); - } + if ($mime_filename) + { + $filename = $mime_filename; + $encoded = $this->encode_file($filename); + } - $fd = fopen($filename, "r"); - $contents = fread($fd, filesize($filename)); + $fd = fopen($filename, "r"); + $contents = fread($fd, filesize($filename)); - $this->mimeOut = "--" . $mime_boundary . "\n"; - $this->mimeOut .= "Content-Type: " . $mimetype . ";\n\tname=".'"'."$szFilenameToDisplay".'"'."\n"; - $this->mimeOut .= "Content-Transfer-Encoding: quoted-printable\n"; - $this->mimeOut .= "Content-Disposition: attachment;\n\tfilename=".'"'."$szFilenameToDisplay".'"'."\n\n"; + $this->mimeOut = "--" . $mime_boundary . "\n"; + $this->mimeOut .= "Content-Type: " . $mimetype . ";\n\tname=".'"'."$szFilenameToDisplay".'"'."\n"; + $this->mimeOut .= "Content-Transfer-Encoding: quoted-printable\n"; + $this->mimeOut .= "Content-Disposition: attachment;\n\tfilename=".'"'."$szFilenameToDisplay".'"'."\n\n"; - if ( $mimetype == "message/rfc822" ) - { - $this->mimeOut .= "From: ".$szFromAddress."\n"; - $this->mimeOut .= "To: ".$this->emailAddress."\n"; - $this->mimeOut .= "Date: ".enano_date('r') . " UT\n"; - $this->mimeOut .= "Reply-To:".$szFromAddress."\n"; - $this->mimeOut .= "Subject: ".$this->mailSubject."\n"; - $this->mimeOut .= "X-Mailer: PHP/".phpversion()."\n"; - $this->mimeOut .= "MIME-Version: 1.0\n"; - } + if ( $mimetype == "message/rfc822" ) + { + $this->mimeOut .= "From: ".$szFromAddress."\n"; + $this->mimeOut .= "To: ".$this->emailAddress."\n"; + $this->mimeOut .= "Date: ".enano_date('r') . " UT\n"; + $this->mimeOut .= "Reply-To:".$szFromAddress."\n"; + $this->mimeOut .= "Subject: ".$this->mailSubject."\n"; + $this->mimeOut .= "X-Mailer: PHP/".phpversion()."\n"; + $this->mimeOut .= "MIME-Version: 1.0\n"; + } - $this->mimeOut .= $contents."\n"; - $this->mimeOut .= "--" . $mime_boundary . "--" . "\n"; + $this->mimeOut .= $contents."\n"; + $this->mimeOut .= "--" . $mime_boundary . "--" . "\n"; - return $out; - // added -- to notify email client attachment is done - } + return $out; + // added -- to notify email client attachment is done + } - function getMimeHeaders($filename, $mime_filename="") - { - $mime_boundary = "--==================_846811060==_"; + function getMimeHeaders($filename, $mime_filename="") + { + $mime_boundary = "--==================_846811060==_"; - if ($mime_filename) - { - $filename = $mime_filename; - } + if ($mime_filename) + { + $filename = $mime_filename; + } - $out = "MIME-Version: 1.0\n"; - $out .= "Content-Type: multipart/mixed;\n\tboundary=".'"'."$mime_boundary".'"'."\n\n"; - $out .= "This message is in MIME format. Since your mail reader does not understand\n"; - $out .= "this format, some or all of this message may not be legible."; + $out = "MIME-Version: 1.0\n"; + $out .= "Content-Type: multipart/mixed;\n\tboundary=".'"'."$mime_boundary".'"'."\n\n"; + $out .= "This message is in MIME format. Since your mail reader does not understand\n"; + $out .= "this format, some or all of this message may not be legible."; - return $out; - } + return $out; + } - // - // Split string by RFC 2045 semantics (76 chars per line, end with \r\n). - // - function myChunkSplit($str) - { - $stmp = $str; - $len = strlen($stmp); - $out = ""; + // + // Split string by RFC 2045 semantics (76 chars per line, end with \r\n). + // + function myChunkSplit($str) + { + $stmp = $str; + $len = strlen($stmp); + $out = ""; - while ($len > 0) - { - if ($len >= 76) - { - $out .= substr($stmp, 0, 76) . "\r\n"; - $stmp = substr($stmp, 76); - $len = $len - 76; - } - else - { - $out .= $stmp . "\r\n"; - $stmp = ""; - $len = 0; - } - } - return $out; - } + while ($len > 0) + { + if ($len >= 76) + { + $out .= substr($stmp, 0, 76) . "\r\n"; + $stmp = substr($stmp, 76); + $len = $len - 76; + } + else + { + $out .= $stmp . "\r\n"; + $stmp = ""; + $len = 0; + } + } + return $out; + } - // - // Split the specified file up into a string and return it - // - function encode_file($sourcefile) - { - if (is_readable(@realpath($sourcefile))) - { - $fd = fopen($sourcefile, "r"); - $contents = fread($fd, filesize($sourcefile)); - $encoded = $this->myChunkSplit(base64_encode($contents)); - fclose($fd); - } + // + // Split the specified file up into a string and return it + // + function encode_file($sourcefile) + { + if (is_readable(@realpath($sourcefile))) + { + $fd = fopen($sourcefile, "r"); + $contents = fread($fd, filesize($sourcefile)); + $encoded = $this->myChunkSplit(base64_encode($contents)); + fclose($fd); + } - return $encoded; - } + return $encoded; + } } // class emailer @@ -334,293 +334,293 @@ class EmailEncryptor { - 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); - - function __construct() - { - $i = 0; - $this->p = 0; - $this->q = 0; - while($this->p * $this->q < 255 || $this->p == $this->q) - { - $this->p = $this->primes[mt_rand(0, sizeof($this->primes)-1)]; - $this->q = $this->primes[mt_rand(0, sizeof($this->primes)-1)]; - } - } - - function testAll() { - $size = sizeof($this->primes); - - $allCharacters = ""; - for($c = 33; $c <= 126; $c++) - $allCharacters = $allCharacters . $this->fromCharCode($c); - - for($i = 0; $i < $size - 1; $i++) { - for($j = $i + 1; $j < $size; $j++) { - $this->p = $this->primes[$i]; - $this->q = $this->primes[$j]; - if($this->p*$this->q < 255) - break; - $k = $this->makeKey($allCharacters); - $encrypted = $k['X']; - $decrypted = $this->goForth($encrypted,$this->p*$this->q,$k['D']); - if($decrypted != $allCharacters) { - die('Test failed'); - } - } - } - return 'GOOD'; - } - - function charCodeAt($str, $i) - { - return ord(substr($str, $i, 1)); - } - - function fromCharCode($str) - { - return chr($str); - } - - function MakeArray($l) { - $a = Array(); - $i=0; - do { - $a[$i]=null; - $i++; - } while($i < $l); - return $a; - } - - function makeKey($addr,$subj = '',$body = '') { - $value = ""; - - if($this->p * $this->q < 255) - { - return("P*Q must be greater than 255! P*Q = " . $this->p*$this->q); - } - elseif($this->p == $this->q) - { - return("P cannot be equal to Q!"); - } - elseif($addr == "") - { - return("You must enter an address to encrypt!"); - } - else - { - // Make the key - $c = 0; - $z = ($this->p-1)*($this->q-1); - $e = 0; - $n = $this->p*$this->q; - $d = 0; - - do { - $e++; - $d = $this->getKey($this->primes[$e],$z); - } while($d==1); - $e = $this->primes[$e]; - - // Turn the string into an array of numbers < 255 - $m = $addr; - $emailLength = strlen($m); - $justEmail = ""; - $sep = ( strstr('?', $m) ) ? '&' : '?'; - if($subj != "") { - $m = $m . "{$sep}subject=" . $subj; - } - $sep = ( strstr($m, '?') ) ? '&' : '?'; - if($body != "") { - $m = $m . "{$sep}body=" . $body; - } - - $length = strlen($m); - $theString = $this->MakeArray($length); - for($i = 0; $i < $length; $i++) { - $theString[$i] = $this->charCodeAt($m, $i); - } - - // Encrypt each of the numbers - $theCode = $this->MakeArray($length); - $c = ""; - $temp = 0; - for($i = 0; $i < $length; $i++) { - if($i != 0) - $c .= " "; - $temp = $this->myMod($theString[$i],$e,$n); - $theCode[$i] = $temp; - $c .= $temp; - if($i == $emailLength - 1) - $justEmail = $c; - } - } - return Array('X'=>$justEmail, 'N'=>$n, 'D'=>$d, 'E'=>$e, 'C'=>$c, 'M'=>$m); - } - - // Finds x^e % y for large values of (x^e) - function myMod($x,$e,$y) { - if ($e % 2 == 0) { - $answer = 1; - for($i = 1; $i <= e/2; $i++) { - $temp = ($x*$x) % $y; - $answer = ($temp*$answer) % $y; - } - } else { - $answer = $x; - for($i = 1; $i <= $e/2; $i++) { - $temp = ($x*$x) % $y; - $answer = ($temp*$answer) % $y; - } - } - return $answer; - } - - - function getKey($e,$z) { - $A = 1; - $B = 0; - $C = $z; - $F = 0; - $G = 1; - $bar = $e; - // Euclid's Algorithm: - while ($bar != 0) { - $foo = floor($C/$bar); - $K = $A - $foo * $F; - $L = $B - $foo * $G; - $M = $C - $foo * $bar; - $A = $F; - $B = $G; - $C = $bar; - $F = $K; - $G = $L; - $bar = $M; - } - if ($B < 0) - { - return ($B + $z); - } - else - { - return ($B); - } - } - - function goForth($c,$n,$d) { - $c .= " "; - $length = strlen($c); - $number = 0; - $bar = 0; - $answer = ""; - - for($i = 0; $i < $length; $i++) { - $number = 0; - $bar = 0; - while($this->charCodeAt($c, $i) != 32) { - $number = $number * 10; - $number = $number + $this->charCodeAt($c, $i)-48; - $i++; - } - $answer .= $this->fromCharCode($this->decrypt($number,$n,$d)); - } - return $answer; - } - - function decrypt($c,$n,$d) { - // Split exponents up - if ($d % 2== 0) { - $bar = 1; - for($i = 1; $i <= $d/2; $i++) { - $foo = ($c*$c) % $n; - $bar = ($foo*$bar) % $n; - } - } else { - $bar = $c; - for($i = 1; $i <= $d/2; $i++) { - $foo = ($c*$c) % $n; - $bar = ($foo*$bar) % $n; - } - } - return $bar; - } - - function writeOptions() { - $size = sizeof($this->primes); - for($i = 0; $i < $size; $i++) - echo(""); - } - - function jscode() { - return ""; - } - - /** - * Wrapper - spits out ready-to-use HTML - * @param string $address The e-mail address - * @param string $subject The subject of the e-mail. OPTIONAL. - * @param string $body The main content of the e-mail. OPTIONAL and doesn't work in many e-mail clients. - * @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) - */ - - function encryptEmail($address, $subject = '', $body = '', $text = false) - { - $key = $this->makeKey($address, $subject, $body); - if ( $text ) - { - if(preg_match('/^(mailto:)?(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/', $text)) - { - // This is a mailto link and normal obfuscation should be used - $text = false; - } - } - $text1 = ( $text ) ? '' : ''; - $text2 = ( $text ) ? "$text <".$this->obfuscate_text($this->mask_address($address)).">" : $this->obfuscate_text($this->mask_address($address)); - $email = ''.$text1.''; - return $email; - } - - /** - * Replace @ symbols with " " and dots with " ". - * @param string $email An e-mail address. - * @return string - */ - - function mask_address($email) - { - $at = array(' (AT) ', ' __AT__ ', ' *AT* ', ' [AT] ', ' ', ' <__AT__> '); - $dot = array(' (DOT) ', ' __DOT__ ', ' *DOT* ', ' [DOT] ', ' ', ' <__DOT__> '); - while(strstr($email, '@')) - { - $my_at = $at[ array_rand($at) ]; - $email = str_replace_once('@', $my_at, $email); - } - while(strstr($email, '.')) - { - $my_dot = $dot[ array_rand($dot) ]; - $email = str_replace_once('.', $my_dot, $email); - } - return $email; - } - - /** - * Turn a string of text into hex-encoded HTML entities - * @param string $text the text to encode - * @return string - */ + 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); + + function __construct() + { + $i = 0; + $this->p = 0; + $this->q = 0; + while($this->p * $this->q < 255 || $this->p == $this->q) + { + $this->p = $this->primes[mt_rand(0, sizeof($this->primes)-1)]; + $this->q = $this->primes[mt_rand(0, sizeof($this->primes)-1)]; + } + } + + function testAll() { + $size = sizeof($this->primes); + + $allCharacters = ""; + for($c = 33; $c <= 126; $c++) + $allCharacters = $allCharacters . $this->fromCharCode($c); + + for($i = 0; $i < $size - 1; $i++) { + for($j = $i + 1; $j < $size; $j++) { + $this->p = $this->primes[$i]; + $this->q = $this->primes[$j]; + if($this->p*$this->q < 255) + break; + $k = $this->makeKey($allCharacters); + $encrypted = $k['X']; + $decrypted = $this->goForth($encrypted,$this->p*$this->q,$k['D']); + if($decrypted != $allCharacters) { + die('Test failed'); + } + } + } + return 'GOOD'; + } + + function charCodeAt($str, $i) + { + return ord(substr($str, $i, 1)); + } + + function fromCharCode($str) + { + return chr($str); + } + + function MakeArray($l) { + $a = Array(); + $i=0; + do { + $a[$i]=null; + $i++; + } while($i < $l); + return $a; + } + + function makeKey($addr,$subj = '',$body = '') { + $value = ""; + + if($this->p * $this->q < 255) + { + return("P*Q must be greater than 255! P*Q = " . $this->p*$this->q); + } + elseif($this->p == $this->q) + { + return("P cannot be equal to Q!"); + } + elseif($addr == "") + { + return("You must enter an address to encrypt!"); + } + else + { + // Make the key + $c = 0; + $z = ($this->p-1)*($this->q-1); + $e = 0; + $n = $this->p*$this->q; + $d = 0; + + do { + $e++; + $d = $this->getKey($this->primes[$e],$z); + } while($d==1); + $e = $this->primes[$e]; + + // Turn the string into an array of numbers < 255 + $m = $addr; + $emailLength = strlen($m); + $justEmail = ""; + $sep = ( strstr('?', $m) ) ? '&' : '?'; + if($subj != "") { + $m = $m . "{$sep}subject=" . $subj; + } + $sep = ( strstr($m, '?') ) ? '&' : '?'; + if($body != "") { + $m = $m . "{$sep}body=" . $body; + } + + $length = strlen($m); + $theString = $this->MakeArray($length); + for($i = 0; $i < $length; $i++) { + $theString[$i] = $this->charCodeAt($m, $i); + } + + // Encrypt each of the numbers + $theCode = $this->MakeArray($length); + $c = ""; + $temp = 0; + for($i = 0; $i < $length; $i++) { + if($i != 0) + $c .= " "; + $temp = $this->myMod($theString[$i],$e,$n); + $theCode[$i] = $temp; + $c .= $temp; + if($i == $emailLength - 1) + $justEmail = $c; + } + } + return Array('X'=>$justEmail, 'N'=>$n, 'D'=>$d, 'E'=>$e, 'C'=>$c, 'M'=>$m); + } + + // Finds x^e % y for large values of (x^e) + function myMod($x,$e,$y) { + if ($e % 2 == 0) { + $answer = 1; + for($i = 1; $i <= e/2; $i++) { + $temp = ($x*$x) % $y; + $answer = ($temp*$answer) % $y; + } + } else { + $answer = $x; + for($i = 1; $i <= $e/2; $i++) { + $temp = ($x*$x) % $y; + $answer = ($temp*$answer) % $y; + } + } + return $answer; + } + + + function getKey($e,$z) { + $A = 1; + $B = 0; + $C = $z; + $F = 0; + $G = 1; + $bar = $e; + // Euclid's Algorithm: + while ($bar != 0) { + $foo = floor($C/$bar); + $K = $A - $foo * $F; + $L = $B - $foo * $G; + $M = $C - $foo * $bar; + $A = $F; + $B = $G; + $C = $bar; + $F = $K; + $G = $L; + $bar = $M; + } + if ($B < 0) + { + return ($B + $z); + } + else + { + return ($B); + } + } + + function goForth($c,$n,$d) { + $c .= " "; + $length = strlen($c); + $number = 0; + $bar = 0; + $answer = ""; + + for($i = 0; $i < $length; $i++) { + $number = 0; + $bar = 0; + while($this->charCodeAt($c, $i) != 32) { + $number = $number * 10; + $number = $number + $this->charCodeAt($c, $i)-48; + $i++; + } + $answer .= $this->fromCharCode($this->decrypt($number,$n,$d)); + } + return $answer; + } + + function decrypt($c,$n,$d) { + // Split exponents up + if ($d % 2== 0) { + $bar = 1; + for($i = 1; $i <= $d/2; $i++) { + $foo = ($c*$c) % $n; + $bar = ($foo*$bar) % $n; + } + } else { + $bar = $c; + for($i = 1; $i <= $d/2; $i++) { + $foo = ($c*$c) % $n; + $bar = ($foo*$bar) % $n; + } + } + return $bar; + } + + function writeOptions() { + $size = sizeof($this->primes); + for($i = 0; $i < $size; $i++) + echo(""); + } + + function jscode() { + return ""; + } + + /** + * Wrapper - spits out ready-to-use HTML + * @param string $address The e-mail address + * @param string $subject The subject of the e-mail. OPTIONAL. + * @param string $body The main content of the e-mail. OPTIONAL and doesn't work in many e-mail clients. + * @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) + */ + + function encryptEmail($address, $subject = '', $body = '', $text = false) + { + $key = $this->makeKey($address, $subject, $body); + if ( $text ) + { + if(preg_match('/^(mailto:)?(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/', $text)) + { + // This is a mailto link and normal obfuscation should be used + $text = false; + } + } + $text1 = ( $text ) ? '' : ''; + $text2 = ( $text ) ? "$text <".$this->obfuscate_text($this->mask_address($address)).">" : $this->obfuscate_text($this->mask_address($address)); + $email = ''.$text1.''; + return $email; + } + + /** + * Replace @ symbols with " " and dots with " ". + * @param string $email An e-mail address. + * @return string + */ + + function mask_address($email) + { + $at = array(' (AT) ', ' __AT__ ', ' *AT* ', ' [AT] ', ' ', ' <__AT__> '); + $dot = array(' (DOT) ', ' __DOT__ ', ' *DOT* ', ' [DOT] ', ' ', ' <__DOT__> '); + while(strstr($email, '@')) + { + $my_at = $at[ array_rand($at) ]; + $email = str_replace_once('@', $my_at, $email); + } + while(strstr($email, '.')) + { + $my_dot = $dot[ array_rand($dot) ]; + $email = str_replace_once('.', $my_dot, $email); + } + return $email; + } + + /** + * Turn a string of text into hex-encoded HTML entities + * @param string $text the text to encode + * @return string + */ - function obfuscate_text($text) - { - $a = enano_str_split($text, 1); - $s = ''; - foreach($a as $k => $c) - { - $ch = (string)dechex(ord($a[$k])); - if(strlen($ch) < 2) $ch = '0' . $ch; - $s .= '&#x'.$ch.';'; - } - return $s; - } + function obfuscate_text($text) + { + $a = enano_str_split($text, 1); + $s = ''; + foreach($a as $k => $c) + { + $ch = (string)dechex(ord($a[$k])); + if(strlen($ch) < 2) $ch = '0' . $ch; + $s .= '&#x'.$ch.';'; + } + return $s; + } }