| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- <?php
- /**
- * MINZ - Copyright 2011 Marien Fressinaud
- * Sous licence AGPL3 <http://www.gnu.org/licenses/>
- */
- /**
- * La classe Configuration permet de gérer la configuration de l'application
- */
- class Minz_Configuration {
- const CONF_PATH_NAME = '/config.php';
- /**
- * VERSION est la version actuelle de MINZ
- */
- const VERSION = '1.3.1.freshrss'; // version spéciale FreshRSS
- /**
- * valeurs possibles pour l'"environment"
- * SILENT rend l'application muette (pas de log)
- * PRODUCTION est recommandée pour une appli en production
- * (log les erreurs critiques)
- * DEVELOPMENT log toutes les erreurs
- */
- const SILENT = 0;
- const PRODUCTION = 1;
- const DEVELOPMENT = 2;
- /**
- * définition des variables de configuration
- * $salt une chaîne de caractères aléatoires (obligatoire)
- * $environment gère le niveau d'affichage pour log et erreurs
- * $base_url le chemin de base pour accéder à l'application
- * $title le nom de l'application
- * $language la langue par défaut de l'application
- * $db paramètres pour la base de données (tableau)
- * - host le serveur de la base
- * - user nom d'utilisateur
- * - password mot de passe de l'utilisateur
- * - base le nom de la base de données
- */
- private static $salt = '';
- private static $environment = Minz_Configuration::PRODUCTION;
- private static $base_url = '';
- private static $title = '';
- private static $language = 'en';
- private static $default_user = '';
- private static $allow_anonymous = false;
- private static $allow_anonymous_refresh = false;
- private static $auth_type = 'none';
- private static $api_enabled = false;
- private static $unsafe_autologin_enabled = false;
- private static $db = array (
- 'type' => 'mysql',
- 'host' => '',
- 'user' => '',
- 'password' => '',
- 'base' => '',
- 'prefix' => '',
- );
- const MAX_SMALL_INT = 16384;
- private static $limits = array(
- 'cache_duration' => 800, //SimplePie cache duration in seconds
- 'timeout' => 10, //SimplePie timeout in seconds
- 'max_inactivity' => PHP_INT_MAX, //Time in seconds after which a user who has not used the account is considered inactive (no auto-refresh of feeds).
- 'max_feeds' => Minz_Configuration::MAX_SMALL_INT,
- 'max_categories' => Minz_Configuration::MAX_SMALL_INT,
- );
- private static $extensions_enabled = array();
- /*
- * Getteurs
- */
- public static function salt () {
- return self::$salt;
- }
- public static function environment ($str = false) {
- $env = self::$environment;
- if ($str) {
- switch (self::$environment) {
- case self::SILENT:
- $env = 'silent';
- break;
- case self::DEVELOPMENT:
- $env = 'development';
- break;
- case self::PRODUCTION:
- default:
- $env = 'production';
- }
- }
- return $env;
- }
- public static function baseUrl () {
- return self::$base_url;
- }
- public static function title () {
- return self::$title;
- }
- public static function language () {
- return self::$language;
- }
- public static function dataBase () {
- return self::$db;
- }
- public static function limits() {
- return self::$limits;
- }
- public static function defaultUser () {
- return self::$default_user;
- }
- public static function allowAnonymous() {
- return self::$allow_anonymous;
- }
- public static function allowAnonymousRefresh() {
- return self::$allow_anonymous_refresh;
- }
- public static function authType() {
- return self::$auth_type;
- }
- public static function needsLogin() {
- return self::$auth_type !== 'none';
- }
- public static function canLogIn() {
- return self::$auth_type === 'form' || self::$auth_type === 'persona';
- }
- public static function apiEnabled() {
- return self::$api_enabled;
- }
- public static function unsafeAutologinEnabled() {
- return self::$unsafe_autologin_enabled;
- }
- public static function extensionsEnabled() {
- return self::$extensions_enabled;
- }
- public static function _allowAnonymous($allow = false) {
- self::$allow_anonymous = ((bool)$allow) && self::canLogIn();
- }
- public static function _allowAnonymousRefresh($allow = false) {
- self::$allow_anonymous_refresh = ((bool)$allow) && self::allowAnonymous();
- }
- public static function _authType($value) {
- $value = strtolower($value);
- switch ($value) {
- case 'form':
- case 'http_auth':
- case 'persona':
- case 'none':
- self::$auth_type = $value;
- break;
- }
- self::_allowAnonymous(self::$allow_anonymous);
- }
- public static function _enableApi($value = false) {
- self::$api_enabled = (bool)$value;
- }
- public static function _enableAutologin($value = false) {
- self::$unsafe_autologin_enabled = (bool)$value;
- }
- /**
- * Initialise les variables de configuration
- * @exception Minz_FileNotExistException si le CONF_PATH_NAME n'existe pas
- * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
- */
- public static function init () {
- try {
- self::parseFile ();
- self::setReporting ();
- } catch (Minz_FileNotExistException $e) {
- throw $e;
- } catch (Minz_BadConfigurationException $e) {
- throw $e;
- }
- }
- public static function writeFile() {
- $ini_array = array(
- 'general' => array(
- 'environment' => self::environment(true),
- 'salt' => self::$salt,
- 'base_url' => self::$base_url,
- 'title' => self::$title,
- 'default_user' => self::$default_user,
- 'allow_anonymous' => self::$allow_anonymous,
- 'allow_anonymous_refresh' => self::$allow_anonymous_refresh,
- 'auth_type' => self::$auth_type,
- 'api_enabled' => self::$api_enabled,
- 'unsafe_autologin_enabled' => self::$unsafe_autologin_enabled,
- ),
- 'limits' => self::$limits,
- 'db' => self::$db,
- );
- @rename(DATA_PATH . self::CONF_PATH_NAME, DATA_PATH . self::CONF_PATH_NAME . '.bak.php');
- $result = file_put_contents(DATA_PATH . self::CONF_PATH_NAME, "<?php\n return " . var_export($ini_array, true) . ';');
- if (function_exists('opcache_invalidate')) {
- opcache_invalidate(DATA_PATH . self::CONF_PATH_NAME); //Clear PHP 5.5+ cache for include
- }
- return (bool)$result;
- }
- /**
- * Parse un fichier de configuration
- * @exception Minz_PermissionDeniedException si le CONF_PATH_NAME n'est pas accessible
- * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
- */
- private static function parseFile () {
- $ini_array = include(DATA_PATH . self::CONF_PATH_NAME);
- if (!is_array($ini_array)) {
- throw new Minz_PermissionDeniedException (
- DATA_PATH . self::CONF_PATH_NAME,
- Minz_Exception::ERROR
- );
- }
- // [general] est obligatoire
- if (!isset ($ini_array['general'])) {
- throw new Minz_BadConfigurationException (
- '[general]',
- Minz_Exception::ERROR
- );
- }
- $general = $ini_array['general'];
- // salt est obligatoire
- if (!isset ($general['salt'])) {
- if (isset($general['sel_application'])) { //v0.6
- $general['salt'] = $general['sel_application'];
- } else {
- throw new Minz_BadConfigurationException (
- 'salt',
- Minz_Exception::ERROR
- );
- }
- }
- self::$salt = $general['salt'];
- if (isset ($general['environment'])) {
- switch ($general['environment']) {
- case 'silent':
- self::$environment = Minz_Configuration::SILENT;
- break;
- case 'development':
- self::$environment = Minz_Configuration::DEVELOPMENT;
- break;
- case 'production':
- self::$environment = Minz_Configuration::PRODUCTION;
- break;
- default:
- if ($general['environment'] >= 0 &&
- $general['environment'] <= 2) {
- // fallback 0.7-beta
- self::$environment = $general['environment'];
- } else {
- throw new Minz_BadConfigurationException (
- 'environment',
- Minz_Exception::ERROR
- );
- }
- }
- }
- if (isset ($general['base_url'])) {
- self::$base_url = $general['base_url'];
- }
- if (isset ($general['title'])) {
- self::$title = $general['title'];
- }
- if (isset ($general['language'])) {
- self::$language = $general['language'];
- }
- if (isset ($general['default_user'])) {
- self::$default_user = $general['default_user'];
- }
- if (isset ($general['auth_type'])) {
- self::_authType($general['auth_type']);
- }
- if (isset ($general['allow_anonymous'])) {
- self::$allow_anonymous = (
- ((bool)($general['allow_anonymous'])) &&
- ($general['allow_anonymous'] !== 'no')
- );
- }
- if (isset ($general['allow_anonymous_refresh'])) {
- self::$allow_anonymous_refresh = (
- ((bool)($general['allow_anonymous_refresh'])) &&
- ($general['allow_anonymous_refresh'] !== 'no')
- );
- }
- if (isset ($general['api_enabled'])) {
- self::$api_enabled = (
- ((bool)($general['api_enabled'])) &&
- ($general['api_enabled'] !== 'no')
- );
- }
- if (isset ($general['unsafe_autologin_enabled'])) {
- self::$unsafe_autologin_enabled = (
- ((bool)($general['unsafe_autologin_enabled'])) &&
- ($general['unsafe_autologin_enabled'] !== 'no')
- );
- }
- if (isset($ini_array['limits'])) {
- $limits = $ini_array['limits'];
- if (isset($limits['cache_duration'])) {
- $v = intval($limits['cache_duration']);
- if ($v > 0) {
- self::$limits['cache_duration'] = $v;
- }
- }
- if (isset($limits['timeout'])) {
- $v = intval($limits['timeout']);
- if ($v > 0) {
- self::$limits['timeout'] = $v;
- }
- }
- if (isset($limits['max_inactivity'])) {
- $v = intval($limits['max_inactivity']);
- if ($v > 0) {
- self::$limits['max_inactivity'] = $v;
- }
- }
- if (isset($limits['max_feeds'])) {
- $v = intval($limits['max_feeds']);
- if ($v > 0 && $v < Minz_Configuration::MAX_SMALL_INT) {
- self::$limits['max_feeds'] = $v;
- }
- }
- if (isset($limits['max_categories'])) {
- $v = intval($limits['max_categories']);
- if ($v > 0 && $v < Minz_Configuration::MAX_SMALL_INT) {
- self::$limits['max_categories'] = $v;
- }
- }
- }
- // Extensions
- if (isset($ini_array['extensions']) && is_array($ini_array['extensions'])) {
- self::$extensions_enabled = $ini_array['extensions'];
- }
- // Base de données
- if (isset ($ini_array['db'])) {
- $db = $ini_array['db'];
- if (empty($db['type'])) {
- throw new Minz_BadConfigurationException (
- 'type',
- Minz_Exception::ERROR
- );
- }
- switch ($db['type']) {
- case 'mysql':
- if (empty($db['host'])) {
- throw new Minz_BadConfigurationException (
- 'host',
- Minz_Exception::ERROR
- );
- }
- if (empty($db['user'])) {
- throw new Minz_BadConfigurationException (
- 'user',
- Minz_Exception::ERROR
- );
- }
- if (!isset($db['password'])) {
- throw new Minz_BadConfigurationException (
- 'password',
- Minz_Exception::ERROR
- );
- }
- if (empty($db['base'])) {
- throw new Minz_BadConfigurationException (
- 'base',
- Minz_Exception::ERROR
- );
- }
- self::$db['host'] = $db['host'];
- self::$db['user'] = $db['user'];
- self::$db['password'] = $db['password'];
- self::$db['base'] = $db['base'];
- if (isset($db['prefix'])) {
- self::$db['prefix'] = $db['prefix'];
- }
- break;
- case 'sqlite':
- self::$db['host'] = '';
- self::$db['user'] = '';
- self::$db['password'] = '';
- self::$db['base'] = '';
- self::$db['prefix'] = '';
- break;
- default:
- throw new Minz_BadConfigurationException (
- 'type',
- Minz_Exception::ERROR
- );
- break;
- }
- self::$db['type'] = $db['type'];
- }
- }
- private static function setReporting() {
- switch (self::$environment) {
- case self::PRODUCTION:
- error_reporting(E_ALL);
- ini_set('display_errors','Off');
- ini_set('log_errors', 'On');
- break;
- case self::DEVELOPMENT:
- error_reporting(E_ALL);
- ini_set('display_errors','On');
- ini_set('log_errors', 'On');
- break;
- case self::SILENT:
- error_reporting(0);
- break;
- }
- }
- }
|