1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]> |
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
|
|
|
|
22
|
|
|
namespace OCA\WorkflowEngine\Check; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use OCP\IL10N; |
26
|
|
|
use OCP\WorkflowEngine\ICheck; |
27
|
|
|
use OCP\WorkflowEngine\IManager; |
28
|
|
|
|
29
|
|
|
abstract class AbstractStringCheck implements ICheck { |
30
|
|
|
|
31
|
|
|
/** @var array[] Nested array: [Pattern => [ActualValue => Regex Result]] */ |
32
|
|
|
protected $matches; |
33
|
|
|
|
34
|
|
|
/** @var IL10N */ |
35
|
|
|
protected $l; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param IL10N $l |
39
|
|
|
*/ |
40
|
|
|
public function __construct(IL10N $l) { |
41
|
|
|
$this->l = $l; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
abstract protected function getActualValue(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $operator |
51
|
|
|
* @param string $value |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function executeCheck($operator, $value) { |
55
|
|
|
$actualValue = $this->getActualValue(); |
56
|
|
|
return $this->executeStringCheck($operator, $value, $actualValue); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $operator |
61
|
|
|
* @param string $checkValue |
62
|
|
|
* @param string $actualValue |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
protected function executeStringCheck($operator, $checkValue, $actualValue) { |
66
|
|
|
if ($operator === 'is') { |
67
|
|
|
return $checkValue === $actualValue; |
68
|
|
|
} else if ($operator === '!is') { |
69
|
|
|
return $checkValue !== $actualValue; |
70
|
|
|
} else { |
71
|
|
|
$match = $this->match($checkValue, $actualValue); |
72
|
|
|
if ($operator === 'matches') { |
73
|
|
|
return $match === 1; |
74
|
|
|
} else { |
75
|
|
|
return $match === 0; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $operator |
82
|
|
|
* @param string $value |
83
|
|
|
* @throws \UnexpectedValueException |
84
|
|
|
*/ |
85
|
|
|
public function validateCheck($operator, $value) { |
86
|
|
|
if (!in_array($operator, ['is', '!is', 'matches', '!matches'])) { |
87
|
|
|
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (in_array($operator, ['matches', '!matches']) && |
91
|
|
|
@preg_match($value, null) === false) { |
92
|
|
|
throw new \UnexpectedValueException($this->l->t('The given regular expression is invalid'), 2); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function supportedEntities(): array { |
97
|
|
|
// universal by default |
98
|
|
|
return []; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function isAvailableForScope(int $scope): bool { |
102
|
|
|
// admin only by default |
103
|
|
|
return $scope === IManager::SCOPE_ADMIN; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $pattern |
108
|
|
|
* @param string $subject |
109
|
|
|
* @return int|bool |
110
|
|
|
*/ |
111
|
|
|
protected function match($pattern, $subject) { |
112
|
|
|
$patternHash = md5($pattern); |
113
|
|
|
$subjectHash = md5($subject); |
114
|
|
|
if (isset($this->matches[$patternHash][$subjectHash])) { |
115
|
|
|
return $this->matches[$patternHash][$subjectHash]; |
116
|
|
|
} |
117
|
|
|
if (!isset($this->matches[$patternHash])) { |
118
|
|
|
$this->matches[$patternHash] = []; |
119
|
|
|
} |
120
|
|
|
$this->matches[$patternHash][$subjectHash] = preg_match($pattern, $subject); |
121
|
|
|
return $this->matches[$patternHash][$subjectHash]; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|