RequestURL.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 RequestURL extends AbstractStringCheck {
  25. /** @var string */
  26. protected $url;
  27. /** @var IRequest */
  28. protected $request;
  29. /**
  30. * @param IL10N $l
  31. * @param IRequest $request
  32. */
  33. public function __construct(IL10N $l, IRequest $request) {
  34. parent::__construct($l);
  35. $this->request = $request;
  36. }
  37. /**
  38. * @param string $operator
  39. * @param string $value
  40. * @return bool
  41. */
  42. public function executeCheck($operator, $value) {
  43. $actualValue = $this->getActualValue();
  44. if (in_array($operator, ['is', '!is'])) {
  45. switch ($value) {
  46. case 'webdav':
  47. if ($operator === 'is') {
  48. return $this->isWebDAVRequest();
  49. } else {
  50. return !$this->isWebDAVRequest();
  51. }
  52. }
  53. }
  54. return $this->executeStringCheck($operator, $value, $actualValue);
  55. }
  56. /**
  57. * @return string
  58. */
  59. protected function getActualValue() {
  60. if ($this->url !== null) {
  61. return $this->url;
  62. }
  63. $this->url = $this->request->getServerProtocol() . '://';// E.g. http(s) + ://
  64. $this->url .= $this->request->getServerHost();// E.g. localhost
  65. $this->url .= $this->request->getScriptName();// E.g. /nextcloud/index.php
  66. $this->url .= $this->request->getPathInfo();// E.g. /apps/files_texteditor/ajax/loadfile
  67. return $this->url; // E.g. https://localhost/nextcloud/index.php/apps/files_texteditor/ajax/loadfile
  68. }
  69. /**
  70. * @return bool
  71. */
  72. protected function isWebDAVRequest() {
  73. return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && (
  74. $this->request->getPathInfo() === '/webdav' ||
  75. strpos($this->request->getPathInfo(), '/webdav/') === 0 ||
  76. $this->request->getPathInfo() === '/dav/files' ||
  77. strpos($this->request->getPathInfo(), '/dav/files/') === 0
  78. );
  79. }
  80. }