<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require 'mailerPHP/PHPMailerAutoload.php';

/*
 * Orkin.php
 *
 * Created on March 30, 2017, 10:46 PM
 *
*/

/**
 <P>This class is used to receive request by Orkin

 @author 
 @see RMScheduler
 */

// http://localhost/RMScheduler/rm_webservices/index.php/MailSender
class MailSender extends CI_Controller {

function __construct() {
        @session_start();
        parent::__construct();
       // $this->load->library('security');
        $this->ci = & get_instance();
        $this->ci->load->database();
        $this->allow_get_array = TRUE;
        //$this->ci->load->model('Model_orkin');

    }
    
    public function index()
	{
		echo'test ';

	}
    
   
  public function sendEmailGET(){
    $bcc = "programacion@orkincr.com";
	$email    = "";
	$urlLink  = "";
	$commEmai = "";
	$host = "";
	$user = "";
	$pass = "";
	$subj = "";
	$nameUser = "";

	if ( $_SERVER["REQUEST_METHOD"] == "GET" ) {
 	  $email    = $_GET["email"];
	  $urlLink  = $_GET["url"];
	  $commEmai = $_GET["comm"];
	  $host 	= $_GET["host"];
	  $user 	= $_GET["user"];
	  $pass 	= $_GET["pass"];
	  $subj 	= $_GET["subj"];
	  $nameUser = $_GET["nameUser"];
	}
	//var_dump($_GET);
	$urlLink =  str_replace("%26","&",$urlLink);
	$urlLink =  str_replace(" ","%20",$urlLink);
	$urlLink = str_replace("#","%23",$urlLink);
	$subj    =  str_replace("%20"," ",$subj);
	$nameUser  =  str_replace("%20"," ",$nameUser);


	
		$config = Array(
            'protocol' => "smtp",
            'smtp_host' => $host,
            'smtp_port' => 587,
            'smtp_timeout' => 10,
            'smtp_user' => $user,
            'smtp_pass' => $pass,
            'mailtype' => "html",
            'charset' => "utf-8",
            'newline' => "\r\n",
            'crlf' => "\r\n",
            'validation' => true
            //'smtp_crypto' => 'ssl'
        );
        
    date_default_timezone_set('America/Costa_Rica');
    $p1 = 'Estimado cliente. ';
	$p2 = 'Usted esta recibiendo un reporte enviado por ORKIN. ';
	$p3 = 'Por favor presione '. " <a href='".$urlLink ;
	$p4 = "'>aqui</a>". ' para visualizarlo.';

	$emailBody    =  $p1. $p2. $p3. $p4. ' <br> '. $commEmai;
    //Obtenemos la instancia
    $this->load->library('email', $config);
    $this->email->set_crlf("\n");
    //Se configura las opciones del email
    $this->email->from($user, $nameUser);
    $this->email->to($email);
    //$this->email->bcc($bcc);
    //Código para reestablecer la contraseña
    $this->email->subject($subj);
    $this->email->message($emailBody);
    //Se envia el email
    
	if(!$this->email->send()) {
    	//echo ' ERROR';
    	echo $this->email->print_debugger();
	    //echo ' Error: ' . $mail->ErrorInfo;
	} else {
    	echo 'OK';
	}

 }

public function sendReportByEmail(){
    
	$email    = "";
	$urlLink  = "";
	$user = "";
	$pass = "";
	$client = "";
	$nameUser = "";
	$reason = "";


 	  $email    = $this->input->post("email");
	  $urlLink  = $this->input->post("url");
	  $user 	= $this->input->post("user");
	  $pass 	= $this->input->post("pass");
	  $nameUser = $this->input->post("nameUser");
	  $client 	= $this->input->post("client");
	  $reason 	= $this->input->post("reason");


	$urlLink =  str_replace("%26","&",$urlLink);
	$urlLink =  str_replace(" ","%20",$urlLink);
	//$urlLink = str_replace("#sc#","-",$urlLink);
    //$urlLink = str_replace("-","%2D",$urlLink);
    $urlLink = str_replace("#","%23",$urlLink);
	$nameUser  =  str_replace("%20"," ",$nameUser);

	//echo $urlLink.'!!!';

	
	$config = Array(
            'protocol' => "smtp",
            'smtp_host' => "mail.route-miner.com",
            'smtp_port' => 587,
            'smtp_timeout' => 15,
            'smtp_user' => $user,
            'smtp_pass' => $pass,
            'mailtype' => "html",
            'charset' => "utf-8",
            'newline' => "\r\n",
            'crlf' => "\r\n ",
            'validation' => true
        );
        
    date_default_timezone_set('America/Costa_Rica');
    $p1 = 'Hubo un problema con ' . '<a href='.$urlLink.'>este</a>'. ' reporte.';
    $p2 = '';
    switch ($reason) {
    	case "0":
			$p2 = 'No se pudo identificar la causa del problema.';
			break;
		case "1":
			$p2 = 'No se encontro el fin del reporte.';
			break;
		case "2":
			$p2 = 'Esta vacío,no se agregaron datos al reporte.';
			break;
		case "3":
			$p2 = 'Hubo un problema al recuperar los datos del inicio o fin del reporte.';
			break;
		case "4":
			$p2 = 'Reporte invalido o inactivo.';
			break;
		case "5":
			$p2 = 'No se pudo recuperar las imagenes de las firmas.';
			break;
		case "6":
			$p2 = 'No se pudo ubicar el reporte por que no se reconoce el cliente.';
			break;
	}

	$emailBody    =  $p1. $p2. ' <br> ';
    //Obtenemos la instancia
    $this->load->library('email', $config);
    $this->email->set_crlf("\n");
    //Se configura las opciones del email
    $this->email->from($user, $nameUser);
    $this->email->to($email);
    $subject = "Problema con reporte generado para ".$client." ";
    //echo $subject."<br>";
    $this->email->subject($subject);
    $this->email->message($emailBody);
    //Se envia el email
    $sended = false;
    $count = 0;
    while(!$sended){

    	if(!$this->email->send()) {
    	//echo 'ERROR
    	echo $this->email->print_debugger();
	    //echo ' Error: ' . $mail->ErrorInfo;
	    $count++;
	    sleep(1);

		} else {
	    	echo 'OK';
	    	$sended = true;
		}
		if($count == 3){
			break;
		}
    }

 }
    
    
public function sendEmailPOST(){
    $bcc = "programacion@orkincr.com";
	$email    = "";
	$urlLink  = "";
	$commEmai = "";
	$host = "";
	$user = "";
	$pass = "";
	$subj = "";
	$nameUser = "";

	if ( $_SERVER["REQUEST_METHOD"] == "POST" ) {
 	  $email    = $_POST["email"];
	  $urlLink  = $_POST["url"];
	  $commEmai = $_POST["comm"];
	  $host 	= $_POST["host"];
	  $user 	= $_POST["user"];
	  $pass 	= $_POST["pass"];
	  $subj 	= $_POST["subj"];
	  $nameUser = $_POST["nameUser"];
	}
	//var_dump($_POST);
	$urlLink =  str_replace("%26","&",$urlLink);
	$urlLink =  str_replace(" ","%20",$urlLink);
	$urlLink = str_replace("#","%23",$urlLink);
	$urlLink = str_replace("%3D","=",$urlLink);
	$urlLink = str_replace("%3A",":",$urlLink);
	$urlLink = str_replace("%3F","?",$urlLink);
	$subj    =  str_replace("%20"," ",$subj);
	$nameUser  =  str_replace("%20"," ",$nameUser);


	
		$config = Array(
            'protocol' => "smtp",
            'smtp_host' => $host,
            'smtp_port' => 587,
            'smtp_timeout' => 10,
            'smtp_user' => $user,
            'smtp_pass' => $pass,
            'mailtype' => "html",
            'charset' => "utf-8",
            'newline' => "\r\n",
            'crlf' => "\r\n",
            'validation' => true
            //'smtp_crypto' => 'ssl'
        );
        
    date_default_timezone_set('America/Costa_Rica');
    $p1 = 'Estimado cliente. ';
	$p2 = 'Usted esta recibiendo un reporte enviado por ORKIN. ';
	$p3 = 'Por favor presione '. " <a href='".$urlLink ;
	$p4 = "'>aqui</a>". ' para visualizarlo.';

	$emailBody    =  $p1. $p2. $p3. $p4. ' <br> '. $commEmai;

	//echo $emailBody;
    //Obtenemos la instancia
    $this->load->library('email', $config);
    $this->email->set_crlf("\n");
    //Se configura las opciones del email
    $this->email->from($user, $nameUser);
    $this->email->to($email);
    //$this->email->bcc($bcc);
    //Código para reestablecer la contraseña
    $this->email->subject($subj);
    $this->email->message($emailBody);
    //Se envia el email
    
	if(!$this->email->send()) {
    	//echo ' ERROR';
    	echo $this->email->print_debugger();
	    //echo ' Error: ' . $mail->ErrorInfo;
	} else {
    	echo 'OK';
	}

 } 
    
    
    
}



?>
