Wiki source for EditPhpArrayAction

Show raw source

====An action to edit a php array taken from a php file such as in wikka.config.php====

To use the function you need to call it like { {editphparray folder/file.php} }

The script assumes that the array is one "variable,value" per line with possibly one comment before them.

The characteristics of the script are:
- get the first array in the file and preserves rest of the file even after saving
- creates editable form with all the array "variable,value" pairs
- preserves the contents of the rest of the file whether they are comments or instructions
- it will give a blank entry at the end, for adding extra "comment,variable,value" trio (comment is optional)
- can erase a variable, value pair by simply emptying the variable input box and saving
- can erase a comment by simply emptying the comment input box and saving
- the input box of the value grows according to the data it holds
- checks whether user has admin rights
- can see a file in a folder simply by specifying folder/file path

- not conventional way to pass the file parameter to the script has been followed
- possibly other conventions are not followed

Well here is the script save it in action/editphparray.php

// restric t access to admins
if ($this->IsAdmin($this->GetUser())) {

//{{editphparray wikka.config.php}}

foreach ($vars as $param => $value) {

function magic_quotes_strip($mixed) {
return array_map('magic_quotes_strip', $mixed);
return stripslashes($mixed);

// Is magic quotes on?
//if(get_magic_quotes_gpc() ) {
// $_POST = magic_quotes_strip($_POST);

//for testing
//echo "<PRE>".print_r($_POST)."</PRE>";

//will rewrite only version of file grabbed
function write_file($filename,$newdata,$mtime) {
//echo $mtime ." == ".filemtime($filename);
if ($mtime == filemtime($filename)) // check if it was modified ever since it was grabbed by client
return 1;
return 0;

function read_file_and_info($filename) {
return $data;

if (isset($_POST['submit']) && isset($_POST['filename']) ) {

$variables = $_POST["variable"];
$values = $_POST["value"];
$end = ",";

$array_count = sizeof($variables);
if (empty($variables[$array_count - 2])) //did the user fill in the last comment and variable
$array_count -= 2 ;

for($i=0; $i< $array_count; $i++)
if ( $i == $array_count -1 ) //last array value
$end = "";
if (empty($variables[$i]))
else if ( $values[$i] == "//" )
$array_lines .= "//".$variables[$i] ."\n" ;
$array_lines .= "\t\"".$variables[$i] ."\" => \"". $values[$i] ."\"".$end."\n" ;
$file_contents = $_POST["file_start"]."\n".$array_lines.$_POST["file_end"];

//for testing purposes
// echo "<textarea name='v' rows='42' cols='80'>$file_contents</textarea><HR>";

if ( ! write_file($_POST['filename'],$file_contents,$_POST["mtime"]) )
echo "could not write file please reedit<BR>";
echo $_POST['filename']. " was saved succesfully<BR>";


$file = read_file_and_info($file_to_process);

echo $this->FormOpen();
<input type="hidden" name="filename" value="<?php echo $file_to_process;?>"><BR>
<input type="hidden" name="mtime" value="<?php echo $file["mtime"] ?>"><BR>

$file_start .= "array(";
echo "<input type='hidden' name='file_start' value='$file_start'>";
$file_end = ");".$file_end;
echo "<input type='hidden' name='file_end' value='$file_end'>";
echo "<table class='edit'>\n";
foreach($lines as $line)
echo "<TR>";
list($variable,$value) = explode("=>",$line);
$line = trim($line);
if (substr($line,0,2) == "//") //preserve comments
$line = htmlspecialchars(substr($line,2));
echo "<TD colspan='2'><BR><input size='100' type='text' name='variable[]' value=\"$line\"><input type='hidden' name='value[]' value='//'></TD>";
else if ( empty($value) ) //non array lines preserve
$line = htmlentities($line);
echo "<TD colspan='2'><input type='hidden' name='variable[]' value=\"$line\"><input type='hidden' name='value[]' value='//'></TD>";
$variable = trim($variable);
$variable = trim($variable,"\"");
$value = trim($value);
$value = trim($value,"\",);");
echo "<TD><input size='30' type='text' name='variable[]' value='$variable'> = </TD>";
$len = strlen($value);
if ($len<50)
echo "<TD><input size='70' type='text' name='value[]' value='$value'></TD>";
echo "<TD><textarea name='value[]' rows='".round($len/45)."' cols='52'>$value</textarea></TD>";

echo "</TR>";

echo "<TD colspan='2'><BR><input size='100' type='text' name='variable[]' value=''><input type='hidden' name='value[]' value='//'></TD>";
echo "<TR><TD><input size='30' type='text' name='variable[]' value=''> = </TD><TD><textarea name='value[]' rows='2' cols='52'></textarea></TD></TR>";

echo "</table>\n";

echo "<input type='submit' name='submit' value='save'></form>";


====Other possible uses of the script====


Can be used in editing language files that are defined like the wikka.config.php array.
The discussion about Internationalization is not over yet as of 28 March 2006 so my proposal for localization is as follows:

Call a translation function for each phrase in the wikka
echo "unauthorized usage";
echo trans("UNATHORIZED");

where trans just takes values from an array in en.php
which is defined like this
$translations = array(
"UNATHORIZED" => "unauthorized usage" ,
which is very easily modified with this script, and very easily a translation is made (just make a copy of this and modify the values of its variables)

finally in the header there should be the decision as to what language the user prefers

CategoryDevelopmentActions CategoryUserContributions
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki