1
0

RequestURL.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\WorkflowEngine\Check;
  25. use OCP\IL10N;
  26. use OCP\IRequest;
  27. class RequestURL extends AbstractStringCheck {
  28. public const CLI = 'cli';
  29. /** @var ?string */
  30. protected $url;
  31. /** @var IRequest */
  32. protected $request;
  33. /**
  34. * @param IL10N $l
  35. * @param IRequest $request
  36. */
  37. public function __construct(IL10N $l, IRequest $request) {
  38. parent::__construct($l);
  39. $this->request = $request;
  40. }
  41. /**
  42. * @param string $operator
  43. * @param string $value
  44. * @return bool
  45. */
  46. public function executeCheck($operator, $value) {
  47. if (\OC::$CLI) {
  48. $actualValue = $this->url = RequestURL::CLI;
  49. } else {
  50. $actualValue = $this->getActualValue();
  51. }
  52. if (in_array($operator, ['is', '!is'])) {
  53. switch ($value) {
  54. case 'webdav':
  55. if ($operator === 'is') {
  56. return $this->isWebDAVRequest();
  57. } else {
  58. return !$this->isWebDAVRequest();
  59. }
  60. }
  61. }
  62. return $this->executeStringCheck($operator, $value, $actualValue);
  63. }
  64. /**
  65. * @return string
  66. */
  67. protected function getActualValue() {
  68. if ($this->url !== null) {
  69. return $this->url;
  70. }
  71. $this->url = $this->request->getServerProtocol() . '://';// E.g. http(s) + ://
  72. $this->url .= $this->request->getServerHost();// E.g. localhost
  73. $this->url .= $this->request->getScriptName();// E.g. /nextcloud/index.php
  74. $this->url .= $this->request->getPathInfo();// E.g. /apps/files_texteditor/ajax/loadfile
  75. return $this->url; // E.g. https://localhost/nextcloud/index.php/apps/files_texteditor/ajax/loadfile
  76. }
  77. protected function isWebDAVRequest(): bool {
  78. if ($this->url === RequestURL::CLI) {
  79. return false;
  80. }
  81. return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && (
  82. $this->request->getPathInfo() === '/webdav' ||
  83. strpos($this->request->getPathInfo(), '/webdav/') === 0 ||
  84. $this->request->getPathInfo() === '/dav/files' ||
  85. strpos($this->request->getPathInfo(), '/dav/files/') === 0
  86. );
  87. }
  88. }