Configuration.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 $db = array (
  52. 'host' => false,
  53. 'user' => false,
  54. 'password' => false,
  55. 'base' => false
  56. );
  57. /*
  58. * Getteurs
  59. */
  60. public static function salt () {
  61. return self::$sel_application;
  62. }
  63. public static function environment () {
  64. return self::$environment;
  65. }
  66. public static function baseUrl () {
  67. return self::$base_url;
  68. }
  69. public static function useUrlRewriting () {
  70. return self::$use_url_rewriting;
  71. }
  72. public static function title () {
  73. return stripslashes(self::$title);
  74. }
  75. public static function language () {
  76. return self::$language;
  77. }
  78. public static function cacheEnabled () {
  79. return self::$cache_enabled;
  80. }
  81. public static function delayCache () {
  82. return self::$delay_cache;
  83. }
  84. public static function dataBase () {
  85. return self::$db;
  86. }
  87. public static function defaultUser () {
  88. return self::$default_user;
  89. }
  90. public static function currentUser () {
  91. return self::$current_user;
  92. }
  93. public static function isAdmin () {
  94. return self::$current_user === self::$default_user;
  95. }
  96. /**
  97. * Initialise les variables de configuration
  98. * @exception Minz_FileNotExistException si le CONF_PATH_NAME n'existe pas
  99. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  100. */
  101. public static function init () {
  102. try {
  103. self::parseFile ();
  104. self::setReporting ();
  105. } catch (Minz_FileNotExistException $e) {
  106. throw $e;
  107. } catch (Minz_BadConfigurationException $e) {
  108. throw $e;
  109. }
  110. }
  111. /**
  112. * Parse un fichier de configuration de type ".ini"
  113. * @exception Minz_FileNotExistException si le CONF_PATH_NAME n'existe pas
  114. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  115. */
  116. private static function parseFile () {
  117. if (!file_exists (DATA_PATH . self::CONF_PATH_NAME)) {
  118. throw new Minz_FileNotExistException (
  119. DATA_PATH . self::CONF_PATH_NAME,
  120. Minz_Exception::ERROR
  121. );
  122. }
  123. $ini_array = include(DATA_PATH . self::CONF_PATH_NAME);
  124. if (!$ini_array) {
  125. throw new Minz_PermissionDeniedException (
  126. DATA_PATH . self::CONF_PATH_NAME,
  127. Minz_Exception::ERROR
  128. );
  129. }
  130. // [general] est obligatoire
  131. if (!isset ($ini_array['general'])) {
  132. throw new Minz_BadConfigurationException (
  133. '[general]',
  134. Minz_Exception::ERROR
  135. );
  136. }
  137. $general = $ini_array['general'];
  138. // sel_application est obligatoire
  139. if (!isset ($general['sel_application'])) {
  140. throw new Minz_BadConfigurationException (
  141. 'sel_application',
  142. Minz_Exception::ERROR
  143. );
  144. }
  145. self::$sel_application = $general['sel_application'];
  146. if (isset ($general['environment'])) {
  147. switch ($general['environment']) {
  148. case 'silent':
  149. self::$environment = Minz_Configuration::SILENT;
  150. break;
  151. case 'development':
  152. self::$environment = Minz_Configuration::DEVELOPMENT;
  153. break;
  154. case 'production':
  155. self::$environment = Minz_Configuration::PRODUCTION;
  156. break;
  157. default:
  158. throw new Minz_BadConfigurationException (
  159. 'environment',
  160. Minz_Exception::ERROR
  161. );
  162. }
  163. }
  164. if (isset ($general['base_url'])) {
  165. self::$base_url = $general['base_url'];
  166. }
  167. if (isset ($general['use_url_rewriting'])) {
  168. self::$use_url_rewriting = $general['use_url_rewriting'];
  169. }
  170. if (isset ($general['title'])) {
  171. self::$title = $general['title'];
  172. }
  173. if (isset ($general['language'])) {
  174. self::$language = $general['language'];
  175. }
  176. if (isset ($general['cache_enabled'])) {
  177. self::$cache_enabled = $general['cache_enabled'];
  178. if (CACHE_PATH === false && self::$cache_enabled) {
  179. throw new FileNotExistException (
  180. 'CACHE_PATH',
  181. Minz_Exception::ERROR
  182. );
  183. }
  184. }
  185. if (isset ($general['delay_cache'])) {
  186. self::$delay_cache = $general['delay_cache'];
  187. }
  188. if (isset ($general['default_user'])) {
  189. self::$default_user = $general['default_user'];
  190. self::$current_user = self::$default_user;
  191. }
  192. // Base de données
  193. $db = false;
  194. if (isset ($ini_array['db'])) {
  195. $db = $ini_array['db'];
  196. }
  197. if ($db) {
  198. if (!isset ($db['host'])) {
  199. throw new Minz_BadConfigurationException (
  200. 'host',
  201. Minz_Exception::ERROR
  202. );
  203. }
  204. if (!isset ($db['user'])) {
  205. throw new Minz_BadConfigurationException (
  206. 'user',
  207. Minz_Exception::ERROR
  208. );
  209. }
  210. if (!isset ($db['password'])) {
  211. throw new Minz_BadConfigurationException (
  212. 'password',
  213. Minz_Exception::ERROR
  214. );
  215. }
  216. if (!isset ($db['base'])) {
  217. throw new Minz_BadConfigurationException (
  218. 'base',
  219. Minz_Exception::ERROR
  220. );
  221. }
  222. self::$db['type'] = isset ($db['type']) ? $db['type'] : 'mysql';
  223. self::$db['host'] = $db['host'];
  224. self::$db['user'] = $db['user'];
  225. self::$db['password'] = $db['password'];
  226. self::$db['base'] = $db['base'];
  227. self::$db['prefix'] = isset ($db['prefix']) ? $db['prefix'] : '';
  228. }
  229. }
  230. private static function setReporting () {
  231. if (self::environment () == self::DEVELOPMENT) {
  232. error_reporting (E_ALL);
  233. ini_set ('display_errors','On');
  234. ini_set('log_errors', 'On');
  235. } elseif (self::environment () == self::PRODUCTION) {
  236. error_reporting(E_ALL);
  237. ini_set('display_errors','Off');
  238. ini_set('log_errors', 'On');
  239. } else {
  240. error_reporting(0);
  241. }
  242. }
  243. }