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:

Disadvantages:

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

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

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

    foreach ($vars as $param => $value) {
        list($file_to_process)=explode(";",$value);
    }

    /*
    function magic_quotes_strip($mixed) {
       if(is_array($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);
            clearstatcache();
            if ($mtime == filemtime($filename)) // check if it was modified ever since it was grabbed by client
            {
                $f=fopen($filename,"w");
                fwrite($f,$newdata);
                fclose($f);
                return 1;
            }
            else
            {
                return 0;
            }
        }

        function read_file_and_info($filename) {
            clearstatcache();
            $data["mtime"]=filemtime($filename);          
            $f=fopen($filename,"r");
            $data["contents"]=fread($f,filesize($filename));
            fclose($f);  
            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]))
                continue;
            else if ( $values[$i] == "//" )
                $array_lines .= "//".$variables[$i] ."\n" ;
            else
                $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>";
        else
            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>
    <?php

        list($file_start,$file_array)=explode("array(",$file["contents"]);
        $file_start .= "array(";
        echo "<input type='hidden' name='file_start' value='$file_start'>";
        list($file_array,$file_end)=explode(");",$file_array);
        $file_end = ");".$file_end;
        echo "<input type='hidden' name='file_end' value='$file_end'>";
        $lines=explode("\n",$file_array);
        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>";
            }
            else
            {
                $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>";
                else
                    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


Localization:

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";

becomes:
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
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki