Created
May 21, 2017 12:12
-
-
Save izanagi1995/e3a140d7a28c3cd3994318fa0ff00e45 to your computer and use it in GitHub Desktop.
useConfig.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $section = ""; | |
| $config = null; | |
| $output = []; | |
| $tableauTraites = []; | |
| function useConfig($file){ | |
| return parse_ini_file($file, true); | |
| } | |
| function redoIni($matches){ | |
| global $config; | |
| global $section; | |
| $key = $matches["key"]; | |
| $new_value = isset($config[$section][$key]) ? $config[$section][$key] : $matches["value"]; | |
| if(!empty($matches["r"])){ | |
| return "$key = \"$new_value\""; | |
| }else{ | |
| return "$key = $new_value"; | |
| } | |
| } | |
| function config2ini($fileName){ | |
| global $config; | |
| global $output; | |
| global $section; | |
| global $tableauTraites; | |
| if(!(file_exists($fileName))){ | |
| die("Ce fichier n'existe pas!"); | |
| } | |
| $lignes = file($fileName); | |
| foreach($lignes as $ligne){ | |
| if(preg_match("/^\[(.*)\]/", $ligne, $matches)){ | |
| $section = substr($matches[0], 1, -1); | |
| $output[] = "[".$section."]\r\n"; | |
| }else{ | |
| if(preg_match("/(.*)\[\] =/", $ligne, $matches2)){ | |
| $key = $matches2[1]; | |
| if(!isset($config[$section][$key])){ | |
| $output[] = $ligne; | |
| }else if(!in_array($key, $tableauTraites)){ | |
| $tableauTraites[] = $key; | |
| foreach($config[$section][$key] as $k => $v) | |
| $output[] = "{$key}[] = \"$v\"\r\n"; | |
| } | |
| }else{ | |
| $s = preg_replace_callback('/^(?<key>.*) = (?<r>"?)(?<value>.*?)(?<l>"?)$/', "redoIni", $ligne); | |
| $output[] = $s; | |
| } | |
| } | |
| } | |
| } | |
| function gereBloc($section, $bloc){ | |
| $min = isset($bloc['min']) ? $bloc['min'] : null; | |
| $max = isset($bloc['max']) ? $bloc['max'] : null; | |
| $pas = isset($bloc['pas']) ? $bloc['pas'] : null; | |
| unset($bloc['min']); | |
| unset($bloc['max']); | |
| unset($bloc['pas']); | |
| $choix = isset($bloc['choix']) ? $bloc['choix'] : []; | |
| unset($bloc['choix']); | |
| $out = []; | |
| foreach($bloc as $key => $value){ | |
| $id = "{$section}_$key"; | |
| $name = "$section"."[".$key."]"; | |
| $out[] = "<label for='$id'>$key</label>"; | |
| switch($key){ | |
| case 'taille': | |
| $input_taille = "<input type='number' id='$id' name='$name' value='$value' required "; | |
| if($min) $input_taille .= "min = $min "; | |
| if($max) $input_taille .= "max = $max "; | |
| if($pas) $input_taille .= "step = $pas"; | |
| $input_taille .= ">"; | |
| $out[] = $input_taille; | |
| break; | |
| case 'type': | |
| $type = "<span class='checkList'>"; | |
| foreach($value as $format){ | |
| $type .= "<input type='checkbox' id='{$id}_{$format}' name='{$section}[choix][]' value='$format'".(in_array($format, $choix)?" checked":"").">"; | |
| $type .= "<label for='{$id}_{$format}'>$format</label>"; | |
| } | |
| $type .= "</span>"; | |
| $out[] = $type; | |
| break; | |
| default: | |
| $out[] = "<input type='text' id='$id' name='$name' value='$value' required>"; | |
| } | |
| $out[] = "<br>"; | |
| } | |
| return $out; | |
| } | |
| function afficheConfig($config){ | |
| $out = []; | |
| $out[] = "<form id='modifConfig' name='modifConfig' method='POST'>"; | |
| foreach($config as $section => $contenu){ | |
| $out[] = "<fieldset>"; | |
| $out[] = "<legend>".$section."</legend>"; | |
| $out = array_merge($out, gereBloc($section, $contenu)); | |
| $out[] = "</fieldset>"; | |
| } | |
| $out[] = "<input type='submit' value='Envoyer'>"; | |
| $out[] = "</form>"; | |
| return implode("\n", $out); | |
| } | |
| if ($_SERVER['REQUEST_METHOD'] == 'POST'){ | |
| global $output; | |
| $config = $_POST; | |
| config2ini("config.ini"); | |
| file_put_contents("config.ini", implode("", $output)); | |
| } | |
| echo afficheConfig(useConfig("config.ini")); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment