Configuration.php 8.8 KB

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