Configuration.php 6.7 KB

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