Having trouble editing config.xml (/tmp/config.cache problem?)



  • I am using a simple PHP script to try to grab a xml file with captive portal information from a website, but I am having trouble in two senses:

    1. If I try to unlink the /tmp/config.cache file, I get all sorts of unexpected behavior and errors like WARNING: Cannot use scalar value as array, and something about foreach(), and it looks like all my captive portal information is wiped out.
    2. If I don't unlink /tmp/config.cache, it looks like any changes to the config.xml are immediately overwritten, and thus nullified, as if nothing happened.

    The script is as follows:

    #!/usr/local/bin/php
    
    $file = fopen("/conf/config.xml", "r+") or die ("Unable to open /conf/config.xml\r\n");
    $subject = file_get_contents("/conf/config.xml");
    $replacement = file_get_contents("http://www.ourwebsite.net/cp/xml/captiveportal-config-file.xml") or die("Unable to update xml file!\r\n");
    
    if (strlen($replacement) < 1)
    {
       //xml file corrupt or didnt download properly
       die ("XML file corrupt!");
    }
    
    $pattern = '/<captiveportal>/';
    preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
    $offset = $matches[0][1];
    
    if ($offset < 1)
    {
       // <captiveportal>tag not found, something's wrong
       die("Invalid file! ( <captiveportal>tag not found)");
    }
    
    $before = substr($subject, 0, $offset);
    
    $pattern = '/<\/captiveportal>/';
    preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
    $offset = $matches[0][1];
    
    if ($offset < 1)
    {
       // <captiveportal>tag not found, something's wrong
       die("Invalid file! (</captiveportal> tag not found)");
    }
    
    $after = substr($subject, $offset + 16);
    
    $subject = $before . "\r\n" . $replacement . "\r\n" . $after . "\r\n";
    
    //update xml file
    fwrite($file, $subject);
    
    fclose($file);
    
    //delete cache file
    unlink("/tmp/config.cache");
    echo "Deleted /tmp/config.cache\r\n";
    
    echo "Successfully updated /conf/config.xml\r\n";
    
    ?></captiveportal></captiveportal></captiveportal> 
    


  • It seems when I delete the config.cache, it replaces it with one containing something like i:-1;
    From researching around, it seems that I can build an array with parse_config(true) and then overwrite the config.cache file with a serialize()'d config array, however I'm noticing that pfsense's parse_config() function creates an array which somehow loses some of the passthrumac entries, which is not good. Any suggestions on how to fix this behavior?


Log in to reply