Configuration.php 7.7 KB

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