Configuration.php 7.5 KB

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