<?
Serval::call('tpl')->settings = $config['settings'];
 
$result = array('status'=>'error','msg'=>'Ошибка!');
if(Serval::call('Auth')->ID>0){
	switch($path[2]){
		case 'precheck':
			if( $getProject = Serval::call('Projects')->getProject((int)$_POST['project_id']) ){
				Serval::call('db')->update("mod_projects",array('status'=>'check','attempt_check'=>'0'),"status NOT IN('new','process','waiting') AND id=".(int)$_POST['project_id']);
				$result = array('status'=>'ok');  
			}else{
				$result = array('status'=>'error','msg'=>'Ошибка! Проект не найден!');
			}	
		break;
		case 'verified':
			if( $getProject = Serval::call('Projects')->getProject((int)$_POST['project_id']) ){
				if(is_array($_POST['scan_id']) && count($_POST['scan_id'])>0){
					$scan_ids = array_map("intval",$_POST['scan_id']);
					if( $log = Serval::call('db')->Select("mod_scan","id,status",
						"project_id='{$getProject['id']}' AND is_verified='no' AND status IN('error','warning') AND id IN(".implode(",",$scan_ids).")") ){
					 	$_verified = array();
					 	$_isError = false;
						foreach($log as $v){
						 	$_verified[] = $v['id'];
						 	if($v['status']=="error"){
								$_isError = true; 
							}
						}
						Serval::call('db')->update("mod_scan",array('is_verified'=>'yes'),"project_id='{$getProject['id']}' AND id IN(".implode(",",$_verified).")");
						if($_isError){
							Serval::call('db')->update("mod_projects",array('status'=>'ok','error_msg'=>''),"status='error' AND id=".$getProject['id']);
						}
					}
					$result = array('status'=>'ok','mass'=>true); 
				}else{
					switch($_POST['type']){
						case 'error':
							Serval::call('db')->update("mod_projects",array('status'=>'ok','error_msg'=>''),"status='error' AND id=".$getProject['id']);
							Serval::call('db')->update("mod_scan",array('is_verified'=>'yes'),"project_id='{$getProject['id']}' AND status='error'");
						break;
						case 'warning':
							Serval::call('db')->update("mod_scan",array('is_verified'=>'yes'),"project_id='{$getProject['id']}' AND id=".(int)$_POST['scan_id']);	
						break;
					}
					$result = array('status'=>'ok'); 
				} 
			}else{
				$result = array('status'=>'error','msg'=>'Ошибка! Проект не найден!');
			}
		break;
		case 'loginfo':
			if( $getProject = Serval::call('Projects')->getProject((int)$_GET['project_id']) ){
				Serval::call('tpl')->getProject = $getProject;
				if((int)$_GET['page']>0 && !empty($_GET['status'])){
					switch($_GET['status']){
						case 'delete':
							Serval::call('tpl')->getList['list']['delete'] = Serval::call('Projects')->getLogInfoPage(array(
								'project_id'=>$getProject['id'],
								'scan_id'=>(int)$_GET['scan_id'],
								'status'=>'delete',
								'page'=>$_GET['page'],
							));
							Serval::call('tpl')->getList['project_id'] = $getProject['id'];
							Serval::call('tpl')->getList['scan_id'] = (int)$_GET['scan_id'];
							$result = array('status'=>'ok','msg'=>Serval::call('tpl')->fetch('block/log_del'));
						break;
						case 'new':
							Serval::call('tpl')->getList['list']['new'] = Serval::call('Projects')->getLogInfoPage(array(
								'project_id'=>$getProject['id'],
								'scan_id'=>(int)$_GET['scan_id'],
								'status'=>'new',
								'page'=>$_GET['page'],
							));
							Serval::call('tpl')->getList['project_id'] = $getProject['id'];
							Serval::call('tpl')->getList['scan_id'] = (int)$_GET['scan_id'];
							$result = array('status'=>'ok','msg'=>Serval::call('tpl')->fetch('block/log_new'));
						break;
						case 'change':
							Serval::call('tpl')->getList['list']['change'] = Serval::call('Projects')->getLogInfoPage(array(
								'project_id'=>$getProject['id'],
								'scan_id'=>(int)$_GET['scan_id'],
								'status'=>'change',
								'page'=>$_GET['page'],
							));
							Serval::call('tpl')->getList['project_id'] = $getProject['id'];
							Serval::call('tpl')->getList['scan_id'] = (int)$_GET['scan_id'];
							$result = array('status'=>'ok','msg'=>Serval::call('tpl')->fetch('block/log_change'));
						break;
						
					}
					
				}else{
					Serval::call('tpl')->getList = Serval::call('Projects')->getLogInfo($getProject['id'],(int)$_GET['scan_id']);
					$result = array('status'=>'ok','msg'=>Serval::call('tpl')->fetch('block/log'));
				}
			}else{
				$result = array('status'=>'error','msg'=>'Ошибка! Проект не найден!');
			}
		break;
		case 'delete_project':
			if( $getProject = Serval::call('Projects')->getProject((int)$_GET['id']) ){
        $result = Serval::call('Projects')->deleteProject((int)$_GET['id']);
			}else{
				$result = array('status'=>'error','msg'=>'Ошибка! Проект не найден!');
			}
		break;
		case 'test':

			if( $getProject = Serval::call('Projects')->getProject((int)$_GET['id']) ){

				$res = Serval::call('Check')->run(array(
					'scode'=>md5($getProject['scode'].$config['solt']),
					'protocol'=>$getProject['protocol'],
					'url'=>$getProject['domain'],
					'script'=>$getProject['path'],
					'rootDir'=>$getProject['spath'],
					'crypt_type'=>$getProject['crypt_type'],
					'listDir'=>array(),
					'is_test'=>($_GET['type']=="fast" ? 'yes' : 'no'),
					'ext'=>array_map("trim",explode(",",$getProject['ext'])),
					'exc'=>array_map("trim",explode(",",$getProject['exc'])),
					'excf'=>array_map("trim",explode(",",$getProject['excf'])),
					'timeout'=>($config['settings']['scanner_timeout']>0 ? $config['settings']['scanner_timeout'] : 30),
					'dirMax'=>($getProject['maxdir']>0 ? $getProject['maxdir'] : ($config['settings']['scanner_maxdir']>0 ? $config['settings']['scanner_maxdir'] : 100))
				));
				if($res['status']=="ok"){
					Serval::call('tpl')->result = $res;
					$result = array('status'=>'ok','msg'=>Serval::call('tpl')->fetch('block/test'));
				}else{
					$result = $res;
				}
			}else{
				$result = array('status'=>'error','msg'=>'Ошибка! Проект не найден!');
			}
		break;
		case 'project':        
			if(Serval::call('tpl')->row = Serval::call('Projects')->getProject((int)$_GET['id'])){ 
				Serval::call('tpl')->editProject = true;
				Serval::call('tpl')->display("form/project",false);
				die();
			}else{
				echo implode("<br>",Serval::call('Projects')->error);
				die();
			}                                        
		break;
		case 'save_project': 
			if(Serval::call('Projects')->saveProject($_POST['id'])){
				$result = array('status'=>'ok','msg'=>'Проект успешно сохранен!');
			}else{
				$result = array('status'=>'error','msg'=>implode("<br>",Serval::call('Projects')->error));
			} 
		break;
		case 'profile':
			if(Serval::call('tpl')->row = Serval::call('db')->SelectRecord("mod_users","","id=".Serval::call('db')->quote((int)$_GET['id']) ) ){
				Serval::call('tpl')->display("block/profile",false);
				die();
			} 
		break;
		case 'user':        
			if(Serval::call('tpl')->row = Serval::call('db')->SelectRecord("mod_users","","id=".Serval::call('db')->quote((int)$_GET['id']) ) ){
				Serval::call('tpl')->editUser = true;
				Serval::call('tpl')->display("form/user",false);
				die();
			}                                        
		break;
		case 'save_user':
		if($_POST['id']>0){
		 	if(Serval::call('Auth')->SaveUser()){
			 	$result = array('status'=>'ok','msg'=>'Пользователь успешно создан!');
			}else{
			  $result = array('status'=>'error','msg'=>implode("<br>",Serval::call('Auth')->error));
			}
		}else{
		 	if(Serval::call('Auth')->createUser()){
			 	$result = array('status'=>'ok','msg'=>'Пользователь успешно создан!');
			}else{
			  $result = array('status'=>'error','msg'=>implode("<br>",Serval::call('Auth')->error));
			}
		}
		break;
		case 'delete_user':        
			if(Serval::call('Auth')->deleteUser()){
				$result = array('status'=>'ok','msg'=>'Пользователь успешно создан!');
			}else{
				$result = array('status'=>'error','msg'=>implode("<br>",Serval::call('Auth')->error));
			}                                       
		break;
		case 'password_save':
	 	if(Serval::call('Auth')->changePassword()){
		 	$result = array('status'=>'ok','msg'=>'Пароль изменен!');
		}else{
		  $result = array('status'=>'error','msg'=>implode("<br>",Serval::call('Auth')->error));
		}
		break;
	}
}
echo json_encode($result);
die();
?>