system: Linux mars.sprixweb.com 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
cmd: 

Direktori : /home/pfhr/public_html/admin/classes/Lib/
Upload File :
Current File : /home/pfhr/public_html/admin/classes/Lib/Email.php

<?php
class Lib_Email
{
	/**
	* list of To addresses
	*
	* @var	array $sendto
	*/
	private $sendto = array();
	/**
	* list of cc addresses
	*
	* @var	array $acc
	*/
	private $acc = array();
	/**
	* list of bcc addresses
	*
	* @var	array $abcc
	*/
	private $abcc = array();
	/**
	* paths of attached files
	*
	* @var array $aattach
	*/
	private $aattach = array();
	/**
	* list of message headers
	*
	* @var array $xheaders
	*/
	private $xheaders = array();
	/**
	* message priorities referential
	*
	* @var array $priorities
	*/
	private $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
	/**
	* character set of message
	*
	* @var string $charset
	*/	
	private $charset = "us-ascii";
	/**
	* set of bit
	*
	* @var string $ctencoding
	*/
	private $ctencoding = "7bit";
	/**
	 * set of receipt
	 *
	 * @var integer $receipt
	 */	
	private $receipt = 0;
	/**
	* Store from email id
	*
	* @var	array $from
	*/
	private $from;
	/**
	* Store to email id
	*
	* @var	array $to
	*/
	private $to;
	/**
	* Store email subject
	*
	* @var	array $subject
	*/
	private $subject;
	/**
	* Store email body
	*
	* @var	array $body
	*/
	private $body;
	/**
	* set the Reply-to header
	*
	* @var	string $address
	*/
	private $address;
	/**
	* list of cc addresses
	*
	* @var	string $cc
	*/
	private $cc;
	/**
	* list of bcc addresses
	*
	* @var	string $bcc
	*/
	private $bcc;
	/**
	* attach a file to the mail
	*
	* @var	string $filename
	*/
	private $filename;
	/**
	* attach a file type
	*
	* @var	string $filetype
	*/
	private $filetype;
	/**
	* set the organization header
	*
	* @var	string $org
	*/
	private $org;
	/**
	* instruct the Mailclient to display the file if possible ("inline") or always as a link ("attachment")
	*
	* @var string $disposition
	*/
	private $disposition;
	/**
	* message priorities referential
	*
	* @var string $priority
	*/
	private $priority;
	/**
	 * Stores function type's name
	 *
	 * @var string $type
	 */	 
	private $type;
	/**
	 * Flag for error
	 *
	 * @var boolean $error
	 */	
	private $error;	
	/**
	 * Stores error numbers
	 *
	 * @var string $errmsg
	 */		
	private $errmsg='1';	
	/**
	 * Stores error numbers and description
	 *
	 * @var array $debug
	 */	
	public $debug = array();
	/**
	 * Stores the output
	 *
	 * @var bool $result
	 */	
	public $result;
	
/**
	 * Constructs a Lib_Email object with given parameters
	 * also it will invoke email process
	 * 
	 * @param string $type
	 * @param string $from
	 * @param string $to
	 * @param string $subject
	 * @param string $body
	 * @param string $address	 
	 * @param string $cc
	 * @param string $bcc
	 * @param string $priority
	 * @param string $filename
	 * @param string $filetype
	 * @param string $org	 
	 * @param string $charset
	 * @param string $disposition
	 * @param string $receipt	 
	 * @return Lib_Email
	 */
	 
	public function Lib_Email($type,$from,$to,$subject,$body,$address='',$cc='', $bcc='',$priority='',$filename='', $filetype = '', $org='',$charset='',$disposition = "inline",$receipt='0')
	{		
		$this->type = $type;
		$this->from = $from;
		$this->to = $to;
		$this->subject = $subject;
		$this->body = $body;	
		
		$this->address = $address;
		$this->cc = $cc;
		$this->bcc = $bcc;
		$this->priority = $priority;
		$this->filename = $filename;
		
		$this->filetype = $filetype;
		$this->disposition = $disposition;
		$this->org = $org;
		$this->charset = $charset;
		$this->receipt = $receipt;
		
		$this->autoCheck( true );
		$this->boundary= "--" . md5( uniqid("myboundary") );
		if($this->isValidCall())
		{
			if(strtolower($type)=='mail')
				$this->sendEmail();						
		}
	}
	
	/**
	 * Check whether the function call is valid or not
	 *
	 * @return bool
	 */
	 
	private function isValidCall()
	{
		if(strtolower($this->type)!='mail')
		{
			echo '<b>Component Error!<b> Invalid argument <i>type</i> - mail';
			exit();
		}		
		else if(empty($this->from))
		{
			echo '<b>Component Error!<b> Invalid argument <i>from</i> - from expected';
			exit();	
		}
		else if(empty($this->to))
		{
			echo '<b>Component Error!<b> Invalid argument <i>to</i> - to expected';
			exit();	
		}
		else if(empty($this->subject))
		{
			echo '<b>Component Error!<b> Invalid argument <i>subject</i> - subject expected';
			exit();	
		}
		else if(empty($this->body))
		{
			echo '<b>Component Error!<b> Invalid argument <i>body</i> - body expected';
			exit();	
		}		
		return true;		
	}
	
