Configuration.php 11 KB

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