Page 1 sur 1

Ecriture de fichier impossible

Posté : 25 mars 2008, 13:50
par nelumbo
Bonjour :D ,

Dans la partie d'administration de mon site, j'indique dans des champs la configuration et options de fonctionnement du site/script . Ce fichier de configuration set.php génère un fichier avec à l'intérieur la config. Il s'agit du fichier gen_inc.php . Malheureusement, lorsque je clique sur le bouton "update" rien ne s'enregistre ! Pire que ça, le fichier se vide !

Je suis sur un serveur PHP 5, sur PHP 4 il n'y a aucun problème. Le CHMOD est ok .
Fichier set.php
<?

/* Startup settings for each program */

$files["gen_inc.php"] = "General Settings";
$files["pay_inc.php"] = "Payment Settings";
$header_file = "admheader.php";
$footer_file = "admfooter.php";
$current_editing_file = "gen_inc.php";


/* Default variables */
$override= 1;
$error=0;



include ("$header_file");
include ("set_fill.php");


foreach ($files as $key => $value) {
	echo "<a href='?file=$key'><b>$value</b></a>&nbsp;&nbsp;";
	
	if (isset($_REQUEST["file"]))
	{
		if ($_REQUEST["file"] == $key)
		{
			$current_editing_file = $key;	
		}
		
	}
	
}

if (!file_exists($current_editing_file))
{
	//fopen("config/$current_editing_file", "w+");	
}

//echo "<h1>$program_name</h1>";
echo "<h3>".$files[$current_editing_file]."</h3>";




function getItems($aType,$aFieldName,$aFieldValue,$aX,$aY,$default)
{	
	if ($aType == "c")
	{
		$tmp_value = split(",", $GLOBALS["$aFieldName"]);
		$aFieldValue = trim($aFieldValue);
		echo "<input type='checkbox' name='$aFieldName" . "[]" . "' value='$aFieldValue'";
		if (in_array($aFieldValue,$tmp_value))
			echo " checked";
		
		echo ">\n";
	}
	elseif ($aType=="t")
	{
		$aFieldValue = trim($GLOBALS["$aFieldName"]);
		
		if (Empty($aFieldValue))
			$aFieldValue = $default;
		echo "<input type='text' name='$aFieldName' value='$aFieldValue' size='$aX'>\n";				
	}
	elseif ($aType == "r")
	{
		$default = trim($default);
		$va = $GLOBALS["$aFieldName"];
		if (is_array($va))
			$val = trim($va[0]);
		else 
			$val = trim($va);
		$tmp_value = $val;
		$aFieldValue = trim($aFieldValue);
		
		echo "<input type='radio' name='$aFieldName" . "[]" . "' value='$aFieldValue'";
		if ($aFieldValue===$tmp_value)
			echo " checked";
		elseif (!$tmp_value AND $default==$aFieldValue)
				echo " checked";
		echo ">\n";
		
		
	}
	
	elseif ($aType=="ta")
	{
		$aFieldValue = trim($GLOBALS["$aFieldName"]);
		if (Empty($aFieldValue))
			$aFieldValue = $default;
		echo "<textarea name='$aFieldName' cols='$aX' rows='$aY'>$aFieldValue</textarea>\n";				
	}
	
	
}



if (isset($_REQUEST["submit"]))
{
	$str = "<?";
	foreach ($HTTP_POST_VARS as $key => $value) 
	{
		$key = trim($key);
		
		if ($key<>"submit" && $key<>"file")
		{
			if (is_array($value))
			{
				$opt = "";
				foreach ($value as $element)
				{
					$opt = $opt . $element . ",";
				}
				$opt = substr($opt,0,-1);
				$str =  $str . "$". "$key = \"".trim($opt)."\"" . ";\n";
			}
			else 
			{
				$str =  $str . "$". "$key = \"".trim($value)."\"" . ";\n";
			}	
				
		}
	}
	$str = $str . "?>";
	if ($set_demo==1)
	{
		echo "<b>Demo-mode</b><br>System in demo-mode, no configuration saved!<br>";	
	}
	else 
	{
	$r = fopen("config/$current_editing_file","w");
	$write = fwrite($r,$str);
	}

}
require ("config/$current_editing_file");  

echo "<form method='post' action='set.php'>";

if (file_exists("config/$current_editing_file.txt"))
	$options = file("config/$current_editing_file.txt");
