Newer
Older
Jaime Perez Crespo
committed
/**
* Configuration of SimpleSAMLphp
Andreas Åkre Solberg
committed
*
Andreas Åkre Solberg
committed
* @author Andreas Aakre Solberg, UNINETT AS. <andreas.solberg@uninett.no>
* @package SimpleSAMLphp
class SimpleSAML_Configuration implements \SimpleSAML\Utils\ClearableState
{
/**
* A default value which means that the given option is required.
*
* @var string
*/
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.
*
* @var string
*/
private $location;
/**
* The file this configuration was loaded from.
*
* @var string|null
*/
private $filename = null;
Jaime Perez Crespo
committed
/**
* 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'
Jaime Perez Crespo
committed
ob_start();
if (interface_exists('Throwable')) {
try {
require($filename);
} catch (ParseError $e) {
self::$loadedConfigs[$filename] = self::loadFromArray(array(), '[ARRAY]', 'simplesaml');
throw new SimpleSAML\Error\ConfigurationError($e->getMessage(), $filename, array());
}
} else {
require($filename);
}
Jaime Perez Crespo
committed
$spurious_output = ob_get_length() > 0;
ob_end_clean();
Jaime Perez Crespo
committed
// check that $config exists
if (!isset($config)) {
throw new \SimpleSAML\Error\ConfigurationError(
'$config is not defined in the configuration file.',
$filename
);
}
// check that $config is initialized to an array
Jaime Perez Crespo
committed
throw new \SimpleSAML\Error\ConfigurationError(
'$config is not an array.',
$filename
);
}
// check that $config is not empty
if (empty($config)) {
throw new \SimpleSAML\Error\ConfigurationError(
'$config is empty.',
$filename
);
// file does not exist, but is required
Jaime Perez Crespo
committed
throw new \SimpleSAML\Error\ConfigurationError('Missing configuration file', $filename);
Jaime Perez Crespo
committed
// file does not exist, but is optional, so return an empty configuration object without saving it
$cfg = new SimpleSAML_Configuration(array(), $filename);
$cfg->filename = $filename;
return $cfg;
}
$cfg = new SimpleSAML_Configuration($config, $filename);
$cfg->filename = $filename;
self::$loadedConfigs[$filename] = $cfg;
Jaime Perez Crespo
committed
if ($spurious_output) {
SimpleSAML\Logger::warning(
"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;
}
/**
* Load a configuration file from a configuration set.
Loading
Loading full blame...