Configuration.php 8.5 KB

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