Configuration.php 11 KB

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