<?
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'),"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){
					if( $log = Serval::call('db')->Select("mod_scan","id,status","project_id='{$getProject['id']}' AND is_verified='no' AND status IN('error','warning')") ){
					 	$_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')->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'])),
					'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();
?>