RequestTime.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\WorkflowEngine\Check;
  7. use OCP\AppFramework\Utility\ITimeFactory;
  8. use OCP\IL10N;
  9. use OCP\WorkflowEngine\ICheck;
  10. class RequestTime implements ICheck {
  11. public const REGEX_TIME = '([0-1][0-9]|2[0-3]):([0-5][0-9])';
  12. public const REGEX_TIMEZONE = '([a-zA-Z]+(?:\\/[a-zA-Z\-\_]+)+)';
  13. /** @var bool[] */
  14. protected $cachedResults;
  15. /**
  16. * @param ITimeFactory $timeFactory
  17. */
  18. public function __construct(
  19. protected IL10N $l,
  20. protected ITimeFactory $timeFactory,
  21. ) {
  22. }
  23. /**
  24. * @param string $operator
  25. * @param string $value
  26. * @return bool
  27. */
  28. public function executeCheck($operator, $value) {
  29. $valueHash = md5($value);
  30. if (isset($this->cachedResults[$valueHash])) {
  31. return $this->cachedResults[$valueHash];
  32. }
  33. $timestamp = $this->timeFactory->getTime();
  34. $values = json_decode($value, true);
  35. $timestamp1 = $this->getTimestamp($timestamp, $values[0]);
  36. $timestamp2 = $this->getTimestamp($timestamp, $values[1]);
  37. if ($timestamp1 < $timestamp2) {
  38. $in = $timestamp1 <= $timestamp && $timestamp <= $timestamp2;
  39. } else {
  40. $in = $timestamp1 <= $timestamp || $timestamp <= $timestamp2;
  41. }
  42. return ($operator === 'in') ? $in : !$in;
  43. }
  44. /**
  45. * @param int $currentTimestamp
  46. * @param string $value Format: "H:i e"
  47. * @return int
  48. */
  49. protected function getTimestamp($currentTimestamp, $value) {
  50. [$time1, $timezone1] = explode(' ', $value);
  51. [$hour1, $minute1] = explode(':', $time1);
  52. $date1 = new \DateTime('now', new \DateTimeZone($timezone1));
  53. $date1->setTimestamp($currentTimestamp);
  54. $date1->setTime((int)$hour1, (int)$minute1);
  55. return $date1->getTimestamp();
  56. }
  57. /**
  58. * @param string $operator
  59. * @param string $value
  60. * @throws \UnexpectedValueException
  61. */
  62. public function validateCheck($operator, $value) {
  63. if (!in_array($operator, ['in', '!in'])) {
  64. throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
  65. }
  66. $regexValue = '\"' . self::REGEX_TIME . ' ' . self::REGEX_TIMEZONE . '\"';
  67. $result = preg_match('/^\[' . $regexValue . ',' . $regexValue . '\]$/', $value, $matches);
  68. if (!$result) {
  69. throw new \UnexpectedValueException($this->l->t('The given time span is invalid'), 2);
  70. }
  71. $values = json_decode($value, true);
  72. $time1 = \DateTime::createFromFormat('H:i e', (string)$values[0]);
  73. if ($time1 === false) {
  74. throw new \UnexpectedValueException($this->l->t('The given start time is invalid'), 3);
  75. }
  76. $time2 = \DateTime::createFromFormat('H:i e', (string)$values[1]);
  77. if ($time2 === false) {
  78. throw new \UnexpectedValueException($this->l->t('The given end time is invalid'), 4);
  79. }
  80. }
  81. public function isAvailableForScope(int $scope): bool {
  82. return true;
  83. }
  84. /**
  85. * returns a list of Entities the checker supports. The values must match
  86. * the class name of the entity.
  87. *
  88. * An empty result means the check is universally available.
  89. *
  90. * @since 18.0.0
  91. */
  92. public function supportedEntities(): array {
  93. return [];
  94. }
  95. }