Configuration.php 9.8 KB

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