RequestUserAgent.php 2.0 KB

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