Configuration.php 10 KB

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