RequestUserAgent.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\WorkflowEngine\Check;
  26. use OCP\IL10N;
  27. use OCP\IRequest;
  28. class RequestUserAgent extends AbstractStringCheck {
  29. /** @var IRequest */
  30. protected $request;
  31. /**
  32. * @param IL10N $l
  33. * @param IRequest $request
  34. */
  35. public function __construct(IL10N $l, IRequest $request) {
  36. parent::__construct($l);
  37. $this->request = $request;
  38. }
  39. /**
  40. * @param string $operator
  41. * @param string $value
  42. * @return bool
  43. */
  44. public function executeCheck($operator, $value) {
  45. $actualValue = $this->getActualValue();
  46. if (in_array($operator, ['is', '!is'], true)) {
  47. switch ($value) {
  48. case 'android':
  49. $operator = $operator === 'is' ? 'matches' : '!matches';
  50. $value = IRequest::USER_AGENT_CLIENT_ANDROID;
  51. break;
  52. case 'ios':
  53. $operator = $operator === 'is' ? 'matches' : '!matches';
  54. $value = IRequest::USER_AGENT_CLIENT_IOS;
  55. break;
  56. case 'desktop':
  57. $operator = $operator === 'is' ? 'matches' : '!matches';
  58. $value = IRequest::USER_AGENT_CLIENT_DESKTOP;
  59. break;
  60. case 'mail':
  61. if ($operator === 'is') {
  62. return $this->executeStringCheck('matches', IRequest::USER_AGENT_OUTLOOK_ADDON, $actualValue)
  63. || $this->executeStringCheck('matches', IRequest::USER_AGENT_THUNDERBIRD_ADDON, $actualValue);
  64. }
  65. return $this->executeStringCheck('!matches', IRequest::USER_AGENT_OUTLOOK_ADDON, $actualValue)
  66. && $this->executeStringCheck('!matches', IRequest::USER_AGENT_THUNDERBIRD_ADDON, $actualValue);
  67. }
  68. }
  69. return $this->executeStringCheck($operator, $value, $actualValue);
  70. }
  71. /**
  72. * @return string
  73. */
  74. protected function getActualValue() {
  75. return $this->request->getHeader('User-Agent');
  76. }
  77. public function isAvailableForScope(int $scope): bool {
  78. return true;
  79. }
  80. }