1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| <?php
/**
* Created by PhpStorm.
* User: Killian Kaeses
* Date: 02/05/2016
* Time: 16:48
*/
class Config{
public $name;
public $defaultName = "inc/config.ini.php";
public $data;
public $writer = "inc/useConfig.php";
function __construct($nom = ""){
if($nom == "")$this->name = $this->defaultName;
$this->load();
}
function load(){
$this->data=parse_ini_file($this->name, true);
}
public function getData($g, $n){
if(isset($this->data[$g][$n])) return $this->data[$g][$n];
else return "[$g][$n] inconnu";
}
public function getForm(){
if(!isset($this->data)) return('config not loaded');
$data = $this->data;
$out[] = "<form id='conf' name='conf' method=\"post\" action=\"$this->writer\">";
foreach($data as $group=>$property){
if($group!="erreur")$out[]=$this->handleGroup($group, $property);
}
$out[]= "<input id='send' name='send' type=\"submit\" value=\"Envoyer\">";
$out[]= "</form>";
return implode("\n", $out);
}
function handleGroup($g, $p){
$tmp = "<fieldset>
<legend>" . $g . "</legend>";
if(isset($p["comment"])) $tmp .= "<p id=comment>".$p["comment"]."</p>";
foreach($p as $k => $v){
if($k!="comment"&&$k!="type"&&$k!="choix"&&$k!="min"&&$k!="max"){
if(is_numeric($v)) {
$tmp .= "<p><label for='" . $g."[".$k . "]' style='text-transform : capitalize;'>"
. $k . " : </label><input type=number value='"
. $v . "' step='1' id='" . $g."[".$k . "]' name='"
. $g."[".$k . "]'";
if (isset($p["min"])) {
$tmp .= " min='" . $p["min"] . "'";
}
if (isset($p["max"])) {
$tmp .= " max='" . $p["max"] . "'";
}
$tmp .= ">";
if(isset($p["min"])&&isset($p["max"])){
if($p["min"]==$p["max"]){
$tmp .= " (non modifiable) <script>document.getElementById(\"".$g."[".$k."]\").disabled = true;</script>";
}else{
$tmp .= " min($p[min]) max ($p[min]) step(1)";
}
}
$tmp .= "</p>";
}else{
$tmp .= "<p><label for='" . $g."[".$k . "]'>" . $k . " : </label><input type=text value='" .$v . "' id='" . $g."[".$k . "]' name='" . $g."[".$k . "]'></p>";
}
}
}
if (isset($p["type"])&&isset($p["choix"])) {
$tmp .= "<p style='text-transform : capitalize;'>type : ";
foreach ($p["type"] as $v) {
$tmp .= "<input type='checkbox' name='" . $g."[".$v . "]' id='" . $g."[".$v . "]' style='margin-left : 10px;'><label for='" . $v . "'>" . $v . "</label>";
}
$tmp .= "</p>";
foreach ($p["choix"] as $v) {
$tmp .= "<script> document.getElementById('" . $g."[".$v . "]').checked = true; </script>";
}
}
$tmp .= "</fieldset>";
return $tmp;
}
public function writeConfig(){
unlink($this->name);
$monfichier = fopen($this->name, 'a+');
fputs($monfichier, "[erreur]\n");
fputs($monfichier, "interdit = \"<?php echo 'Vous n\\'êtes pas autorisé à voir ce contenu.\"'; exit; ?>\"\n");
fputs($monfichier, "comment = \"; Les commentaires commencent par ';', comme dans php.ini\"\n\n");
foreach($this->data as $k => $v){
$isArrayDone = 0;
if($k != "erreur") {
fputs($monfichier, "[" . $k . "]\n");
foreach ($v as $k2 => $v2) {
if(is_array($v2)){
if($isArrayDone==0) {
foreach ($v2 as $i) {
if (is_numeric($i)) {
fputs($monfichier, $k2 . "[] = $i\n");
} else {
fputs($monfichier, $k2 . "[] = \"$i\"\n");
}
}
foreach ($v2 as $i) {
if (isset($_POST[$k][$i])) {
if (is_numeric($i)) {
fputs($monfichier, "choix[] = $i\n");
} else {
fputs($monfichier, "choix[] = \"$i\"\n");
}
}
}
$isArrayDone = 1;
}
}else if(isset($_POST[$k][$k2])){
if(is_numeric($_POST[$k][$k2])){
fputs($monfichier, $k2 . " = " . $_POST[$k][$k2]."\n");
}else{
fputs($monfichier, $k2 . " = \"" . $_POST[$k][$k2]."\"\n");
}
}else{
if(is_numeric($v2)){
fputs($monfichier, $k2 . " = ".$v2."\n");
}else{
fputs($monfichier, $k2 . " = \"".$v2."\"\n");
}
}
}
fputs($monfichier, "\n");
}
}
fclose($monfichier);
return "Configuration sauvegardée";
}
} |
Partager