Configuration.php 9.3 KB

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