Configuration.php 7.0 KB

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