Configuration.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. const MAX_SMALL_INT = 16384;
  58. private static $limits = array(
  59. 'cache_duration' => 800, //SimplePie cache duration in seconds
  60. 'timeout' => 10, //SimplePie timeout in seconds
  61. 'max_inactivity' => PHP_INT_MAX, //Time in seconds after which a user who has not used the account is considered inactive (no auto-refresh of feeds).
  62. 'max_feeds' => Minz_Configuration::MAX_SMALL_INT,
  63. 'max_categories' => Minz_Configuration::MAX_SMALL_INT,
  64. );
  65. private static $extensions_enabled = array();
  66. /*
  67. * Getteurs
  68. */
  69. public static function salt () {
  70. return self::$salt;
  71. }
  72. public static function environment ($str = false) {
  73. $env = self::$environment;
  74. if ($str) {
  75. switch (self::$environment) {
  76. case self::SILENT:
  77. $env = 'silent';
  78. break;
  79. case self::DEVELOPMENT:
  80. $env = 'development';
  81. break;
  82. case self::PRODUCTION:
  83. default:
  84. $env = 'production';
  85. }
  86. }
  87. return $env;
  88. }
  89. public static function baseUrl () {
  90. return self::$base_url;
  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 dataBase () {
  99. return self::$db;
  100. }
  101. public static function limits() {
  102. return self::$limits;
  103. }
  104. public static function defaultUser () {
  105. return self::$default_user;
  106. }
  107. public static function allowAnonymous() {
  108. return self::$allow_anonymous;
  109. }
  110. public static function allowAnonymousRefresh() {
  111. return self::$allow_anonymous_refresh;
  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 apiEnabled() {
  123. return self::$api_enabled;
  124. }
  125. public static function unsafeAutologinEnabled() {
  126. return self::$unsafe_autologin_enabled;
  127. }
  128. public static function extensionsEnabled() {
  129. return self::$extensions_enabled;
  130. }
  131. public static function _allowAnonymous($allow = false) {
  132. self::$allow_anonymous = ((bool)$allow) && self::canLogIn();
  133. }
  134. public static function _allowAnonymousRefresh($allow = false) {
  135. self::$allow_anonymous_refresh = ((bool)$allow) && self::allowAnonymous();
  136. }
  137. public static function _authType($value) {
  138. $value = strtolower($value);
  139. switch ($value) {
  140. case 'form':
  141. case 'http_auth':
  142. case 'persona':
  143. case 'none':
  144. self::$auth_type = $value;
  145. break;
  146. }
  147. self::_allowAnonymous(self::$allow_anonymous);
  148. }
  149. public static function _enableApi($value = false) {
  150. self::$api_enabled = (bool)$value;
  151. }
  152. public static function _enableAutologin($value = false) {
  153. self::$unsafe_autologin_enabled = (bool)$value;
  154. }
  155. /**
  156. * Initialise les variables de configuration
  157. * @exception Minz_FileNotExistException si le CONF_PATH_NAME n'existe pas
  158. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  159. */
  160. public static function init () {
  161. try {
  162. self::parseFile ();
  163. self::setReporting ();
  164. } catch (Minz_FileNotExistException $e) {
  165. throw $e;
  166. } catch (Minz_BadConfigurationException $e) {
  167. throw $e;
  168. }
  169. }
  170. public static function writeFile() {
  171. $ini_array = array(
  172. 'general' => array(
  173. 'environment' => self::environment(true),
  174. 'salt' => self::$salt,
  175. 'base_url' => self::$base_url,
  176. 'title' => self::$title,
  177. 'default_user' => self::$default_user,
  178. 'allow_anonymous' => self::$allow_anonymous,
  179. 'allow_anonymous_refresh' => self::$allow_anonymous_refresh,
  180. 'auth_type' => self::$auth_type,
  181. 'api_enabled' => self::$api_enabled,
  182. 'unsafe_autologin_enabled' => self::$unsafe_autologin_enabled,
  183. ),
  184. 'limits' => self::$limits,
  185. 'db' => self::$db,
  186. );
  187. @rename(DATA_PATH . self::CONF_PATH_NAME, DATA_PATH . self::CONF_PATH_NAME . '.bak.php');
  188. $result = file_put_contents(DATA_PATH . self::CONF_PATH_NAME, "<?php\n return " . var_export($ini_array, true) . ';');
  189. if (function_exists('opcache_invalidate')) {
  190. opcache_invalidate(DATA_PATH . self::CONF_PATH_NAME); //Clear PHP 5.5+ cache for include
  191. }
  192. return (bool)$result;
  193. }
  194. /**
  195. * Parse un fichier de configuration
  196. * @exception Minz_PermissionDeniedException si le CONF_PATH_NAME n'est pas accessible
  197. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  198. */
  199. private static function parseFile () {
  200. $ini_array = include(DATA_PATH . self::CONF_PATH_NAME);
  201. if (!is_array($ini_array)) {
  202. throw new Minz_PermissionDeniedException (
  203. DATA_PATH . self::CONF_PATH_NAME,
  204. Minz_Exception::ERROR
  205. );
  206. }
  207. // [general] est obligatoire
  208. if (!isset ($ini_array['general'])) {
  209. throw new Minz_BadConfigurationException (
  210. '[general]',
  211. Minz_Exception::ERROR
  212. );
  213. }
  214. $general = $ini_array['general'];
  215. // salt est obligatoire
  216. if (!isset ($general['salt'])) {
  217. if (isset($general['sel_application'])) { //v0.6
  218. $general['salt'] = $general['sel_application'];
  219. } else {
  220. throw new Minz_BadConfigurationException (
  221. 'salt',
  222. Minz_Exception::ERROR
  223. );
  224. }
  225. }
  226. self::$salt = $general['salt'];
  227. if (isset ($general['environment'])) {
  228. switch ($general['environment']) {
  229. case 'silent':
  230. self::$environment = Minz_Configuration::SILENT;
  231. break;
  232. case 'development':
  233. self::$environment = Minz_Configuration::DEVELOPMENT;
  234. break;
  235. case 'production':
  236. self::$environment = Minz_Configuration::PRODUCTION;
  237. break;
  238. default:
  239. if ($general['environment'] >= 0 &&
  240. $general['environment'] <= 2) {
  241. // fallback 0.7-beta
  242. self::$environment = $general['environment'];
  243. } else {
  244. throw new Minz_BadConfigurationException (
  245. 'environment',
  246. Minz_Exception::ERROR
  247. );
  248. }
  249. }
  250. }
  251. if (isset ($general['base_url'])) {
  252. self::$base_url = $general['base_url'];
  253. }
  254. if (isset ($general['title'])) {
  255. self::$title = $general['title'];
  256. }
  257. if (isset ($general['language'])) {
  258. self::$language = $general['language'];
  259. }
  260. if (isset ($general['default_user'])) {
  261. self::$default_user = $general['default_user'];
  262. }
  263. if (isset ($general['auth_type'])) {
  264. self::_authType($general['auth_type']);
  265. }
  266. if (isset ($general['allow_anonymous'])) {
  267. self::$allow_anonymous = (
  268. ((bool)($general['allow_anonymous'])) &&
  269. ($general['allow_anonymous'] !== 'no')
  270. );
  271. }
  272. if (isset ($general['allow_anonymous_refresh'])) {
  273. self::$allow_anonymous_refresh = (
  274. ((bool)($general['allow_anonymous_refresh'])) &&
  275. ($general['allow_anonymous_refresh'] !== 'no')
  276. );
  277. }
  278. if (isset ($general['api_enabled'])) {
  279. self::$api_enabled = (
  280. ((bool)($general['api_enabled'])) &&
  281. ($general['api_enabled'] !== 'no')
  282. );
  283. }
  284. if (isset ($general['unsafe_autologin_enabled'])) {
  285. self::$unsafe_autologin_enabled = (
  286. ((bool)($general['unsafe_autologin_enabled'])) &&
  287. ($general['unsafe_autologin_enabled'] !== 'no')
  288. );
  289. }
  290. if (isset($ini_array['limits'])) {
  291. $limits = $ini_array['limits'];
  292. if (isset($limits['cache_duration'])) {
  293. $v = intval($limits['cache_duration']);
  294. if ($v > 0) {
  295. self::$limits['cache_duration'] = $v;
  296. }
  297. }
  298. if (isset($limits['timeout'])) {
  299. $v = intval($limits['timeout']);
  300. if ($v > 0) {
  301. self::$limits['timeout'] = $v;
  302. }
  303. }
  304. if (isset($limits['max_inactivity'])) {
  305. $v = intval($limits['max_inactivity']);
  306. if ($v > 0) {
  307. self::$limits['max_inactivity'] = $v;
  308. }
  309. }
  310. if (isset($limits['max_feeds'])) {
  311. $v = intval($limits['max_feeds']);
  312. if ($v > 0 && $v < Minz_Configuration::MAX_SMALL_INT) {
  313. self::$limits['max_feeds'] = $v;
  314. }
  315. }
  316. if (isset($limits['max_categories'])) {
  317. $v = intval($limits['max_categories']);
  318. if ($v > 0 && $v < Minz_Configuration::MAX_SMALL_INT) {
  319. self::$limits['max_categories'] = $v;
  320. }
  321. }
  322. }
  323. // Extensions
  324. if (isset($ini_array['extensions']) && is_array($ini_array['extensions'])) {
  325. self::$extensions_enabled = $ini_array['extensions'];
  326. }
  327. // Base de données
  328. if (isset ($ini_array['db'])) {
  329. $db = $ini_array['db'];
  330. if (empty($db['type'])) {
  331. throw new Minz_BadConfigurationException (
  332. 'type',
  333. Minz_Exception::ERROR
  334. );
  335. }
  336. switch ($db['type']) {
  337. case 'mysql':
  338. if (empty($db['host'])) {
  339. throw new Minz_BadConfigurationException (
  340. 'host',
  341. Minz_Exception::ERROR
  342. );
  343. }
  344. if (empty($db['user'])) {
  345. throw new Minz_BadConfigurationException (
  346. 'user',
  347. Minz_Exception::ERROR
  348. );
  349. }
  350. if (!isset($db['password'])) {
  351. throw new Minz_BadConfigurationException (
  352. 'password',
  353. Minz_Exception::ERROR
  354. );
  355. }
  356. if (empty($db['base'])) {
  357. throw new Minz_BadConfigurationException (
  358. 'base',
  359. Minz_Exception::ERROR
  360. );
  361. }
  362. self::$db['host'] = $db['host'];
  363. self::$db['user'] = $db['user'];
  364. self::$db['password'] = $db['password'];
  365. self::$db['base'] = $db['base'];
  366. if (isset($db['prefix'])) {
  367. self::$db['prefix'] = $db['prefix'];
  368. }
  369. break;
  370. case 'sqlite':
  371. self::$db['host'] = '';
  372. self::$db['user'] = '';
  373. self::$db['password'] = '';
  374. self::$db['base'] = '';
  375. self::$db['prefix'] = '';
  376. break;
  377. default:
  378. throw new Minz_BadConfigurationException (
  379. 'type',
  380. Minz_Exception::ERROR
  381. );
  382. break;
  383. }
  384. self::$db['type'] = $db['type'];
  385. }
  386. }
  387. private static function setReporting() {
  388. switch (self::$environment) {
  389. case self::PRODUCTION:
  390. error_reporting(E_ALL);
  391. ini_set('display_errors','Off');
  392. ini_set('log_errors', 'On');
  393. break;
  394. case self::DEVELOPMENT:
  395. error_reporting(E_ALL);
  396. ini_set('display_errors','On');
  397. ini_set('log_errors', 'On');
  398. break;
  399. case self::SILENT:
  400. error_reporting(0);
  401. break;
  402. }
  403. }
  404. }