Welcome Guest, Not a member yet? Register   Sign In
How can I write file without overwrite
#1

I'm trying to store my settings, but I can't well. I have too much variable.I have settings[variable] input I use this at form inputs.
Like this;

Form1 has these inputs
PHP Code:
<input type="text" class="form-control" name="settings[facebook]">
<
input type="text" class="form-control" name="settings[twitter]">
<
input type="text" class="form-control" name="settings[site_title]">
<
input type="text" class="form-control" name="settings[site_description]">
<
input type="text" class="form-control" name="settings[site_keywords]"

Form2 has these inputs
PHP Code:
<input type="text" class="form-control" name="settings[jumboH1]">
<
input type="text" class="form-control" name="settings[jumboParagraph]">
<
input type="text" class="form-control" name="settings[panel1]">
<
input type="text" class="form-control" name="settings[panel2]">
<
input type="text" class="form-control" name="settings[panel3]"


And i post these with a hidden input at this controller. It is a method:
This method apply settings.php incoming settings[] array but removing all of settings.php and write again.
PHP Code:
public function applysetting()
 
   {
 
       $submit $this->input->post('submit');
 
       if (isset($submit)) {
 
           $html '<?php' PHP_EOL PHP_EOL;
 
           foreach ($this->input->post('settings') as $key => $value) {
 
               $html .= '$settings["' $key '"]="' $value '";' PHP_EOL;
 
           }
 
           file_put_contents(FCPATH '/settings.php'$html);
 
       }
 
   

If i replace file_put_contents to write_file(FCPATH . '/settings.php', $html, 'a') it rewrites the same things. For example

PHP Code:
<?php

$settings
["jumboH1"]="Login h1 test";
$settings["jumboParag"]="Login paragraph test";
$settings["panel1"]="Panel 1";
$settings["panel2"]="Panel 2";
$settings["panel3"]="Panel 3";
<?
php

$settings
["jumboH1"]="input1";
$settings["jumboParag"]="input2";
$settings["panel1"]="input3";
$settings["panel2"]="input4";
$settings["panel3"]="input5";

<?
php

[size=small][font=MonacoConsolasCouriermonospace]$settings["jumboH1"]="input test";[/font][/size]
[
size=small][font=MonacoConsolasCouriermonospace]$settings["jumboParag"]="input test2";[/font][/size]
[
size=small][font=MonacoConsolasCouriermonospace]$settings["panel1"]="input333";[/font][/size]
[
size=small][font=MonacoConsolasCouriermonospace]$settings["panel2"]="input444";[/font][/size]
$settings["panel3"]="input115"
How can i fix it?
Reply
#2

If you're just appending, you need to check if the file exists first, else you'll break it every time. If you want to overwrite it, I suggest unlinking the file. I wrote a couple of examples below that should work, depending on your use-case. That said, I don't recommend anything that potentially writes user inputs directly into a php file.


PHP Code:
<?php

// Overwrite file completely
function createNewSettingsFile()
{
  $file FCPATH '/settings.php';
  $submit $this->input->post('submit');
  if (isset($submit)) {
    $html '<?php'.PHP_EOL.PHP_EOL;
    foreach ($this->input->post('settings') as $key => $value) {
      $html .= '$settings['".$key."'] = "' $value '";' PHP_EOL;
    }

    // Make sure the file doesn't exist to prevent just appending and thus
    // breaking the output of this function.
    if (file_exists($file)) {
      unlink($file);
    }

    file_put_contents($file$html);
  }
}

// Add to existing file, if it exists
function addNewSettings()
{
  $file FCPATH '/settings.php';
  $submit $this->input->post($submit);

  if (isset($submit)) {

    // Check if file exists before adding <?php
    $html = (file_exists($html)) ? '' '<?php'.PHP_EOL.PHP_EOL;

    foreach ($this->input->post('settings') as $key => $value) {
      $html .= '$settings['".$key."'] = "' $value '";' PHP_EOL;
    }

    // Write the contents ( added FILE_APPEND to make sure we are adding
    // and definitely not just creating a new file )
    file_put_contents($file$htmlFILE_APPEND);
  }

Reply
#3

(06-17-2019, 04:36 PM)mladoux Wrote: If you're just appending, you need to check if the file exists first, else you'll break it every time. If you want to overwrite it, I suggest unlinking the file. I wrote a couple of examples below that should work, depending on your use-case. That said, I don't recommend anything that potentially writes user inputs directly into a php file.


PHP Code:
<?php

// Overwrite file completely
function createNewSettingsFile()
{
  $file FCPATH '/settings.php';
  $submit $this->input->post('submit');
  if (isset($submit)) {
    $html '<?php'.PHP_EOL.PHP_EOL;
    foreach ($this->input->post('settings') as $key => $value) {
      $html .= '$settings['".$key."'] = "' $value '";' PHP_EOL;
    }

    // Make sure the file doesn't exist to prevent just appending and thus
    // breaking the output of this function.
    if (file_exists($file)) {
      unlink($file);
    }

    file_put_contents($file$html);
  }
}

// Add to existing file, if it exists
function addNewSettings()
{
  $file FCPATH '/settings.php';
  $submit $this->input->post($submit);

  if (isset($submit)) {

    // Check if file exists before adding <?php
    $html = (file_exists($html)) ? '' '<?php'.PHP_EOL.PHP_EOL;

    foreach ($this->input->post('settings') as $key => $value) {
      $html .= '$settings['".$key."'] = "' $value '";' PHP_EOL;
    }

    // Write the contents ( added FILE_APPEND to make sure we are adding
    // and definitely not just creating a new file )
    file_put_contents($file$htmlFILE_APPEND);
  }


I use your second method function addNewSettings. 

It's good but when I update any setting for example settings.php has 
PHP Code:
$settings["jumboH1"]="Login h1 test";
$settings["jumboParag"]="Login paragraph test";
$settings["panel1"]="Panel 1";
$settings["panel2"]="Panel 2";
$settings["panel3"]="Panel 3"

Then I submit form for update some settings. 

PHP Code:
$settings["jumboH1"]="Login h1 test";
$settings["jumboParag"]="Login paragraph test";
$settings["panel1"]="Panel 1";
$settings["panel2"]="Panel 2";
$settings["panel3"]="Panel 3";
$settings["jumboH1"]="Login h1 testNEW";
$settings["jumboParag"]="Login paragraph testNEW";
$settings["panel1"]="Panel 1NEW";
$settings["panel2"]="Panel 2NEW";
$settings["panel3"]="Panel 3NEW"

It's not updating existing settings, add new lines after the already existing
Reply
#4

Those new settings will overwrite pre-existing values, however, to actually replace the lines is a bit more complex, as I wasn't sure exactly what you were trying to do. If you're trying to add and update, I would basically do an array merge, then completely overwrite the file. See: https://www.php.net/manual/en/function.array-merge.php

Obviously, my solution was just to get you started. Again, I think what you're doing here is very bad practice from a security concept, so I don't feel very comfortable giving a full on solution to someone who probably does not have the grasp of the language necessary to deal with it in a safe fashion. The link I gave you should be enough to give you some ideas, and help you figure out how to do it if you really just want to do this without too much trouble. I would highly suggest you re-think your strategy for storing and updating settings in the long run to a method that isn't directly executable, and can give you some way of filtering to ensure you're not just writing a back door into whatever your project is for some malicious party to take advantage of, as this particular setup would be trivial to compromise in the best of situations.
Reply
#5

I would also note there are performance issues with saving settings that way as well. If you must use a file-based solution, might I suggest something like a sqlite database?
Reply
#6

(06-18-2019, 06:37 PM)mladoux Wrote: I would also note there are performance issues with saving settings that way as well. If you must use a file-based solution, might I suggest something like a sqlite database?

Okay then I will save settings to database. And geting settings i will check and store settings to session. Is it good?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB