preferences.py 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. from base64 import urlsafe_b64encode, urlsafe_b64decode
  2. from zlib import compress, decompress
  3. from sys import version
  4. from searx import settings, autocomplete
  5. from searx.languages import language_codes as languages
  6. from searx.url_utils import parse_qs, urlencode
  7. if version[0] == '3':
  8. unicode = str
  9. COOKIE_MAX_AGE = 60 * 60 * 24 * 365 * 5 # 5 years
  10. LANGUAGE_CODES = [l[0] for l in languages]
  11. LANGUAGE_CODES.append('all')
  12. DISABLED = 0
  13. ENABLED = 1
  14. class MissingArgumentException(Exception):
  15. pass
  16. class ValidationException(Exception):
  17. pass
  18. class Setting(object):
  19. """Base class of user settings"""
  20. def __init__(self, default_value, **kwargs):
  21. super(Setting, self).__init__()
  22. self.value = default_value
  23. for key, value in kwargs.items():
  24. setattr(self, key, value)
  25. self._post_init()
  26. def _post_init(self):
  27. pass
  28. def parse(self, data):
  29. self.value = data
  30. def get_value(self):
  31. return self.value
  32. def save(self, name, resp):
  33. resp.set_cookie(name, self.value, max_age=COOKIE_MAX_AGE)
  34. class StringSetting(Setting):
  35. """Setting of plain string values"""
  36. pass
  37. class EnumStringSetting(Setting):
  38. """Setting of a value which can only come from the given choices"""
  39. def _validate_selection(self, selection):
  40. if selection not in self.choices:
  41. raise ValidationException('Invalid value: "{0}"'.format(selection))
  42. def _post_init(self):
  43. if not hasattr(self, 'choices'):
  44. raise MissingArgumentException('Missing argument: choices')
  45. self._validate_selection(self.value)
  46. def parse(self, data):
  47. self._validate_selection(data)
  48. self.value = data
  49. class MultipleChoiceSetting(EnumStringSetting):
  50. """Setting of values which can only come from the given choices"""
  51. def _validate_selections(self, selections):
  52. for item in selections:
  53. if item not in self.choices:
  54. raise ValidationException('Invalid value: "{0}"'.format(selections))
  55. def _post_init(self):
  56. if not hasattr(self, 'choices'):
  57. raise MissingArgumentException('Missing argument: choices')
  58. self._validate_selections(self.value)
  59. def parse(self, data):
  60. if data == '':
  61. self.value = []
  62. return
  63. elements = data.split(',')
  64. self._validate_selections(elements)
  65. self.value = elements
  66. def parse_form(self, data):
  67. self.value = []
  68. for choice in data:
  69. if choice in self.choices and choice not in self.value:
  70. self.value.append(choice)
  71. def save(self, name, resp):
  72. resp.set_cookie(name, ','.join(self.value), max_age=COOKIE_MAX_AGE)
  73. class SearchLanguageSetting(EnumStringSetting):
  74. """Available choices may change, so user's value may not be in choices anymore"""
  75. def parse(self, data):
  76. if data not in self.choices and data != self.value:
  77. # hack to give some backwards compatibility with old language cookies
  78. data = str(data).replace('_', '-')
  79. lang = data.split('-')[0]
  80. if data in self.choices:
  81. pass
  82. elif lang in self.choices:
  83. data = lang
  84. elif data == 'nb-NO':
  85. data = 'no-NO'
  86. elif data == 'ar-XA':
  87. data = 'ar-SA'
  88. else:
  89. data = self.value
  90. self.value = data
  91. class MapSetting(Setting):
  92. """Setting of a value that has to be translated in order to be storable"""
  93. def _post_init(self):
  94. if not hasattr(self, 'map'):
  95. raise MissingArgumentException('missing argument: map')
  96. if self.value not in self.map.values():
  97. raise ValidationException('Invalid default value')
  98. def parse(self, data):
  99. if data not in self.map:
  100. raise ValidationException('Invalid choice: {0}'.format(data))
  101. self.value = self.map[data]
  102. self.key = data
  103. def save(self, name, resp):
  104. if hasattr(self, 'key'):
  105. resp.set_cookie(name, self.key, max_age=COOKIE_MAX_AGE)
  106. class SwitchableSetting(Setting):
  107. """ Base class for settings that can be turned on && off"""
  108. def _post_init(self):
  109. self.disabled = set()
  110. self.enabled = set()
  111. if not hasattr(self, 'choices'):
  112. raise MissingArgumentException('missing argument: choices')
  113. def transform_form_items(self, items):
  114. return items
  115. def transform_values(self, values):
  116. return values
  117. def parse_cookie(self, data):
  118. if data[DISABLED] != '':
  119. self.disabled = set(data[DISABLED].split(','))
  120. if data[ENABLED] != '':
  121. self.enabled = set(data[ENABLED].split(','))
  122. def parse_form(self, items):
  123. items = self.transform_form_items(items)
  124. self.disabled = set()
  125. self.enabled = set()
  126. for choice in self.choices:
  127. if choice['default_on']:
  128. if choice['id'] in items:
  129. self.disabled.add(choice['id'])
  130. else:
  131. if choice['id'] not in items:
  132. self.enabled.add(choice['id'])
  133. def save(self, resp):
  134. resp.set_cookie('disabled_{0}'.format(self.value), ','.join(self.disabled), max_age=COOKIE_MAX_AGE)
  135. resp.set_cookie('enabled_{0}'.format(self.value), ','.join(self.enabled), max_age=COOKIE_MAX_AGE)
  136. def get_disabled(self):
  137. disabled = self.disabled
  138. for choice in self.choices:
  139. if not choice['default_on'] and choice['id'] not in self.enabled:
  140. disabled.add(choice['id'])
  141. return self.transform_values(disabled)
  142. def get_enabled(self):
  143. enabled = self.enabled
  144. for choice in self.choices:
  145. if choice['default_on'] and choice['id'] not in self.disabled:
  146. enabled.add(choice['id'])
  147. return self.transform_values(enabled)
  148. class EnginesSetting(SwitchableSetting):
  149. def _post_init(self):
  150. super(EnginesSetting, self)._post_init()
  151. transformed_choices = []
  152. for engine_name, engine in self.choices.items():
  153. for category in engine.categories:
  154. transformed_choice = dict()
  155. transformed_choice['default_on'] = not engine.disabled
  156. transformed_choice['id'] = '{}__{}'.format(engine_name, category)
  157. transformed_choices.append(transformed_choice)
  158. self.choices = transformed_choices
  159. def transform_form_items(self, items):
  160. return [item[len('engine_'):].replace('_', ' ').replace(' ', '__') for item in items]
  161. def transform_values(self, values):
  162. if len(values) == 1 and next(iter(values)) == '':
  163. return list()
  164. transformed_values = []
  165. for value in values:
  166. engine, category = value.split('__')
  167. transformed_values.append((engine, category))
  168. return transformed_values
  169. class PluginsSetting(SwitchableSetting):
  170. def _post_init(self):
  171. super(PluginsSetting, self)._post_init()
  172. transformed_choices = []
  173. for plugin in self.choices:
  174. transformed_choice = dict()
  175. transformed_choice['default_on'] = plugin.default_on
  176. transformed_choice['id'] = plugin.id
  177. transformed_choices.append(transformed_choice)
  178. self.choices = transformed_choices
  179. def transform_form_items(self, items):
  180. return [item[len('plugin_'):] for item in items]
  181. class Preferences(object):
  182. """Validates and saves preferences to cookies"""
  183. def __init__(self, themes, categories, engines, plugins):
  184. super(Preferences, self).__init__()
  185. self.key_value_settings = {'categories': MultipleChoiceSetting(['general'], choices=categories),
  186. 'language': SearchLanguageSetting(settings['search']['language'],
  187. choices=LANGUAGE_CODES),
  188. 'locale': EnumStringSetting(settings['ui']['default_locale'],
  189. choices=list(settings['locales'].keys()) + ['']),
  190. 'autocomplete': EnumStringSetting(settings['search']['autocomplete'],
  191. choices=list(autocomplete.backends.keys()) + ['']),
  192. 'image_proxy': MapSetting(settings['server']['image_proxy'],
  193. map={'': settings['server']['image_proxy'],
  194. '0': False,
  195. '1': True,
  196. 'True': True,
  197. 'False': False}),
  198. 'method': EnumStringSetting('POST', choices=('GET', 'POST')),
  199. 'safesearch': MapSetting(settings['search']['safe_search'], map={'0': 0,
  200. '1': 1,
  201. '2': 2}),
  202. 'theme': EnumStringSetting(settings['ui']['default_theme'], choices=themes),
  203. 'results_on_new_tab': MapSetting(False, map={'0': False,
  204. '1': True,
  205. 'False': False,
  206. 'True': True})}
  207. self.engines = EnginesSetting('engines', choices=engines)
  208. self.plugins = PluginsSetting('plugins', choices=plugins)
  209. self.unknown_params = {}
  210. def get_as_url_params(self):
  211. settings_kv = {}
  212. for k, v in self.key_value_settings.items():
  213. if isinstance(v, MultipleChoiceSetting):
  214. settings_kv[k] = ','.join(v.get_value())
  215. else:
  216. settings_kv[k] = v.get_value()
  217. settings_kv['disabled_engines'] = ','.join(self.engines.disabled)
  218. settings_kv['enabled_engines'] = ','.join(self.engines.enabled)
  219. settings_kv['disabled_plugins'] = ','.join(self.plugins.disabled)
  220. settings_kv['enabled_plugins'] = ','.join(self.plugins.enabled)
  221. return urlsafe_b64encode(compress(urlencode(settings_kv).encode('utf-8'))).decode('utf-8')
  222. def parse_encoded_data(self, input_data):
  223. decoded_data = decompress(urlsafe_b64decode(input_data.encode('utf-8')))
  224. self.parse_dict({x: y[0] for x, y in parse_qs(unicode(decoded_data)).items()})
  225. def parse_dict(self, input_data):
  226. for user_setting_name, user_setting in input_data.items():
  227. if user_setting_name in self.key_value_settings:
  228. self.key_value_settings[user_setting_name].parse(user_setting)
  229. elif user_setting_name == 'disabled_engines':
  230. self.engines.parse_cookie((input_data.get('disabled_engines', ''),
  231. input_data.get('enabled_engines', '')))
  232. elif user_setting_name == 'disabled_plugins':
  233. self.plugins.parse_cookie((input_data.get('disabled_plugins', ''),
  234. input_data.get('enabled_plugins', '')))
  235. def parse_form(self, input_data):
  236. disabled_engines = []
  237. enabled_categories = []
  238. disabled_plugins = []
  239. for user_setting_name, user_setting in input_data.items():
  240. if user_setting_name in self.key_value_settings:
  241. self.key_value_settings[user_setting_name].parse(user_setting)
  242. elif user_setting_name.startswith('engine_'):
  243. disabled_engines.append(user_setting_name)
  244. elif user_setting_name.startswith('category_'):
  245. enabled_categories.append(user_setting_name[len('category_'):])
  246. elif user_setting_name.startswith('plugin_'):
  247. disabled_plugins.append(user_setting_name)
  248. else:
  249. self.unknown_params[user_setting_name] = user_setting
  250. self.key_value_settings['categories'].parse_form(enabled_categories)
  251. self.engines.parse_form(disabled_engines)
  252. self.plugins.parse_form(disabled_plugins)
  253. # cannot be used in case of engines or plugins
  254. def get_value(self, user_setting_name):
  255. if user_setting_name in self.key_value_settings:
  256. return self.key_value_settings[user_setting_name].get_value()
  257. def save(self, resp):
  258. for user_setting_name, user_setting in self.key_value_settings.items():
  259. user_setting.save(user_setting_name, resp)
  260. self.engines.save(resp)
  261. self.plugins.save(resp)
  262. for k, v in self.unknown_params.items():
  263. resp.set_cookie(k, v, max_age=COOKIE_MAX_AGE)
  264. return resp