<?php



/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: 			*/

// +----------------------------------------------------------------------+

// | PHP version 4/5                                                      |

// +----------------------------------------------------------------------+

// | Copyright (c) 2004-2010 ARMIA INC                                    |

// +----------------------------------------------------------------------+

// | This source file is a part of iScripts VistaCart                     |

// +----------------------------------------------------------------------+

// | Authors: Jipson<jipson.thomas@armiasystems.com>             		  |

// +----------------------------------------------------------------------+

App::import('Sanitize');


class SettingsController extends AppController

{
 

    var $name = "Setting";

    var $helpers = array('Html', 'Form','Pagination','Javascript','Ajax');   

	var $components = array ('Pagination','Catmenu','History','Email'); // Added

	var $uses = array('Setting',  'Country','Currency','Category','Statecode'); 

    function indexm()
    {

    	
        $cnfg 				=	Configure::read('Neutrino');

        $data	=	$this->Setting->find('list',array('fields'=>array('Setting.fieldname', 'Setting.value')));

      
        $style				=	$cnfg["style"];
        $this->set('style', "admin/".$style);
       	$this->__validateLoginStatus(); 
	   	$admsess	=	$this->Session->read('Admin');
		if($admsess["settings"] <> 1){
			$this->redirect(array('controller' => 'admins', 'action' => 'index')); 
			exit();
		}

		/*********************** Section to load css files name................................*/
		$theme_root=CSS;
		$csarr	=	array();
		$themes_dir = @ opendir($theme_root);
		$i	=	0;


		while ( ($theme_file = readdir($themes_dir)) !== false ) 
		{
			if($theme_file <> '.' && $theme_file <> '..' && $theme_file <> 'index.php' && $theme_file <> 'index.html' && $theme_file <> '.htaccess' && $theme_file <> 'jquery.ad-gallery.css' &&  $theme_file <> 'admin'){
				$thm		=	explode('.css',$theme_file);
				$csarr[$i]	=	$thm[0];
				$i++;
			}
		}

		asort($csarr);		
		$this->set('csarr', $csarr);		

		/*********************** Section to load css files name................................*/

		if($_SERVER['REQUEST_METHOD']=='POST'){
		//print_r($_POST);exit;
			$set		=	$_POST;
			$flag		=	true;
			$msg		=	'';
                        if($set['site_name'] <> ''){
                            $set['site_name'] = addslashes($set['site_name']);
                        }
			if($set['admin_email']==''){
				$msg = $msg." Site email can't be empty<br>";
				$flag	=	false;
			}else{
				if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $set['admin_email'])){
					$msg 	= $msg." Invalid Site email<br>";
					$flag	=	false;
				}
			}
                       if(trim($set['user_credit'] != "")) {
				if (!preg_match("/^[0-9]*\.?[0-9]+$/", trim($set['user_credit'])) ) {
					$msg 	= $msg." Invalid user credit<br>";
					$flag	=	false;
				}
			}
                       // echo $set['enable_facebook']. $set['facebook_url'];exit;
                       if(isset ($set['enable_facebook'])){
                        if($set['enable_facebook'] == 'Y') {
				if(trim($set['facebook_url'])=='') {
                                $msg = $msg." Facebook url can't be empty<br>";
                                $flag	=	false;
                            }else {
                               if(!$this->validateURL($set['facebook_url'])){
                                    $msg 	= $msg." Invalid Facebook url<br>";
                                    $flag	=	false;
                                }
                            }
			}else {
                            $set['enable_facebook'] = 'N';
                        }
                       }else {
                            $set['enable_facebook'] = 'N';
                        }

                         if(isset ($set['enable_twitter'])){
                        if($set['enable_twitter'] == 'Y') {
				if(trim($set['twitter_url'])=='') {
                                $msg = $msg."Twitter url can't be empty<br>";
                                $flag	=	false;
                            }else {
                              //  if (!eregi("#^http?://.+*", $set['admin_email'])) {
                                if(!$this->validateURL($set['twitter_url'])){
                                    $msg 	= $msg." Invalid Twitter url<br>";
                                    $flag	=	false;
                                }
                            }
			}else {
                            $set['enable_twitter'] = 'N';
                        }
                       }else {
                            $set['enable_twitter'] = 'N';
                        }


