RequestTime.php 3.1 KB

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