Airship\Cabin\Bridge\Landing\Cabins::saveSettings PHP Method

saveSettings() protected method

Attempt to save the user-provided cabin configuration
protected saveSettings ( string $cabinName, array $cabins = [], array $post = [] ) : boolean
$cabinName string
$cabins array
$post array
return boolean
    protected function saveSettings(string $cabinName, array $cabins = [], array $post = []) : bool
    {
        $ds = DIRECTORY_SEPARATOR;
        $twigEnv = \Airship\configWriter(ROOT . $ds . 'config' . $ds . 'templates');
        // Content-Security-Policy
        $csp = [];
        foreach ($post['content_security_policy'] as $dir => $rules) {
            if ($dir === 'upgrade-insecure-requests' || $dir === 'inherit') {
                continue;
            }
            if (empty($rules['allow'])) {
                $csp[$dir]['allow'] = [];
            } else {
                $csp[$dir]['allow'] = [];
                foreach ($rules['allow'] as $url) {
                    if (!empty($url) && \is_string($url)) {
                        $csp[$dir]['allow'][] = $url;
                    }
                }
            }
            if (isset($rules['disable-security'])) {
                $csp[$dir]['allow'][] = '*';
            }
            if ($dir === 'script-src') {
                $csp[$dir]['unsafe-inline'] = !empty($rules['unsafe-inline']);
                $csp[$dir]['unsafe-eval'] = !empty($rules['unsafe-eval']);
                $csp[$dir]['self'] = !empty($rules['self']);
            } elseif ($dir === 'style-src') {
                $csp[$dir]['unsafe-inline'] = !empty($rules['unsafe-inline']);
                $csp[$dir]['self'] = !empty($rules['self']);
            } elseif ($dir !== 'plugin-types') {
                $csp[$dir]['self'] = !empty($rules['self']);
                $csp[$dir]['data'] = !empty($rules['data']);
            }
        }
        $csp['inherit'] = !empty($post['content_security_policy']['inherit']);
        $csp['upgrade-insecure-requests'] = !empty($post['content_security_policy']['upgrade-insecure-requests']);
        $saveCabins = [];
        foreach ($cabins as $cab => $cab_data) {
            if ($cab_data['name'] !== $cabinName) {
                // Pass-through
                $saveCabins[$cab] = $cab_data;
            } else {
                $saveCabins[$post['config']['path']] = $post['config'];
                if (isset($cab_data['namespace'])) {
                    // This should be immutable.
                    $saveCabins[$post['config']['path']]['namespace'] = $cab_data['namespace'];
                }
                unset($saveCabins['path']);
            }
        }
        // Save CSP
        \file_put_contents(ROOT . $ds . 'config' . $ds . 'Cabin' . $ds . $cabinName . $ds . 'content_security_policy.json', \json_encode($csp, JSON_PRETTY_PRINT));
        // Configuration
        if (!empty($post['cabin_manage_fallback'])) {
            $config_extra = \json_decode($post['config_extra'], true);
            $twig_vars = \json_decode($post['twig_vars'], true);
        } else {
            $config_extra = $post['config_extra'];
            $twig_vars = $post['twig_vars'];
        }
        if (!empty($config_extra)) {
            \Airship\saveJSON(ROOT . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'Cabin' . DIRECTORY_SEPARATOR . $cabinName . DIRECTORY_SEPARATOR . 'config.json', $config_extra);
        }
        if (!empty($twig_vars)) {
            \Airship\saveJSON(ROOT . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'Cabin' . DIRECTORY_SEPARATOR . $cabinName . DIRECTORY_SEPARATOR . 'twig_vars.json', $twig_vars);
        }
        // Clear the cache
        \unlink(ROOT . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'csp.' . $cabinName . '.json');
        if (\extension_loaded('apcu')) {
            \apcu_clear_cache();
        }
        // Save cabins.json
        \file_put_contents(ROOT . $ds . 'config' . $ds . 'cabins.json', $twigEnv->render('cabins.twig', ['cabins' => $saveCabins]));
        // Delete the cabin cache
        if (\file_exists(ROOT . 'tmp' . $ds . 'cache' . $ds . 'cabin_data.json')) {
            \unlink(ROOT . 'tmp' . $ds . 'cache' . $ds . 'cabin_data.json');
        }
        return true;
    }