<?php /* EL PSY CONGROO */    	     	 
#注册插件     	    		
RegisterPlugin("AnquanLogin", "ActivePlugin_AnquanLogin");       		 		
    			 	   
if (!class_exists('JWT')) {     				 		
    require dirname(__FILE__) . '/jwt.php';        	  	
}     	  	  	
if (!class_exists('Google2FA')) {      	   	 
    require dirname(__FILE__) . '/goog2fa.php';       	  	 
}    	    	  
     	  			 
$AnquanLogin_ByAqVerify = false;    		 	 			
$AnquanLogin_ExpireDay = 30 * 24;         	  
$AnquanLogin_Agent_Enable = true;    			 		 	
$AnquanLogin_IP_Enable = false;      	  		 
$AnquanLogin_LevelRange = 1;     			 	 	
$AnquanLogin_AppendKey = '';     		   	 
$AnquanLogin_Lb_LevelRange = 1;     	   			
$AnquanLogin_Lb_Enable = false;    			   	 
     		  			
function ActivePlugin_AnquanLogin() {       	 		 
	global $zbp;    			 	 		
	global $AnquanLogin_ExpireDay,$AnquanLogin_Agent_Enable,$AnquanLogin_IP_Enable,$AnquanLogin_LevelRange,$AnquanLogin_AppendKey,$AnquanLogin_Lb_LevelRange,$AnquanLogin_Lb_Enable;    	   		  
     		     
    if($zbp->HasConfig('AnquanLogin') == true){    	  		   
	    $AnquanLogin_ExpireDay    = $zbp->Config('AnquanLogin')->ExpireDay;    	 				  
	    $AnquanLogin_Agent_Enable = $zbp->Config('AnquanLogin')->Agent_Enable;    			 		  
	    $AnquanLogin_IP_Enable    = $zbp->Config('AnquanLogin')->IP_Enable;    			 			 
	    $AnquanLogin_LevelRange   = $zbp->Config('AnquanLogin')->LevelRange;    	 	 				
	    $AnquanLogin_AppendKey    = $zbp->Config('AnquanLogin')->AppendKey;       	  		
        $AnquanLogin_Lb_Enable    = $zbp->Config('AnquanLogin')->Lb_Enable;    		      
        $AnquanLogin_Lb_LevelRange= $zbp->Config('AnquanLogin')->Lb_LevelRange;    	 		  		
	}    	 					 
        				
	if($AnquanLogin_Lb_Enable == true){     			    
		Add_Filter_Plugin('Filter_Plugin_Login_Header', 'AnquanLogin_Login');    								
		Add_Filter_Plugin('Filter_Plugin_Cmd_Begin', 'AnquanLogin_Cmd_Begin');     		  		 
		Add_Filter_Plugin('Filter_Plugin_Member_Edit_Response', 'AnquanLogin_Add_Menu');          		
	}    			     
      	    	
	Add_Filter_Plugin('Filter_Plugin_VerifyLogin_Succeed', 'AnquanLogin_VerifyLogin');    				 	  
	Add_Filter_Plugin('Filter_Plugin_Zbp_Load_Pre', 'AnquanLogin_Zbp_Load_Pre');    		 	  	 
	Add_Filter_Plugin('Filter_Plugin_Zbp_Load', 'AnquanLogin_Del_Old_Password');    			 		 	
}     	 		   
     	 		   
function AnquanLogin_Add_Menu(){     		 				
	global $action,$zbp,$AnquanLogin_Lb_LevelRange,$AnquanLogin_Lb_Enable;      		  		
	if (GetVars('id','GET')==$zbp->user->ID && $zbp->user->ID > 0 && $AnquanLogin_Lb_Enable == true && $zbp->user->Level <= $AnquanLogin_Lb_LevelRange)
	echo '<script type="text/javascript">
	$("div.SubMenu").append("<a href=\"../../zb_users/plugin/AnquanLogin/config.php\"><span class=\"m-left\">Google两步验证</span></a>");
	</script>';	
}    			    	
     			  		