	/**
	 * This is the main function to call all functions and mail send
	 *
	 * @return bool
	 */
	
	private function sendEmail()
	{
		$this->subject($this->subject);
		$this->from($this->from);
		$this->replyTo($this->address);
		$this->to($this->to);
		if($this->errmsg!==1)
		{			
			$this->receipt($this->receipt);
			$this->cc($this->cc);
			if($this->errmsg!==1)
			{			
				$this->bcc($this->bcc);
				if($this->errmsg!==1)
				{
					$this->body($this->body,$this->charset);
					$this->organization($this->org);
					$this->priority($this->priority);
					$this->attach($this->filename,$this->filetype,$this->disposition);					
					$this->send();					
				}
				else
				{
					$this->error = 1;
					$this->debug['errinfo'] = array(1003=>'bcc mail id is invalid');
					return false;
				}
			}
			else
			{
			$this->error = 1;
			$this->debug['errinfo'] = array(1002=>'cc mail id is invalid');
			return false;
			}
		}
		else
		{
			$this->error = 1;
			$this->debug['errinfo'] = array(1001=>'To mail id is invalid');
			return false;
		}
	}
	
	/**	 		
	* activate or desactivate the email addresses validator
	* ex: autoCheck( true ) turn the validator on
	* by default autoCheck feature is on
	*
	* @param boolean	$bool set to true to turn on the auto validation
	*/
	
	private function autoCheck( $bool )
	{
		if( $bool )
			$this->checkAddress = true;
		else
			$this->checkAddress = false;
	}
	
	/**	
	* Define the subject line of the email
	*
	* @param string $subject any monoline string
	*/
	
	private function subject( $subject )
	{
		$this->xheaders['Subject'] = strtr( $subject, "\r\n" , "  " );
	}

	/**
	* set the sender of the mail
	*
	* @param string $from should be an email address
	*/
 
	private function from( $from )
	{
		if( ! is_string($from) ) 
		{
			echo "Class Mail: error, From is not a string";
			exit;
		}
		$this->xheaders['From'] = $from;
	}

	/**
	* set the Reply-to header 
	*
	* @param string $email should be an email address
	*/ 
	
	private function replyTo( $address )
	{
		if( ! is_string($address) ) 
			return false;
	
		$this->xheaders["Reply-To"] = $address;		
	}

	/**
	* add a receipt to the mail ie.  a confirmation is returned to the "From" address (or "replyTo" if defined) 
	* when the receiver opens the message.
	*
	* @warning this functionality is *not* a standard, thus only some mail clients are compliants.
	*/
 
	private function receipt($receipt)
	{
		if($receipt!=0)
			$this->receipt = 1;
	}

	/**
	* set the mail recipient
	*
	* @param string $to email address, accept both a single address or an array of addresses
	*/

	private function to( $to )
	{
		// TODO : test validité sur to
		if( is_array( $to ) )
			$this->sendto= $to;
		else 
			$this->sendto[] = $to;

		if( $this->checkAddress == true )
			$this->checkAdresses( $this->sendto );
	}

	/**		
	 * set the CC headers ( carbon copy )
	 *
	 * $cc : email address(es), accept both array and string
	 */

	private function cc( $cc )
	{
		if(!empty($cc))
		{
			if( is_array($cc) )
				$this->acc= $cc;
			else 
				$this->acc[]= $cc;
		
			if( $this->checkAddress == true )
				$this->checkAdresses( $this->acc );
		}
	
	}

	/**
	 * set the bcc headers ( blank carbon copy ). 
	 *
	 * $bcc : email address(es), accept both array and string
	 */

	private function bcc( $bcc )
	{
		if(!empty($bcc))
		{
			if( is_array($bcc) ) 
			{
				$this->abcc = $bcc;
			} 
			else 
			{
				$this->abcc[]= $bcc;
			}

			if( $this->checkAddress == true )
				$this->checkAdresses( $this->abcc );
		}
	}

	/**		
	 * set the body (message) of the mail
	 * define the charset if the message contains extended characters (accents)
	 * default to us-ascii
	 * $mail->body( "mél en français avec des accents", "iso-8859-1" );
	 */
	 
	private function body($body, $charset="" )
	{
		$this->body = $body;
	
		if( $charset != "" ) 
		{
			$this->charset = strtolower($charset);
			if( $this->charset != "us-ascii" )
				$this->ctencoding = "8bit";
		}
	}

	/**
	 * set the organization header
	 */
 
	private function organization( $org )
	{
		if( trim( $org != "" )  )
			$this->xheaders['Organization'] = $org;
	}

