Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up settings in PHP headers #2089

Open
ghormann opened this issue Dec 24, 2024 · 5 comments
Open

Clean up settings in PHP headers #2089

ghormann opened this issue Dec 24, 2024 · 5 comments
Assignees

Comments

@ghormann
Copy link
Member

Currently, every single setting is replated into the header of every single page (including some which might be sensitive). There is probably a better way to handle some of the more sensitive settings.

fpp/www/config.php

Lines 735 to 747 in 99b25c3

<script type="text/javascript">
MINYEAR = <? echo MINYEAR; ?>;
MAXYEAR = <? echo MAXYEAR; ?>;
var settings = new Array();
<?
foreach ($settings as $key => $value) {
if (!is_array($value)) {
printf(" settings['%s'] = \"%s\";\n", $key, $value);
} else {
$js_array = json_encode($value);
printf(" settings['%s'] = %s;\n", $key, $js_array);
}
}

@ghormann ghormann self-assigned this Dec 24, 2024
@OnlineDynamic OnlineDynamic self-assigned this Jan 9, 2025
@OnlineDynamic
Copy link
Collaborator

OnlineDynamic commented Jan 9, 2025

@ghormann I have a solution for this which limits the settings exposed on a page by page basis based on a config json which allows us to define which pages a setting is to be exposed on (and has the option to expose on all pages if required) - tested and working in my dev.... the bit I need to do is go through an identify what actually needs exposing where.... you happy for me to finish and push to master?

@OnlineDynamic
Copy link
Collaborator

This is what my new version looks like:

`
<script type="text/javascript">
// Standard Common JS Variables
MINYEAR = ;
MAXYEAR = ;
var FPP_FULL_VERSION = '';
var FPP_VERSION = '';
var FPP_MAJOR_VERSION = ;
var FPP_MINOR_VERSION = ;

var FPP_PATCH_VERSION = 0;

var FPP_PATCH_VERSION = ;

    var pageName = "<? echo str_ireplace('.php', '', basename($_SERVER['PHP_SELF'])) ?>";

    var helpPage = "<? echo basename($_SERVER['PHP_SELF']) ?>";
    if (pageName == "plugin") {
        var pluginPage = "<? echo preg_replace('/.*page=/', '', $_SERVER['REQUEST_URI']); ?>";
        var pluginBase = "<? echo preg_replace("/^\//", "", preg_replace('/page=.*/', '', $_SERVER['REQUEST_URI'])); ?>";
        helpPage = pluginBase + "nopage=1&page=help/" + pluginPage;
    }
    else {
        helpPage = "help/" + helpPage;
    }


    // Dynamic JS Settings Array with settings which need to be exposed on a page by page basis to browser
    var settings = new Array();
    <?

    function LoadJSExposedSettings()
    {
        global $JSExposedSettings;
        global $JSExposedSettingsLoaded;

        if (!$JSExposedSettingsLoaded) {
            $file = '/opt/fpp/www/JS_exposed_settings.json';

            if (file_exists($file)) {
                $data = json_decode(file_get_contents($file), true);
                $JSExposedSettings = $data["settingNames"];
                $JSExposedSettingsLoaded = 1;
            } else {
                echo "error no JS_exposed_settings.json";
            }
        }
    }
    $JSExposedSettingsLoaded = 0;
    LoadJSExposedSettings();

    foreach ($settings as $key => $value) {
        if (array_key_exists($key, $JSExposedSettings)) {
            if (in_array($pageName, $JSExposedSettings[$key]["exposedToPages"]) || in_array("all", $JSExposedSettings[$key]["exposedToPages"])) {

                //Print out settings that are exposed to the browser
                if (!is_array($value)) {
                    printf("	settings['%s'] = \"%s\";\n", $key, $value);
                } else {
                    $js_array = json_encode($value);
                    printf("    settings['%s'] = %s;\n", $key, $js_array);
                }
            }
        }
    }
    ?>


</script>`

@ghormann ghormann removed their assignment Jan 9, 2025
@ghormann
Copy link
Member Author

ghormann commented Jan 9, 2025

@OnlineDynamic I have no problem if you take this over, but why add the additional configuration file? Should we just use settings.json. You can add a new optional field to specify any restrictions.

@OnlineDynamic
Copy link
Collaborator

@OnlineDynamic I have no problem if you take this over, but why add the additional configuration file? Should we just use settings.json. You can add a new optional field to specify any restrictions.

OK good point- I have add the JSExposed logic to settings.json rather than having a seperate config file

@OnlineDynamic
Copy link
Collaborator

@ghormann you will see I have pushed my initial changes for this into master... next step is to clean up some of the settings which aren't defined in the settings.json file currently... for the mo I have auto included them to stop anything breaking... there is still a chance that a setting that is defined in the json hasn't correct got the page(s) its needed on correctly defined so will need a bit of testing and keep in the back of mind if something JS isn't working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants