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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\WorkflowEngine\Check;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\IL10N;
  29. use OCP\WorkflowEngine\ICheck;
  30. class RequestTime implements ICheck {
  31. public const REGEX_TIME = '([0-1][0-9]|2[0-3]):([0-5][0-9])';
  32. public const REGEX_TIMEZONE = '([a-zA-Z]+(?:\\/[a-zA-Z\-\_]+)+)';
  33. /** @var bool[] */
  34. protected $cachedResults;
  35. /** @var IL10N */
  36. protected $l;
  37. /** @var ITimeFactory */
  38. protected $timeFactory;
  39. /**
  40. * @param ITimeFactory $timeFactory
  41. */
  42. public function __construct(IL10N $l, ITimeFactory $timeFactory) {
  43. $this->l = $l;
  44. $this->timeFactory = $timeFactory;
  45. }
  46. /**
  47. * @param string $operator
  48. * @param string $value
  49. * @return bool
  50. */
  51. public function executeCheck($operator, $value) {
  52. $valueHash = md5($value);
  53. if (isset($this->cachedResults[$valueHash])) {
  54. return $this->cachedResults[$valueHash];
  55. }
  56. $timestamp = $this->timeFactory->getTime();
  57. $values = json_decode($value, true);
  58. $timestamp1 = $this->getTimestamp($timestamp, $values[0]);
  59. $timestamp2 = $this->getTimestamp($timestamp, $values[1]);
  60. if ($timestamp1 < $timestamp2) {
  61. $in = $timestamp1 <= $timestamp && $timestamp <= $timestamp2;
  62. } else {
  63. $in = $timestamp1 <= $timestamp || $timestamp <= $timestamp2;
  64. }
  65. return ($operator === 'in') ? $in : !$in;
  66. }
  67. /**
  68. * @param int $currentTimestamp
  69. * @param string $value Format: "H:i e"
  70. * @return int
  71. */
  72. protected function getTimestamp($currentTimestamp, $value) {
  73. [$time1, $timezone1] = explode(' ', $value);
  74. [$hour1, $minute1] = explode(':', $time1);
  75. $date1 = new \DateTime('now', new \DateTimeZone($timezone1));
  76. $date1->setTimestamp($currentTimestamp);
  77. $date1->setTime($hour1, $minute1);
  78. return $date1->getTimestamp();
  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, ['in', '!in'])) {
  87. throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1);
  88. }
  89. $regexValue = '\"' . self::REGEX_TIME . ' ' . self::REGEX_TIMEZONE . '\"';
  90. $result = preg_match('/^\[' . $regexValue . ',' . $regexValue . '\]$/', $value, $matches);
  91. if (!$result) {
  92. throw new \UnexpectedValueException($this->l->t('The given time span is invalid'), 2);
  93. }
  94. $values = json_decode($value, true);
  95. $time1 = \DateTime::createFromFormat('H:i e', $values[0]);
  96. if ($time1 === false) {
  97. throw new \UnexpectedValueException($this->l->t('The given start time is invalid'), 3);
  98. }
  99. $time2 = \DateTime::createFromFormat('H:i e', $values[1]);
  100. if ($time2 === false) {
  101. throw new \UnexpectedValueException($this->l->t('The given end time is invalid'), 4);
  102. }
  103. }
  104. public function isAvailableForScope(int $scope): bool {
  105. return true;
  106. }
  107. /**
  108. * returns a list of Entities the checker supports. The values must match
  109. * the class name of the entity.
  110. *
  111. * An empty result means the check is universally available.
  112. *
  113. * @since 18.0.0
  114. */
  115. public function supportedEntities(): array {
  116. return [];
  117. }
  118. }