<?php namespace App\Controllers;

use CodeIgniter\Controller;
use App\Andy\Utility;

use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\OAuth;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
use CodeIgniter\I18n\Time;



class Attachment  extends BaseController
{

protected $mimeType;
protected $mimeLogic;
protected $fileSize;
protected $count;
protected $newCount;
 protected $myTime;

 public function __construct() 
				    {
								parent::__construct();
						  $this->myTime = parent::getTime();
						$this->myDate= date("d/m/Y",$this->myTime); 	
					
			       }
	









	public function index()

	{
	echo "i am class Attachment, method index";
	}

	public function showForm()
	{


		
			$data =[
			'title'=>'for attachment ',
		        'info'=>'&nbsp; ',
			'count'=>'null',
			'date'=>$this->myDate
			];
				
			echo view('header',$data);
			echo view('attachForm',$data);
			echo view('footer',$data);	
		


	}      

public function process()


		{
                 session_start();
		    helper('form');
			helper('html');       
			
			 
			   $description = $this->request->getVar('fileDescription'); 
			   $accessCode = $this->request->getVar('code'); 
			   $file = $this->request->getFile('userfile');
			   $fileName= $file->getName(); 
			   $tempfile = $file->getTempName();
	                   $theFile = new \CodeIgniter\Files\File($tempfile);
			   $this->mimeType=    $theFile->getMimeType();
			   $this->fileSize= $theFile->getSize('kb');  
			   $handle= new Utility();
	                   $this->mimeLogic= $handle->checkMime($this->mimeType);
			   
			   $ip = $_SERVER['REMOTE_ADDR'];
			    $theIp = strval($ip);
			   
			   
			  $theIp;
			   $message =" enquiry for printing: \r\n"."their Ip address" .$theIp. " \r\n ". " file description ".$description. "";
			   
			   
			   
			   
			   
			   
			  
			   if ($accessCode!="000")
			   {
			   
					   if(isset( $_SESSION['count']) ==false)
			
					{
			
					$_SESSION['count']= 5;
			                $this->count= $_SESSION['count'];
			               echo  $this->count;
					}
			 
			   	   if(isset( $_SESSION['count']) ==true)
				   
				   {
				   $this->count= $_SESSION['count'];
				   $this->newCount=$this->count-1;
				   $_SESSION['count']= $this->newCount;
				   
				   
				   
				   if ($_SESSION['count'] < 1)
				   
				   {
				  return redirect('spam');
				   
				   }
				   
				   }
			  
		
			$data =[
			'title'=>'for attachment ',
		        'info'=>'you have no right to upload',
			
			];
				
			echo view('header',$data);
			echo view('attachForm',$data);
			echo view('footer');	
		
			   
			   exit();
			   
			   
			   }
			  
			  
			   if ($this->fileSize > 400 )
			   {
			   
			   
			    helper('form');
			helper('html');
		
			$data =[
			'title'=>'for attachment ',
		        'info'=>'file too big max 400kb'
			];
				
			echo view('header',$data);
			echo view('attachForm',$data);
			echo view('footer');	
		
			   
			   exit();
			   
			   
			   }
			  
			   
			   
			  
			   
	                   if ($this->mimeLogic!=True)
			   {
			   
			   
			    helper('form');
			helper('html');
		
			$data =[
			'title'=>'for attachment ',
		        'info'=>'thats not an allowed file  '
			];
				
			echo view('header',$data);
			echo view('attachForm',$data);
			echo view('footer');	
		
			   
			   exit();
			   
			   
			   }
	   
	                 
			
			     
	   
			   
	   
			    try 
			{
			
	   
				  
	 
	 
			       $mail = new PHPMailer(); 
				$mail->isSMTP();
				$mail->Timeout = 20;
				//Enable SMTP debugging, 0 = off (for production use),  1 = client messages,  2 = client and server messages
				$mail->SMTPDebug = 0;
				//Set the hostname of the mail server
				$mail->Host = 'smtp.gmail.com';
				$mail->Port = 587;
				$mail->SMTPSecure = 'tls';
				$mail->SMTPAuth = true;
				//Username to use for SMTP authentication - use full email address for gmail
				$mail->Username = "andybrookestar@gmail.com";
				//Password to use for SMTP authentication
				$mail->Password = "ewskcctyzsieqvhr";
				//Set who the message is to be sent from
				$mail->setFrom('andy@benxmidia.com', 'andy brookes');
				$mail->addAddress('andybrookestar@yahoo.com', 'andy');
				//$mail->addAddress('blackcatglm@gmail.com', 'benjain');
				
				$mail->addAttachment($tempfile,$fileName);
				//$mail->addReplyTo('replyto@example.com', 'First Last');
				//$mail->addAddress('ginaacheampong@gmail.com', 'gina');
				//$mail->AddCC('andybrookestar@yahoo.com','andy');
		
		
				//Set the subject line
				$mail->Subject = 'enquiry from benxmidia dev slackware    ';
				//Read an HTML message body from an external file, convert referenced images to embedded,
				//convert HTML into a basic plain-text alternative body
				//$mail->msgHTML(file_get_contents('contents.html'), __DIR__);
				//Replace the plain text body with one created manually
				$mail->Body = $message;
		$logic2 = $result=	$mail->send();
		   }
		
		catch ( \Exception  $e)
			 {
			 $data = [
						'title'  => 'info',
					  
				   'info'=>'somwething went wrong '
					 ];

				echo view('header', $data);
				echo view('info', $data);
				echo view('footer');	
				  
		   
			 die();
			 //above try catch works 
		   }
		
			
	 
	 
			if($logic2== true)
			
			{
			$data =[
			'title'=>'info ',
		        'info'=>'your file was sent'
			];
				
			echo view('header',$data);
			echo view('info',$data);
			echo view('footer');	
		
			
			
			}
	 
			
	 
	 

		}//end process
}//end class 
