Steve,
Thanks for the coding tip. I have since built a class that does the above but instead of changing the variables in 1 text area I have each individual variable in a separate field.
I use the HTTP_POST_VARS array to rewrite the code into the file using a for statement.
Here is the class:
<?php
class Setting {
var $hdr = "<?php\n";
var $ftr = " / Do not edit this file. Use the admin to change the settings. /\n ?".">";
function htmlquote($var){
$var = ereg_replace('"','"', $var);
$var = ereg_replace("'","'", $var);
return $var;
}
function store_data(){
global $HTTP_POST_VARS;
if (isset($HTTP_POST_VARS)){
$alen = sizeof($HTTP_POST_VARS);
$data = $this->hdr;
for ($j = 0; $j < $alen; ++$j){
$ckey = key($HTTP_POST_VARS);
$HTTP_POST_VARS[$ckey] = $this->htmlquote(current($HTTP_POST_VARS));
$data .= " $".key($HTTP_POST_VARS)." = \"".current($HTTP_POST_VARS)."\";\n";
next($HTTP_POST_VARS);
}
$data .= $this->ftr;
return $data;
}else{
$e = new Error;
$e->error_number = "POST_VARS_NOT_PRESENT";
$e->error_string = "Post variables were not submitted.";
$e->calling_package = "Setting";
$e->method = "data";
$e->report();
}
}
function get_data($filename){
$fp = fopen($filename, "r");
$fs = filesize($filename);
$content = fread($fp, $fs);
fclose($fp);
// Get rid of first and last php tag ...
$content = substr($content, 5);
$pos = strrpos($content, ";");
$content = substr($content, 0, $pos);
// Do this because semi colon is the separator for explode and it could well be a variable also ...
$content = ereg_replace("\";\"", "\"semicolon\"", $content);
$acontent = explode(";", $content);
$alen = sizeof($acontent);
for ($j = 0; $j < $alen; ++$j){
$str = trim(current($acontent));
$end_pos = strpos($str, "=") - 1;
$length = $end_pos - 1;
$form_var[$j] = substr($str, 1, $length);
next($acontent);
}
return $form_var;
}
function put_data($file){
$file_back = $file.".bak";
@unlink($file_back);
if(!copy($file, $file_back)){
$e = new Error;
$e->error_number = "FILE_NOT_BACKED_UP";
$e->error_string = "File \"$file\" could not be backed up to \"$file_back\".";
$e->calling_package = "Setting";
$e->method = "put_data";
$e->report();
}
if (is_writeable($file)){
$fp = fopen($file, "w");
fwrite($fp, $this->store_data());
fclose($fp);
}else{
$e = new Error;
$e->error_number = "FILE_NOT_WRITABLE";
$e->error_string = "Cannot write to file \"$file\".";
$e->calling_package = "Setting";
$e->method = "put_data";
$e->report();
}
}
} // end class Setting
?>
I use the PHPLIB template class (by Kristian Koehntopp) to generate the form.
Here is the code:
<?php
require("Template.inc");
require("class_setting.php");
include("enable.php");
// Initiate objects ...
$t = new Template($DOCUMENT_ROOT, "keep");
$s = new Setting;
// "act" switch ...
if (isset($act)){
// Update settings file and display result ...
$filename = urldecode($filename);
$s->put_data($filename);
} // isset($act)
// Define variables referencing files ...
$t->set_file(array("b_main_v" => "b_main_v.html", "b_setting_v_main" => "b_setting_v_main.html"));
// b_setting_f_input ...
$filename = "regional.inc";
include($filename);
$aform = $s->get_data($filename);
if (is_array($aform)){
$t->set_file("b_setting_f_input", "b_setting_f_input.html");
$t->set_var("action_variable", $PHP_SELF."?act=0&filename=".urlencode($filename));
$t->set_var($ab_setting_f_input);
$t->set_block("b_setting_f_input", "row_setting", "row");
$alen = sizeof($aform);
for ($j = 0; $j < $alen; ++$j){
// Set up form ...
$t->set_var(array("n_variable" => ucfirst($aform[$j]), "variable" => $aform[$j], "v_variable" => ${$aform[$j]}));
$t->parse("row", "row_setting", true);
}
$t->parse("content_setting", "b_setting_f_input");
}else{
$e = new Error;
$e->error_number = "NO_FORM_VARIABLES";
$e->error_string = "No form variables in the file.";
$e->calling_package = "Setting";
$e->method = "main";
$e->report();
}
// b_setting_v_main ...
$t->set_var("title_setting", "Settings manager");
// b_menu_v ...
include("menu.php");
// b_main_v ...
$t->set_var(array("page_title" => $ammtitle["mmtitle_setting"], "sub_menu" => $sub_menu));
$t->parse("content", array("b_setting_v_main","b_main_v"));
$t->p("content");
?>
The template files are as follows:
b_setting_f_input.html
<!-- Variable list: action_variable BLOCK row_setting(n_variable, variable, v_variable) -->
<form action="{action_variable}" method="post">
<table border="1" width="100%">
<!-- BEGIN row_setting -->
<tr>
<td width="50%" align="right">{n_variable}:</td><td width="50%"><input type="text" name="{variable}" value="{v_variable}"></td>
</tr>
<!-- END row_setting -->
<tr>
<td width="50%" align="right"><input type="submit" value="{submit_button}"></td><td width="50%"><input type="reset" value="{reset_button}"></td>
</tr>
</table>
</form>
b_setting_main.html
<!-- Variable list: title_setting, content_setting -->
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tr>
<td width="100%">{title_setting}</td>
</tr>
<tr>
<td width="100%" align="center">{content_setting}</td>
</tr>
</table>
b_main_v.html
<!-- Variable list: page_title, main_menu, sub_menu, content. -->
<head>
<title>{page_title}</title>
<script src="back_office.js"></script>
<link rel="stylesheet" href="style_ie.css">
</head>
<body>
<table border="1" width="100%" cellspacing="0" cellpadding="2">
<tr>
<td width="100%" align="center">{main_menu}</td>
</tr>
<tr>
<td width="100%">{sub_menu}</td>
</tr>
<tr>
<td width="100%" align="center">{content}</td>
</tr>
</table>
</body>
Once again thanks for your help ...
++Tx
Pete