Configuration.php 8.4 KB

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