Configuration.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 $allow_anonymous = false;
  51. private static $auth_type = 'none';
  52. private static $db = array (
  53. 'host' => false,
  54. 'user' => false,
  55. 'password' => false,
  56. 'base' => false
  57. );
  58. /*
  59. * Getteurs
  60. */
  61. public static function salt () {
  62. return self::$sel_application;
  63. }
  64. public static function environment () {
  65. return self::$environment;
  66. }
  67. public static function baseUrl () {
  68. return self::$base_url;
  69. }
  70. public static function useUrlRewriting () {
  71. return self::$use_url_rewriting;
  72. }
  73. public static function title () {
  74. return self::$title;
  75. }
  76. public static function language () {
  77. return self::$language;
  78. }
  79. public static function cacheEnabled () {
  80. return self::$cache_enabled;
  81. }
  82. public static function delayCache () {
  83. return self::$delay_cache;
  84. }
  85. public static function dataBase () {
  86. return self::$db;
  87. }
  88. public static function defaultUser () {
  89. return self::$default_user;
  90. }
  91. public static function isAdmin($currentUser) {
  92. return $currentUser === self::$default_user;
  93. }
  94. public static function allowAnonymous() {
  95. return self::$allow_anonymous;
  96. }
  97. public static function authType() {
  98. return self::$auth_type;
  99. }
  100. public static function needsLogin() {
  101. return self::$auth_type !== 'none';
  102. }
  103. public static function canLogIn() {
  104. return self::$auth_type === 'persona';
  105. }
  106. public static function _allowAnonymous($allow = false) {
  107. self::$allow_anonymous = (bool)$allow;
  108. }
  109. public static function _authType($value) {
  110. $value = strtolower($value);
  111. switch ($value) {
  112. case 'http_auth':
  113. case 'persona':
  114. case 'none':
  115. self::$auth_type = $value;
  116. break;
  117. }
  118. }
  119. /**
  120. * Initialise les variables de configuration
  121. * @exception Minz_FileNotExistException si le CONF_PATH_NAME n'existe pas
  122. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  123. */
  124. public static function init () {
  125. try {
  126. self::parseFile ();
  127. self::setReporting ();
  128. } catch (Minz_FileNotExistException $e) {
  129. throw $e;
  130. } catch (Minz_BadConfigurationException $e) {
  131. throw $e;
  132. }
  133. }
  134. public static function writeFile() {
  135. $ini_array = array(
  136. 'general' => array(
  137. 'environment' => self::$environment,
  138. 'use_url_rewriting' => self::$use_url_rewriting,
  139. 'sel_application' => self::$sel_application,
  140. 'base_url' => self::$base_url,
  141. 'title' => self::$title,
  142. 'default_user' => self::$default_user,
  143. 'allow_anonymous' => self::$allow_anonymous,
  144. 'auth_type' => self::$auth_type,
  145. ),
  146. 'db' => self::$db,
  147. );
  148. @rename(DATA_PATH . self::CONF_PATH_NAME, DATA_PATH . self::CONF_PATH_NAME . '.bak');
  149. return file_put_contents(DATA_PATH . self::CONF_PATH_NAME, "<?php\n return " . var_export($ini_array, true) . ';');
  150. }
  151. /**
  152. * Parse un fichier de configuration
  153. * @exception Minz_FileNotExistException si le CONF_PATH_NAME n'existe pas
  154. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  155. */
  156. private static function parseFile () {
  157. if (!file_exists (DATA_PATH . self::CONF_PATH_NAME)) {
  158. throw new Minz_FileNotExistException (
  159. DATA_PATH . self::CONF_PATH_NAME,
  160. Minz_Exception::ERROR
  161. );
  162. }
  163. $ini_array = include(DATA_PATH . self::CONF_PATH_NAME);
  164. if (!$ini_array) {
  165. throw new Minz_PermissionDeniedException (
  166. DATA_PATH . self::CONF_PATH_NAME,
  167. Minz_Exception::ERROR
  168. );
  169. }
  170. // [general] est obligatoire
  171. if (!isset ($ini_array['general'])) {
  172. throw new Minz_BadConfigurationException (
  173. '[general]',
  174. Minz_Exception::ERROR
  175. );
  176. }
  177. $general = $ini_array['general'];
  178. // sel_application est obligatoire
  179. if (!isset ($general['sel_application'])) {
  180. throw new Minz_BadConfigurationException (
  181. 'sel_application',
  182. Minz_Exception::ERROR
  183. );
  184. }
  185. self::$sel_application = $general['sel_application'];
  186. if (isset ($general['environment'])) {
  187. switch ($general['environment']) {
  188. case Minz_Configuration::SILENT:
  189. case 'silent':
  190. self::$environment = Minz_Configuration::SILENT;
  191. break;
  192. case Minz_Configuration::DEVELOPMENT:
  193. case 'development':
  194. self::$environment = Minz_Configuration::DEVELOPMENT;
  195. break;
  196. case Minz_Configuration::PRODUCTION:
  197. case 'production':
  198. self::$environment = Minz_Configuration::PRODUCTION;
  199. break;
  200. default:
  201. throw new Minz_BadConfigurationException (
  202. 'environment',
  203. Minz_Exception::ERROR
  204. );
  205. }
  206. }
  207. if (isset ($general['base_url'])) {
  208. self::$base_url = $general['base_url'];
  209. }
  210. if (isset ($general['use_url_rewriting'])) {
  211. self::$use_url_rewriting = $general['use_url_rewriting'];
  212. }
  213. if (isset ($general['title'])) {
  214. self::$title = $general['title'];
  215. }
  216. if (isset ($general['language'])) {
  217. self::$language = $general['language'];
  218. }
  219. if (isset ($general['cache_enabled'])) {
  220. self::$cache_enabled = $general['cache_enabled'];
  221. if (CACHE_PATH === false && self::$cache_enabled) {
  222. throw new FileNotExistException (
  223. 'CACHE_PATH',
  224. Minz_Exception::ERROR
  225. );
  226. }
  227. }
  228. if (isset ($general['delay_cache'])) {
  229. self::$delay_cache = inval($general['delay_cache']);
  230. }
  231. if (isset ($general['default_user'])) {
  232. self::$default_user = $general['default_user'];
  233. }
  234. if (isset ($general['allow_anonymous'])) {
  235. self::$allow_anonymous = ((bool)($general['allow_anonymous'])) && ($general['allow_anonymous'] !== 'no');
  236. }
  237. if (isset ($general['auth_type'])) {
  238. self::_authType($general['auth_type']);
  239. }
  240. // Base de données
  241. $db = false;
  242. if (isset ($ini_array['db'])) {
  243. $db = $ini_array['db'];
  244. }
  245. if ($db) {
  246. if (!isset ($db['host'])) {
  247. throw new Minz_BadConfigurationException (
  248. 'host',
  249. Minz_Exception::ERROR
  250. );
  251. }
  252. if (!isset ($db['user'])) {
  253. throw new Minz_BadConfigurationException (
  254. 'user',
  255. Minz_Exception::ERROR
  256. );
  257. }
  258. if (!isset ($db['password'])) {
  259. throw new Minz_BadConfigurationException (
  260. 'password',
  261. Minz_Exception::ERROR
  262. );
  263. }
  264. if (!isset ($db['base'])) {
  265. throw new Minz_BadConfigurationException (
  266. 'base',
  267. Minz_Exception::ERROR
  268. );
  269. }
  270. self::$db['type'] = isset ($db['type']) ? $db['type'] : 'mysql';
  271. self::$db['host'] = $db['host'];
  272. self::$db['user'] = $db['user'];
  273. self::$db['password'] = $db['password'];
  274. self::$db['base'] = $db['base'];
  275. self::$db['prefix'] = isset ($db['prefix']) ? $db['prefix'] : '';
  276. }
  277. }
  278. private static function setReporting() {
  279. switch (self::$environment) {
  280. case self::PRODUCTION:
  281. error_reporting(E_ALL);
  282. ini_set('display_errors','Off');
  283. ini_set('log_errors', 'On');
  284. break;
  285. case self::DEVELOPMENT:
  286. error_reporting(E_ALL);
  287. ini_set('display_errors','On');
  288. ini_set('log_errors', 'On');
  289. break;
  290. case self::SILENT:
  291. error_reporting(0);
  292. break;
  293. }
  294. }
  295. }