Configuration.php 7.5 KB

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