Configuration.php 7.5 KB

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