Configuration.php 7.9 KB

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