Configuration.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. 'ttl_default' => 3600,
  9. 'mail_login' => '',
  10. 'token' => '',
  11. 'passwordHash' => '', //CRYPT_BLOWFISH
  12. 'apiPasswordHash' => '', //CRYPT_BLOWFISH
  13. 'posts_per_page' => 20,
  14. 'view_mode' => 'normal',
  15. 'default_view' => FreshRSS_Entry::STATE_NOT_READ,
  16. 'auto_load_more' => true,
  17. 'display_posts' => false,
  18. 'onread_jump_next' => true,
  19. 'lazyload' => true,
  20. 'sticky_post' => true,
  21. 'sort_order' => 'DESC',
  22. 'anon_access' => false,
  23. 'mark_when' => array(
  24. 'article' => true,
  25. 'site' => true,
  26. 'scroll' => false,
  27. 'reception' => false,
  28. ),
  29. 'theme' => 'Origine',
  30. 'content_width' => 'thin',
  31. 'shortcuts' => array(
  32. 'mark_read' => 'r',
  33. 'mark_favorite' => 'f',
  34. 'go_website' => 'space',
  35. 'next_entry' => 'j',
  36. 'prev_entry' => 'k',
  37. 'first_entry' => 'home',
  38. 'last_entry' => 'end',
  39. 'collapse_entry' => 'c',
  40. 'load_more' => 'm',
  41. 'auto_share' => 's',
  42. 'focus_search' => 'a',
  43. ),
  44. 'topline_read' => true,
  45. 'topline_favorite' => true,
  46. 'topline_date' => true,
  47. 'topline_link' => true,
  48. 'bottomline_read' => true,
  49. 'bottomline_favorite' => true,
  50. 'bottomline_sharing' => true,
  51. 'bottomline_tags' => true,
  52. 'bottomline_date' => true,
  53. 'bottomline_link' => true,
  54. 'sharing' => array(),
  55. 'queries' => 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 === FreshRSS_Entry::STATE_ALL ? FreshRSS_Entry::STATE_ALL : FreshRSS_Entry::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 _ttl_default($value) {
  150. $value = intval($value);
  151. $this->data['ttl_default'] = $value >= -1 ? $value : 3600;
  152. }
  153. public function _shortcuts ($values) {
  154. foreach ($values as $key => $value) {
  155. if (isset($this->data['shortcuts'][$key])) {
  156. $this->data['shortcuts'][$key] = $value;
  157. }
  158. }
  159. }
  160. public function _passwordHash ($value) {
  161. $this->data['passwordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
  162. }
  163. public function _apiPasswordHash ($value) {
  164. $this->data['apiPasswordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
  165. }
  166. public function _mail_login ($value) {
  167. $value = filter_var($value, FILTER_VALIDATE_EMAIL);
  168. if ($value) {
  169. $this->data['mail_login'] = $value;
  170. } else {
  171. $this->data['mail_login'] = '';
  172. }
  173. }
  174. public function _anon_access ($value) {
  175. $this->data['anon_access'] = ((bool)$value) && $value !== 'no';
  176. }
  177. public function _mark_when ($values) {
  178. foreach ($values as $key => $value) {
  179. if (isset($this->data['mark_when'][$key])) {
  180. $this->data['mark_when'][$key] = ((bool)$value) && $value !== 'no';
  181. }
  182. }
  183. }
  184. public function _sharing ($values) {
  185. $this->data['sharing'] = array();
  186. foreach ($values as $value) {
  187. if (!is_array($value)) {
  188. continue;
  189. }
  190. // Verify URL and add default value when needed
  191. if (isset($value['url'])) {
  192. $is_url = (
  193. filter_var ($value['url'], FILTER_VALIDATE_URL) ||
  194. (version_compare(PHP_VERSION, '5.3.3', '<') &&
  195. (strpos($value, '-') > 0) &&
  196. ($value === filter_var($value, FILTER_SANITIZE_URL)))
  197. ); //PHP bug #51192
  198. if (!$is_url) {
  199. continue;
  200. }
  201. } else {
  202. $value['url'] = null;
  203. }
  204. // Add a default name
  205. if (empty($value['name'])) {
  206. $value['name'] = $value['type'];
  207. }
  208. $this->data['sharing'][] = $value;
  209. }
  210. }
  211. public function _queries ($values) {
  212. $this->data['queries'] = array();
  213. foreach ($values as $value) {
  214. $value = array_filter($value);
  215. $params = $value;
  216. unset($params['name']);
  217. unset($params['url']);
  218. $value['url'] = Minz_Url::display(array('params' => $params));
  219. $this->data['queries'][] = $value;
  220. }
  221. }
  222. public function _theme($value) {
  223. $this->data['theme'] = $value;
  224. }
  225. public function _content_width($value) {
  226. if ($value === 'medium' ||
  227. $value === 'large' ||
  228. $value === 'no_limit') {
  229. $this->data['content_width'] = $value;
  230. } else {
  231. $this->data['content_width'] = 'thin';
  232. }
  233. }
  234. public function _token($value) {
  235. $this->data['token'] = $value;
  236. }
  237. public function _auto_load_more($value) {
  238. $this->data['auto_load_more'] = ((bool)$value) && $value !== 'no';
  239. }
  240. public function _topline_read($value) {
  241. $this->data['topline_read'] = ((bool)$value) && $value !== 'no';
  242. }
  243. public function _topline_favorite($value) {
  244. $this->data['topline_favorite'] = ((bool)$value) && $value !== 'no';
  245. }
  246. public function _topline_date($value) {
  247. $this->data['topline_date'] = ((bool)$value) && $value !== 'no';
  248. }
  249. public function _topline_link($value) {
  250. $this->data['topline_link'] = ((bool)$value) && $value !== 'no';
  251. }
  252. public function _bottomline_read($value) {
  253. $this->data['bottomline_read'] = ((bool)$value) && $value !== 'no';
  254. }
  255. public function _bottomline_favorite($value) {
  256. $this->data['bottomline_favorite'] = ((bool)$value) && $value !== 'no';
  257. }
  258. public function _bottomline_sharing($value) {
  259. $this->data['bottomline_sharing'] = ((bool)$value) && $value !== 'no';
  260. }
  261. public function _bottomline_tags($value) {
  262. $this->data['bottomline_tags'] = ((bool)$value) && $value !== 'no';
  263. }
  264. public function _bottomline_date($value) {
  265. $this->data['bottomline_date'] = ((bool)$value) && $value !== 'no';
  266. }
  267. public function _bottomline_link($value) {
  268. $this->data['bottomline_link'] = ((bool)$value) && $value !== 'no';
  269. }
  270. }