Configuration.php 6.7 KB

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