Configuration.php 7.6 KB

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