aboutsummaryrefslogtreecommitdiff
path: root/app/Models/Configuration.php
blob: 8668470b08234f192e6540963876d59bd7dd2174 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php

class FreshRSS_Configuration {
	private $filename;

	private $data = array(
		'language' => 'en',
		'old_entries' => 3,
		'keep_history_default' => 0,
		'ttl_default' => 3600,
		'mail_login' => '',
		'token' => '',
		'passwordHash' => '',	//CRYPT_BLOWFISH
		'apiPasswordHash' => '',	//CRYPT_BLOWFISH
		'posts_per_page' => 20,
		'view_mode' => 'normal',
		'default_view' => 'adaptive',
		'default_state' => FreshRSS_Entry::STATE_NOT_READ,
		'auto_load_more' => true,
		'display_posts' => false,
		'display_categories' => false,
		'hide_read_feeds' => true,
		'onread_jump_next' => true,
		'lazyload' => true,
		'sticky_post' => true,
		'reading_confirm' => false,
		'auto_remove_article' => false,
		'sort_order' => 'DESC',
		'anon_access' => false,
		'mark_when' => array(
			'article' => true,
			'site' => true,
			'scroll' => false,
			'reception' => false,
		),
		'theme' => 'Origine',
		'content_width' => 'thin',
		'shortcuts' => array(
			'mark_read' => 'r',
			'mark_favorite' => 'f',
			'go_website' => 'space',
			'next_entry' => 'j',
			'prev_entry' => 'k',
			'first_entry' => 'home',
			'last_entry' => 'end',
			'collapse_entry' => 'c',
			'load_more' => 'm',
			'auto_share' => 's',
			'focus_search' => 'a',
			'user_filter' => 'u',
			'help' => 'f1',
			'close_dropdown' => 'escape',
		),
		'topline_read' => true,
		'topline_favorite' => true,
		'topline_date' => true,
		'topline_link' => true,
		'bottomline_read' => true,
		'bottomline_favorite' => true,
		'bottomline_sharing' => true,
		'bottomline_tags' => true,
		'bottomline_date' => true,
		'bottomline_link' => true,
		'sharing' => array(),
		'queries' => array(),
		'html5_notif_timeout' => 0,
	);

	private $available_languages = array(
		'en' => 'English',
		'fr' => 'Français',
	);

	private $shares;

	public function __construct($user) {
		$this->filename = DATA_PATH . DIRECTORY_SEPARATOR . $user . '_user.php';

		$data = @include($this->filename);
		if (!is_array($data)) {
			throw new Minz_PermissionDeniedException($this->filename);
		}

		foreach ($data as $key => $value) {
			if (isset($this->data[$key])) {
				$function = '_' . $key;
				$this->$function($value);
			}
		}
		$this->data['user'] = $user;

		$this->shares = DATA_PATH . DIRECTORY_SEPARATOR . 'shares.php';

		$shares = @include($this->shares);
		if (!is_array($shares)) {
			throw new Minz_PermissionDeniedException($this->shares);
		}

		$this->data['shares'] = $shares;
	}

	public function save() {
		@rename($this->filename, $this->filename . '.bak.php');
		unset($this->data['shares']); // Remove shares because it is not intended to be stored in user configuration
		if (file_put_contents($this->filename, "<?php\n return " . var_export($this->data, true) . ';', LOCK_EX) === false) {
			throw new Minz_PermissionDeniedException($this->filename);
		}
		if (function_exists('opcache_invalidate')) {
			opcache_invalidate($this->filename);	//Clear PHP 5.5+ cache for include
		}
		invalidateHttpCache();
		return true;
	}

	public function __get($name) {
		if (array_key_exists($name, $this->data)) {
			return $this->data[$name];
		} else {
			$trace = debug_backtrace();
			trigger_error('Undefined FreshRSS_Configuration->' . $name . 'in ' . $trace[0]['file'] . ' line ' . $trace[0]['line'], E_USER_NOTICE);	//TODO: Use Minz exceptions
			return null;
		}
	}

	public function availableLanguages() {
		return $this->available_languages;
	}

	public function remove_query_by_get($get) {
		$final_queries = array();
		foreach ($this->queries as $key => $query) {
			if (empty($query['get']) || $query['get'] !== $get) {
				$final_queries[$key] = $query;
			}
		}
		$this->_queries($final_queries);
	}

	public function _language($value) {
		if (!isset($this->available_languages[$value])) {
			$value = 'en';
		}
		$this->data['language'] = $value;
	}
	public function _posts_per_page($value) {
		$value = intval($value);
		$this->data['posts_per_page'] = $value > 0 ? $value : 10;
	}
	public function _view_mode($value) {
		if ($value === 'global' || $value === 'reader') {
			$this->data['view_mode'] = $value;
		} else {
			$this->data['view_mode'] = 'normal';
		}
	}
	public function _default_view($value) {
		switch ($value) {
		case 'all':
			$this->data['default_view'] = $value;
			$this->data['default_state'] = (FreshRSS_Entry::STATE_READ +
			                                FreshRSS_Entry::STATE_NOT_READ);
			break;
		case 'adaptive':
		case 'unread':
		default:
			$this->data['default_view'] = $value;
			$this->data['default_state'] = FreshRSS_Entry::STATE_NOT_READ;
		}
	}
	public function _default_state($value) {
		$this->data['default_state'] = (int)$value;
	}

