You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RequestTime.php 3.8KB

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