summaryrefslogtreecommitdiff
path: root/program/lib
diff options
context:
space:
mode:
Diffstat (limited to 'program/lib')
-rw-r--r--program/lib/Mail/mime.php733
-rw-r--r--program/lib/Mail/mimeDecode.php842
-rw-r--r--program/lib/Mail/mimePart.php351
-rw-r--r--program/lib/PEAR.php927
-rw-r--r--program/lib/des.inc218
-rw-r--r--program/lib/enriched.inc114
-rw-r--r--program/lib/html2text.inc440
-rw-r--r--program/lib/icl_commons.inc81
-rw-r--r--program/lib/imap.inc2038
-rw-r--r--program/lib/mime.inc322
-rw-r--r--program/lib/smtp.inc351
-rw-r--r--program/lib/utf7.inc384
-rw-r--r--program/lib/utf8.inc102
13 files changed, 6903 insertions, 0 deletions
diff --git a/program/lib/Mail/mime.php b/program/lib/Mail/mime.php
new file mode 100644
index 000000000..830352c42
--- /dev/null
+++ b/program/lib/Mail/mime.php
@@ -0,0 +1,733 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+// +-----------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Richard Heyes |
+// | Copyright (c) 2003-2005 The PHP Group |
+// | All rights reserved. |
+// | |
+// | Redistribution and use in source and binary forms, with or without |
+// | modification, are permitted provided that the following conditions |
+// | are met: |
+// | |
+// | o Redistributions of source code must retain the above copyright |
+// | notice, this list of conditions and the following disclaimer. |
+// | o Redistributions in binary form must reproduce the above copyright |
+// | notice, this list of conditions and the following disclaimer in the |
+// | documentation and/or other materials provided with the distribution.|
+// | o The names of the authors may not be used to endorse or promote |
+// | products derived from this software without specific prior written |
+// | permission. |
+// | |
+// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+// | |
+// +-----------------------------------------------------------------------+
+// | Author: Richard Heyes <richard@phpguru.org> |
+// | Tomas V.V.Cox <cox@idecnet.com> (port to PEAR) |
+// +-----------------------------------------------------------------------+
+//
+// $Id$
+
+require_once('PEAR.php');
+require_once('Mail/mimePart.php');
+
+/**
+ * Mime mail composer class. Can handle: text and html bodies, embedded html
+ * images and attachments.
+ * Documentation and examples of this class are avaible here:
+ * http://pear.php.net/manual/
+ *
+ * @notes This class is based on HTML Mime Mail class from
+ * Richard Heyes <richard@phpguru.org> which was based also
+ * in the mime_mail.class by Tobias Ratschiller <tobias@dnet.it> and
+ * Sascha Schumann <sascha@schumann.cx>
+ *
+ * Function _encodeHeaders() changed by Thomas Bruederli <roundcube@gmail.com>
+ * in order to be read correctly by Google Gmail
+ *
+ * @author Richard Heyes <richard.heyes@heyes-computing.net>
+ * @author Tomas V.V.Cox <cox@idecnet.com>
+ * @package Mail
+ * @access public
+ */
+class Mail_mime
+{
+ /**
+ * Contains the plain text part of the email
+ * @var string
+ */
+ var $_txtbody;
+ /**
+ * Contains the html part of the email
+ * @var string
+ */
+ var $_htmlbody;
+ /**
+ * contains the mime encoded text
+ * @var string
+ */
+ var $_mime;
+ /**
+ * contains the multipart content
+ * @var string
+ */
+ var $_multipart;
+ /**
+ * list of the attached images
+ * @var array
+ */
+ var $_html_images = array();
+ /**
+ * list of the attachements
+ * @var array
+ */
+ var $_parts = array();
+ /**
+ * Build parameters
+ * @var array
+ */
+ var $_build_params = array();
+ /**
+ * Headers for the mail
+ * @var array
+ */
+ var $_headers = array();
+ /**
+ * End Of Line sequence (for serialize)
+ * @var string
+ */
+ var $_eol;
+
+
+ /**
+ * Constructor function
+ *
+ * @access public
+ */
+ function Mail_mime($crlf = "\r\n")
+ {
+ $this->_setEOL($crlf);
+ $this->_build_params = array(
+ 'text_encoding' => '7bit',
+ 'html_encoding' => 'quoted-printable',
+ '7bit_wrap' => 998,
+ 'html_charset' => 'ISO-8859-1',
+ 'text_charset' => 'ISO-8859-1',
+ 'head_charset' => 'ISO-8859-1'
+ );
+ }
+
+ /**
+ * Wakeup (unserialize) - re-sets EOL constant
+ *
+ * @access private
+ */
+ function __wakeup()
+ {
+ $this->_setEOL($this->_eol);
+ }
+
+ /**
+ * Accessor function to set the body text. Body text is used if
+ * it's not an html mail being sent or else is used to fill the
+ * text/plain part that emails clients who don't support
+ * html should show.
+ *
+ * @param string $data Either a string or
+ * the file name with the contents
+ * @param bool $isfile If true the first param should be treated
+ * as a file name, else as a string (default)
+ * @param bool $append If true the text or file is appended to
+ * the existing body, else the old body is
+ * overwritten
+ * @return mixed true on success or PEAR_Error object
+ * @access public
+ */
+ function setTXTBody($data, $isfile = false, $append = false)
+ {
+ if (!$isfile) {
+ if (!$append) {
+ $this->_txtbody = $data;
+ } else {
+ $this->_txtbody .= $data;
+ }
+ } else {
+ $cont = $this->_file2str($data);
+ if (PEAR::isError($cont)) {
+ return $cont;
+ }
+ if (!$append) {
+ $this->_txtbody = $cont;
+ } else {
+ $this->_txtbody .= $cont;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Adds a html part to the mail
+ *
+ * @param string $data Either a string or the file name with the
+ * contents
+ * @param bool $isfile If true the first param should be treated
+ * as a file name, else as a string (default)
+ * @return mixed true on success or PEAR_Error object
+ * @access public
+ */
+ function setHTMLBody($data, $isfile = false)
+ {
+ if (!$isfile) {
+ $this->_htmlbody = $data;
+ } else {
+ $cont = $this->_file2str($data);
+ if (PEAR::isError($cont)) {
+ return $cont;
+ }
+ $this->_htmlbody = $cont;
+ }
+
+ return true;
+ }
+
+ /**
+ * Adds an image to the list of embedded images.
+ *
+ * @param string $file The image file name OR image data itself
+ * @param string $c_type The content type
+ * @param string $name The filename of the image.
+ * Only use if $file is the image data
+ * @param bool $isfilename Whether $file is a filename or not
+ * Defaults to true
+ * @return mixed true on success or PEAR_Error object
+ * @access public
+ */
+ function addHTMLImage($file, $c_type='application/octet-stream',
+ $name = '', $isfilename = true)
+ {
+ $filedata = ($isfilename === true) ? $this->_file2str($file)
+ : $file;
+ if ($isfilename === true) {
+ $filename = ($name == '' ? basename($file) : basename($name));
+ } else {
+ $filename = basename($name);
+ }
+ if (PEAR::isError($filedata)) {
+ return $filedata;
+ }
+ $this->_html_images[] = array(
+ 'body' => $filedata,
+ 'name' => $filename,
+ 'c_type' => $c_type,
+ 'cid' => md5(uniqid(time()))
+ );
+ return true;
+ }
+
+ /**
+ * Adds a file to the list of attachments.
+ *
+ * @param string $file The file name of the file to attach
+ * OR the file data itself
+ * @param string $c_type The content type
+ * @param string $name The filename of the attachment
+ * Only use if $file is the file data
+ * @param bool $isFilename Whether $file is a filename or not
+ * Defaults to true
+ * @return mixed true on success or PEAR_Error object
+ * @access public
+ */
+ function addAttachment($file, $c_type = 'application/octet-stream',
+ $name = '', $isfilename = true,
+ $encoding = 'base64')
+ {
+ $filedata = ($isfilename === true) ? $this->_file2str($file)
+ : $file;
+ if ($isfilename === true) {
+ // Force the name the user supplied, otherwise use $file
+ $filename = (!empty($name)) ? $name : $file;
+ } else {
+ $filename = $name;
+ }
+ if (empty($filename)) {
+ return PEAR::raiseError(
+ 'The supplied filename for the attachment can\'t be empty'
+ );
+ }
+ $filename = basename($filename);
+ if (PEAR::isError($filedata)) {
+ return $filedata;
+ }
+
+ $this->_parts[] = array(
+ 'body' => $filedata,
+ 'name' => $filename,
+ 'c_type' => $c_type,
+ 'encoding' => $encoding
+ );
+ return true;
+ }
+
+ /**
+ * Get the contents of the given file name as string
+ *
+ * @param string $file_name path of file to process
+ * @return string contents of $file_name
+ * @access private
+ */
+ function &_file2str($file_name)
+ {
+ if (!is_readable($file_name)) {
+ return PEAR::raiseError('File is not readable ' . $file_name);
+ }
+ if (!$fd = fopen($file_name, 'rb')) {
+ return PEAR::raiseError('Could not open ' . $file_name);
+ }
+ $cont = fread($fd, filesize($file_name));
+ fclose($fd);
+ return $cont;
+ }
+
+ /**
+ * Adds a text subpart to the mimePart object and
+ * returns it during the build process.
+ *
+ * @param mixed The object to add the part to, or
+ * null if a new object is to be created.
+ * @param string The text to add.
+ * @return object The text mimePart object
+ * @access private
+ */
+ function &_addTextPart(&$obj, $text)
+ {
+ $params['content_type'] = 'text/plain';
+ $params['encoding'] = $this->_build_params['text_encoding'];
+ $params['charset'] = $this->_build_params['text_charset'];
+ if (is_object($obj)) {
+ return $obj->addSubpart($text, $params);
+ } else {
+ return new Mail_mimePart($text, $params);
+ }
+ }
+
+ /**
+ * Adds a html subpart to the mimePart object and
+ * returns it during the build process.
+ *
+ * @param mixed The object to add the part to, or
+ * null if a new object is to be created.
+ * @return object The html mimePart object
+ * @access private
+ */
+ function &_addHtmlPart(&$obj)
+ {
+ $params['content_type'] = 'text/html';
+ $params['encoding'] = $this->_build_params['html_encoding'];
+ $params['charset'] = $this->_build_params['html_charset'];
+ if (is_object($obj)) {
+ return $obj->addSubpart($this->_htmlbody, $params);
+ } else {
+ return new Mail_mimePart($this->_htmlbody, $params);
+ }
+ }
+
+ /**
+ * Creates a new mimePart object, using multipart/mixed as
+ * the initial content-type and returns it during the
+ * build process.
+ *
+ * @return object The multipart/mixed mimePart object
+ * @access private
+ */
+ function &_addMixedPart()
+ {
+ $params['content_type'] = 'multipart/mixed';
+ return new Mail_mimePart('', $params);
+ }
+
+ /**
+ * Adds a multipart/alternative part to a mimePart
+ * object (or creates one), and returns it during
+ * the build process.
+ *
+ * @param mixed The object to add the part to, or
+ * null if a new object is to be created.
+ * @return object The multipart/mixed mimePart object
+ * @access private
+ */
+ function &_addAlternativePart(&$obj)
+ {
+ $params['content_type'] = 'multipart/alternative';
+ if (is_object($obj)) {
+ return $obj->addSubpart('', $params);
+ } else {
+ return new Mail_mimePart('', $params);
+ }
+ }
+
+ /**
+ * Adds a multipart/related part to a mimePart
+ * object (or creates one), and returns it during
+ * the build process.
+ *
+ * @param mixed The object to add the part to, or
+ * null if a new object is to be created
+ * @return object The multipart/mixed mimePart object
+ * @access private
+ */
+ function &_addRelatedPart(&$obj)
+ {
+ $params['content_type'] = 'multipart/related';
+ if (is_object($obj)) {
+ return $obj->addSubpart('', $params);
+ } else {
+ return new Mail_mimePart('', $params);
+ }
+ }
+
+ /**
+ * Adds an html image subpart to a mimePart object
+ * and returns it during the build process.
+ *
+ * @param object The mimePart to add the image to
+ * @param array The image information
+ * @return object The image mimePart object
+ * @access private
+ */
+ function &_addHtmlImagePart(&$obj, $value)
+ {
+ $params['content_type'] = $value['c_type'];
+ $params['encoding'] = 'base64';
+ $params['disposition'] = 'inline';
+ $params['dfilename'] = $value['name'];
+ $params['cid'] = $value['cid'];
+ $obj->addSubpart($value['body'], $params);
+ }
+
+ /**
+ * Adds an attachment subpart to a mimePart object
+ * and returns it during the build process.
+ *
+ * @param object The mimePart to add the image to
+ * @param array The attachment information
+ * @return object The image mimePart object
+ * @access private
+ */
+ function &_addAttachmentPart(&$obj, $value)
+ {
+ $params['content_type'] = $value['c_type'];
+ $params['encoding'] = $value['encoding'];
+ $params['disposition'] = 'attachment';
+ $params['dfilename'] = $value['name'];
+ $obj->addSubpart($value['body'], $params);
+ }
+
+ /**
+ * Builds the multipart message from the list ($this->_parts) and
+ * returns the mime content.
+ *
+ * @param array Build parameters that change the way the email
+ * is built. Should be associative. Can contain:
+ * text_encoding - What encoding to use for plain text
+ * Default is 7bit
+ * html_encoding - What encoding to use for html
+ * Default is quoted-printable
+ * 7bit_wrap - Number of characters before text is
+ * wrapped in 7bit encoding
+ * Default is 998
+ * html_charset - The character set to use for html.
+ * Default is iso-8859-1
+ * text_charset - The character set to use for text.
+ * Default is iso-8859-1
+ * head_charset - The character set to use for headers.
+ * Default is iso-8859-1
+ * @return string The mime content
+ * @access public
+ */
+ function &get($build_params = null)
+ {
+ if (isset($build_params)) {
+ while (list($key, $value) = each($build_params)) {
+ $this->_build_params[$key] = $value;
+ }
+ }
+
+ if (!empty($this->_html_images) AND isset($this->_htmlbody)) {
+ foreach ($this->_html_images as $value) {
+ $regex = '#src\s*=\s*(["\']?)' . preg_quote($value['name']) .
+ '(["\'])?#';
+ $rep = 'src=\1cid:' . $value['cid'] .'\2';
+ $this->_htmlbody = preg_replace($regex, $rep,
+ $this->_htmlbody
+ );
+ }
+ }
+
+ $null = null;
+ $attachments = !empty($this->_parts) ? true : false;
+ $html_images = !empty($this->_html_images) ? true : false;
+ $html = !empty($this->_htmlbody) ? true : false;
+ $text = (!$html AND !empty($this->_txtbody)) ? true : false;
+
+ switch (true) {
+ case $text AND !$attachments:
+ $message =& $this->_addTextPart($null, $this->_txtbody);
+ break;
+
+ case !$text AND !$html AND $attachments:
+ $message =& $this->_addMixedPart();
+ for ($i = 0; $i < count($this->_parts); $i++) {
+ $this->_addAttachmentPart($message, $this->_parts[$i]);
+ }
+ break;
+
+ case $text AND $attachments:
+ $message =& $this->_addMixedPart();
+ $this->_addTextPart($message, $this->_txtbody);
+ for ($i = 0; $i < count($this->_parts); $i++) {
+ $this->_addAttachmentPart($message, $this->_parts[$i]);
+ }
+ break;
+
+ case $html AND !$attachments AND !$html_images:
+ if (isset($this->_txtbody)) {
+ $message =& $this->_addAlternativePart($null);
+ $this->_addTextPart($message, $this->_txtbody);
+ $this->_addHtmlPart($message);
+ } else {
+ $message =& $this->_addHtmlPart($null);
+ }
+ break;
+
+ case $html AND !$attachments AND $html_images:
+ if (isset($this->_txtbody)) {
+ $message =& $this->_addAlternativePart($null);
+ $this->_addTextPart($message, $this->_txtbody);
+ $related =& $this->_addRelatedPart($message);
+ } else {
+ $message =& $this->_addRelatedPart($null);
+ $related =& $message;
+ }
+ $this->_addHtmlPart($related);
+ for ($i = 0; $i < count($this->_html_images); $i++) {
+ $this->_addHtmlImagePart($related, $this->_html_images[$i]);
+ }
+ break;
+
+ case $html AND $attachments AND !$html_images:
+ $message =& $this->_addMixedPart();
+ if (isset($this->_txtbody)) {
+ $alt =& $this->_addAlternativePart($message);
+ $this->_addTextPart($alt, $this->_txtbody);
+ $this->_addHtmlPart($alt);
+ } else {
+ $this->_addHtmlPart($message);
+ }
+ for ($i = 0; $i < count($this->_parts); $i++) {
+ $this->_addAttachmentPart($message, $this->_parts[$i]);
+ }
+ break;
+
+ case $html AND $attachments AND $html_images:
+ $message =& $this->_addMixedPart();
+ if (isset($this->_txtbody)) {
+ $alt =& $this->_addAlternativePart($message);
+ $this->_addTextPart($alt, $this->_txtbody);
+ $rel =& $this->_addRelatedPart($alt);
+ } else {
+ $rel =& $this->_addRelatedPart($message);
+ }
+ $this->_addHtmlPart($rel);
+ for ($i = 0; $i < count($this->_html_images); $i++) {
+ $this->_addHtmlImagePart($rel, $this->_html_images[$i]);
+ }
+ for ($i = 0; $i < count($this->_parts); $i++) {
+ $this->_addAttachmentPart($message, $this->_parts[$i]);
+ }
+ break;
+
+ }
+
+ if (isset($message)) {
+ $output = $message->encode();
+ $this->_headers = array_merge($this->_headers,
+ $output['headers']);
+ return $output['body'];
+
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Returns an array with the headers needed to prepend to the email
+ * (MIME-Version and Content-Type). Format of argument is:
+ * $array['header-name'] = 'header-value';
+ *
+ * @param array $xtra_headers Assoc array with any extra headers.
+ * Optional.
+ * @return array Assoc array with the mime headers
+ * @access public
+ */
+ function &headers($xtra_headers = null)
+ {
+ // Content-Type header should already be present,
+ // So just add mime version header
+ $headers['MIME-Version'] = '1.0';
+ if (isset($xtra_headers)) {
+ $headers = array_merge($headers, $xtra_headers);
+ }
+ $this->_headers = array_merge($headers, $this->_headers);
+
+ return $this->_encodeHeaders($this->_headers);
+ }
+
+ /**
+ * Get the text version of the headers
+ * (usefull if you want to use the PHP mail() function)
+ *
+ * @param array $xtra_headers Assoc array with any extra headers.
+ * Optional.
+ * @return string Plain text headers
+ * @access public
+ */
+ function txtHeaders($xtra_headers = null)
+ {
+ $headers = $this->headers($xtra_headers);
+ $ret = '';
+ foreach ($headers as $key => $val) {
+ $ret .= "$key: $val" . MAIL_MIME_CRLF;
+ }
+ return $ret;
+ }
+
+ /**
+ * Sets the Subject header
+ *
+ * @param string $subject String to set the subject to
+ * access public
+ */
+ function setSubject($subject)
+ {
+ $this->_headers['Subject'] = $subject;
+ }
+
+ /**
+ * Set an email to the From (the sender) header
+ *
+ * @param string $email The email direction to add
+ * @access public
+ */
+ function setFrom($email)
+ {
+ $this->_headers['From'] = $email;
+ }
+
+ /**
+ * Add an email to the Cc (carbon copy) header
+ * (multiple calls to this method are allowed)
+ *
+ * @param string $email The email direction to add
+ * @access public
+ */
+ function addCc($email)
+ {
+ if (isset($this->_headers['Cc'])) {
+ $this->_headers['Cc'] .= ", $email";
+ } else {
+ $this->_headers['Cc'] = $email;
+ }
+ }
+
+ /**
+ * Add an email to the Bcc (blank carbon copy) header
+ * (multiple calls to this method are allowed)
+ *
+ * @param string $email The email direction to add
+ * @access public
+ */
+ function addBcc($email)
+ {
+ if (isset($this->_headers['Bcc'])) {
+ $this->_headers['Bcc'] .= ", $email";
+ } else {
+ $this->_headers['Bcc'] = $email;
+ }
+ }
+
+ /**
+ * Encodes a header as per RFC2047
+ *
+ * @param string $input The header data to encode
+ * @return string Encoded data
+ * @access private
+ */
+ function _encodeHeaders($input)
+ {
+ $enc_prefix = '=?' . $this->_build_params['head_charset'] . '?Q?';
+ foreach ($input as $hdr_name => $hdr_value) {
+ if (preg_match('/(\w*[\x80-\xFF]+\w*)/', $hdr_value)) {
+ $enc_value = preg_replace('/([\x80-\xFF])/e', '"=".strtoupper(dechex(ord("\1")))', $hdr_value);
+ // check for <email address> in string
+ if (preg_match('/<[a-z0-9\-\.\+\_]+@[a-z0-9]([a-z0-9\-].?)*[a-z0-9]\\.[a-z]{2,5}>/i', $enc_value) && ($p = strrpos($enc_value, '<'))) {
+ $hdr_value = $enc_prefix . substr($enc_value, 0, $p-1) . '?= ' . substr($enc_value, $p, strlen($enc_value)-$p);
+ } else {
+ $hdr_value = $enc_prefix . $enc_value . '?=';
+ }
+ }
+ $input[$hdr_name] = $hdr_value;
+ }
+
+ return $input;
+ }
+
+ /* replaced 2005/07/08 by roundcube@gmail.com
+
+ function _encodeHeaders_old($input)
+ {
+ foreach ($input as $hdr_name => $hdr_value) {
+ preg_match_all('/(\w*[\x80-\xFF]+\w*)/', $hdr_value, $matches);
+ foreach ($matches[1] as $value) {
+ $replacement = preg_replace('/([\x80-\xFF])/e',
+ '"=" .
+ strtoupper(dechex(ord("\1")))',
+ $value);
+ $hdr_value = str_replace($value, '=?' .
+ $this->_build_params['head_charset'] .
+ '?Q?' . $replacement . '?=',
+ $hdr_value);
+ }
+ $input[$hdr_name] = $hdr_value;
+ }
+
+ return $input;
+ }
+ */
+
+ /**
+ * Set the object's end-of-line and define the constant if applicable
+ *
+ * @param string $eol End Of Line sequence
+ * @access private
+ */
+ function _setEOL($eol)
+ {
+ $this->_eol = $eol;
+ if (!defined('MAIL_MIME_CRLF')) {
+ define('MAIL_MIME_CRLF', $this->_eol, true);
+ }
+ }
+
+
+
+} // End of class
+?>
diff --git a/program/lib/Mail/mimeDecode.php b/program/lib/Mail/mimeDecode.php
new file mode 100644
index 000000000..92827b727
--- /dev/null
+++ b/program/lib/Mail/mimeDecode.php
@@ -0,0 +1,842 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+// +-----------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Richard Heyes |
+// | Copyright (c) 2003-2005 The PHP Group |
+// | All rights reserved. |
+// | |
+// | Redistribution and use in source and binary forms, with or without |
+// | modification, are permitted provided that the following conditions |
+// | are met: |
+// | |
+// | o Redistributions of source code must retain the above copyright |
+// | notice, this list of conditions and the following disclaimer. |
+// | o Redistributions in binary form must reproduce the above copyright |
+// | notice, this list of conditions and the following disclaimer in the |
+// | documentation and/or other materials provided with the distribution.|
+// | o The names of the authors may not be used to endorse or promote |
+// | products derived from this software without specific prior written |
+// | permission. |
+// | |
+// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+// | |
+// +-----------------------------------------------------------------------+
+// | Author: Richard Heyes <richard@phpguru.org> |
+// +-----------------------------------------------------------------------+
+
+require_once 'PEAR.php';
+
+/**
+* +----------------------------- IMPORTANT ------------------------------+
+* | Usage of this class compared to native php extensions such as |
+* | mailparse or imap, is slow and may be feature deficient. If available|
+* | you are STRONGLY recommended to use the php extensions. |
+* +----------------------------------------------------------------------+
+*
+* Mime Decoding class
+*
+* This class will parse a raw mime email and return
+* the structure. Returned structure is similar to
+* that returned by imap_fetchstructure().
+*
+* USAGE: (assume $input is your raw email)
+*
+* $decode = new Mail_mimeDecode($input, "\r\n");
+* $structure = $decode->decode();
+* print_r($structure);
+*
+* Or statically:
+*
+* $params['input'] = $input;
+* $structure = Mail_mimeDecode::decode($params);
+* print_r($structure);
+*
+* TODO:
+* o Implement multipart/appledouble
+* o UTF8: ???
+
+ > 4. We have also found a solution for decoding the UTF-8
+ > headers. Therefore I made the following function:
+ >
+ > function decode_utf8($txt) {
+ > $trans=array("&#8216;"=>"õ","ű"=>"û","Ő"=>"&#8226;","Ű"
+ =>"&#8250;");
+ > $txt=strtr($txt,$trans);
+ > return(utf8_decode($txt));
+ > }
+ >
+ > And I have inserted the following line to the class:
+ >
+ > if (strtolower($charset)=="utf-8") $text=decode_utf8($text);
+ >
+ > ... before the following one in the "_decodeHeader" function:
+ >
+ > $input = str_replace($encoded, $text, $input);
+ >
+ > This way from now on it can easily decode the UTF-8 headers too.
+
+*
+* @author Richard Heyes <richard@phpguru.org>
+* @version $Revision$
+* @package Mail
+*/
+class Mail_mimeDecode extends PEAR
+{
+ /**
+ * The raw email to decode
+ * @var string
+ */
+ var $_input;
+
+ /**
+ * The header part of the input
+ * @var string
+ */
+ var $_header;
+
+ /**
+ * The body part of the input
+ * @var string
+ */
+ var $_body;
+
+ /**
+ * If an error occurs, this is used to store the message
+ * @var string
+ */
+ var $_error;
+
+ /**
+ * Flag to determine whether to include bodies in the
+ * returned object.
+ * @var boolean
+ */
+ var $_include_bodies;
+
+ /**
+ * Flag to determine whether to decode bodies
+ * @var boolean
+ */
+ var $_decode_bodies;
+
+ /**
+ * Flag to determine whether to decode headers
+ * @var boolean
+ */
+ var $_decode_headers;
+
+ /**
+ * Constructor.
+ *
+ * Sets up the object, initialise the variables, and splits and
+ * stores the header and body of the input.
+ *
+ * @param string The input to decode
+ * @access public
+ */
+ function Mail_mimeDecode($input)
+ {
+ list($header, $body) = $this->_splitBodyHeader($input);
+
+ $this->_input = $input;
+ $this->_header = $header;
+ $this->_body = $body;
+ $this->_decode_bodies = false;
+ $this->_include_bodies = true;
+ }
+
+ /**
+ * Begins the decoding process. If called statically
+ * it will create an object and call the decode() method
+ * of it.
+ *
+ * @param array An array of various parameters that determine
+ * various things:
+ * include_bodies - Whether to include the body in the returned
+ * object.
+ * decode_bodies - Whether to decode the bodies
+ * of the parts. (Transfer encoding)
+ * decode_headers - Whether to decode headers
+ * input - If called statically, this will be treated
+ * as the input
+ * @return object Decoded results
+ * @access public
+ */
+ function decode($params = null)
+ {
+ // determine if this method has been called statically
+ $isStatic = !(isset($this) && get_class($this) == __CLASS__);
+
+ // Have we been called statically?
+ // If so, create an object and pass details to that.
+ if ($isStatic AND isset($params['input'])) {
+
+ $obj = new Mail_mimeDecode($params['input']);
+ $structure = $obj->decode($params);
+
+ // Called statically but no input
+ } elseif ($isStatic) {
+ return PEAR::raiseError('Called statically and no input given');
+
+ // Called via an object
+ } else {
+ $this->_include_bodies = isset($params['include_bodies']) ?
+ $params['include_bodies'] : false;
+ $this->_decode_bodies = isset($params['decode_bodies']) ?
+ $params['decode_bodies'] : false;
+ $this->_decode_headers = isset($params['decode_headers']) ?
+ $params['decode_headers'] : false;
+
+ $structure = $this->_decode($this->_header, $this->_body);
+ if ($structure === false) {
+ $structure = $this->raiseError($this->_error);
+ }
+ }
+
+ return $structure;
+ }
+
+ /**
+ * Performs the decoding. Decodes the body string passed to it
+ * If it finds certain content-types it will call itself in a
+ * recursive fashion
+ *
+ * @param string Header section
+ * @param string Body section
+ * @return object Results of decoding process
+ * @access private
+ */
+ function _decode($headers, $body, $default_ctype = 'text/plain')
+ {
+ $return = new stdClass;
+ $return->headers = array();
+ $headers = $this->_parseHeaders($headers);
+
+ foreach ($headers as $value) {
+ if (isset($return->headers[strtolower($value['name'])]) AND !is_array($return->headers[strtolower($value['name'])])) {
+ $return->headers[strtolower($value['name'])] = array($return->headers[strtolower($value['name'])]);
+ $return->headers[strtolower($value['name'])][] = $value['value'];
+
+ } elseif (isset($return->headers[strtolower($value['name'])])) {
+ $return->headers[strtolower($value['name'])][] = $value['value'];
+
+ } else {
+ $return->headers[strtolower($value['name'])] = $value['value'];
+ }
+ }
+
+ reset($headers);
+ while (list($key, $value) = each($headers)) {
+ $headers[$key]['name'] = strtolower($headers[$key]['name']);
+ switch ($headers[$key]['name']) {
+
+ case 'content-type':
+ $content_type = $this->_parseHeaderValue($headers[$key]['value']);
+
+ if (preg_match('/([0-9a-z+.-]+)\/([0-9a-z+.-]+)/i', $content_type['value'], $regs)) {
+ $return->ctype_primary = $regs[1];
+ $return->ctype_secondary = $regs[2];
+ }
+
+ if (isset($content_type['other'])) {
+ while (list($p_name, $p_value) = each($content_type['other'])) {
+ $return->ctype_parameters[$p_name] = $p_value;
+ }
+ }
+ break;
+
+ case 'content-disposition':
+ $content_disposition = $this->_parseHeaderValue($headers[$key]['value']);
+ $return->disposition = $content_disposition['value'];
+ if (isset($content_disposition['other'])) {
+ while (list($p_name, $p_value) = each($content_disposition['other'])) {
+ $return->d_parameters[$p_name] = $p_value;
+ }
+ }
+ break;
+
+ case 'content-transfer-encoding':
+ $content_transfer_encoding = $this->_parseHeaderValue($headers[$key]['value']);
+ break;
+ }
+ }
+
+ if (isset($content_type)) {
+ switch (strtolower($content_type['value'])) {
+ case 'text/plain':
+ $encoding = isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit';
+ $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body) : null;
+ break;
+
+ case 'text/html':
+ $encoding = isset($content_transfer_encoding) ? $content_transfer_encoding['value'] : '7bit';
+ $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $encoding) : $body) : null;
+ break;
+
+ case 'multipart/parallel':
+ case 'multipart/report': // RFC1892
+ case 'multipart/signed': // PGP
+ case 'multipart/digest':
+ case 'multipart/alternative':
+ case 'multipart/related':
+ case 'multipart/mixed':
+ if(!isset($content_type['other']['boundary'])){
+ $this->_error = 'No boundary found for ' . $content_type['value'] . ' part';
+ return false;
+ }
+
+ $default_ctype = (strtolower($content_type['value']) === 'multipart/digest') ? 'message/rfc822' : 'text/plain';
+ $parts = $this->_boundarySplit($body, $content_type['other']['boundary']);
+ for ($i = 0; $i < count($parts); $i++) {
+ list($part_header, $part_body) = $this->_splitBodyHeader($parts[$i]);
+ $part = $this->_decode($part_header, $part_body, $default_ctype);
+ if($part === false)
+ $part = $this->raiseError($this->_error);
+ $return->parts[] = $part;
+ }
+ break;
+
+ case 'message/rfc822':
+ $obj = &new Mail_mimeDecode($body);
+ $return->parts[] = $obj->decode(array('include_bodies' => $this->_include_bodies,
+ 'decode_bodies' => $this->_decode_bodies,
+ 'decode_headers' => $this->_decode_headers));
+ unset($obj);
+ break;
+
+ default:
+ if(!isset($content_transfer_encoding['value']))
+ $content_transfer_encoding['value'] = '7bit';
+ $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body, $content_transfer_encoding['value']) : $body) : null;
+ break;
+ }
+
+ } else {
+ $ctype = explode('/', $default_ctype);
+ $return->ctype_primary = $ctype[0];
+ $return->ctype_secondary = $ctype[1];
+ $this->_include_bodies ? $return->body = ($this->_decode_bodies ? $this->_decodeBody($body) : $body) : null;
+ }
+
+ return $return;
+ }
+
+ /**
+ * Given the output of the above function, this will return an
+ * array of references to the parts, indexed by mime number.
+ *
+ * @param object $structure The structure to go through
+ * @param string $mime_number Internal use only.
+ * @return array Mime numbers
+ */
+ function &getMimeNumbers(&$structure, $no_refs = false, $mime_number = '', $prepend = '')
+ {
+ $return = array();
+ if (!empty($structure->parts)) {
+ if ($mime_number != '') {
+ $structure->mime_id = $prepend . $mime_number;
+ $return[$prepend . $mime_number] = &$structure;
+ }
+ for ($i = 0; $i < count($structure->parts); $i++) {
+
+
+ if (!empty($structure->headers['content-type']) AND substr(strtolower($structure->headers['content-type']), 0, 8) == 'message/') {
+ $prepend = $prepend . $mime_number . '.';
+ $_mime_number = '';
+ } else {
+ $_mime_number = ($mime_number == '' ? $i + 1 : sprintf('%s.%s', $mime_number, $i + 1));
+ }
+
+ $arr = &Mail_mimeDecode::getMimeNumbers($structure->parts[$i], $no_refs, $_mime_number, $prepend);
+ foreach ($arr as $key => $val) {
+ $no_refs ? $return[$key] = '' : $return[$key] = &$arr[$key];
+ }
+ }
+ } else {
+ if ($mime_number == '') {
+ $mime_number = '1';
+ }
+ $structure->mime_id = $prepend . $mime_number;
+ $no_refs ? $return[$prepend . $mime_number] = '' : $return[$prepend . $mime_number] = &$structure;
+ }
+
+ return $return;
+ }
+
+ /**
+ * Given a string containing a header and body
+ * section, this function will split them (at the first
+ * blank line) and return them.
+ *
+ * @param string Input to split apart
+ * @return array Contains header and body section
+ * @access private
+ */
+ function _splitBodyHeader($input)
+ {
+ if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $input, $match)) {
+ return array($match[1], $match[2]);
+ }
+ $this->_error = 'Could not split header and body';
+ return false;
+ }
+
+ /**
+ * Parse headers given in $input and return
+ * as assoc array.
+ *
+ * @param string Headers to parse
+ * @return array Contains parsed headers
+ * @access private
+ */
+ function _parseHeaders($input)
+ {
+
+ if ($input !== '') {
+ // Unfold the input
+ $input = preg_replace("/\r?\n/", "\r\n", $input);
+ $input = preg_replace("/\r\n(\t| )+/", ' ', $input);
+ $headers = explode("\r\n", trim($input));
+
+ foreach ($headers as $value) {
+ $hdr_name = substr($value, 0, $pos = strpos($value, ':'));
+ $hdr_value = substr($value, $pos+1);
+ if($hdr_value[0] == ' ')
+ $hdr_value = substr($hdr_value, 1);
+
+ $return[] = array(
+ 'name' => $hdr_name,
+ 'value' => $this->_decode_headers ? $this->_decodeHeader($hdr_value) : $hdr_value
+ );
+ }
+ } else {
+ $return = array();
+ }
+
+ return $return;
+ }
+
+ /**
+ * Function to parse a header value,
+ * extract first part, and any secondary
+ * parts (after ;) This function is not as
+ * robust as it could be. Eg. header comments
+ * in the wrong place will probably break it.
+ *
+ * @param string Header value to parse
+ * @return array Contains parsed result
+ * @access private
+ */
+ function _parseHeaderValue($input)
+ {
+
+ if (($pos = strpos($input, ';')) !== false) {
+
+ $return['value'] = trim(substr($input, 0, $pos));
+ $input = trim(substr($input, $pos+1));
+
+ if (strlen($input) > 0) {
+
+ // This splits on a semi-colon, if there's no preceeding backslash
+ // Now works with quoted values; had to glue the \; breaks in PHP
+ // the regex is already bordering on incomprehensible
+ $splitRegex = '/([^;\'"]*[\'"]([^\'"]*([^\'"]*)*)[\'"][^;\'"]*|([^;]+))(;|$)/';
+ preg_match_all($splitRegex, $input, $matches);
+ $parameters = array();
+ for ($i=0; $i<count($matches[0]); $i++) {
+ $param = $matches[0][$i];
+ while (substr($param, -2) == '\;') {
+ $param .= $matches[0][++$i];
+ }
+ $parameters[] = $param;
+ }
+
+ for ($i = 0; $i < count($parameters); $i++) {
+ $param_name = trim(substr($parameters[$i], 0, $pos = strpos($parameters[$i], '=')), "'\";\t\\ ");
+ $param_value = trim(str_replace('\;', ';', substr($parameters[$i], $pos + 1)), "'\";\t\\ ");
+ if ($param_value[0] == '"') {
+ $param_value = substr($param_value, 1, -1);
+ }
+ $return['other'][$param_name] = $param_value;
+ $return['other'][strtolower($param_name)] = $param_value;
+ }
+ }
+ } else {
+ $return['value'] = trim($input);
+ }
+
+ return $return;
+ }
+
+ /**
+ * This function splits the input based
+ * on the given boundary
+ *
+ * @param string Input to parse
+ * @return array Contains array of resulting mime parts
+ * @access private
+ */
+ function _boundarySplit($input, $boundary)
+ {
+ $parts = array();
+
+ $bs_possible = substr($boundary, 2, -2);
+ $bs_check = '\"' . $bs_possible . '\"';
+
+ if ($boundary == $bs_check) {
+ $boundary = $bs_possible;
+ }
+
+ $tmp = explode('--' . $boundary, $input);
+ $count = count($tmp);
+
+ // when boundaries are set correctly we should have at least 3 parts;
+ // if not, return the last one (tbr)
+ if ($count<3)
+ return array($tmp[$count-1]);
+
+ for ($i = 1; $i < $count - 1; $i++) {
+ $parts[] = $tmp[$i];
+ }
+
+ return $parts;
+ }
+
+ /**
+ * Given a header, this function will decode it
+ * according to RFC2047. Probably not *exactly*
+ * conformant, but it does pass all the given
+ * examples (in RFC2047).
+ *
+ * @param string Input header value to decode
+ * @return string Decoded header value
+ * @access private
+ */
+ function _decodeHeader($input)
+ {
+ // Remove white space between encoded-words
+ $input = preg_replace('/(=\?[^?]+\?(q|b)\?[^?]*\?=)(\s)+=\?/i', '\1=?', $input);
+
+ // For each encoded-word...
+ while (preg_match('/(=\?([^?]+)\?(q|b)\?([^?]*)\?=)/i', $input, $matches)) {
+
+ $encoded = $matches[1];
+ $charset = $matches[2];
+ $encoding = $matches[3];
+ $text = $matches[4];
+
+ switch (strtolower($encoding)) {
+ case 'b':
+ $text = base64_decode($text);
+ break;
+
+ case 'q':
+ $text = str_replace('_', ' ', $text);
+ preg_match_all('/=([a-f0-9]{2})/i', $text, $matches);
+ foreach($matches[1] as $value)
+ $text = str_replace('='.$value, chr(hexdec($value)), $text);
+ break;
+ }
+
+ $input = str_replace($encoded, $text, $input);
+ }
+
+ return $input;
+ }
+
+ /**
+ * Given a body string and an encoding type,
+ * this function will decode and return it.
+ *
+ * @param string Input body to decode
+ * @param string Encoding type to use.
+ * @return string Decoded body
+ * @access private
+ */
+ function _decodeBody($input, $encoding = '7bit')
+ {
+ switch (strtolower($encoding)) {
+ case '7bit':
+ return $input;
+ break;
+
+ case 'quoted-printable':
+ return $this->_quotedPrintableDecode($input);
+ break;
+
+ case 'base64':
+ return base64_decode($input);
+ break;
+
+ default:
+ return $input;
+ }
+ }
+
+ /**
+ * Given a quoted-printable string, this
+ * function will decode and return it.
+ *
+ * @param string Input body to decode
+ * @return string Decoded body
+ * @access private
+ */
+ function _quotedPrintableDecode($input)
+ {
+ // Remove soft line breaks
+ $input = preg_replace("/=\r?\n/", '', $input);
+
+ // Replace encoded characters
+ $input = preg_replace('/=([a-f0-9]{2})/ie', "chr(hexdec('\\1'))", $input);
+
+ return $input;
+ }
+
+ /**
+ * Checks the input for uuencoded files and returns
+ * an array of them. Can be called statically, eg:
+ *
+ * $files =& Mail_mimeDecode::uudecode($some_text);
+ *
+ * It will check for the begin 666 ... end syntax
+ * however and won't just blindly decode whatever you
+ * pass it.
+ *
+ * @param string Input body to look for attahcments in
+ * @return array Decoded bodies, filenames and permissions
+ * @access public
+ * @author Unknown
+ */
+ function &uudecode($input)
+ {
+ // Find all uuencoded sections
+ preg_match_all("/begin ([0-7]{3}) (.+)\r?\n(.+)\r?\nend/Us", $input, $matches);
+
+ for ($j = 0; $j < count($matches[3]); $j++) {
+
+ $str = $matches[3][$j];
+ $filename = $matches[2][$j];
+ $fileperm = $matches[1][$j];
+
+ $file = '';
+ $str = preg_split("/\r?\n/", trim($str));
+ $strlen = count($str);
+
+ for ($i = 0; $i < $strlen; $i++) {
+ $pos = 1;
+ $d = 0;
+ $len=(int)(((ord(substr($str[$i],0,1)) -32) - ' ') & 077);
+
+ while (($d + 3 <= $len) AND ($pos + 4 <= strlen($str[$i]))) {
+ $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20);
+ $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20);
+ $c2 = (ord(substr($str[$i],$pos+2,1)) ^ 0x20);
+ $c3 = (ord(substr($str[$i],$pos+3,1)) ^ 0x20);
+ $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4));
+
+ $file .= chr(((($c1 - ' ') & 077) << 4) | ((($c2 - ' ') & 077) >> 2));
+
+ $file .= chr(((($c2 - ' ') & 077) << 6) | (($c3 - ' ') & 077));
+
+ $pos += 4;
+ $d += 3;
+ }
+
+ if (($d + 2 <= $len) && ($pos + 3 <= strlen($str[$i]))) {
+ $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20);
+ $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20);
+ $c2 = (ord(substr($str[$i],$pos+2,1)) ^ 0x20);
+ $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4));
+
+ $file .= chr(((($c1 - ' ') & 077) << 4) | ((($c2 - ' ') & 077) >> 2));
+
+ $pos += 3;
+ $d += 2;
+ }
+
+ if (($d + 1 <= $len) && ($pos + 2 <= strlen($str[$i]))) {
+ $c0 = (ord(substr($str[$i],$pos,1)) ^ 0x20);
+ $c1 = (ord(substr($str[$i],$pos+1,1)) ^ 0x20);
+ $file .= chr(((($c0 - ' ') & 077) << 2) | ((($c1 - ' ') & 077) >> 4));
+
+ }
+ }
+ $files[] = array('filename' => $filename, 'fileperm' => $fileperm, 'filedata' => $file);
+ }
+
+ return $files;
+ }
+
+ /**
+ * getSendArray() returns the arguments required for Mail::send()
+ * used to build the arguments for a mail::send() call
+ *
+ * Usage:
+ * $mailtext = Full email (for example generated by a template)
+ * $decoder = new Mail_mimeDecode($mailtext);
+ * $parts = $decoder->getSendArray();
+ * if (!PEAR::isError($parts) {
+ * list($recipents,$headers,$body) = $parts;
+ * $mail = Mail::factory('smtp');
+ * $mail->send($recipents,$headers,$body);
+ * } else {
+ * echo $parts->message;
+ * }
+ * @return mixed array of recipeint, headers,body or Pear_Error
+ * @access public
+ * @author Alan Knowles <alan@akbkhome.com>
+ */
+ function getSendArray()
+ {
+ // prevent warning if this is not set
+ $this->_decode_headers = FALSE;
+ $headerlist =$this->_parseHeaders($this->_header);
+ $to = "";
+ if (!$headerlist) {
+ return $this->raiseError("Message did not contain headers");
+ }
+ foreach($headerlist as $item) {
+ $header[$item['name']] = $item['value'];
+ switch (strtolower($item['name'])) {
+ case "to":
+ case "cc":
+ case "bcc":
+ $to = ",".$item['value'];
+ default:
+ break;
+ }
+ }
+ if ($to == "") {
+ return $this->raiseError("Message did not contain any recipents");
+ }
+ $to = substr($to,1);
+ return array($to,$header,$this->_body);
+ }
+
+ /**
+ * Returns a xml copy of the output of
+ * Mail_mimeDecode::decode. Pass the output in as the
+ * argument. This function can be called statically. Eg:
+ *
+ * $output = $obj->decode();
+ * $xml = Mail_mimeDecode::getXML($output);
+ *
+ * The DTD used for this should have been in the package. Or
+ * alternatively you can get it from cvs, or here:
+ * http://www.phpguru.org/xmail/xmail.dtd.
+ *
+ * @param object Input to convert to xml. This should be the
+ * output of the Mail_mimeDecode::decode function
+ * @return string XML version of input
+ * @access public
+ */
+ function getXML($input)
+ {
+ $crlf = "\r\n";
+ $output = '<?xml version=\'1.0\'?>' . $crlf .
+ '<!DOCTYPE email SYSTEM "http://www.phpguru.org/xmail/xmail.dtd">' . $crlf .
+ '<email>' . $crlf .
+ Mail_mimeDecode::_getXML($input) .
+ '</email>';
+
+ return $output;
+ }
+
+ /**
+ * Function that does the actual conversion to xml. Does a single
+ * mimepart at a time.
+ *
+ * @param object Input to convert to xml. This is a mimepart object.
+ * It may or may not contain subparts.
+ * @param integer Number of tabs to indent
+ * @return string XML version of input
+ * @access private
+ */
+ function _getXML($input, $indent = 1)
+ {
+ $htab = "\t";
+ $crlf = "\r\n";
+ $output = '';
+ $headers = @(array)$input->headers;
+
+ foreach ($headers as $hdr_name => $hdr_value) {
+
+ // Multiple headers with this name
+ if (is_array($headers[$hdr_name])) {
+ for ($i = 0; $i < count($hdr_value); $i++) {
+ $output .= Mail_mimeDecode::_getXML_helper($hdr_name, $hdr_value[$i], $indent);
+ }
+
+ // Only one header of this sort
+ } else {
+ $output .= Mail_mimeDecode::_getXML_helper($hdr_name, $hdr_value, $indent);
+ }
+ }
+
+ if (!empty($input->parts)) {
+ for ($i = 0; $i < count($input->parts); $i++) {
+ $output .= $crlf . str_repeat($htab, $indent) . '<mimepart>' . $crlf .
+ Mail_mimeDecode::_getXML($input->parts[$i], $indent+1) .
+ str_repeat($htab, $indent) . '</mimepart>' . $crlf;
+ }
+ } elseif (isset($input->body)) {
+ $output .= $crlf . str_repeat($htab, $indent) . '<body><![CDATA[' .
+ $input->body . ']]></body>' . $crlf;
+ }
+
+ return $output;
+ }
+
+ /**
+ * Helper function to _getXML(). Returns xml of a header.
+ *
+ * @param string Name of header
+ * @param string Value of header
+ * @param integer Number of tabs to indent
+ * @return string XML version of input
+ * @access private
+ */
+ function _getXML_helper($hdr_name, $hdr_value, $indent)
+ {
+ $htab = "\t";
+ $crlf = "\r\n";
+ $return = '';
+
+ $new_hdr_value = ($hdr_name != 'received') ? Mail_mimeDecode::_parseHeaderValue($hdr_value) : array('value' => $hdr_value);
+ $new_hdr_name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $hdr_name)));
+
+ // Sort out any parameters
+ if (!empty($new_hdr_value['other'])) {
+ foreach ($new_hdr_value['other'] as $paramname => $paramvalue) {
+ $params[] = str_repeat($htab, $indent) . $htab . '<parameter>' . $crlf .
+ str_repeat($htab, $indent) . $htab . $htab . '<paramname>' . htmlspecialchars($paramname) . '</paramname>' . $crlf .
+ str_repeat($htab, $indent) . $htab . $htab . '<paramvalue>' . htmlspecialchars($paramvalue) . '</paramvalue>' . $crlf .
+ str_repeat($htab, $indent) . $htab . '</parameter>' . $crlf;
+ }
+
+ $params = implode('', $params);
+ } else {
+ $params = '';
+ }
+
+ $return = str_repeat($htab, $indent) . '<header>' . $crlf .
+ str_repeat($htab, $indent) . $htab . '<headername>' . htmlspecialchars($new_hdr_name) . '</headername>' . $crlf .
+ str_repeat($htab, $indent) . $htab . '<headervalue>' . htmlspecialchars($new_hdr_value['value']) . '</headervalue>' . $crlf .
+ $params .
+ str_repeat($htab, $indent) . '</header>' . $crlf;
+
+ return $return;
+ }
+
+} // End of class
+?>
diff --git a/program/lib/Mail/mimePart.php b/program/lib/Mail/mimePart.php
new file mode 100644
index 000000000..b429b905e
--- /dev/null
+++ b/program/lib/Mail/mimePart.php
@@ -0,0 +1,351 @@
+<?php
+// +-----------------------------------------------------------------------+
+// | Copyright (c) 2002-2003 Richard Heyes |
+// | All rights reserved. |
+// | |
+// | Redistribution and use in source and binary forms, with or without |
+// | modification, are permitted provided that the following conditions |
+// | are met: |
+// | |
+// | o Redistributions of source code must retain the above copyright |
+// | notice, this list of conditions and the following disclaimer. |
+// | o Redistributions in binary form must reproduce the above copyright |
+// | notice, this list of conditions and the following disclaimer in the |
+// | documentation and/or other materials provided with the distribution.|
+// | o The names of the authors may not be used to endorse or promote |
+// | products derived from this software without specific prior written |
+// | permission. |
+// | |
+// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+// | |
+// +-----------------------------------------------------------------------+
+// | Author: Richard Heyes <richard@phpguru.org> |
+// +-----------------------------------------------------------------------+
+
+/**
+*
+* Raw mime encoding class
+*
+* What is it?
+* This class enables you to manipulate and build
+* a mime email from the ground up.
+*
+* Why use this instead of mime.php?
+* mime.php is a userfriendly api to this class for
+* people who aren't interested in the internals of
+* mime mail. This class however allows full control
+* over the email.
+*
+* Eg.
+*
+* // Since multipart/mixed has no real body, (the body is
+* // the subpart), we set the body argument to blank.
+*
+* $params['content_type'] = 'multipart/mixed';
+* $email = new Mail_mimePart('', $params);
+*
+* // Here we add a text part to the multipart we have
+* // already. Assume $body contains plain text.
+*
+* $params['content_type'] = 'text/plain';
+* $params['encoding'] = '7bit';
+* $text = $email->addSubPart($body, $params);
+*
+* // Now add an attachment. Assume $attach is
+* the contents of the attachment
+*
+* $params['content_type'] = 'application/zip';
+* $params['encoding'] = 'base64';
+* $params['disposition'] = 'attachment';
+* $params['dfilename'] = 'example.zip';
+* $attach =& $email->addSubPart($body, $params);
+*
+* // Now build the email. Note that the encode
+* // function returns an associative array containing two
+* // elements, body and headers. You will need to add extra
+* // headers, (eg. Mime-Version) before sending.
+*
+* $email = $message->encode();
+* $email['headers'][] = 'Mime-Version: 1.0';
+*
+*
+* Further examples are available at http://www.phpguru.org
+*
+* TODO:
+* - Set encode() to return the $obj->encoded if encode()
+* has already been run. Unless a flag is passed to specifically
+* re-build the message.
+*
+* @author Richard Heyes <richard@phpguru.org>
+* @version $Revision$
+* @package Mail
+*/
+
+class Mail_mimePart {
+
+ /**
+ * The encoding type of this part
+ * @var string
+ */
+ var $_encoding;
+
+ /**
+ * An array of subparts
+ * @var array
+ */
+ var $_subparts;
+
+ /**
+ * The output of this part after being built
+ * @var string
+ */
+ var $_encoded;
+
+ /**
+ * Headers for this part
+ * @var array
+ */
+ var $_headers;
+
+ /**
+ * The body of this part (not encoded)
+ * @var string
+ */
+ var $_body;
+
+ /**
+ * Constructor.
+ *
+ * Sets up the object.
+ *
+ * @param $body - The body of the mime part if any.
+ * @param $params - An associative array of parameters:
+ * content_type - The content type for this part eg multipart/mixed
+ * encoding - The encoding to use, 7bit, 8bit, base64, or quoted-printable
+ * cid - Content ID to apply
+ * disposition - Content disposition, inline or attachment
+ * dfilename - Optional filename parameter for content disposition
+ * description - Content description
+ * charset - Character set to use
+ * @access public
+ */
+ function Mail_mimePart($body = '', $params = array())
+ {
+ if (!defined('MAIL_MIMEPART_CRLF')) {
+ define('MAIL_MIMEPART_CRLF', defined('MAIL_MIME_CRLF') ? MAIL_MIME_CRLF : "\r\n", TRUE);
+ }
+
+ foreach ($params as $key => $value) {
+ switch ($key) {
+ case 'content_type':
+ $headers['Content-Type'] = $value . (isset($charset) ? '; charset="' . $charset . '"' : '');
+ break;
+
+ case 'encoding':
+ $this->_encoding = $value;
+ $headers['Content-Transfer-Encoding'] = $value;
+ break;
+
+ case 'cid':
+ $headers['Content-ID'] = '<' . $value . '>';
+ break;
+
+ case 'disposition':
+ $headers['Content-Disposition'] = $value . (isset($dfilename) ? '; filename="' . $dfilename . '"' : '');
+ break;
+
+ case 'dfilename':
+ if (isset($headers['Content-Disposition'])) {
+ $headers['Content-Disposition'] .= '; filename="' . $value . '"';
+ } else {
+ $dfilename = $value;
+ }
+ break;
+
+ case 'description':
+ $headers['Content-Description'] = $value;
+ break;
+
+ case 'charset':
+ if (isset($headers['Content-Type'])) {
+ $headers['Content-Type'] .= '; charset="' . $value . '"';
+ } else {
+ $charset = $value;
+ }
+ break;
+ }
+ }
+
+ // Default content-type
+ if (!isset($headers['Content-Type'])) {
+ $headers['Content-Type'] = 'text/plain';
+ }
+
+ //Default encoding
+ if (!isset($this->_encoding)) {
+ $this->_encoding = '7bit';
+ }
+
+ // Assign stuff to member variables
+ $this->_encoded = array();
+ $this->_headers = $headers;
+ $this->_body = $body;
+ }
+
+ /**
+ * encode()
+ *
+ * Encodes and returns the email. Also stores
+ * it in the encoded member variable
+ *
+ * @return An associative array containing two elements,
+ * body and headers. The headers element is itself
+ * an indexed array.
+ * @access public
+ */
+ function encode()
+ {
+ $encoded =& $this->_encoded;
+
+ if (!empty($this->_subparts)) {
+ srand((double)microtime()*1000000);
+ $boundary = '=_' . md5(rand() . microtime());
+ $this->_headers['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF . "\t" . 'boundary="' . $boundary . '"';
+
+ // Add body parts to $subparts
+ for ($i = 0; $i < count($this->_subparts); $i++) {
+ $headers = array();
+ $tmp = $this->_subparts[$i]->encode();
+ foreach ($tmp['headers'] as $key => $value) {
+ $headers[] = $key . ': ' . $value;
+ }
+ $subparts[] = implode(MAIL_MIMEPART_CRLF, $headers) . MAIL_MIMEPART_CRLF . MAIL_MIMEPART_CRLF . $tmp['body'];
+ }
+
+ $encoded['body'] = '--' . $boundary . MAIL_MIMEPART_CRLF .
+ implode('--' . $boundary . MAIL_MIMEPART_CRLF, $subparts) .
+ '--' . $boundary.'--' . MAIL_MIMEPART_CRLF;
+
+ } else {
+ $encoded['body'] = $this->_getEncodedData($this->_body, $this->_encoding) . MAIL_MIMEPART_CRLF;
+ }
+
+ // Add headers to $encoded
+ $encoded['headers'] =& $this->_headers;
+
+ return $encoded;
+ }
+
+ /**
+ * &addSubPart()
+ *
+ * Adds a subpart to current mime part and returns
+ * a reference to it
+ *
+ * @param $body The body of the subpart, if any.
+ * @param $params The parameters for the subpart, same
+ * as the $params argument for constructor.
+ * @return A reference to the part you just added. It is
+ * crucial if using multipart/* in your subparts that
+ * you use =& in your script when calling this function,
+ * otherwise you will not be able to add further subparts.
+ * @access public
+ */
+ function &addSubPart($body, $params)
+ {
+ $this->_subparts[] = new Mail_mimePart($body, $params);
+ return $this->_subparts[count($this->_subparts) - 1];
+ }
+
+ /**
+ * _getEncodedData()
+ *
+ * Returns encoded data based upon encoding passed to it
+ *
+ * @param $data The data to encode.
+ * @param $encoding The encoding type to use, 7bit, base64,
+ * or quoted-printable.
+ * @access private
+ */
+ function _getEncodedData($data, $encoding)
+ {
+ switch ($encoding) {
+ case '8bit':
+ case '7bit':
+ return $data;
+ break;
+
+ case 'quoted-printable':
+ return $this->_quotedPrintableEncode($data);
+ break;
+
+ case 'base64':
+ return rtrim(chunk_split(base64_encode($data), 76, MAIL_MIMEPART_CRLF));
+ break;
+
+ default:
+ return $data;
+ }
+ }
+
+ /**
+ * quoteadPrintableEncode()
+ *
+ * Encodes data to quoted-printable standard.
+ *
+ * @param $input The data to encode
+ * @param $line_max Optional max line length. Should
+ * not be more than 76 chars
+ *
+ * @access private
+ */
+ function _quotedPrintableEncode($input , $line_max = 76)
+ {
+ $lines = preg_split("/\r?\n/", $input);
+ $eol = MAIL_MIMEPART_CRLF;
+ $escape = '=';
+ $output = '';
+
+ while(list(, $line) = each($lines)){
+
+ $linlen = strlen($line);
+ $newline = '';
+
+ for ($i = 0; $i < $linlen; $i++) {
+ $char = substr($line, $i, 1);
+ $dec = ord($char);
+
+ if (($dec == 32) AND ($i == ($linlen - 1))){ // convert space at eol only
+ $char = '=20';
+
+ } elseif(($dec == 9) AND ($i == ($linlen - 1))) { // convert tab at eol only
+ $char = '=09';
+ } elseif($dec == 9) {
+ ; // Do nothing if a tab.
+ } elseif(($dec == 61) OR ($dec < 32 ) OR ($dec > 126)) {
+ $char = $escape . strtoupper(sprintf('%02s', dechex($dec)));
+ }
+
+ if ((strlen($newline) + strlen($char)) >= $line_max) { // MAIL_MIMEPART_CRLF is not counted
+ $output .= $newline . $escape . $eol; // soft line break; " =\r\n" is okay
+ $newline = '';
+ }
+ $newline .= $char;
+ } // end of for
+ $output .= $newline . $eol;
+ }
+ $output = substr($output, 0, -1 * strlen($eol)); // Don't want last crlf
+ return $output;
+ }
+} // End of class
+?>
diff --git a/program/lib/PEAR.php b/program/lib/PEAR.php
new file mode 100644
index 000000000..5b76d7540
--- /dev/null
+++ b/program/lib/PEAR.php
@@ -0,0 +1,927 @@
+<?php
+//
+// +----------------------------------------------------------------------+
+// | PHP Version 4 |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 1997-2002 The PHP Group |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 2.0 of the PHP license, |
+// | that is bundled with this package in the file LICENSE, and is |
+// | available at through the world-wide-web at |
+// | http://www.php.net/license/2_02.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +----------------------------------------------------------------------+
+// | Authors: Sterling Hughes <sterling@php.net> |
+// | Stig Bakken <ssb@fast.no> |
+// | Tomas V.V.Cox <cox@idecnet.com> |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+//
+
+define('PEAR_ERROR_RETURN', 1);
+define('PEAR_ERROR_PRINT', 2);
+define('PEAR_ERROR_TRIGGER', 4);
+define('PEAR_ERROR_DIE', 8);
+define('PEAR_ERROR_CALLBACK', 16);
+define('PEAR_ZE2', (function_exists('version_compare') &&
+ version_compare(zend_version(), "2-dev", "ge")));
+
+if (substr(PHP_OS, 0, 3) == 'WIN') {
+ define('OS_WINDOWS', true);
+ define('OS_UNIX', false);
+ define('PEAR_OS', 'Windows');
+} else {
+ define('OS_WINDOWS', false);
+ define('OS_UNIX', true);
+ define('PEAR_OS', 'Unix'); // blatant assumption
+}
+
+$GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN;
+$GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE;
+$GLOBALS['_PEAR_destructor_object_list'] = array();
+$GLOBALS['_PEAR_shutdown_funcs'] = array();
+$GLOBALS['_PEAR_error_handler_stack'] = array();
+
+ini_set('track_errors', true);
+
+/**
+ * Base class for other PEAR classes. Provides rudimentary
+ * emulation of destructors.
+ *
+ * If you want a destructor in your class, inherit PEAR and make a
+ * destructor method called _yourclassname (same name as the
+ * constructor, but with a "_" prefix). Also, in your constructor you
+ * have to call the PEAR constructor: $this->PEAR();.
+ * The destructor method will be called without parameters. Note that
+ * at in some SAPI implementations (such as Apache), any output during
+ * the request shutdown (in which destructors are called) seems to be
+ * discarded. If you need to get any debug information from your
+ * destructor, use error_log(), syslog() or something similar.
+ *
+ * IMPORTANT! To use the emulated destructors you need to create the
+ * objects by reference, ej: $obj =& new PEAR_child;
+ *
+ * @since PHP 4.0.2
+ * @author Stig Bakken <ssb@fast.no>
+ * @see http://pear.php.net/manual/
+ */
+class PEAR
+{
+ // {{{ properties
+
+ /**
+ * Whether to enable internal debug messages.
+ *
+ * @var bool
+ * @access private
+ */
+ var $_debug = false;
+
+ /**
+ * Default error mode for this object.
+ *
+ * @var int
+ * @access private
+ */
+ var $_default_error_mode = null;
+
+ /**
+ * Default error options used for this object when error mode
+ * is PEAR_ERROR_TRIGGER.
+ *
+ * @var int
+ * @access private
+ */
+ var $_default_error_options = null;
+
+ /**
+ * Default error handler (callback) for this object, if error mode is
+ * PEAR_ERROR_CALLBACK.
+ *
+ * @var string
+ * @access private
+ */
+ var $_default_error_handler = '';
+
+ /**
+ * Which class to use for error objects.
+ *
+ * @var string
+ * @access private
+ */
+ var $_error_class = 'PEAR_Error';
+
+ /**
+ * An array of expected errors.
+ *
+ * @var array
+ * @access private
+ */
+ var $_expected_errors = array();
+
+ // }}}
+
+ // {{{ constructor
+
+ /**
+ * Constructor. Registers this object in
+ * $_PEAR_destructor_object_list for destructor emulation if a
+ * destructor object exists.
+ *
+ * @param string $error_class (optional) which class to use for
+ * error objects, defaults to PEAR_Error.
+ * @access public
+ * @return void
+ */
+ function PEAR($error_class = null)
+ {
+ $classname = get_class($this);
+ if ($this->_debug) {
+ print "PEAR constructor called, class=$classname\n";
+ }
+ if ($error_class !== null) {
+ $this->_error_class = $error_class;
+ }
+ while ($classname) {
+ $destructor = "_$classname";
+ if (method_exists($this, $destructor)) {
+ global $_PEAR_destructor_object_list;
+ $_PEAR_destructor_object_list[] = &$this;
+ break;
+ } else {
+ $classname = get_parent_class($classname);
+ }
+ }
+ }
+
+ // }}}
+ // {{{ destructor
+
+ /**
+ * Destructor (the emulated type of...). Does nothing right now,
+ * but is included for forward compatibility, so subclass
+ * destructors should always call it.
+ *
+ * See the note in the class desciption about output from
+ * destructors.
+ *
+ * @access public
+ * @return void
+ */
+ function _PEAR() {
+ if ($this->_debug) {
+ printf("PEAR destructor called, class=%s\n", get_class($this));
+ }
+ }
+
+ // }}}
+ // {{{ getStaticProperty()
+
+ /**
+ * If you have a class that's mostly/entirely static, and you need static
+ * properties, you can use this method to simulate them. Eg. in your method(s)
+ * do this: $myVar = &PEAR::getStaticProperty('myVar');
+ * You MUST use a reference, or they will not persist!
+ *
+ * @access public
+ * @param string $class The calling classname, to prevent clashes
+ * @param string $var The variable to retrieve.
+ * @return mixed A reference to the variable. If not set it will be
+ * auto initialised to NULL.
+ */
+ function &getStaticProperty($class, $var)
+ {
+ static $properties;
+ return $properties[$class][$var];
+ }
+
+ // }}}
+ // {{{ registerShutdownFunc()
+
+ /**
+ * Use this function to register a shutdown method for static
+ * classes.
+ *
+ * @access public
+ * @param mixed $func The function name (or array of class/method) to call
+ * @param mixed $args The arguments to pass to the function
+ * @return void
+ */
+ function registerShutdownFunc($func, $args = array())
+ {
+ $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
+ }
+
+ // }}}
+ // {{{ isError()
+
+ /**
+ * Tell whether a value is a PEAR error.
+ *
+ * @param mixed $data the value to test
+ * @access public
+ * @return bool true if parameter is an error
+ */
+ function isError($data) {
+ return (bool)(is_object($data) &&
+ (get_class($data) == 'pear_error' ||
+ is_subclass_of($data, 'pear_error')));
+ }
+
+ // }}}
+ // {{{ setErrorHandling()
+
+ /**
+ * Sets how errors generated by this DB object should be handled.
+ * Can be invoked both in objects and statically. If called
+ * statically, setErrorHandling sets the default behaviour for all
+ * PEAR objects. If called in an object, setErrorHandling sets
+ * the default behaviour for that object.
+ *
+ * @param int $mode
+ * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
+ * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE or
+ * PEAR_ERROR_CALLBACK.
+ *
+ * @param mixed $options
+ * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
+ * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
+ *
+ * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
+ * to be the callback function or method. A callback
+ * function is a string with the name of the function, a
+ * callback method is an array of two elements: the element
+ * at index 0 is the object, and the element at index 1 is
+ * the name of the method to call in the object.
+ *
+ * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
+ * a printf format string used when printing the error
+ * message.
+ *
+ * @access public
+ * @return void
+ * @see PEAR_ERROR_RETURN
+ * @see PEAR_ERROR_PRINT
+ * @see PEAR_ERROR_TRIGGER
+ * @see PEAR_ERROR_DIE
+ * @see PEAR_ERROR_CALLBACK
+ *
+ * @since PHP 4.0.5
+ */
+
+ function setErrorHandling($mode = null, $options = null)
+ {
+ if (isset($this)) {
+ $setmode = &$this->_default_error_mode;
+ $setoptions = &$this->_default_error_options;
+ } else {
+ $setmode = &$GLOBALS['_PEAR_default_error_mode'];
+ $setoptions = &$GLOBALS['_PEAR_default_error_options'];
+ }
+
+ switch ($mode) {
+ case PEAR_ERROR_RETURN:
+ case PEAR_ERROR_PRINT:
+ case PEAR_ERROR_TRIGGER:
+ case PEAR_ERROR_DIE:
+ case null:
+ $setmode = $mode;
+ $setoptions = $options;
+ break;
+
+ case PEAR_ERROR_CALLBACK:
+ $setmode = $mode;
+ if ((is_string($options) && function_exists($options)) ||
+ (is_array($options) && method_exists(@$options[0], @$options[1])))
+ {
+ $setoptions = $options;
+ } else {
+ trigger_error("invalid error callback", E_USER_WARNING);
+ }
+ break;
+
+ default:
+ trigger_error("invalid error mode", E_USER_WARNING);
+ break;
+ }
+ }
+
+ // }}}
+ // {{{ expectError()
+
+ /**
+ * This method is used to tell which errors you expect to get.
+ * Expected errors are always returned with error mode
+ * PEAR_ERROR_RETURN. Expected error codes are stored in a stack,
+ * and this method pushes a new element onto it. The list of
+ * expected errors are in effect until they are popped off the
+ * stack with the popExpect() method.
+ *
+ * Note that this method can not be called statically
+ *
+ * @param mixed $code a single error code or an array of error codes to expect
+ *
+ * @return int the new depth of the "expected errors" stack
+ * @access public
+ */
+ function expectError($code = '*')
+ {
+ if (is_array($code)) {
+ array_push($this->_expected_errors, $code);
+ } else {
+ array_push($this->_expected_errors, array($code));
+ }
+ return sizeof($this->_expected_errors);
+ }
+
+ // }}}
+ // {{{ popExpect()
+
+ /**
+ * This method pops one element off the expected error codes
+ * stack.
+ *
+ * @return array the list of error codes that were popped
+ */
+ function popExpect()
+ {
+ return array_pop($this->_expected_errors);
+ }
+
+ // }}}
+ // {{{ _checkDelExpect()
+
+ /**
+ * This method checks unsets an error code if available
+ *
+ * @param mixed error code
+ * @return bool true if the error code was unset, false otherwise
+ * @access private
+ * @since PHP 4.3.0
+ */
+ function _checkDelExpect($error_code)
+ {
+ $deleted = false;
+
+ foreach ($this->_expected_errors AS $key => $error_array) {
+ if (in_array($error_code, $error_array)) {
+ unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
+ $deleted = true;
+ }
+
+ // clean up empty arrays
+ if (0 == count($this->_expected_errors[$key])) {
+ unset($this->_expected_errors[$key]);
+ }
+ }
+ return $deleted;
+ }
+
+ // }}}
+ // {{{ delExpect()
+
+ /**
+ * This method deletes all occurences of the specified element from
+ * the expected error codes stack.
+ *
+ * @param mixed $error_code error code that should be deleted
+ * @return mixed list of error codes that were deleted or error
+ * @access public
+ * @since PHP 4.3.0
+ */
+ function delExpect($error_code)
+ {
+ $deleted = false;
+
+ if ((is_array($error_code) && (0 != count($error_code)))) {
+ // $error_code is a non-empty array here;
+ // we walk through it trying to unset all
+ // values
+ foreach($error_code AS $key => $error) {
+ if ($this->_checkDelExpect($error)) {
+ $deleted = true;
+ } else {
+ $deleted = false;
+ }
+ }
+ return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
+ } elseif (!empty($error_code)) {
+ // $error_code comes alone, trying to unset it
+ if ($this->_checkDelExpect($error_code)) {
+ return true;
+ } else {
+ return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
+ }
+ } else {
+ // $error_code is empty
+ return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
+ }
+ }
+
+ // }}}
+ // {{{ raiseError()
+
+ /**
+ * This method is a wrapper that returns an instance of the
+ * configured error class with this object's default error
+ * handling applied. If the $mode and $options parameters are not
+ * specified, the object's defaults are used.
+ *
+ * @param mixed $message a text error message or a PEAR error object
+ *
+ * @param int $code a numeric error code (it is up to your class
+ * to define these if you want to use codes)
+ *
+ * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
+ * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE or
+ * PEAR_ERROR_CALLBACK.
+ *
+ * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
+ * specifies the PHP-internal error level (one of
+ * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
+ * If $mode is PEAR_ERROR_CALLBACK, this
+ * parameter specifies the callback function or
+ * method. In other error modes this parameter
+ * is ignored.
+ *
+ * @param string $userinfo If you need to pass along for example debug
+ * information, this parameter is meant for that.
+ *
+ * @param string $error_class The returned error object will be
+ * instantiated from this class, if specified.
+ *
+ * @param bool $skipmsg If true, raiseError will only pass error codes,
+ * the error message parameter will be dropped.
+ *
+ * @access public
+ * @return object a PEAR error object
+ * @see PEAR::setErrorHandling
+ * @since PHP 4.0.5
+ */
+ function &raiseError($message = null,
+ $code = null,
+ $mode = null,
+ $options = null,
+ $userinfo = null,
+ $error_class = null,
+ $skipmsg = false)
+ {
+ // The error is yet a PEAR error object
+ if (is_object($message)) {
+ $code = $message->getCode();
+ $userinfo = $message->getUserInfo();
+ $error_class = $message->getType();
+ $message = $message->getMessage();
+ }
+
+ if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
+ if ($exp[0] == "*" ||
+ (is_int(reset($exp)) && in_array($code, $exp)) ||
+ (is_string(reset($exp)) && in_array($message, $exp))) {
+ $mode = PEAR_ERROR_RETURN;
+ }
+ }
+ // No mode given, try global ones
+ if ($mode === null) {
+ // Class error handler
+ if (isset($this) && isset($this->_default_error_mode)) {
+ $mode = $this->_default_error_mode;
+ $options = $this->_default_error_options;
+ // Global error handler
+ } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
+ $mode = $GLOBALS['_PEAR_default_error_mode'];
+ $options = $GLOBALS['_PEAR_default_error_options'];
+ }
+ }
+
+ if ($error_class !== null) {
+ $ec = $error_class;
+ } elseif (isset($this) && isset($this->_error_class)) {
+ $ec = $this->_error_class;
+ } else {
+ $ec = 'PEAR_Error';
+ }
+ if ($skipmsg) {
+ return new $ec($code, $mode, $options, $userinfo);
+ } else {
+ return new $ec($message, $code, $mode, $options, $userinfo);
+ }
+ }
+
+ // }}}
+ // {{{ throwError()
+
+ /**
+ * Simpler form of raiseError with fewer options. In most cases
+ * message, code and userinfo are enough.
+ *
+ * @param string $message
+ *
+ */
+ function &throwError($message = null,
+ $code = null,
+ $userinfo = null)
+ {
+ if (isset($this)) {
+ return $this->raiseError($message, $code, null, null, $userinfo);
+ } else {
+ return PEAR::raiseError($message, $code, null, null, $userinfo);
+ }
+ }
+
+ // }}}
+ // {{{ pushErrorHandling()
+
+ /**
+ * Push a new error handler on top of the error handler options stack. With this
+ * you can easily override the actual error handler for some code and restore
+ * it later with popErrorHandling.
+ *
+ * @param mixed $mode (same as setErrorHandling)
+ * @param mixed $options (same as setErrorHandling)
+ *
+ * @return bool Always true
+ *
+ * @see PEAR::setErrorHandling
+ */
+ function pushErrorHandling($mode, $options = null)
+ {
+ $stack = &$GLOBALS['_PEAR_error_handler_stack'];
+ if (isset($this)) {
+ $def_mode = &$this->_default_error_mode;
+ $def_options = &$this->_default_error_options;
+ } else {
+ $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
+ $def_options = &$GLOBALS['_PEAR_default_error_options'];
+ }
+ $stack[] = array($def_mode, $def_options);
+
+ if (isset($this)) {
+ $this->setErrorHandling($mode, $options);
+ } else {
+ PEAR::setErrorHandling($mode, $options);
+ }
+ $stack[] = array($mode, $options);
+ return true;
+ }
+
+ // }}}
+ // {{{ popErrorHandling()
+
+ /**
+ * Pop the last error handler used
+ *
+ * @return bool Always true
+ *
+ * @see PEAR::pushErrorHandling
+ */
+ function popErrorHandling()
+ {
+ $stack = &$GLOBALS['_PEAR_error_handler_stack'];
+ array_pop($stack);
+ list($mode, $options) = $stack[sizeof($stack) - 1];
+ array_pop($stack);
+ if (isset($this)) {
+ $this->setErrorHandling($mode, $options);
+ } else {
+ PEAR::setErrorHandling($mode, $options);
+ }
+ return true;
+ }
+
+ // }}}
+ // {{{ loadExtension()
+
+ /**
+ * OS independant PHP extension load. Remember to take care
+ * on the correct extension name for case sensitive OSes.
+ *
+ * @param string $ext The extension name
+ * @return bool Success or not on the dl() call
+ */
+ function loadExtension($ext)
+ {
+ if (!extension_loaded($ext)) {
+ if (OS_WINDOWS) {
+ $suffix = '.dll';
+ } elseif (PHP_OS == 'HP-UX') {
+ $suffix = '.sl';
+ } elseif (PHP_OS == 'AIX') {
+ $suffix = '.a';
+ } elseif (PHP_OS == 'OSX') {
+ $suffix = '.bundle';
+ } else {
+ $suffix = '.so';
+ }
+ return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
+ }
+ return true;
+ }
+
+ // }}}
+}
+
+// {{{ _PEAR_call_destructors()
+
+function _PEAR_call_destructors()
+{
+ global $_PEAR_destructor_object_list;
+ if (is_array($_PEAR_destructor_object_list) &&
+ sizeof($_PEAR_destructor_object_list))
+ {
+ reset($_PEAR_destructor_object_list);
+ while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
+ $classname = get_class($objref);
+ while ($classname) {
+ $destructor = "_$classname";
+ if (method_exists($objref, $destructor)) {
+ $objref->$destructor();
+ break;
+ } else {
+ $classname = get_parent_class($classname);
+ }
+ }
+ }
+ // Empty the object list to ensure that destructors are
+ // not called more than once.
+ $_PEAR_destructor_object_list = array();
+ }
+
+ // Now call the shutdown functions
+ if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
+ foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
+ call_user_func_array($value[0], $value[1]);
+ }
+ }
+}
+
+// }}}
+
+class PEAR_Error
+{
+ // {{{ properties
+
+ var $error_message_prefix = '';
+ var $mode = PEAR_ERROR_RETURN;
+ var $level = E_USER_NOTICE;
+ var $code = -1;
+ var $message = '';
+ var $userinfo = '';
+
+ // Wait until we have a stack-groping function in PHP.
+ //var $file = '';
+ //var $line = 0;
+
+
+ // }}}
+ // {{{ constructor
+
+ /**
+ * PEAR_Error constructor
+ *
+ * @param string $message message
+ *
+ * @param int $code (optional) error code
+ *
+ * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
+ * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER or
+ * PEAR_ERROR_CALLBACK
+ *
+ * @param mixed $options (optional) error level, _OR_ in the case of
+ * PEAR_ERROR_CALLBACK, the callback function or object/method
+ * tuple.
+ *
+ * @param string $userinfo (optional) additional user/debug info
+ *
+ * @access public
+ *
+ */
+ function PEAR_Error($message = 'unknown error', $code = null,
+ $mode = null, $options = null, $userinfo = null)
+ {
+ if ($mode === null) {
+ $mode = PEAR_ERROR_RETURN;
+ }
+ $this->message = $message;
+ $this->code = $code;
+ $this->mode = $mode;
+ $this->userinfo = $userinfo;
+ if ($mode & PEAR_ERROR_CALLBACK) {
+ $this->level = E_USER_NOTICE;
+ $this->callback = $options;
+ } else {
+ if ($options === null) {
+ $options = E_USER_NOTICE;
+ }
+ $this->level = $options;
+ $this->callback = null;
+ }
+ if ($this->mode & PEAR_ERROR_PRINT) {
+ if (is_null($options) || is_int($options)) {
+ $format = "%s";
+ } else {
+ $format = $options;
+ }
+ printf($format, $this->getMessage());
+ }
+ if ($this->mode & PEAR_ERROR_TRIGGER) {
+ trigger_error($this->getMessage(), $this->level);
+ }
+ if ($this->mode & PEAR_ERROR_DIE) {
+ $msg = $this->getMessage();
+ if (is_null($options) || is_int($options)) {
+ $format = "%s";
+ if (substr($msg, -1) != "\n") {
+ $msg .= "\n";
+ }
+ } else {
+ $format = $options;
+ }
+ die(sprintf($format, $msg));
+ }
+ if ($this->mode & PEAR_ERROR_CALLBACK) {
+ if (is_string($this->callback) && strlen($this->callback)) {
+ call_user_func($this->callback, $this);
+ } elseif (is_array($this->callback) &&
+ sizeof($this->callback) == 2 &&
+ is_object($this->callback[0]) &&
+ is_string($this->callback[1]) &&
+ strlen($this->callback[1])) {
+ @call_user_func($this->callback, $this);
+ }
+ }
+ }
+
+ // }}}
+ // {{{ getMode()
+
+ /**
+ * Get the error mode from an error object.
+ *
+ * @return int error mode
+ * @access public
+ */
+ function getMode() {
+ return $this->mode;
+ }
+
+ // }}}
+ // {{{ getCallback()
+
+ /**
+ * Get the callback function/method from an error object.
+ *
+ * @return mixed callback function or object/method array
+ * @access public
+ */
+ function getCallback() {
+ return $this->callback;
+ }
+
+ // }}}
+ // {{{ getMessage()
+
+
+ /**
+ * Get the error message from an error object.
+ *
+ * @return string full error message
+ * @access public
+ */
+ function getMessage()
+ {
+ return ($this->error_message_prefix . $this->message);
+ }
+
+
+ // }}}
+ // {{{ getCode()
+
+ /**
+ * Get error code from an error object
+ *
+ * @return int error code
+ * @access public
+ */
+ function getCode()
+ {
+ return $this->code;
+ }
+
+ // }}}
+ // {{{ getType()
+
+ /**
+ * Get the name of this error/exception.
+ *
+ * @return string error/exception name (type)
+ * @access public
+ */
+ function getType()
+ {
+ return get_class($this);
+ }
+
+ // }}}
+ // {{{ getUserInfo()
+
+ /**
+ * Get additional user-supplied information.
+ *
+ * @return string user-supplied information
+ * @access public
+ */
+ function getUserInfo()
+ {
+ return $this->userinfo;
+ }
+
+ // }}}
+ // {{{ getDebugInfo()
+
+ /**
+ * Get additional debug information supplied by the application.
+ *
+ * @return string debug information
+ * @access public
+ */
+ function getDebugInfo()
+ {
+ return $this->getUserInfo();
+ }
+
+ // }}}
+ // {{{ addUserInfo()
+
+ function addUserInfo($info)
+ {
+ if (empty($this->userinfo)) {
+ $this->userinfo = $info;
+ } else {
+ $this->userinfo .= " ** $info";
+ }
+ }
+
+ // }}}
+ // {{{ toString()
+
+ /**
+ * Make a string representation of this object.
+ *
+ * @return string a string with an object summary
+ * @access public
+ */
+ function toString() {
+ $modes = array();
+ $levels = array(E_USER_NOTICE => 'notice',
+ E_USER_WARNING => 'warning',
+ E_USER_ERROR => 'error');
+ if ($this->mode & PEAR_ERROR_CALLBACK) {
+ if (is_array($this->callback)) {
+ $callback = get_class($this->callback[0]) . '::' .
+ $this->callback[1];
+ } else {
+ $callback = $this->callback;
+ }
+ return sprintf('[%s: message="%s" code=%d mode=callback '.
+ 'callback=%s prefix="%s" info="%s"]',
+ get_class($this), $this->message, $this->code,
+ $callback, $this->error_message_prefix,
+ $this->userinfo);
+ }
+ if ($this->mode & PEAR_ERROR_PRINT) {
+ $modes[] = 'print';
+ }
+ if ($this->mode & PEAR_ERROR_TRIGGER) {
+ $modes[] = 'trigger';
+ }
+ if ($this->mode & PEAR_ERROR_DIE) {
+ $modes[] = 'die';
+ }
+ if ($this->mode & PEAR_ERROR_RETURN) {
+ $modes[] = 'return';
+ }
+ return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
+ 'prefix="%s" info="%s"]',
+ get_class($this), $this->message, $this->code,
+ implode("|", $modes), $levels[$this->level],
+ $this->error_message_prefix,
+ $this->userinfo);
+ }
+
+ // }}}
+}
+
+register_shutdown_function("_PEAR_call_destructors");
+
+/*
+ * Local Variables:
+ * mode: php
+ * tab-width: 4
+ * c-basic-offset: 4
+ * End:
+ */
+?>
diff --git a/program/lib/des.inc b/program/lib/des.inc
new file mode 100644
index 000000000..00ecd688f
--- /dev/null
+++ b/program/lib/des.inc
@@ -0,0 +1,218 @@
+<?php
+
+//PHP version
+//Paul Tero, July 2001
+//http://www.shopable.co.uk/des.html
+//
+//Optimised for performance with large blocks by Michael Hayworth, November 2001
+//http://www.netdealing.com
+//
+//Converted from JavaScript to PHP by Jim Gibbs, June 2004
+//
+//THIS SOFTWARE IS PROVIDED "AS IS" AND
+//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+//IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+//ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+//FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+//DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+//OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+//HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+//OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+//SUCH DAMAGE.
+
+//des
+//this takes the key, the message, and whether to encrypt or decrypt
+function des ($key, $message, $encrypt, $mode, $iv) {
+ //declaring this locally speeds things up a bit
+ $spfunction1 = array (0x1010400,0,0x10000,0x1010404,0x1010004,0x10404,0x4,0x10000,0x400,0x1010400,0x1010404,0x400,0x1000404,0x1010004,0x1000000,0x4,0x404,0x1000400,0x1000400,0x10400,0x10400,0x1010000,0x1010000,0x1000404,0x10004,0x1000004,0x1000004,0x10004,0,0x404,0x10404,0x1000000,0x10000,0x1010404,0x4,0x1010000,0x1010400,0x1000000,0x1000000,0x400,0x1010004,0x10000,0x10400,0x1000004,0x400,0x4,0x1000404,0x10404,0x1010404,0x10004,0x1010000,0x1000404,0x1000004,0x404,0x10404,0x1010400,0x404,0x1000400,0x1000400,0,0x10004,0x10400,0,0x1010004);
+ $spfunction2 = array (-0x7fef7fe0,-0x7fff8000,0x8000,0x108020,0x100000,0x20,-0x7fefffe0,-0x7fff7fe0,-0x7fffffe0,-0x7fef7fe0,-0x7fef8000,-0x80000000,-0x7fff8000,0x100000,0x20,-0x7fefffe0,0x108000,0x100020,-0x7fff7fe0,0,-0x80000000,0x8000,0x108020,-0x7ff00000,0x100020,-0x7fffffe0,0,0x108000,0x8020,-0x7fef8000,-0x7ff00000,0x8020,0,0x108020,-0x7fefffe0,0x100000,-0x7fff7fe0,-0x7ff00000,-0x7fef8000,0x8000,-0x7ff00000,-0x7fff8000,0x20,-0x7fef7fe0,0x108020,0x20,0x8000,-0x80000000,0x8020,-0x7fef8000,0x100000,-0x7fffffe0,0x100020,-0x7fff7fe0,-0x7fffffe0,0x100020,0x108000,0,-0x7fff8000,0x8020,-0x80000000,-0x7fefffe0,-0x7fef7fe0,0x108000);
+ $spfunction3 = array (0x208,0x8020200,0,0x8020008,0x8000200,0,0x20208,0x8000200,0x20008,0x8000008,0x8000008,0x20000,0x8020208,0x20008,0x8020000,0x208,0x8000000,0x8,0x8020200,0x200,0x20200,0x8020000,0x8020008,0x20208,0x8000208,0x20200,0x20000,0x8000208,0x8,0x8020208,0x200,0x8000000,0x8020200,0x8000000,0x20008,0x208,0x20000,0x8020200,0x8000200,0,0x200,0x20008,0x8020208,0x8000200,0x8000008,0x200,0,0x8020008,0x8000208,0x20000,0x8000000,0x8020208,0x8,0x20208,0x20200,0x8000008,0x8020000,0x8000208,0x208,0x8020000,0x20208,0x8,0x8020008,0x20200);
+ $spfunction4 = array (0x802001,0x2081,0x2081,0x80,0x802080,0x800081,0x800001,0x2001,0,0x802000,0x802000,0x802081,0x81,0,0x800080,0x800001,0x1,0x2000,0x800000,0x802001,0x80,0x800000,0x2001,0x2080,0x800081,0x1,0x2080,0x800080,0x2000,0x802080,0x802081,0x81,0x800080,0x800001,0x802000,0x802081,0x81,0,0,0x802000,0x2080,0x800080,0x800081,0x1,0x802001,0x2081,0x2081,0x80,0x802081,0x81,0x1,0x2000,0x800001,0x2001,0x802080,0x800081,0x2001,0x2080,0x800000,0x802001,0x80,0x800000,0x2000,0x802080);
+ $spfunction5 = array (0x100,0x2080100,0x2080000,0x42000100,0x80000,0x100,0x40000000,0x2080000,0x40080100,0x80000,0x2000100,0x40080100,0x42000100,0x42080000,0x80100,0x40000000,0x2000000,0x40080000,0x40080000,0,0x40000100,0x42080100,0x42080100,0x2000100,0x42080000,0x40000100,0,0x42000000,0x2080100,0x2000000,0x42000000,0x80100,0x80000,0x42000100,0x100,0x2000000,0x40000000,0x2080000,0x42000100,0x40080100,0x2000100,0x40000000,0x42080000,0x2080100,0x40080100,0x100,0x2000000,0x42080000,0x42080100,0x80100,0x42000000,0x42080100,0x2080000,0,0x40080000,0x42000000,0x80100,0x2000100,0x40000100,0x80000,0,0x40080000,0x2080100,0x40000100);
+ $spfunction6 = array (0x20000010,0x20400000,0x4000,0x20404010,0x20400000,0x10,0x20404010,0x400000,0x20004000,0x404010,0x400000,0x20000010,0x400010,0x20004000,0x20000000,0x4010,0,0x400010,0x20004010,0x4000,0x404000,0x20004010,0x10,0x20400010,0x20400010,0,0x404010,0x20404000,0x4010,0x404000,0x20404000,0x20000000,0x20004000,0x10,0x20400010,0x404000,0x20404010,0x400000,0x4010,0x20000010,0x400000,0x20004000,0x20000000,0x4010,0x20000010,0x20404010,0x404000,0x20400000,0x404010,0x20404000,0,0x20400010,0x10,0x4000,0x20400000,0x404010,0x4000,0x400010,0x20004010,0,0x20404000,0x20000000,0x400010,0x20004010);
+ $spfunction7 = array (0x200000,0x4200002,0x4000802,0,0x800,0x4000802,0x200802,0x4200800,0x4200802,0x200000,0,0x4000002,0x2,0x4000000,0x4200002,0x802,0x4000800,0x200802,0x200002,0x4000800,0x4000002,0x4200000,0x4200800,0x200002,0x4200000,0x800,0x802,0x4200802,0x200800,0x2,0x4000000,0x200800,0x4000000,0x200800,0x200000,0x4000802,0x4000802,0x4200002,0x4200002,0x2,0x200002,0x4000000,0x4000800,0x200000,0x4200800,0x802,0x200802,0x4200800,0x802,0x4000002,0x4200802,0x4200000,0x200800,0,0x2,0x4200802,0,0x200802,0x4200000,0x800,0x4000002,0x4000800,0x800,0x200002);
+ $spfunction8 = array (0x10001040,0x1000,0x40000,0x10041040,0x10000000,0x10001040,0x40,0x10000000,0x40040,0x10040000,0x10041040,0x41000,0x10041000,0x41040,0x1000,0x40,0x10040000,0x10000040,0x10001000,0x1040,0x41000,0x40040,0x10040040,0x10041000,0x1040,0,0,0x10040040,0x10000040,0x10001000,0x41040,0x40000,0x41040,0x40000,0x10041000,0x1000,0x40,0x10040040,0x1000,0x41040,0x10001000,0x40,0x10000040,0x10040000,0x10040040,0x10000000,0x40000,0x10001040,0,0x10041040,0x40040,0x10000040,0x10040000,0x10001000,0x10001040,0,0x10041040,0x41000,0x41000,0x1040,0x1040,0x40040,0x10000000,0x10041000);
+ $masks = array (4294967295,2147483647,1073741823,536870911,268435455,134217727,67108863,33554431,16777215,8388607,4194303,2097151,1048575,524287,262143,131071,65535,32767,16383,8191,4095,2047,1023,511,255,127,63,31,15,7,3,1,0);
+
+ //create the 16 or 48 subkeys we will need
+ $keys = des_createKeys ($key);
+ $m=0;
+ $len = strlen($message);
+ $chunk = 0;
+ //set up the loops for single and triple des
+ $iterations = ((count($keys) == 32) ? 3 : 9); //single or triple des
+ if ($iterations == 3) {$looping = (($encrypt) ? array (0, 32, 2) : array (30, -2, -2));}
+ else {$looping = (($encrypt) ? array (0, 32, 2, 62, 30, -2, 64, 96, 2) : array (94, 62, -2, 32, 64, 2, 30, -2, -2));}
+
+ $message .= (chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0)); //pad the message out with null bytes
+ //store the result here
+ $result = "";
+ $tempresult = "";
+
+ if ($mode == 1) { //CBC mode
+ $cbcleft = (ord($iv{$m++}) << 24) | (ord($iv{$m++}) << 16) | (ord($iv{$m++}) << 8) | ord($iv{$m++});
+ $cbcright = (ord($iv{$m++}) << 24) | (ord($iv{$m++}) << 16) | (ord($iv{$m++}) << 8) | ord($iv{$m++});
+ $m=0;
+ }
+
+ //loop through each 64 bit chunk of the message
+ while ($m < $len) {
+ $left = (ord($message{$m++}) << 24) | (ord($message{$m++}) << 16) | (ord($message{$m++}) << 8) | ord($message{$m++});
+ $right = (ord($message{$m++}) << 24) | (ord($message{$m++}) << 16) | (ord($message{$m++}) << 8) | ord($message{$m++});
+
+ //for Cipher Block Chaining mode, xor the message with the previous result
+ if ($mode == 1) {if ($encrypt) {$left ^= $cbcleft; $right ^= $cbcright;} else {$cbcleft2 = $cbcleft; $cbcright2 = $cbcright; $cbcleft = $left; $cbcright = $right;}}
+
+ //first each 64 but chunk of the message must be permuted according to IP
+ $temp = (($left >> 4 & $masks[4]) ^ $right) & 0x0f0f0f0f; $right ^= $temp; $left ^= ($temp << 4);
+ $temp = (($left >> 16 & $masks[16]) ^ $right) & 0x0000ffff; $right ^= $temp; $left ^= ($temp << 16);
+ $temp = (($right >> 2 & $masks[2]) ^ $left) & 0x33333333; $left ^= $temp; $right ^= ($temp << 2);
+ $temp = (($right >> 8 & $masks[8]) ^ $left) & 0x00ff00ff; $left ^= $temp; $right ^= ($temp << 8);
+ $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1);
+
+ $left = (($left << 1) | ($left >> 31 & $masks[31]));
+ $right = (($right << 1) | ($right >> 31 & $masks[31]));
+
+ //do this either 1 or 3 times for each chunk of the message
+ for ($j=0; $j<$iterations; $j+=3) {
+ $endloop = $looping[$j+1];
+ $loopinc = $looping[$j+2];
+ //now go through and perform the encryption or decryption
+ for ($i=$looping[$j]; $i!=$endloop; $i+=$loopinc) { //for efficiency
+ $right1 = $right ^ $keys[$i];
+ $right2 = (($right >> 4 & $masks[4]) | ($right << 28)) ^ $keys[$i+1];
+ //the result is attained by passing these bytes through the S selection functions
+ $temp = $left;
+ $left = $right;
+ $right = $temp ^ ($spfunction2[($right1 >> 24 & $masks[24]) & 0x3f] | $spfunction4[($right1 >> 16 & $masks[16]) & 0x3f]
+ | $spfunction6[($right1 >> 8 & $masks[8]) & 0x3f] | $spfunction8[$right1 & 0x3f]
+ | $spfunction1[($right2 >> 24 & $masks[24]) & 0x3f] | $spfunction3[($right2 >> 16 & $masks[16]) & 0x3f]
+ | $spfunction5[($right2 >> 8 & $masks[8]) & 0x3f] | $spfunction7[$right2 & 0x3f]);
+ }
+ $temp = $left; $left = $right; $right = $temp; //unreverse left and right
+ } //for either 1 or 3 iterations
+
+ //move then each one bit to the right
+ $left = (($left >> 1 & $masks[1]) | ($left << 31));
+ $right = (($right >> 1 & $masks[1]) | ($right << 31));
+
+ //now perform IP-1, which is IP in the opposite direction
+ $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1);
+ $temp = (($right >> 8 & $masks[8]) ^ $left) & 0x00ff00ff; $left ^= $temp; $right ^= ($temp << 8);
+ $temp = (($right >> 2 & $masks[2]) ^ $left) & 0x33333333; $left ^= $temp; $right ^= ($temp << 2);
+ $temp = (($left >> 16 & $masks[16]) ^ $right) & 0x0000ffff; $right ^= $temp; $left ^= ($temp << 16);
+ $temp = (($left >> 4 & $masks[4]) ^ $right) & 0x0f0f0f0f; $right ^= $temp; $left ^= ($temp << 4);
+
+ //for Cipher Block Chaining mode, xor the message with the previous result
+ if ($mode == 1) {if ($encrypt) {$cbcleft = $left; $cbcright = $right;} else {$left ^= $cbcleft2; $right ^= $cbcright2;}}
+ $tempresult .= (chr($left>>24 & $masks[24]) . chr(($left>>16 & $masks[16]) & 0xff) . chr(($left>>8 & $masks[8]) & 0xff) . chr($left & 0xff) . chr($right>>24 & $masks[24]) . chr(($right>>16 & $masks[16]) & 0xff) . chr(($right>>8 & $masks[8]) & 0xff) . chr($right & 0xff));
+
+ $chunk += 8;
+ if ($chunk == 512) {$result .= $tempresult; $tempresult = ""; $chunk = 0;}
+ } //for every 8 characters, or 64 bits in the message
+
+ //return the result as an array
+ return ($result . $tempresult);
+} //end of des
+
+//des_createKeys
+//this takes as input a 64 bit key (even though only 56 bits are used)
+//as an array of 2 integers, and returns 16 48 bit keys
+function des_createKeys ($key) {
+ //declaring this locally speeds things up a bit
+ $pc2bytes0 = array (0,0x4,0x20000000,0x20000004,0x10000,0x10004,0x20010000,0x20010004,0x200,0x204,0x20000200,0x20000204,0x10200,0x10204,0x20010200,0x20010204);
+ $pc2bytes1 = array (0,0x1,0x100000,0x100001,0x4000000,0x4000001,0x4100000,0x4100001,0x100,0x101,0x100100,0x100101,0x4000100,0x4000101,0x4100100,0x4100101);
+ $pc2bytes2 = array (0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808,0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808);
+ $pc2bytes3 = array (0,0x200000,0x8000000,0x8200000,0x2000,0x202000,0x8002000,0x8202000,0x20000,0x220000,0x8020000,0x8220000,0x22000,0x222000,0x8022000,0x8222000);
+ $pc2bytes4 = array (0,0x40000,0x10,0x40010,0,0x40000,0x10,0x40010,0x1000,0x41000,0x1010,0x41010,0x1000,0x41000,0x1010,0x41010);
+ $pc2bytes5 = array (0,0x400,0x20,0x420,0,0x400,0x20,0x420,0x2000000,0x2000400,0x2000020,0x2000420,0x2000000,0x2000400,0x2000020,0x2000420);
+ $pc2bytes6 = array (0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002,0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002);
+ $pc2bytes7 = array (0,0x10000,0x800,0x10800,0x20000000,0x20010000,0x20000800,0x20010800,0x20000,0x30000,0x20800,0x30800,0x20020000,0x20030000,0x20020800,0x20030800);
+ $pc2bytes8 = array (0,0x40000,0,0x40000,0x2,0x40002,0x2,0x40002,0x2000000,0x2040000,0x2000000,0x2040000,0x2000002,0x2040002,0x2000002,0x2040002);
+ $pc2bytes9 = array (0,0x10000000,0x8,0x10000008,0,0x10000000,0x8,0x10000008,0x400,0x10000400,0x408,0x10000408,0x400,0x10000400,0x408,0x10000408);
+ $pc2bytes10 = array (0,0x20,0,0x20,0x100000,0x100020,0x100000,0x100020,0x2000,0x2020,0x2000,0x2020,0x102000,0x102020,0x102000,0x102020);
+ $pc2bytes11 = array (0,0x1000000,0x200,0x1000200,0x200000,0x1200000,0x200200,0x1200200,0x4000000,0x5000000,0x4000200,0x5000200,0x4200000,0x5200000,0x4200200,0x5200200);
+ $pc2bytes12 = array (0,0x1000,0x8000000,0x8001000,0x80000,0x81000,0x8080000,0x8081000,0x10,0x1010,0x8000010,0x8001010,0x80010,0x81010,0x8080010,0x8081010);
+ $pc2bytes13 = array (0,0x4,0x100,0x104,0,0x4,0x100,0x104,0x1,0x5,0x101,0x105,0x1,0x5,0x101,0x105);
+ $masks = array (4294967295,2147483647,1073741823,536870911,268435455,134217727,67108863,33554431,16777215,8388607,4194303,2097151,1048575,524287,262143,131071,65535,32767,16383,8191,4095,2047,1023,511,255,127,63,31,15,7,3,1,0);
+
+ //how many iterations (1 for des, 3 for triple des)
+ $iterations = ((strlen($key) >= 24) ? 3 : 1);
+ //stores the return keys
+ $keys = array (); // size = 32 * iterations but you don't specify this in php
+ //now define the left shifts which need to be done
+ $shifts = array (0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0);
+ //other variables
+ $m=0;
+ $n=0;
+
+ for ($j=0; $j<$iterations; $j++) { //either 1 or 3 iterations
+ $left = (ord($key{$m++}) << 24) | (ord($key{$m++}) << 16) | (ord($key{$m++}) << 8) | ord($key{$m++});
+ $right = (ord($key{$m++}) << 24) | (ord($key{$m++}) << 16) | (ord($key{$m++}) << 8) | ord($key{$m++});
+
+ $temp = (($left >> 4 & $masks[4]) ^ $right) & 0x0f0f0f0f; $right ^= $temp; $left ^= ($temp << 4);
+ $temp = (($right >> 16 & $masks[16]) ^ $left) & 0x0000ffff; $left ^= $temp; $right ^= ($temp << -16);
+ $temp = (($left >> 2 & $masks[2]) ^ $right) & 0x33333333; $right ^= $temp; $left ^= ($temp << 2);
+ $temp = (($right >> 16 & $masks[16]) ^ $left) & 0x0000ffff; $left ^= $temp; $right ^= ($temp << -16);
+ $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1);
+ $temp = (($right >> 8 & $masks[8]) ^ $left) & 0x00ff00ff; $left ^= $temp; $right ^= ($temp << 8);
+ $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1);
+
+ //the right side needs to be shifted and to get the last four bits of the left side
+ $temp = ($left << 8) | (($right >> 20 & $masks[20]) & 0x000000f0);
+ //left needs to be put upside down
+ $left = ($right << 24) | (($right << 8) & 0xff0000) | (($right >> 8 & $masks[8]) & 0xff00) | (($right >> 24 & $masks[24]) & 0xf0);
+ $right = $temp;
+
+ //now go through and perform these shifts on the left and right keys
+ for ($i=0; $i < count($shifts); $i++) {
+ //shift the keys either one or two bits to the left
+ if ($shifts[$i] > 0) {
+ $left = (($left << 2) | ($left >> 26 & $masks[26]));
+ $right = (($right << 2) | ($right >> 26 & $masks[26]));
+ } else {
+ $left = (($left << 1) | ($left >> 27 & $masks[27]));
+ $right = (($right << 1) | ($right >> 27 & $masks[27]));
+ }
+ $left = $left & -0xf;
+ $right = $right & -0xf;
+
+ //now apply PC-2, in such a way that E is easier when encrypting or decrypting
+ //this conversion will look like PC-2 except only the last 6 bits of each byte are used
+ //rather than 48 consecutive bits and the order of lines will be according to
+ //how the S selection functions will be applied: S2, S4, S6, S8, S1, S3, S5, S7
+ $lefttemp = $pc2bytes0[$left >> 28 & $masks[28]] | $pc2bytes1[($left >> 24 & $masks[24]) & 0xf]
+ | $pc2bytes2[($left >> 20 & $masks[20]) & 0xf] | $pc2bytes3[($left >> 16 & $masks[16]) & 0xf]
+ | $pc2bytes4[($left >> 12 & $masks[12]) & 0xf] | $pc2bytes5[($left >> 8 & $masks[8]) & 0xf]
+ | $pc2bytes6[($left >> 4 & $masks[4]) & 0xf];
+ $righttemp = $pc2bytes7[$right >> 28 & $masks[28]] | $pc2bytes8[($right >> 24 & $masks[24]) & 0xf]
+ | $pc2bytes9[($right >> 20 & $masks[20]) & 0xf] | $pc2bytes10[($right >> 16 & $masks[16]) & 0xf]
+ | $pc2bytes11[($right >> 12 & $masks[12]) & 0xf] | $pc2bytes12[($right >> 8 & $masks[8]) & 0xf]
+ | $pc2bytes13[($right >> 4 & $masks[4]) & 0xf];
+ $temp = (($righttemp >> 16 & $masks[16]) ^ $lefttemp) & 0x0000ffff;
+ $keys[$n++] = $lefttemp ^ $temp; $keys[$n++] = $righttemp ^ ($temp << 16);
+ }
+ } //for each iterations
+ //return the keys we've created
+ return $keys;
+} //end of des_createKeys
+
+/*
+////////////////////////////// TEST //////////////////////////////
+function stringToHex ($s) {
+ $r = "0x";
+ $hexes = array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
+ for ($i=0; $i<strlen($s); $i++) {$r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);}
+ return $r;
+}
+echo "<PRE>";
+$key = "this is a 24 byte key !!";
+$message = "This is a test message";
+$ciphertext = des ($key, $message, 1, 0, null);
+echo "DES Test Encrypted: " . stringToHex ($ciphertext);
+$recovered_message = des ($key, $ciphertext, 0, 0, null);
+echo "\n";
+echo "DES Test Decrypted: " . $recovered_message;
+*/
+?> \ No newline at end of file
diff --git a/program/lib/enriched.inc b/program/lib/enriched.inc
new file mode 100644
index 000000000..2435a8233
--- /dev/null
+++ b/program/lib/enriched.inc
@@ -0,0 +1,114 @@
+<?php
+/*
+ File: read_enriched.inc
+ Author: Ryo Chijiiwa
+ License: GPL (part of IlohaMail)
+ Purpose: functions for handling text/enriched messages
+ Reference: RFC 1523, 1896
+*/
+
+
+function enriched_convert_newlines($str){
+ //remove single newlines, convert N newlines to N-1
+
+ $str = str_replace("\r\n","\n",$str);
+ $len = strlen($str);
+
+ $nl = 0;
+ $out = '';
+ for($i=0;$i<$len;$i++){
+ $c = $str[$i];
+ if (ord($c)==10) $nl++;
+ if ($nl && ord($c)!=10) $nl = 0;
+ if ($nl!=1) $out.=$c;
+ else $out.=' ';
+ }
+ return $out;
+}
+
+function enriched_convert_formatting($body){
+ $a=array('<bold>'=>'<b>','</bold>'=>'</b>','<italic>'=>'<i>',
+ '</italic>'=>'</i>','<fixed>'=>'<tt>','</fixed>'=>'</tt>',
+ '<smaller>'=>'<font size=-1>','</smaller>'=>'</font>',
+ '<bigger>'=>'<font size=+1>','</bigger>'=>'</font>',
+ '<underline>'=>'<span style="text-decoration: underline">',
+ '</underline>'=>'</span>',
+ '<flushleft>'=>'<span style="text-align:left">',
+ '</flushleft>'=>'</span>',
+ '<flushright>'=>'<span style="text-align:right">',
+ '</flushright>'=>'</span>',
+ '<flushboth>'=>'<span style="text-align:justified">',
+ '</flushboth>'=>'</span>',
+ '<indent>'=>'<span style="padding-left: 20px">',
+ '</indent>'=>'</span>',
+ '<indentright>'=>'<span style="padding-right: 20px">',
+ '</indentright>'=>'</span>');
+
+ while(list($find,$replace)=each($a)){
+ $body = eregi_replace($find,$replace,$body);
+ }
+ return $body;
+}
+
+function enriched_font($body){
+ $pattern = '/(.*)\<fontfamily\>\<param\>(.*)\<\/param\>(.*)\<\/fontfamily\>(.*)/ims';
+ while(preg_match($pattern,$body,$a)){
+ //print_r($a);
+ if (count($a)!=5) continue;
+ $body=$a[1].'<span style="font-family: '.$a[2].'">'.$a[3].'</span>'.$a[4];
+ }
+
+ return $body;
+}
+
+
+function enriched_color($body){
+ $pattern = '/(.*)\<color\>\<param\>(.*)\<\/param\>(.*)\<\/color\>(.*)/ims';
+ while(preg_match($pattern,$body,$a)){
+ //print_r($a);
+ if (count($a)!=5) continue;
+
+ //extract color (either by name, or ####,####,####)
+ if (strpos($a[2],',')){
+ $rgb = explode(',',$a[2]);
+ $color ='#';
+ for($i=0;$i<3;$i++) $color.=substr($rgb[$i],0,2); //just take first 2 bytes
+ }else{
+ $color = $a[2];
+ }
+
+ //put it all together
+ $body = $a[1].'<span style="color: '.$color.'">'.$a[3].'</span>'.$a[4];
+ }
+
+ return $body;
+}
+
+function enriched_excerpt($body){
+
+ $pattern = '/(.*)\<excerpt\>(.*)\<\/excerpt\>(.*)/i';
+ while(preg_match($pattern,$body,$a)){
+ //print_r($a);
+ if (count($a)!=4) continue;
+ $quoted = '';
+ $lines = explode('<br>',$a[2]);
+ foreach($lines as $n=>$line) $quoted.='&gt;'.$line.'<br>';
+ $body=$a[1].'<span class="quotes">'.$quoted.'</span>'.$a[3];
+ }
+
+ return $body;
+}
+
+function enriched_to_html($body){
+ $body = str_replace('<<','&lt;',$body);
+ $body = enriched_convert_newlines($body);
+ $body = str_replace("\n", '<br>', $body);
+ $body = enriched_convert_formatting($body);
+ $body = enriched_color($body);
+ $body = enriched_font($body);
+ $body = enriched_excerpt($body);
+ //$body = nl2br($body);
+ return $body;
+}
+
+?> \ No newline at end of file
diff --git a/program/lib/html2text.inc b/program/lib/html2text.inc
new file mode 100644
index 000000000..82a254e56
--- /dev/null
+++ b/program/lib/html2text.inc
@@ -0,0 +1,440 @@
+<?php
+
+/*************************************************************************
+* *
+* class.html2text.inc *
+* *
+*************************************************************************
+* *
+* Converts HTML to formatted plain text *
+* *
+* Copyright (c) 2005 Jon Abernathy <jon@chuggnutt.com> *
+* All rights reserved. *
+* *
+* This script is free software; you can redistribute it and/or modify *
+* it under the terms of the GNU General Public License as published by *
+* the Free Software Foundation; either version 2 of the License, or *
+* (at your option) any later version. *
+* *
+* The GNU General Public License can be found at *
+* http://www.gnu.org/copyleft/gpl.html. *
+* *
+* This script is distributed in the hope that it will be useful, *
+* but WITHOUT ANY WARRANTY; without even the implied warranty of *
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+* GNU General Public License for more details. *
+* *
+* Author(s): Jon Abernathy <jon@chuggnutt.com> *
+* *
+* Last modified: 04/06/05 *
+* Modified: 2004/05/19 (tbr) *
+* *
+*************************************************************************/
+
+
+/**
+* Takes HTML and converts it to formatted, plain text.
+*
+* Thanks to Alexander Krug (http://www.krugar.de/) to pointing out and
+* correcting an error in the regexp search array. Fixed 7/30/03.
+*
+* Updated set_html() function's file reading mechanism, 9/25/03.
+*
+* Thanks to Joss Sanglier (http://www.dancingbear.co.uk/) for adding
+* several more HTML entity codes to the $search and $replace arrays.
+* Updated 11/7/03.
+*
+* Thanks to Darius Kasperavicius (http://www.dar.dar.lt/) for
+* suggesting the addition of $allowed_tags and its supporting function
+* (which I slightly modified). Updated 3/12/04.
+*
+* Thanks to Justin Dearing for pointing out that a replacement for the
+* <TH> tag was missing, and suggesting an appropriate fix.
+* Updated 8/25/04.
+*
+* Thanks to Mathieu Collas (http://www.myefarm.com/) for finding a
+* display/formatting bug in the _build_link_list() function: email
+* readers would show the left bracket and number ("[1") as part of the
+* rendered email address.
+* Updated 12/16/04.
+*
+* Thanks to Wojciech Bajon (http://histeria.pl/) for submitting code
+* to handle relative links, which I hadn't considered. I modified his
+* code a bit to handle normal HTTP links and MAILTO links. Also for
+* suggesting three additional HTML entity codes to search for.
+* Updated 03/02/05.
+*
+* Thanks to Jacob Chandler for pointing out another link condition
+* for the _build_link_list() function: "https".
+* Updated 04/06/05.
+*
+* @author Jon Abernathy <jon@chuggnutt.com>
+* @version 0.6.1
+* @since PHP 4.0.2
+*/
+class html2text
+{
+
+ /**
+ * Contains the HTML content to convert.
+ *
+ * @var string $html
+ * @access public
+ */
+ var $html;
+
+ /**
+ * Contains the converted, formatted text.
+ *
+ * @var string $text
+ * @access public
+ */
+ var $text;
+
+ /**
+ * Maximum width of the formatted text, in columns.
+ *
+ * @var integer $width
+ * @access public
+ */
+ var $width = 70;
+
+ /**
+ * List of preg* regular expression patterns to search for,
+ * used in conjunction with $replace.
+ *
+ * @var array $search
+ * @access public
+ * @see $replace
+ */
+ var $search = array(
+ "/\r/", // Non-legal carriage return
+ "/[\n\t]+/", // Newlines and tabs
+ '/<script[^>]*>.*?<\/script>/i', // <script>s -- which strip_tags supposedly has problems with
+ //'/<!-- .* -->/', // Comments -- which strip_tags might have problem a with
+ '/<a href="([^"]+)"[^>]*>(.+?)<\/a>/ie', // <a href="">
+ '/<h[123][^>]*>(.+?)<\/h[123]>/ie', // H1 - H3
+ '/<h[456][^>]*>(.+?)<\/h[456]>/ie', // H4 - H6
+ '/<p[^>]*>/i', // <P>
+ '/<br[^>]*>/i', // <br>
+ '/<b[^>]*>(.+?)<\/b>/ie', // <b>
+ '/<i[^>]*>(.+?)<\/i>/i', // <i>
+ '/(<ul[^>]*>|<\/ul>)/i', // <ul> and </ul>
+ '/(<ol[^>]*>|<\/ol>)/i', // <ol> and </ol>
+ '/<li[^>]*>/i', // <li>
+ '/<hr[^>]*>/i', // <hr>
+ '/(<table[^>]*>|<\/table>)/i', // <table> and </table>
+ '/(<tr[^>]*>|<\/tr>)/i', // <tr> and </tr>
+ '/<td[^>]*>(.+?)<\/td>/i', // <td> and </td>
+ '/<th[^>]*>(.+?)<\/th>/i', // <th> and </th>
+ '/&nbsp;/i',
+ '/&quot;/i',
+ '/&gt;/i',
+ '/&lt;/i',
+ '/&amp;/i',
+ '/&copy;/i',
+ '/&trade;/i',
+ '/&#8220;/',
+ '/&#8221;/',
+ '/&#8211;/',
+ '/&#8217;/',
+ '/&#38;/',
+ '/&#169;/',
+ '/&#8482;/',
+ '/&#151;/',
+ '/&#147;/',
+ '/&#148;/',
+ '/&#149;/',
+ '/&reg;/i',
+ '/&bull;/i',
+ '/&[&;]+;/i'
+ );
+
+ /**
+ * List of pattern replacements corresponding to patterns searched.
+ *
+ * @var array $replace
+ * @access public
+ * @see $search
+ */
+ var $replace = array(
+ '', // Non-legal carriage return
+ ' ', // Newlines and tabs
+ '', // <script>s -- which strip_tags supposedly has problems with
+ //'', // Comments -- which strip_tags might have problem a with
+ '$this->_build_link_list("\\1", "\\2")', // <a href="">
+ "strtoupper(\"\n\n\\1\n\n\")", // H1 - H3
+ "ucwords(\"\n\n\\1\n\n\")", // H4 - H6
+ "\n\n\t", // <P>
+ "\n", // <br>
+ 'strtoupper("\\1")', // <b>
+ '_\\1_', // <i>
+ "\n\n", // <ul> and </ul>
+ "\n\n", // <ol> and </ol>
+ "\t*", // <li>
+ "\n-------------------------\n", // <hr>
+ "\n\n", // <table> and </table>
+ "\n", // <tr> and </tr>
+ "\t\t\\1\n", // <td> and </td>
+ "strtoupper(\"\t\t\\1\n\")", // <th> and </th>
+ ' ',
+ '"',
+ '>',
+ '<',
+ '&',
+ '(c)',
+ '(tm)',
+ '"',
+ '"',
+ '-',
+ "'",
+ '&',
+ '(c)',
+ '(tm)',
+ '--',
+ '"',
+ '"',
+ '*',
+ '(R)',
+ '*',
+ ''
+ );
+
+ /**
+ * Contains a list of HTML tags to allow in the resulting text.
+ *
+ * @var string $allowed_tags
+ * @access public
+ * @see set_allowed_tags()
+ */
+ var $allowed_tags = '';
+
+ /**
+ * Contains the base URL that relative links should resolve to.
+ *
+ * @var string $url
+ * @access public
+ */
+ var $url;
+
+ /**
+ * Indicates whether content in the $html variable has been converted yet.
+ *
+ * @var boolean $converted
+ * @access private
+ * @see $html, $text
+ */
+ var $_converted = false;
+
+ /**
+ * Contains URL addresses from links to be rendered in plain text.
+ *
+ * @var string $link_list
+ * @access private
+ * @see _build_link_list()
+ */
+ var $_link_list = array();
+
+ /**
+ * Constructor.
+ *
+ * If the HTML source string (or file) is supplied, the class
+ * will instantiate with that source propagated, all that has
+ * to be done it to call get_text().
+ *
+ * @param string $source HTML content
+ * @param boolean $from_file Indicates $source is a file to pull content from
+ * @access public
+ * @return void
+ */
+ function html2text( $source = '', $from_file = false )
+ {
+ if ( !empty($source) ) {
+ $this->set_html($source, $from_file);
+ }
+ $this->set_base_url();
+ }
+
+ /**
+ * Loads source HTML into memory, either from $source string or a file.
+ *
+ * @param string $source HTML content
+ * @param boolean $from_file Indicates $source is a file to pull content from
+ * @access public
+ * @return void
+ */
+ function set_html( $source, $from_file = false )
+ {
+ $this->html = $source;
+
+ if ( $from_file && file_exists($source) ) {
+ $fp = fopen($source, 'r');
+ $this->html = fread($fp, filesize($source));
+ fclose($fp);
+ }
+
+ $this->_converted = false;
+ }
+
+ /**
+ * Returns the text, converted from HTML.
+ *
+ * @access public
+ * @return string
+ */
+ function get_text()
+ {
+ if ( !$this->_converted ) {
+ $this->_convert();
+ }
+
+ return $this->text;
+ }
+
+ /**
+ * Prints the text, converted from HTML.
+ *
+ * @access public
+ * @return void
+ */
+ function print_text()
+ {
+ print $this->get_text();
+ }
+
+ /**
+ * Alias to print_text(), operates identically.
+ *
+ * @access public
+ * @return void
+ * @see print_text()
+ */
+ function p()
+ {
+ print $this->get_text();
+ }
+
+ /**
+ * Sets the allowed HTML tags to pass through to the resulting text.
+ *
+ * Tags should be in the form "<p>", with no corresponding closing tag.
+ *
+ * @access public
+ * @return void
+ */
+ function set_allowed_tags( $allowed_tags = '' )
+ {
+ if ( !empty($allowed_tags) ) {
+ $this->allowed_tags = $allowed_tags;
+ }
+ }
+
+ /**
+ * Sets a base URL to handle relative links.
+ *
+ * @access public
+ * @return void
+ */
+ function set_base_url( $url = '' )
+ {
+ if ( empty($url) ) {
+ $this->url = 'http://' . $_SERVER['HTTP_HOST'];
+ } else {
+ // Strip any trailing slashes for consistency (relative
+ // URLs may already start with a slash like "/file.html")
+ if ( substr($url, -1) == '/' ) {
+ $url = substr($url, 0, -1);
+ }
+ $this->url = $url;
+ }
+ }
+
+ /**
+ * Workhorse function that does actual conversion.
+ *
+ * First performs custom tag replacement specified by $search and
+ * $replace arrays. Then strips any remaining HTML tags, reduces whitespace
+ * and newlines to a readable format, and word wraps the text to
+ * $width characters.
+ *
+ * @access private
+ * @return void
+ */
+ function _convert()
+ {
+ // Variables used for building the link list
+ //$link_count = 1;
+ //$this->_link_list = '';
+
+ $text = trim(stripslashes($this->html));
+
+ // Run our defined search-and-replace
+ $text = preg_replace($this->search, $this->replace, $text);
+
+ // Strip any other HTML tags
+ $text = strip_tags($text, $this->allowed_tags);
+
+ // Bring down number of empty lines to 2 max
+ $text = preg_replace("/\n\s+\n/", "\n", $text);
+ $text = preg_replace("/[\n]{3,}/", "\n\n", $text);
+
+ // Add link list
+ if ( sizeof($this->_link_list) ) {
+ $text .= "\n\nLinks:\n------\n";
+ foreach ($this->_link_list as $id => $link) {
+ $text .= '[' . ($id+1) . '] ' . $link . "\n";
+ }
+ }
+
+ // Wrap the text to a readable format
+ // for PHP versions >= 4.0.2. Default width is 75
+ $text = wordwrap($text, $this->width);
+
+ $this->text = $text;
+
+ $this->_converted = true;
+ }
+
+ /**
+ * Helper function called by preg_replace() on link replacement.
+ *
+ * Maintains an internal list of links to be displayed at the end of the
+ * text, with numeric indices to the original point in the text they
+ * appeared. Also makes an effort at identifying and handling absolute
+ * and relative links.
+ *
+ * @param integer $link_count Counter tracking current link number
+ * @param string $link URL of the link
+ * @param string $display Part of the text to associate number with
+ * @access private
+ * @return string
+ */
+ function _build_link_list($link, $display)
+ {
+ $link_lc = strtolower($link);
+
+ if (substr($link_lc, 0, 7) == 'http://' || substr($link_lc, 0, 8) == 'https://' || substr($link_lc, 0, 7) == 'mailto:')
+ {
+ $url = $link;
+ }
+ else
+ {
+ $url = $this->url;
+ if ($link{0} != '/') {
+ $url .= '/';
+ }
+ $url .= $link;
+ }
+
+ $index = array_search($url, $this->_link_list);
+ if ($index===FALSE)
+ {
+ $index = sizeof($this->_link_list);
+ $this->_link_list[$index] = $url;
+ }
+
+ return $display . ' [' . ($index+1) . ']';
+ }
+}
+
+?> \ No newline at end of file
diff --git a/program/lib/icl_commons.inc b/program/lib/icl_commons.inc
new file mode 100644
index 000000000..599205178
--- /dev/null
+++ b/program/lib/icl_commons.inc
@@ -0,0 +1,81 @@
+<?php
+function mod_b64_decode($data){
+ return base64_decode(str_replace(",","/",$data));
+}
+
+function mod_b64_encode($data){
+ return str_replace("/",",",str_replace("=","",base64_encode($data)));
+}
+
+
+function utf8_to_html($str){
+ $len = strlen($str);
+ $out = "";
+ for($i=0;$i<$len;$i+=2){
+ $val = ord($str[$i]);
+ $next_val = ord($str[$i+1]);
+ if ($val<255){
+ $out.="&#".($val*256+$next_val).";";
+ }else{
+ $out.=$str[$i].$str[$i+1];
+ }
+ }
+ return $out;
+}
+
+function iil_utf7_decode($str, $raw=false){
+ if (strpos($str, '&')===false) return $str;
+
+ $len = strlen($str);
+ $in_b64 = false;
+ $b64_data = "";
+ $out = "";
+ for ($i=0;$i<$len;$i++){
+ $char = $str[$i];
+ if ($char=='&') $in_b64 = true;
+ else if ($in_b64 && $char=='-'){
+ $in_b64 = false;
+ if ($b64_data=="") $out.="&";
+ else{
+ $dec=mod_b64_decode($b64_data);
+ $out.=($raw?$dec:utf8_to_html($dec));
+ $b64_data = "";
+ }
+ }else if ($in_b64) $b64_data.=$char;
+ else $out.=$char;
+ }
+ return $out;
+}
+
+function iil_utf7_encode($str){
+ if (!ereg("[\200-\237]",$str) && !ereg("[\241-\377]",$str))
+ return $str;
+
+ $len = strlen($str);
+
+ for ($i=0;$i<$len;$i++){
+ $val = ord($str[$i]);
+ if ($val>=224 && $val<=239){
+ $unicode = ($val-224) * 4096 + (ord($str[$i+1])-128) * 64 + (ord($str[$i+2])-128);
+ $i+=2;
+ $utf_code.=chr((int)($unicode/256)).chr($unicode%256);
+ }else if ($val>=192 && $val<=223){
+ $unicode = ($val-192) * 64 + (ord($str[$i+1])-128);
+ $i++;
+ $utf_code.=chr((int)($unicode/256)).chr($unicode%256);
+ }else{
+ if ($utf_code){
+ $out.='&'.mod_b64_encode($utf_code).'-';
+ $utf_code="";
+ }
+ if ($str[$i]=="-") $out.="&";
+ $out.=$str[$i];
+ }
+ }
+ if ($utf_code)
+ $out.='&'.mod_b64_encode($utf_code).'-';
+ return $out;
+}
+
+
+?> \ No newline at end of file
diff --git a/program/lib/imap.inc b/program/lib/imap.inc
new file mode 100644
index 000000000..53a518bee
--- /dev/null
+++ b/program/lib/imap.inc
@@ -0,0 +1,2038 @@
+<?php
+/////////////////////////////////////////////////////////
+//
+// Iloha IMAP Library (IIL)
+//
+// (C)Copyright 2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
+//
+// This file is part of IlohaMail. IlohaMail is free software released
+// under the GPL license. See enclosed file COPYING for details, or
+// see http://www.fsf.org/copyleft/gpl.html
+//
+/////////////////////////////////////////////////////////
+
+/********************************************************
+
+ FILE: include/imap.inc
+ PURPOSE:
+ Provide alternative IMAP library that doesn't rely on the standard
+ C-Client based version. This allows IlohaMail to function regardless
+ of whether or not the PHP build it's running on has IMAP functionality
+ built-in.
+ USEAGE:
+ Function containing "_C_" in name require connection handler to be
+ passed as one of the parameters. To obtain connection handler, use
+ iil_Connect()
+
+********************************************************/
+
+// changed path to work within roundcube webmail
+include_once("lib/icl_commons.inc");
+
+
+if (!$IMAP_USE_HEADER_DATE) $IMAP_USE_INTERNAL_DATE = true;
+$IMAP_MONTHS=array("Jan"=>1,"Feb"=>2,"Mar"=>3,"Apr"=>4,"May"=>5,"Jun"=>6,"Jul"=>7,"Aug"=>8,"Sep"=>9,"Oct"=>10,"Nov"=>11,"Dec"=>12);
+$IMAP_SERVER_TZ = date('Z');
+
+$iil_error;
+$iil_errornum;
+$iil_selected;
+
+class iilConnection{
+ var $fp;
+ var $error;
+ var $errorNum;
+ var $selected;
+ var $message;
+ var $host;
+ var $cache;
+ var $uid_cache;
+ var $do_cache;
+ var $exists;
+ var $recent;
+ var $rootdir;
+ var $delimiter;
+}
+
+class iilBasicHeader{
+ var $id;
+ var $uid;
+ var $subject;
+ var $from;
+ var $to;
+ var $cc;
+ var $replyto;
+ var $in_reply_to;
+ var $date;
+ var $messageID;
+ var $size;
+ var $encoding;
+ var $ctype;
+ var $flags;
+ var $timestamp;
+ var $f;
+ var $seen;
+ var $deleted;
+ var $recent;
+ var $answered;
+ var $junk;
+ var $internaldate;
+ var $is_reply;
+}
+
+
+class iilThreadHeader{
+ var $id;
+ var $sbj;
+ var $irt;
+ var $mid;
+}
+
+
+function iil_xor($string, $string2){
+ $result = "";
+ $size = strlen($string);
+ for ($i=0; $i<$size; $i++) $result .= chr(ord($string[$i]) ^ ord($string2[$i]));
+
+ return $result;
+}
+
+function iil_ReadLine($fp, $size){
+ $line="";
+ if ($fp){
+ do{
+ $buffer = fgets($fp, 2048);
+ $line.=$buffer;
+ }while($buffer[strlen($buffer)-1]!="\n");
+ }
+ return $line;
+}
+
+function iil_MultLine($fp, $line){
+ $line = chop($line);
+ if (ereg('\{[0-9]+\}$', $line)){
+ $out = "";
+ preg_match_all('/(.*)\{([0-9]+)\}$/', $line, $a);
+ $bytes = $a[2][0];
+ while(strlen($out)<$bytes){
+ $out.=chop(iil_ReadLine($fp, 1024));
+ }
+ $line = $a[1][0]."\"$out\"";
+ }
+ return $line;
+}
+
+function iil_ReadBytes($fp, $bytes){
+ $data = "";
+ $len = 0;
+ do{
+ $data.=fread($fp, $bytes-$len);
+ $len = strlen($data);
+ }while($len<$bytes);
+ return $data;
+}
+
+function iil_ReadReply($fp){
+ do{
+ $line = chop(trim(iil_ReadLine($fp, 1024)));
+ }while($line[0]=="*");
+
+ return $line;
+}
+
+function iil_ParseResult($string){
+ $a=explode(" ", $string);
+ if (count($a) > 2){
+ if (strcasecmp($a[1], "OK")==0) return 0;
+ else if (strcasecmp($a[1], "NO")==0) return -1;
+ else if (strcasecmp($a[1], "BAD")==0) return -2;
+ }else return -3;
+}
+
+// check if $string starts with $match
+function iil_StartsWith($string, $match){
+ $len = strlen($match);
+ if ($len==0) return false;
+ if (strncmp($string, $match, $len)==0) return true;
+ else return false;
+}
+
+function iil_StartsWithI($string, $match){
+ $len = strlen($match);
+ if ($len==0) return false;
+ if (strncasecmp($string, $match, $len)==0) return true;
+ else return false;
+}
+
+
+function iil_C_Authenticate(&$conn, $user, $pass, $encChallenge){
+
+ // initialize ipad, opad
+ for ($i=0;$i<64;$i++){
+ $ipad.=chr(0x36);
+ $opad.=chr(0x5C);
+ }
+ // pad $pass so it's 64 bytes
+ $padLen = 64 - strlen($pass);
+ for ($i=0;$i<$padLen;$i++) $pass .= chr(0);
+ // generate hash
+ $hash = md5(iil_xor($pass,$opad).pack("H*",md5(iil_xor($pass, $ipad).base64_decode($encChallenge))));
+ // generate reply
+ $reply = base64_encode($user." ".$hash);
+
+ // send result, get reply
+ fputs($conn->fp, $reply."\r\n");
+ $line = iil_ReadLine($conn->fp, 1024);
+
+ // process result
+ if (iil_ParseResult($line)==0){
+ $conn->error .= "";
+ $conn->errorNum = 0;
+ return $conn->fp;
+ }else{
+ $conn->error .= 'Authentication failed (AUTH): <br>"'.htmlspecialchars($line)."\"";
+ $conn->errorNum = -2;
+ return false;
+ }
+}
+
+function iil_C_Login(&$conn, $user, $password){
+
+ fputs($conn->fp, "a001 LOGIN $user \"$password\"\r\n");
+
+ do{
+ $line = iil_ReadReply($conn->fp);
+ }while(!iil_StartsWith($line, "a001 "));
+ $a=explode(" ", $line);
+ if (strcmp($a[1],"OK")==0){
+ $result=$conn->fp;
+ $conn->error.="";
+ $conn->errorNum = 0;
+ }else{
+ $result=false;
+ fclose($conn->fp);
+ $conn->error .= 'Authentication failed (LOGIN):<br>"'.htmlspecialchars($line)."\"";
+ $conn->errorNum = -2;
+ }
+ return $result;
+}
+
+function iil_ParseNamespace2($str, &$i, $len=0, $l){
+ if (!$l) $str = str_replace("NIL", "()", $str);
+ if (!$len) $len = strlen($str);
+ $data = array();
+ $in_quotes = false;
+ $elem = 0;
+ for($i;$i<$len;$i++){
+ $c = (string)$str[$i];
+ if ($c=='(' && !$in_quotes){
+ $i++;
+ $data[$elem] = iil_ParseNamespace2($str, $i, $len, $l++);
+ $elem++;
+ }else if ($c==')' && !$in_quotes) return $data;
+ else if ($c=="\\"){
+ $i++;
+ if ($in_quotes) $data[$elem].=$c.$str[$i];
+ }else if ($c=='"'){
+ $in_quotes = !$in_quotes;
+ if (!$in_quotes) $elem++;
+ }else if ($in_quotes){
+ $data[$elem].=$c;
+ }
+ }
+ return $data;
+}
+
+function iil_C_NameSpace(&$conn){
+ global $my_prefs;
+
+ if ($my_prefs["rootdir"]) return true;
+
+ fputs($conn->fp, "ns1 NAMESPACE\r\n");
+ do{
+ $line = iil_ReadLine($conn->fp, 1024);
+ if (iil_StartsWith($line, "* NAMESPACE")){
+ $i = 0;
+ $data = iil_ParseNamespace2(substr($line,11), $i, 0, 0);
+ }
+ }while(!iil_StartsWith($line, "ns1"));
+
+ if (!is_array($data)) return false;
+
+ $user_space_data = $data[0];
+ if (!is_array($user_space_data)) return false;
+
+ $first_userspace = $user_space_data[0];
+ if (count($first_userspace)!=2) return false;
+
+ $conn->rootdir = $first_userspace[0];
+ $conn->delimiter = $first_userspace[1];
+ $my_prefs["rootdir"] = substr($conn->rootdir, 0, -1);
+
+ return true;
+
+}
+
+function iil_Connect($host, $user, $password){
+ global $iil_error, $iil_errornum;
+ global $ICL_SSL, $ICL_PORT;
+ global $IMAP_NO_CACHE;
+ global $my_prefs, $IMAP_USE_INTERNAL_DATE;
+
+ $iil_error = "";
+ $iil_errornum = 0;
+
+ //strip slashes
+ $user = stripslashes($user);
+ $password = stripslashes($password);
+
+ //set auth method
+ $auth_method = "plain";
+ if (func_num_args() >= 4){
+ $auth_array = func_get_arg(3);
+ if (is_array($auth_array)) $auth_method = $auth_array["imap"];
+ if (empty($auth_method)) $auth_method = "plain";
+ }
+ $message = "INITIAL: $auth_method\n";
+
+ $result = false;
+
+ //initialize connection
+ $conn = new iilConnection;
+ $conn->error="";
+ $conn->errorNum=0;
+ $conn->selected="";
+ $conn->user = $user;
+ $conn->host = $host;
+ $conn->cache = array();
+ $conn->do_cache = (function_exists("cache_write")&&!$IMAP_NO_CACHE);
+ $conn->cache_dirty = array();
+
+ if ($my_prefs['sort_field']=='INTERNALDATE') $IMAP_USE_INTERNAL_DATE = true;
+ else if ($my_prefs['sort_field']=='DATE') $IMAP_USE_INTERNAL_DATE = false;
+ //echo '<!-- conn sort_field: '.$my_prefs['sort_field'].' //-->';
+
+ //check input
+ if (empty($host)) $iil_error .= "Invalid host<br>\n";
+ if (empty($user)) $iil_error .= "Invalid user<br>\n";
+ if (empty($password)) $iil_error .= "Invalid password<br>\n";
+ if (!empty($iil_error)) return false;
+ if (!$ICL_PORT) $ICL_PORT = 143;
+
+ //check for SSL
+ if ($ICL_SSL){
+ $host = "ssl://".$host;
+ }
+
+ //open socket connection
+ $conn->fp = @fsockopen($host, $ICL_PORT);
+ if (!$conn->fp){
+ $iil_error = "Could not connect to $host at port $ICL_PORT";
+ $iil_errornum = -1;
+ return false;
+ }
+
+ $iil_error.="Socket connection established\r\n";
+ $line=iil_ReadLine($conn->fp, 300);
+
+ if (strcasecmp($auth_method, "check")==0){
+ //check for supported auth methods
+
+ //default to plain text auth
+ $auth_method = "plain";
+
+ //check for CRAM-MD5
+ fputs($conn->fp, "cp01 CAPABILITY\r\n");
+ do{
+ $line = trim(chop(iil_ReadLine($conn->fp, 100)));
+ $a = explode(" ", $line);
+ if ($line[0]=="*"){
+ while ( list($k, $w) = each($a) ){
+ if ((strcasecmp($w, "AUTH=CRAM_MD5")==0)||
+ (strcasecmp($w, "AUTH=CRAM-MD5")==0)){
+ $auth_method = "auth";
+ }
+ }
+ }
+ }while($a[0]!="cp01");
+ }
+
+ if (strcasecmp($auth_method, "auth")==0){
+ $conn->message.="Trying CRAM-MD5\n";
+ //do CRAM-MD5 authentication
+ fputs($conn->fp, "a000 AUTHENTICATE CRAM-MD5\r\n");
+ $line = trim(chop(iil_ReadLine($conn->fp, 1024)));
+ if ($line[0]=="+"){
+ $conn->message.='Got challenge: '.htmlspecialchars($line)."\n";
+ //got a challenge string, try CRAM-5
+ $result = iil_C_Authenticate($conn, $user, $password, substr($line,2));
+ $conn->message.= "Tried CRAM-MD5: $result \n";
+ }else{
+ $conn->message.='No challenge ('.htmlspecialchars($line)."), try plain\n";
+ $auth = "plain";
+ }
+ }
+
+ if ((!$result)||(strcasecmp($auth, "plain")==0)){
+ //do plain text auth
+ $result = iil_C_Login($conn, $user, $password);
+ $conn->message.="Tried PLAIN: $result \n";
+ }
+
+ $conn->message .= $auth;
+
+ if ($result){
+ iil_C_Namespace($conn);
+ return $conn;
+ }else{
+ $iil_error = $conn->error;
+ $iil_errornum = $conn->errorNum;
+ return false;
+ }
+}
+
+function iil_Close(&$conn){
+ iil_C_WriteCache($conn);
+ if (@fputs($conn->fp, "I LOGOUT\r\n")){
+ fgets($conn->fp, 1024);
+ fclose($conn->fp);
+ $conn->fp = false;
+ }
+}
+
+function iil_ClearCache($user, $host){
+}
+
+
+function iil_C_WriteCache(&$conn){
+ //echo "<!-- doing iil_C_WriteCache //-->\n";
+ if (!$conn->do_cache) return false;
+
+ if (is_array($conn->cache)){
+ while(list($folder,$data)=each($conn->cache)){
+ if ($folder && is_array($data) && $conn->cache_dirty[$folder]){
+ $key = $folder.".imap";
+ $result = cache_write($conn->user, $conn->host, $key, $data, true);
+ //echo "<!-- writing $key $data: $result //-->\n";
+ }
+ }
+ }
+}
+
+function iil_C_EnableCache(&$conn){
+ $conn->do_cache = true;
+}
+
+function iil_C_DisableCache(&$conn){
+ $conn->do_cache = false;
+}
+
+function iil_C_LoadCache(&$conn, $folder){
+ if (!$conn->do_cache) return false;
+
+ $key = $folder.".imap";
+ if (!is_array($conn->cache[$folder])){
+ $conn->cache[$folder] = cache_read($conn->user, $conn->host, $key);
+ $conn->cache_dirty[$folder] = false;
+ }
+}
+
+function iil_C_ExpireCachedItems(&$conn, $folder, $message_set){
+
+ if (!$conn->do_cache) return; //caching disabled
+ if (!is_array($conn->cache[$folder])) return; //cache not initialized|empty
+ if (count($conn->cache[$folder])==0) return; //cache not initialized|empty
+
+ $uids = iil_C_FetchHeaderIndex($conn, $folder, $message_set, "UID");
+ $num_removed = 0;
+ if (is_array($uids)){
+ //echo "<!-- unsetting: ".implode(",",$uids)." //-->\n";
+ while(list($n,$uid)=each($uids)){
+ unset($conn->cache[$folder][$uid]);
+ //$conn->cache[$folder][$uid] = false;
+ //$num_removed++;
+ }
+ $conn->cache_dirty[$folder] = true;
+
+ //echo '<!--'."\n";
+ //print_r($conn->cache);
+ //echo "\n".'//-->'."\n";
+ }else{
+ echo "<!-- failed to get uids: $message_set //-->\n";
+ }
+
+ /*
+ if ($num_removed>0){
+ $new_cache;
+ reset($conn->cache[$folder]);
+ while(list($uid,$item)=each($conn->cache[$folder])){
+ if ($item) $new_cache[$uid] = $conn->cache[$folder][$uid];
+ }
+ $conn->cache[$folder] = $new_cache;
+ }
+ */
+}
+
+function iil_ExplodeQuotedString($delimiter, $string){
+ $quotes=explode("\"", $string);
+ while ( list($key, $val) = each($quotes))
+ if (($key % 2) == 1)
+ $quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]);
+ $string=implode("\"", $quotes);
+
+ $result=explode($delimiter, $string);
+ while ( list($key, $val) = each($result) )
+ $result[$key] = str_replace("_!@!_", $delimiter, $result[$key]);
+
+ return $result;
+}
+
+function iil_CheckForRecent($host, $user, $password, $mailbox){
+ if (empty($mailbox)) $mailbox="INBOX";
+
+ $conn=iil_Connect($host, $user, $password, "plain");
+ $fp = $conn->fp;
+ if ($fp){
+ fputs($fp, "a002 EXAMINE \"$mailbox\"\r\n");
+ do{
+ $line=chop(iil_ReadLine($fp, 300));
+ $a=explode(" ", $line);
+ if (($a[0]=="*") && (strcasecmp($a[2], "RECENT")==0)) $result=(int)$a[1];
+ }while (!iil_StartsWith($a[0],"a002"));
+
+ fputs($fp, "a003 LOGOUT\r\n");
+ fclose($fp);
+ }else $result=-2;
+
+ return $result;
+}
+
+function iil_C_Select(&$conn, $mailbox){
+ $fp = $conn->fp;
+
+ if (empty($mailbox)) return false;
+ if (strcmp($conn->selected, $mailbox)==0) return true;
+
+ iil_C_LoadCache($conn, $mailbox);
+
+ if (fputs($fp, "sel1 SELECT \"$mailbox\"\r\n")){
+ do{
+ $line=chop(iil_ReadLine($fp, 300));
+ $a=explode(" ", $line);
+ if (count($a) == 3){
+ if (strcasecmp($a[2], "EXISTS")==0) $conn->exists=(int)$a[1];
+ if (strcasecmp($a[2], "RECENT")==0) $conn->recent=(int)$a[1];
+ }
+ }while (!iil_StartsWith($line, "sel1"));
+
+ $a=explode(" ", $line);
+
+ if (strcasecmp($a[1],"OK")==0){
+ $conn->selected = $mailbox;
+ return true;
+ }else return false;
+ }else{
+ return false;
+ }
+}
+
+function iil_C_CheckForRecent(&$conn, $mailbox){
+ if (empty($mailbox)) $mailbox="INBOX";
+
+ iil_C_Select($conn, $mailbox);
+ if ($conn->selected==$mailbox) return $conn->recent;
+ else return false;
+}
+
+function iil_C_CountMessages(&$conn, $mailbox, $refresh=false){
+ if ($refresh) $conn->selected="";
+ iil_C_Select($conn, $mailbox);
+ if ($conn->selected==$mailbox) return $conn->exists;
+ else return false;
+}
+
+function iil_SplitHeaderLine($string){
+ $pos=strpos($string, ":");
+ if ($pos>0){
+ $res[0]=substr($string, 0, $pos);
+ $res[1]=trim(substr($string, $pos+1));
+ return $res;
+ }else{
+ return $string;
+ }
+}
+
+function iil_StrToTime($str){
+ global $IMAP_MONTHS,$IMAP_SERVER_TZ;
+
+ if ($str) $time1 = strtotime($str);
+ if ($time1 && $time1!=-1) return $time1-$IMAP_SERVER_TZ;
+
+ //echo '<!--'.$str.'//-->';
+
+ //replace double spaces with single space
+ $str = trim($str);
+ $str = str_replace(" ", " ", $str);
+
+ //strip off day of week
+ $pos=strpos($str, " ");
+ if (!is_numeric(substr($str, 0, $pos))) $str = substr($str, $pos+1);
+
+ //explode, take good parts
+ $a=explode(" ",$str);
+ //$month_a=array("Jan"=>1,"Feb"=>2,"Mar"=>3,"Apr"=>4,"May"=>5,"Jun"=>6,"Jul"=>7,"Aug"=>8,"Sep"=>9,"Oct"=>10,"Nov"=>11,"Dec"=>12);
+ $month_str=$a[1];
+ $month=$IMAP_MONTHS[$month_str];
+ $day=$a[0];
+ $year=$a[2];
+ $time=$a[3];
+ $tz_str = $a[4];
+ $tz = substr($tz_str, 0, 3);
+ $ta=explode(":",$time);
+ $hour=(int)$ta[0]-(int)$tz;
+ $minute=$ta[1];
+ $second=$ta[2];
+
+ //make UNIX timestamp
+ $time2 = mktime($hour, $minute, $second, $month, $day, $year);
+ //echo '<!--'.$time1.' '.$time2.' //-->'."\n";
+ return $time2;
+}
+
+function iil_C_Sort(&$conn, $mailbox, $field){
+ /* Do "SELECT" command */
+ if (!iil_C_Select($conn, $mailbox)) return false;
+
+ $field = strtoupper($field);
+ if ($field=='INTERNALDATE') $field='ARRIVAL';
+ $fields = array('ARRIVAL'=>1,'CC'=>1,'DATE'=>1,'FROM'=>1,'SIZE'=>1,'SUBJECT'=>1,'TO'=>1);
+
+ if (!$fields[$field]) return false;
+
+ $fp = $conn->fp;
+ $command = 's SORT ('.$field.') US-ASCII ALL'."\r\n";
+ $line = $data = '';
+
+ if (!fputs($fp, $command)) return false;
+ do{
+ $line = chop(iil_ReadLine($fp, 1024));
+ if (iil_StartsWith($line, '* SORT')) $data.=($data?' ':'').substr($line,7);
+ }while($line[0]!='s');
+
+ if (empty($data)){
+ $conn->error = $line;
+ return false;
+ }
+
+ $out = explode(' ',$data);
+ return $out;
+}
+
+function iil_C_FetchHeaderIndex(&$conn, $mailbox, $message_set, $index_field,$normalize=true){
+ global $IMAP_USE_INTERNAL_DATE;
+
+ $c=0;
+ $result=array();
+ $fp = $conn->fp;
+
+ if (empty($index_field)) $index_field="DATE";
+ $index_field = strtoupper($index_field);
+
+ if (empty($message_set)) return array();
+
+ //$fields_a["DATE"] = ($IMAP_USE_INTERNAL_DATE?6:1);
+ $fields_a['DATE'] = 1;
+ $fields_a['INTERNALDATE'] = 6;
+ $fields_a['FROM'] = 1;
+ $fields_a['REPLY-TO'] = 1;
+ $fields_a['SENDER'] = 1;
+ $fields_a['TO'] = 1;
+ $fields_a['SUBJECT'] = 1;
+ $fields_a['UID'] = 2;
+ $fields_a['SIZE'] = 2;
+ $fields_a['SEEN'] = 3;
+ $fields_a['RECENT'] = 4;
+ $fields_a['DELETED'] = 5;
+
+ $mode=$fields_a[$index_field];
+ if (!($mode > 0)) return false;
+
+ /* Do "SELECT" command */
+ if (!iil_C_Select($conn, $mailbox)) return false;
+
+ /* FETCH date,from,subject headers */
+ if ($mode==1){
+ $key="fhi".($c++);
+ $request=$key." FETCH $message_set (BODY.PEEK[HEADER.FIELDS ($index_field)])\r\n";
+ if (!fputs($fp, $request)) return false;
+ do{
+
+ $line=chop(iil_ReadLine($fp, 200));
+ $a=explode(" ", $line);
+ if (($line[0]=="*") && ($a[2]=="FETCH") && ($line[strlen($line)-1]!=")")){
+ $id=$a[1];
+
+ $str=$line=chop(iil_ReadLine($fp, 300));
+
+ while($line[0]!=")"){ //caution, this line works only in this particular case
+ $line=chop(iil_ReadLine($fp, 300));
+ if ($line[0]!=")"){
+ if (ord($line[0]) <= 32){ //continuation from previous header line
+ $str.=" ".trim($line);
+ }
+ if ((ord($line[0]) > 32) || (strlen($line[0]) == 0)){
+ list($field, $string) = iil_SplitHeaderLine($str);
+ if (strcasecmp($field, "date")==0){
+ $result[$id]=iil_StrToTime($string);
+ }else{
+ $result[$id] = str_replace("\"", "", $string);
+ if ($normalize) $result[$id]=strtoupper($result[$id]);
+ }
+ $str=$line;
+ }
+ }
+ }
+ }
+ /*
+ $end_pos = strlen($line)-1;
+ if (($line[0]=="*") && ($a[2]=="FETCH") && ($line[$end_pos]=="}")){
+ $id = $a[1];
+ $pos = strrpos($line, "{")+1;
+ $bytes = (int)substr($line, $pos, $end_pos-$pos);
+ $received = 0;
+ do{
+ $line = iil_ReadLine($fp, 0);
+ $received+=strlen($line);
+ $line = chop($line);
+
+ if ($received>$bytes) break;
+ else if (!$line) continue;
+
+ list($field,$string)=explode(": ", $line);
+
+ if (strcasecmp($field, "date")==0)
+ $result[$id] = iil_StrToTime($string);
+ else if ($index_field!="DATE")
+ $result[$id]=strtoupper(str_replace("\"", "", $string));
+ }while($line[0]!=")");
+ }else{
+ //one line response, not expected so ignore
+ }
+ */
+ }while(!iil_StartsWith($line, $key));
+ }else if ($mode==6){
+ $key="fhi".($c++);
+ $request = $key." FETCH $message_set (INTERNALDATE)\r\n";
+ if (!fputs($fp, $request)) return false;
+ do{
+ $line=chop(iil_ReadLine($fp, 200));
+ if ($line[0]=="*"){
+ //original: "* 10 FETCH (INTERNALDATE "31-Jul-2002 09:18:02 -0500")"
+ $paren_pos = strpos($line, "(");
+ $foo = substr($line, 0, $paren_pos);
+ $a = explode(" ", $foo);
+ $id = $a[1];
+
+ $open_pos = strpos($line, "\"") + 1;
+ $close_pos = strrpos($line, "\"");
+ if ($open_pos && $close_pos){
+ $len = $close_pos - $open_pos;
+ $time_str = substr($line, $open_pos, $len);
+ $result[$id] = strtotime($time_str);
+ }
+ }else{
+ $a = explode(" ", $line);
+ }
+ }while(!iil_StartsWith($a[0], $key));
+ }else{
+ if ($mode >= 3) $field_name="FLAGS";
+ else if ($index_field=="SIZE") $field_name="RFC822.SIZE";
+ else $field_name=$index_field;
+
+ /* FETCH uid, size, flags */
+ $key="fhi".($c++);
+ $request=$key." FETCH $message_set ($field_name)\r\n";
+
+ if (!fputs($fp, $request)) return false;
+ do{
+ $line=chop(iil_ReadLine($fp, 200));
+ $a = explode(" ", $line);
+ if (($line[0]=="*") && ($a[2]=="FETCH")){
+ $line=str_replace("(", "", $line);
+ $line=str_replace(")", "", $line);
+ $a=explode(" ", $line);
+
+ $id=$a[1];
+
+ if (isset($result[$id])) continue; //if we already got the data, skip forward
+ if ($a[3]!=$field_name) continue; //make sure it's returning what we requested
+
+ /* Caution, bad assumptions, next several lines */
+ if ($mode==2) $result[$id]=$a[4];
+ else{
+ $haystack=strtoupper($line);
+ $result[$id]=(strpos($haystack, $index_field) > 0 ? "F" : "N");
+ }
+ }
+ }while(!iil_StartsWith($line, $key));
+ }
+
+ //check number of elements...
+ list($start_mid,$end_mid)=explode(':',$message_set);
+ if (is_numeric($start_mid) && is_numeric($end_mid)){
+ //count how many we should have
+ $should_have = $end_mid - $start_mid +1;
+
+ //if we have less, try and fill in the "gaps"
+ if (count($result)<$should_have){
+ for($i=$start_mid;$i<=$end_mid;$i++) if (!isset($result[$i])) $result[$i] = '';
+ }
+ }
+
+ return $result;
+
+}
+
+function iil_CompressMessageSet($message_set){
+ //given a comma delimited list of independent mid's,
+ //compresses by grouping sequences together
+
+ //if less than 255 bytes long, let's not bother
+ if (strlen($message_set)<255) return $message_set;
+
+ //see if it's already been compress
+ if (strpos($message_set,':')!==false) return $message_set;
+
+ //separate, then sort
+ $ids = explode(',',$message_set);
+ sort($ids);
+
+ $result = array();
+ $start = $prev = $ids[0];
+ foreach($ids as $id){
+ $incr = $id - $prev;
+ if ($incr>1){ //found a gap
+ if ($start==$prev) $result[] = $prev; //push single id
+ else $result[] = $start.':'.$prev; //push sequence as start_id:end_id
+ $start = $id; //start of new sequence
+ }
+ $prev = $id;
+ }
+ //handle the last sequence/id
+ if ($start==$prev) $result[] = $prev;
+ else $result[] = $start.':'.$prev;
+
+ //return as comma separated string
+ return implode(',',$result);
+}
+
+function iil_C_UIDsToMIDs(&$conn, $mailbox, $uids){
+ if (!is_array($uids) || count($uids)==0) return array();
+ return iil_C_Search($conn, $mailbox, "UID ".implode(",", $uids));
+}
+
+function iil_C_UIDToMID(&$conn, $mailbox, $uid){
+ $result = iil_C_UIDsToMIDs($conn, $mailbox, array($uid));
+ if (count($result)==1) return $result[0];
+ else return false;
+}
+
+function iil_C_FetchUIDs(&$conn,$mailbox){
+ global $clock;
+
+ $num = iil_C_CountMessages(&$conn, $mailbox);
+ if ($num==0) return array();
+ $message_set = '1'.($num>1?':'.$num:'');
+
+ //if cache not enabled, just call iil_C_FetchHeaderIndex on 'UID' field
+ if (!$conn->do_cache)
+ return iil_C_FetchHeaderIndex($conn, $mailbox, $message_set, 'UID');
+
+ //otherwise, let's check cache first
+ $key = $mailbox.'.uids';
+ $cache_good = true;
+ if ($conn->uid_cache) $data = $conn->uid_cache;
+ else $data = cache_read($conn->user, $conn->host, $key);
+
+ //was anything cached at all?
+ if ($data===false) $cache_good = -1;
+
+ //make sure number of messages were the same
+ if ($cache_good>0 && $data['n']!=$num) $cache_good = -2;
+
+ //if everything's okay so far...
+ if ($cache_good>0){
+ //check UIDs of highest mid with current and cached
+ $temp = iil_C_Search($conn, $mailbox, 'UID '.$data['d'][$num]);
+ if (!$temp || !is_array($temp) || $temp[0]!=$num) $cache_good=-3;
+ }
+
+ //if cached data's good, return it
+ if ($cache_good>0){
+ return $data['d'];
+ }
+
+ //otherwise, we need to fetch it
+ $data = array('n'=>$num,'d'=>array());
+ $data['d'] = iil_C_FetchHeaderIndex($conn, $mailbox, $message_set, 'UID');
+ cache_write($conn->user, $conn->host, $key, $data);
+ $conn->uid_cache = $data;
+ return $data['d'];
+}
+
+function iil_SortThreadHeaders($headers, $index_a, $uids){
+ asort($index_a);
+ $result = array();
+ foreach($index_a as $mid=>$foobar){
+ $uid = $uids[$mid];
+ $result[$uid] = $headers[$uid];
+ }
+ return $result;
+}
+
+function iil_C_FetchThreadHeaders(&$conn, $mailbox, $message_set){
+ global $clock;
+ global $index_a;
+
+ if (empty($message_set)) return false;
+
+ $result = array();
+ $uids = iil_C_FetchUIDs($conn, $mailbox);
+ $debug = false;
+
+ /* Get cached records where possible */
+ if ($conn->do_cache){
+ $cached = cache_read($conn->user, $conn->host, $mailbox.'.thhd');
+ if ($cached && is_array($uids) && count($uids)>0){
+ $needed_set = "";
+ foreach($uids as $id=>$uid){
+ if ($cached[$uid]){
+ $result[$uid] = $cached[$uid];
+ $result[$uid]->id = $id;
+ }else $needed_set.=($needed_set?",":"").$id;
+ }
+ if ($needed_set) $message_set = $needed_set;
+ else $message_set = '';
+ }
+ }
+ $message_set = iil_CompressMessageSet($message_set);
+ if ($debug) echo "Still need: ".$message_set;
+
+ /* if we're missing any, get them */
+ if ($message_set){
+ /* FETCH date,from,subject headers */
+ $key="fh";
+ $fp = $conn->fp;
+ $request=$key." FETCH $message_set (BODY.PEEK[HEADER.FIELDS (SUBJECT MESSAGE-ID IN-REPLY-TO)])\r\n";
+ $mid_to_id = array();
+ if (!fputs($fp, $request)) return false;
+ do{
+ $line = chop(iil_ReadLine($fp, 1024));
+ if ($debug) echo $line."\n";
+ if (ereg('\{[0-9]+\}$', $line)){
+ $a = explode(" ", $line);
+ $new = array();
+
+ $new_thhd = new iilThreadHeader;
+ $new_thhd->id = $a[1];
+ do{
+ $line=chop(iil_ReadLine($fp, 1024),"\r\n");
+ if (iil_StartsWithI($line,'Message-ID:') || (iil_StartsWithI($line,'In-Reply-To:')) || (iil_StartsWithI($line,'SUBJECT:'))){
+ $pos = strpos($line, ":");
+ $field_name = substr($line, 0, $pos);
+ $field_val = substr($line, $pos+1);
+ $new[strtoupper($field_name)] = trim($field_val);
+ }else if (ereg('^[[:space:]]', $line)){
+ $new[strtoupper($field_name)].= trim($line);
+ }
+ }while($line[0]!=')');
+ $new_thhd->sbj = $new['SUBJECT'];
+ $new_thhd->mid = substr($new['MESSAGE-ID'], 1, -1);
+ $new_thhd->irt = substr($new['IN-REPLY-TO'], 1, -1);
+
+ $result[$uids[$new_thhd->id]] = $new_thhd;
+ }
+ }while(!iil_StartsWith($line, "fh"));
+ }
+
+ /* sort headers */
+ if (is_array($index_a)){
+ $result = iil_SortThreadHeaders($result, $index_a, $uids);
+ }
+
+ /* write new set to cache */
+ if ($conn->do_cache){
+ if (count($result)!=count($cached))
+ cache_write($conn->user, $conn->host, $mailbox.'.thhd', $result);
+ }
+
+ //echo 'iil_FetchThreadHeaders:'."\n";
+ //print_r($result);
+
+ return $result;
+}
+
+function iil_C_BuildThreads2(&$conn, $mailbox, $message_set, &$clock){
+ global $index_a;
+
+ if (empty($message_set)) return false;
+
+ $result=array();
+ $roots=array();
+ $root_mids = array();
+ $sub_mids = array();
+ $strays = array();
+ $messages = array();
+ $fp = $conn->fp;
+ $debug = false;
+
+ $sbj_filter_pat = '[a-zA-Z]{2,3}(\[[0-9]*\])?:([[:space:]]*)';
+
+ /* Do "SELECT" command */
+ if (!iil_C_Select($conn, $mailbox)) return false;
+
+ /* FETCH date,from,subject headers */
+ $mid_to_id = array();
+ $messages = array();
+ $headers = iil_C_FetchThreadHeaders($conn, $mailbox, $message_set);
+ if ($clock) $clock->register('fetched headers');
+
+ if ($debug) print_r($headers);
+
+ /* go through header records */
+ foreach($headers as $header){
+ //$id = $header['i'];
+ //$new = array('id'=>$id, 'MESSAGE-ID'=>$header['m'],
+ // 'IN-REPLY-TO'=>$header['r'], 'SUBJECT'=>$header['s']);
+ $id = $header->id;
+ $new = array('id'=>$id, 'MESSAGE-ID'=>$header->mid,
+ 'IN-REPLY-TO'=>$header->irt, 'SUBJECT'=>$header->sbj);
+
+ /* add to message-id -> mid lookup table */
+ $mid_to_id[$new['MESSAGE-ID']] = $id;
+
+ /* if no subject, use message-id */
+ if (empty($new['SUBJECT'])) $new['SUBJECT'] = $new['MESSAGE-ID'];
+
+ /* if subject contains 'RE:' or has in-reply-to header, it's a reply */
+ $sbj_pre ='';
+ $has_re = false;
+ if (eregi($sbj_filter_pat, $new['SUBJECT'])) $has_re = true;
+ if ($has_re||$new['IN-REPLY-TO']) $sbj_pre = 'RE:';
+
+ /* strip out 're:', 'fw:' etc */
+ if ($has_re) $sbj = ereg_replace($sbj_filter_pat,'', $new['SUBJECT']);
+ else $sbj = $new['SUBJECT'];
+ $new['SUBJECT'] = $sbj_pre.$sbj;
+
+
+ /* if subject not a known thread-root, add to list */
+ if ($debug) echo $id.' '.$new['SUBJECT']."\t".$new['MESSAGE-ID']."\n";
+ $root_id = $roots[$sbj];
+
+ if ($root_id && ($has_re || !$root_in_root[$root_id])){
+ if ($debug) echo "\tfound root: $root_id\n";
+ $sub_mids[$new['MESSAGE-ID']] = $root_id;
+ $result[$root_id][] = $id;
+ }else if (!isset($roots[$sbj])||(!$has_re&&$root_in_root[$root_id])){
+ /* try to use In-Reply-To header to find root
+ unless subject contains 'Re:' */
+ if ($has_re&&$new['IN-REPLY-TO']){
+ if ($debug) echo "\tlooking: ".$new['IN-REPLY-TO']."\n";
+
+ //reply to known message?
+ $temp = $sub_mids[$new['IN-REPLY-TO']];
+
+ if ($temp){
+ //found it, root:=parent's root
+ if ($debug) echo "\tfound parent: ".$new['SUBJECT']."\n";
+ $result[$temp][] = $id;
+ $sub_mids[$new['MESSAGE-ID']] = $temp;
+ $sbj = '';
+ }else{
+ //if we can't find referenced parent, it's a "stray"
+ $strays[$id] = $new['IN-REPLY-TO'];
+ }
+ }
+
+ //add subject as root
+ if ($sbj){
+ if ($debug) echo "\t added to root\n";
+ $roots[$sbj] = $id;
+ $root_in_root[$id] = !$has_re;
+ $sub_mids[$new['MESSAGE-ID']] = $id;
+ $result[$id] = array($id);
+ }
+ if ($debug) echo $new['MESSAGE-ID']."\t".$sbj."\n";
+ }
+
+ }
+
+ //now that we've gone through all the messages,
+ //go back and try and link up the stray threads
+ if (count($strays)>0){
+ foreach($strays as $id=>$irt){
+ $root_id = $sub_mids[$irt];
+ if (!$root_id || $root_id==$id) continue;
+ $result[$root_id] = array_merge($result[$root_id],$result[$id]);
+ unset($result[$id]);
+ }
+ }
+
+ if ($clock) $clock->register('data prepped');
+
+ if ($debug) print_r($roots);
+ //print_r($result);
+ return $result;
+}
+
+
+function iil_SortThreads(&$tree, $index, $sort_order='ASC'){
+ if (!is_array($tree) || !is_array($index)) return false;
+
+ //create an id to position lookup table
+ $i = 0;
+ foreach($index as $id=>$val){
+ $i++;
+ $index[$id] = $i;
+ }
+ $max = $i+1;
+
+ //for each tree, set array key to position
+ $itree = array();
+ foreach($tree as $id=>$node){
+ if (count($tree[$id])<=1){
+ //for "threads" with only one message, key is position of that message
+ $n = $index[$id];
+ $itree[$n] = array($n=>$id);
+ }else{
+ //for "threads" with multiple messages,
+ $min = $max;
+ $new_a = array();
+ foreach($tree[$id] as $mid){
+ $new_a[$index[$mid]] = $mid; //create new sub-array mapping position to id
+ $pos = $index[$mid];
+ if ($pos&&$pos<$min) $min = $index[$mid]; //find smallest position
+ }
+ $n = $min; //smallest position of child is thread position
+
+ //assign smallest position to root level key
+ //set children array to one created above
+ ksort($new_a);
+ $itree[$n] = $new_a;
+ }
+ }
+
+
+ //sort by key, this basically sorts all threads
+ ksort($itree);
+ $i=0;
+ $out=array();
+ foreach($itree as $k=>$node){
+ $out[$i] = $itree[$k];
+ $i++;
+ }
+
+ //return
+ return $out;
+}
+
+function iil_IndexThreads(&$tree){
+ /* creates array mapping mid to thread id */
+
+ if (!is_array($tree)) return false;
+
+ $t_index = array();
+ foreach($tree as $pos=>$kids){
+ foreach($kids as $kid) $t_index[$kid] = $pos;
+ }
+
+ return $t_index;
+}
+
+function iil_C_FetchHeaders(&$conn, $mailbox, $message_set){
+ global $IMAP_USE_INTERNAL_DATE;
+
+ $c=0;
+ $result=array();
+ $fp = $conn->fp;
+
+ if (empty($message_set)) return array();
+
+ /* Do "SELECT" command */
+ if (!iil_C_Select($conn, $mailbox)){
+ $conn->error = "Couldn't select $mailbox";
+ return false;
+ }
+
+ /* Get cached records where possible */
+ if ($conn->do_cache){
+ $uids = iil_C_FetchHeaderIndex($conn, $mailbox, $message_set, "UID");
+ if (is_array($uids) && count($conn->cache[$mailbox]>0)){
+ $needed_set = "";
+ while(list($id,$uid)=each($uids)){
+ if ($conn->cache[$mailbox][$uid]){
+ $result[$id] = $conn->cache[$mailbox][$uid];
+ $result[$id]->id = $id;
+ }else $needed_set.=($needed_set?",":"").$id;
+ }
+ //echo "<!-- iil_C_FetchHeader\nMessage Set: $message_set\nNeeded Set:$needed_set\n//-->\n";
+ if ($needed_set) $message_set = iil_CompressMessageSet($needed_set);
+ else return $result;
+ }
+ }
+
+ /* FETCH date,from,subject headers */
+ $key="fh".($c++);
+ $request=$key." FETCH $message_set (BODY.PEEK[HEADER.FIELDS (DATE FROM TO SUBJECT REPLY-TO IN-REPLY-TO CC CONTENT-TRANSFER-ENCODING CONTENT-TYPE MESSAGE-ID)])\r\n";
+
+ // echo "// $request\n\n";
+
+ if (!fputs($fp, $request)) return false;
+ do{
+ $line=chop(iil_ReadLine($fp, 200));
+ $a=explode(" ", $line);
+ if (($line[0]=="*") && ($a[2]=="FETCH")){
+ $id=$a[1];
+ $result[$id]=new iilBasicHeader;
+ $result[$id]->id = $id;
+ $result[$id]->subject = "";
+ /*
+ Start parsing headers. The problem is, some header "lines" take up multiple lines.
+ So, we'll read ahead, and if the one we're reading now is a valid header, we'll
+ process the previous line. Otherwise, we'll keep adding the strings until we come
+ to the next valid header line.
+ */
+ $i = 0;
+ $lines = array();
+ do{
+ $line = chop(iil_ReadLine($fp, 300),"\r\n");
+ if (ord($line[0])<=32) $lines[$i].=(empty($lines[$i])?"":"\n").trim(chop($line));
+ else{
+ $i++;
+ $lines[$i] = trim(chop($line));
+ }
+ }while($line[0]!=")");
+
+ //process header, fill iilBasicHeader obj.
+ // initialize
+ if (is_array($headers)){
+ reset($headers);
+ while ( list($k, $bar) = each($headers) ) $headers[$k] = "";
+ }
+
+ // create array with header field:data
+ $headers = array();
+ while ( list($lines_key, $str) = each($lines) ){
+ list($field, $string) = iil_SplitHeaderLine($str);
+ $field = strtolower($field);
+ $headers[$field] = $string;
+ }
+ $result[$id]->date = $headers["date"];
+ $result[$id]->timestamp = iil_StrToTime($headers["date"]);
+ $result[$id]->from = $headers["from"];
+ $result[$id]->to = str_replace("\n", " ", $headers["to"]);
+ $result[$id]->subject = str_replace("\n", "", $headers["subject"]);
+ $result[$id]->replyto = str_replace("\n", " ", $headers["reply-to"]);
+ $result[$id]->cc = str_replace("\n", " ", $headers["cc"]);
+ $result[$id]->encoding = str_replace("\n", " ", $headers["content-transfer-encoding"]);
+ $result[$id]->ctype = str_replace("\n", " ", $headers["content-type"]);
+ //$result[$id]->in_reply_to = ereg_replace("[\n<>]",'', $headers['in-reply-to']);
+ list($result[$id]->ctype,$foo) = explode(";", $headers["content-type"]);
+ $messageID = $headers["message-id"];
+ if ($messageID) $messageID = substr(substr($messageID, 1), 0, strlen($messageID)-2);
+ else $messageID = "mid:".$id;
+ $result[$id]->messageID = $messageID;
+
+ }
+ }while(strcmp($a[0], $key)!=0);
+
+ /*
+ FETCH uid, size, flags
+ Sample reply line: "* 3 FETCH (UID 2417 RFC822.SIZE 2730 FLAGS (\Seen \Deleted))"
+ */
+ $command_key="fh".($c++);
+ $request= $command_key." FETCH $message_set (UID RFC822.SIZE FLAGS INTERNALDATE)\r\n";
+ if (!fputs($fp, $request)) return false;
+ do{
+ $line=chop(iil_ReadLine($fp, 200));
+ //$a = explode(" ", $line);
+ //if (($line[0]=="*") && ($a[2]=="FETCH")){
+ if ($line[0]=="*"){
+ //echo "<!-- $line //-->\n";
+ //get outter most parens
+ $open_pos = strpos($line, "(") + 1;
+ $close_pos = strrpos($line, ")");
+ if ($open_pos && $close_pos){
+ //extract ID from pre-paren
+ $pre_str = substr($line, 0, $open_pos);
+ $pre_a = explode(" ", $line);
+ $id = $pre_a[1];
+
+ //get data
+ $len = $close_pos - $open_pos;
+ $str = substr($line, $open_pos, $len);
+
+ //swap parents with quotes, then explode
+ $str = eregi_replace("[()]", "\"", $str);
+ $a = iil_ExplodeQuotedString(" ", $str);
+
+ //did we get the right number of replies?
+ $parts_count = count($a);
+ if ($parts_count>=8){
+ for ($i=0;$i<$parts_count;$i=$i+2){
+ if (strcasecmp($a[$i],"UID")==0) $result[$id]->uid=$a[$i+1];
+ else if (strcasecmp($a[$i],"RFC822.SIZE")==0) $result[$id]->size=$a[$i+1];
+ else if (strcasecmp($a[$i],"INTERNALDATE")==0) $time_str = $a[$i+1];
+ else if (strcasecmp($a[$i],"FLAGS")==0) $flags_str = $a[$i+1];
+ }
+
+ // process flags
+ $flags_str = eregi_replace('[\\\"]', "", $flags_str);
+ $flags_a = explode(" ", $flags_str);
+ //echo "<!-- ID: $id FLAGS: ".implode(",", $flags_a)." //-->\n";
+
+ $result[$id]->seen = false;
+ $result[$id]->recent = false;
+ $result[$id]->deleted = false;
+ $result[$id]->answered = false;
+ if (is_array($flags_a)){
+ reset($flags_a);
+ while (list($key,$val)=each($flags_a)){
+ if (strcasecmp($val,"Seen")==0) $result[$id]->seen = true;
+ else if (strcasecmp($val, "Deleted")==0) $result[$id]->deleted=true;
+ else if (strcasecmp($val, "Recent")==0) $result[$id]->recent = true;
+ else if (strcasecmp($val, "Answered")==0) $result[$id]->answered = true;
+ }
+ $result[$id]->flags=$flags_str;
+ }
+
+ // if time is gmt...
+ $time_str = str_replace('GMT','+0000',$time_str);
+
+ //get timezone
+ $time_str = substr($time_str, 0, -1);
+ $time_zone_str = substr($time_str, -5); //extract timezone
+ $time_str = substr($time_str, 1, -6); //remove quotes
+ $time_zone = (float)substr($time_zone_str, 1, 2); //get first two digits
+ if ($time_zone_str[3]!='0') $time_zone += 0.5; //handle half hour offset
+ if ($time_zone_str[0]=="-") $time_zone = $time_zone * -1.0; //minus?
+ $result[$id]->internaldate = $time_str;
+
+ if ($IMAP_USE_INTERNAL_DATE){
+ //calculate timestamp
+ $timestamp = strtotime($time_str); //return's server's time
+ $na_timestamp = $timestamp;
+ $timestamp -= $time_zone * 3600; //compensate for tz, get GMT
+ $result[$id]->timestamp = $timestamp;
+ }
+
+ if ($conn->do_cache){
+ $uid = $result[$id]->uid;
+ $conn->cache[$mailbox][$uid] = $result[$id];
+ $conn->cache_dirty[$mailbox] = true;
+ }
+ //echo "<!-- ID: $id : $time_str -- local: $na_timestamp (".date("F j, Y, g:i a", $na_timestamp).") tz: $time_zone -- GMT: ".$timestamp." (".date("F j, Y, g:i a", $timestamp).") //-->\n";
+ }else{
+ //echo "<!-- ERROR: $id : $str //-->\n";
+ }
+ }
+ }
+ }while(strpos($line, $command_key)===false);
+
+ return $result;
+}
+
+
+function iil_C_FetchHeader(&$conn, $mailbox, $id){
+ $fp = $conn->fp;
+ $a=iil_C_FetchHeaders($conn, $mailbox, $id);
+ if (is_array($a)) return $a[$id];
+ else return false;
+}
+
+
+function iil_SortHeaders($a, $field, $flag){
+ if (empty($field)) $field="uid";
+ $field=strtolower($field);
+ if ($field=="date"||$field=='internaldate') $field="timestamp";
+ if (empty($flag)) $flag="ASC";
+ $flag=strtoupper($flag);
+
+ $c=count($a);
+ if ($c>0){
+ /*
+ Strategy:
+ First, we'll create an "index" array.
+ Then, we'll use sort() on that array,
+ and use that to sort the main array.
+ */
+
+ // create "index" array
+ $index=array();
+ reset($a);
+ while (list($key, $val)=each($a)){
+ $data=$a[$key]->$field;
+ if (is_string($data)) $data=strtoupper(str_replace("\"", "", $data));
+ $index[$key]=$data;
+ }
+
+ // sort index
+ $i=0;
+ if ($flag=="ASC") asort($index);
+ else arsort($index);
+
+ // form new array based on index
+ $result=array();
+ reset($index);
+ while (list($key, $val)=each($index)){
+ $result[$i]=$a[$key];
+ $i++;
+ }
+ }
+
+ return $result;
+}
+
+function iil_C_Expunge(&$conn, $mailbox){
+ $fp = $conn->fp;
+ if (iil_C_Select($conn, $mailbox)){
+ $c=0;
+ fputs($fp, "exp1 EXPUNGE\r\n");
+ do{
+ $line=chop(iil_ReadLine($fp, 100));
+ if ($line[0]=="*") $c++;
+ }while (!iil_StartsWith($line, "exp1"));
+
+ if (iil_ParseResult($line) == 0){
+ $conn->selected = ""; //state has changed, need to reselect
+ //$conn->exists-=$c;
+ return $c;
+ }else{
+ $conn->error = $line;
+ return -1;
+ }
+ }
+
+ return -1;
+}
+
+function iil_C_ModFlag(&$conn, $mailbox, $messages, $flag, $mod){
+ if ($mod!="+" && $mod!="-") return -1;
+
+ $fp = $conn->fp;
+ $flags=array(
+ "SEEN"=>"\\Seen",
+ "DELETED"=>"\\Deleted",
+ "RECENT"=>"\\Recent",
+ "ANSWERED"=>"\\Answered",
+ "DRAFT"=>"\\Draft",
+ "FLAGGED"=>"\\Flagged"
+ );
+ $flag=strtoupper($flag);
+ $flag=$flags[$flag];
+ if (iil_C_Select($conn, $mailbox)){
+ $c=0;
+ fputs($fp, "flg STORE $messages ".$mod."FLAGS (".$flag.")\r\n");
+ do{
+ $line=chop(iil_ReadLine($fp, 100));
+ if ($line[0]=="*") $c++;
+ }while (!iil_StartsWith($line, "flg"));
+
+ if (iil_ParseResult($line) == 0){
+ iil_C_ExpireCachedItems($conn, $mailbox, $messages);
+ return $c;
+ }else{
+ $conn->error = $line;
+ return -1;
+ }
+ }else{
+ $conn->error = "Select failed";
+ return -1;
+ }
+}
+
+function iil_C_Flag(&$conn, $mailbox, $messages, $flag){
+ return iil_C_ModFlag($conn, $mailbox, $messages, $flag, "+");
+}
+
+function iil_C_Unflag(&$conn, $mailbox, $messages, $flag){
+ return iil_C_ModFlag($conn, $mailbox, $messages, $flag, "-");
+}
+
+function iil_C_Delete(&$conn, $mailbox, $messages){
+ return iil_C_ModFlag($conn, $mailbox, $messages, "DELETED", "+");
+}
+
+function iil_C_Undelete(&$conn, $mailbox, $messages){
+ return iil_C_ModFlag($conn, $mailbox, $messages, "DELETED", "-");
+}
+
+
+function iil_C_Unseen(&$conn, $mailbox, $messages){
+ return iil_C_ModFlag($conn, $mailbox, $messages, "SEEN", "-");
+}
+
+
+function iil_C_Copy(&$conn, $messages, $from, $to){
+ $fp = $conn->fp;
+
+ if (empty($from) || empty($to)) return -1;
+
+ if (iil_C_Select($conn, $from)){
+ $c=0;
+
+ fputs($fp, "cpy1 COPY $messages \"$to\"\r\n");
+ $line=iil_ReadReply($fp);
+ return iil_ParseResult($line);
+ }else{
+ return -1;
+ }
+}
+
+function iil_FormatSearchDate($month, $day, $year){
+ $month = (int)$month;
+ $months=array(
+ 1=>"Jan", 2=>"Feb", 3=>"Mar", 4=>"Apr",
+ 5=>"May", 6=>"Jun", 7=>"Jul", 8=>"Aug",
+ 9=>"Sep", 10=>"Oct", 11=>"Nov", 12=>"Dec"
+ );
+ return $day."-".$months[$month]."-".$year;
+}
+
+function iil_C_CountUnseen(&$conn, $folder){
+ $index = iil_C_Search($conn, $folder, "ALL UNSEEN");
+ if (is_array($index)){
+ $str = implode(",", $index);
+ if (empty($str)) return false;
+ else return count($index);
+ }else return false;
+}
+
+function iil_C_UID2ID(&$conn, $folder, $uid){
+ if ($uid > 0){
+ $id_a = iil_C_Search($conn, $folder, "UID $uid");
+ if (is_array($id_a)){
+ $count = count($id_a);
+ if ($count > 1) return false;
+ else return $id_a[0];
+ }
+ }
+ return false;
+}
+
+function iil_C_Search(&$conn, $folder, $criteria){
+ $fp = $conn->fp;
+ if (iil_C_Select($conn, $folder)){
+ $c=0;
+
+ $query = "srch1 SEARCH ".chop($criteria)."\r\n";
+ fputs($fp, $query);
+ do{
+ $line=trim(chop(iil_ReadLine($fp, 10000)));
+ if (eregi("^\* SEARCH", $line)){
+ $str = trim(substr($line, 8));
+ $messages = explode(" ", $str);
+ }
+ }while(!iil_StartsWith($line, "srch1"));
+
+ $result_code=iil_ParseResult($line);
+ if ($result_code==0) return $messages;
+ else{
+ $conn->error = "iil_C_Search: ".$line."<br>\n";
+ return false;
+ }
+
+ }else{
+ $conn->error = "iil_C_Search: Couldn't select \"$folder\" <br>\n";
+ return false;
+ }
+}
+
+function iil_C_Move(&$conn, $messages, $from, $to){
+ $fp = $conn->fp;
+
+ if (!$from || !$to) return -1;
+
+ $r=iil_C_Copy($conn, $messages, $from,$to);
+ if ($r==0){
+ return iil_C_Delete($conn, $from, $messages);
+ }else{
+ return $r;
+ }
+}
+
+function iil_C_GetHierarchyDelimiter(&$conn){
+ if ($conn->delimiter) return $conn->delimiter;
+
+ $fp = $conn->fp;
+ $delimiter = false;
+
+ //try (LIST "" ""), should return delimiter (RFC2060 Sec 6.3.8)
+ if (!fputs($fp, "ghd LIST \"\" \"\"\r\n")) return false;
+ do{
+ $line=iil_ReadLine($fp, 500);
+ if ($line[0]=="*"){
+ $line = rtrim($line);
+ $a=iil_ExplodeQuotedString(" ", $line);
+ if ($a[0]=="*") $delimiter = str_replace("\"", "", $a[count($a)-2]);
+ }
+ }while (!iil_StartsWith($line, "ghd"));
+
+ if (strlen($delimiter)>0) return $delimiter;
+
+ //if that fails, try namespace extension
+ //try to fetch namespace data
+ fputs($conn->fp, "ns1 NAMESPACE\r\n");
+ do{
+ $line = iil_ReadLine($conn->fp, 1024);
+ if (iil_StartsWith($line, "* NAMESPACE")){
+ $i = 0;
+ $data = iil_ParseNamespace2(substr($line,11), $i, 0, 0);
+ }
+ }while(!iil_StartsWith($line, "ns1"));
+
+ if (!is_array($data)) return false;
+
+ //extract user space data (opposed to global/shared space)
+ $user_space_data = $data[0];
+ if (!is_array($user_space_data)) return false;
+
+ //get first element
+ $first_userspace = $user_space_data[0];
+ if (!is_array($first_userspace)) return false;
+
+ //extract delimiter
+ $delimiter = $first_userspace[1];
+
+ return $delimiter;
+}
+
+function iil_C_ListMailboxes(&$conn, $ref, $mailbox){
+ global $IGNORE_FOLDERS;
+
+ $ignore = $IGNORE_FOLDERS[strtolower($conn->host)];
+
+ $fp = $conn->fp;
+ if (empty($mailbox)) $mailbox="*";
+ if (empty($ref) && $conn->rootdir) $ref = $conn->rootdir;
+
+ // send command
+ if (!fputs($fp, "lmb LIST \"".$ref."\" \"$mailbox\"\r\n")) return false;
+ $i=0;
+ // get folder list
+ do{
+ $line=iil_ReadLine($fp, 500);
+ $line=iil_MultLine($fp, $line);
+
+ $a = explode(" ", $line);
+ if (($line[0]=="*") && ($a[1]=="LIST")){
+ $line = rtrim($line);
+ // split one line
+ $a=iil_ExplodeQuotedString(" ", $line);
+ // last string is folder name
+ $folder = str_replace("\"", "", $a[count($a)-1]);
+ if (empty($ignore) || (!empty($ignore) && !eregi($ignore, $folder))) $folders[$i] = $folder;
+ // second from last is delimiter
+ $delim = str_replace("\"", "", $a[count($a)-2]);
+ // is it a container?
+ $i++;
+ }
+ }while (!iil_StartsWith($line, "lmb"));
+
+ if (is_array($folders)){
+ if (!empty($ref)){
+ // if rootdir was specified, make sure it's the first element
+ // some IMAP servers (i.e. Courier) won't return it
+ if ($ref[strlen($ref)-1]==$delim) $ref = substr($ref, 0, strlen($ref)-1);
+ if ($folders[0]!=$ref) array_unshift($folders, $ref);
+ }
+ return $folders;
+ }else if (iil_ParseResult($line)==0){
+ return array('INBOX');
+ }else{
+ $conn->error = $line;
+ return false;
+ }
+}
+
+
+function iil_C_ListSubscribed(&$conn, $ref, $mailbox){
+ global $IGNORE_FOLDERS;
+
+ $ignore = $IGNORE_FOLDERS[strtolower($conn->host)];
+
+ $fp = $conn->fp;
+ if (empty($mailbox)) $mailbox = "*";
+ if (empty($ref) && $conn->rootdir) $ref = $conn->rootdir;
+ $folders = array();
+
+ // send command
+ if (!fputs($fp, "lsb LSUB \"".$ref."\" \"".$mailbox."\"\r\n")){
+ $conn->error = "Couldn't send LSUB command\n";
+ return false;
+ }
+ $i=0;
+ // get folder list
+ do{
+ $line=iil_ReadLine($fp, 500);
+ $line=iil_MultLine($fp, $line);
+ $a = explode(" ", $line);
+ if (($line[0]=="*") && ($a[1]=="LSUB")){
+ $line = rtrim($line);
+ // split one line
+ $a=iil_ExplodeQuotedString(" ", $line);
+ // last string is folder name
+ //$folder = UTF7DecodeString(str_replace("\"", "", $a[count($a)-1]));
+ $folder = str_replace("\"", "", $a[count($a)-1]);
+ if ((!in_array($folder, $folders)) && (empty($ignore) || (!empty($ignore) && !eregi($ignore, $folder)))) $folders[$i] = $folder;
+ // second from last is delimiter
+ $delim = str_replace("\"", "", $a[count($a)-2]);
+ // is it a container?
+ $i++;
+ }
+ }while (!iil_StartsWith($line, "lsb"));
+
+ if (is_array($folders)){
+ if (!empty($ref)){
+ // if rootdir was specified, make sure it's the first element
+ // some IMAP servers (i.e. Courier) won't return it
+ if ($ref[strlen($ref)-1]==$delim) $ref = substr($ref, 0, strlen($ref)-1);
+ if ($folders[0]!=$ref) array_unshift($folders, $ref);
+ }
+ return $folders;
+ }else{
+ $conn->error = $line;
+ return false;
+ }
+}
+
+
+function iil_C_Subscribe(&$conn, $folder){
+ $fp = $conn->fp;
+
+ $query = "sub1 SUBSCRIBE \"".$folder."\"\r\n";
+ fputs($fp, $query);
+ $line=trim(chop(iil_ReadLine($fp, 10000)));
+ return iil_ParseResult($line);
+}
+
+
+function iil_C_UnSubscribe(&$conn, $folder){
+ $fp = $conn->fp;
+
+ $query = "usub1 UNSUBSCRIBE \"".$folder."\"\r\n";
+ fputs($fp, $query);
+ $line=trim(chop(iil_ReadLine($fp, 10000)));
+ return iil_ParseResult($line);
+}
+
+
+function iil_C_FetchPartHeader(&$conn, $mailbox, $id, $part){
+ $fp = $conn->fp;
+ $result=false;
+ if (($part==0)||(empty($part))) $part="HEADER";
+ else $part.=".MIME";
+
+ if (iil_C_Select($conn, $mailbox)){
+ $key="fh".($c++);
+ $request=$key." FETCH $id (BODY.PEEK[$part])\r\n";
+ if (!fputs($fp, $request)) return false;
+ do{
+ $line=chop(iil_ReadLine($fp, 200));
+ $a=explode(" ", $line);
+ if (($line[0]=="*") && ($a[2]=="FETCH") && ($line[strlen($line)-1]!=")")){
+ $line=iil_ReadLine($fp, 300);
+ while(chop($line)!=")"){
+ $result.=$line;
+ $line=iil_ReadLine($fp, 300);
+ }
+ }
+ }while(strcmp($a[0], $key)!=0);
+ }
+
+ return $result;
+}
+
+
+function iil_C_HandlePartBody(&$conn, $mailbox, $id, $part, $mode){
+ /* modes:
+ 1: return string
+ 2: print
+ 3: base64 and print
+ */
+ $fp = $conn->fp;
+ $result=false;
+ if (($part==0)||(empty($part))) $part="TEXT";
+
+ if (iil_C_Select($conn, $mailbox)){
+ $reply_key="* ".$id;
+ // format request
+ $key="ftch".($c++)." ";
+ $request=$key."FETCH $id (BODY.PEEK[$part])\r\n";
+ // send request
+ if (!fputs($fp, $request)) return false;
+ // receive reply line
+ do{
+ $line = chop(iil_ReadLine($fp, 1000));
+ $a = explode(" ", $line);
+ }while ($a[2]!="FETCH");
+ $len = strlen($line);
+ if ($line[$len-1] == ")"){
+ //one line response, get everything between first and last quotes
+ $from = strpos($line, "\"") + 1;
+ $to = strrpos($line, "\"");
+ $len = $to - $from;
+ if ($mode==1) $result = substr($line, $from, $len);
+ else if ($mode==2) echo substr($line, $from, $len);
+ else if ($mode==3) echo base64_decode(substr($line, $from, $len));
+ }else if ($line[$len-1] == "}"){
+ //multi-line request, find sizes of content and receive that many bytes
+ $from = strpos($line, "{") + 1;
+ $to = strrpos($line, "}");
+ $len = $to - $from;
+ $sizeStr = substr($line, $from, $len);
+ $bytes = (int)$sizeStr;
+ $received = 0;
+ while ($received < $bytes){
+ $remaining = $bytes - $received;
+ $line = iil_ReadLine($fp, 1024);
+ $len = strlen($line);
+ if ($len > $remaining) substr($line, 0, $remaining);
+ $received += strlen($line);
+ if ($mode==1) $result .= chop($line)."\n";
+ else if ($mode==2){ echo chop($line)."\n"; flush(); }
+ else if ($mode==3){ echo base64_decode($line); flush(); }
+ }
+ }
+ // read in anything up until 'til last line
+ do{
+ $line = iil_ReadLine($fp, 1024);
+ }while(!iil_StartsWith($line, $key));
+
+ if ($result){
+ $result = chop($result);
+ return substr($result, 0, strlen($result)-1);
+ }else return false;
+ }else{
+ echo "Select failed.";
+ }
+
+ if ($mode==1) return $result;
+ else return $received;
+}
+
+function iil_C_FetchPartBody(&$conn, $mailbox, $id, $part){
+ return iil_C_HandlePartBody($conn, $mailbox, $id, $part, 1);
+}
+
+function iil_C_PrintPartBody(&$conn, $mailbox, $id, $part){
+ iil_C_HandlePartBody($conn, $mailbox, $id, $part, 2);
+}
+
+function iil_C_PrintBase64Body(&$conn, $mailbox, $id, $part){
+ iil_C_HandlePartBody($conn, $mailbox, $id, $part, 3);
+}
+
+function iil_C_CreateFolder(&$conn, $folder){
+ $fp = $conn->fp;
+ if (fputs($fp, "c CREATE \"".$folder."\"\r\n")){
+ do{
+ $line=iil_ReadLine($fp, 300);
+ }while($line[0]!="c");
+ $conn->error = $line;
+ return (iil_ParseResult($line)==0);
+ }else{
+ return false;
+ }
+}
+
+function iil_C_RenameFolder(&$conn, $from, $to){
+ $fp = $conn->fp;
+ if (fputs($fp, "r RENAME \"".$from."\" \"".$to."\"\r\n")){
+ do{
+ $line=iil_ReadLine($fp, 300);
+ }while($line[0]!="r");
+ return (iil_ParseResult($line)==0);
+ }else{
+ return false;
+ }
+}
+
+function iil_C_DeleteFolder(&$conn, $folder){
+ $fp = $conn->fp;
+ if (fputs($fp, "d DELETE \"".$folder."\"\r\n")){
+ do{
+ $line=iil_ReadLine($fp, 300);
+ }while($line[0]!="d");
+ return (iil_ParseResult($line)==0);
+ }else{
+ $conn->error = "Couldn't send command\n";
+ return false;
+ }
+}
+
+function iil_C_Append(&$conn, $folder, $message){
+ if (!$folder) return false;
+ $fp = $conn->fp;
+
+ $message = str_replace("\r", "", $message);
+ $message = str_replace("\n", "\r\n", $message);
+
+ $len = strlen($message);
+ if (!$len) return false;
+
+ $request="A APPEND \"".$folder."\" (\\Seen) {".$len."}\r\n";
+ // echo $request.'<br>';
+ if (fputs($fp, $request)){
+ $line=iil_ReadLine($fp, 100);
+ // echo $line.'<br>';
+
+ $sent = fwrite($fp, $message."\r\n");
+ flush();
+ do{
+ $line=iil_ReadLine($fp, 1000);
+ //echo $line.'<br>';
+ }while($line[0]!="A");
+
+ $result = (iil_ParseResult($line)==0);
+ if (!$result) $conn->error .= $line."<br>\n";
+ return $result;
+
+ }else{
+ $conn->error .= "Couldn't send command \"$request\"<br>\n";
+ return false;
+ }
+}
+
+
+function iil_C_AppendFromFile(&$conn, $folder, $path){
+ if (!$folder) return false;
+
+ //open message file
+ $in_fp = false;
+ if (file_exists(realpath($path))) $in_fp = fopen($path, "r");
+ if (!$in_fp){
+ $conn->error .= "Couldn't open $path for reading<br>\n";
+ return false;
+ }
+
+ $fp = $conn->fp;
+ $len = filesize($path);
+ if (!$len) return false;
+
+ //send APPEND command
+ $request="A APPEND \"".$folder."\" (\\Seen) {".$len."}\r\n";
+ $bytes_sent = 0;
+ if (fputs($fp, $request)){
+ $line=iil_ReadLine($fp, 100);
+
+ //send file
+ while(!feof($in_fp)){
+ $buffer = fgets($in_fp, 4096);
+ $bytes_sent += strlen($buffer);
+ fputs($fp, $buffer);
+ }
+ fclose($in_fp);
+
+ fputs($fp, "\r\n");
+
+ //read response
+ do{
+ $line=iil_ReadLine($fp, 1000);
+ //echo $line.'<br>';
+ }while($line[0]!="A");
+
+ $result = (iil_ParseResult($line)==0);
+ if (!$result) $conn->error .= $line."<br>\n";
+ return $result;
+
+ }else{
+ $conn->error .= "Couldn't send command \"$request\"<br>\n";
+ return false;
+ }
+}
+
+
+function iil_C_FetchStructureString(&$conn, $folder, $id){
+ $fp = $conn->fp;
+ $result=false;
+ if (iil_C_Select($conn, $folder)){
+ $key = "F1247";
+ if (fputs($fp, "$key FETCH $id (BODYSTRUCTURE)\r\n")){
+ do{
+ $line=chop(iil_ReadLine($fp, 5000));
+ if ($line[0]=="*"){
+ if (ereg("\}$", $line)){
+ preg_match('/(.+)\{([0-9]+)\}/', $line, $match);
+ $result = $match[1];
+ do{
+ $line = chop(iil_ReadLine($fp, 100));
+ if (!preg_match("/^$key/", $line)) $result .= $line;
+ else $done = true;
+ }while(!$done);
+ }else{
+ $result = $line;
+ }
+ list($pre, $post) = explode("BODYSTRUCTURE ", $result);
+ $result = substr($post, 0, strlen($post)-1); //truncate last ')' and return
+ }
+ }while (!preg_match("/^$key/",$line));
+ }
+ }
+ return $result;
+}
+
+function iil_C_PrintSource(&$conn, $folder, $id, $part){
+ $header = iil_C_FetchPartHeader($conn, $folder, $id, $part);
+ //echo str_replace("\r", "", $header);
+ echo $header;
+ echo iil_C_PrintPartBody($conn, $folder, $id, $part);
+}
+
+function iil_C_GetQuota(&$conn){
+/*
+b GETQUOTAROOT "INBOX"
+* QUOTAROOT INBOX user/rchijiiwa1
+* QUOTA user/rchijiiwa1 (STORAGE 654 9765)
+b OK Completed
+*/
+ $fp = $conn->fp;
+ $result=false;
+ $quota_line = "";
+
+ //get line containing quota info
+ if (fputs($fp, "QUOT1 GETQUOTAROOT \"INBOX\"\r\n")){
+ do{
+ $line=chop(iil_ReadLine($fp, 5000));
+ if (iil_StartsWith($line, "* QUOTA ")) $quota_line = $line;
+ }while(!iil_StartsWith($line, "QUOT1"));
+ }
+
+ //return false if not found, parse if found
+ if (!empty($quota_line)){
+ $quota_line = eregi_replace("[()]", "", $quota_line);
+ $parts = explode(" ", $quota_line);
+ $storage_part = array_search("STORAGE", $parts);
+ if ($storage_part>0){
+ $result = array();
+ $used = $parts[$storage_part+1];
+ $total = $parts[$storage_part+2];
+ $result["used"] = $used;
+ $result["total"] = (empty($total)?"??":$total);
+ $result["percent"] = (empty($total)?"??":round(($used/$total)*100));
+ $result["free"] = 100 - $result["percent"];
+ }
+ }
+
+ return $result;
+}
+
+
+function iil_C_ClearFolder(&$conn, $folder){
+ $num_in_trash = iil_C_CountMessages($conn, $folder);
+ if ($num_in_trash > 0) iil_C_Delete($conn, $folder, "1:".$num_in_trash);
+ return (iil_C_Expunge($conn, $folder) >= 0);
+}
+
+?> \ No newline at end of file
diff --git a/program/lib/mime.inc b/program/lib/mime.inc
new file mode 100644
index 000000000..ad6561ed7
--- /dev/null
+++ b/program/lib/mime.inc
@@ -0,0 +1,322 @@
+<?php
+/////////////////////////////////////////////////////////
+//
+// Iloha MIME Library (IML)
+//
+// (C)Copyright 2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
+//
+// This file is part of IlohaMail. IlohaMail is free software released
+// under the GPL license. See enclosed file COPYING for details, or
+// see http://www.fsf.org/copyleft/gpl.html
+//
+/////////////////////////////////////////////////////////
+
+/********************************************************
+
+ FILE: include/mime.inc
+ PURPOSE:
+ Provide functions for handling mime messages.
+ USAGE:
+ Use iil_C_FetchStructureString to get IMAP structure stirng, then pass that through
+ iml_GetRawStructureArray() to get root node to a nested data structure.
+ Pass root node to the iml_GetPart*() functions to retreive individual bits of info.
+
+********************************************************/
+$MIME_INVALID = -1;
+$MIME_TEXT = 0;
+$MIME_MULTIPART = 1;
+$MIME_MESSAGE = 2;
+$MIME_APPLICATION = 3;
+$MIME_AUDIO = 4;
+$MIME_IMAGE = 5;
+$MIME_VIDEO = 6;
+$MIME_OTHER = 7;
+
+function iml_ClosingParenPos($str, $start){
+ $level=0;
+ $len = strlen($str);
+ $in_quote = 0;
+ for ($i=$start;$i<$len;$i++){
+ if ($str[$i]=="\"") $in_quote = ($in_quote + 1) % 2;
+ if (!$in_quote){
+ if ($str[$i]=="(") $level++;
+ else if (($level > 0) && ($str[$i]==")")) $level--;
+ else if (($level == 0) && ($str[$i]==")")) return $i;
+ }
+ }
+}
+
+function iml_ParseBSString($str){
+
+ $id = 0;
+ $a = array();
+ $len = strlen($str);
+
+ $in_quote = 0;
+ for ($i=0; $i<$len; $i++){
+ if ($str[$i] == "\"") $in_quote = ($in_quote + 1) % 2;
+ else if (!$in_quote){
+ if ($str[$i] == " ") $id++; //space means new element
+ else if ($str[$i]=="("){ //new part
+ $i++;
+ $endPos = iml_ClosingParenPos($str, $i);
+ $partLen = $endPos - $i;
+ $part = substr($str, $i, $partLen);
+ $a[$id] = iml_ParseBSString($part); //send part string
+ if ($verbose){
+ echo "{>".$endPos."}";
+ flush();
+ }
+ $i = $endPos;
+ }else $a[$id].=$str[$i]; //add to current element in array
+ }else if ($in_quote){
+ if ($str[$i]=="\\") $i++; //escape backslashes
+ else $a[$id].=$str[$i]; //add to current element in array
+ }
+ }
+
+ reset($a);
+ return $a;
+}
+
+function iml_GetRawStructureArray($str){
+ $line=substr($str, 1, strlen($str) - 2);
+ $line = str_replace(")(", ") (", $line);
+
+ $struct = iml_ParseBSString($line);
+ if ((strcasecmp($struct[0], "message")==0) && (strcasecmp($struct[1], "rfc822")==0)){
+ $struct = array($struct);
+ }
+ return $struct;
+}
+
+function iml_GetPartArray($a, $part){
+ if (!is_array($a)) return false;
+ if (strpos($part, ".") > 0){
+ $original_part = $part;
+ $pos = strpos($part, ".");
+ $rest = substr($original_part, $pos+1);
+ $part = substr($original_part, 0, $pos);
+ if ((strcasecmp($a[0], "message")==0) && (strcasecmp($a[1], "rfc822")==0)){
+ $a = $a[8];
+ }
+ //echo "m - part: $original_part current: $part rest: $rest array: ".implode(" ", $a)."<br>\n";
+ return iml_GetPartArray($a[$part-1], $rest);
+ }else if ($part>0){
+ if ((strcasecmp($a[0], "message")==0) && (strcasecmp($a[1], "rfc822")==0)){
+ $a = $a[8];
+ }
+ //echo "s - part: $part rest: $rest array: ".implode(" ", $a)."<br>\n";
+ if (is_array($a[$part-1])) return $a[$part-1];
+ else return false;
+ }else if (($part==0) || (empty($part))){
+ return $a;
+ }
+}
+
+function iml_GetNumParts($a, $part){
+ if (is_array($a)){
+ $parent=iml_GetPartArray($a, $part);
+
+ if ((strcasecmp($parent[0], "message")==0) && (strcasecmp($parent[1], "rfc822")==0)){
+ $parent = $parent[8];
+ }
+
+ $is_array=true;
+ $c=0;
+ while (( list ($key, $val) = each ($parent) )&&($is_array)){
+ $is_array=is_array($parent[$key]);
+ if ($is_array) $c++;
+ }
+ return $c;
+ }
+
+ return false;
+}
+
+function iml_GetPartTypeString($a, $part){
+ $part_a=iml_GetPartArray($a, $part);
+ if ($part_a){
+ if (is_array($part_a[0])){
+ $type_str = "MULTIPART/";
+ reset($part_a);
+ while(list($n,$element)=each($part_a)){
+ if (!is_array($part_a[$n])){
+ $type_str.=$part_a[$n];
+ break;
+ }
+ }
+ return $type_str;
+ }else return $part_a[0]."/".$part_a[1];
+ }else return false;
+}
+
+function iml_GetFirstTextPart($structure,$part){
+ if ($part==0) $part="";
+ $typeCode = -1;
+ while ($typeCode!=0){
+ $typeCode = iml_GetPartTypeCode($structure, $part);
+ if ($typeCode == 1){
+ $part .= (empty($part)?"":".")."1";
+ }else if ($typeCode > 0){
+ $parts_a = explode(".", $part);
+ $lastPart = count($parts_a) - 1;
+ $parts_a[$lastPart] = (int)$parts_a[$lastPart] + 1;
+ $part = implode(".", $parts_a);
+ }else if ($typeCode == -1){
+ return "";
+ }
+ }
+
+ return $part;
+}
+
+function iml_GetPartTypeCode($a, $part){
+ $types=array(0=>"text",1=>"multipart",2=>"message",3=>"application",4=>"audio",5=>"image",6=>"video",7=>"other");
+
+ $part_a=iml_GetPartArray($a, $part);
+ if ($part_a){
+ if (is_array($part_a[0])) $str="multipart";
+ else $str=$part_a[0];
+
+ $code=7;
+ while ( list($key, $val) = each($types)) if (strcasecmp($val, $str)==0) $code=$key;
+ return $code;
+ }else return -1;
+}
+
+function iml_GetPartEncodingCode($a, $part){
+ $encodings=array("7BIT", "8BIT", "BINARY", "BASE64", "QUOTED-PRINTABLE", "OTHER");
+
+ $part_a=iml_GetPartArray($a, $part);
+ if ($part_a){
+ if (is_array($part_a[0])) return -1;
+ else $str=$part_a[5];
+
+ $code=5;
+ while ( list($key, $val) = each($encodings)) if (strcasecmp($val, $str)==0) $code=$key;
+
+ return $code;
+
+ }else return -1;
+}
+
+function iml_GetPartEncodingString($a, $part){
+ $part_a=iml_GetPartArray($a, $part);
+ if ($part_a){
+ if (is_array($part_a[0])) return -1;
+ else return $part_a[5];
+ }else return -1;
+}
+
+function iml_GetPartSize($a, $part){
+ $part_a=iml_GetPartArray($a, $part);
+ if ($part_a){
+ if (is_array($part_a[0])) return -1;
+ else return $part_a[6];
+ }else return -1;
+}
+
+function iml_GetPartID($a, $part){
+ $part_a=iml_GetPartArray($a, $part);
+ if ($part_a){
+ if (is_array($part_a[0])) return -1;
+ else return $part_a[3];
+ }else return -1;
+}
+
+function iml_GetPartDisposition($a, $part){
+ $part_a=iml_GetPartArray($a, $part);
+ if ($part_a){
+ if (is_array($part_a[0])) return -1;
+ else{
+ $id = count($part_a) - 2;
+ if (is_array($part_a[$id])) return $part_a[$id][0];
+ else return "";
+ }
+ }else return "";
+}
+
+function iml_GetPartName($a, $part){
+ $part_a=iml_GetPartArray($a, $part);
+ if ($part_a){
+ if (is_array($part_a[0])) return -1;
+ else{
+ $name = "";
+ if (is_array($part_a[2])){
+ //first look in content type
+ $name="";
+ while ( list($key, $val) = each ($part_a[2])){
+ if ((strcasecmp($val, "NAME")==0)||(strcasecmp($val, "FILENAME")==0))
+ $name=$part_a[2][$key+1];
+ }
+ }
+ if (empty($name)){
+ //check in content disposition
+ $id = count($part_a) - 2;
+ if ((is_array($part_a[$id])) && (is_array($part_a[$id][1]))){
+ $array = $part_a[$id][1];
+ while ( list($key, $val) = each($array)){
+ if ((strcasecmp($val, "NAME")==0)||(strcasecmp($val, "FILENAME")==0))
+ $name=$array[$key+1];
+ }
+ }
+ }
+ return $name;
+ }
+ }else return "";
+}
+
+
+function iml_GetPartCharset($a, $part){
+ $part_a=iml_GetPartArray($a, $part);
+ if ($part_a){
+ if (is_array($part_a[0])) return -1;
+ else{
+ if (is_array($part_a[2])){
+ $name="";
+ while ( list($key, $val) = each ($part_a[2])) if (strcasecmp($val, "charset")==0) $name=$part_a[2][$key+1];
+ return $name;
+ }
+ else return "";
+ }
+ }else return "";
+}
+
+function iml_GetPartList($a, $part){
+ //echo "MOO?"; flush();
+ $data = array();
+ $num_parts = iml_GetNumParts($a, $part);
+ //echo "($num_parts)"; flush();
+ if ($num_parts !== false){
+ //echo "<!-- ($num_parts parts)//-->\n";
+ for ($i = 0; $i<$num_parts; $i++){
+ $part_code = $part.(empty($part)?"":".").($i+1);
+ $part_type = iml_GetPartTypeCode($a, $part_code);
+ $part_disposition = iml_GetPartDisposition($a, $part_code);
+ //echo "<!-- part: $part_code type: $part_type //-->\n";
+ if (strcasecmp($part_disposition, "attachment")!=0 &&
+ (($part_type == 1) || ($part_type==2))){
+ $data = array_merge($data, iml_GetPartList($a, $part_code));
+ }else{
+ $data[$part_code]["typestring"] = iml_GetPartTypeString($a, $part_code);
+ $data[$part_code]["disposition"] = $part_disposition;
+ $data[$part_code]["size"] = iml_GetPartSize($a, $part_code);
+ $data[$part_code]["name"] = iml_GetPartName($a, $part_code);
+ $data[$part_code]["id"] = iml_GetPartID($a, $part_code);
+ }
+ }
+ }
+ return $data;
+}
+
+function iml_GetNextPart($part){
+ if (strpos($part, ".")===false) return $part++;
+ else{
+ $parts_a = explode(".", $part);
+ $num_levels = count($parts_a);
+ $parts_a[$num_levels-1]++;
+ return implode(".", $parts_a);
+ }
+}
+?> \ No newline at end of file
diff --git a/program/lib/smtp.inc b/program/lib/smtp.inc
new file mode 100644
index 000000000..ebff9d263
--- /dev/null
+++ b/program/lib/smtp.inc
@@ -0,0 +1,351 @@
+<?php
+/////////////////////////////////////////////////////////
+//
+// include/smtp.inc
+//
+// (C)Copyright 2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
+//
+// This file is part of IlohaMail.
+// IlohaMail is free software released under the GPL
+// license. See enclosed file COPYING for details,
+// or see http://www.fsf.org/copyleft/gpl.html
+//
+/////////////////////////////////////////////////////////
+
+/********************************************************
+
+ AUTHOR: Ryo Chijiiwa <ryo@ilohamail.org>
+ FILE: include/smtp.php
+ PURPOSE:
+ Provide SMTP functionality using pure PHP.
+ PRE-CONDITIONS:
+ The functions here require a SMTP server configured to allow relaying.
+ POST-CONDITIONS:
+ The following global variables are returned:
+ $smtp_errornum - error number, 0 if successful
+ $smtp_error - error message(s)
+ $SMTP_TYPE - Optional
+ COMMENTS:
+ The optional $smtp_message_file can be used for sending extremely large
+ messages. Storing large messages in a variable will consume whatever
+ amount of memory required, which may be more than is available if dealing
+ with messages with large attachments. By storing them in a file, it becomes
+ possible to read/send bits at a time, drastically reducing memory useage.
+
+ This library only provides bare-bones SMTP functionality. It is up to the
+ parent code to form valid RFC822 (MIME) messages.
+
+********************************************************/
+
+ //set some global constants
+ if (strcasecmp($SMTP_TYPE, "courier")==0){
+ $SMTP_REPLY_DELIM = "-";
+ $SMTP_DATA_REPLY = 250;
+ }else{
+ $SMTP_REPLY_DELIM = " ";
+ $SMTP_DATA_REPLY = 354;
+ }
+
+ /* fgets replacement that's multi-line aware */
+ function smtp_get_response($fp, $len){
+ $end = false;
+ do{
+ $line = chop(fgets($fp, 5120));
+ // echo "SMTP:".$line."<br>\n"; flush();
+ if ((strlen($line)==3) || ($line[3]==' ')) $end = true;
+ }while(!$end);
+
+ return $line;
+ }
+
+
+ function smtp_check_reply($reply){
+ global $smtp_error;
+ global $SMTP_REPLY_DELIM;
+
+ $a = explode($SMTP_REPLY_DELIM, chop($reply));
+
+ if (count($a) >= 1){
+
+ if ($a[0]==250||$a[0]==354) return true;
+ else{
+ $smtp_error .= $reply."\n";
+ }
+ }else{
+ $smtp_error .= "Invalid SMTP response line: $reply\n";
+ }
+
+ return false;
+ }
+
+
+ function smtp_split($str){
+ $result = array();
+ $pos = strpos($str, " ");
+ if ($pos===false){
+ $result[0] = $str;
+ }else{
+ $result[0] = substr($str, 0, $pos);
+ $result[1] = substr($str, $pos+1);
+ }
+
+ return $result;
+ }
+
+
+ function smtp_ehlo(&$conn, $host){
+ $result = "";
+ fputs($conn, "EHLO $host\r\n");
+ //echo "Sent: EHLO $host\n"; flush();
+ do{
+ $line = fgets($conn, 2048);
+ //echo "Got: $line"; flush();
+ $a = explode(" ", $line);
+ if ($a[0]=="250-AUTH") $result .= substr($line, 9);
+ }while(!is_numeric($a[0]));
+
+ if ($a[0]==250) return $result;
+ else return $a[0];
+
+ }
+
+
+ function smtp_auth_login(&$conn, $user, $pass){
+ $auth["username"] = base64_encode($user);
+ $auth["password"] = base64_encode($pass);
+
+ fputs($conn, "AUTH LOGIN\r\n");
+
+ //echo "Sent: AUTH LOGIN\n"; flush();
+
+ //get first line
+ $line = smtp_get_response($conn, 1024);
+ //echo "AUTH_LOGIN << $line"; flush();
+ $parts = smtp_split($line);
+ //valid reply?
+ if (($parts[0]!=334) || (empty($parts[1]))) return false;
+ //send data
+ $prompt = eregi_replace("[^a-z]", "", strtolower(base64_decode($parts[1])));
+ fputs($conn, $auth[$prompt]."\r\n");
+ //echo "AUT_LOGIN >> ".$auth[$prompt]."\n"; flush();
+
+ //get second line
+ $line = smtp_get_response($conn, 1024);
+ //echo "AUTH_LOGIN << $line"; flush();
+ $parts = smtp_split($line);
+ //valid reply?
+ if (($parts[0]!=334) || (empty($parts[1]))) return false;
+ $prompt = eregi_replace("[^a-z]", "", strtolower(base64_decode($parts[1])));
+ fputs($conn, $auth[$prompt]."\r\n");
+ //echo "AUT_LOGIN >> ".$auth[$prompt]."\n"; flush();
+
+ $line = smtp_get_response($conn, 1024);
+ //echo "AUTH_LOGIN << $line"; flush();
+ $parts = smtp_split($line);
+ return ($parts[0]==235);
+ }
+
+
+ function smtp_connect($host, $port, $user, $pass){
+ global $smtp_errornum;
+ global $smtp_error;
+
+ //auth user?
+ global $SMTP_USER, $SMTP_PASSWORD;
+ if ((!empty($SMTP_USER)) && (!empty($SMTP_PASSWORD))){
+ $user = $SMTP_USER;
+ $pass = $SMTP_PASSWORD;
+ }
+
+ // echo "User: $user<br>\n";
+
+ //figure out auth mode
+ global $AUTH_MODE;
+ $auth_mode = $AUTH_MODE["smtp"];
+ if (empty($auth_mode)) $auth_mode = "none";
+ if (empty($user) || empty($pass)) $auth_mode = "none";
+
+ // echo "authmode: $auth_mode<br>\n"; flush();
+
+ //initialize defaults
+ if (empty($host)) $host = "localhost";
+ if (empty($port)) $port = 25;
+
+ // echo "Connecting to $host:$port<br>\n"; flush();
+
+ //connect to SMTP server
+ $conn = fsockopen($host, $port);
+
+ if (!$conn){
+ //echo "fsockopen failed\n";
+ $smtp_error = "Couldn't connect to $host:$port<br>\n";
+ return false;
+ }
+
+ //read greeting
+ $greeting = smtp_get_response($conn, 1024);
+
+ // echo "Connected: $greeting<br>\n"; flush();
+
+ if (($auth_mode=="check") || ($auth_mode=="auth")){
+ // echo "Trying EHLO<br>\n"; flush();
+ $auth_modes = smtp_ehlo($conn, $_SERVER["SERVER_NAME"]);
+ // echo "smtp_ehlo returned: $auth_modes<br>\n"; flush();
+ if ($auth_modes===false){
+ $smtp_error = "EHLO failed\n";
+ $conn = false;
+ }else if (stristr($auth_modes, "LOGIN")!==false){
+ echo "trying AUTH LOGIN\n"; flush();
+ if (!smtp_auth_login($conn, $user, $pass)){
+ //echo "CONN: AUTH_LOGIN failed\n"; flush();
+ $conn = false;
+ }
+ //echo "Conn after LOGIN: $conn<br>\n"; flush();
+ }
+ }else{
+ fputs($conn, "HELO ".$_SERVER["SERVER_NAME"]."\r\n");
+ $line = smtp_get_response($conn, 1024);
+ if (!smtp_check_reply($line)){
+ $conn = false;
+ $smtp_error .= $line."\n";
+ }
+ // echo "after HELO: $conn<br>\n"; flush();
+ }
+
+ return $conn;
+ }
+
+
+ function smtp_close($conn){
+ fclose($conn);
+ }
+
+
+ function smtp_mail($conn, $from, $recipients, $message, $is_file){
+ global $smtp_errornum;
+ global $smtp_error;
+ global $SMTP_DATA_REPLY;
+
+ //check recipients and sender addresses
+ if ((count($recipients)==0) || (!is_array($recipients))){
+ $smtp_errornum = -1;
+ $smtp_error .= "Recipients list is empty\n";
+ return false;
+ }
+ if (empty($from)){
+ $smtp_errornum = -2;
+ $smtp_error .= "From address unspecified\n";
+ return false;
+ }
+
+ if (!$conn){
+ $smtp_errornum = -3;
+ $smtp_error .= "Invalid connection\n";
+ }
+
+ if (!ereg("^<", $from)) $from = "<".$from;
+ if (!ereg(">$", $from)) $from = $from.">";
+
+ //send MAIL FROM command
+ $command = "MAIL FROM: $from\r\n";
+ // echo nl2br(htmlspecialchars($command));
+ fputs($conn, $command);
+
+ if (smtp_check_reply(smtp_get_response($conn, 1024))){
+ //send RCPT TO commands, count valid recipients
+ $num_recipients = 0;
+ while ( list($k, $recipient) = each($recipients) ){
+ $command = "RCPT TO: $recipient\r\n";
+ fputs($conn, $command);
+ $reply = smtp_check_reply(smtp_get_response($conn, 1024));
+ if ($reply) $num_recipients++;
+ else $smtp_error .= $reply."\n";
+ }
+
+ //error out if no valid recipiets
+ if ($num_recipients == 0){
+ $smtp_errornum = -1;
+ $smtp_error .= "No valid recipients\n";
+ return false;
+ }
+
+ //send DATA command
+ fputs($conn, "DATA\r\n");
+ $reply = chop(smtp_get_response($conn, 1024));
+ $a = explode(" ", $reply);
+
+ //error out if DATA command ill received
+ if ($a[0]!=$SMTP_DATA_REPLY){
+ $smtp_errornum = -4;
+ $smtp_error .= $reply;
+ return false;
+ }
+ //send data
+ if ($is_file){
+ //if message file, open file
+ $fp = false;
+ if (file_exists(realpath($message))) $fp = fopen($message, "rb");
+ if (!$fp)
+ {
+ $smtp_errornum = -4;
+ $smtp_error .= "Invlid message file\n";
+ return false;
+ }
+
+ //send file
+ while(!feof($fp)){
+ $buffer = chop(fgets($fp, 4096), "\r\n");
+ fputs($conn, $buffer."\r\n");
+ }
+ fclose($fp);
+ fputs($conn, "\r\n.\r\n");
+
+ return smtp_check_reply(smtp_get_response($conn, 1024));
+ }else{
+ //else, send message
+ $message = str_replace("\r\n", "\n", $message);
+ $message = str_replace("\n", "\r\n", $message);
+ $message = str_replace("\r\n.\r\n", "\r\n..\r\n", $message);
+ fputs($conn, $message);
+ fputs($conn, "\r\n.\r\n");
+
+ return smtp_check_reply(smtp_get_response($conn, 1024));
+ }
+ }
+
+ return false;
+ }
+
+
+ function smtp_ExplodeQuotedString($delimiter, $string){
+ $quotes=explode("\"", $string);
+ while ( list($key, $val) = each($quotes))
+ if (($key % 2) == 1)
+ $quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]);
+ $string=implode("\"", $quotes);
+
+ $result=explode($delimiter, $string);
+ while ( list($key, $val) = each($result) )
+ $result[$key] = str_replace("_!@!_", $delimiter, $result[$key]);
+
+ return $result;
+ }
+
+
+ function smtp_expand($str){
+ $addresses = array();
+ $recipients = smtp_ExplodeQuotedString(",", $str);
+ reset($recipients);
+ while ( list($k, $recipient) = each($recipients) ){
+ $a = explode(" ", $recipient);
+ while ( list($k2, $word) = each($a) ){
+ if ((strpos($word, "@") > 0) && (strpos($word, "\"")===false)){
+ if (!ereg("^<", $word)) $word = "<".$word;
+ if (!ereg(">$", $word)) $word = $word.">";
+ if (in_array($word, $addresses)===false) array_push($addresses, $word);
+ }
+ }
+ }
+ return $addresses;
+ }
+?> \ No newline at end of file
diff --git a/program/lib/utf7.inc b/program/lib/utf7.inc
new file mode 100644
index 000000000..a887958cf
--- /dev/null
+++ b/program/lib/utf7.inc
@@ -0,0 +1,384 @@
+<?php
+//
+//
+// utf7.inc - Routines to encode bytes to UTF7 and decode UTF7 strings
+//
+// Copyright (C) 1999, 2002 Ziberex and Torben Rybner
+//
+//
+// Version 1.01 2002-06-08 19:00
+//
+// - Adapted for use in IlohaMail (modified UTF-7 decoding)
+// - Converted from C to PHP4
+//
+//
+// Version 1.00 1999-09-03 19:00
+//
+// - Encodes bytes to UTF7 strings
+// *OutString = '\0';
+// StartBase64Encode();
+// for (CP = InString; *CP; CP++)
+// strcat(OutString, Base64Encode(*CP));
+// strcat(OutString, StopBase64Encode());
+// - Decodes Base64 strings to bytes
+// StartBase64Decode();
+// for (CP1 = InString, CP2 = OutString; *CP1 && (*CP1 != '='); CP1++)
+// CP2 += Base64Decode(*CP1, CP2);
+// StopBase64Decode();
+//
+
+$BASE64LENGTH = 60;
+
+$BASE64DECODE_NO_DATA = -1;
+$BASE64DECODE_EMPTY_DATA = -2;
+$BASE64DECODE_INVALID_DATA = -3;
+
+
+//
+//
+// Used for conversion to UTF7
+//
+$_ToUTF7 = array
+(
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+ 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
+ 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', ','
+);
+
+//
+//
+// Used for conversion from UTF7
+// (0x80 => Illegal, 0x40 => CR/LF)
+//
+$_FromUTF7 = array
+(
+ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 00 - 07 - Ctrl -
+ 0x80, 0x80, 0x40, 0x80, 0x80, 0x40, 0x80, 0x80, // 08 - 0F - Ctrl -
+ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 10 - 17 - Ctrl -
+ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 18 - 1F - Ctrl -
+ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 20 - 27 !"#$%&'
+ 0x80, 0x80, 0x80, 0x3E, 0x3F, 0x80, 0x80, 0x3F, // 28 - 2F ()*+,-./
+ 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, // 30 - 37 01234567
+ 0x3C, 0x3D, 0x80, 0x80, 0x80, 0x00, 0x80, 0x80, // 38 - 3F 89:;<=>?
+ 0x80, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, // 40 - 47 @ABCDEFG
+ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // 48 - 4F HIJKLMNO
+ 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, // 50 - 57 PQRSTUVW
+ 0x17, 0x18, 0x19, 0x80, 0x80, 0x80, 0x80, 0x80, // 58 - 5F XYZ[\]^_
+ 0x80, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, // 60 - 67 `abcdefg
+ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, // 68 - 6F hijklmno
+ 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, // 70 - 77 pqrstuvw
+ 0x31, 0x32, 0x33, 0x80, 0x80, 0x80, 0x80, 0x80, // 78 - 7F xyz{|}~
+);
+
+
+//
+//
+// UTF7EncodeInit:
+//
+// Start the encoding of bytes
+//
+function UTF7EncodeInit(&$Context)
+{
+ $Context[ "Data" ] = "";
+ $Context[ "Count" ] = 0;
+ $Context[ "Pos" ] = 0;
+ $Context[ "State" ] = 0;
+} // UTF7EncodeInit
+
+
+//
+//
+// UTF7EncodeByte:
+//
+// Encodes one byte to UTF7
+//
+function UTF7EncodeByte(&$Context, $Byte)
+{
+ global $_ToUTF7;
+
+ $Byte = ord($Byte);
+ switch ($Context[ "State" ])
+ {
+ case 0:
+ // Convert into a byte
+ $Context[ "Data" ] = $_ToUTF7[ $Byte >> 2 ];
+ $Context[ "Pos" ]++;
+ // Save residue for next converted byte
+ $Context[ "Residue" ] = ($Byte & 0x03) << 4;
+ // This is the first byte in this line
+ $Context[ "Count" ] = 1;
+ // Next state is 1
+ $Context[ "State" ] = 1;
+ break;
+
+ case 1:
+ // Convert into a byte
+ $Context[ "Data" ] .= $_ToUTF7[ $Context[ "Residue" ] | ($Byte >> 4) ];
+ $Context[ "Pos" ]++;
+ // Save residue for next converted byte
+ $Context[ "Residue" ] = ($Byte & 0x0F) << 2;
+ // Bumb byte counter
+ $Context[ "Count" ]++;
+ // Next state is 2
+ $Context[ "State" ] = 2;
+ break;
+
+ case 2:
+ // Convert into a byte
+ $Context[ "Data" ] .= $_ToUTF7[ $Context[ "Residue" ] | ($Byte >> 6) ];
+ $Context[ "Pos" ]++;
+ // Residue fits precisely into the next byte
+ $Context[ "Data" ] .= $_ToUTF7[ $Byte & 0x3F ];
+ $Context[ "Pos" ]++;
+ // Bumb byte counter
+ $Context[ "Count" ]++;
+ // Next state is 3
+ $Context[ "State" ] = 3;
+ break;
+
+ case 3:
+ // Convert into a byte
+ $Context[ "Data" ] .= $_ToUTF7[ $Byte >> 2 ];
+ $Context[ "Pos" ]++;
+ // Save residue for next converted byte
+ $Context[ "Residue" ] = ($Byte & 0x03) << 4;
+ // Bumb byte counter
+ $Context[ "Count" ]++;
+ // Next state is 1
+ $Context[ "State" ] = 1;
+ break;
+
+ default:
+ // printf("Internal error in UTF7Encode: State is %d\n", $Context[ "State" ]);
+ // exit(1);
+ break;
+ }
+} // UTF7EncodeByte
+
+
+//
+//
+// UTF7EncodeFinal:
+//
+// Terminates the encoding of bytes
+//
+function UTF7EncodeFinal(&$Context)
+{
+ if ($Context[ "State" ] == 0)
+ return "";
+ if ($Context[ "State" ] != 3)
+ UTF7EncodeByte($Context, "\0");
+ return $Context[ "Data" ];
+} // UTF7EncodeFinal
+
+
+//
+//
+// UTF7EncodeString
+//
+// Encodes a string to modified UTF-7 format
+//
+function UTF7EncodeString($String)
+{
+ // Not during encoding, yet
+ $Encoding = false;
+ // Go through the string
+ for ($I = 0; $I < strlen($String); $I++)
+ {
+ $Ch = substr($String, $I, 1);
+ if (ord($Ch) > 0x7F)
+ {
+ if (! $Encoding)
+ {
+ $RetVal .= "&";
+ $Encoding = true;
+ // Initialise UTF7 context
+ UTF7EncodeInit($Context);
+ }
+ UTF7EncodeByte($Context, "\0");
+ UTF7EncodeByte($Context, $Ch);
+ }
+ elseif ($Ch == "&")
+ {
+ if (! $Encoding)
+ {
+ $RetVal .= "&";
+ $Encoding = true;
+ // Initialise UTF7 context
+ UTF7EncodeInit($Context);
+ }
+ else
+ {
+ UTF7EncodeByte($Context, "\0");
+ UTF7EncodeByte($Context, $Ch);
+ }
+ }
+ else
+ {
+ if ($Encoding)
+ {
+ $RetVal .= UTF7EncodeFinal($Context) . "-$Ch";
+ $Encoding = false;
+ }
+ else
+ $RetVal .= $Ch;
+ }
+ }
+ if ($Encoding)
+ $RetVal .= UTF7EncodeFinal($Context) . "-";
+ return $RetVal;
+} // UTF7EncodeString
+
+
+//
+//
+// UTF7DecodeInit:
+//
+// Start the decoding of bytes
+//
+function UTF7DecodeInit(&$Context)
+{
+ $Context[ "Data" ] = "";
+ $Context[ "State" ] = 0;
+ $Context[ "Pos" ] = 0;
+} // UTF7DecodeInit
+
+
+//
+//
+// UTF7DecodeByte:
+//
+// Decodes one character from UTF7
+//
+function UTF7DecodeByte(&$Context, $Byte)
+{
+ global $BASE64DECODE_INVALID_DATA;
+ global $_FromUTF7;
+
+ // Restore bits
+ $Byte = $_FromUTF7[ ord($Byte) ];
+ // Ignore carriage returns and linefeeds
+ if ($Byte == 0x40)
+ return "";
+ // Invalid byte - Tell caller!
+ if ($Byte == 0x80)
+ $Context[ "Count" ] = $BASE64DECODE_INVALID_DATA;
+ switch ($Context[ "State" ])
+ {
+ case 0:
+ // Save residue
+ $Context[ "Residue" ] = $Byte;
+ // Initialise count
+ $Context[ "Count" ] = 0;
+ // Next state
+ $Context[ "State" ] = 1;
+ break;
+
+ case 1:
+ // Store byte
+ $Context[ "Data" ] .= chr(($Context[ "Residue" ] << 2) | ($Byte >> 4));
+ $Context[ "Pos" ]++;
+ // Update count
+ $Context[ "Count" ]++;
+ // Save residue
+ $Context[ "Residue" ] = $Byte;
+ // Next state
+ $Context[ "State" ] = 2;
+ break;
+
+ case 2:
+ // Store byte
+ $Context[ "Data" ] .= chr(($Context[ "Residue" ] << 4) | ($Byte >> 2));
+ $Context[ "Pos" ]++;
+ // Update count
+ $Context[ "Count" ]++;
+ // Save residue
+ $Context[ "Residue" ] = $Byte;
+ // Next state
+ $Context[ "State" ] = 3;
+ break;
+
+ case 3:
+ // Store byte
+ $Context[ "Data" ] .= chr(($Context[ "Residue" ] << 6) | $Byte);
+ $Context[ "Pos" ]++;
+ // Update count
+ $Context[ "Count" ]++;
+ // Next state
+ $Context[ "State" ] = 4;
+ break;
+
+ case 4:
+ // Save residue
+ $Context[ "Residue" ] = $Byte;
+ // Next state
+ $Context[ "State" ] = 1;
+ break;
+ }
+} // UTF7DecodeByte
+
+
+//
+//
+// UTF7DecodeFinal:
+//
+// Decodes one character from UTF7
+//
+function UTF7DecodeFinal(&$Context)
+{
+ // Buffer not empty - Return remainder!
+ if ($Context[ "Count" ])
+ {
+ $Context[ "Pos" ] = 0;
+ $Context[ "State" ] = 0;
+ return $Context[ "Data" ];
+ }
+ return "";
+} // UTF7DecodeFinal
+
+
+//
+//
+// UTF7DecodeString
+//
+// Converts a string encoded in modified UTF-7 encoding
+// to ISO 8859-1.
+// OBS: Works only for valid ISO 8859-1 characters in the
+// encoded data
+//
+function UTF7DecodeString($String)
+{
+ $Decoding = false;
+ for ($I = 0; $I < strlen($String); $I++)
+ {
+ $Ch = substr($String, $I, 1);
+ if ($Decoding)
+ {
+ if ($Ch == "-")
+ {
+ $RetVal .= UTF7DecodeFinal($Context);
+ $Decoding = false;
+ }
+ else
+ UTF7DecodeByte($Context, $Ch);
+ }
+ elseif ($Ch == "&")
+ {
+ if (($I < strlen($String) - 1) && (substr($String, $I + 1, 1) == "-"))
+ {
+ $RetVal .= $Ch;
+ $I++;
+ }
+ else
+ {
+ UTF7DecodeInit($Context);
+ $Decoding = true;
+ }
+ }
+ else
+ $RetVal .= $Ch;
+ }
+ return str_replace("\0", "", $RetVal);
+} // UTF7DecodeString
+?>
diff --git a/program/lib/utf8.inc b/program/lib/utf8.inc
new file mode 100644
index 000000000..72a96b4e9
--- /dev/null
+++ b/program/lib/utf8.inc
@@ -0,0 +1,102 @@
+<?php
+/////////////////////////////
+// utf8.inc
+// (C)2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
+//
+// Description:
+// UTF-8 handling functions
+//
+// This file is part of IlohaMail. IlohaMail is free software released
+// under the GPL license. See enclosed file COPYING for details, or
+// see http://www.fsf.org/copyleft/gpl.html
+////////////////////////////
+
+/**
+* takes a string of utf-8 encoded characters and converts it to a string of unicode entities
+* each unicode entitiy has the form &#nnnnn; n={0..9} and can be displayed by utf-8 supporting
+* browsers
+* @param $source string encoded using utf-8 [STRING]
+* @return string of unicode entities [STRING]
+* @access public
+*/
+/**
+* Author: ronen at greyzone dot com
+* Taken from php.net comment:
+* http://www.php.net/manual/en/function.utf8-decode.php
+**/
+function utf8ToUnicodeEntities ($source) {
+ // array used to figure what number to decrement from character order value
+ // according to number of characters used to map unicode to ascii by utf-8
+ $decrement[4] = 240;
+ $decrement[3] = 224;
+ $decrement[2] = 192;
+ $decrement[1] = 0;
+
+ // the number of bits to shift each charNum by
+ $shift[1][0] = 0;
+ $shift[2][0] = 6;
+ $shift[2][1] = 0;
+ $shift[3][0] = 12;
+ $shift[3][1] = 6;
+ $shift[3][2] = 0;
+ $shift[4][0] = 18;
+ $shift[4][1] = 12;
+ $shift[4][2] = 6;
+ $shift[4][3] = 0;
+
+ $pos = 0;
+ $len = strlen ($source);
+ $encodedString = '';
+ while ($pos < $len) {
+ $asciiPos = ord (substr ($source, $pos, 1));
+ if (($asciiPos >= 240) && ($asciiPos <= 255)) {
+ // 4 chars representing one unicode character
+ $thisLetter = substr ($source, $pos, 4);
+ $pos += 4;
+ }
+ else if (($asciiPos >= 224) && ($asciiPos <= 239)) {
+ // 3 chars representing one unicode character
+ $thisLetter = substr ($source, $pos, 3);
+ $pos += 3;
+ }
+ else if (($asciiPos >= 192) && ($asciiPos <= 223)) {
+ // 2 chars representing one unicode character
+ $thisLetter = substr ($source, $pos, 2);
+ $pos += 2;
+ }
+ else {
+ // 1 char (lower ascii)
+ $thisLetter = substr ($source, $pos, 1);
+ $pos += 1;
+ }
+
+ // process the string representing the letter to a unicode entity
+ $thisLen = strlen ($thisLetter);
+ $thisPos = 0;
+ $decimalCode = 0;
+ while ($thisPos < $thisLen) {
+ $thisCharOrd = ord (substr ($thisLetter, $thisPos, 1));
+ if ($thisPos == 0) {
+ $charNum = intval ($thisCharOrd - $decrement[$thisLen]);
+ $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
+ }
+ else {
+ $charNum = intval ($thisCharOrd - 128);
+ $decimalCode += ($charNum << $shift[$thisLen][$thisPos]);
+ }
+
+ $thisPos++;
+ }
+
+ if ($thisLen == 1)
+ $encodedLetter = "&#". str_pad($decimalCode, 3, "0", STR_PAD_LEFT) . ';';
+ else
+ $encodedLetter = "&#". str_pad($decimalCode, 5, "0", STR_PAD_LEFT) . ';';
+
+ $encodedString .= $encodedLetter;
+ }
+
+ return $encodedString;
+}
+
+?> \ No newline at end of file