<?
if(isset($_GET['export']) && $_GET['export']=="yes"){
	$xml = "<serval>";
		$xml.="<solt>{$config['solt']}</solt>";
		$xml.="<solt_auth>{$config['solt_auth']}</solt_auth>";
		$xml.="<users>";
			if($get = Serval::call('db')->Select("mod_users") ){
				foreach($get  as $val){
					$xml.="<user>";
						$xml.="<id>{$val['id']}</id>";
						$xml.="<fio>{$val['fio']}</fio>";
						$xml.="<email>{$val['email']}</email>";
						$xml.="<password>{$val['password']}</password>";
						$xml.="<is_active>{$val['is_active']}</is_active>";
						$xml.="<type>{$val['type']}</type>";
						$xml.="<date>{$val['date']}</date>";
					$xml.="</user>";
				}
			}
		$xml.="</users>";
		$xml.="<projects>";
			if($get = Serval::call('db')->Select("mod_projects") ){
				foreach($get  as $val){
					$xml.="<project>";
						$xml.="<id>{$val['id']}</id>";
						$xml.="<protocol>{$val['protocol']}</protocol>";
						$xml.="<domain>{$val['domain']}</domain>";
						$xml.="<path>{$val['path']}</path>";
						$xml.="<spath>{$val['spath']}</spath>";
						$xml.="<scode>{$val['scode']}</scode>";
						$xml.="<ext>{$val['ext']}</ext>";
						$xml.="<exc>{$val['exc']}</exc>";
						$xml.="<excf>{$val['excf']}</excf>";
						$xml.="<enc>{$val['enc']}</enc>";
						$xml.="<date>{$val['date']}</date>";
						$xml.="<crypt_type>{$val['crypt_type']}</crypt_type>";
						$xml.="<maxdir>{$val['maxdir']}</maxdir>";
						$xml.="<users_id>{$val['users_id']}</users_id>";
						$xml.="<check_hour>{$val['check_hour']}</check_hour>";
						$xml.="<is_active>{$val['is_active']}</is_active>";
						$xml.="<is_change_email>{$val['is_change_email']}</is_change_email>";
						$xml.="<maxtime>{$val['maxtime']}</maxtime>";
					$xml.="</project>";
				}
			}
				
		$xml.="</projects>";
		$xml.="<projects2users>";
			if($get = Serval::call('db')->Select("mod_projects2users") ){
				foreach($get  as $val){
					$xml.="<item>";
						$xml.="<projects_id>{$val['projects_id']}</projects_id>";
						$xml.="<users_id>{$val['users_id']}</users_id>";
					$xml.="</item>";
				}
			}
		$xml.="</projects2users>";
	$xml.= "</serval>";
	header('Content-Type: application/force-download');
	header('Content-Disposition: inline; filename="serval_export_'.date("Ymd").'.xml"');
	echo $xml;
	die();
}
Serval::call('tpl')->error = false;
if(isset($_POST['import'])){
	if($_FILES['file']['size']>0){
		$ext = mb_strtolower(@end(explode(".",$_FILES['file']['name'])),'utf-8');
		if($ext=="xml"){

			if($xml = @simplexml_load_file($_FILES['file']['tmp_name']) ){
				$solt 		= (string)$xml->solt;
				$solt_auth 	= (string)$xml->solt_auth;
				if(!empty($solt) && !empty($solt_auth)){
					if(is_object($xml->users) && count($xml->users->user)>0){
						$sql = array();
						$i=0;
						foreach($xml->users->user as $item){
							$row = array();
							foreach($item as $field=>$data){
								if($i==0){
									$sql['users']['field'][] = $field;
								}elseif(!in_array($field,$sql['users']['field'])){
									continue;
								}
								$row[] = Serval::call('db')->Quote((string)$data);
							}
							if(count($row)==count($sql['users']['field'])){
								$sql['users']['list'][] = "(".implode(",",$row).")";
							}
							if($i==0){
								$i++;
							}
						}
						
						if(is_object($xml->projects) && count($xml->projects->project)>0){
							$i=0;
							foreach($xml->projects->project as $item){
								$row = array();
								foreach($item as $field=>$data){
									if($i==0){
										$sql['projects']['field'][] = $field;
									}elseif(!in_array($field,$sql['projects']['field'])){
										continue;
									}
									$row[] = Serval::call('db')->Quote((string)$data);
								}
								if(count($row)==count($sql['projects']['field'])){
									$sql['projects']['list'][] = "(".implode(",",$row).")";
								}
								if($i==0){
									$i++;
								}
							}	
						}
						if(is_object($xml->projects2users) && count($xml->projects2users->item)>0){
							$i=0;
							foreach($xml->projects2users->item as $item){
								$row = array();
								foreach($item as $field=>$data){
									if($i==0){
										$sql['projects2users']['field'][] = $field;
									}elseif(!in_array($field,$sql['projects2users']['field'])){
										continue;
									}
									$row[] = Serval::call('db')->Quote((string)$data);
								}
								if(count($row)==count($sql['projects2users']['field'])){
									$sql['projects2users']['list'][] = "(".implode(",",$row).")";
								}
								if($i==0){
									$i++;
								}
							}	
						}
						if(is_array($sql['users']) && count($sql['users'])>0){
							unset($xml);
							/* Записываем в config.php соль*/
							$config = array(
								'db'=>array(
									'host'=>$config['db']['host'],
									'user'=>$config['db']['user'],
									'name'=>$config['db']['name'],
									'password'=>$config['db']['password']
								),
								'root'=>$config['root'],
								'domain'=>$config['domain'],
								'solt'=>$solt,
								'solt_auth'=>$solt_auth,
								'http_dir'=>$config['http_dir'],
							);
							$res = "<?php \nreturn ".var_export($config, true).";\n?>";
							if(file_put_contents($config['root']."/config.php", $res)){
								foreach($sql as $type=>$data){
									switch($type){
										case 'users':
										case 'projects':
										case 'projects2users':
											Serval::call('db')->run("TRUNCATE mod_{$type}");
											Serval::call('db')->run("INSERT IGNORE mod_{$type} (".implode(",",$data['field']).") VALUES ".implode(",",$data['list']).";");
										break;
									}
								}
								Serval::call('db')->run("TRUNCATE mod_scan_dir");
								Serval::call('db')->run("TRUNCATE mod_scan_log");
								Serval::call('db')->run("TRUNCATE mod_scan");
						
								header("location: {$config['http_dir']}");
								die();
							}else{
								Serval::call('tpl')->error = "Не могу изменить в config.php";
							}
						}else{
							Serval::call('tpl')->error = "В файле нет данных о пользователях";
						}						
					}else{
						Serval::call('tpl')->error = "В файле нет данных о пользователях";
					}
				}else{
					Serval::call('tpl')->error = "В файле должна быть соль для кодирования";
				}
			}else{
				Serval::call('tpl')->error = "Не могу прочитать файл";
			}
		}else{
			Serval::call('tpl')->error = "Файл импорта должен быть XML";
		}
	}else{
		Serval::call('tpl')->error = "Укажите файл";
	}
}
if(isset($_POST['save'])){
	if(is_array($_POST['form']) && count($_POST['form'])>0){
		foreach($_POST['form'] as $key=>$val){
			Serval::call('db')->update("mod_settings",array(
				'is_value'=>$val
			),"is_key=".Serval::call('db')->quote($key));
		}                                           
	}
	header("location: ".(isset($config['http_dir']) ? $config['http_dir'] : '/')."settings/?ok=1");
}
Serval::call('tpl')->PAGE['name'] = "Настройка системы";
Serval::call('tpl')->PAGE['meta_title'] = "Настройка системы";
Serval::call('tpl')->settings = $config['settings'];
Serval::call('tpl')->display("settings");
?>