Configuration.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. 'sort_order' => 'DESC',
  19. 'anon_access' => false,
  20. 'mark_when' => array(
  21. 'article' => true,
  22. 'site' => true,
  23. 'scroll' => false,
  24. 'reception' => false,
  25. ),
  26. 'theme' => 'Origine',
  27. 'shortcuts' => array(
  28. 'mark_read' => 'r',
  29. 'mark_favorite' => 'f',
  30. 'go_website' => 'space',
  31. 'next_entry' => 'j',
  32. 'prev_entry' => 'k',
  33. 'collapse_entry' => 'c',
  34. 'load_more' => 'm',
  35. 'auto_share' => 's',
  36. ),
  37. 'topline_read' => true,
  38. 'topline_favorite' => true,
  39. 'topline_date' => true,
  40. 'topline_link' => true,
  41. 'bottomline_read' => true,
  42. 'bottomline_favorite' => true,
  43. 'bottomline_sharing' => true,
  44. 'bottomline_tags' => true,
  45. 'bottomline_date' => true,
  46. 'bottomline_link' => true,
  47. 'sharing' => array(
  48. 'shaarli' => '',
  49. 'wallabag' => '',
  50. 'diaspora' => '',
  51. 'twitter' => true,
  52. 'g+' => true,
  53. 'facebook' => true,
  54. 'email' => true,
  55. 'print' => true,
  56. ),
  57. );
  58. private $available_languages = array(
  59. 'en' => 'English',
  60. 'fr' => 'Français',
  61. );
  62. public function __construct ($user) {
  63. $this->filename = DATA_PATH . '/' . $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. }
  76. public function save() {
  77. @rename($this->filename, $this->filename . '.bak.php');
  78. if (file_put_contents($this->filename, "<?php\n return " . var_export($this->data, true) . ';', LOCK_EX) === false) {
  79. throw new Minz_PermissionDeniedException($this->filename);
  80. }
  81. if (function_exists('opcache_invalidate')) {
  82. opcache_invalidate($this->filename); //Clear PHP 5.5+ cache for include
  83. }
  84. invalidateHttpCache();
  85. return true;
  86. }
  87. public function __get($name) {
  88. if (array_key_exists($name, $this->data)) {
  89. return $this->data[$name];
  90. } else {
  91. $trace = debug_backtrace();
  92. trigger_error('Undefined FreshRSS_Configuration->' . $name . 'in ' . $trace[0]['file'] . ' line ' . $trace[0]['line'], E_USER_NOTICE); //TODO: Use Minz exceptions
  93. return null;
  94. }
  95. }
  96. public function sharing($key = false) {
  97. if ($key === false) {
  98. return $this->data['sharing'];
  99. }
  100. if (isset($this->data['sharing'][$key])) {
  101. return $this->data['sharing'][$key];
  102. }
  103. return false;
  104. }
  105. public function availableLanguages() {
  106. return $this->available_languages;
  107. }
  108. public function _language($value) {
  109. if (!isset($this->available_languages[$value])) {
  110. $value = 'en';
  111. }
  112. $this->data['language'] = $value;
  113. }
  114. public function _posts_per_page ($value) {
  115. $value = intval($value);
  116. $this->data['posts_per_page'] = $value > 0 ? $value : 10;
  117. }
  118. public function _view_mode ($value) {
  119. if ($value === 'global' || $value === 'reader') {
  120. $this->data['view_mode'] = $value;
  121. } else {
  122. $this->data['view_mode'] = 'normal';
  123. }
  124. }
  125. public function _default_view ($value) {
  126. $this->data['default_view'] = $value === 'all' ? 'all' : 'not_read';
  127. }
  128. public function _display_posts ($value) {
  129. $this->data['display_posts'] = ((bool)$value) && $value !== 'no';
  130. }
  131. public function _onread_jump_next ($value) {
  132. $this->data['onread_jump_next'] = ((bool)$value) && $value !== 'no';
  133. }
  134. public function _lazyload ($value) {
  135. $this->data['lazyload'] = ((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 _mail_login ($value) {
  159. $value = filter_var($value, FILTER_VALIDATE_EMAIL);
  160. if ($value) {
  161. $this->data['mail_login'] = $value;
  162. } else {
  163. $this->data['mail_login'] = '';
  164. }
  165. }
  166. public function _anon_access ($value) {
  167. $this->data['anon_access'] = ((bool)$value) && $value !== 'no';
  168. }
  169. public function _mark_when ($values) {
  170. foreach ($values as $key => $value) {
  171. if (isset($this->data['mark_when'][$key])) {
  172. $this->data['mark_when'][$key] = ((bool)$value) && $value !== 'no';
  173. }
  174. }
  175. }
  176. public function _sharing ($values) {
  177. $are_url = array ('shaarli', 'wallabag', 'diaspora');
  178. foreach ($values as $key => $value) {
  179. if (in_array($key, $are_url)) {
  180. $is_url = (
  181. filter_var ($value, FILTER_VALIDATE_URL) ||
  182. (version_compare(PHP_VERSION, '5.3.3', '<') &&
  183. (strpos($value, '-') > 0) &&
  184. ($value === filter_var($value, FILTER_SANITIZE_URL)))
  185. ); //PHP bug #51192
  186. if (!$is_url) {
  187. $value = '';
  188. }
  189. } elseif (!is_bool($value)) {
  190. $value = true;
  191. }
  192. $this->data['sharing'][$key] = $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. }