Configuration.php 9.8 KB

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