RequestTime.php 3.7 KB

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