                        //stream send settings

                         if(isset ($set['streamsendEnable'])) {
                            $set['streamsendEnable'] = 'Y';
                            if(trim($set['streamsendLoginID'])=='') {
                            	$msg = $msg."Streamsend LoginID can't be empty<br>";
                            	$flag	=	false;
                            }
                            if(trim($set['streamsendKey'])=='') {
                            	$msg = $msg."Streamsend Key can't be empty<br>";
                            	$flag	=	false;
                            }
                            if(trim($set['streamsendListID'])=='') {
                            	$msg = $msg."Streamsend ListID can't be empty<br>";
                            	$flag	=	false;
                            }
                        }else {
                            $set['streamsendEnable'] = 'N';
                        }

			if($flag==true){
				if(!empty($_FILES['site_logo']['tmp_name'])) {
					$ext	=	explode('.',$_FILES['site_logo']['name']);
					$error 	= 	$this->Setting->handleFileUpload($_FILES['site_logo'], 'SiteLogo.'.$ext[1]);
				}
                                $fax_status=$set['allow_fax'];
				foreach($set as $fieldname => $value){
                                   if(($fieldname=='fax_email' || $fieldname=='fax_number') && $fax_status=='N') $value='';
					$this->Setting->update($fieldname,$value);
				}
                                
				$this->Session->setFlash(__('Settings Updated Successfully!', true));
				$data	=	$this->Setting->find('list',array('fields'=>array('Setting.fieldname', 'Setting.value')));

				$this->redirect(array('controller' => 'Settings', 'action' => 'indexm')); 

					exit();
                               
			}else{
				$this->Session->setFlash(__($msg, true));
				$msg='';
				$data	=	$set;
			}
		}else{
			$data	=	$this->Setting->find('list',array('fields'=>array('Setting.fieldname', 'Setting.value'))); 
		}

		$this->set('data', $data);
                global $currency;
                $cnfg 				=	Configure::read('Neutrino');
                $curr = $this->Currency->findByCode($cnfg["currency"]);
                $this->set('curlogo', $curr["Currency"]["logo"]);

		$this->layout = 'adm'; 


    } 


	function ship_setting(){
		 $this->__validateLoginStatus(); 
                 $cnfg 				=	Configure::read('Neutrino');
        $style				=	$cnfg["style"];
        $this->set('style', "admin/".$style);
		 $admsess	=	$this->Session->read('Admin');

		if($admsess["settings"] <> 1){

			$this->redirect(array('controller' => 'admins', 'action' => 'index')); 

			exit();

		}

		 if($_SERVER['REQUEST_METHOD']=='POST'){
	
			$set		=	$_POST;

			$flag		=	true;

			$msg		=	'';

			if(!isset($set["shipping_require"])){

				$set["shipping_require"]	=	'N';

			}

			if(!isset($set["shipping_require_australia"])){

				$set["shipping_require_australia"]	=	'N';

			}

			if(!isset($set["shipping_require_canada"])){

				$set["shipping_require_canada"]	=	'N';

			}

			if(!isset($set["shipping_require_fedex"])){

				$set["shipping_require_fedex"]	=	'N';

			}

			if(!isset($set["shipping_require_usps"])){

				$set["shipping_require_usps"]	=	'N';

			}

			if(!isset($set["foflat_shipping_require"])){

				$set["foflat_shipping_require"]	=	'N';

			}

			if(!isset($set["tflat_shipping_require"])){

				$set["tflat_shipping_require"]	=	'N';

			}

			if(!isset($set["sflat_shipping_require"])){

				$set["sflat_shipping_require"]	=	'N';

			}

			if(!isset($set["fflat_shipping_require"])){

				$set["fflat_shipping_require"]	=	'N';

			}
			
			$set["shipping_origin_country"]	=	$set["data"]["shipping_origin_country"];
			if($set["data"]["shipping_origin_country"]=='US')			
			$set["shipping_origin_state"]	=	$set["shipping_origin_state"];
			else
			$set["shipping_origin_state"]	=	$set["origin_state"];
	
			unset($set["data"]);

			if($set['shipping_origin_country']==''){

				$msg = $msg." Shipping Origin Country can't be empty";

				$flag	=	false;

			}
                        if($set['shipping_origin_city']==''){

				$msg = $msg." Shipping Origin city can't be empty";

				$flag	=	false;

			}

			if($set['shipping_origin_zip']==''){

				$msg = $msg." Shipping Origin Postalcode can't be empty";

				$flag	=	false;

			}

			if($flag==true){

				foreach($set as $fieldname => $value){

					$this->Setting->update($fieldname,$value);

				}

				$this->Session->setFlash(__('Shipping settings Updated Successfully!', true));

			}else{

				$this->Session->setFlash(__($msg, true));

				$msg='';

			}

			
			$this->redirect(array('controller' => 'Settings', 'action' => 'ship_setting')); 
		    exit;
		}

		//$this->redirect(array('controller' => 'settings', 'action' => 'indexm/ship')); 
		//exit;		
		$countrylist	=	$this->Country->find('list',array('fields'=>array('Country.country_iso_code_2','Country.country_name'))); 
		$currencylist	=	$this->Currency->find('list',array('fields'=>array('Currency.code','Currency.name'))); 
		$this->set('countrylist', $countrylist);		
		 //Setting the US States dtails
		$us_cntrydet = $this->Country->find("Country.country_iso_code_2 ='US'");	
		if(isset($us_cntrydet["Country"]["country_id"]))
		{
		 $us_states	=	$this->Statecode->findAll("Statecode.country_id ='".$us_cntrydet["Country"]["country_id"]."'");		
		 $this->set('us_states', $us_states);	
		} 		
		$data	=	$this->Setting->find('list',array('fields'=>array('Setting.fieldname', 'Setting.value'))); 
		$this->set('data', $data);
		$curr			=	$this->Currency->findByCode($data["currency"] );
		$this->set('pcurr', $curr["Currency"]);		
		$this->layout = 'adm'; 
	}



	function email_setting(){
		 $this->__validateLoginStatus(); 
                 $cnfg 				=	Configure::read('Neutrino');
        $style				=	$cnfg["style"];
        $this->set('style', "admin/".$style);
		 $admsess	=	$this->Session->read('Admin');

		if($admsess["settings"] <> 1){

			$this->redirect(array('controller' => 'admins', 'action' => 'index')); 

			exit();

		}

		$data	=	$this->Setting->find('list',array('fields'=>array('Setting.fieldname', 'Setting.value'))); 
		$this->set('data', $data);

		//echopre($_SERVER);

		


		 if($_SERVER['REQUEST_METHOD']=='POST'){


	
			$set		=	$_POST;

			$flag		=	true;

			$msg		=	'';

			

			if($set['smtp_host']==''){

				$msg = $msg." Smtp hostname  can't be empty";

				$flag	=	false;

			}
          if($set['smtp_user']==''){

				$msg = $msg." Smtp username can't be empty";

				$flag	=	false;

			}

			if($set['smtp_password']==''){

				$msg = $msg." Smtp password can't be empty";

				$flag	=	false;

			}

			if($set['smtp_port']==''){

				$msg = $msg." Smtp port can't be empty";

				$flag	=	false;
			}

			if($flag==true){



				foreach($set as $fieldname => $value){

					$this->Setting->update($fieldname,$value);

				}

				$this->Session->setFlash(__('Email  settings Updated Successfully!', true));

			}else{

				$this->Session->setFlash(__($msg, true));

				$msg='';

			}

			
			
			$this->redirect(array('controller' => 'Settings', 'action' => 'email_setting')); 
		    exit;
		}
		
		$data	=	$this->Setting->find('list',array('fields'=>array('Setting.fieldname', 'Setting.value'))); 
		$this->set('data', $data);
			
		$this->layout = 'adm'; 
	}

	function pay_setting(){

		 $this->__validateLoginStatus(); 
                 $cnfg 				=	Configure::read('Neutrino');
        $style				=	$cnfg["style"];
        $this->set('style', "admin/".$style);

		 $admsess	=	$this->Session->read('Admin');

		if($admsess["settings"] <> 1){

			$this->redirect(array('controller' => 'admins', 'action' => 'index')); 

			exit();

		}

 if($_SERVER['REQUEST_METHOD']=='POST'){
		$set		=	$_POST;

		
		if(isset($set["paypal_testmode"])){
		$set["paypal_testmode"]='Y'; }
		else {
		$set["paypal_testmode"]='N'; }		
		if(isset($set["authorize_testmode"])){
		$set["authorize_testmode"]='Y'; }
		else {
		$set["authorize_testmode"]='N'; }
                
                //AK 2 co test
                if(isset($set["twocheckout_testmode"])){
		$set["twocheckout_testmode"]='Y'; }
		else {
		$set["twocheckout_testmode"]='N'; }

                //Palpal pro
                if(isset($set["paypalpro_testmode"])){
		$set["paypalpro_testmode"]='Y'; }
		else {
		$set["paypalpro_testmode"]='N'; }

                 //Palpal flow
                if(isset($set["paypalflow_testmode"])){
		$set["paypalflow_testmode"]='Y'; }
		else {
		$set["paypalflow_testmode"]='N'; }

                //Palpal flow link
                if(isset($set["paypallink_testmode"])){
		$set["paypallink_testmode"]='Y'; }
		else {
		$set["paypallink_testmode"]='N'; }

                //Ogone
                if(isset($set["ogone_testmode"])){
		$set["ogone_testmode"]='Y'; }
		else {
		$set["ogone_testmode"]='N'; }
                
                //paypal express
                if(isset($set["paypal_express_testmode"])){
		$set["paypal_express_testmode"]='Y'; }
		else {
		$set["paypal_express_testmode"]='N'; }


                //paypal express
                if(isset($set["paypal_advancedtestmode"])){
		$set["paypal_advancedtestmode"]='Y'; }
		else {
		$set["paypal_advancedtestmode"]='N'; }

                //Braintree
                if(isset($set["braintree_testmode"])){
		$set["braintree_testmode"]='Y'; }
		else {
		$set["braintree_testmode"]='N'; }
                //Quick book
                if(isset($set["quickbook_testmode"])){
		$set["quickbook_testmode"]='Y'; }
		else {
		$set["quickbook_testmode"]='N'; }

               

		$flag		=	true;

		$pay_check	=	false;

		$auth_check	=	false;

		$your_check	=	false;
                
                $paypal_express_check = false;

		$world_check=	false;
		$blue_check=	false;

		$goog_check	=	false;

                //AK 2 co 
                $twoco_check = false;

                $moneybrokers_check = false;

                $paypalpro_check = false;

                $paypalflow_check = false;

                $paypallink_check = false;

                $paypaladvanced_check = false;

                $ogone_check = false;

                $braintree_check = false;
                $quickbook_check = false;

		$msg		=	'';

		$popt	=	array();

		$i	=	0;

		if(isset($set["pay_paypal"])){

			$popt[$i]	=	$_POST["pay_paypal"];

			$pay_check	=	true;

			$i++;

			unset($set["pay_paypal"]);

		}
                //AK 2 co test
                if(isset($set["pay_twocheckout"])){

			$popt[$i]	=	$_POST["pay_twocheckout"];

			$twoco_check	=	true;

			$i++;

			unset($set["pay_twocheckout"]);

		}
                
                 //AK 2 paypapro  test

                if(isset($set["pay_paypalpro"])){

			$popt[$i]	=	$_POST["pay_paypalpro"];

			$paypalpro_check	=	true;

			$i++;

			unset($set["pay_paypalpro"]);

		}

                //AK 2 payflow  test

                if(isset($set["pay_paypalflow"])){

			$popt[$i]	=	$_POST["pay_paypalflow"];

			$paypalflow_check	=	true;

			$i++;

			unset($set["pay_paypalflow"]);

		}


                //AK 2 payflow link test

                if(isset($set["pay_paypallink"])){

			$popt[$i]	=	$_POST["pay_paypallink"];

			$paypallink_check	=	true;

			$i++;

			unset($set["pay_paypallink"]);

		}

                //AK paypaladvanced link test

                if(isset($set["pay_paypaladvanced"])){

			$popt[$i]	=	$_POST["pay_paypaladvanced"];

			$paypaladvanced_check	=	true;

			$i++;

			unset($set["pay_paypaladvanced"]);

		}


                //AK ogone test

                if(isset($set["pay_ogone"])){

			$popt[$i]	=	$_POST["pay_ogone"];

			$ogone_check	=	true;

			$i++;

			unset($set["pay_ogone"]);

		}

                //AK Braintree


                if(isset($set["pay_braintree"])){

			$popt[$i]	=	$_POST["pay_braintree"];

			$braintree_check	=	true;

			$i++;

			unset($set["pay_braintree"]);

		}

                //AK quickbook
                if(isset($set["quickbook"])){

			$popt[$i]	=	$_POST["quickbook"];

			$quickbook_check	=	true;

			$i++;

			unset($set["pay_twocheckout"]);

		}


                if(isset($set["pay_moneybrokers"])){

			$popt[$i]               =	$_POST["pay_moneybrokers"];

			$moneybrokers_check	=	true;

			$i++;

			unset($set["pay_moneybrokers"]);

		}
                
                if(isset($set["paypal_express"])){

			$popt[$i]	=	$_POST["paypal_express"];

			$paypal_express_check	=	true;

			$i++;

			unset($set["paypal_express"]);

		}


		if(isset($set["ccard"])){

			if($set["ccard"] == "yourpay"){

				$popt[$i]	=	$set["ccard"];

				$your_check	=	true;

				$i++;

				unset($set["ccard"]);

			}elseif($set["ccard"] == "authorize"){

				$popt[$i]	=	$set["ccard"];

				$auth_check	=	true;

				$i++;

				unset($set["ccard"]);

			}elseif($set["ccard"] == "bluepay"){

				$popt[$i]	=	$set["ccard"];

				$blue_check	=	true;

				$i++;

				unset($set["ccard"]);

			}elseif($set["ccard"] == "worldpay"){

				$popt[$i]		=	$set["ccard"];

				$world_check	=	true;

				$i++;

				unset($set["ccard"]);

			}else{

				unset($set["ccard"]);

			}

		}

		

		

		//for offline credit payment

		if(isset($set["offline_creditpay"]))

		{

		  $set["offline_creditpay"]='Y';

		  $popt[$i]	=	$_POST["offline_creditpay"];

		  $i++; }

		else {

		$set["offline_creditpay"]='N';	 }				

		$this->Setting->updateofflinecredit($set["offline_creditpay"]);

		//for offline credit payment

		

		

		if(!empty($popt)){

			$set["payment_method"]	=	implode("-*-",$popt);

		}else{

			$msg 	= " Select atleast one payment method<br>";

			$flag	=	false;

		}
//print_r($set);exit;
		

		$set["currency"]				=	$set["data"]["currency"];

		unset($set["data"]);

		if($pay_check == true){

			if($set['paypal_email']==''){

				$msg .= " Paypal email can't be empty<br>";

				$flag	=	false;

			}else{

				if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $set['paypal_email'])){

					$msg 	= $msg." Invalid Paypal email<br>";

					$flag	=	false;

				

				}

			}

			if($set['paypal_idtoken']==''){

				$msg = $msg." Paypal Paypal Id  Token can't be empty";

				$flag	=	false;

			}

			

		}


                //************ 2 co set data start****
                if($twoco_check == true){

			if($set['twoco_vendorId']==''){

				$msg =" 2co vendorId can't be empty <br>";

				$flag	=	false;

			}


		}

                //***********2 co set data ends****
                 //************ paypalpro set data start****

                if($paypalpro_check == true){

                    if($set['paypalpro_signature']==''){

				$msg = $msg." Paypalpro signature can't be empty !<br>";

				$flag	=	false;

			}

			if($set['paypalpro_username']==''){

				$msg .= " Paypalpro Username can't be empty !<br>";

				$flag	=	false;

			}else{

				if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*.[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $set['paypalpro_username'])){

					$msg 	= $msg." Invalid Paypalpro Username !<br>";

					$flag	=	false;



				}

			}
                        if($set['paypalpro_password']==''){

				$msg = $msg." Paypalpro password can't be empty !<br>";

				$flag	=	false;

			}
			



		}


                //************ paypalflow set data start****

                if($paypalflow_check == true){

                    if($set['paypalflow_partnerid']==''){

				$msg = $msg." Paypalflow Partner ID can't be empty !<br>";

				$flag	=	false;

			}

			if($set['paypalflow_vendorid']==''){

				$msg .= " Paypalflow Vendor ID can't be empty !<br>";

				$flag	=	false;

			}
                        if($set['paypalflow_password']==''){

				$msg = $msg." Paypalflow password  can't be empty !<br>";

				$flag	=	false;

			}




		}

                
                //paypal express
                if($paypal_express_check	== true){

			if($set['paypal_express_username']==''){

				$msg = $msg." PayPal Express Username can't be empty<br>";

				$flag	=	false;

			}

			if($set['paypal_express_password']==''){

				$msg = $msg." PayPal Express Password can't be empty<br>";

				$flag	=	false;

			}
                        
                        if($set['paypal_express_signature']==''){

				$msg = $msg." PayPal Express Signature can't be empty<br>";

				$flag	=	false;

			}

		}



                //************ paypalflow link set data start****

                if($paypallink_check == true){

                    if($set['paypallink_partnerid']==''){

				$msg = $msg." Paypalflowlink Partner ID can't be empty !<br>";

				$flag	=	false;

			}

			if($set['paypallink_vendorid']==''){

				$msg .= " Paypalflowlink Vendor ID can't be empty !<br>";

				$flag	=	false;

			}
                        
		}


                 //************ paypalflow link set data start****

                if($paypallink_check == true){

                    if($set['paypallink_partnerid']==''){

				$msg = $msg." Paypalflowlink Partner ID can't be empty !<br>";

				$flag	=	false;

			}

			if($set['paypallink_vendorid']==''){

				$msg .= " Paypalflowlink Vendor ID can't be empty !<br>";

				$flag	=	false;

			}

		}


                //************ paypaladvanced set data start****

                if($paypaladvanced_check == true){

                    if($set['paypal_advancedPARTNER']==''){

				$msg = $msg." Paypal Advanced Partner ID can't be empty !<br>";

				$flag	=	false;

			}

			if($set['paypal_advancedVENDOR']==''){

				$msg .= " Paypal Advanced Vendor ID can't be empty !<br>";

				$flag	=	false;

			}
                         if($set['paypal_advancedUSER']==''){

				$msg = $msg." Paypal Advanced username can't be empty !<br>";

				$flag	=	false;

			}
                        if($set['paypal_advancedPWD']==''){

				$msg = $msg." Paypal Advanced password  can't be empty !<br>";

				$flag	=	false;

			}




		}


