Configuration.php 9.6 KB

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