0

Hello guys and girls..
I have a problem with the media wiki endgine..
I want to create user groups via the UI, but these groups and permissions are static. (By static I mean that they are written in a php file that puts them into an array.)
The php file is 6000+ lines long.
I'm trying to read it until I reach the comment that I placed (// [Permissions start]), and then append a string after this line.
Sadly, when I open the file with the fopen($filePath, 'a') it doesn't work.. 'a+' Is for appending string in the end of the file, so I don't want to hear anything with it unless if you can thing of a solution with it.

Here is my code..

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form action="" method="post"> 
    <input type="text" name="titleGr" /> <input type="submit" name="submit" value="submit" /><br />
    <input type="checkbox" name="checkGr[0]" value="1" /> per1<br /> <!-- checkboxes will set 1 or 0 -->
    <input type="checkbox" name="checkGr[1]" value="1"/> per2<br />
    </form>
</body>
</html>

<?php
if(isset($_POST['submit'])){

// Packing the temporary array with the permissions
    $wbGroupPer[$_POST['titleGr']] = array('per1_label'=>'0', 'per2_label'=>'0'); // setting false parameters inside the array

    $arrKeys = array_keys($wbGroupPer[$_POST['titleGr']]); // getting the keys in a temporary array

    foreach($_POST['checkGr'] as $key => $permission){ // organizing the temporary group array
        $wbGroupPer[$_POST['titleGr']][$arrKeys[$key]] = $permission; // setting permissions for the group
    }

    print_r($wbGroupPer);




    if(file_exists("D:\\xampp\\htdocs\\samples\\DefaultSettings.php")){
        $fileSize = filesize("D:\\xampp\\htdocs\\samples\\DefaultSettings.php"); // gets the file size in bytes

        $fileHandle = fopen("D:\\xampp\\htdocs\\samples\\DefaultSettings.php", 'r'); // opens the file for reading
        $i = 0;

        $oneLine = fgets($fileHandle);

        while(!feof($fileHandle)){
            echo $i . ' ' . $oneLine . '<br />';
            $i = $i + 1;
            $oneLine = htmlspecialchars(fgets($fileHandle));

            if(stristr($oneLine, '[Permissions start]') === false){
                //File not found
            }
            else{
                fclose($fileHandle);

                $fileHandle = fopen("D:\\xampp\\htdocs\\samples\\DefaultSettings.php", 'a');
                fwrite($fileHandle, '\r\n');

                for($i=0; $i<count($wbGroupPer[$_POST['titleGr']]); $i++){ // loop for setting the string
                    $cond = ($wbGroupPer[$_POST['titleGr']][$arrKeys[$i]]) ? 'true' : 'false'; // setting the boolean values
                    $saveString = '$wbGroupPermissions[' . $_POST['titleGr'] . '][' . $arrKeys[$i] .']=' . $cond; // setting the string for passing to the file
                    //file_put_contents("D:\\xampp\\htdocs\\samples\\DefaultSettings.php", $saveString, FILE_APPEND);
                    fwrite($fileHandle, $saveString);
                }
                break;

                break;
            }
        }
        fclose($fileHandle);
    }
    else{
        echo "File not found!!";
    }
}
?>

I've Uploaded the Default.php file for you..
I would be very happy If you help me out, or give me a good hint atleast.
And sorry about the bad condition of the code. It's just a test for media wiki.

Edited by Nimerion

1
Contributor
1
Reply
13
Views
3 Years
Discussion Span
Last Post by Nimerion
0

Sorry, when I was posting the code I forgot to remove the second break on line 62.. So please ignore that line.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.