Configuration.php 9.5 KB

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