<?php
//DB Data
require_once("../../../db.php");
require_once("../../../includes/bootstrap_frontend.php");
sessionsClass::site_protection(true,true,true,false);


if($_POST){
    $pass_msg_reg = '';
    $user_msg_reg = '';
    $email_msg_reg = '';
    $captcha_msg_reg = '';
    $pass_alert_reg = '';
    $user_alert_reg = '';
    $email_alert_reg = '';
    $captcha_alert_reg = '';
    $reg_msg='';
	$reg_type = 'INFM';
    
    $valid = '';
	$initialpass = 'partner88';

        $username       = dbase::globalMagic($_POST['user_name']);
        $email          = dbase::globalMagic($_POST['user_email']);
        $password       = dbase::globalMagic(md5($initialpass));
        $password2      = dbase::globalMagic(md5($initialpass));
        $user_role          = dbase::globalMagic($_POST['user_role']);
		
		      
        /* stats data - please note: visitor tracking won't work without js enabled*/
        
        $ipad       = dbase::globalMagic($_SERVER['REMOTE_ADDR']);
                
        $browser    = general::getBrowser();
        $os         = general::getOS();
        
        $lang       = general::getLang();
        $country    = general::getCountry();
        
        $screenres  = general::getScreenRes();
        
        if (isset($_SESSION['refurl'])){
           
            $search_engine_term         =   general::get_search_term_engine(dbase::globalMagic($_SESSION['refurl']));
            
            $searchengine               =   $search_engine_term['searchengine'];
            $searchterm                 =   $search_engine_term['searchterm'];
            
            $addressreferal = $_SESSION['refurl'];
            $domain = $_SESSION['refdomain'];
            
        }
        else{
            $searchengine='none';
            $searchterm='---';
            
            $addressreferal = '';
            $domain = '';
            
        }
        
        //should really be set by now
        if(isset($_SESSION['landingpage'])){
            $landingpage = htmlentities(dbase::globalMagic($_SESSION['landingpage']));
        }
        else{
            $landingpage='none';
        }
        
        
        $group = 4;        
               
        $userNameResponse = validator::usernameValidate($username,0,0);
        
        if ($userNameResponse['Ack']=='fail'){
            $valid = 'fail';
            $user_alert_reg = 'box-shadow: inset 0 0 2px 1px #ff9f9f;';
            $user_msg_reg = $userNameResponse['Msg'];
        }
        
      
        $passwordResponse = validator::passwordValidate($password,$password2,0);
        
        if ($passwordResponse['Ack']=='fail'){
            $valid = 'fail';
            $pass_alert_reg = 'box-shadow: inset 0 0 2px 1px #ff9f9f;';
            $pass_msg_reg = $passwordResponse['Msg'];
        }
        
        $emailResponse = validator::emailValidate($email,0,0);
        
        if ($emailResponse['Ack']=='fail'){
            $valid = 'fail';
            $email_alert_reg = 'box-shadow: inset 0 0 2px 1px #ff9f9f;';
            $email_msg_reg = $emailResponse['Msg'];
      
        }
        else {
            //need to do check against blocks
            //refer needs to be refined so user can select if referal or domain referal
            
            //need to break down the email into domain and referal in to add/domain referal values
            
            //then move whole block in to validation class
            global $conn;
            $email_domain=explode("@",$email);
            $sql_where='';
            //really only the email domain area needs to be in this else statement
          /*
            types:  ip=1
                    refdomain=5
                    referurl=4
                    email:3
                    email domain:2
          
          */
            
            if(trim($addressreferal) !=''){
                $sql_where = " OR ( blockvalue = '$addressreferal' AND type=4 ) ";
            }
            
            if(trim($domain) !=''){
                 $sql_where = " OR ( blockvalue = '$domain' AND type=5 ) ";
            }
            
            if(trim($email_domain[1]!='')){
                $sql_where = " OR ( blockvalue='".$email_domain[1]."' AND type=2 ) ";
            }
           
            $sqlBlock = " SELECT blockid FROM security_blocks WHERE (( blockvalue='$ipad' AND type=1 ) OR ( blockvalue='$email' AND type=3 ) $sql_where ) AND valid=1 AND blockarea IN (1,3) ";
                        
            $dataPostBack =  dbase::globalQueryPlus($sqlBlock,$conn,2);
              
           
            if ($dataPostBack[1]>0){
             
                $valid = 'fail';
           
                $reg_msg = 'could not register your account.';
                
            }
           
        }
        
		//echo 'validacion';
        if ($valid !='fail' && trim($reg_msg)==''){
			
			//echo 'ANTES DE';
            //we can add
            $salt = general::generate_salt();
            $password_hash = general::doubleSalt($password,$salt);
    
            $acti_code = time();
            $valid=1;
            $openid = 'userbase';
            $keys_values = array(
                                "userid"=>"NULL",
                                "username"=>$username,
                                "screenname"=>$username,
                                "p_hash"=>$password_hash,
                                "s_hash"=>$salt,
                                "valid"=>$valid,
								"rol_id"=>$user_role,
								"user_type"=>"1",
                                "acti_code"=>$acti_code,
                                "ipad"=>$ipad,
                                "date_joined"=>"now()",
                                "lastip"=>"no visit",
                                "last_visit"=>"",
                                "email"=>$email,
                                "gravtar_email"=>"",
                                "usergroup"=>$group,
                                "temppass"=>"",
                                "tpdate"=>"",
                                "tpip"=>"",
                                "tp_flag"=>0,
                                "browser"=>$browser,
                                "os"=>$os,
                                "lang"=>$lang,
                                "country"=>$country,
                                "refid"=>dbase::globalMagic($_SESSION['refid']),
                                "refurl"=>dbase::globalMagic($_SESSION['refurl']),
                                "refdomain"=>dbase::globalMagic($_SESSION['refdomain']),
                                "contact"=>$contact,
                                "fname"=>"NULL",
                                "sname"=>"NULL",
                                "mobilenum"=>"N/A",
                                "screenres"=>$screenres,
                                "searchengine"=>$searchengine,
                                "searchterm"=>$searchterm,
                                "smstok"=>"",
                                "smsip"=>"",
                                "smstimedate"=>"",
                                "oneuse"=>"",
                                "landingpage"=>$landingpage,
                                "openidurl"=>$openid,
                                "authentication_source"=>"userbase",
								"img_flag"=>"0",
								"img_url"=>"http://localhost/user/images/no_img.gif",
                             );
			
			if (Admin::addUser($keys_values)){
          				                
				$sqlres = 'SXCS'; //si hubo respuesta y la transaccion se completo
				$typemessage = 'success';
				$titlemessage = 'Felicitaciones'; 
				$resmessage = 'El lider ha sido agregado exitosamente';//contenido del mensaje
              
            }
            else{                
                				
				$sqlres = 'SXCS'; //si hubo respuesta y la transaccion se completo
				$typemessage = 'error';
				$titlemessage = 'Lo Sentimos'; 
				$resmessage = 'Oops, algo dentro de los datos no estaba correcto y el lider no se pudo crear!';//contenido del mensaje
				
            }
            
        }
        else{
            if(trim($reg_msg)==''){
               $reg_msg='please correct the errors and try again.';
			   $reg_type = 'WRNG';
             
            }
        }
        
         //setcaptcha();
    
}else{
    $pass_msg_reg = '';
    $user_msg_reg = '';
    $email_msg_reg = '';
    $captcha_msg_reg = '';
    $pass_alert_reg = '';
    $user_alert_reg = '';
    $email_alert_reg = '';
    $captcha_alert_reg = '';
    $reg_msg='Los datos se perdieron durante la comunicación.';
	$reg_type = 'INFM';   
	//echo 'no registro';

}



header("Location: ../../account-users.php");

?>