else
{ 
	$options = "";
	echo "<p><font color='red'><b>Error:</b></font><br>Question file config/$current_editing_file.txt not found.</p>";	
	$error = 1;
}

if (!$error)
{
	echo "<input type='submit' name='submit' value='Save settings'><br><br>";
}

foreach ($options as $line)
{
	//include "config/gen_inc.php";

	$tmp_split = split("\|",$line);
	$type = $tmp_split[0];	
	$varname = $tmp_split[1];
	$title = $tmp_split[2];
	$question = $tmp_split[3];
	$tmp_options = $tmp_split[4];
	$tmp_options_val = $tmp_split[5];
	$x = $tmp_split[6];
	$y = $tmp_split[7];
	$default = $tmp_split[8];
	$grab_fill_var = $tmp_split[9];
	$options = split(",",$tmp_options);
	$options_val = split(",",$tmp_options_val);
	$num_options = count($options);
	
	echo "<b>$title</b><br>";
	echo $question . "<br>";
	
	$grab_fill_var=trim($grab_fill_var);
	
	if (!empty($grab_fill_var))
	{
		$get_data = call_user_func(trim($grab_fill_var));	
		$options = $get_data;
		$num_options = count($options);
		
	}
	
		
	
	for ($i=0; $i<$num_options; $i++)
 	{
 		if ($grab_fill_var)
 		{
 			$tmp_sp = split(",", $options[$i]);
 			$value = $tmp_sp[0];
 			$descr = $tmp_sp[1];	
 				
 			echo getItems($type,$varname,$value,$x,$y,$default);	
 			echo $descr . "&nbsp;&nbsp;";
 		}
 		else
 		{ 
 			$value = $options_val[$i];
 			echo getItems($type,$varname,$value,$x,$y,$default);	
 			echo $options[$i] . "&nbsp;&nbsp;";
 		}
  	}
	
	 echo "<p>";	
	
}
if (!$error)
{
	echo "<input type='hidden' name='file' value='$current_editing_file'>";
	echo "<input type='submit' name='submit' value='Save settings'>";
}
echo "</form>";

include ("$footer_file");

?>[/code]

Le fichier gen_inc.php vide :
[code]
<? ?>
le fichier gen_inc.php tel qu'il devrait être :
<?
$set_from = "Mon site";
$set_url = "http://www.site.fr";
$set_lang = "eng";
$set_li = "6O7Qo6BU";
$set_full_path = "/var/www/vhosts/si/httptedocs";
$set_from_email = "[email protected]";
$set_smtp = "";
$set_auto_match = "2";
$set_img_validation = "0";
$set_num_list_search = "10";
$set_num_list_profile = "5";

//// et bien d'autres
?>
Merci beaucoup pour votre aide

Posté : 25 mars 2008, 14:54
par Cyrano
Deux choses :
if (isset($_REQUEST["submit"]))
Dangereux ça, parce que dans ce cas, je peux envoyer une requête en mettant "sumbit" en paramètre d'url pour déclencher le système avec http://adresse.de.ton/site?submit=1 par exemple et comme il ny aura aucune donnée $_POST, ça va te vider le fichier.

Second point, en PHP5, $HTTP_POST_VARS n'est plus valide et il faut utiliser $_POST à la place. C'est pour ça que ton fichier est vide, ça ne rentre même pas dans la boucle.

Posté : 25 mars 2008, 19:03
par nelumbo
Super, ça fonctionne :D .
Merciiiiiiiii !
if (isset($_REQUEST["submit"]))
Dangereux ça, parce que dans ce cas, je peux envoyer une requête en mettant "sumbit" en paramètre d'url pour déclencher le système avec http://adresse.de.ton/site?submit=1 par exemple et comme il ny aura aucune donnée $_POST, ça va te vider le fichier.
Même si le dossier est protégé par un htaccess ?

Posté : 25 mars 2008, 19:11
par Cyrano
Absolument puisque l'accès se fait avec un navigateur : $_REQUEST contient les valeurs combinées entre autres de $_POST et de $_GET.

Posté : 25 mars 2008, 20:58
par nelumbo
Comment puis je sécuriser cette partie ?

Posté : 26 mars 2008, 14:19
par Cyrano
Si tu utilises la méthode POST, alors remplace $_REQUEST par $_POST et le problème sera réglé.

Posté : 26 mars 2008, 19:26
par nelumbo
Merci beaucoup :wink: