aboutsummaryrefslogtreecommitdiffstats
path: root/apps/workflowengine/lib/Check/RequestURL.php
blob: 43123bddbe999478ec31937a2ca40fab0db9a0f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
 * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\WorkflowEngine\Check;

use OCP\IL10N;
use OCP\IRequest;

class RequestURL extends AbstractStringCheck {
	public const CLI = 'cli';

	/** @var ?string */
	protected $url;

	/** @var IRequest */
	protected $request;

	/**
	 * @param IL10N $l
	 * @param IRequest $request
	 */
	public function __construct(IL10N $l, IRequest $request) {
		parent::__construct($l);
		$this->request = $request;
	}

	/**
	 * @param string $operator
	 * @param string $value
	 * @return bool
	 */
	public function executeCheck($operator, $value) {
		if (\OC::$CLI) {
			$actualValue = $this->url = RequestURL::CLI;
		} else {
			$actualValue = $this->getActualValue();
		}
		if (in_array($operator, ['is', '!is'])) {
			switch ($value) {
				case 'webdav':
					if ($operator === 'is') {
						return $this->isWebDAVRequest();
					} else {
						return !$this->isWebDAVRequest();
					}
			}
		}
		return $this->executeStringCheck($operator, $value, $actualValue);
	}

	/**
	 * @return string
	 */
	protected function getActualValue() {
		if ($this->url !== null) {
			return $this->url;
		}

		$this->url = $this->request->getServerProtocol() . '://';// E.g. http(s) + ://
		$this->url .= $this->request->getServerHost();// E.g. localhost
		$this->url .= $this->request->getScriptName();// E.g. /nextcloud/index.php
		$this->url .= $this->request->getPathInfo();// E.g. /apps/files_texteditor/ajax/loadfile

		return $this->url; // E.g. https://localhost/nextcloud/index.php/apps/files_texteditor/ajax/loadfile
	}

	protected function isWebDAVRequest(): bool {
		if ($this->url === RequestURL::CLI) {
			return false;
		}
		return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && (
			$this->request->getPathInfo() === '/webdav' ||
			str_starts_with($this->request->getPathInfo() ?? '', '/webdav/') ||
			$this->request->getPathInfo() === '/dav/files' ||
			str_starts_with($this->request->getPathInfo() ?? '', '/dav/files/')
		);
	}
}