This project is making me nuts. I would like to add a function to create a new page (so I don't overwrite the original). Currently, the pagename is defined on
the editor.php page like this
<?php
require('SZConfigFile.class.php');
$SZConfigFile = new SZConfigFile('config.inc.php');
$SZConfigFile->load();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<LINK REL="StyleSheet" HREF="inc/admin.css" type="text/css">
</head>
<body>
<?=$SZConfigFile->display();?>
</body>
</html>
each function has it's own page like this
Config.update.php
<?php
require('SZConfigFile.class.php');
$SZConfigFile = new SZConfigFile('config.inc.php');
$SZConfigFile->save($_POST);
header("Location: config.php");
?>
okay so here is the class file
class SZConfigFile {
var $FILE_ARRAY;
var $FILE_CONTENTS_ARRAY;
var $FILE_LINES;
var $PARSED_ARRAY;
var $File;
var $classname;
var $COMMENT_EDITABLE = false;
var $VARIABLE_EDITABLE = false;
var $VALUE_EDITABLE = true;
function SZConfigFile($file) {
$this->FILE = $file;
}
function load() {
$this->FILE_ARRAY = file($this->FILE);
$this->FILE_LINES = count($this->FILE_ARRAY);
$line = 0;
for($i=0; $i<$this->FILE_LINES; $i++) {
$line_content = $this->FILE_ARRAY[$i];
if (ereg('^[//]', trim($line_content))) {
$this->FILE_CONTENTS_ARRAY[$line] = $line_content;
$this->PARSED_ARRAY[$line][3] = $line_content;
} else if (ereg('^[\$#].+;', $line_content)) {
$this->FILE_CONTENTS_ARRAY[$line] = $line_content;
$equal_pos = strpos($line_content, '=');
$semicolon_pos = strrpos($line_content, ';');
$comment_pos = strpos($line_content, '//', $semicolon_pos);
$value = trim(substr($line_content, $equal_pos+1, $semicolon_pos - $equal_pos - 1));
if (strpos($value, '"') === 0) {
$value = substr($value, 1, strlen($value)-2);
}
if (strpos($value, "'") === 0) {
$value = substr($value, 1, strlen($value)-2);
}
$this->PARSED_ARRAY[$line][0] = trim(substr($line_content, 0, $equal_pos));
$this->PARSED_ARRAY[$line][1] = $value;
if($comment_pos) {
$this->PARSED_ARRAY[$line][2] = trim(substr($line_content, $comment_pos+2, strlen($line_content)-$comment_pos));
}
$line++;
}
}
}
function save($postvars) {
extract($postvars);
$fp = @fopen($this->FILE, 'w') or die("Could not open <b>$this->FILE</b> for writing!");
fwrite($fp, "<?php\n");
$num_vars = count($var_headcmt);
for ($i=0; $i<$num_vars; $i++) {
if (isset($var_headcmt[$i]) && strlen($var_headcmt[$i]) > 0) {
fwrite($fp, $var_headcmt[$i]);
} else if (isset($var_val[$i]) && strlen($var_val[$i]) > 0) {
fwrite($fp, $var_name[$i]."\t= ");
$var_val[$i] = ereg_replace("\r\n", " ", $var_val[$i]);
if(! is_numeric($var_val[$i]) && ereg("^[^\"\']{1,1}|^$", $var_val[$i])) {
fwrite($fp, "'".stripslashes($var_val[$i])."';");
} else {
fwrite($fp, stripslashes($var_val[$i]).";");
}
if (isset($var_cmt[$i])) {
fwrite($fp, "\t// ".str_replace(";", ",", stripslashes($var_cmt[$i])));
}
}
fwrite($fp, "\n");
}
fwrite($fp, "\n?>");
fclose($fp);
}
function _search($needle, $haystack) {
if (!is_array($haystack) || !is_string($needle))
return false;
@reset($haystack);
while (list ($key, $value) = each($haystack)) {
$pos = strpos(trim($value), $needle);
if ($pos > -1 && !is_bool($pos)) {
return $key;
}
}
return false;
}
function remove($variable_name) {
$this->load();
$key = $this->_search($variable_name, $this->FILE_CONTENTS_ARRAY);
if ($key === false) {
return false;
} else {
unset($this->FILE_CONTENTS_ARRAY[$key]);
$this->_updateFile();
}
}
function _updateFile() {
$fp = @fopen($this->FILE, 'w') or die("Could not open <b>$this->FILE</b> for writing!");
fwrite($fp, "<?php\n");
for ($i=0; $i<count($this->FILE_CONTENTS_ARRAY)+1; $i++) {
if (isset($this->FILE_CONTENTS_ARRAY[$i])) {
fwrite($fp, $this->FILE_CONTENTS_ARRAY[$i]);
}
}
fwrite($fp, "\n?>");
fclose($fp);
return true;
}
function add() {
$this->load();
$this->FILE_CONTENTS_ARRAY[count($this->FILE_CONTENTS_ARRAY)] = '$untitled = 0; //';
$this->_updateFile();
return true;
}
function display() {
$num_vars = count($this->PARSED_ARRAY);
$str = '
<form action="config.update.php" method="POST">
<table class="listlines" cellspacing="0" cellpadding="0">
<tr>
<td class="listlines">
<table width="100%" cellspacing="1" cellpadding="0">
<tr>
<td class="mainheader" colspan="4"><a href="config.add.php"><img src="images/icons/add.gif" align="absmiddle" alt="" border="0" align="absmiddle"></a> Website Config </td>
</tr>
<tr>
<td class="listheader" width="1%"> </td>
<td class="listheader">Variable</td>
<td class="listheader">Value</td>
<td class="listheader">Comment</td>
</tr>
';
for ($i=0; $i<$num_vars; $i++) {
if ($this->classname == 'lightbg') {
$this->classname = 'darkbg';
} else {
$this->classname = 'lightbg';
}
$var_orig = $this->PARSED_ARRAY[$i][0];
$var_name = str_replace("_", " ", ucfirst(str_replace("\$", "", $this->PARSED_ARRAY[$i][0])));
$var_val = $this->PARSED_ARRAY[$i][1];
$var_cmt = isset($this->PARSED_ARRAY[$i][2])?$this->PARSED_ARRAY[$i][2]:'';
$bgcolor = ($i % 2) ? '#EEEEEE' : '#DDDDDD';
if (!empty($this->PARSED_ARRAY[$i][3])) {
$head = str_replace("//", "", $this->PARSED_ARRAY[$i][3]);
$str .= '
<input type="hidden" name="var_headcmt[]" value="'.$this->PARSED_ARRAY[$i][3].'">
<input type="hidden" name="var_val[]" value="">
<input type="hidden" name="var_name[]" value="">
<input type="hidden" name="var_cmt[]" value="">
<tr bgcolor="#CCCCCC">
<td colspan="4" class="formheader">'.$head.'</td>
</tr>
'."\n";
}
$str .= '
<tr>
<td class="icon_'.$this->classname.'"><a href="config.remove.php?variable_name='.$var_orig.'"><img src="images/icons/remove.gif" align="absmiddle" alt="" border="0"></a></td>
<td nowrap class="'.$this->classname.'" align="right">
<input type="hidden" name="var_headcmt[]" value="">
'."\n";
if ($this->VARIABLE_EDITABLE || $var_orig == '$untitled') {
$str .= '<input name="var_name[]" value="'.$var_orig.'"></td>'."\n";
} else {
$str .= $var_name.'<input type="hidden" name="var_name[]" value="'.$var_orig.'"></td>'."\n";
}
$str .='<td class="'.$this->classname.'">'."\n";
if ($this->VALUE_EDITABLE) {
if(is_numeric($var_val)) {
$str .= '<input style="width:300px" name="var_val[]" value="'.$var_val.'">'."\n";
} else {
$height = ceil(strlen($var_val) / 45) * 19;
$str .= '<textarea name="var_val[]" style="height: '.$height.'px" wrap="virtual">'.$var_val.'</textarea>'."\n";
}
} else {
$str .= '<input type="Hidden" name="var_val[]" value="'.$var_val.'">'.htmlspecialchars($var_val)."\n";
}
$str .= '
</td>
<td nowrap class="'.$this->classname.'">
'."\n";
if ($this->COMMENT_EDITABLE || $var_cmt == '') {
$str .= '<input style="width:250px" name="var_cmt[]" value="'.$var_cmt.'">'."\n";
} else {
$str .= '<input type="Hidden" name="var_cmt[]" value="'.$var_cmt.'">'.$var_cmt."\n";
}
$str .= '
</td>
</tr>'."\n";
}
$str .= '
<tr>
<td colspan="4" class="listfooter" align="right"><button class="cancel" type="button" onclick="history.go(-1);">cancel</button> <button class="save" type="submit" name="form_submit">save</button></td>
</tr>
</table>
</form>
'."\n";
return $str;
}
}
?>
basically, I want to be able to load the include file but create/save it with a new name that I input from a form. I have read/tried everything I could think of and I suck at figuring this out.