	public function _display_posts($value) {
		$this->data['display_posts'] = ((bool)$value) && $value !== 'no';
	}
	public function _display_categories($value) {
		$this->data['display_categories'] = ((bool)$value) && $value !== 'no';
	}
	public function _hide_read_feeds($value) {
		$this->data['hide_read_feeds'] = (bool)$value;
	}
	public function _onread_jump_next($value) {
		$this->data['onread_jump_next'] = ((bool)$value) && $value !== 'no';
	}
	public function _lazyload($value) {
		$this->data['lazyload'] = ((bool)$value) && $value !== 'no';
	}
	public function _sticky_post($value) {
		$this->data['sticky_post'] = ((bool)$value) && $value !== 'no';
	}
	public function _reading_confirm($value) {
		$this->data['reading_confirm'] = ((bool)$value) && $value !== 'no';
	}
	public function _auto_remove_article($value) {
		$this->data['auto_remove_article'] = ((bool)$value) && $value !== 'no';
	}
	public function _sort_order($value) {
		$this->data['sort_order'] = $value === 'ASC' ? 'ASC' : 'DESC';
	}
	public function _old_entries($value) {
		$value = intval($value);
		$this->data['old_entries'] = $value > 0 ? $value : 3;
	}
	public function _keep_history_default($value) {
		$value = intval($value);
		$this->data['keep_history_default'] = $value >= -1 ? $value : 0;
	}
	public function _ttl_default($value) {
		$value = intval($value);
		$this->data['ttl_default'] = $value >= -1 ? $value : 3600;
	}
	public function _shortcuts($values) {
		foreach ($values as $key => $value) {
			if (isset($this->data['shortcuts'][$key])) {
				$this->data['shortcuts'][$key] = $value;
			}
		}
	}
	public function _passwordHash($value) {
		$this->data['passwordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
	}
	public function _apiPasswordHash($value) {
		$this->data['apiPasswordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
	}
	public function _mail_login($value) {
		$value = filter_var($value, FILTER_VALIDATE_EMAIL);
		if ($value) {
			$this->data['mail_login'] = $value;
		} else {
			$this->data['mail_login'] = '';
		}
	}
	public function _anon_access($value) {
		$this->data['anon_access'] = ((bool)$value) && $value !== 'no';
	}
	public function _mark_when($values) {
		foreach ($values as $key => $value) {
			if (isset($this->data['mark_when'][$key])) {
				$this->data['mark_when'][$key] = ((bool)$value) && $value !== 'no';
			}
		}
	}
	public function _sharing($values) {
		$this->data['sharing'] = array();
		$unique = array();
		foreach ($values as $value) {
			if (!is_array($value)) {
				continue;
			}

			// Verify URL and add default value when needed
			if (isset($value['url'])) {
				$is_url = (
					filter_var($value['url'], FILTER_VALIDATE_URL) ||
					(version_compare(PHP_VERSION, '5.3.3', '<') &&
						(strpos($value, '-') > 0) &&
						($value === filter_var($value, FILTER_SANITIZE_URL)))
				);  //PHP bug #51192
				if (!$is_url) {
					continue;
				}
			} else {
				$value['url'] = null;
			}

			// Add a default name
			if (empty($value['name'])) {
				$value['name'] = $value['type'];
			}

			$json_value = json_encode($value);
			if (!in_array($json_value, $unique)) {
				$unique[] = $json_value;
				$this->data['sharing'][] = $value;
			}
		}
	}
	public function _queries($values) {
		$this->data['queries'] = array();
		foreach ($values as $value) {
			$value = array_filter($value);
			$params = $value;
			unset($params['name']);
			unset($params['url']);
			$value['url'] = Minz_Url::display(array('params' => $params));

			$this->data['queries'][] = $value;
		}
	}
	public function _theme($value) {
		$this->data['theme'] = $value;
	}
	public function _content_width($value) {
		if ($value === 'medium' ||
				$value === 'large' ||
				$value === 'no_limit') {
			$this->data['content_width'] = $value;
		} else {
			$this->data['content_width'] = 'thin';
		}
	}
	
	public function _html5_notif_timeout($value) {
		$value = intval($value);
		$this->data['html5_notif_timeout'] = $value >= 0 ? $value : 0;
	}
	
	public function _token($value) {
		$this->data['token'] = $value;
	}
	public function _auto_load_more($value) {
		$this->data['auto_load_more'] = ((bool)$value) && $value !== 'no';
	}
	public function _topline_read($value) {
		$this->data['topline_read'] = ((bool)$value) && $value !== 'no';
	}
	public function _topline_favorite($value) {
		$this->data['topline_favorite'] = ((bool)$value) && $value !== 'no';
	}
	public function _topline_date($value) {
		$this->data['topline_date'] = ((bool)$value) && $value !== 'no';
	}
	public function _topline_link($value) {
		$this->data['topline_link'] = ((bool)$value) && $value !== 'no';
	}
	public function _bottomline_read($value) {
		$this->data['bottomline_read'] = ((bool)$value) && $value !== 'no';
	}
	public function _bottomline_favorite($value) {
		$this->data['bottomline_favorite'] = ((bool)$value) && $value !== 'no';
	}
	public function _bottomline_sharing($value) {
		$this->data['bottomline_sharing'] = ((bool)$value) && $value !== 'no';
	}
	public function _bottomline_tags($value) {
		$this->data['bottomline_tags'] = ((bool)$value) && $value !== 'no';
	}
	public function _bottomline_date($value) {
		$this->data['bottomline_date'] = ((bool)$value) && $value !== 'no';
	}
	public function _bottomline_link($value) {
		$this->data['bottomline_link'] = ((bool)$value) && $value !== 'no';
	}
}