HEX
Server: Apache
System: Linux v38079.2is.nl 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: democfellows (10015)
PHP: 8.1.34
Disabled: opcache_get_status
Upload Files
File: /var/www/vhosts/creativefellows.nl/jhtaxatie.creativefellows.nl/classes/ImapController.php
<?php

	/**
	* Description
	*/
	class ImapController{
				
		
		function __construct($settings)
		{
			
	 		$hostname 				= "{". $settings["server"]  .":". $settings["port"]  ."/ssl/novalidate-cert}INBOX"; 
			
			$username 				= $settings["username"];
			$password 				= $settings["password"];
			$this->attachment_name 	= [];
	 
			/* try to connect */
			try
			{
				$inbox = imap_open($hostname,$username,$password);		
				
 
				$emails = imap_search($inbox,'ALL');
				

 
				/* useful only if the above search is set to 'ALL' */
				$max_emails = 1;
 
				$att_count = 0;
				
				/* if any emails found, iterate through each email */
				if($emails) {
 
				    $count = 1;
 
				    /* put the newest emails on top */
				    rsort($emails);
 
				    /* for every email... */
				    foreach($emails as $email_number) 
				    {
					
					
				        /* get information specific to this email */
				        $overview = imap_fetch_overview($inbox,$email_number,0);
						
						
						// check if email is new
						if($overview[0]->deleted == 1) continue;
					
				
				        /* get mail message */
				        $message = imap_fetchbody($inbox,$email_number,2);
 
				        /* get mail structure */
				        $structure = imap_fetchstructure($inbox, $email_number);
 
				        $attachments = array();
 
				        /* if any attachments found... */
				        if(isset($structure->parts) && count($structure->parts)) 
				        {	
						
							/*
							 * main parts
							 */
							foreach($structure->parts as $main_part)
							{
							
								if($main_part->disposition){
								
								
					                $attachments[$att_count] = array(
					                    'is_attachment' => false,
					                    'filename' => '',
					                    'name' => '',
					                    'attachment' => '',
										'extension' => ''
					                );
								
					                if($main_part->ifdparameters) 
					                {	
									
					                    foreach($main_part->dparameters as $object) 
					                    {	
					                        if(strtolower($object->attribute) == 'filename') 
					                        {
					                            $attachments[$att_count]['is_attachment'] = true;
					                            $attachments[$att_count]['filename'] = $object->value;
												$attachments[$att_count]['extension'] = end(explode(".",$object->value));
					                        }
					                    }
					                }
 
					                if($main_part->ifparameters) 
					                {
					                    foreach($main_part->parameters as $object) 
					                    {
					                        if(strtolower($object->attribute) == 'name') 
					                        {
					                            $attachments[$att_count]['is_attachment'] = true;
					                            $attachments[$att_count]['name'] = $object->value;
												$attachments[$att_count]['extension'] = end(explode(".",$object->value));
					                        }
					                    }
					                }
								 
					                if($attachments[$att_count]['is_attachment']) 
					                {
					                    $att_data = imap_fetchbody($inbox, $email_number, 2);
									
					                    /* 4 = QUOTED-PRINTABLE encoding */
					                    if($main_part->encoding == 3) 
					                    { 
					                        $attachments[$att_count]['attachment'] = base64_decode($att_data);
					                    }
					                    /* 3 = BASE64 encoding */
					                    elseif($main_part->encoding == 4) 
					                    { 
					                        $attachments[$att_count]['attachment'] = quoted_printable_decode($att_data);
					                    }
									
					                }
								
									$att_count++;
								}
							
							
							}
						
						
							/* 
							 * subparts
							 */
				            for($i = 0; $i < count($structure->parts); $i++) 
				            {
														
								if(!$structure->parts[$i]->parts) continue;
							
								$sub_parts = $structure->parts[$i]->parts;
							
								foreach($sub_parts as $x => $part){
								
								
									if(!$part->disposition) continue;
								
					                $attachments[$att_count] = array(
					                    'is_attachment' => false,
					                    'filename' => '',
					                    'name' => '',
					                    'attachment' => '',
										'extension' => ''
					                );
 
					                if($part->ifdparameters) 
					                {	
									
					                    foreach($part->dparameters as $object) 
					                    {	
					                        if(strtolower($object->attribute) == 'filename') 
					                        {
					                            $attachments[$att_count]['is_attachment'] = true;
					                            $attachments[$att_count]['filename'] = $object->value;
												$attachments[$att_count]['extension'] = end(explode(".",$object->value));
					                        }
					                    }
					                }
 
					                if($part->ifparameters) 
					                {
					                    foreach($part->parameters as $object) 
					                    {
					                        if(strtolower($object->attribute) == 'name') 
					                        {
					                            $attachments[$att_count]['is_attachment'] = true;
					                            $attachments[$att_count]['name'] = $object->value;
												$attachments[$att_count]['extension'] = end(explode(".",$object->value));
					                        }
					                    }
					                }
								 
					                if($attachments[$att_count]['is_attachment']) 
					                {
					                    $att_data = imap_fetchbody($inbox, $email_number, 2.2);
 
					                    // 4 = QUOTED-PRINTABLE encoding
					                    if($part->encoding == 3) 
					                    { 
					                        $attachments[$att_count]['attachment'] = base64_decode($att_data);
					                    }
					                   	//3 = BASE64 encoding 
					                    elseif($part->encoding == 4) 
					                    { 
					                        $attachments[$att_count]['attachment'] = quoted_printable_decode($att_data);
					                    }
									
					                }
								
									
								}
							
								$att_count++;
				            }
												
				        }
					
					
				        /* iterate through each attachment and save it */
				        foreach($attachments as $count => $attachment)
				        {
				            if($attachment['is_attachment'] == 1 && strtolower($attachment['extension']) == "csv")
				            {
				                $filename = $attachment['name'];
				                if(empty($filename)) $filename = $attachment['filename'];
							
								//$this->attachment_name = "../csv/" .uniqid() . "-" . $filename;
								$this->attachment_name = "../csv/" .date("Y-m-d")."-". uniqid() .".csv";
								
			                
								$fp = fopen($this->attachment_name, "w+");
							
								stream_filter_append($fp, 'convert.quoted-printable-decode', STREAM_FILTER_WRITE);
							
				                fwrite($fp, $attachment['attachment']);
				                fclose($fp);
							
				            }
 
				        }
					
						//d($attachments);
					
						// move to read folder and delete
						imap_mail_move($inbox,$email_number,'INBOX.archive');
						imap_delete($inbox,$email_number);
 
				        if($count++ >= $max_emails) break;
					
					
					
				    }
 
				} 
 		   	
		
				/* close the connection */
				imap_close($inbox);
						
			}
			catch(\Exception $e)
			{
			    $imapErrors = implode("; ", imap_errors());
			    $message = $e->getMessage() . "\n\nIMAP ERRORS: {$imapErrors}";
			    throw new Exception($message);
			}
/// 		   	if(!$inbox) return 'Cannot connect to Mail Server: ' . imap_last_error();

 
 			
				
		}
		
		
	}
	
	
?>