//************ ogone link set data start****

                if($ogone_check == true){

			if($set['ogone_PSPID']==''){

				$msg =" Ogone PSPID can't be empty !<br>";

				$flag	=	false;

			}
                        if($set['ogone_passphrase']==''){

				$msg =" Ogone passphrase can't be empty !<br>";

				$flag	=	false;

			}



		}




                 //************ Braintree set data start****

                if($braintree_check == true){

                    if($set['braintree_merchantId']==''){

				$msg = $msg." Braintree Merchant ID can't be empty !<br>";

				$flag	=	false;

			}

			if($set['braintree_publicKey']==''){

				$msg .= "Braintree Public Key can't be empty !<br>";

				$flag	=	false;

			}
                         if($set['braintree_privateKey']==''){

				$msg = $msg."Braintree Private Key can't be empty !<br>";

				$flag	=	false;

			}
                        

		}


                
                //***********paypalpro set data ends****

                //************ quickbook set data start****

                if($quickbook_check == true){

                    if($set['quickbook_applicationname']==''){

				$msg = $msg." Quickbook Application Name can't be empty !<br>";

				$flag	=	false;

			}

			if($set['quickbook_key']==''){

				$msg .= " Quickbook Key can't be empty !<br>";

				$flag	=	false;

			}
                        
		}

            //************ quickbook set data ends****
                
		if($auth_check == true){

			if($set['authorize_email']==''){

				$msg =" Authorize.Net email can't be empty <br>";

				$flag	=	false;

			}else{

				if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $set['authorize_email'])){

					$msg 	= $msg." Invalid Authorize.Net email !<br>";

					$flag	=	false;

				

				}

			}

			if($set['authorize_loginid']==''){

				$msg = $msg." Authorize.Net Login Id can't be empty<br>";

				$flag	=	false;

			}

			if($set['authorize_transactionkey']==''){

				$msg = $msg." Authorize.Net Transaction Key can't be empty<br>";

				$flag	=	false;

			}

			

			/*if($set['authorize_password']==''){

				$msg = $msg." Authorize.Net Password can't be empty<br>";

				$flag	=	false;

			}*/

			

		}



		if($world_check	==	true){

			if($set['worldpay_inst_Id']==''){

				$msg = $msg." WorldPay Installation Id can't be empty<br>";

				$flag	=	false;

			}

			if($set['worldpay_email']==''){

				$msg = $msg."WorldPay Email can't be empty<br>";

				$flag	=	false;

			}else{

				if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $set['worldpay_email'])){

					$msg 	= $msg." Invalid WorldPay email<br>";

					$flag	=	false;

				

				}

			}

		}

		if($your_check	==	true){

			if($set['yourpay_storeid']==''){

				$msg = $msg." YourPay Store Id can't be empty<br>";

				$flag	=	false;

			}
                        if(!isset ($_FILES["txtYourPayCertificate"]["name"])){
                                $msg = $msg." Upload a valid YourPay certificate<br>";

				$flag	=	false;
                        }else{
                                $set['yourpay_certificate_file'] = $_FILES["txtYourPayCertificate"]["name"];
                                $this->Setting->handleFileUpload($_FILES["txtYourPayCertificate"],$_FILES["txtYourPayCertificate"]["name"],'permiss');
                        }


		}


		if($flag==true){

			foreach($set as $fieldname => $value){

				$this->Setting->update($fieldname,$value);

			}

			$this->Session->setFlash(__('Payment settings Updated Successfully!', true));

		}else{

			$this->Session->setFlash(__($msg, true));

			$msg='';

		}
      }
		//$this->redirect(array('controller' => 'settings', 'action' => 'indexm/pay')); 

		//exit;
		$data	=	$this->Setting->find('list',array('fields'=>array('Setting.fieldname', 'Setting.value'))); 
		$data["pay_check"]	=	"N";
		$data["pay_google"]	=	"N";
		$data['ccard']		=	'';

                //Akhil 
                $data["pay_twocheckout"]	=	"N";
                $data["pay_moneybrokers"]	=	"N";
                $data["paypalpro_check"]	=	"N";
                $data["paypalflow_check"]	=	"N";
                $data["paypallink_check"]	=	"N";
                $data["ogone_check"]            =	"N";
                $data["pay_exp_check"]          =	"N";
                $data["paypaladvanced_check"]	=	"N";
                $data["braintree_check"]	=	"N";
                $data["quickbook_check"]	=	"N";

		if(isset($data["payment_method"])){
			$pck	=	explode("-*-",$data["payment_method"]);
                       
			foreach($pck as $pk){
				if($pk == "paypal"){
					$data["pay_check"]	=	"Y";
				}elseif($pk == "authorize"){
					$data['ccard']		=	'authorize';
				}elseif($pk == "authorize"){
					$data['ccard']		=	'authorize';
				}elseif($pk == "bluepay"){
					$data['ccard']		=	'bluepay';
				}elseif($pk == "worldpay"){
					$data['ccard']		=	'worldpay';
				}elseif($pk == "googlechk"){
					$data["pay_google"]	=	"Y";
				}elseif($pk == "twocheckout"){
                                        $data["pay_twocheckout"]	=	"Y";
				}elseif($pk == "moneybrokers"){
                                        $data["pay_moneybrokers"]	=	"Y";
				}elseif($pk == "paypalpro"){
                                        $data["paypalpro_check"]	=	"Y";
				}elseif($pk == "paypalflow"){
                                        $data["paypalflow_check"]	=	"Y";
				}elseif($pk == "paypallink"){
                                        $data["paypallink_check"]	=	"Y";
				}elseif($pk == "ogone"){
                                        $data["ogone_check"]	=	"Y";
				}elseif($pk == "paypal_express"){
					$data["pay_exp_check"]	=	"Y";
				}elseif($pk == "paypaladvanced"){
					$data["paypaladvanced_check"]	=	"Y";
				}elseif($pk == "braintree"){
					$data["braintree_check"]	=	"Y";
				}elseif($pk == "quickbook"){
					$data["quickbook_check"]	=	"Y";
				}
			}
		}		

		$this->set('data', $data);
		$currencylist	=	$this->Currency->find('list',array('fields'=>array('Currency.code','Currency.name'))); 
		$this->set('currencylist', $currencylist);		
		$this->layout = 'adm'; 
	}

    function beforeFilter()
    {
		if (isset($this->Setting) && !empty($this->Setting->table))  
        {  
             $this->Setting->load();  
        } 
		global $sitefolder; 
		global $meta_description;
		global $meta_keywords;
		global $page_title;
		$cnfg 				=	Configure::read('Neutrino');
		$sitefolder			=	$cnfg["sitebasefolder"];
		$meta_description	=	$cnfg["meta_description"];
		$meta_keywords		=	$cnfg["meta_keywords"];
		$page_title			=	$cnfg["page_title"];
		global $style; 
		$style				=	$cnfg["style"];
		$this->set('style', $style);
		$this->set('meta_description', $meta_description);
		$this->set('meta_keywords', $meta_keywords);
		$this->pageTitle = $page_title	; 
		$this->set('cnfg',$cnfg);		
		$this->set('sitefolder', $sitefolder);
		$httpHost	=	$_SERVER['HTTP_HOST'];
		$this->set('sitebase','http://'.$httpHost);
		
    }

	function beforeRender()
	{
		// $lst	=	$this->Category->findAll();
                  $lst	=	$this->Category->findAll(null, NULL, 'Category.cname ASC', null, null);
		  if(empty($lst)===false)
		 { 
			 foreach($lst as $det)
			 $menu_array[$det["Category"]["id"]] = array('name' => $det["Category"]["cname"],'parent' => $det["Category"]["parent_id"]);
			 $this->set('menu_array', $menu_array);		
		}	 
	}

    function __validateLoginStatus($typ='A')
    {
           if($typ == 'A'){
				if($this->Session->check('Admin') == false)
				{
					$this->redirect(array('controller' => 'admins', 'action' => 'login')); 
					$this->Session->setFlash('The URL you\'ve followed requires you login.');
				}
			}else{
				if($this->Session->check('Userlog') == false)
				{
					$this->redirect(array('controller' => 'users', 'action' => 'login')); 
					$this->Session->setFlash('The URL you\'ve followed requires you login.');
				}
			}
    } 

	#################################### Function to show template preview...............................
	function tempreviu($name){
		$this->set("name",$name);
		$this->layout = 'ajax'; 		
	}
        function validateURL($URL) {
            $pattern_1 = "/(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/";

            if(preg_match($pattern_1, $URL)) {// || preg_match($pattern_2, $URL)){
                return true;
            } else {
                return false;
            }
        }


}

?>