Configuration.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. if (file_put_contents($this->filename, "<?php\n return " . var_export($array, true) . ';', LOCK_EX) === false) {
  76. throw new Minz_PermissionDeniedException($this->filename);
  77. }
  78. if (function_exists('opcache_invalidate')) {
  79. opcache_invalidate($this->filename); //Clear PHP 5.5+ cache for include
  80. }
  81. invalidateHttpCache();
  82. return true;
  83. }
  84. public function __get($name) {
  85. if (array_key_exists($name, $this->data)) {
  86. return $this->data[$name];
  87. } else {
  88. $trace = debug_backtrace();
  89. trigger_error('Undefined FreshRSS_Configuration->' . $name . 'in ' . $trace[0]['file'] . ' line ' . $trace[0]['line'], E_USER_NOTICE); //TODO: Use Minz exceptions
  90. return null;
  91. }
  92. }
  93. public function sharing($key = false) {
  94. if ($key === false) {
  95. return $this->data['sharing'];
  96. }
  97. if (isset($this->data['sharing'][$key])) {
  98. return $this->data['sharing'][$key];
  99. }
  100. return false;
  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 === 'all' ? 'all' : '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 _sort_order ($value) {
  135. $this->data['sort_order'] = $value === 'ASC' ? 'ASC' : 'DESC';
  136. }
  137. public function _old_entries($value) {
  138. $value = intval($value);
  139. $this->data['old_entries'] = $value > 0 ? $value : 3;
  140. }
  141. public function _keep_history_default($value) {
  142. $value = intval($value);
  143. $this->data['keep_history_default'] = $value >= -1 ? $value : 0;
  144. }
  145. public function _shortcuts ($values) {
  146. foreach ($values as $key => $value) {
  147. if (isset($this->data['shortcuts'][$key])) {
  148. $this->data['shortcuts'][$key] = $value;
  149. }
  150. }
  151. }
  152. public function _mail_login ($value) {
  153. $value = filter_var($value, FILTER_VALIDATE_EMAIL);
  154. if ($value) {
  155. $this->data['mail_login'] = $value;
  156. } else {
  157. $this->data['mail_login'] = '';
  158. }
  159. }
  160. public function _anon_access ($value) {
  161. $this->data['anon_access'] = ((bool)$value) && $value !== 'no';
  162. }
  163. public function _mark_when ($values) {
  164. foreach ($values as $key => $value) {
  165. if (isset($this->data['mark_when'][$key])) {
  166. $this->data['mark_when'][$key] = ((bool)$value) && $value !== 'no';
  167. }
  168. }
  169. }
  170. public function _sharing ($values) {
  171. $are_url = array ('shaarli', 'poche', 'diaspora');
  172. foreach ($values as $key => $value) {
  173. if (in_array($key, $are_url)) {
  174. $is_url = (
  175. filter_var ($value, FILTER_VALIDATE_URL) ||
  176. (version_compare(PHP_VERSION, '5.3.3', '<') &&
  177. (strpos($value, '-') > 0) &&
  178. ($value === filter_var($value, FILTER_SANITIZE_URL)))
  179. ); //PHP bug #51192
  180. if (!$is_url) {
  181. $value = '';
  182. }
  183. } elseif (!is_bool($value)) {
  184. $value = true;
  185. }
  186. $this->data['sharing'][$key] = $value;
  187. }
  188. }
  189. public function _theme($value) {
  190. $this->data['theme'] = $value;
  191. }
  192. public function _token($value) {
  193. $this->data['token'] = $value;
  194. }
  195. public function _auto_load_more($value) {
  196. $this->data['auto_load_more'] = ((bool)$value) && $value !== 'no';
  197. }
  198. public function _topline_read($value) {
  199. $this->data['topline_read'] = ((bool)$value) && $value !== 'no';
  200. }
  201. public function _topline_favorite($value) {
  202. $this->data['topline_favorite'] = ((bool)$value) && $value !== 'no';
  203. }
  204. public function _topline_date($value) {
  205. $this->data['topline_date'] = ((bool)$value) && $value !== 'no';
  206. }
  207. public function _topline_link($value) {
  208. $this->data['topline_link'] = ((bool)$value) && $value !== 'no';
  209. }
  210. public function _bottomline_read($value) {
  211. $this->data['bottomline_read'] = ((bool)$value) && $value !== 'no';
  212. }
  213. public function _bottomline_favorite($value) {
  214. $this->data['bottomline_favorite'] = ((bool)$value) && $value !== 'no';
  215. }
  216. public function _bottomline_sharing($value) {
  217. $this->data['bottomline_sharing'] = ((bool)$value) && $value !== 'no';
  218. }
  219. public function _bottomline_tags($value) {
  220. $this->data['bottomline_tags'] = ((bool)$value) && $value !== 'no';
  221. }
  222. public function _bottomline_date($value) {
  223. $this->data['bottomline_date'] = ((bool)$value) && $value !== 'no';
  224. }
  225. public function _bottomline_link($value) {
  226. $this->data['bottomline_link'] = ((bool)$value) && $value !== 'no';
  227. }
  228. }