	/**
	 * set the mail priority 
	 * $priority : integer taken between 1 (highest) and 5 ( lowest )
	 * ex: $mail->priority(1) ; => Highest
	 */
 
	private function priority( $priority )
	{
		if( ! intval( $priority ) )
			return false;
		
		if( ! isset( $this->priorities[$priority-1]) )
			return false;

		$this->xheaders["X-Priority"] = $this->priorities[$priority-1];
	
		return true;
	}

	/**
	 * attach a file to the mail	 
	 */

	private function attach( $filename, $filetype, $disposition)
	{
		// TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
		if( $filetype == "" )
			$filetype = "application/x-unknown-content-type";
		
		$this->aattach[] = $filename;
		$this->actype[] = $filetype;
		$this->adispo[] = $disposition;
	}

	/**
	* Build the email message	
	*/
	
	private function buildMail()
	{
		// build the headers
		$this->headers = "";
		//$this->xheaders['To'] = implode( ", ", $this->sendto );
	
		if( count($this->acc) > 0 )
			$this->xheaders['CC'] = implode( ", ", $this->acc );
	
		if( count($this->abcc) > 0 ) 
			$this->xheaders['BCC'] = implode( ", ", $this->abcc );	

		if( $this->receipt ) 
		{
			if( isset($this->xheaders["Reply-To"] ) )
				$this->xheaders["Disposition-Notification-To"] = $this->xheaders["Reply-To"];
			else 
				$this->xheaders["Disposition-Notification-To"] = $this->xheaders['From'];
		}
	
		if( $this->charset != "" ) 
		{
			$this->xheaders["Mime-Version"] = "1.0";
			$this->xheaders["Content-Type"] = "text/html; charset=$this->charset";
			$this->xheaders["Content-Transfer-Encoding"] = $this->ctencoding;
		}

		$this->xheaders["X-Mailer"] = "Php/libMailv1.3";
	
		// include attached files
		if( count( $this->aattach ) > 1 ) 
		{			
			$this->buildAttachement();
		} 
		else 
		{
			$this->fullBody = $this->body;
		}
		reset($this->xheaders);
		while( list( $hdr,$value ) = each( $this->xheaders )  ) 
		{
			if( $hdr != "Subject" )
				$this->headers .= "$hdr: $value\n";
		}
	}

	/**	
	* format and send the mail	
	*/ 
	
	private function send()
	{
		$this->buildMail();
	
		$this->strTo = implode( ", ", $this->sendto );
	
		// envoie du mail
		$res = mail( $this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers );
		if($res)
		{
			$this->result="true";
	 		return $this->result;
		}
		else
		{
			$this->result="false";
	 		return $this->result;
		}		
	}

	/**
	 * return the whole e-mail , headers + message
	 * can be used for displaying the message in plain text or logging it
	 */

	private function get()
	{
		$this->buildMail();
		$mail = "To: " . $this->strTo . "\n";
		$mail .= $this->headers . "\n";
		$mail .= $this->fullBody;
		return $mail;
	}

	/**
	* check an email address validity	
	* @param string $address : email address to check
	* @return true if email adress is ok
	*/
 
	private function validEmail($address)
	{
		if( ereg( ".*<(.+)>", $address, $regs ) ) 
		{
			$address = $regs[1];
		}
 		if(ereg( "^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) ) 
 			return true;
	 	else
 			return false;
	}

	/**
	* check validity of email addresses 
	* @param	array $aad - 
	* @return if unvalid, output an error message and exit, this may -should- be customized
	*/
 
	private function checkAdresses( $aad )
	{
		for($i=0;$i< count( $aad); $i++ ) 
		{
			if( ! $this->validEmail( $aad[$i]) ) 
			{
				$this->errmsg = 1;							
			}
		}
	}

	/**
	* check and encode attach file(s) . internal use only
	*/

	private function buildAttachement()
	{
		$this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";

		$this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
		$this->fullBody .= "Content-Type: text/html; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n" . $this->body ."\n";
	
		$sep= chr(13) . chr(10);
	
		$ata= array();
		$k=0;
	
		// for each attached file, do...
		for( $i=0; $i < count( $this->aattach); $i++ ) {
		
			$filename = $this->aattach[$i];
			$basename = basename($filename);
			$ctype = $this->actype[$i];	// content-type
			$disposition = $this->adispo[$i];
		
			if( ! file_exists( $filename) ) 
			{
				echo "Class Mail, method attach : file $filename can't be found"; exit;
			}
			$subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding: base64\nContent-Disposition: $disposition;\n  filename=\"$basename\"\n";
			$ata[$k++] = $subhdr;
			// non encoded line length
			$linesz= filesize( $filename)+1;
			$fp= fopen( $filename, 'r' );
			$ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
			fclose($fp);
		}
		$this->fullBody .= implode($sep, $ata);
	}
	
}
?>