Configuration.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. class FreshRSS_Configuration {
  3. private $filename;
  4. private $data = array(
  5. 'language' => 'en',
  6. 'old_entries' => 3,
  7. 'keep_history_default' => 0,
  8. 'ttl_default' => 3600,
  9. 'mail_login' => '',
  10. 'token' => '',
  11. 'passwordHash' => '', //CRYPT_BLOWFISH
  12. 'apiPasswordHash' => '', //CRYPT_BLOWFISH
  13. 'posts_per_page' => 20,
  14. 'view_mode' => 'normal',
  15. 'default_view' => 'adaptive',
  16. 'default_state' => FreshRSS_Entry::STATE_NOT_READ,
  17. 'auto_load_more' => true,
  18. 'display_posts' => false,
  19. 'display_categories' => false,
  20. 'hide_read_feeds' => true,
  21. 'onread_jump_next' => true,
  22. 'lazyload' => true,
  23. 'sticky_post' => true,
  24. 'reading_confirm' => false,
  25. 'auto_remove_article' => false,
  26. 'sort_order' => 'DESC',
  27. 'anon_access' => false,
  28. 'mark_when' => array(
  29. 'article' => true,
  30. 'site' => true,
  31. 'scroll' => false,
  32. 'reception' => false,
  33. ),
  34. 'theme' => 'Origine',
  35. 'content_width' => 'thin',
  36. 'shortcuts' => array(
  37. 'mark_read' => 'r',
  38. 'mark_favorite' => 'f',
  39. 'go_website' => 'space',
  40. 'next_entry' => 'j',
  41. 'prev_entry' => 'k',
  42. 'first_entry' => 'home',
  43. 'last_entry' => 'end',
  44. 'collapse_entry' => 'c',
  45. 'load_more' => 'm',
  46. 'auto_share' => 's',
  47. 'focus_search' => 'a',
  48. 'user_filter' => 'u',
  49. 'help' => 'f1',
  50. 'close_dropdown' => 'escape',
  51. ),
  52. 'topline_read' => true,
  53. 'topline_favorite' => true,
  54. 'topline_date' => true,
  55. 'topline_link' => true,
  56. 'bottomline_read' => true,
  57. 'bottomline_favorite' => true,
  58. 'bottomline_sharing' => true,
  59. 'bottomline_tags' => true,
  60. 'bottomline_date' => true,
  61. 'bottomline_link' => true,
  62. 'sharing' => array(),
  63. 'queries' => array(),
  64. 'html5_notif_timeout' => 0,
  65. 'extensions_enabled' => array(),
  66. );
  67. private $available_languages = array(
  68. 'en' => 'English',
  69. 'fr' => 'Français',
  70. );
  71. private $shares;
  72. public function __construct($user) {
  73. $this->filename = DATA_PATH . DIRECTORY_SEPARATOR . $user . '_user.php';
  74. $data = @include($this->filename);
  75. if (!is_array($data)) {
  76. throw new Minz_PermissionDeniedException($this->filename);
  77. }
  78. foreach ($data as $key => $value) {
  79. if (isset($this->data[$key])) {
  80. $function = '_' . $key;
  81. $this->$function($value);
  82. }
  83. }
  84. $this->data['user'] = $user;
  85. $this->shares = DATA_PATH . DIRECTORY_SEPARATOR . 'shares.php';
  86. $shares = @include($this->shares);
  87. if (!is_array($shares)) {
  88. throw new Minz_PermissionDeniedException($this->shares);
  89. }
  90. $this->data['shares'] = $shares;
  91. }
  92. public function save() {
  93. @rename($this->filename, $this->filename . '.bak.php');
  94. unset($this->data['shares']); // Remove shares because it is not intended to be stored in user configuration
  95. if (file_put_contents($this->filename, "<?php\n return " . var_export($this->data, true) . ';', LOCK_EX) === false) {
  96. throw new Minz_PermissionDeniedException($this->filename);
  97. }
  98. if (function_exists('opcache_invalidate')) {
  99. opcache_invalidate($this->filename); //Clear PHP 5.5+ cache for include
  100. }
  101. invalidateHttpCache();
  102. return true;
  103. }
  104. public function __get($name) {
  105. if (array_key_exists($name, $this->data)) {
  106. return $this->data[$name];
  107. } else {
  108. $trace = debug_backtrace();
  109. trigger_error('Undefined FreshRSS_Configuration->' . $name . 'in ' . $trace[0]['file'] . ' line ' . $trace[0]['line'], E_USER_NOTICE); //TODO: Use Minz exceptions
  110. return null;
  111. }
  112. }
  113. public function availableLanguages() {
  114. return $this->available_languages;
  115. }
  116. public function remove_query_by_get($get) {
  117. $final_queries = array();
  118. foreach ($this->queries as $key => $query) {
  119. if (empty($query['get']) || $query['get'] !== $get) {
  120. $final_queries[$key] = $query;
  121. }
  122. }
  123. $this->_queries($final_queries);
  124. }
  125. public function _language($value) {
  126. if (!isset($this->available_languages[$value])) {
  127. $value = 'en';
  128. }
  129. $this->data['language'] = $value;
  130. }
  131. public function _posts_per_page($value) {
  132. $value = intval($value);
  133. $this->data['posts_per_page'] = $value > 0 ? $value : 10;
  134. }
  135. public function _view_mode($value) {
  136. if ($value === 'global' || $value === 'reader') {
  137. $this->data['view_mode'] = $value;
  138. } else {
  139. $this->data['view_mode'] = 'normal';
  140. }
  141. }
  142. public function _default_view($value) {
  143. switch ($value) {
  144. case 'all':
  145. $this->data['default_view'] = $value;
  146. $this->data['default_state'] = (FreshRSS_Entry::STATE_READ +
  147. FreshRSS_Entry::STATE_NOT_READ);
  148. break;
  149. case 'adaptive':
  150. case 'unread':
  151. default:
  152. $this->data['default_view'] = $value;
  153. $this->data['default_state'] = FreshRSS_Entry::STATE_NOT_READ;
  154. }
  155. }
  156. public function _default_state($value) {
  157. $this->data['default_state'] = (int)$value;
  158. }
  159. public function _display_posts($value) {
  160. $this->data['display_posts'] = ((bool)$value) && $value !== 'no';
  161. }
  162. public function _display_categories($value) {
  163. $this->data['display_categories'] = ((bool)$value) && $value !== 'no';
  164. }
  165. public function _hide_read_feeds($value) {
  166. $this->data['hide_read_feeds'] = (bool)$value;
  167. }
  168. public function _onread_jump_next($value) {
  169. $this->data['onread_jump_next'] = ((bool)$value) && $value !== 'no';
  170. }
  171. public function _lazyload($value) {
  172. $this->data['lazyload'] = ((bool)$value) && $value !== 'no';
  173. }
  174. public function _sticky_post($value) {
  175. $this->data['sticky_post'] = ((bool)$value) && $value !== 'no';
  176. }
  177. public function _reading_confirm($value) {
  178. $this->data['reading_confirm'] = ((bool)$value) && $value !== 'no';
  179. }
  180. public function _auto_remove_article($value) {
  181. $this->data['auto_remove_article'] = ((bool)$value) && $value !== 'no';
  182. }
  183. public function _sort_order($value) {
  184. $this->data['sort_order'] = $value === 'ASC' ? 'ASC' : 'DESC';
  185. }
  186. public function _old_entries($value) {
  187. $value = intval($value);
  188. $this->data['old_entries'] = $value > 0 ? $value : 3;
  189. }
  190. public function _keep_history_default($value) {
  191. $value = intval($value);
  192. $this->data['keep_history_default'] = $value >= -1 ? $value : 0;
  193. }
  194. public function _ttl_default($value) {
  195. $value = intval($value);
  196. $this->data['ttl_default'] = $value >= -1 ? $value : 3600;
  197. }
  198. public function _shortcuts($values) {
  199. foreach ($values as $key => $value) {
  200. if (isset($this->data['shortcuts'][$key])) {
  201. $this->data['shortcuts'][$key] = $value;
  202. }
  203. }
  204. }
  205. public function _passwordHash($value) {
  206. $this->data['passwordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
  207. }
  208. public function _apiPasswordHash($value) {
  209. $this->data['apiPasswordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
  210. }
  211. public function _mail_login($value) {
  212. $value = filter_var($value, FILTER_VALIDATE_EMAIL);
  213. if ($value) {
  214. $this->data['mail_login'] = $value;
  215. } else {
  216. $this->data['mail_login'] = '';
  217. }
  218. }
  219. public function _anon_access($value) {
  220. $this->data['anon_access'] = ((bool)$value) && $value !== 'no';
  221. }
  222. public function _mark_when($values) {
  223. foreach ($values as $key => $value) {
  224. if (isset($this->data['mark_when'][$key])) {
  225. $this->data['mark_when'][$key] = ((bool)$value) && $value !== 'no';
  226. }
  227. }
  228. }
  229. public function _sharing($values) {
  230. $this->data['sharing'] = array();
  231. $unique = array();
  232. foreach ($values as $value) {
  233. if (!is_array($value)) {
  234. continue;
  235. }
  236. // Verify URL and add default value when needed
  237. if (isset($value['url'])) {
  238. $is_url = (
  239. filter_var($value['url'], FILTER_VALIDATE_URL) ||
  240. (version_compare(PHP_VERSION, '5.3.3', '<') &&
  241. (strpos($value, '-') > 0) &&
  242. ($value === filter_var($value, FILTER_SANITIZE_URL)))
  243. ); //PHP bug #51192
  244. if (!$is_url) {
  245. continue;
  246. }
  247. } else {
  248. $value['url'] = null;
  249. }
  250. // Add a default name
  251. if (empty($value['name'])) {
  252. $value['name'] = $value['type'];
  253. }
  254. $json_value = json_encode($value);
  255. if (!in_array($json_value, $unique)) {
  256. $unique[] = $json_value;
  257. $this->data['sharing'][] = $value;
  258. }
  259. }
  260. }
  261. public function _queries($values) {
  262. $this->data['queries'] = array();
  263. foreach ($values as $value) {
  264. $value = array_filter($value);
  265. $params = $value;
  266. unset($params['name']);
  267. unset($params['url']);
  268. $value['url'] = Minz_Url::display(array('params' => $params));
  269. $this->data['queries'][] = $value;
  270. }
  271. }
  272. public function _theme($value) {
  273. $this->data['theme'] = $value;
  274. }
  275. public function _content_width($value) {
  276. if ($value === 'medium' ||
  277. $value === 'large' ||
  278. $value === 'no_limit') {
  279. $this->data['content_width'] = $value;
  280. } else {
  281. $this->data['content_width'] = 'thin';
  282. }
  283. }
  284. public function _html5_notif_timeout($value) {
  285. $value = intval($value);
  286. $this->data['html5_notif_timeout'] = $value >= 0 ? $value : 0;
  287. }
  288. public function _token($value) {
  289. $this->data['token'] = $value;
  290. }
  291. public function _auto_load_more($value) {
  292. $this->data['auto_load_more'] = ((bool)$value) && $value !== 'no';
  293. }
  294. public function _topline_read($value) {
  295. $this->data['topline_read'] = ((bool)$value) && $value !== 'no';
  296. }
  297. public function _topline_favorite($value) {
  298. $this->data['topline_favorite'] = ((bool)$value) && $value !== 'no';
  299. }
  300. public function _topline_date($value) {
  301. $this->data['topline_date'] = ((bool)$value) && $value !== 'no';
  302. }
  303. public function _topline_link($value) {
  304. $this->data['topline_link'] = ((bool)$value) && $value !== 'no';
  305. }
  306. public function _bottomline_read($value) {
  307. $this->data['bottomline_read'] = ((bool)$value) && $value !== 'no';
  308. }
  309. public function _bottomline_favorite($value) {
  310. $this->data['bottomline_favorite'] = ((bool)$value) && $value !== 'no';
  311. }
  312. public function _bottomline_sharing($value) {
  313. $this->data['bottomline_sharing'] = ((bool)$value) && $value !== 'no';
  314. }
  315. public function _bottomline_tags($value) {
  316. $this->data['bottomline_tags'] = ((bool)$value) && $value !== 'no';
  317. }
  318. public function _bottomline_date($value) {
  319. $this->data['bottomline_date'] = ((bool)$value) && $value !== 'no';
  320. }
  321. public function _bottomline_link($value) {
  322. $this->data['bottomline_link'] = ((bool)$value) && $value !== 'no';
  323. }
  324. public function _extensions_enabled($value) {
  325. if (!is_array($value)) {
  326. $value = array($value);
  327. }
  328. $this->data['extensions_enabled'] = $value;
  329. }
  330. public function removeExtension($ext_name) {
  331. $this->data['extensions_enabled'] = array_diff(
  332. $this->data['extensions_enabled'],
  333. array($ext_name)
  334. );
  335. }
  336. public function addExtension($ext_name) {
  337. $found = array_search($ext_name, $this->data['extensions_enabled']) !== false;
  338. if (!$found) {
  339. $this->data['extensions_enabled'][] = $ext_name;
  340. }
  341. }
  342. }