/src/lib/Helper/AppSettings/ServiceSettingsHelper.php

https://github.com/marius-wieschollek/passwords · PHP · 385 lines · 267 code · 35 blank · 83 comment · 5 complexity · c8c6f8c49a3a44e0ddaf1ffe84b6b9a3 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of the Passwords App
  4. * created by Marius David Wieschollek
  5. * and licensed under the AGPL.
  6. */
  7. namespace OCA\Passwords\Helper\AppSettings;
  8. use OCA\Passwords\Exception\ApiException;
  9. use OCA\Passwords\Helper\Favicon\BestIconHelper;
  10. use OCA\Passwords\Helper\Image\ImagickHelper;
  11. use OCA\Passwords\Helper\Preview\BrowshotPreviewHelper;
  12. use OCA\Passwords\Helper\Preview\ScreeenlyHelper;
  13. use OCA\Passwords\Helper\Preview\ScreenShotLayerHelper;
  14. use OCA\Passwords\Helper\Preview\ScreenShotMachineHelper;
  15. use OCA\Passwords\Services\ConfigurationService;
  16. use OCA\Passwords\Services\HelperService;
  17. use OCP\IL10N;
  18. /**
  19. * Class ServiceSettingsHelper
  20. *
  21. * @package OCA\Passwords\Helper\AppSettings
  22. */
  23. class ServiceSettingsHelper extends AbstractSettingsHelper {
  24. /**
  25. * @var IL10N
  26. */
  27. protected IL10N $localisation;
  28. /**
  29. * @var HelperService
  30. */
  31. protected HelperService $helperService;
  32. /**
  33. * @var string
  34. */
  35. protected string $scope = 'service';
  36. /**
  37. * @var array
  38. */
  39. protected array $keys
  40. = [
  41. 'security' => 'service/security',
  42. 'words' => 'service/words',
  43. 'images' => 'service/images',
  44. 'preview' => 'service/preview',
  45. 'favicon' => 'service/favicon'
  46. ];
  47. /**
  48. * @var array
  49. */
  50. protected array $defaults
  51. = [
  52. 'security' => HelperService::SECURITY_HIBP,
  53. 'preview' => HelperService::PREVIEW_DEFAULT,
  54. 'favicon' => HelperService::FAVICON_DEFAULT,
  55. 'preview.api' => '',
  56. 'favicon.api' => ''
  57. ];
  58. /**
  59. * ServiceSettingsHelper constructor.
  60. *
  61. * @param ConfigurationService $config
  62. * @param HelperService $helperService
  63. * @param IL10N $localisation
  64. */
  65. public function __construct(ConfigurationService $config, HelperService $helperService, IL10N $localisation) {
  66. parent::__construct($config);
  67. $this->localisation = $localisation;
  68. $this->helperService = $helperService;
  69. }
  70. /**
  71. * @return array
  72. */
  73. public function list(): array {
  74. try {
  75. return [
  76. $this->get('words'),
  77. $this->get('images'),
  78. $this->get('favicon'),
  79. $this->get('preview'),
  80. $this->get('security'),
  81. $this->get('favicon.api'),
  82. $this->get('preview.api')
  83. ];
  84. } catch(ApiException $e) {
  85. return [];
  86. }
  87. }
  88. /**
  89. * @param $key
  90. *
  91. * @return array
  92. * @throws ApiException
  93. */
  94. public function get(string $key): array {
  95. switch($key) {
  96. case 'words':
  97. return $this->getGenericSetting('words');
  98. case 'images':
  99. return $this->getGenericSetting('images');
  100. case 'favicon':
  101. return $this->getGenericSetting('favicon');
  102. case 'preview':
  103. return $this->getGenericSetting('preview');
  104. case 'security':
  105. return $this->getGenericSetting('security');
  106. case 'favicon.api':
  107. return $this->getFaviconApiSetting();
  108. case 'preview.api':
  109. return $this->getPreviewApiSetting();
  110. }
  111. throw new ApiException('Unknown setting identifier', 400);
  112. }
  113. /**
  114. * @param string $setting
  115. *
  116. * @return string
  117. * @throws ApiException
  118. */
  119. protected function getSettingKey(string $setting): string {
  120. switch($setting) {
  121. case 'preview.api':
  122. return $this->getPreviewApiSettingKey();
  123. case 'favicon.api':
  124. return $this->getFaviconApiSettingKey();
  125. }
  126. return parent::getSettingKey($setting);
  127. }
  128. /**
  129. * @param string $setting
  130. *
  131. * @return string
  132. * @throws ApiException
  133. */
  134. protected function getSettingDefault(string $setting) {
  135. switch($setting) {
  136. case 'words':
  137. return $this->helperService->getDefaultWordsHelperName();
  138. case 'images':
  139. return HelperService::getImageHelperName();
  140. }
  141. return parent::getSettingDefault($setting);
  142. }
  143. /**
  144. * @return array
  145. */
  146. protected function getFaviconApiSetting(): array {
  147. $configKey = $this->getFaviconApiSettingKey();
  148. $default = $this->getFaviconApiSettingDefault();
  149. $value = $this->config->getAppValue($configKey, $default);
  150. $isDefault = !$this->config->hasAppValue($configKey);
  151. return $this->generateSettingArray(
  152. 'favicon.api',
  153. $value,
  154. [],
  155. $default,
  156. $isDefault,
  157. 'string',
  158. [
  159. 'service.favicon' => [HelperService::FAVICON_BESTICON]
  160. ]
  161. );
  162. }
  163. /**
  164. * @return string
  165. */
  166. protected function getFaviconApiSettingKey(): string {
  167. $service = $this->config->getAppValue('service/favicon', HelperService::FAVICON_DEFAULT);
  168. if($service === HelperService::FAVICON_BESTICON) {
  169. return BestIconHelper::BESTICON_CONFIG_KEY;
  170. }
  171. return 'service/favicon/api';
  172. }
  173. /**
  174. * @return string
  175. */
  176. protected function getFaviconApiSettingDefault(): string {
  177. return '';
  178. }
  179. /**
  180. * @return array
  181. */
  182. protected function getPreviewApiSetting(): array {
  183. $configKey = $this->getPreviewApiSettingKey();
  184. $value = $this->config->getAppValue($configKey, '');
  185. $isDefault = !$this->config->hasAppValue($configKey);
  186. return $this->generateSettingArray(
  187. 'preview.api',
  188. $value,
  189. [],
  190. '',
  191. $isDefault,
  192. 'string',
  193. [
  194. 'service.preview' => [
  195. HelperService::PREVIEW_SCREEENLY,
  196. HelperService::PREVIEW_BROW_SHOT,
  197. HelperService::PREVIEW_SCREEN_SHOT_LAYER,
  198. HelperService::PREVIEW_SCREEN_SHOT_MACHINE
  199. ]
  200. ]
  201. );
  202. }
  203. /**
  204. * @return string
  205. */
  206. protected function getPreviewApiSettingKey(): string {
  207. $service = $this->config->getAppValue('service/preview', HelperService::PREVIEW_DEFAULT);
  208. if($service === HelperService::PREVIEW_SCREEN_SHOT_LAYER) {
  209. return ScreenShotLayerHelper::SSL_API_CONFIG_KEY;
  210. }
  211. if($service === HelperService::PREVIEW_SCREEN_SHOT_MACHINE) {
  212. return ScreenShotMachineHelper::SSM_API_CONFIG_KEY;
  213. }
  214. if($service === HelperService::PREVIEW_BROW_SHOT) {
  215. return BrowshotPreviewHelper::BWS_API_CONFIG_KEY;
  216. }
  217. if($service === HelperService::PREVIEW_SCREEENLY) {
  218. return ScreeenlyHelper::SCREEENLY_API_CONFIG_KEY;
  219. }
  220. return 'service/preview/api';
  221. }
  222. /**
  223. * @return array
  224. */
  225. protected function getSecurityOptions(): array {
  226. return [
  227. $this->generateOptionArray(
  228. HelperService::SECURITY_HIBP,
  229. $this->localisation->t('Have I been pwned? (recommended)')
  230. ),
  231. $this->generateOptionArray(
  232. HelperService::SECURITY_BIG_LOCAL,
  233. $this->localisation->t('10 Million Passwords (Local)')
  234. ),
  235. $this->generateOptionArray(
  236. HelperService::SECURITY_SMALL_LOCAL,
  237. $this->localisation->t('1 Million Passwords (Local)')
  238. ),
  239. $this->generateOptionArray(
  240. HelperService::SECURITY_BIGDB_HIBP,
  241. $this->localisation->t('10Mio Passwords & Hibp?')
  242. )
  243. ];
  244. }
  245. /**
  246. * @return array
  247. */
  248. protected function getWordsOptions(): array {
  249. return [
  250. $this->generateOptionArray(
  251. HelperService::WORDS_LOCAL,
  252. $this->localisation->t('Local dictionary'),
  253. $this->helperService->getWordsHelper(HelperService::WORDS_LOCAL)->isAvailable()
  254. ),
  255. $this->generateOptionArray(
  256. HelperService::WORDS_LEIPZIG,
  257. $this->localisation->t('Leipzig Corpora Collection (recommended)'),
  258. $this->helperService->getWordsHelper(HelperService::WORDS_LEIPZIG)->isAvailable()
  259. ),
  260. $this->generateOptionArray(
  261. HelperService::WORDS_SNAKES,
  262. $this->localisation->t('watchout4snakes.com'),
  263. $this->helperService->getWordsHelper(HelperService::WORDS_SNAKES)->isAvailable()
  264. ),
  265. $this->generateOptionArray(
  266. HelperService::WORDS_RANDOM,
  267. $this->localisation->t('Random Characters'),
  268. $this->helperService->getWordsHelper(HelperService::WORDS_RANDOM)->isAvailable()
  269. )
  270. ];
  271. }
  272. /**
  273. * @return array
  274. */
  275. protected function getFaviconOptions(): array {
  276. return [
  277. $this->generateOptionArray(
  278. HelperService::FAVICON_LOCAL,
  279. $this->localisation->t('Local analyzer')
  280. ),
  281. $this->generateOptionArray(
  282. HelperService::FAVICON_BESTICON,
  283. $this->localisation->t('Besticon (recommended)')
  284. ),
  285. $this->generateOptionArray(
  286. HelperService::FAVICON_FAVICON_GRABBER,
  287. $this->localisation->t('favicongrabber.com')
  288. ),
  289. $this->generateOptionArray(
  290. HelperService::FAVICON_DUCK_DUCK_GO,
  291. $this->localisation->t('DuckDuckGo')
  292. ),
  293. $this->generateOptionArray(
  294. HelperService::FAVICON_GOOGLE,
  295. $this->localisation->t('Google')
  296. ),
  297. $this->generateOptionArray(
  298. HelperService::FAVICON_DEFAULT,
  299. $this->localisation->t('None')
  300. )
  301. ];
  302. }
  303. /**
  304. * @return array
  305. */
  306. protected function getPreviewOptions(): array {
  307. return [
  308. $this->generateOptionArray(
  309. HelperService::PREVIEW_PAGERES,
  310. $this->localisation->t('Pageres CLI (Local)')
  311. ),
  312. $this->generateOptionArray(
  313. HelperService::PREVIEW_BROW_SHOT,
  314. $this->localisation->t('Browshot')
  315. ),
  316. $this->generateOptionArray(
  317. HelperService::PREVIEW_SCREEENLY,
  318. $this->localisation->t('screeenly')
  319. ),
  320. $this->generateOptionArray(
  321. HelperService::PREVIEW_SCREEN_SHOT_LAYER,
  322. $this->localisation->t('screenshotlayer')
  323. ),
  324. $this->generateOptionArray(
  325. HelperService::PREVIEW_SCREEN_SHOT_MACHINE,
  326. $this->localisation->t('screenshotmachine.com')
  327. ),
  328. $this->generateOptionArray(
  329. HelperService::PREVIEW_DEFAULT,
  330. $this->localisation->t('None')
  331. )
  332. ];
  333. }
  334. /**
  335. * @return array
  336. */
  337. protected function getImagesOptions(): array {
  338. return [
  339. $this->generateOptionArray(
  340. HelperService::IMAGES_IMAGICK,
  341. $this->localisation->t('Imagick/GMagick (recommended)'),
  342. ImagickHelper::isAvailable()
  343. ),
  344. $this->generateOptionArray(
  345. HelperService::IMAGES_GDLIB,
  346. $this->localisation->t('PHP GDLib')
  347. )
  348. ];
  349. }
  350. }