RequestURL.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 RequestURL extends AbstractStringCheck {
  10. public const CLI = 'cli';
  11. /** @var ?string */
  12. protected $url;
  13. /** @var IRequest */
  14. protected $request;
  15. /**
  16. * @param IL10N $l
  17. * @param IRequest $request
  18. */
  19. public function __construct(IL10N $l, IRequest $request) {
  20. parent::__construct($l);
  21. $this->request = $request;
  22. }
  23. /**
  24. * @param string $operator
  25. * @param string $value
  26. * @return bool
  27. */
  28. public function executeCheck($operator, $value) {
  29. if (\OC::$CLI) {
  30. $actualValue = $this->url = RequestURL::CLI;
  31. } else {
  32. $actualValue = $this->getActualValue();
  33. }
  34. if (in_array($operator, ['is', '!is'])) {
  35. switch ($value) {
  36. case 'webdav':
  37. if ($operator === 'is') {
  38. return $this->isWebDAVRequest();
  39. } else {
  40. return !$this->isWebDAVRequest();
  41. }
  42. }
  43. }
  44. return $this->executeStringCheck($operator, $value, $actualValue);
  45. }
  46. /**
  47. * @return string
  48. */
  49. protected function getActualValue() {
  50. if ($this->url !== null) {
  51. return $this->url;
  52. }
  53. $this->url = $this->request->getServerProtocol() . '://';// E.g. http(s) + ://
  54. $this->url .= $this->request->getServerHost();// E.g. localhost
  55. $this->url .= $this->request->getScriptName();// E.g. /nextcloud/index.php
  56. $this->url .= $this->request->getPathInfo();// E.g. /apps/files_texteditor/ajax/loadfile
  57. return $this->url; // E.g. https://localhost/nextcloud/index.php/apps/files_texteditor/ajax/loadfile
  58. }
  59. protected function isWebDAVRequest(): bool {
  60. if ($this->url === RequestURL::CLI) {
  61. return false;
  62. }
  63. return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && (
  64. $this->request->getPathInfo() === '/webdav' ||
  65. str_starts_with($this->request->getPathInfo() ?? '', '/webdav/') ||
  66. $this->request->getPathInfo() === '/dav/files' ||
  67. str_starts_with($this->request->getPathInfo() ?? '', '/dav/files/')
  68. );
  69. }
  70. }