RequestUserAgent.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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'], true)) {
  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. case 'mail':
  57. if ($operator === 'is') {
  58. return $this->executeStringCheck('matches', IRequest::USER_AGENT_OUTLOOK_ADDON, $actualValue)
  59. || $this->executeStringCheck('matches', IRequest::USER_AGENT_THUNDERBIRD_ADDON, $actualValue);
  60. }
  61. return $this->executeStringCheck('!matches', IRequest::USER_AGENT_OUTLOOK_ADDON, $actualValue)
  62. && $this->executeStringCheck('!matches', IRequest::USER_AGENT_THUNDERBIRD_ADDON, $actualValue);
  63. }
  64. }
  65. return $this->executeStringCheck($operator, $value, $actualValue);
  66. }
  67. /**
  68. * @return string
  69. */
  70. protected function getActualValue() {
  71. return (string) $this->request->getHeader('User-Agent');
  72. }
  73. }