| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2021-04-26 20:18:20 +02:00
										 |  |  | # lint: pylint | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  | """Searx preferences implementation.
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # pylint: disable=useless-object-inheritance | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 23:56:37 +02:00
										 |  |  | from base64 import urlsafe_b64encode, urlsafe_b64decode | 
					
						
							|  |  |  | from zlib import compress, decompress | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  | from urllib.parse import parse_qs, urlencode | 
					
						
							| 
									
										
										
										
											2023-01-30 18:40:22 +01:00
										 |  |  | from typing import Iterable, Dict, List, Optional | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import flask | 
					
						
							| 
									
										
										
										
											2023-01-30 18:40:22 +01:00
										 |  |  | import babel | 
					
						
							| 
									
										
										
										
											2017-10-25 23:56:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | from searx import settings, autocomplete | 
					
						
							| 
									
										
										
										
											2022-09-29 20:54:46 +02:00
										 |  |  | from searx.enginelib import Engine | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | from searx.plugins import Plugin | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  | from searx.locales import LOCALE_NAMES | 
					
						
							| 
									
										
										
										
											2020-10-01 11:29:31 +02:00
										 |  |  | from searx.webutils import VALID_LANGUAGE_CODE | 
					
						
							| 
									
										
										
										
											2022-07-24 09:32:05 +02:00
										 |  |  | from searx.engines import DEFAULT_CATEGORY | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5  # 5 years | 
					
						
							| 
									
										
										
										
											2017-11-01 13:58:48 +01:00
										 |  |  | DOI_RESOLVERS = list(settings['doi_resolvers']) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ValidationException(Exception): | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     """Exption from ``cls.__init__`` when configuration value is invalid.""" | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-12 09:42:27 +02:00
										 |  |  | class Setting: | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |     """Base class of user settings""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def __init__(self, default_value, locked: bool = False): | 
					
						
							| 
									
										
										
										
											2020-08-27 14:38:39 +02:00
										 |  |  |         super().__init__() | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         self.value = default_value | 
					
						
							| 
									
										
										
										
											2020-10-23 16:22:55 +02:00
										 |  |  |         self.locked = locked | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def parse(self, data: str): | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |         """Parse ``data`` and store the result at ``self.value``
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         If needed, its overwritten in the inheritance. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         self.value = data | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def get_value(self): | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |         """Returns the value of the setting
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         If needed, its overwritten in the inheritance. | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         return self.value | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def save(self, name: str, resp: flask.Response): | 
					
						
							| 
									
										
										
										
											2022-09-27 17:01:00 +02:00
										 |  |  |         """Save cookie ``name`` in the HTTP response object
 | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         If needed, its overwritten in the inheritance."""
 | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  |         resp.set_cookie(name, self.value, max_age=COOKIE_MAX_AGE) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class StringSetting(Setting): | 
					
						
							|  |  |  |     """Setting of plain string values""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class EnumStringSetting(Setting): | 
					
						
							|  |  |  |     """Setting of a value which can only come from the given choices""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def __init__(self, default_value: str, choices: Iterable[str], locked=False): | 
					
						
							|  |  |  |         super().__init__(default_value, locked) | 
					
						
							|  |  |  |         self.choices = choices | 
					
						
							| 
									
										
										
										
											2016-11-14 22:24:40 +01:00
										 |  |  |         self._validate_selection(self.value) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def _validate_selection(self, selection: str): | 
					
						
							|  |  |  |         if selection not in self.choices: | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |             raise ValidationException('Invalid value: "{0}"'.format(selection)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def parse(self, data: str): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         """Parse and validate ``data`` and store the result at ``self.value``""" | 
					
						
							| 
									
										
										
										
											2016-12-14 03:55:56 +01:00
										 |  |  |         self._validate_selection(data) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         self.value = data | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | class MultipleChoiceSetting(Setting): | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |     """Setting of values which can only come from the given choices""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def __init__(self, default_value: List[str], choices: Iterable[str], locked=False): | 
					
						
							|  |  |  |         super().__init__(default_value, locked) | 
					
						
							|  |  |  |         self.choices = choices | 
					
						
							|  |  |  |         self._validate_selections(self.value) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _validate_selections(self, selections: List[str]): | 
					
						
							| 
									
										
										
										
											2016-11-14 22:24:40 +01:00
										 |  |  |         for item in selections: | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |             if item not in self.choices: | 
					
						
							| 
									
										
										
										
											2016-11-14 22:24:40 +01:00
										 |  |  |                 raise ValidationException('Invalid value: "{0}"'.format(selections)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def parse(self, data: str): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         """Parse and validate ``data`` and store the result at ``self.value``""" | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         if data == '': | 
					
						
							|  |  |  |             self.value = [] | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         elements = data.split(',') | 
					
						
							| 
									
										
										
										
											2016-11-14 22:24:40 +01:00
										 |  |  |         self._validate_selections(elements) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         self.value = elements | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def parse_form(self, data: List[str]): | 
					
						
							| 
									
										
										
										
											2020-10-23 16:22:55 +02:00
										 |  |  |         if self.locked: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         self.value = [] | 
					
						
							|  |  |  |         for choice in data: | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |             if choice in self.choices and choice not in self.value: | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |                 self.value.append(choice) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def save(self, name: str, resp: flask.Response): | 
					
						
							| 
									
										
										
										
											2022-09-27 17:01:00 +02:00
										 |  |  |         """Save cookie ``name`` in the HTTP response object""" | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         resp.set_cookie(name, ','.join(self.value), max_age=COOKIE_MAX_AGE) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  | class SetSetting(Setting): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     """Setting of values of type ``set`` (comma separated string)""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def __init__(self, *args, **kwargs): | 
					
						
							|  |  |  |         super().__init__(*args, **kwargs) | 
					
						
							|  |  |  |         self.values = set() | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def get_value(self): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         """Returns a string with comma separated values.""" | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  |         return ','.join(self.values) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def parse(self, data: str): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         """Parse and validate ``data`` and store the result at ``self.value``""" | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  |         if data == '': | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |             self.values = set() | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         elements = data.split(',') | 
					
						
							|  |  |  |         for element in elements: | 
					
						
							|  |  |  |             self.values.add(element) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def parse_form(self, data: str): | 
					
						
							| 
									
										
										
										
											2020-10-23 16:22:55 +02:00
										 |  |  |         if self.locked: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  |         elements = data.split(',') | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |         self.values = set(elements) | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def save(self, name: str, resp: flask.Response): | 
					
						
							| 
									
										
										
										
											2022-09-27 17:01:00 +02:00
										 |  |  |         """Save cookie ``name`` in the HTTP response object""" | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  |         resp.set_cookie(name, ','.join(self.values), max_age=COOKIE_MAX_AGE) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-14 03:55:56 +01:00
										 |  |  | class SearchLanguageSetting(EnumStringSetting): | 
					
						
							|  |  |  |     """Available choices may change, so user's value may not be in choices anymore""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-23 10:03:42 +01:00
										 |  |  |     def _validate_selection(self, selection): | 
					
						
							| 
									
										
										
										
											2022-12-16 21:28:57 +01:00
										 |  |  |         if selection != '' and selection != 'auto' and not VALID_LANGUAGE_CODE.match(selection): | 
					
						
							| 
									
										
										
										
											2020-02-23 10:03:42 +01:00
										 |  |  |             raise ValidationException('Invalid language code: "{0}"'.format(selection)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def parse(self, data: str): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         """Parse and validate ``data`` and store the result at ``self.value``""" | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |         if data not in self.choices and data != self.value: | 
					
						
							| 
									
										
										
										
											2016-12-14 03:55:56 +01:00
										 |  |  |             # hack to give some backwards compatibility with old language cookies | 
					
						
							|  |  |  |             data = str(data).replace('_', '-') | 
					
						
							| 
									
										
										
										
											2021-07-03 17:51:39 +02:00
										 |  |  |             lang = data.split('-', maxsplit=1)[0] | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-14 03:55:56 +01:00
										 |  |  |             if data in self.choices: | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  |             elif lang in self.choices: | 
					
						
							|  |  |  |                 data = lang | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2016-12-14 06:51:15 +01:00
										 |  |  |                 data = self.value | 
					
						
							| 
									
										
										
										
											2020-10-01 11:29:31 +02:00
										 |  |  |         self._validate_selection(data) | 
					
						
							| 
									
										
										
										
											2016-12-14 03:55:56 +01:00
										 |  |  |         self.value = data | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | class MapSetting(Setting): | 
					
						
							|  |  |  |     """Setting of a value that has to be translated in order to be storable""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def __init__(self, default_value, map: Dict[str, object], locked=False):  # pylint: disable=redefined-builtin | 
					
						
							|  |  |  |         super().__init__(default_value, locked) | 
					
						
							|  |  |  |         self.map = map | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if self.value not in self.map.values(): | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |             raise ValidationException('Invalid default value') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def parse(self, data: str): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         """Parse and validate ``data`` and store the result at ``self.value``""" | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         if data not in self.map: | 
					
						
							|  |  |  |             raise ValidationException('Invalid choice: {0}'.format(data)) | 
					
						
							|  |  |  |         self.value = self.map[data] | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |         self.key = data  # pylint: disable=attribute-defined-outside-init | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def save(self, name: str, resp: flask.Response): | 
					
						
							| 
									
										
										
										
											2022-09-27 17:01:00 +02:00
										 |  |  |         """Save cookie ``name`` in the HTTP response object""" | 
					
						
							| 
									
										
										
										
											2017-01-21 20:08:08 +01:00
										 |  |  |         if hasattr(self, 'key'): | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  |             resp.set_cookie(name, self.key, max_age=COOKIE_MAX_AGE) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 18:45:50 +01:00
										 |  |  | class BooleanChoices: | 
					
						
							|  |  |  |     """Maps strings to booleans that are either true or false.""" | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 18:45:50 +01:00
										 |  |  |     def __init__(self, name: str, choices: Dict[str, bool], locked: bool = False): | 
					
						
							| 
									
										
										
										
											2022-01-04 14:57:39 +01:00
										 |  |  |         self.name = name | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |         self.choices = choices | 
					
						
							| 
									
										
										
										
											2022-01-06 18:45:50 +01:00
										 |  |  |         self.locked = locked | 
					
						
							| 
									
										
										
										
											2022-01-13 19:39:18 +01:00
										 |  |  |         self.default_choices = dict(choices) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-07 13:34:35 +02:00
										 |  |  |     def transform_form_items(self, items): | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         return items | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-07 13:34:35 +02:00
										 |  |  |     def transform_values(self, values): | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         return values | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 14:14:37 +01:00
										 |  |  |     def parse_cookie(self, data_disabled: str, data_enabled: str): | 
					
						
							| 
									
										
										
										
											2022-01-06 18:45:50 +01:00
										 |  |  |         for disabled in data_disabled.split(','): | 
					
						
							|  |  |  |             if disabled in self.choices: | 
					
						
							|  |  |  |                 self.choices[disabled] = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for enabled in data_enabled.split(','): | 
					
						
							|  |  |  |             if enabled in self.choices: | 
					
						
							|  |  |  |                 self.choices[enabled] = True | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def parse_form(self, items: List[str]): | 
					
						
							| 
									
										
										
										
											2020-10-23 16:22:55 +02:00
										 |  |  |         if self.locked: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 18:45:50 +01:00
										 |  |  |         disabled = self.transform_form_items(items) | 
					
						
							|  |  |  |         for setting in self.choices: | 
					
						
							|  |  |  |             self.choices[setting] = setting not in disabled | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def enabled(self): | 
					
						
							|  |  |  |         return (k for k, v in self.choices.items() if v) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def disabled(self): | 
					
						
							|  |  |  |         return (k for k, v in self.choices.items() if not v) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 14:57:39 +01:00
										 |  |  |     def save(self, resp: flask.Response): | 
					
						
							| 
									
										
										
										
											2022-09-27 17:01:00 +02:00
										 |  |  |         """Save cookie in the HTTP response object""" | 
					
						
							| 
									
										
										
										
											2022-01-13 19:39:18 +01:00
										 |  |  |         disabled_changed = (k for k in self.disabled if self.default_choices[k]) | 
					
						
							|  |  |  |         enabled_changed = (k for k in self.enabled if not self.default_choices[k]) | 
					
						
							|  |  |  |         resp.set_cookie('disabled_{0}'.format(self.name), ','.join(disabled_changed), max_age=COOKIE_MAX_AGE) | 
					
						
							|  |  |  |         resp.set_cookie('enabled_{0}'.format(self.name), ','.join(enabled_changed), max_age=COOKIE_MAX_AGE) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-07 13:34:35 +02:00
										 |  |  |     def get_disabled(self): | 
					
						
							| 
									
										
										
										
											2022-01-06 18:45:50 +01:00
										 |  |  |         return self.transform_values(list(self.disabled)) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-07 13:34:35 +02:00
										 |  |  |     def get_enabled(self): | 
					
						
							| 
									
										
										
										
											2022-01-06 18:45:50 +01:00
										 |  |  |         return self.transform_values(list(self.enabled)) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 18:45:50 +01:00
										 |  |  | class EnginesSetting(BooleanChoices): | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |     """Engine settings""" | 
					
						
							| 
									
										
										
										
											2016-07-10 16:44:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def __init__(self, default_value, engines: Iterable[Engine]): | 
					
						
							| 
									
										
										
										
											2022-01-06 18:45:50 +01:00
										 |  |  |         choices = {} | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |         for engine in engines: | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |             for category in engine.categories: | 
					
						
							| 
									
										
										
										
											2022-07-24 09:32:05 +02:00
										 |  |  |                 if not category in list(settings['categories_as_tabs'].keys()) + [DEFAULT_CATEGORY]: | 
					
						
							| 
									
										
										
										
											2022-01-04 15:39:19 +01:00
										 |  |  |                     continue | 
					
						
							| 
									
										
										
										
											2022-01-06 18:45:50 +01:00
										 |  |  |                 choices['{}__{}'.format(engine.name, category)] = not engine.disabled | 
					
						
							|  |  |  |         super().__init__(default_value, choices) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def transform_form_items(self, items): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         return [item[len('engine_') :].replace('_', ' ').replace('  ', '__') for item in items] | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def transform_values(self, values): | 
					
						
							| 
									
										
										
										
											2016-05-04 16:14:04 +02:00
										 |  |  |         if len(values) == 1 and next(iter(values)) == '': | 
					
						
							| 
									
										
										
										
											2021-08-31 10:40:29 +02:00
										 |  |  |             return [] | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         transformed_values = [] | 
					
						
							|  |  |  |         for value in values: | 
					
						
							|  |  |  |             engine, category = value.split('__') | 
					
						
							|  |  |  |             transformed_values.append((engine, category)) | 
					
						
							|  |  |  |         return transformed_values | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 18:45:50 +01:00
										 |  |  | class PluginsSetting(BooleanChoices): | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |     """Plugin settings""" | 
					
						
							| 
									
										
										
										
											2016-07-10 16:44:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def __init__(self, default_value, plugins: Iterable[Plugin]): | 
					
						
							| 
									
										
										
										
											2022-01-06 18:45:50 +01:00
										 |  |  |         super().__init__(default_value, {plugin.id: plugin.default_on for plugin in plugins}) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def transform_form_items(self, items): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         return [item[len('plugin_') :] for item in items] | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-30 18:40:22 +01:00
										 |  |  | class ClientPref: | 
					
						
							|  |  |  |     """Container to assemble client prefferences and settings.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # hint: searx.webapp.get_client_settings should be moved into this class | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     locale: babel.Locale | 
					
						
							|  |  |  |     """Locale prefered by the client.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, locale: Optional[babel.Locale] = None): | 
					
						
							|  |  |  |         self.locale = locale | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def locale_tag(self): | 
					
						
							|  |  |  |         if self.locale is None: | 
					
						
							|  |  |  |             return None | 
					
						
							|  |  |  |         tag = self.locale.language | 
					
						
							|  |  |  |         if self.locale.territory: | 
					
						
							|  |  |  |             tag += '-' + self.locale.territory | 
					
						
							|  |  |  |         return tag | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def from_http_request(cls, http_request: flask.Request): | 
					
						
							|  |  |  |         """Build ClientPref object from HTTP request.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         - `Accept-Language used for locale setting | 
					
						
							|  |  |  |           <https://www.w3.org/International/questions/qa-accept-lang-locales.en>`__ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							|  |  |  |         al_header = http_request.headers.get("Accept-Language") | 
					
						
							|  |  |  |         if not al_header: | 
					
						
							|  |  |  |             return cls(locale=None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         pairs = [] | 
					
						
							|  |  |  |         for l in al_header.split(','): | 
					
						
							|  |  |  |             # fmt: off | 
					
						
							|  |  |  |             lang, qvalue = [_.strip() for _ in (l.split(';') + ['q=1',])[:2]] | 
					
						
							|  |  |  |             # fmt: on | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 qvalue = float(qvalue.split('=')[-1]) | 
					
						
							|  |  |  |                 locale = babel.Locale.parse(lang, sep='-') | 
					
						
							|  |  |  |             except (ValueError, babel.core.UnknownLocaleError): | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             pairs.append((locale, qvalue)) | 
					
						
							| 
									
										
										
										
											2023-05-22 12:17:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         locale = None | 
					
						
							|  |  |  |         if pairs: | 
					
						
							|  |  |  |             pairs.sort(reverse=True, key=lambda x: x[1]) | 
					
						
							|  |  |  |             locale = pairs[0][0] | 
					
						
							|  |  |  |         return cls(locale=locale) | 
					
						
							| 
									
										
										
										
											2023-01-30 18:40:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-12 09:42:27 +02:00
										 |  |  | class Preferences: | 
					
						
							| 
									
										
										
										
											2017-07-10 12:47:25 +02:00
										 |  |  |     """Validates and saves preferences to cookies""" | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-30 18:40:22 +01:00
										 |  |  |     def __init__( | 
					
						
							|  |  |  |         self, | 
					
						
							|  |  |  |         themes: List[str], | 
					
						
							|  |  |  |         categories: List[str], | 
					
						
							|  |  |  |         engines: Dict[str, Engine], | 
					
						
							|  |  |  |         plugins: Iterable[Plugin], | 
					
						
							|  |  |  |         client: Optional[ClientPref] = None, | 
					
						
							|  |  |  |     ): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-27 14:38:39 +02:00
										 |  |  |         super().__init__() | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |         self.key_value_settings: Dict[str, Setting] = { | 
					
						
							| 
									
										
										
										
											2021-12-27 09:16:03 +01:00
										 |  |  |             # fmt: off | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |             'categories': MultipleChoiceSetting( | 
					
						
							| 
									
										
										
										
											2020-10-23 16:22:55 +02:00
										 |  |  |                 ['general'], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('categories'), | 
					
						
							| 
									
										
										
										
											2020-10-23 16:22:55 +02:00
										 |  |  |                 choices=categories + ['none'] | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |             ), | 
					
						
							|  |  |  |             'language': SearchLanguageSetting( | 
					
						
							| 
									
										
										
										
											2021-05-28 18:45:22 +02:00
										 |  |  |                 settings['search']['default_lang'], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('language'), | 
					
						
							| 
									
										
										
										
											2021-10-21 10:41:57 +02:00
										 |  |  |                 choices=settings['search']['languages'] + [''] | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |             ), | 
					
						
							|  |  |  |             'locale': EnumStringSetting( | 
					
						
							| 
									
										
										
										
											2021-05-28 18:45:22 +02:00
										 |  |  |                 settings['ui']['default_locale'], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('locale'), | 
					
						
							| 
									
										
										
										
											2021-08-03 15:13:00 +02:00
										 |  |  |                 choices=list(LOCALE_NAMES.keys()) + [''] | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |             ), | 
					
						
							|  |  |  |             'autocomplete': EnumStringSetting( | 
					
						
							| 
									
										
										
										
											2021-05-28 18:45:22 +02:00
										 |  |  |                 settings['search']['autocomplete'], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('autocomplete'), | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |                 choices=list(autocomplete.backends.keys()) + [''] | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             'image_proxy': MapSetting( | 
					
						
							| 
									
										
										
										
											2021-05-28 18:45:22 +02:00
										 |  |  |                 settings['server']['image_proxy'], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('image_proxy'), | 
					
						
							| 
									
										
										
										
											2020-02-23 20:56:05 +01:00
										 |  |  |                 map={ | 
					
						
							| 
									
										
										
										
											2021-05-28 18:45:22 +02:00
										 |  |  |                     '': settings['server']['image_proxy'], | 
					
						
							| 
									
										
										
										
											2020-02-23 20:56:05 +01:00
										 |  |  |                     '0': False, | 
					
						
							|  |  |  |                     '1': True, | 
					
						
							|  |  |  |                     'True': True, | 
					
						
							|  |  |  |                     'False': False | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |             ), | 
					
						
							|  |  |  |             'method': EnumStringSetting( | 
					
						
							| 
									
										
										
										
											2021-05-28 18:45:22 +02:00
										 |  |  |                 settings['server']['method'], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('method'), | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |                 choices=('GET', 'POST') | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             'safesearch': MapSetting( | 
					
						
							| 
									
										
										
										
											2021-05-28 18:45:22 +02:00
										 |  |  |                 settings['search']['safe_search'], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('safesearch'), | 
					
						
							| 
									
										
										
										
											2020-02-23 20:56:05 +01:00
										 |  |  |                 map={ | 
					
						
							|  |  |  |                     '0': 0, | 
					
						
							|  |  |  |                     '1': 1, | 
					
						
							|  |  |  |                     '2': 2 | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |             'theme': EnumStringSetting( | 
					
						
							| 
									
										
										
										
											2021-05-28 18:45:22 +02:00
										 |  |  |                 settings['ui']['default_theme'], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('theme'), | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |                 choices=themes | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             'results_on_new_tab': MapSetting( | 
					
						
							| 
									
										
										
										
											2021-05-28 18:45:22 +02:00
										 |  |  |                 settings['ui']['results_on_new_tab'], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('results_on_new_tab'), | 
					
						
							| 
									
										
										
										
											2020-02-23 20:56:05 +01:00
										 |  |  |                 map={ | 
					
						
							|  |  |  |                     '0': False, | 
					
						
							|  |  |  |                     '1': True, | 
					
						
							|  |  |  |                     'False': False, | 
					
						
							|  |  |  |                     'True': True | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |             ), | 
					
						
							|  |  |  |             'doi_resolver': MultipleChoiceSetting( | 
					
						
							| 
									
										
										
										
											2021-04-04 13:36:33 +02:00
										 |  |  |                 [settings['default_doi_resolver'], ], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('doi_resolver'), | 
					
						
							| 
									
										
										
										
											2020-10-23 16:22:55 +02:00
										 |  |  |                 choices=DOI_RESOLVERS | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |             ), | 
					
						
							| 
									
										
										
										
											2021-11-19 13:49:16 +01:00
										 |  |  |             'simple_style': EnumStringSetting( | 
					
						
							|  |  |  |                 settings['ui']['theme_args']['simple_style'], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('simple_style'), | 
					
						
							| 
									
										
										
										
											2021-11-19 13:49:16 +01:00
										 |  |  |                 choices=['', 'auto', 'light', 'dark'] | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2022-07-03 17:35:54 +02:00
										 |  |  |             'center_alignment': MapSetting( | 
					
						
							|  |  |  |                 settings['ui']['center_alignment'], | 
					
						
							|  |  |  |                 locked=is_locked('center_alignment'), | 
					
						
							| 
									
										
										
										
											2022-06-03 14:10:48 +02:00
										 |  |  |                 map={ | 
					
						
							|  |  |  |                     '0': False, | 
					
						
							|  |  |  |                     '1': True, | 
					
						
							|  |  |  |                     'False': False, | 
					
						
							|  |  |  |                     'True': True | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2020-11-22 18:00:21 +01:00
										 |  |  |             'advanced_search': MapSetting( | 
					
						
							| 
									
										
										
										
											2021-05-28 18:45:22 +02:00
										 |  |  |                 settings['ui']['advanced_search'], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('advanced_search'), | 
					
						
							| 
									
										
										
										
											2020-11-22 18:00:21 +01:00
										 |  |  |                 map={ | 
					
						
							|  |  |  |                     '0': False, | 
					
						
							|  |  |  |                     '1': True, | 
					
						
							|  |  |  |                     'False': False, | 
					
						
							| 
									
										
										
										
											2020-11-23 19:13:29 +01:00
										 |  |  |                     'True': True, | 
					
						
							|  |  |  |                     'on': True, | 
					
						
							| 
									
										
										
										
											2020-11-22 18:00:21 +01:00
										 |  |  |                 } | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2021-11-06 12:26:48 +01:00
										 |  |  |             'query_in_title': MapSetting( | 
					
						
							|  |  |  |                 settings['ui']['query_in_title'], | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |                 locked=is_locked('query_in_title'), | 
					
						
							| 
									
										
										
										
											2021-11-06 12:26:48 +01:00
										 |  |  |                 map={ | 
					
						
							|  |  |  |                     '': settings['ui']['query_in_title'], | 
					
						
							|  |  |  |                     '0': False, | 
					
						
							|  |  |  |                     '1': True, | 
					
						
							|  |  |  |                     'True': True, | 
					
						
							|  |  |  |                     'False': False | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2022-01-23 11:37:57 +01:00
										 |  |  |             'infinite_scroll': MapSetting( | 
					
						
							|  |  |  |                 settings['ui']['infinite_scroll'], | 
					
						
							|  |  |  |                 locked=is_locked('infinite_scroll'), | 
					
						
							|  |  |  |                 map={ | 
					
						
							|  |  |  |                     '': settings['ui']['infinite_scroll'], | 
					
						
							|  |  |  |                     '0': False, | 
					
						
							|  |  |  |                     '1': True, | 
					
						
							|  |  |  |                     'True': True, | 
					
						
							|  |  |  |                     'False': False | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2021-12-27 09:16:03 +01:00
										 |  |  |             # fmt: on | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |         self.engines = EnginesSetting('engines', engines=engines.values()) | 
					
						
							|  |  |  |         self.plugins = PluginsSetting('plugins', plugins=plugins) | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  |         self.tokens = SetSetting('tokens') | 
					
						
							| 
									
										
										
										
											2023-01-30 18:40:22 +01:00
										 |  |  |         self.client = client or ClientPref() | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |         self.unknown_params: Dict[str, str] = {} | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-10 12:47:25 +02:00
										 |  |  |     def get_as_url_params(self): | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |         """Return preferences as URL parameters""" | 
					
						
							| 
									
										
										
										
											2017-07-10 12:47:25 +02:00
										 |  |  |         settings_kv = {} | 
					
						
							|  |  |  |         for k, v in self.key_value_settings.items(): | 
					
						
							| 
									
										
										
										
											2020-10-23 16:22:55 +02:00
										 |  |  |             if v.locked: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2017-07-10 12:47:25 +02:00
										 |  |  |             if isinstance(v, MultipleChoiceSetting): | 
					
						
							|  |  |  |                 settings_kv[k] = ','.join(v.get_value()) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 settings_kv[k] = v.get_value() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         settings_kv['disabled_engines'] = ','.join(self.engines.disabled) | 
					
						
							|  |  |  |         settings_kv['enabled_engines'] = ','.join(self.engines.enabled) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         settings_kv['disabled_plugins'] = ','.join(self.plugins.disabled) | 
					
						
							|  |  |  |         settings_kv['enabled_plugins'] = ','.join(self.plugins.enabled) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  |         settings_kv['tokens'] = ','.join(self.tokens.values) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  |         return urlsafe_b64encode(compress(urlencode(settings_kv).encode())).decode() | 
					
						
							| 
									
										
										
										
											2017-10-25 23:56:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def parse_encoded_data(self, input_data: str): | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |         """parse (base64) preferences from request (``flask.request.form['preferences']``)""" | 
					
						
							| 
									
										
										
										
											2021-06-26 17:14:13 +02:00
										 |  |  |         bin_data = decompress(urlsafe_b64decode(input_data)) | 
					
						
							| 
									
										
										
										
											2019-07-17 09:42:40 +02:00
										 |  |  |         dict_data = {} | 
					
						
							| 
									
										
										
										
											2022-11-24 20:12:06 +01:00
										 |  |  |         for x, y in parse_qs(bin_data.decode('ascii'), keep_blank_values=True).items(): | 
					
						
							| 
									
										
										
										
											2021-06-26 17:14:13 +02:00
										 |  |  |             dict_data[x] = y[0] | 
					
						
							| 
									
										
										
										
											2019-07-17 09:42:40 +02:00
										 |  |  |         self.parse_dict(dict_data) | 
					
						
							| 
									
										
										
										
											2017-07-10 12:47:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def parse_dict(self, input_data: Dict[str, str]): | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |         """parse preferences from request (``flask.request.form``)""" | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  |         for user_setting_name, user_setting in input_data.items(): | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |             if user_setting_name in self.key_value_settings: | 
					
						
							| 
									
										
										
										
											2020-10-23 16:22:55 +02:00
										 |  |  |                 if self.key_value_settings[user_setting_name].locked: | 
					
						
							|  |  |  |                     continue | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |                 self.key_value_settings[user_setting_name].parse(user_setting) | 
					
						
							|  |  |  |             elif user_setting_name == 'disabled_engines': | 
					
						
							| 
									
										
										
										
											2022-01-04 14:14:37 +01:00
										 |  |  |                 self.engines.parse_cookie(input_data.get('disabled_engines', ''), input_data.get('enabled_engines', '')) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |             elif user_setting_name == 'disabled_plugins': | 
					
						
							| 
									
										
										
										
											2022-01-04 14:14:37 +01:00
										 |  |  |                 self.plugins.parse_cookie(input_data.get('disabled_plugins', ''), input_data.get('enabled_plugins', '')) | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  |             elif user_setting_name == 'tokens': | 
					
						
							|  |  |  |                 self.tokens.parse(user_setting) | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |             elif not any( | 
					
						
							|  |  |  |                 user_setting_name.startswith(x) for x in ['enabled_', 'disabled_', 'engine_', 'category_', 'plugin_'] | 
					
						
							|  |  |  |             ): | 
					
						
							| 
									
										
										
										
											2017-12-05 22:30:20 +01:00
										 |  |  |                 self.unknown_params[user_setting_name] = user_setting | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def parse_form(self, input_data: Dict[str, str]): | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |         """Parse formular (``<input>``) data from a ``flask.request.form``""" | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         disabled_engines = [] | 
					
						
							|  |  |  |         enabled_categories = [] | 
					
						
							|  |  |  |         disabled_plugins = [] | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  |         for user_setting_name, user_setting in input_data.items(): | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |             if user_setting_name in self.key_value_settings: | 
					
						
							|  |  |  |                 self.key_value_settings[user_setting_name].parse(user_setting) | 
					
						
							|  |  |  |             elif user_setting_name.startswith('engine_'): | 
					
						
							|  |  |  |                 disabled_engines.append(user_setting_name) | 
					
						
							|  |  |  |             elif user_setting_name.startswith('category_'): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |                 enabled_categories.append(user_setting_name[len('category_') :]) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |             elif user_setting_name.startswith('plugin_'): | 
					
						
							|  |  |  |                 disabled_plugins.append(user_setting_name) | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  |             elif user_setting_name == 'tokens': | 
					
						
							|  |  |  |                 self.tokens.parse_form(user_setting) | 
					
						
							| 
									
										
										
										
											2016-06-26 01:44:42 +02:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 self.unknown_params[user_setting_name] = user_setting | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         self.key_value_settings['categories'].parse_form(enabled_categories) | 
					
						
							|  |  |  |         self.engines.parse_form(disabled_engines) | 
					
						
							|  |  |  |         self.plugins.parse_form(disabled_plugins) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # cannot be used in case of engines or plugins | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def get_value(self, user_setting_name: str): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         """Returns the value for ``user_setting_name``""" | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |         ret_val = None | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         if user_setting_name in self.key_value_settings: | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |             ret_val = self.key_value_settings[user_setting_name].get_value() | 
					
						
							| 
									
										
										
										
											2017-12-05 22:30:20 +01:00
										 |  |  |         if user_setting_name in self.unknown_params: | 
					
						
							| 
									
										
										
										
											2020-02-23 20:52:00 +01:00
										 |  |  |             ret_val = self.unknown_params[user_setting_name] | 
					
						
							|  |  |  |         return ret_val | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  |     def save(self, resp: flask.Response): | 
					
						
							| 
									
										
										
										
											2022-09-27 17:01:00 +02:00
										 |  |  |         """Save cookie in the HTTP response object""" | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  |         for user_setting_name, user_setting in self.key_value_settings.items(): | 
					
						
							| 
									
										
										
										
											2021-07-03 17:51:39 +02:00
										 |  |  |             # pylint: disable=unnecessary-dict-index-lookup | 
					
						
							| 
									
										
										
										
											2020-10-23 16:22:55 +02:00
										 |  |  |             if self.key_value_settings[user_setting_name].locked: | 
					
						
							|  |  |  |                 continue | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |             user_setting.save(user_setting_name, resp) | 
					
						
							|  |  |  |         self.engines.save(resp) | 
					
						
							|  |  |  |         self.plugins.save(resp) | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  |         self.tokens.save('tokens', resp) | 
					
						
							| 
									
										
										
										
											2016-06-26 01:44:42 +02:00
										 |  |  |         for k, v in self.unknown_params.items(): | 
					
						
							|  |  |  |             resp.set_cookie(k, v, max_age=COOKIE_MAX_AGE) | 
					
						
							| 
									
										
										
										
											2016-04-08 16:38:05 +02:00
										 |  |  |         return resp | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-07 13:34:35 +02:00
										 |  |  |     def validate_token(self, engine): | 
					
						
							| 
									
										
										
										
											2020-02-01 11:01:17 +01:00
										 |  |  |         valid = True | 
					
						
							|  |  |  |         if hasattr(engine, 'tokens') and engine.tokens: | 
					
						
							|  |  |  |             valid = False | 
					
						
							|  |  |  |             for token in self.tokens.values: | 
					
						
							|  |  |  |                 if token in engine.tokens: | 
					
						
							|  |  |  |                     valid = True | 
					
						
							|  |  |  |                     break | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return valid | 
					
						
							| 
									
										
										
										
											2020-10-23 16:22:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 11:53:42 +01:00
										 |  |  | def is_locked(setting_name: str): | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |     """Checks if a given setting name is locked by settings.yml""" | 
					
						
							| 
									
										
										
										
											2020-10-23 16:22:55 +02:00
										 |  |  |     if 'preferences' not in settings: | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  |     if 'lock' not in settings['preferences']: | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  |     return setting_name in settings['preferences']['lock'] |