RequestUserAgent.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. /**
  11. * @param IL10N $l
  12. * @param IRequest $request
  13. */
  14. public function __construct(
  15. IL10N $l,
  16. protected IRequest $request,
  17. ) {
  18. parent::__construct($l);
  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. }