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\AppFramework\Utility\ITimeFactory; |
26
|
|
|
use OCP\IL10N; |
27
|
|
|
use OCP\WorkflowEngine\ICheck; |
28
|
|
|
|
29
|
|
|
class RequestTime implements ICheck { |
30
|
|
|
|
31
|
|
|
const REGEX_TIME = '([0-1][0-9]|2[0-3]):([0-5][0-9])'; |
32
|
|
|
const REGEX_TIMEZONE = '([a-zA-Z]+(?:\\/[a-zA-Z\-\_]+)+)'; |
33
|
|
|
|
34
|
|
|
/** @var bool[] */ |
35
|
|
|
protected $cachedResults; |
36
|
|
|
|
37
|
|
|
/** @var IL10N */ |
38
|
|
|
protected $l; |
39
|
|
|
|
40
|
|
|
/** @var ITimeFactory */ |
41
|
|
|
protected $timeFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param ITimeFactory $timeFactory |
45
|
|
|
*/ |
46
|
|
|
public function __construct(IL10N $l, ITimeFactory $timeFactory) { |
47
|
|
|
$this->l = $l; |
48
|
|
|
$this->timeFactory = $timeFactory; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $operator |
53
|
|
|
* @param string $value |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function executeCheck($operator, $value) { |
57
|
|
|
$valueHash = md5($value); |
58
|
|
|
|
59
|
|
|
if (isset($this->cachedResults[$valueHash])) { |
60
|
|
|
return $this->cachedResults[$valueHash]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$timestamp = $this->timeFactory->getTime(); |
64
|
|
|
|
65
|
|
|
$values = json_decode($value, true); |
66
|
|
|
$timestamp1 = $this->getTimestamp($timestamp, $values[0]); |
67
|
|
|
$timestamp2 = $this->getTimestamp($timestamp, $values[1]); |
68
|
|
|
|
69
|
|
|
if ($timestamp1 < $timestamp2) { |
70
|
|
|
$in = $timestamp1 <= $timestamp && $timestamp <= $timestamp2; |
71
|
|
|
} else { |
72
|
|
|
$in = $timestamp1 <= $timestamp || $timestamp <= $timestamp2; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return ($operator === 'in') ? $in : !$in; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param int $currentTimestamp |
80
|
|
|
* @param string $value Format: "H:i e" |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
|
|
protected function getTimestamp($currentTimestamp, $value) { |
84
|
|
|
list($time1, $timezone1) = explode(' ', $value); |
85
|
|
|
list($hour1, $minute1) = explode(':', $time1); |
86
|
|
|
$date1 = new \DateTime('now', new \DateTimeZone($timezone1)); |
87
|
|
|
$date1->setTimestamp($currentTimestamp); |
88
|
|
|
$date1->setTime($hour1, $minute1); |
|
|
|
|
89
|
|
|
|
90
|
|
|
return $date1->getTimestamp(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $operator |
95
|
|
|
* @param string $value |
96
|
|
|
* @throws \UnexpectedValueException |
97
|
|
|
*/ |
98
|
|
|
public function validateCheck($operator, $value) { |
99
|
|
|
if (!in_array($operator, ['in', '!in'])) { |
100
|
|
|
throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$regexValue = '\"' . self::REGEX_TIME . ' ' . self::REGEX_TIMEZONE . '\"'; |
104
|
|
|
$result = preg_match('/^\[' . $regexValue . ',' . $regexValue . '\]$/', $value, $matches); |
105
|
|
|
if (!$result) { |
106
|
|
|
throw new \UnexpectedValueException($this->l->t('The given time span is invalid'), 2); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$values = json_decode($value, true); |
110
|
|
|
$time1 = \DateTime::createFromFormat('H:i e', $values[0]); |
111
|
|
|
if ($time1 === false) { |
112
|
|
|
throw new \UnexpectedValueException($this->l->t('The given start time is invalid'), 3); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$time2 = \DateTime::createFromFormat('H:i e', $values[1]); |
116
|
|
|
if ($time2 === false) { |
117
|
|
|
throw new \UnexpectedValueException($this->l->t('The given end time is invalid'), 4); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function isAvailableForScope(int $scope): bool { |
122
|
|
|
return true; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* returns a list of Entities the checker supports. The values must match |
127
|
|
|
* the class name of the entity. |
128
|
|
|
* |
129
|
|
|
* An empty result means the check is universally available. |
130
|
|
|
* |
131
|
|
|
* @since 18.0.0 |
132
|
|
|
*/ |
133
|
|
|
public function supportedEntities(): array { |
134
|
|
|
return []; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|