Configuration.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. 'posts_per_page' => 20,
  11. 'view_mode' => 'normal',
  12. 'default_view' => 'not_read',
  13. 'auto_load_more' => true,
  14. 'display_posts' => false,
  15. 'onread_jump_next' => true,
  16. 'lazyload' => true,
  17. 'sort_order' => 'DESC',
  18. 'anon_access' => false,
  19. 'mark_when' => array(
  20. 'article' => true,
  21. 'site' => true,
  22. 'scroll' => false,
  23. 'reception' => false,
  24. ),
  25. 'theme' => 'default',
  26. 'shortcuts' => array(
  27. 'mark_read' => 'r',
  28. 'mark_favorite' => 'f',
  29. 'go_website' => 'space',
  30. 'next_entry' => 'j',
  31. 'prev_entry' => 'k',
  32. 'collapse_entry' => 'c',
  33. 'load_more' => 'm',
  34. 'auto_share' => 's',
  35. ),
  36. 'topline_read' => true,
  37. 'topline_favorite' => true,
  38. 'topline_date' => true,
  39. 'topline_link' => true,
  40. 'bottomline_read' => true,
  41. 'bottomline_favorite' => true,
  42. 'bottomline_sharing' => true,
  43. 'bottomline_tags' => true,
  44. 'bottomline_date' => true,
  45. 'bottomline_link' => true,
  46. 'sharing' => array(
  47. 'shaarli' => '',
  48. 'poche' => '',
  49. 'diaspora' => '',
  50. 'twitter' => true,
  51. 'g+' => true,
  52. 'facebook' => true,
  53. 'email' => true,
  54. 'print' => true,
  55. ),
  56. );
  57. private $available_languages = array(
  58. 'en' => 'English',
  59. 'fr' => 'Français',
  60. );
  61. public function __construct ($user) {
  62. $this->filename = DATA_PATH . '/' . $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. }
  75. public function save() {
  76. @rename($this->filename, $this->filename . '.bak.php');
  77. if (file_put_contents($this->filename, "<?php\n return " . var_export($this->data, true) . ';', LOCK_EX) === false) {
  78. throw new Minz_PermissionDeniedException($this->filename);
  79. }
  80. if (function_exists('opcache_invalidate')) {
  81. opcache_invalidate($this->filename); //Clear PHP 5.5+ cache for include
  82. }
  83. invalidateHttpCache();
  84. return true;
  85. }
  86. public function __get($name) {
  87. if (array_key_exists($name, $this->data)) {
  88. return $this->data[$name];
  89. } else {
  90. $trace = debug_backtrace();
  91. trigger_error('Undefined FreshRSS_Configuration->' . $name . 'in ' . $trace[0]['file'] . ' line ' . $trace[0]['line'], E_USER_NOTICE); //TODO: Use Minz exceptions
  92. return null;
  93. }
  94. }
  95. public function sharing($key = false) {
  96. if ($key === false) {
  97. return $this->data['sharing'];
  98. }
  99. if (isset($this->data['sharing'][$key])) {
  100. return $this->data['sharing'][$key];
  101. }
  102. return false;
  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 === 'all' ? 'all' : '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 _sort_order ($value) {
  137. $this->data['sort_order'] = $value === 'ASC' ? 'ASC' : 'DESC';
  138. }
  139. public function _old_entries($value) {
  140. $value = intval($value);
  141. $this->data['old_entries'] = $value > 0 ? $value : 3;
  142. }
  143. public function _keep_history_default($value) {
  144. $value = intval($value);
  145. $this->data['keep_history_default'] = $value >= -1 ? $value : 0;
  146. }
  147. public function _shortcuts ($values) {
  148. foreach ($values as $key => $value) {
  149. if (isset($this->data['shortcuts'][$key])) {
  150. $this->data['shortcuts'][$key] = $value;
  151. }
  152. }
  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. $are_url = array ('shaarli', 'poche', 'diaspora');
  174. foreach ($values as $key => $value) {
  175. if (in_array($key, $are_url)) {
  176. $is_url = (
  177. filter_var ($value, FILTER_VALIDATE_URL) ||
  178. (version_compare(PHP_VERSION, '5.3.3', '<') &&
  179. (strpos($value, '-') > 0) &&
  180. ($value === filter_var($value, FILTER_SANITIZE_URL)))
  181. ); //PHP bug #51192
  182. if (!$is_url) {
  183. $value = '';
  184. }
  185. } elseif (!is_bool($value)) {
  186. $value = true;
  187. }
  188. $this->data['sharing'][$key] = $value;
  189. }
  190. }
  191. public function _theme($value) {
  192. $this->data['theme'] = $value;
  193. }
  194. public function _token($value) {
  195. $this->data['token'] = $value;
  196. }
  197. public function _auto_load_more($value) {
  198. $this->data['auto_load_more'] = ((bool)$value) && $value !== 'no';
  199. }
  200. public function _topline_read($value) {
  201. $this->data['topline_read'] = ((bool)$value) && $value !== 'no';
  202. }
  203. public function _topline_favorite($value) {
  204. $this->data['topline_favorite'] = ((bool)$value) && $value !== 'no';
  205. }
  206. public function _topline_date($value) {
  207. $this->data['topline_date'] = ((bool)$value) && $value !== 'no';
  208. }
  209. public function _topline_link($value) {
  210. $this->data['topline_link'] = ((bool)$value) && $value !== 'no';
  211. }
  212. public function _bottomline_read($value) {
  213. $this->data['bottomline_read'] = ((bool)$value) && $value !== 'no';
  214. }
  215. public function _bottomline_favorite($value) {
  216. $this->data['bottomline_favorite'] = ((bool)$value) && $value !== 'no';
  217. }
  218. public function _bottomline_sharing($value) {
  219. $this->data['bottomline_sharing'] = ((bool)$value) && $value !== 'no';
  220. }
  221. public function _bottomline_tags($value) {
  222. $this->data['bottomline_tags'] = ((bool)$value) && $value !== 'no';
  223. }
  224. public function _bottomline_date($value) {
  225. $this->data['bottomline_date'] = ((bool)$value) && $value !== 'no';
  226. }
  227. public function _bottomline_link($value) {
  228. $this->data['bottomline_link'] = ((bool)$value) && $value !== 'no';
  229. }
  230. }