Configuration.php 7.2 KB

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