Configuration.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * MINZ - Copyright 2011 Marien Fressinaud
  4. * Sous licence AGPL3 <http://www.gnu.org/licenses/>
  5. */
  6. /**
  7. * La classe Configuration permet de gérer la configuration de l'application
  8. */
  9. class Minz_Configuration {
  10. const CONF_PATH_NAME = '/config.php';
  11. /**
  12. * VERSION est la version actuelle de MINZ
  13. */
  14. const VERSION = '1.3.1.freshrss'; // version spéciale FreshRSS
  15. /**
  16. * valeurs possibles pour l'"environment"
  17. * SILENT rend l'application muette (pas de log)
  18. * PRODUCTION est recommandée pour une appli en production
  19. * (log les erreurs critiques)
  20. * DEVELOPMENT log toutes les erreurs
  21. */
  22. const SILENT = 0;
  23. const PRODUCTION = 1;
  24. const DEVELOPMENT = 2;
  25. /**
  26. * définition des variables de configuration
  27. * $sel_application une chaîne de caractères aléatoires (obligatoire)
  28. * $environment gère le niveau d'affichage pour log et erreurs
  29. * $use_url_rewriting indique si on utilise l'url_rewriting
  30. * $base_url le chemin de base pour accéder à l'application
  31. * $title le nom de l'application
  32. * $language la langue par défaut de l'application
  33. * $cacheEnabled permet de savoir si le cache doit être activé
  34. * $delayCache la limite de cache
  35. * $db paramètres pour la base de données (tableau)
  36. * - host le serveur de la base
  37. * - user nom d'utilisateur
  38. * - password mot de passe de l'utilisateur
  39. * - base le nom de la base de données
  40. */
  41. private static $sel_application = '';
  42. private static $environment = Minz_Configuration::PRODUCTION;
  43. private static $base_url = '';
  44. private static $use_url_rewriting = false;
  45. private static $title = '';
  46. private static $language = 'en';
  47. private static $cache_enabled = false;
  48. private static $delay_cache = 3600;
  49. private static $default_user = '';
  50. private static $current_user = '';
  51. private static $allow_anonymous = false;
  52. private static $auth_type = 'none';
  53. private static $db = array (
  54. 'host' => false,
  55. 'user' => false,
  56. 'password' => false,
  57. 'base' => false
  58. );
  59. /*
  60. * Getteurs
  61. */
  62. public static function salt () {
  63. return self::$sel_application;
  64. }
  65. public static function environment () {
  66. return self::$environment;
  67. }
  68. public static function baseUrl () {
  69. return self::$base_url;
  70. }
  71. public static function useUrlRewriting () {
  72. return self::$use_url_rewriting;
  73. }
  74. public static function title () {
  75. return self::$title;
  76. }
  77. public static function language () {
  78. return self::$language;
  79. }
  80. public static function cacheEnabled () {
  81. return self::$cache_enabled;
  82. }
  83. public static function delayCache () {
  84. return self::$delay_cache;
  85. }
  86. public static function dataBase () {
  87. return self::$db;
  88. }
  89. public static function defaultUser () {
  90. return self::$default_user;
  91. }
  92. public static function currentUser () {
  93. return self::$current_user;
  94. }
  95. public static function isAdmin () {
  96. return self::$current_user === self::$default_user;
  97. }
  98. public static function allowAnonymous() {
  99. return self::$allow_anonymous;
  100. }
  101. public static function authType() {
  102. return self::$auth_type;
  103. }
  104. public static function _allowAnonymous($allow = false) {
  105. self::$allow_anonymous = (bool)$allow;
  106. }
  107. public static function _authType($value) {
  108. $value = strtolower($value);
  109. switch ($value) {
  110. case 'none':
  111. case 'http_auth':
  112. case 'persona':
  113. self::$auth_type = $value;
  114. break;
  115. }
  116. }
  117. /**
  118. * Initialise les variables de configuration
  119. * @exception Minz_FileNotExistException si le CONF_PATH_NAME n'existe pas
  120. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  121. */
  122. public static function init () {
  123. try {
  124. self::parseFile ();
  125. self::setReporting ();
  126. } catch (Minz_FileNotExistException $e) {
  127. throw $e;
  128. } catch (Minz_BadConfigurationException $e) {
  129. throw $e;
  130. }
  131. }
  132. public static function writeFile() {
  133. $ini_array = array(
  134. 'general' => array(
  135. 'environment' => self::$environment,
  136. 'use_url_rewriting' => self::$use_url_rewriting,
  137. 'sel_application' => self::$sel_application,
  138. 'base_url' => self::$base_url,
  139. 'title' => self::$title,
  140. 'default_user' => self::$default_user,
  141. 'allow_anonymous' => self::$allow_anonymous,
  142. 'auth_type' => self::$auth_type,
  143. ),
  144. 'db' => self::$db,
  145. );
  146. @rename(DATA_PATH . self::CONF_PATH_NAME, DATA_PATH . self::CONF_PATH_NAME . '.bak');
  147. return file_put_contents(DATA_PATH . self::CONF_PATH_NAME, "<?php\n return " . var_export($ini_array, true) . ';');
  148. }
  149. /**
  150. * Parse un fichier de configuration
  151. * @exception Minz_FileNotExistException si le CONF_PATH_NAME n'existe pas
  152. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  153. */
  154. private static function parseFile () {
  155. if (!file_exists (DATA_PATH . self::CONF_PATH_NAME)) {
  156. throw new Minz_FileNotExistException (
  157. DATA_PATH . self::CONF_PATH_NAME,
  158. Minz_Exception::ERROR
  159. );
  160. }
  161. $ini_array = include(DATA_PATH . self::CONF_PATH_NAME);
  162. if (!$ini_array) {
  163. throw new Minz_PermissionDeniedException (
  164. DATA_PATH . self::CONF_PATH_NAME,
  165. Minz_Exception::ERROR
  166. );
  167. }
  168. // [general] est obligatoire
  169. if (!isset ($ini_array['general'])) {
  170. throw new Minz_BadConfigurationException (
  171. '[general]',
  172. Minz_Exception::ERROR
  173. );
  174. }
  175. $general = $ini_array['general'];
  176. // sel_application est obligatoire
  177. if (!isset ($general['sel_application'])) {
  178. throw new Minz_BadConfigurationException (
  179. 'sel_application',
  180. Minz_Exception::ERROR
  181. );
  182. }
  183. self::$sel_application = $general['sel_application'];
  184. if (isset ($general['environment'])) {
  185. switch ($general['environment']) {
  186. case Minz_Configuration::SILENT:
  187. case 'silent':
  188. self::$environment = Minz_Configuration::SILENT;
  189. break;
  190. case Minz_Configuration::DEVELOPMENT:
  191. case 'development':
  192. self::$environment = Minz_Configuration::DEVELOPMENT;
  193. break;
  194. case Minz_Configuration::PRODUCTION:
  195. case 'production':
  196. self::$environment = Minz_Configuration::PRODUCTION;
  197. break;
  198. default:
  199. throw new Minz_BadConfigurationException (
  200. 'environment',
  201. Minz_Exception::ERROR
  202. );
  203. }
  204. }
  205. if (isset ($general['base_url'])) {
  206. self::$base_url = $general['base_url'];
  207. }
  208. if (isset ($general['use_url_rewriting'])) {
  209. self::$use_url_rewriting = $general['use_url_rewriting'];
  210. }
  211. if (isset ($general['title'])) {
  212. self::$title = $general['title'];
  213. }
  214. if (isset ($general['language'])) {
  215. self::$language = $general['language'];
  216. }
  217. if (isset ($general['cache_enabled'])) {
  218. self::$cache_enabled = $general['cache_enabled'];
  219. if (CACHE_PATH === false && self::$cache_enabled) {
  220. throw new FileNotExistException (
  221. 'CACHE_PATH',
  222. Minz_Exception::ERROR
  223. );
  224. }
  225. }
  226. if (isset ($general['delay_cache'])) {
  227. self::$delay_cache = inval($general['delay_cache']);
  228. }
  229. if (isset ($general['default_user'])) {
  230. self::$default_user = $general['default_user'];
  231. self::$current_user = self::$default_user;
  232. }
  233. if (isset ($general['allow_anonymous'])) {
  234. self::$allow_anonymous = ((bool)($general['allow_anonymous'])) && ($general['allow_anonymous'] !== 'no');
  235. }
  236. if (isset ($general['auth_type'])) {
  237. self::_authType($general['auth_type']);
  238. }
  239. // Base de données
  240. $db = false;
  241. if (isset ($ini_array['db'])) {
  242. $db = $ini_array['db'];
  243. }
  244. if ($db) {
  245. if (!isset ($db['host'])) {
  246. throw new Minz_BadConfigurationException (
  247. 'host',
  248. Minz_Exception::ERROR
  249. );
  250. }
  251. if (!isset ($db['user'])) {
  252. throw new Minz_BadConfigurationException (
  253. 'user',
  254. Minz_Exception::ERROR
  255. );
  256. }
  257. if (!isset ($db['password'])) {
  258. throw new Minz_BadConfigurationException (
  259. 'password',
  260. Minz_Exception::ERROR
  261. );
  262. }
  263. if (!isset ($db['base'])) {
  264. throw new Minz_BadConfigurationException (
  265. 'base',
  266. Minz_Exception::ERROR
  267. );
  268. }
  269. self::$db['type'] = isset ($db['type']) ? $db['type'] : 'mysql';
  270. self::$db['host'] = $db['host'];
  271. self::$db['user'] = $db['user'];
  272. self::$db['password'] = $db['password'];
  273. self::$db['base'] = $db['base'];
  274. self::$db['prefix'] = isset ($db['prefix']) ? $db['prefix'] : '';
  275. }
  276. }
  277. private static function setReporting() {
  278. switch (self::$environment) {
  279. case self::PRODUCTION:
  280. error_reporting(E_ALL);
  281. ini_set('display_errors','Off');
  282. ini_set('log_errors', 'On');
  283. break;
  284. case self::DEVELOPMENT:
  285. error_reporting(E_ALL);
  286. ini_set('display_errors','On');
  287. ini_set('log_errors', 'On');
  288. break;
  289. case self::SILENT:
  290. error_reporting(0);
  291. break;
  292. }
  293. }
  294. }