<?php /* EL PSY CONGROO */    		 	 	 	
require dirname(__FILE__) . '/../../../zb_system/function/c_system_base.php';    					 	 
    	  	   	
require dirname(__FILE__) . '/../../../zb_system/function/c_system_admin.php';    	 		 	  
    		 	 	 	
$zbp->Load();    			 			 
    						 	
if ($zbp->user->ID==0) {$zbp->ShowError(6);die();}    			     
       	  	 
if (!$zbp->CheckPlugin('AnquanLogin')) {$zbp->ShowError(48);die();}    			 	 		
if ($zbp->Config('AnquanLogin')->Lb_Enable == false) {$zbp->ShowError(48);die();}     		 	  	
if ($zbp->user->Level > 0 && $zbp->user->Level <= $zbp->Config('AnquanLogin')->Lb_LevelRange){}         		 
else    				   	
	{$zbp->ShowError(48);die();}    	 	  		 
    						 	
    			 	 		
if($_SERVER['REQUEST_METHOD']=='POST'){      	  		 
    								
if(isset($_POST['zc_enable'])){     	  		 	
	if($_POST['zc_enable']==false){      	   		
		$zbp->user->Metas->LB_enable = false;    					   
		$zbp->user->Metas->LB_key    = '';    			 	 	 
		$zbp->user->Save();     		   		
		$zbp->SetHint('good');       	 			
	}else{    		    		
		$_POST['zc_key'] = strip_tags($_POST['zc_key']);     		 			 
		$TimeStamp    = Google2FA::get_timestamp();    	  		 		
		$secretkey    = Google2FA::base32_decode($_POST['zc_key']);    		    	 
		$otp          = Google2FA::oath_hotp($secretkey, $TimeStamp);      		   	
		$result       = Google2FA::verify_key($_POST['zc_key'], $_POST['zc_code'],4);    		      
		if($result == true){    	     		
			$zbp->user->Metas->LB_enable = true;       	 	  
			$zbp->user->Metas->LB_key    = substr( $_POST['zc_key'] , 0, 16);    			   		
			$zbp->user->Save();    	 		 		 
			$zbp->SetHint('good');    	 				 	
		}else{      	  			
			$zbp->SetHint('bad');     	 		  	
		}     	  	  	
	}    				  	 
}    	 	  			
       	  		
    	     		
Redirect('./config.php');    	   	   
      	   		
}       		  	
         	  
$blogtitle='Z-Blog安全登录-Google两步验证';    		 			  
    	 	  			
require $blogpath . 'zb_system/admin/admin_header.php';    				 		 
require $blogpath . 'zb_system/admin/admin_top.php';        	 		
    					   
    		 		 	 
?>
<div id="divMain">
  <div class="divHeader"><?php echo $blogtitle;?></div>
  <div class="SubMenu">
  </div>
  <div id="divMain2">
<div class="divHeader2">个人配置：</div>
<form class="config" id="user" method="post" action="config.php">
<?php
$name=urlencode($zbp->user->Name);       		 	 
$key=sha1( $zbp->user->Guid . time() );     	 	    
$key.=sha1( $zbp->guid . time() );     	  	 		
$key=str_replace(array(0,1,8,9), array('','','',''), $key);    		   	 	
$key=strtoupper(substr( $key,0,16 ));        	 	 
     		    	
if ($zbp->user->Metas->LB_key != ''){    		  		  
  $key=$zbp->user->Metas->LB_key;     	  		  
}    		     	
$enable=(int)$zbp->user->Metas->LB_enable;    	  		 		
     			 		 
$url="qr.php?chl=otpauth://totp/$name%3Fsecret%3D$key";      			 		
      						 
$TimeStamp    = Google2FA::get_timestamp();      		  		
$secretkey    = Google2FA::base32_decode($key);    // Decode it into binary    		  	  	
$otp          = Google2FA::oath_hotp($secretkey, $TimeStamp);      		    
     				 		
?>
<p>
	<span class="title">
		开启 <?php echo $zbp->user->Name ?> 账号的两步验证:</span>
	<input id="zc_enable" name="zc_enable" type="text" value="<?php echo $enable;?>" class="checkbox"/>&nbsp;
<?php
if ($zbp->user->Metas->LB_enable == true)    		 		 		
	echo '(当前状态：两步验证已开启)';    	  	 	  
else    	    	 	
	echo '(当前状态：未开启两步验证)';     	  		 	
 ?>
</p>
<p>
	<span class="title">
		名称:</span>
	<input id="zc_username" class="edit" size="50" readonly="readonly" name="zc_username" type="text" value="<?php echo urldecode($name);?>"/>
</p>
<p>
	<span class="title">
		密钥:</span>
	<input id="zc_key" class="edit" size="50" name="zc_key" readonly="readonly" type="text" value="<?php echo $key;?>"/>
	<br /><br />
拿出你的手机，安装并使用<b>Google身份验证器</b>或是<b>Authy</b>，扫一下二维码，或是手动输入<b>密钥</b>。
	<br />
<img id="ga" src="<?php echo $url;?>"/>
</p>
<p>
	<span class="title">
		输入6位验证码:</span>
	<input id="zc_code" class="edit" size="50" name="zc_code" type="text" value="">
	<br/>（请在2分钟内输入，输入超时或是服务器时间与手机时间相差超过2分钟，有可能验证失败。）
</p>
<p>
<input class="button" style="width:180px;height:38px;" type="submit" value="提交" id="btnPost">
</p>
</form>
	<script type="text/javascript">ActiveLeftMenu("azbpcache");</script>
	<script type="text/javascript">AddHeaderIcon("<?php echo $bloghost . 'zb_users/plugin/AnquanLogin/logo.png';?>");</script>
  </div>
</div>


<?php
require $blogpath . 'zb_system/admin/admin_footer.php';        			 
     			   	
RunTime();    	 	    	
?>