1
0

RequestUserAgent.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\WorkflowEngine\Check;
  7. use OCP\IL10N;
  8. use OCP\IRequest;
  9. class RequestUserAgent extends AbstractStringCheck {
  10. /** @var IRequest */
  11. protected $request;
  12. /**
  13. * @param IL10N $l
  14. * @param IRequest $request
  15. */
  16. public function __construct(IL10N $l, IRequest $request) {
  17. parent::__construct($l);
  18. $this->request = $request;
  19. }
  20. /**
  21. * @param string $operator
  22. * @param string $value
  23. * @return bool
  24. */
  25. public function executeCheck($operator, $value) {
  26. $actualValue = $this->getActualValue();
  27. if (in_array($operator, ['is', '!is'], true)) {
  28. switch ($value) {
  29. case 'android':
  30. $operator = $operator === 'is' ? 'matches' : '!matches';
  31. $value = IRequest::USER_AGENT_CLIENT_ANDROID;
  32. break;
  33. case 'ios':
  34. $operator = $operator === 'is' ? 'matches' : '!matches';
  35. $value = IRequest::USER_AGENT_CLIENT_IOS;
  36. break;
  37. case 'desktop':
  38. $operator = $operator === 'is' ? 'matches' : '!matches';
  39. $value = IRequest::USER_AGENT_CLIENT_DESKTOP;
  40. break;
  41. case 'mail':
  42. if ($operator === 'is') {
  43. return $this->executeStringCheck('matches', IRequest::USER_AGENT_OUTLOOK_ADDON, $actualValue)
  44. || $this->executeStringCheck('matches', IRequest::USER_AGENT_THUNDERBIRD_ADDON, $actualValue);
  45. }
  46. return $this->executeStringCheck('!matches', IRequest::USER_AGENT_OUTLOOK_ADDON, $actualValue)
  47. && $this->executeStringCheck('!matches', IRequest::USER_AGENT_THUNDERBIRD_ADDON, $actualValue);
  48. }
  49. }
  50. return $this->executeStringCheck($operator, $value, $actualValue);
  51. }
  52. /**
  53. * @return string
  54. */
  55. protected function getActualValue() {
  56. return $this->request->getHeader('User-Agent');
  57. }
  58. public function isAvailableForScope(int $scope): bool {
  59. return true;
  60. }
  61. }