<?php
global $_,$conf;
switch($_['action']){
	//Reset des paramètres du thème
	case 'customiser_reset_theme':
		global $myUser,$_,$conf;
		if(!$myUser->can('customiser','configure')) throw new Exception("Permissions insuffisantes",403);
		if(file_exists(__DIR__.SLASH.'css'.SLASH.'theme.css'))
			unlink(__DIR__.SLASH.'css'.SLASH.'theme.css');
		header('location: setting.php?section=global.customiser');
	break;
	
	//Sauvegarde des configurations de customiser
	case 'customiser_setting_save':
		Action::write(function(&$response){
			global $myUser,$_,$conf;
			if(!$myUser->can('customiser','configure')) throw new Exception("Permissions insuffisantes",403);

			$themePath = __DIR__.SLASH.'css'.SLASH.'theme.css';
			if(!file_exists($themePath)) exit('Feuille de thème manquante');
			$themeStream = file_get_contents($themePath);

			foreach ($_['fields'] as $i=>$field) {
				if($field['type']=='image'){
					if(!isset($_FILES['fields']) || !isset($_FILES['fields']['name'][$i])) continue;
					$filename = time().$_FILES['fields']['name'][$i]['value'];
					copy($_FILES['fields']['tmp_name'][$i]['value'],__DIR__.SLASH.'img'.SLASH.$filename);
					$value = "url('../img/$filename')";
				}
				if(isset($field['value']) && !empty($field['value'])) 
					$value = $field['value'];
				
				$themeStream = preg_replace('$(\n'.preg_quote($field['signature']).'[\s\t\r\n]*?\s?\{.*'.$field['instruction'].': )([^;]*)(;.*})$isU','${1}'.$value.'$3', $themeStream );
			}
			file_put_contents(__DIR__.SLASH.'css'.SLASH.'theme.css',$themeStream);


		});
	break;
}
?>