function AnquanLogin_Login() {    						 	
	global $zbp;
	echo '<script type="text/javascript">
$(document).ready(function(){
	$("dd.password").after("<dd class=\"username googleauth\"><label>Google验证</label><input type=\"text\" id=\"googleauth\" name=\"googleauth\" size=\"20\" tabindex=\"3\" /></dd>");
});
	</script>';
}      			  	
    	 	 			 
function AnquanLogin_VerifyLogin($useOriginalPS = false) {          		
    global $zbp,$AnquanLogin_LevelRange,$AnquanLogin_ExpireDay;      	 		  
    	 	  		 
    if ($zbp->user->ID >0 && $zbp->user->Level <= $AnquanLogin_LevelRange){    					 	 
    		    		
        if($useOriginalPS == true)    	  		   
        	$ps = $zbp->user->PassWord_MD5Path;    		     	
        else    	 		 		 
        	$ps = AnquanLogin_GetToken($zbp->user->ID,$zbp->user->Password);    		   		 
        $sdt = time() + 3600 * $AnquanLogin_ExpireDay;    		     	
        if(HTTP_SCHEME == 'https://'){          		
            setcookie("password", $ps, $sdt, $zbp->cookiespath, '', true, true);    			    	
        }else{     				  	
            setcookie("password", $ps, $sdt, $zbp->cookiespath);    	 	     
        }    	     	 
     						 
    }    	 				 	
}    	 	 	   
       		 	 
function AnquanLogin_Cmd_Begin(){    	    	  
	global $action,$zbp,$AnquanLogin_Lb_LevelRange;        	  	
	if($action == 'verify'){    		  	   
		$ga= trim(GetVars('googleauth', 'POST'));    	  		 		
	    $u = trim(GetVars('username', 'POST'));    	 	 		 	
	    $p = trim(GetVars('password', 'POST'));    			 	  	
	    if ($zbp->Verify_MD5(GetVars('username', 'POST'), GetVars('password', 'POST'), $m)) {     	   			
		    if($m->ID > 0 && $m->Metas->LB_enable == true && $m->Level > 0 && $m->Level <= $AnquanLogin_Lb_LevelRange){    	  			 	
				$TimeStamp    = Google2FA::get_timestamp();    		  			 
				$secretkey    = Google2FA::base32_decode($m->Metas->LB_key);    	  	 	  
				$otp          = Google2FA::oath_hotp($secretkey, $TimeStamp);          	 
				$result       = Google2FA::verify_key($m->Metas->LB_key, $ga,4);    	 	   	 
				if($result == true){     		     
					//pass     	  		 	
				}else{     			  		
					$zbp->ShowError('Google两步验证码输入错误', __FILE__, __LINE__);     	 			 	
					die;    	 	 	 		
				}    	  	 		 
			}        	 		
    	  			 	
	    }     	      
	}    					 		
}      						
    	    		 
     					 	
function AnquanLogin_Zbp_Load_Pre() {    	   		  
	global $zbp,$AnquanLogin_ByAqVerify;     				   
    if($zbp->user->ID == 0){      		 			
    	$AnquanLogin_ByAqVerify = AnquanLogin_Verify(trim(GetVars('password', 'COOKIE')));     		 	 	 
    }    	 					 
}    	  		 		
    	  			 	
function AnquanLogin_Verify($token) {    		 	   	
	global $zbp,$AnquanLogin_ExpireDay,$AnquanLogin_LevelRange;    			   	 
    		  	   
    try {    		  	  	
       $a = JWT::decode($token, AnquanLogin_GetKey());    	   			 
    } catch (Exception $e) {     				   
       $a = null;    			     
       return false;    			 			 
    }    	  	 	  
    if(is_object($a)==false)return false;     				  	
    if(property_exists($a,'user_id')!==true)return false;    	       
    $m = $zbp->GetMemberByID($a->user_id);       		 		
    if( $m->ID == 0 )return false;      				  
    if( $m->Level == 0 || $m->Level > $AnquanLogin_LevelRange )return false;     		   		
       		   
    if ($a->user_agent === AnquanLogin_GetUserAgent($m->Password) ){    						  
    	if($a->expire - time() > 0){    	 	   	 
    	    $zbp->user = $m;    		    	 
            return true;     		 	   
        }      	 		  
    }    		    	 
         	  
    return false;    	 	   		
     				 	 
}    				  		
       				 
