RequestURL.php 2.0 KB

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