<?php
 class Utility{
 	protected $CI;
	private $font = 'font/monofont.ttf';
 	public function __construct($rules = array())
	{
		$this->CI =& get_instance();
		if(!$this->CI->session->userdata('user_session'))
		      $this->setSecurity(uniqid());
		
	}
	public function setSecurity($security_code)
	{
	        $this->CI->session->set_userdata('user_session',$security_code);
	}
	public function getSecurity()
	{
		/*if($this->CI->session->userdata('user_session')=='')
		      $this->setSecurity(uniqid()); 
		return $this->CI->session->userdata('user_session');*/
		return 1;
	}
	function showFrontMsg()
	{
	    if($this->CI->session->userdata('error_msg'))
	    {   
            echo $this->CI->session->userdata('error_msg');
            $this->CI->session->unset_userdata('error_msg');
	    }
	}
	function showMsg()
	{
	     if($this->CI->session->userdata('msg'))
	     {    
                $caption="Alert!!";
                $icon="fa-ban";
                if($this->CI->session->userdata('class')=="alert-success")
                {
                    $caption="Success!!";
                    $icon="fa-check";
                }
                echo '
                    <div class="alert '.$this->CI->session->userdata('class').' alert-dismissable">
                       <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                           <h4><i class="icon fa '.$icon.'"></i> '.$caption.'</h4>
                           '.$this->CI->session->userdata('msg').'
                   </div>';

                $this->CI->session->unset_userdata('msg');
                $this->CI->session->unset_userdata('class');
	     }
	}
	function setMsg($str,$type)
	{
		$this->CI->session->set_userdata('class','alert-success');
		$this->CI->session->set_userdata('msg',$str);
		if($type=='ERROR'){
			$this->CI->session->set_userdata('class','alert-danger');
			$this->CI->session->set_userdata('msg',$str);
		}
	}
	function sendMail($to,$sub,$body,$from='',$template=TRUE)
	{
    	  /*$config = array(
			    'protocol' => 'smtp',
			    'smtp_host' => 'ssl://smtp.googlemail.com',
			    'smtp_port' => 465,
			    'smtp_user' => 'testdevloper007@gmail.com',
			    'smtp_pass' => 'Password@321',
			    'mailtype'  => 'html', 
			    'charset'   => 'iso-8859-1'
			);*/
    	  $from='noreply@castleevents.com';
    	  $this->CI->load->library('email');
    	  $this->CI->email->set_mailtype("html");
    	  $this->CI->email->from($from, 'Castleevents');
    	  $this->CI->email->to($to); 
		  $this->CI->email->attach($_SERVER['DOCUMENT_ROOT'].'/assets/images/new-logo-mail.png', 'inline');
		  $cid1 = $this->CI->email->attachment_cid($_SERVER['DOCUMENT_ROOT'].'/assets/images/new-logo-mail.png');
    	  $this->CI->email->cc('info@anexus.ch');
    	  $this->CI->email->subject($sub);
		  if($template){
    	  $cont='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    	  <html xmlns="http://www.w3.org/1999/xhtml">
    	  <head>
    	   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	    <title>Untitled Document</title>
    	  </head>
    	
    	  <body>
    		<div style="width:600px; float:left; background:#F3F3F3; border:0px solid #388E1A;">
    		 <div style="text-align:left;padding:1px;background:#fff;"><a href="'.base_url().'"><img alt=Castleevents" src="cid:'.$cid1.'"></a></div>
    		 <div style="font-family:Arial, Helvetica, sans-serif; font-weight:bold; font-size:15px; color:#333; padding:20px; margin: 0; border-top:0px solid #0BB1E0;">
    		   <div>'.$body.'</div>
    	     </div>
    	    </div>
    	  </body>
    	 </html>';
		 }
		 else{
		 $cont=$body;
		 }
		 //echo $cont; 
    	 $this->CI->email->message($cont);
    	 $mail = $this->CI->email->send();
    	 //echo $this->CI->email->print_debugger();
    	 if($mail)
    	 {
    	 	return 1;
    	 }
    	 else
    	 {
    	 	return 0; 
    	 }
    	 
	}
	function sendCIMail($to,$sub,$body,$from='',$template=TRUE)
	{
    	  /*$config = array(
			    'protocol' => 'smtp',
			    'smtp_host' => 'ssl://smtp.googlemail.com',
			    'smtp_port' => 465,
			    'smtp_user' => 'testdevloper007@gmail.com',
			    'smtp_pass' => 'Password@321',
			    'mailtype'  => 'html', 
			    'charset'   => 'iso-8859-1'
			);*/
    	  $from='noreply@castleevents.com';
    	  $this->CI->load->library('email');
    	  $this->CI->email->set_mailtype("html");
    	  $this->CI->email->from($from, 'Castleevents');
    	  $this->CI->email->to($to); 
    	  //$this->CI->email->cc('info@castleevents.com');
		  $this->CI->email->attach($_SERVER['DOCUMENT_ROOT'].'/assets/images/new-logo-mail.png', 'inline');
		  $cid1 = $this->CI->email->attachment_cid($_SERVER['DOCUMENT_ROOT'].'/assets/images/new-logo-mail.png');
    	  $this->CI->email->subject($sub);
		  if($template){
    	  $cont='<!DOCTYPE html>
					<html>
					   <head>
					      <meta charset="utf-8">
					      <meta name="viewport" content="width=device-width">
					      <meta http-equiv="X-UA-Compatible" content="IE=edge">
					      <title>Varification</title>
					      <style type="text/css">
					         html,
					         body {
					         margin: 0 auto !important;
					         padding: 0 !important;         
					         width: 100% !important;
					         }
					         
					         * {
					         -ms-text-size-adjust: 100%;
					         -webkit-text-size-adjust: 100%;
					         }
					         
					         div[style*="margin: 16px 0"] {
					         margin:0 !important;
					         }
					         
					         table,
					         td {
					         mso-table-lspace: 0pt !important;
					         mso-table-rspace: 0pt !important;
					         }
					      
					         table {
					         border-spacing: 0 !important;
					         border-collapse: collapse !important;
					         table-layout: fixed !important;
					         Margin: 0 auto !important;
					         }
					         table table table {
					         table-layout: auto; 
					         }
					       
					         img {
					         -ms-interpolation-mode:bicubic;
					         }
					        
					         .mobile-link--footer a,
					         a[x-apple-data-detectors] {
					         color:inherit !important;
					         text-decoration: underline !important;
					         }
					      </style>
					     
					      <style>
					        
					         .button-td,
					         .button-a {
					         transition: all 100ms ease-in;
					         }
					         .button-td:hover,
					         .button-a:hover {
					         background: #555555 !important;
					         border-color: #555555 !important;
					         }
					      </style>
					   </head>
					   <body width="100%" bgcolor="#222222" style="Margin: 0;">
					      <center style="width: 100%; background: #222222;">
					         <div style="max-width: 600px; margin: 40px auto;">
					           
					                     <table cellspacing="0" cellpadding="0" border="0" align="center" width="100%" style="max-width: 600px;">
					                        <tr>
					                           <td style="padding: 20px 0; text-align: center; background:#131218;">
					                              <a href="'.base_url().'"><img src="cid:'.$cid1.'" width="250" alt="Castleevents" border="0"></a>
					                           </td>
					                        </tr>
					                     </table>'.$body.'</div>
						      </center>
						   </body>
						</html>';
		 }
		 else{
		 $cont=$body;
		 }
		 //echo $cont; 
    	 $this->CI->email->message($cont);
    	 $mail = $this->CI->email->send();
    	 //echo $this->CI->email->print_debugger();
    	 if($mail)
    	 {
    	 	return 1;
    	 }
    	 else
    	 {
    	 	return 0; 
    	 }
    	 
	}
	function pagination($target,$total_rows,$per_page,$uri=3,$search=null)
    {
	   $this->CI->load->library('pagination');
	   $config['base_url'] = $target;
	   $config['total_rows'] = $total_rows;
	   $config['per_page'] = $per_page; 
	   $config['full_tag_open'] = '<div class="pagination">';
	   $config['full_tag_close'] = '</div>';
	   $config['uri_segment'] = $uri;

	   $config['prev_link'] = 'PREV';
       

       $config['next_link'] = 'NEXT';
       
	   $this->CI->pagination->initialize($config); 
	   return $this->CI->pagination->create_links($search);
	}
	function pic_resize($source_path,$thumb_path,$width,$height)
	{
		$this->CI->load->library('image_lib');
	    $config['image_library'] = 'gd2';
	    $config['source_image']  = $source_path;
	    $config['new_image']  =  $thumb_path;
		$config['overwrite'] = TRUE;
		if($width>0)
		$config['width']	 = $width;
			if($height>0)
	    $config['height'] = $height;  
		$config['maintain_ratio'] = FALSE;
	    $this->CI->image_lib->initialize($config);      
	    $this->CI->image_lib->resize();
	}
	function imageblur($source_path,$thumb_path,$width,$height)
	{
		$this->CI->load->library('image_lib');
	    $config['image_library'] = 'ImageMagick';
	    $config['library_path'] = '/home/dapldevelopment/'; 
	    $config['source_image']  = $source_path;
	    $config['new_image']  =  $thumb_path;
		//$config['overwrite'] = TRUE;
		if($width>0)
		$config['width']	 = $width;
			if($height>0)
	    $config['height'] = $height;  
		$config['maintain_ratio'] = true;
		$config['quality'] = '80%';
		$this->CI->image_lib->clear();
	    $this->CI->image_lib->initialize($config);      
	    $this->CI->image_lib->resize();
	}
	function images_upload($source_path='picture/',$width=0,$height=0)
	{
	    if(isset($_FILES['picture']) and $_FILES['picture']['size']!='')
		{
	        $config['upload_path'] = $source_path;
	        $config['allowed_types'] = 'jpg|jpeg|gif|png|PNG';
		    $config['file_name']=time();
		    $this->CI->load->library('upload', $config);
	            if(!$this->CI->upload->do_upload('picture'))
	            {
	               $img['msg'] = $this->CI->upload->display_errors();
	            }
	            else
	            {
	               $img=$this->CI->upload->data();  
		           $this->pic_resize($source_path.$img['orig_name'],$source_path.'thumb/'.$img['orig_name'],$width,$height);
	            }
		      return $img;
	    }
	}
	public function info_cleanQuery($str) {	
		return addslashes(htmlspecialchars($str, ENT_QUOTES));
	}
	public function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
		$code = $this->generateCode($characters);
		/* font size will be 75% of the image height */
		$font_size = $height * 0.75;
		$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
		/* set the colours */
		$background_color = @imagecolorallocate($image, 33, 33, 33);
		$text_color = @imagecolorallocate($image, 253, 221, 3);
		$noise_color = @imagecolorallocate($image, 100, 120, 180);
		/* generate random dots in background */
		for( $i=0; $i<($width*$height)/3; $i++ ) {
			@imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
		}
		/* generate random lines in background */
		for( $i=0; $i<($width*$height)/150; $i++ ) {
			@imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
		}
		/* create textbox and add text */
		$textbox = @imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
		$x = ($width - $textbox[4])/2;
		$y = ($height - $textbox[5])/2;
		@imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
		/* output captcha image to browser */
		header('Content-Type: image/jpeg');
		@imagejpeg($image);
		@imagedestroy($image);
		//$_SESSION['security_code']=$code;
		$this->CI->session->set_userdata('security_code',$code);
	}
	
	public function convertImage($originalImage, $outputImage, $quality)
	{
		// jpg, png, gif or bmp?
		$exploded = explode('.',$originalImage);
		$ext = $exploded[count($exploded) - 1]; 

		if (preg_match('/jpg|jpeg/i',$ext))
			$imageTmp=imagecreatefromjpeg($originalImage);
		else if (preg_match('/png/i',$ext))
			$imageTmp=imagecreatefrompng($originalImage);
		else if (preg_match('/gif/i',$ext))
			$imageTmp=imagecreatefromgif($originalImage);
		else if (preg_match('/bmp/i',$ext))
			$imageTmp=imagecreatefrombmp($originalImage);
		else
			return 0;
		// quality is a value from 0 (worst) to 100 (best)
		imagejpeg($imageTmp, $outputImage, $quality);
		imagedestroy($imageTmp);
		return 1;
	}
	public function xmlToArray($xml, $options = array()) {
		$defaults = array(
			'namespaceSeparator' => ':',//you may want this to be something other than a colon
			'attributePrefix' => '@',   //to distinguish between attributes and nodes with the same name
			'alwaysArray' => array(),   //array of xml tag names which should always become arrays
			'autoArray' => true,        //only create arrays for tags which appear more than once
			'textContent' => '$',       //key used for the text content of elements
			'autoText' => true,         //skip textContent key if node has no attributes or child nodes
			'keySearch' => false,       //optional search and replace on tag and attribute names
			'keyReplace' => false       //replace values for above search values (as passed to str_replace())
		);
		$options = array_merge($defaults, $options);
		$namespaces = $xml->getDocNamespaces();
		$namespaces[''] = null; //add base (empty) namespace
	 
		//get attributes from all namespaces
		$attributesArray = array();
		foreach ($namespaces as $prefix => $namespace) {
			foreach ($xml->attributes($namespace) as $attributeName => $attribute) {
				//replace characters in attribute name
				if ($options['keySearch']) $attributeName =
						str_replace($options['keySearch'], $options['keyReplace'], $attributeName);
				$attributeKey = $options['attributePrefix']
						. ($prefix ? $prefix . $options['namespaceSeparator'] : '')
						. $attributeName;
				$attributesArray[$attributeKey] = (string)$attribute;
			}
		}
	 
		//get child nodes from all namespaces
		$tagsArray = array();
		foreach ($namespaces as $prefix => $namespace) {
			foreach ($xml->children($namespace) as $childXml) {
				//recurse into child nodes
				$childArray = $this->xmlToArray($childXml, $options);
				list($childTagName, $childProperties) = each($childArray);
	 
				//replace characters in tag name
				if ($options['keySearch']) $childTagName =
						str_replace($options['keySearch'], $options['keyReplace'], $childTagName);
				//add namespace prefix, if any
				if ($prefix) $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName;
	 
				if (!isset($tagsArray[$childTagName])) {
					//only entry with this key
					//test if tags of this type should always be arrays, no matter the element count
					$tagsArray[$childTagName] =
							in_array($childTagName, $options['alwaysArray']) || !$options['autoArray']
							? array($childProperties) : $childProperties;
				} elseif (
					is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName])
					=== range(0, count($tagsArray[$childTagName]) - 1)
				) {
					//key already exists and is integer indexed array
					$tagsArray[$childTagName][] = $childProperties;
				} else {
					//key exists so convert to integer indexed array with previous value in position 0
					$tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties);
				}
			}
		}
	 
		//get text content of node
		$textContentArray = array();
		$plainText = trim((string)$xml);
		if ($plainText !== '') $textContentArray[$options['textContent']] = $plainText;
	 
		//stick it all together
		$propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || ($plainText === '')
				? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText;
	 
		//return node as array
		return array(
			$xml->getName() => $propertiesArray
		);
	}
	public function curlRequest($opta)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$opta);
		curl_setopt($ch, CURLOPT_FAILONERROR,1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 15);
		// Set query data here with CURLOPT_POSTFIELDS
		//curl_setopt($ch, CURLOPT_POSTFIELDS, $qry_str);
		
		$content = curl_exec($ch);
		curl_close($ch);
		return $content;
	}
	public function slug($str=''){
		$search=array('!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '=', '|', '~', '`', ',', '.', ';', ':', '"', '{', '}','?','/');
		$replace=array('', '', '', '', '', '', '', '', '', '', '-', '', '', '-', '', '', '', '', '', '', '', '', '','','' );
		$subject=trim($str);
		$course_slug=str_replace($search, $replace,$subject);
		$course_slug=str_replace(' ','-',$course_slug);
		$course_slug=str_replace('--','-',$course_slug);
		return strtolower($course_slug);
	}
	public function getOS($user_agent) { 
		$os_platform    =   "Unknown OS Platform";
		$os_array       =   array(
								'/windows nt 10/i'     =>  'Windows 10',
								'/windows nt 6.3/i'     =>  'Windows 8.1',
								'/windows nt 6.2/i'     =>  'Windows 8',
								'/windows nt 6.1/i'     =>  'Windows 7',
								'/windows nt 6.0/i'     =>  'Windows Vista',
								'/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
								'/windows nt 5.1/i'     =>  'Windows XP',
								'/windows xp/i'         =>  'Windows XP',
								'/windows nt 5.0/i'     =>  'Windows 2000',
								'/windows me/i'         =>  'Windows ME',
								'/win98/i'              =>  'Windows 98',
								'/win95/i'              =>  'Windows 95',
								'/win16/i'              =>  'Windows 3.11',
								'/macintosh|mac os x/i' =>  'Mac OS X',
								'/mac_powerpc/i'        =>  'Mac OS 9',
								'/linux/i'              =>  'Linux',
								'/ubuntu/i'             =>  'Ubuntu',
								'/iphone/i'             =>  'iPhone',
								'/ipod/i'               =>  'iPod',
								'/ipad/i'               =>  'iPad',
								'/android/i'            =>  'Android',
								'/blackberry/i'         =>  'BlackBerry',
								'/webos/i'              =>  'Mobile'
							);

		foreach ($os_array as $regex => $value) { 
			if (preg_match($regex, $user_agent)) {
				$os_platform    =   $value;
			}
		}   
		return $os_platform;
	}
	
	public function getBrowser($user_agent) {
		$browser        =   "Unknown Browser";
		$browser_array  =   array(
								'/msie/i'       =>  'Internet Explorer',
								'/firefox/i'    =>  'Firefox',
								'/safari/i'     =>  'Safari',
								'/chrome/i'     =>  'Chrome',
								'/opera/i'      =>  'Opera',
								'/netscape/i'   =>  'Netscape',
								'/maxthon/i'    =>  'Maxthon',
								'/konqueror/i'  =>  'Konqueror',
								'/mobile/i'     =>  'Handheld Browser'
							);

		foreach ($browser_array as $regex => $value) { 

			if (preg_match($regex, $user_agent)) {
				$browser    =   $value;
			}

		}
		return $browser;
	}
	function passwordGenaretor( $length = 8, $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.?*}{[]' ) 
    {
        return substr( str_shuffle( $chars ), 0, $length );
    }
}