function AnquanLogin_GetKey(){    			 	 	 
    global $zbp,$AnquanLogin_AppendKey;        			 
    return sha1($zbp->guid . sha1($zbp->path . $AnquanLogin_AppendKey) );    	     	 
}    		     	
      	    	
function AnquanLogin_GetUserAgent($ps){       	   	
    global $zbp,$AnquanLogin_Agent_Enable,$AnquanLogin_IP_Enable;    		   		 
     	  		 	
    $s = $zbp->path . $ps;    	  		   
    if($AnquanLogin_Agent_Enable)    	  	  		
    	$s = $s . GetVars("HTTP_USER_AGENT","SERVER");    	    		 
    if($AnquanLogin_IP_Enable)    						  
    	$s = $s . GetVars("REMOTE_ADDR","SERVER");     	   	  
    return sha1($zbp->guid . sha1 ($s) );     	  	  	
}       		   
    	    			
function AnquanLogin_GetToken($id,$ps){      			 	 
    global $zbp,$AnquanLogin_ExpireDay;     					  
    return JWT::encode(array(      	    	
            'user_id' => (string)$id,       			  
            'expire' => (string)(time() + 3600 * $AnquanLogin_ExpireDay),    		 	 		 
            'user_agent' => AnquanLogin_GetUserAgent($ps),    	   	  	
         ), AnquanLogin_GetKey(), 'HS256');     	 					
}      	  	 	
    			 	  	
function AnquanLogin_Del_Old_Password(){       		 		
    global $zbp,$AnquanLogin_ByAqVerify,$AnquanLogin_LevelRange;     	 					
         	 	
    if(GetVars('install','GET') == 'AnquanLogin')return ;     		   	 
    		 	  		
    if ($zbp->user->ID >0  && $zbp->user->Level > 0 && $zbp->user->Level <= $AnquanLogin_LevelRange && $AnquanLogin_ByAqVerify == false ){    	 	  	  
        if(isset($_COOKIE['password'])){         		 
            $_COOKIE['password'] = '';    	 	 	 		
            if(HTTP_SCHEME == 'https://'){    	  			  
                setcookie("password", '', 0, $zbp->cookiespath, '', true, true);    	 			   
            }else{     		 				
                setcookie("password", '', 0, $zbp->cookiespath);      		    
            }    		  		 	
        }     		 	 	 
        $zbp->user = New Member;     		 	 		
    }     				 		
}    	       
    		 					
function InstallPlugin_AnquanLogin() {    				   	
    global $zbp;    		  	   
    if($zbp->HasConfig('AnquanLogin') == false){      		  		
	    $zbp->Config('AnquanLogin')->ExpireDay = 30 * 24;    	       
	    $zbp->Config('AnquanLogin')->Agent_Enable = true;         		 
	    $zbp->Config('AnquanLogin')->IP_Enable = false;       		  	
	    $zbp->Config('AnquanLogin')->LevelRange = 1;    			 			 
	    $zbp->Config('AnquanLogin')->AppendKey = '';    		 	 			
 	    $zbp->Config('AnquanLogin')->Lb_Enable = 0;      					 
        $zbp->Config('AnquanLogin')->Lb_LevelRange = 5;    		 		  	
	    $zbp->SaveConfig('AnquanLogin');     	 			  
	}      			 		
     	 	 		 
    AnquanLogin_VerifyLogin();      		 			
    Redirect($zbp->host . 'zb_system/cmd.php?act=PluginMng');    		  		 	
}    	     		
    		 	 			
function UninstallPlugin_AnquanLogin() {       	    
    global $zbp;        				
    $zbp->DelConfig('AnquanLogin');     		   		
    AnquanLogin_VerifyLogin(true);    		   	 	
}        		  