Configuration.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. public function removeExtension($ext_name) {
  156. self::$extensions_enabled = array_diff(
  157. self::$extensions_enabled,
  158. array($ext_name)
  159. );
  160. }
  161. public function addExtension($ext_name) {
  162. $found = array_search($ext_name, self::$extensions_enabled) !== false;
  163. if (!$found) {
  164. self::$extensions_enabled[] = $ext_name;
  165. }
  166. }
  167. /**
  168. * Initialise les variables de configuration
  169. * @exception Minz_FileNotExistException si le CONF_PATH_NAME n'existe pas
  170. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  171. */
  172. public static function init () {
  173. try {
  174. self::parseFile ();
  175. self::setReporting ();
  176. } catch (Minz_FileNotExistException $e) {
  177. throw $e;
  178. } catch (Minz_BadConfigurationException $e) {
  179. throw $e;
  180. }
  181. }
  182. public static function writeFile() {
  183. $ini_array = array(
  184. 'general' => array(
  185. 'environment' => self::environment(true),
  186. 'salt' => self::$salt,
  187. 'base_url' => self::$base_url,
  188. 'title' => self::$title,
  189. 'default_user' => self::$default_user,
  190. 'allow_anonymous' => self::$allow_anonymous,
  191. 'allow_anonymous_refresh' => self::$allow_anonymous_refresh,
  192. 'auth_type' => self::$auth_type,
  193. 'api_enabled' => self::$api_enabled,
  194. 'unsafe_autologin_enabled' => self::$unsafe_autologin_enabled,
  195. ),
  196. 'limits' => self::$limits,
  197. 'db' => self::$db,
  198. 'extensions_enabled' => self::$extensions_enabled,
  199. );
  200. @rename(DATA_PATH . self::CONF_PATH_NAME, DATA_PATH . self::CONF_PATH_NAME . '.bak.php');
  201. $result = file_put_contents(DATA_PATH . self::CONF_PATH_NAME, "<?php\n return " . var_export($ini_array, true) . ';');
  202. if (function_exists('opcache_invalidate')) {
  203. opcache_invalidate(DATA_PATH . self::CONF_PATH_NAME); //Clear PHP 5.5+ cache for include
  204. }
  205. return (bool)$result;
  206. }
  207. /**
  208. * Parse un fichier de configuration
  209. * @exception Minz_PermissionDeniedException si le CONF_PATH_NAME n'est pas accessible
  210. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  211. */
  212. private static function parseFile () {
  213. $ini_array = include(DATA_PATH . self::CONF_PATH_NAME);
  214. if (!is_array($ini_array)) {
  215. throw new Minz_PermissionDeniedException (
  216. DATA_PATH . self::CONF_PATH_NAME,
  217. Minz_Exception::ERROR
  218. );
  219. }
  220. // [general] est obligatoire
  221. if (!isset ($ini_array['general'])) {
  222. throw new Minz_BadConfigurationException (
  223. '[general]',
  224. Minz_Exception::ERROR
  225. );
  226. }
  227. $general = $ini_array['general'];
  228. // salt est obligatoire
  229. if (!isset ($general['salt'])) {
  230. if (isset($general['sel_application'])) { //v0.6
  231. $general['salt'] = $general['sel_application'];
  232. } else {
  233. throw new Minz_BadConfigurationException (
  234. 'salt',
  235. Minz_Exception::ERROR
  236. );
  237. }
  238. }
  239. self::$salt = $general['salt'];
  240. if (isset ($general['environment'])) {
  241. switch ($general['environment']) {
  242. case 'silent':
  243. self::$environment = Minz_Configuration::SILENT;
  244. break;
  245. case 'development':
  246. self::$environment = Minz_Configuration::DEVELOPMENT;
  247. break;
  248. case 'production':
  249. self::$environment = Minz_Configuration::PRODUCTION;
  250. break;
  251. default:
  252. if ($general['environment'] >= 0 &&
  253. $general['environment'] <= 2) {
  254. // fallback 0.7-beta
  255. self::$environment = $general['environment'];
  256. } else {
  257. throw new Minz_BadConfigurationException (
  258. 'environment',
  259. Minz_Exception::ERROR
  260. );
  261. }
  262. }
  263. }
  264. if (isset ($general['base_url'])) {
  265. self::$base_url = $general['base_url'];
  266. }
  267. if (isset ($general['title'])) {
  268. self::$title = $general['title'];
  269. }
  270. if (isset ($general['language'])) {
  271. self::$language = $general['language'];
  272. }
  273. if (isset ($general['default_user'])) {
  274. self::$default_user = $general['default_user'];
  275. }
  276. if (isset ($general['auth_type'])) {
  277. self::_authType($general['auth_type']);
  278. }
  279. if (isset ($general['allow_anonymous'])) {
  280. self::$allow_anonymous = (
  281. ((bool)($general['allow_anonymous'])) &&
  282. ($general['allow_anonymous'] !== 'no')
  283. );
  284. }
  285. if (isset ($general['allow_anonymous_refresh'])) {
  286. self::$allow_anonymous_refresh = (
  287. ((bool)($general['allow_anonymous_refresh'])) &&
  288. ($general['allow_anonymous_refresh'] !== 'no')
  289. );
  290. }
  291. if (isset ($general['api_enabled'])) {
  292. self::$api_enabled = (
  293. ((bool)($general['api_enabled'])) &&
  294. ($general['api_enabled'] !== 'no')
  295. );
  296. }
  297. if (isset ($general['unsafe_autologin_enabled'])) {
  298. self::$unsafe_autologin_enabled = (
  299. ((bool)($general['unsafe_autologin_enabled'])) &&
  300. ($general['unsafe_autologin_enabled'] !== 'no')
  301. );
  302. }
  303. if (isset($ini_array['limits'])) {
  304. $limits = $ini_array['limits'];
  305. if (isset($limits['cache_duration'])) {
  306. $v = intval($limits['cache_duration']);
  307. if ($v > 0) {
  308. self::$limits['cache_duration'] = $v;
  309. }
  310. }
  311. if (isset($limits['timeout'])) {
  312. $v = intval($limits['timeout']);
  313. if ($v > 0) {
  314. self::$limits['timeout'] = $v;
  315. }
  316. }
  317. if (isset($limits['max_inactivity'])) {
  318. $v = intval($limits['max_inactivity']);
  319. if ($v > 0) {
  320. self::$limits['max_inactivity'] = $v;
  321. }
  322. }
  323. if (isset($limits['max_feeds'])) {
  324. $v = intval($limits['max_feeds']);
  325. if ($v > 0 && $v < Minz_Configuration::MAX_SMALL_INT) {
  326. self::$limits['max_feeds'] = $v;
  327. }
  328. }
  329. if (isset($limits['max_categories'])) {
  330. $v = intval($limits['max_categories']);
  331. if ($v > 0 && $v < Minz_Configuration::MAX_SMALL_INT) {
  332. self::$limits['max_categories'] = $v;
  333. }
  334. }
  335. }
  336. // Extensions
  337. if (isset($ini_array['extensions_enabled']) &&
  338. is_array($ini_array['extensions_enabled'])) {
  339. self::$extensions_enabled = $ini_array['extensions_enabled'];
  340. }
  341. // Base de données
  342. if (isset ($ini_array['db'])) {
  343. $db = $ini_array['db'];
  344. if (empty($db['type'])) {
  345. throw new Minz_BadConfigurationException (
  346. 'type',
  347. Minz_Exception::ERROR
  348. );
  349. }
  350. switch ($db['type']) {
  351. case 'mysql':
  352. if (empty($db['host'])) {
  353. throw new Minz_BadConfigurationException (
  354. 'host',
  355. Minz_Exception::ERROR
  356. );
  357. }
  358. if (empty($db['user'])) {
  359. throw new Minz_BadConfigurationException (
  360. 'user',
  361. Minz_Exception::ERROR
  362. );
  363. }
  364. if (!isset($db['password'])) {
  365. throw new Minz_BadConfigurationException (
  366. 'password',
  367. Minz_Exception::ERROR
  368. );
  369. }
  370. if (empty($db['base'])) {
  371. throw new Minz_BadConfigurationException (
  372. 'base',
  373. Minz_Exception::ERROR
  374. );
  375. }
  376. self::$db['host'] = $db['host'];
  377. self::$db['user'] = $db['user'];
  378. self::$db['password'] = $db['password'];
  379. self::$db['base'] = $db['base'];
  380. if (isset($db['prefix'])) {
  381. self::$db['prefix'] = $db['prefix'];
  382. }
  383. break;
  384. case 'sqlite':
  385. self::$db['host'] = '';
  386. self::$db['user'] = '';
  387. self::$db['password'] = '';
  388. self::$db['base'] = '';
  389. self::$db['prefix'] = '';
  390. break;
  391. default:
  392. throw new Minz_BadConfigurationException (
  393. 'type',
  394. Minz_Exception::ERROR
  395. );
  396. break;
  397. }
  398. self::$db['type'] = $db['type'];
  399. }
  400. }
  401. private static function setReporting() {
  402. switch (self::$environment) {
  403. case self::PRODUCTION:
  404. error_reporting(E_ALL);
  405. ini_set('display_errors','Off');
  406. ini_set('log_errors', 'On');
  407. break;
  408. case self::DEVELOPMENT:
  409. error_reporting(E_ALL);
  410. ini_set('display_errors','On');
  411. ini_set('log_errors', 'On');
  412. break;
  413. case self::SILENT:
  414. error_reporting(0);
  415. break;
  416. }
  417. }
  418. }