Configuration.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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;
  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. }
  122. /**
  123. * Initialise les variables de configuration
  124. * @exception Minz_FileNotExistException si le CONF_PATH_NAME n'existe pas
  125. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  126. */
  127. public static function init () {
  128. try {
  129. self::parseFile ();
  130. self::setReporting ();
  131. } catch (Minz_FileNotExistException $e) {
  132. throw $e;
  133. } catch (Minz_BadConfigurationException $e) {
  134. throw $e;
  135. }
  136. }
  137. public static function writeFile() {
  138. $ini_array = array(
  139. 'general' => array(
  140. 'environment' => self::$environment,
  141. 'use_url_rewriting' => self::$use_url_rewriting,
  142. 'salt' => self::$salt,
  143. 'base_url' => self::$base_url,
  144. 'title' => self::$title,
  145. 'default_user' => self::$default_user,
  146. 'allow_anonymous' => self::$allow_anonymous,
  147. 'auth_type' => self::$auth_type,
  148. ),
  149. 'db' => self::$db,
  150. );
  151. @rename(DATA_PATH . self::CONF_PATH_NAME, DATA_PATH . self::CONF_PATH_NAME . '.bak.php');
  152. $result = file_put_contents(DATA_PATH . self::CONF_PATH_NAME, "<?php\n return " . var_export($ini_array, true) . ';');
  153. if (function_exists('opcache_invalidate')) {
  154. opcache_invalidate(DATA_PATH . self::CONF_PATH_NAME); //Clear PHP 5.5+ cache for include
  155. }
  156. return (bool)$result;
  157. }
  158. /**
  159. * Parse un fichier de configuration
  160. * @exception Minz_PermissionDeniedException si le CONF_PATH_NAME n'est pas accessible
  161. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  162. */
  163. private static function parseFile () {
  164. $ini_array = include(DATA_PATH . self::CONF_PATH_NAME);
  165. if (!is_array($ini_array)) {
  166. throw new Minz_PermissionDeniedException (
  167. DATA_PATH . self::CONF_PATH_NAME,
  168. Minz_Exception::ERROR
  169. );
  170. }
  171. // [general] est obligatoire
  172. if (!isset ($ini_array['general'])) {
  173. throw new Minz_BadConfigurationException (
  174. '[general]',
  175. Minz_Exception::ERROR
  176. );
  177. }
  178. $general = $ini_array['general'];
  179. // salt est obligatoire
  180. if (!isset ($general['salt'])) {
  181. if (isset($general['sel_application'])) { //v0.6
  182. $general['salt'] = $general['sel_application'];
  183. } else {
  184. throw new Minz_BadConfigurationException (
  185. 'salt',
  186. Minz_Exception::ERROR
  187. );
  188. }
  189. }
  190. self::$salt = $general['salt'];
  191. if (isset ($general['environment'])) {
  192. switch ($general['environment']) {
  193. case Minz_Configuration::SILENT:
  194. case 'silent':
  195. self::$environment = Minz_Configuration::SILENT;
  196. break;
  197. case Minz_Configuration::DEVELOPMENT:
  198. case 'development':
  199. self::$environment = Minz_Configuration::DEVELOPMENT;
  200. break;
  201. case Minz_Configuration::PRODUCTION:
  202. case 'production':
  203. self::$environment = Minz_Configuration::PRODUCTION;
  204. break;
  205. default:
  206. throw new Minz_BadConfigurationException (
  207. 'environment',
  208. Minz_Exception::ERROR
  209. );
  210. }
  211. }
  212. if (isset ($general['base_url'])) {
  213. self::$base_url = $general['base_url'];
  214. }
  215. if (isset ($general['use_url_rewriting'])) {
  216. self::$use_url_rewriting = $general['use_url_rewriting'];
  217. }
  218. if (isset ($general['title'])) {
  219. self::$title = $general['title'];
  220. }
  221. if (isset ($general['language'])) {
  222. self::$language = $general['language'];
  223. }
  224. if (isset ($general['cache_enabled'])) {
  225. self::$cache_enabled = $general['cache_enabled'];
  226. if (CACHE_PATH === false && self::$cache_enabled) {
  227. throw new FileNotExistException (
  228. 'CACHE_PATH',
  229. Minz_Exception::ERROR
  230. );
  231. }
  232. }
  233. if (isset ($general['delay_cache'])) {
  234. self::$delay_cache = inval($general['delay_cache']);
  235. }
  236. if (isset ($general['default_user'])) {
  237. self::$default_user = $general['default_user'];
  238. }
  239. if (isset ($general['allow_anonymous'])) {
  240. self::$allow_anonymous = ((bool)($general['allow_anonymous'])) && ($general['allow_anonymous'] !== 'no');
  241. }
  242. if (isset ($general['auth_type'])) {
  243. self::_authType($general['auth_type']);
  244. }
  245. // Base de données
  246. if (isset ($ini_array['db'])) {
  247. $db = $ini_array['db'];
  248. if (empty($db['host'])) {
  249. throw new Minz_BadConfigurationException (
  250. 'host',
  251. Minz_Exception::ERROR
  252. );
  253. }
  254. if (empty($db['user'])) {
  255. throw new Minz_BadConfigurationException (
  256. 'user',
  257. Minz_Exception::ERROR
  258. );
  259. }
  260. if (!isset ($db['password'])) {
  261. throw new Minz_BadConfigurationException (
  262. 'password',
  263. Minz_Exception::ERROR
  264. );
  265. }
  266. if (empty($db['base'])) {
  267. throw new Minz_BadConfigurationException (
  268. 'base',
  269. Minz_Exception::ERROR
  270. );
  271. }
  272. if (!empty($db['type'])) {
  273. self::$db['type'] = $db['type'];
  274. }
  275. self::$db['host'] = $db['host'];
  276. self::$db['user'] = $db['user'];
  277. self::$db['password'] = $db['password'];
  278. self::$db['base'] = $db['base'];
  279. if (isset($db['prefix'])) {
  280. self::$db['prefix'] = $db['prefix'];
  281. }
  282. }
  283. }
  284. private static function setReporting() {
  285. switch (self::$environment) {
  286. case self::PRODUCTION:
  287. error_reporting(E_ALL);
  288. ini_set('display_errors','Off');
  289. ini_set('log_errors', 'On');
  290. break;
  291. case self::DEVELOPMENT:
  292. error_reporting(E_ALL);
  293. ini_set('display_errors','On');
  294. ini_set('log_errors', 'On');
  295. break;
  296. case self::SILENT:
  297. error_reporting(0);
  298. break;
  299. }
  300. }
  301. }