Skip to content
Snippets Groups Projects
Configuration.php 48 KiB
Newer Older
use SimpleSAML\Utils\System;
/**
 * Configuration of SimpleSAMLphp
 * @author Andreas Aakre Solberg, UNINETT AS. <andreas.solberg@uninett.no>
 * @package SimpleSAMLphp
class Configuration implements Utils\ClearableState
{
    /**
     * A default value which means that the given option is required.
     */
    const REQUIRED_OPTION = '___REQUIRED_OPTION___';


    /**
     * Associative array with mappings from instance-names to configuration objects.
     */
    private static $instance = array();


    /**
     * Configuration directories.
     *
     * This associative array contains the mappings from configuration sets to
     * configuration directories.
     */
    private static $configDirs = array();


    /**
     * Cache of loaded configuration files.
     *
     * The index in the array is the full path to the file.
     */
    private static $loadedConfigs = array();


    /**
     * The configuration array.
     */
    private $configuration;


    /**
     * The location which will be given when an error occurs.
     */
    private $location;


    /**
     * The file this configuration was loaded from.
     */
    private $filename = null;


    /**
     * Temporary property that tells if the deprecated getBaseURL() method has been called or not.
     *
     * @var bool
     */
    private $deprecated_base_url_used = false;


    /**
     * Initializes a configuration from the given array.
     *
     * @param array $config The configuration array.
     * @param string $location The location which will be given when an error occurs.
     */
    public function __construct($config, $location)
    {
        assert(is_array($config));
        assert(is_string($location));

        $this->configuration = $config;
        $this->location = $location;
    }

    /**
     * Load the given configuration file.
     *
     * @param string $filename The full path of the configuration file.
     * @param bool $required Whether the file is required.
     * @return \SimpleSAML\Configuration The configuration file. An exception will be thrown if the
     *                                   configuration file is missing.
     * @throws \Exception If the configuration file is invalid or missing.
     */
    private static function loadFromFile($filename, $required)
    {
        assert(is_string($filename));
        assert(is_bool($required));

        if (array_key_exists($filename, self::$loadedConfigs)) {
            return self::$loadedConfigs[$filename];
        }

        if (file_exists($filename)) {
            $config = 'UNINITIALIZED';

            // the file initializes a variable named '$config'
            if (interface_exists('Throwable', false)) {
                try {
                    require($filename);
                } catch (\ParseError $e) {
                    self::$loadedConfigs[$filename] = self::loadFromArray(array(), '[ARRAY]', 'simplesaml');
                    throw new Error\ConfigurationError($e->getMessage(), $filename, array());
                }
            } else {
                require($filename);
            }

            // check that $config exists
            if (!isset($config)) {
                throw new Error\ConfigurationError(
                    '$config is not defined in the configuration file.',
                    $filename
                );
            }

            // check that $config is initialized to an array
            if (!is_array($config)) {
                throw new Error\ConfigurationError(
                    '$config is not an array.',
                    $filename
                );
            }

            // check that $config is not empty
            if (empty($config)) {
                throw new Error\ConfigurationError(
            }
        } elseif ($required) {
            // file does not exist, but is required
            throw new Error\ConfigurationError('Missing configuration file', $filename);
            // file does not exist, but is optional, so return an empty configuration object without saving it
            $cfg = new Configuration(array(), $filename);
        $cfg = new Configuration($config, $filename);
        $cfg->filename = $filename;

        self::$loadedConfigs[$filename] = $cfg;

                "The configuration file '$filename' generates output. Please review your configuration."
            );
        }

        return $cfg;
    }


    /**
     * Set the directory for configuration files for the given configuration set.
     *
     * @param string $path The directory which contains the configuration files.
     * @param string $configSet The configuration set. Defaults to 'simplesaml'.
     */
    public static function setConfigDir($path, $configSet = 'simplesaml')
    {
        assert(is_string($path));
        assert(is_string($configSet));

        self::$configDirs[$configSet] = $path;
    }
Loading
Loading full blame...