Configuration.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. /*
  66. * Getteurs
  67. */
  68. public static function salt () {
  69. return self::$salt;
  70. }
  71. public static function environment ($str = false) {
  72. $env = self::$environment;
  73. if ($str) {
  74. switch (self::$environment) {
  75. case self::SILENT:
  76. $env = 'silent';
  77. break;
  78. case self::DEVELOPMENT:
  79. $env = 'development';
  80. break;
  81. case self::PRODUCTION:
  82. default:
  83. $env = 'production';
  84. }
  85. }
  86. return $env;
  87. }
  88. public static function baseUrl () {
  89. return self::$base_url;
  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 dataBase () {
  98. return self::$db;
  99. }
  100. public static function limits() {
  101. return self::$limits;
  102. }
  103. public static function defaultUser () {
  104. return self::$default_user;
  105. }
  106. public static function allowAnonymous() {
  107. return self::$allow_anonymous;
  108. }
  109. public static function allowAnonymousRefresh() {
  110. return self::$allow_anonymous_refresh;
  111. }
  112. public static function authType() {
  113. return self::$auth_type;
  114. }
  115. public static function needsLogin() {
  116. return self::$auth_type !== 'none';
  117. }
  118. public static function canLogIn() {
  119. return self::$auth_type === 'form' || self::$auth_type === 'persona';
  120. }
  121. public static function apiEnabled() {
  122. return self::$api_enabled;
  123. }
  124. public static function unsafeAutologinEnabled() {
  125. return self::$unsafe_autologin_enabled;
  126. }
  127. public static function _allowAnonymous($allow = false) {
  128. self::$allow_anonymous = ((bool)$allow) && self::canLogIn();
  129. }
  130. public static function _allowAnonymousRefresh($allow = false) {
  131. self::$allow_anonymous_refresh = ((bool)$allow) && self::allowAnonymous();
  132. }
  133. public static function _authType($value) {
  134. $value = strtolower($value);
  135. switch ($value) {
  136. case 'form':
  137. case 'http_auth':
  138. case 'persona':
  139. case 'none':
  140. self::$auth_type = $value;
  141. break;
  142. }
  143. self::_allowAnonymous(self::$allow_anonymous);
  144. }
  145. public static function _enableApi($value = false) {
  146. self::$api_enabled = (bool)$value;
  147. }
  148. public static function _enableAutologin($value = false) {
  149. self::$unsafe_autologin_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. 'salt' => self::$salt,
  171. 'base_url' => self::$base_url,
  172. 'title' => self::$title,
  173. 'default_user' => self::$default_user,
  174. 'allow_anonymous' => self::$allow_anonymous,
  175. 'allow_anonymous_refresh' => self::$allow_anonymous_refresh,
  176. 'auth_type' => self::$auth_type,
  177. 'api_enabled' => self::$api_enabled,
  178. 'unsafe_autologin_enabled' => self::$unsafe_autologin_enabled,
  179. ),
  180. 'limits' => self::$limits,
  181. 'db' => self::$db,
  182. );
  183. @rename(DATA_PATH . self::CONF_PATH_NAME, DATA_PATH . self::CONF_PATH_NAME . '.bak.php');
  184. $result = file_put_contents(DATA_PATH . self::CONF_PATH_NAME, "<?php\n return " . var_export($ini_array, true) . ';');
  185. if (function_exists('opcache_invalidate')) {
  186. opcache_invalidate(DATA_PATH . self::CONF_PATH_NAME); //Clear PHP 5.5+ cache for include
  187. }
  188. return (bool)$result;
  189. }
  190. /**
  191. * Parse un fichier de configuration
  192. * @exception Minz_PermissionDeniedException si le CONF_PATH_NAME n'est pas accessible
  193. * @exception Minz_BadConfigurationException si CONF_PATH_NAME mal formaté
  194. */
  195. private static function parseFile () {
  196. $ini_array = include(DATA_PATH . self::CONF_PATH_NAME);
  197. if (!is_array($ini_array)) {
  198. throw new Minz_PermissionDeniedException (
  199. DATA_PATH . self::CONF_PATH_NAME,
  200. Minz_Exception::ERROR
  201. );
  202. }
  203. // [general] est obligatoire
  204. if (!isset ($ini_array['general'])) {
  205. throw new Minz_BadConfigurationException (
  206. '[general]',
  207. Minz_Exception::ERROR
  208. );
  209. }
  210. $general = $ini_array['general'];
  211. // salt est obligatoire
  212. if (!isset ($general['salt'])) {
  213. if (isset($general['sel_application'])) { //v0.6
  214. $general['salt'] = $general['sel_application'];
  215. } else {
  216. throw new Minz_BadConfigurationException (
  217. 'salt',
  218. Minz_Exception::ERROR
  219. );
  220. }
  221. }
  222. self::$salt = $general['salt'];
  223. if (isset ($general['environment'])) {
  224. switch ($general['environment']) {
  225. case 'silent':
  226. self::$environment = Minz_Configuration::SILENT;
  227. break;
  228. case 'development':
  229. self::$environment = Minz_Configuration::DEVELOPMENT;
  230. break;
  231. case 'production':
  232. self::$environment = Minz_Configuration::PRODUCTION;
  233. break;
  234. default:
  235. if ($general['environment'] >= 0 &&
  236. $general['environment'] <= 2) {
  237. // fallback 0.7-beta
  238. self::$environment = $general['environment'];
  239. } else {
  240. throw new Minz_BadConfigurationException (
  241. 'environment',
  242. Minz_Exception::ERROR
  243. );
  244. }
  245. }
  246. }
  247. if (isset ($general['base_url'])) {
  248. self::$base_url = $general['base_url'];
  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. if (isset($ini_array['limits'])) {
  287. $limits = $ini_array['limits'];
  288. if (isset($limits['cache_duration'])) {
  289. $v = intval($limits['cache_duration']);
  290. if ($v > 0) {
  291. self::$limits['cache_duration'] = $v;
  292. }
  293. }
  294. if (isset($limits['timeout'])) {
  295. $v = intval($limits['timeout']);
  296. if ($v > 0) {
  297. self::$limits['timeout'] = $v;
  298. }
  299. }
  300. if (isset($limits['max_inactivity'])) {
  301. $v = intval($limits['max_inactivity']);
  302. if ($v > 0) {
  303. self::$limits['max_inactivity'] = $v;
  304. }
  305. }
  306. if (isset($limits['max_feeds'])) {
  307. $v = intval($limits['max_feeds']);
  308. if ($v > 0 && $v < Minz_Configuration::MAX_SMALL_INT) {
  309. self::$limits['max_feeds'] = $v;
  310. }
  311. }
  312. if (isset($limits['max_categories'])) {
  313. $v = intval($limits['max_categories']);
  314. if ($v > 0 && $v < Minz_Configuration::MAX_SMALL_INT) {
  315. self::$limits['max_categories'] = $v;
  316. }
  317. }
  318. }
  319. // Base de données
  320. if (isset ($ini_array['db'])) {
  321. $db = $ini_array['db'];
  322. if (empty($db['type'])) {
  323. throw new Minz_BadConfigurationException (
  324. 'type',
  325. Minz_Exception::ERROR
  326. );
  327. }
  328. switch ($db['type']) {
  329. case 'mysql':
  330. if (empty($db['host'])) {
  331. throw new Minz_BadConfigurationException (
  332. 'host',
  333. Minz_Exception::ERROR
  334. );
  335. }
  336. if (empty($db['user'])) {
  337. throw new Minz_BadConfigurationException (
  338. 'user',
  339. Minz_Exception::ERROR
  340. );
  341. }
  342. if (!isset($db['password'])) {
  343. throw new Minz_BadConfigurationException (
  344. 'password',
  345. Minz_Exception::ERROR
  346. );
  347. }
  348. if (empty($db['base'])) {
  349. throw new Minz_BadConfigurationException (
  350. 'base',
  351. Minz_Exception::ERROR
  352. );
  353. }
  354. self::$db['host'] = $db['host'];
  355. self::$db['user'] = $db['user'];
  356. self::$db['password'] = $db['password'];
  357. self::$db['base'] = $db['base'];
  358. if (isset($db['prefix'])) {
  359. self::$db['prefix'] = $db['prefix'];
  360. }
  361. break;
  362. case 'sqlite':
  363. self::$db['host'] = '';
  364. self::$db['user'] = '';
  365. self::$db['password'] = '';
  366. self::$db['base'] = '';
  367. self::$db['prefix'] = '';
  368. break;
  369. default:
  370. throw new Minz_BadConfigurationException (
  371. 'type',
  372. Minz_Exception::ERROR
  373. );
  374. break;
  375. }
  376. self::$db['type'] = $db['type'];
  377. }
  378. }
  379. private static function setReporting() {
  380. switch (self::$environment) {
  381. case self::PRODUCTION:
  382. error_reporting(E_ALL);
  383. ini_set('display_errors','Off');
  384. ini_set('log_errors', 'On');
  385. break;
  386. case self::DEVELOPMENT:
  387. error_reporting(E_ALL);
  388. ini_set('display_errors','On');
  389. ini_set('log_errors', 'On');
  390. break;
  391. case self::SILENT:
  392. error_reporting(0);
  393. break;
  394. }
  395. }
  396. }