Configuration.php 8.4 KB

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