@@ -28,139 +28,139 @@ |
||
28 | 28 | |
29 | 29 | class Expiration { |
30 | 30 | |
31 | - // how long do we keep files in the trash bin if no other value is defined in the config file (unit: days) |
|
32 | - const DEFAULT_RETENTION_OBLIGATION = 30; |
|
33 | - const NO_OBLIGATION = -1; |
|
34 | - |
|
35 | - /** @var ITimeFactory */ |
|
36 | - private $timeFactory; |
|
37 | - |
|
38 | - /** @var string */ |
|
39 | - private $retentionObligation; |
|
40 | - |
|
41 | - /** @var int */ |
|
42 | - private $minAge; |
|
43 | - |
|
44 | - /** @var int */ |
|
45 | - private $maxAge; |
|
46 | - |
|
47 | - /** @var bool */ |
|
48 | - private $canPurgeToSaveSpace; |
|
49 | - |
|
50 | - public function __construct(IConfig $config,ITimeFactory $timeFactory){ |
|
51 | - $this->timeFactory = $timeFactory; |
|
52 | - $this->retentionObligation = $config->getSystemValue('trashbin_retention_obligation', 'auto'); |
|
53 | - |
|
54 | - if ($this->retentionObligation !== 'disabled') { |
|
55 | - $this->parseRetentionObligation(); |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Is trashbin expiration enabled |
|
61 | - * @return bool |
|
62 | - */ |
|
63 | - public function isEnabled(){ |
|
64 | - return $this->retentionObligation !== 'disabled'; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Check if given timestamp in expiration range |
|
69 | - * @param int $timestamp |
|
70 | - * @param bool $quotaExceeded |
|
71 | - * @return bool |
|
72 | - */ |
|
73 | - public function isExpired($timestamp, $quotaExceeded = false){ |
|
74 | - // No expiration if disabled |
|
75 | - if (!$this->isEnabled()) { |
|
76 | - return false; |
|
77 | - } |
|
78 | - |
|
79 | - // Purge to save space (if allowed) |
|
80 | - if ($quotaExceeded && $this->canPurgeToSaveSpace) { |
|
81 | - return true; |
|
82 | - } |
|
83 | - |
|
84 | - $time = $this->timeFactory->getTime(); |
|
85 | - // Never expire dates in future e.g. misconfiguration or negative time |
|
86 | - // adjustment |
|
87 | - if ($time<$timestamp) { |
|
88 | - return false; |
|
89 | - } |
|
90 | - |
|
91 | - // Purge as too old |
|
92 | - if ($this->maxAge !== self::NO_OBLIGATION) { |
|
93 | - $maxTimestamp = $time - ($this->maxAge * 86400); |
|
94 | - $isOlderThanMax = $timestamp < $maxTimestamp; |
|
95 | - } else { |
|
96 | - $isOlderThanMax = false; |
|
97 | - } |
|
98 | - |
|
99 | - if ($this->minAge !== self::NO_OBLIGATION) { |
|
100 | - // older than Min obligation and we are running out of quota? |
|
101 | - $minTimestamp = $time - ($this->minAge * 86400); |
|
102 | - $isMinReached = ($timestamp < $minTimestamp) && $quotaExceeded; |
|
103 | - } else { |
|
104 | - $isMinReached = false; |
|
105 | - } |
|
106 | - |
|
107 | - return $isOlderThanMax || $isMinReached; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @return bool|int |
|
112 | - */ |
|
113 | - public function getMaxAgeAsTimestamp() { |
|
114 | - $maxAge = false; |
|
115 | - if ($this->isEnabled() && $this->maxAge !== self::NO_OBLIGATION) { |
|
116 | - $time = $this->timeFactory->getTime(); |
|
117 | - $maxAge = $time - ($this->maxAge * 86400); |
|
118 | - } |
|
119 | - return $maxAge; |
|
120 | - } |
|
121 | - |
|
122 | - private function parseRetentionObligation(){ |
|
123 | - $splitValues = explode(',', $this->retentionObligation); |
|
124 | - if (!isset($splitValues[0])) { |
|
125 | - $minValue = self::DEFAULT_RETENTION_OBLIGATION; |
|
126 | - } else { |
|
127 | - $minValue = trim($splitValues[0]); |
|
128 | - } |
|
129 | - |
|
130 | - if (!isset($splitValues[1]) && $minValue === 'auto') { |
|
131 | - $maxValue = 'auto'; |
|
132 | - } elseif (!isset($splitValues[1])) { |
|
133 | - $maxValue = self::DEFAULT_RETENTION_OBLIGATION; |
|
134 | - } else { |
|
135 | - $maxValue = trim($splitValues[1]); |
|
136 | - } |
|
137 | - |
|
138 | - if ($minValue === 'auto' && $maxValue === 'auto') { |
|
139 | - // Default: Keep for 30 days but delete anytime if space needed |
|
140 | - $this->minAge = self::DEFAULT_RETENTION_OBLIGATION; |
|
141 | - $this->maxAge = self::NO_OBLIGATION; |
|
142 | - $this->canPurgeToSaveSpace = true; |
|
143 | - } elseif ($minValue !== 'auto' && $maxValue === 'auto') { |
|
144 | - // Keep for X days but delete anytime if space needed |
|
145 | - $this->minAge = intval($minValue); |
|
146 | - $this->maxAge = self::NO_OBLIGATION; |
|
147 | - $this->canPurgeToSaveSpace = true; |
|
148 | - } elseif ($minValue === 'auto' && $maxValue !== 'auto') { |
|
149 | - // Delete anytime if space needed, Delete all older than max automatically |
|
150 | - $this->minAge = self::NO_OBLIGATION; |
|
151 | - $this->maxAge = intval($maxValue); |
|
152 | - $this->canPurgeToSaveSpace = true; |
|
153 | - } elseif ($minValue !== 'auto' && $maxValue !== 'auto') { |
|
154 | - // Delete all older than max OR older than min if space needed |
|
155 | - |
|
156 | - // Max < Min as per https://github.com/owncloud/core/issues/16300 |
|
157 | - if ($maxValue < $minValue) { |
|
158 | - $maxValue = $minValue; |
|
159 | - } |
|
160 | - |
|
161 | - $this->minAge = intval($minValue); |
|
162 | - $this->maxAge = intval($maxValue); |
|
163 | - $this->canPurgeToSaveSpace = false; |
|
164 | - } |
|
165 | - } |
|
31 | + // how long do we keep files in the trash bin if no other value is defined in the config file (unit: days) |
|
32 | + const DEFAULT_RETENTION_OBLIGATION = 30; |
|
33 | + const NO_OBLIGATION = -1; |
|
34 | + |
|
35 | + /** @var ITimeFactory */ |
|
36 | + private $timeFactory; |
|
37 | + |
|
38 | + /** @var string */ |
|
39 | + private $retentionObligation; |
|
40 | + |
|
41 | + /** @var int */ |
|
42 | + private $minAge; |
|
43 | + |
|
44 | + /** @var int */ |
|
45 | + private $maxAge; |
|
46 | + |
|
47 | + /** @var bool */ |
|
48 | + private $canPurgeToSaveSpace; |
|
49 | + |
|
50 | + public function __construct(IConfig $config,ITimeFactory $timeFactory){ |
|
51 | + $this->timeFactory = $timeFactory; |
|
52 | + $this->retentionObligation = $config->getSystemValue('trashbin_retention_obligation', 'auto'); |
|
53 | + |
|
54 | + if ($this->retentionObligation !== 'disabled') { |
|
55 | + $this->parseRetentionObligation(); |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Is trashbin expiration enabled |
|
61 | + * @return bool |
|
62 | + */ |
|
63 | + public function isEnabled(){ |
|
64 | + return $this->retentionObligation !== 'disabled'; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Check if given timestamp in expiration range |
|
69 | + * @param int $timestamp |
|
70 | + * @param bool $quotaExceeded |
|
71 | + * @return bool |
|
72 | + */ |
|
73 | + public function isExpired($timestamp, $quotaExceeded = false){ |
|
74 | + // No expiration if disabled |
|
75 | + if (!$this->isEnabled()) { |
|
76 | + return false; |
|
77 | + } |
|
78 | + |
|
79 | + // Purge to save space (if allowed) |
|
80 | + if ($quotaExceeded && $this->canPurgeToSaveSpace) { |
|
81 | + return true; |
|
82 | + } |
|
83 | + |
|
84 | + $time = $this->timeFactory->getTime(); |
|
85 | + // Never expire dates in future e.g. misconfiguration or negative time |
|
86 | + // adjustment |
|
87 | + if ($time<$timestamp) { |
|
88 | + return false; |
|
89 | + } |
|
90 | + |
|
91 | + // Purge as too old |
|
92 | + if ($this->maxAge !== self::NO_OBLIGATION) { |
|
93 | + $maxTimestamp = $time - ($this->maxAge * 86400); |
|
94 | + $isOlderThanMax = $timestamp < $maxTimestamp; |
|
95 | + } else { |
|
96 | + $isOlderThanMax = false; |
|
97 | + } |
|
98 | + |
|
99 | + if ($this->minAge !== self::NO_OBLIGATION) { |
|
100 | + // older than Min obligation and we are running out of quota? |
|
101 | + $minTimestamp = $time - ($this->minAge * 86400); |
|
102 | + $isMinReached = ($timestamp < $minTimestamp) && $quotaExceeded; |
|
103 | + } else { |
|
104 | + $isMinReached = false; |
|
105 | + } |
|
106 | + |
|
107 | + return $isOlderThanMax || $isMinReached; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @return bool|int |
|
112 | + */ |
|
113 | + public function getMaxAgeAsTimestamp() { |
|
114 | + $maxAge = false; |
|
115 | + if ($this->isEnabled() && $this->maxAge !== self::NO_OBLIGATION) { |
|
116 | + $time = $this->timeFactory->getTime(); |
|
117 | + $maxAge = $time - ($this->maxAge * 86400); |
|
118 | + } |
|
119 | + return $maxAge; |
|
120 | + } |
|
121 | + |
|
122 | + private function parseRetentionObligation(){ |
|
123 | + $splitValues = explode(',', $this->retentionObligation); |
|
124 | + if (!isset($splitValues[0])) { |
|
125 | + $minValue = self::DEFAULT_RETENTION_OBLIGATION; |
|
126 | + } else { |
|
127 | + $minValue = trim($splitValues[0]); |
|
128 | + } |
|
129 | + |
|
130 | + if (!isset($splitValues[1]) && $minValue === 'auto') { |
|
131 | + $maxValue = 'auto'; |
|
132 | + } elseif (!isset($splitValues[1])) { |
|
133 | + $maxValue = self::DEFAULT_RETENTION_OBLIGATION; |
|
134 | + } else { |
|
135 | + $maxValue = trim($splitValues[1]); |
|
136 | + } |
|
137 | + |
|
138 | + if ($minValue === 'auto' && $maxValue === 'auto') { |
|
139 | + // Default: Keep for 30 days but delete anytime if space needed |
|
140 | + $this->minAge = self::DEFAULT_RETENTION_OBLIGATION; |
|
141 | + $this->maxAge = self::NO_OBLIGATION; |
|
142 | + $this->canPurgeToSaveSpace = true; |
|
143 | + } elseif ($minValue !== 'auto' && $maxValue === 'auto') { |
|
144 | + // Keep for X days but delete anytime if space needed |
|
145 | + $this->minAge = intval($minValue); |
|
146 | + $this->maxAge = self::NO_OBLIGATION; |
|
147 | + $this->canPurgeToSaveSpace = true; |
|
148 | + } elseif ($minValue === 'auto' && $maxValue !== 'auto') { |
|
149 | + // Delete anytime if space needed, Delete all older than max automatically |
|
150 | + $this->minAge = self::NO_OBLIGATION; |
|
151 | + $this->maxAge = intval($maxValue); |
|
152 | + $this->canPurgeToSaveSpace = true; |
|
153 | + } elseif ($minValue !== 'auto' && $maxValue !== 'auto') { |
|
154 | + // Delete all older than max OR older than min if space needed |
|
155 | + |
|
156 | + // Max < Min as per https://github.com/owncloud/core/issues/16300 |
|
157 | + if ($maxValue < $minValue) { |
|
158 | + $maxValue = $minValue; |
|
159 | + } |
|
160 | + |
|
161 | + $this->minAge = intval($minValue); |
|
162 | + $this->maxAge = intval($maxValue); |
|
163 | + $this->canPurgeToSaveSpace = false; |
|
164 | + } |
|
165 | + } |
|
166 | 166 | } |
@@ -25,18 +25,18 @@ |
||
25 | 25 | $installedVersion = $config->getAppValue('files_trashbin', 'installed_version'); |
26 | 26 | |
27 | 27 | if (version_compare($installedVersion, '0.6.4', '<')) { |
28 | - $isExpirationEnabled = $config->getSystemValue('trashbin_auto_expire', true); |
|
29 | - $oldObligation = $config->getSystemValue('trashbin_retention_obligation', null); |
|
28 | + $isExpirationEnabled = $config->getSystemValue('trashbin_auto_expire', true); |
|
29 | + $oldObligation = $config->getSystemValue('trashbin_retention_obligation', null); |
|
30 | 30 | |
31 | - $newObligation = 'auto'; |
|
32 | - if ($isExpirationEnabled) { |
|
33 | - if (!is_null($oldObligation)) { |
|
34 | - $newObligation = strval($oldObligation) . ', auto'; |
|
35 | - } |
|
36 | - } else { |
|
37 | - $newObligation = 'disabled'; |
|
38 | - } |
|
31 | + $newObligation = 'auto'; |
|
32 | + if ($isExpirationEnabled) { |
|
33 | + if (!is_null($oldObligation)) { |
|
34 | + $newObligation = strval($oldObligation) . ', auto'; |
|
35 | + } |
|
36 | + } else { |
|
37 | + $newObligation = 'disabled'; |
|
38 | + } |
|
39 | 39 | |
40 | - $config->setSystemValue('trashbin_retention_obligation', $newObligation); |
|
41 | - $config->deleteSystemValue('trashbin_auto_expire'); |
|
40 | + $config->setSystemValue('trashbin_retention_obligation', $newObligation); |
|
41 | + $config->deleteSystemValue('trashbin_auto_expire'); |
|
42 | 42 | } |
@@ -32,10 +32,10 @@ |
||
32 | 32 | |
33 | 33 | \OCA\Files\App::getNavigationManager()->add( |
34 | 34 | array( |
35 | - "id" => 'trashbin', |
|
36 | - "appname" => 'files_trashbin', |
|
37 | - "script" => 'list.php', |
|
38 | - "order" => 50, |
|
39 | - "name" => $l->t('Deleted files') |
|
35 | + "id" => 'trashbin', |
|
36 | + "appname" => 'files_trashbin', |
|
37 | + "script" => 'list.php', |
|
38 | + "order" => 50, |
|
39 | + "name" => $l->t('Deleted files') |
|
40 | 40 | ) |
41 | 41 | ); |
@@ -26,27 +26,27 @@ |
||
26 | 26 | |
27 | 27 | class RequestTime extends Controller { |
28 | 28 | |
29 | - /** |
|
30 | - * @NoAdminRequired |
|
31 | - * |
|
32 | - * @param string $search |
|
33 | - * @return JSONResponse |
|
34 | - */ |
|
35 | - public function getTimezones($search = '') { |
|
36 | - $timezones = \DateTimeZone::listIdentifiers(); |
|
29 | + /** |
|
30 | + * @NoAdminRequired |
|
31 | + * |
|
32 | + * @param string $search |
|
33 | + * @return JSONResponse |
|
34 | + */ |
|
35 | + public function getTimezones($search = '') { |
|
36 | + $timezones = \DateTimeZone::listIdentifiers(); |
|
37 | 37 | |
38 | - if ($search !== '') { |
|
39 | - $timezones = array_filter($timezones, function ($timezone) use ($search) { |
|
40 | - return strpos(strtolower($timezone), strtolower($search)) !== false; |
|
41 | - }); |
|
42 | - } |
|
38 | + if ($search !== '') { |
|
39 | + $timezones = array_filter($timezones, function ($timezone) use ($search) { |
|
40 | + return strpos(strtolower($timezone), strtolower($search)) !== false; |
|
41 | + }); |
|
42 | + } |
|
43 | 43 | |
44 | - $timezones = array_slice($timezones, 0, 10); |
|
44 | + $timezones = array_slice($timezones, 0, 10); |
|
45 | 45 | |
46 | - $response = []; |
|
47 | - foreach ($timezones as $timezone) { |
|
48 | - $response[$timezone] = $timezone; |
|
49 | - } |
|
50 | - return new JSONResponse($response); |
|
51 | - } |
|
46 | + $response = []; |
|
47 | + foreach ($timezones as $timezone) { |
|
48 | + $response[$timezone] = $timezone; |
|
49 | + } |
|
50 | + return new JSONResponse($response); |
|
51 | + } |
|
52 | 52 | } |
@@ -23,54 +23,54 @@ |
||
23 | 23 | |
24 | 24 | class Application extends \OCP\AppFramework\App { |
25 | 25 | |
26 | - public function __construct() { |
|
27 | - parent::__construct('workflowengine'); |
|
26 | + public function __construct() { |
|
27 | + parent::__construct('workflowengine'); |
|
28 | 28 | |
29 | - $this->getContainer()->registerAlias('FlowOperationsController', 'OCA\WorkflowEngine\Controller\FlowOperations'); |
|
30 | - $this->getContainer()->registerAlias('RequestTimeController', 'OCA\WorkflowEngine\Controller\RequestTime'); |
|
31 | - } |
|
29 | + $this->getContainer()->registerAlias('FlowOperationsController', 'OCA\WorkflowEngine\Controller\FlowOperations'); |
|
30 | + $this->getContainer()->registerAlias('RequestTimeController', 'OCA\WorkflowEngine\Controller\RequestTime'); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Register all hooks and listeners |
|
35 | - */ |
|
36 | - public function registerHooksAndListeners() { |
|
37 | - $dispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
38 | - $dispatcher->addListener( |
|
39 | - 'OCP\WorkflowEngine::loadAdditionalSettingScripts', |
|
40 | - function() { |
|
41 | - if (!function_exists('style')) { |
|
42 | - // This is hacky, but we need to load the template class |
|
43 | - class_exists('OCP\Template', true); |
|
44 | - } |
|
33 | + /** |
|
34 | + * Register all hooks and listeners |
|
35 | + */ |
|
36 | + public function registerHooksAndListeners() { |
|
37 | + $dispatcher = $this->getContainer()->getServer()->getEventDispatcher(); |
|
38 | + $dispatcher->addListener( |
|
39 | + 'OCP\WorkflowEngine::loadAdditionalSettingScripts', |
|
40 | + function() { |
|
41 | + if (!function_exists('style')) { |
|
42 | + // This is hacky, but we need to load the template class |
|
43 | + class_exists('OCP\Template', true); |
|
44 | + } |
|
45 | 45 | |
46 | - style('workflowengine', [ |
|
47 | - 'admin', |
|
48 | - ]); |
|
46 | + style('workflowengine', [ |
|
47 | + 'admin', |
|
48 | + ]); |
|
49 | 49 | |
50 | - script('core', [ |
|
51 | - 'oc-backbone-webdav', |
|
52 | - 'systemtags/systemtags', |
|
53 | - 'systemtags/systemtagmodel', |
|
54 | - 'systemtags/systemtagscollection', |
|
55 | - ]); |
|
50 | + script('core', [ |
|
51 | + 'oc-backbone-webdav', |
|
52 | + 'systemtags/systemtags', |
|
53 | + 'systemtags/systemtagmodel', |
|
54 | + 'systemtags/systemtagscollection', |
|
55 | + ]); |
|
56 | 56 | |
57 | - vendor_script('jsTimezoneDetect/jstz'); |
|
57 | + vendor_script('jsTimezoneDetect/jstz'); |
|
58 | 58 | |
59 | - script('workflowengine', [ |
|
60 | - 'admin', |
|
59 | + script('workflowengine', [ |
|
60 | + 'admin', |
|
61 | 61 | |
62 | - // Check plugins |
|
63 | - 'filemimetypeplugin', |
|
64 | - 'filesizeplugin', |
|
65 | - 'filesystemtagsplugin', |
|
66 | - 'requestremoteaddressplugin', |
|
67 | - 'requesttimeplugin', |
|
68 | - 'requesturlplugin', |
|
69 | - 'requestuseragentplugin', |
|
70 | - 'usergroupmembershipplugin', |
|
71 | - ]); |
|
72 | - }, |
|
73 | - -100 |
|
74 | - ); |
|
75 | - } |
|
62 | + // Check plugins |
|
63 | + 'filemimetypeplugin', |
|
64 | + 'filesizeplugin', |
|
65 | + 'filesystemtagsplugin', |
|
66 | + 'requestremoteaddressplugin', |
|
67 | + 'requesttimeplugin', |
|
68 | + 'requesturlplugin', |
|
69 | + 'requestuseragentplugin', |
|
70 | + 'usergroupmembershipplugin', |
|
71 | + ]); |
|
72 | + }, |
|
73 | + -100 |
|
74 | + ); |
|
75 | + } |
|
76 | 76 | } |
@@ -29,134 +29,134 @@ |
||
29 | 29 | |
30 | 30 | class FileMimeType extends AbstractStringCheck { |
31 | 31 | |
32 | - /** @var array */ |
|
33 | - protected $mimeType; |
|
34 | - |
|
35 | - /** @var IRequest */ |
|
36 | - protected $request; |
|
37 | - |
|
38 | - /** @var IMimeTypeDetector */ |
|
39 | - protected $mimeTypeDetector; |
|
40 | - |
|
41 | - /** @var IStorage */ |
|
42 | - protected $storage; |
|
43 | - |
|
44 | - /** @var string */ |
|
45 | - protected $path; |
|
46 | - |
|
47 | - /** |
|
48 | - * @param IL10N $l |
|
49 | - * @param IRequest $request |
|
50 | - * @param IMimeTypeDetector $mimeTypeDetector |
|
51 | - */ |
|
52 | - public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) { |
|
53 | - parent::__construct($l); |
|
54 | - $this->request = $request; |
|
55 | - $this->mimeTypeDetector = $mimeTypeDetector; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param IStorage $storage |
|
60 | - * @param string $path |
|
61 | - */ |
|
62 | - public function setFileInfo(IStorage $storage, $path) { |
|
63 | - $this->storage = $storage; |
|
64 | - $this->path = $path; |
|
65 | - if (!isset($this->mimeType[$this->storage->getId()][$this->path]) |
|
66 | - || $this->mimeType[$this->storage->getId()][$this->path] === '') { |
|
67 | - $this->mimeType[$this->storage->getId()][$this->path] = null; |
|
68 | - } |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @return string |
|
73 | - */ |
|
74 | - protected function getActualValue() { |
|
75 | - if ($this->mimeType[$this->storage->getId()][$this->path] !== null) { |
|
76 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
77 | - } |
|
78 | - |
|
79 | - $this->mimeType[$this->storage->getId()][$this->path] = ''; |
|
80 | - if ($this->isWebDAVRequest()) { |
|
81 | - if ($this->request->getMethod() === 'PUT') { |
|
82 | - $path = $this->request->getPathInfo(); |
|
83 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path); |
|
84 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
85 | - } |
|
86 | - } |
|
87 | - |
|
88 | - if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
|
89 | - $files = $this->request->getUploadedFile('files'); |
|
90 | - if (isset($files['type'][0])) { |
|
91 | - $mimeType = $files['type'][0]; |
|
92 | - if ($this->mimeType === 'application/octet-stream') { |
|
93 | - // Maybe not... |
|
94 | - $mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]); |
|
95 | - if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
96 | - $mimeType = $mimeTypeTest; |
|
97 | - } else { |
|
98 | - $mimeTypeTest = $this->mimeTypeDetector->detect($files['tmp_name'][0]); |
|
99 | - if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
100 | - $mimeType = $mimeTypeTest; |
|
101 | - } |
|
102 | - } |
|
103 | - } |
|
104 | - $this->mimeType[$this->storage->getId()][$this->path] = $mimeType; |
|
105 | - return $mimeType; |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->storage->getMimeType($this->path); |
|
110 | - if ($this->mimeType[$this->storage->getId()][$this->path] === 'application/octet-stream') { |
|
111 | - $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath(); |
|
112 | - } |
|
113 | - |
|
114 | - return $this->mimeType[$this->storage->getId()][$this->path]; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @return string |
|
119 | - */ |
|
120 | - protected function detectMimetypeFromPath() { |
|
121 | - $mimeType = $this->mimeTypeDetector->detectPath($this->path); |
|
122 | - if ($mimeType !== 'application/octet-stream' && $mimeType !== false) { |
|
123 | - return $mimeType; |
|
124 | - } |
|
125 | - |
|
126 | - if ($this->storage->instanceOfStorage('\OC\Files\Storage\Local') |
|
127 | - || $this->storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
128 | - || $this->storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { |
|
129 | - $localFile = $this->storage->getLocalFile($this->path); |
|
130 | - if ($localFile !== false) { |
|
131 | - $mimeType = $this->mimeTypeDetector->detect($localFile); |
|
132 | - if ($mimeType !== false) { |
|
133 | - return $mimeType; |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - return 'application/octet-stream'; |
|
138 | - } else { |
|
139 | - $handle = $this->storage->fopen($this->path, 'r'); |
|
140 | - $data = fread($handle, 8024); |
|
141 | - fclose($handle); |
|
142 | - $mimeType = $this->mimeTypeDetector->detectString($data); |
|
143 | - if ($mimeType !== false) { |
|
144 | - return $mimeType; |
|
145 | - } |
|
146 | - |
|
147 | - return 'application/octet-stream'; |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * @return bool |
|
153 | - */ |
|
154 | - protected function isWebDAVRequest() { |
|
155 | - return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && ( |
|
156 | - $this->request->getPathInfo() === '/webdav' || |
|
157 | - strpos($this->request->getPathInfo(), '/webdav/') === 0 || |
|
158 | - $this->request->getPathInfo() === '/dav/files' || |
|
159 | - strpos($this->request->getPathInfo(), '/dav/files/') === 0 |
|
160 | - ); |
|
161 | - } |
|
32 | + /** @var array */ |
|
33 | + protected $mimeType; |
|
34 | + |
|
35 | + /** @var IRequest */ |
|
36 | + protected $request; |
|
37 | + |
|
38 | + /** @var IMimeTypeDetector */ |
|
39 | + protected $mimeTypeDetector; |
|
40 | + |
|
41 | + /** @var IStorage */ |
|
42 | + protected $storage; |
|
43 | + |
|
44 | + /** @var string */ |
|
45 | + protected $path; |
|
46 | + |
|
47 | + /** |
|
48 | + * @param IL10N $l |
|
49 | + * @param IRequest $request |
|
50 | + * @param IMimeTypeDetector $mimeTypeDetector |
|
51 | + */ |
|
52 | + public function __construct(IL10N $l, IRequest $request, IMimeTypeDetector $mimeTypeDetector) { |
|
53 | + parent::__construct($l); |
|
54 | + $this->request = $request; |
|
55 | + $this->mimeTypeDetector = $mimeTypeDetector; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param IStorage $storage |
|
60 | + * @param string $path |
|
61 | + */ |
|
62 | + public function setFileInfo(IStorage $storage, $path) { |
|
63 | + $this->storage = $storage; |
|
64 | + $this->path = $path; |
|
65 | + if (!isset($this->mimeType[$this->storage->getId()][$this->path]) |
|
66 | + || $this->mimeType[$this->storage->getId()][$this->path] === '') { |
|
67 | + $this->mimeType[$this->storage->getId()][$this->path] = null; |
|
68 | + } |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @return string |
|
73 | + */ |
|
74 | + protected function getActualValue() { |
|
75 | + if ($this->mimeType[$this->storage->getId()][$this->path] !== null) { |
|
76 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
77 | + } |
|
78 | + |
|
79 | + $this->mimeType[$this->storage->getId()][$this->path] = ''; |
|
80 | + if ($this->isWebDAVRequest()) { |
|
81 | + if ($this->request->getMethod() === 'PUT') { |
|
82 | + $path = $this->request->getPathInfo(); |
|
83 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->mimeTypeDetector->detectPath($path); |
|
84 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
85 | + } |
|
86 | + } |
|
87 | + |
|
88 | + if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
|
89 | + $files = $this->request->getUploadedFile('files'); |
|
90 | + if (isset($files['type'][0])) { |
|
91 | + $mimeType = $files['type'][0]; |
|
92 | + if ($this->mimeType === 'application/octet-stream') { |
|
93 | + // Maybe not... |
|
94 | + $mimeTypeTest = $this->mimeTypeDetector->detectPath($files['name'][0]); |
|
95 | + if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
96 | + $mimeType = $mimeTypeTest; |
|
97 | + } else { |
|
98 | + $mimeTypeTest = $this->mimeTypeDetector->detect($files['tmp_name'][0]); |
|
99 | + if ($mimeTypeTest !== 'application/octet-stream' && $mimeTypeTest !== false) { |
|
100 | + $mimeType = $mimeTypeTest; |
|
101 | + } |
|
102 | + } |
|
103 | + } |
|
104 | + $this->mimeType[$this->storage->getId()][$this->path] = $mimeType; |
|
105 | + return $mimeType; |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->storage->getMimeType($this->path); |
|
110 | + if ($this->mimeType[$this->storage->getId()][$this->path] === 'application/octet-stream') { |
|
111 | + $this->mimeType[$this->storage->getId()][$this->path] = $this->detectMimetypeFromPath(); |
|
112 | + } |
|
113 | + |
|
114 | + return $this->mimeType[$this->storage->getId()][$this->path]; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @return string |
|
119 | + */ |
|
120 | + protected function detectMimetypeFromPath() { |
|
121 | + $mimeType = $this->mimeTypeDetector->detectPath($this->path); |
|
122 | + if ($mimeType !== 'application/octet-stream' && $mimeType !== false) { |
|
123 | + return $mimeType; |
|
124 | + } |
|
125 | + |
|
126 | + if ($this->storage->instanceOfStorage('\OC\Files\Storage\Local') |
|
127 | + || $this->storage->instanceOfStorage('\OC\Files\Storage\Home') |
|
128 | + || $this->storage->instanceOfStorage('\OC\Files\ObjectStore\HomeObjectStoreStorage')) { |
|
129 | + $localFile = $this->storage->getLocalFile($this->path); |
|
130 | + if ($localFile !== false) { |
|
131 | + $mimeType = $this->mimeTypeDetector->detect($localFile); |
|
132 | + if ($mimeType !== false) { |
|
133 | + return $mimeType; |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + return 'application/octet-stream'; |
|
138 | + } else { |
|
139 | + $handle = $this->storage->fopen($this->path, 'r'); |
|
140 | + $data = fread($handle, 8024); |
|
141 | + fclose($handle); |
|
142 | + $mimeType = $this->mimeTypeDetector->detectString($data); |
|
143 | + if ($mimeType !== false) { |
|
144 | + return $mimeType; |
|
145 | + } |
|
146 | + |
|
147 | + return 'application/octet-stream'; |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * @return bool |
|
153 | + */ |
|
154 | + protected function isWebDAVRequest() { |
|
155 | + return substr($this->request->getScriptName(), 0 - strlen('/remote.php')) === '/remote.php' && ( |
|
156 | + $this->request->getPathInfo() === '/webdav' || |
|
157 | + strpos($this->request->getPathInfo(), '/webdav/') === 0 || |
|
158 | + $this->request->getPathInfo() === '/dav/files' || |
|
159 | + strpos($this->request->getPathInfo(), '/dav/files/') === 0 |
|
160 | + ); |
|
161 | + } |
|
162 | 162 | } |
@@ -28,94 +28,94 @@ |
||
28 | 28 | |
29 | 29 | abstract class AbstractStringCheck implements ICheck { |
30 | 30 | |
31 | - /** @var array[] Nested array: [Pattern => [ActualValue => Regex Result]] */ |
|
32 | - protected $matches; |
|
31 | + /** @var array[] Nested array: [Pattern => [ActualValue => Regex Result]] */ |
|
32 | + protected $matches; |
|
33 | 33 | |
34 | - /** @var IL10N */ |
|
35 | - protected $l; |
|
34 | + /** @var IL10N */ |
|
35 | + protected $l; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param IL10N $l |
|
39 | - */ |
|
40 | - public function __construct(IL10N $l) { |
|
41 | - $this->l = $l; |
|
42 | - } |
|
37 | + /** |
|
38 | + * @param IL10N $l |
|
39 | + */ |
|
40 | + public function __construct(IL10N $l) { |
|
41 | + $this->l = $l; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * @param IStorage $storage |
|
46 | - * @param string $path |
|
47 | - */ |
|
48 | - public function setFileInfo(IStorage $storage, $path) { |
|
49 | - // Nothing changes here with a different path |
|
50 | - } |
|
44 | + /** |
|
45 | + * @param IStorage $storage |
|
46 | + * @param string $path |
|
47 | + */ |
|
48 | + public function setFileInfo(IStorage $storage, $path) { |
|
49 | + // Nothing changes here with a different path |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @return string |
|
54 | - */ |
|
55 | - abstract protected function getActualValue(); |
|
52 | + /** |
|
53 | + * @return string |
|
54 | + */ |
|
55 | + abstract protected function getActualValue(); |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param string $operator |
|
59 | - * @param string $value |
|
60 | - * @return bool |
|
61 | - */ |
|
62 | - public function executeCheck($operator, $value) { |
|
63 | - $actualValue = $this->getActualValue(); |
|
64 | - return $this->executeStringCheck($operator, $value, $actualValue); |
|
65 | - } |
|
57 | + /** |
|
58 | + * @param string $operator |
|
59 | + * @param string $value |
|
60 | + * @return bool |
|
61 | + */ |
|
62 | + public function executeCheck($operator, $value) { |
|
63 | + $actualValue = $this->getActualValue(); |
|
64 | + return $this->executeStringCheck($operator, $value, $actualValue); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @param string $operator |
|
69 | - * @param string $checkValue |
|
70 | - * @param string $actualValue |
|
71 | - * @return bool |
|
72 | - */ |
|
73 | - protected function executeStringCheck($operator, $checkValue, $actualValue) { |
|
74 | - if ($operator === 'is') { |
|
75 | - return $checkValue === $actualValue; |
|
76 | - } else if ($operator === '!is') { |
|
77 | - return $checkValue !== $actualValue; |
|
78 | - } else { |
|
79 | - $match = $this->match($checkValue, $actualValue); |
|
80 | - if ($operator === 'matches') { |
|
81 | - return $match === 1; |
|
82 | - } else { |
|
83 | - return $match === 0; |
|
84 | - } |
|
85 | - } |
|
86 | - } |
|
67 | + /** |
|
68 | + * @param string $operator |
|
69 | + * @param string $checkValue |
|
70 | + * @param string $actualValue |
|
71 | + * @return bool |
|
72 | + */ |
|
73 | + protected function executeStringCheck($operator, $checkValue, $actualValue) { |
|
74 | + if ($operator === 'is') { |
|
75 | + return $checkValue === $actualValue; |
|
76 | + } else if ($operator === '!is') { |
|
77 | + return $checkValue !== $actualValue; |
|
78 | + } else { |
|
79 | + $match = $this->match($checkValue, $actualValue); |
|
80 | + if ($operator === 'matches') { |
|
81 | + return $match === 1; |
|
82 | + } else { |
|
83 | + return $match === 0; |
|
84 | + } |
|
85 | + } |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * @param string $operator |
|
90 | - * @param string $value |
|
91 | - * @throws \UnexpectedValueException |
|
92 | - */ |
|
93 | - public function validateCheck($operator, $value) { |
|
94 | - if (!in_array($operator, ['is', '!is', 'matches', '!matches'])) { |
|
95 | - throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
|
96 | - } |
|
88 | + /** |
|
89 | + * @param string $operator |
|
90 | + * @param string $value |
|
91 | + * @throws \UnexpectedValueException |
|
92 | + */ |
|
93 | + public function validateCheck($operator, $value) { |
|
94 | + if (!in_array($operator, ['is', '!is', 'matches', '!matches'])) { |
|
95 | + throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
|
96 | + } |
|
97 | 97 | |
98 | - if (in_array($operator, ['matches', '!matches']) && |
|
99 | - @preg_match($value, null) === false) { |
|
100 | - throw new \UnexpectedValueException($this->l->t('The given regular expression is invalid'), 2); |
|
101 | - } |
|
102 | - } |
|
98 | + if (in_array($operator, ['matches', '!matches']) && |
|
99 | + @preg_match($value, null) === false) { |
|
100 | + throw new \UnexpectedValueException($this->l->t('The given regular expression is invalid'), 2); |
|
101 | + } |
|
102 | + } |
|
103 | 103 | |
104 | - /** |
|
105 | - * @param string $pattern |
|
106 | - * @param string $subject |
|
107 | - * @return int|bool |
|
108 | - */ |
|
109 | - protected function match($pattern, $subject) { |
|
110 | - $patternHash = md5($pattern); |
|
111 | - $subjectHash = md5($subject); |
|
112 | - if (isset($this->matches[$patternHash][$subjectHash])) { |
|
113 | - return $this->matches[$patternHash][$subjectHash]; |
|
114 | - } |
|
115 | - if (!isset($this->matches[$patternHash])) { |
|
116 | - $this->matches[$patternHash] = []; |
|
117 | - } |
|
118 | - $this->matches[$patternHash][$subjectHash] = preg_match($pattern, $subject); |
|
119 | - return $this->matches[$patternHash][$subjectHash]; |
|
120 | - } |
|
104 | + /** |
|
105 | + * @param string $pattern |
|
106 | + * @param string $subject |
|
107 | + * @return int|bool |
|
108 | + */ |
|
109 | + protected function match($pattern, $subject) { |
|
110 | + $patternHash = md5($pattern); |
|
111 | + $subjectHash = md5($subject); |
|
112 | + if (isset($this->matches[$patternHash][$subjectHash])) { |
|
113 | + return $this->matches[$patternHash][$subjectHash]; |
|
114 | + } |
|
115 | + if (!isset($this->matches[$patternHash])) { |
|
116 | + $this->matches[$patternHash] = []; |
|
117 | + } |
|
118 | + $this->matches[$patternHash][$subjectHash] = preg_match($pattern, $subject); |
|
119 | + return $this->matches[$patternHash][$subjectHash]; |
|
120 | + } |
|
121 | 121 | } |
@@ -30,90 +30,90 @@ |
||
30 | 30 | |
31 | 31 | class FileSize implements ICheck { |
32 | 32 | |
33 | - /** @var int */ |
|
34 | - protected $size; |
|
35 | - |
|
36 | - /** @var IL10N */ |
|
37 | - protected $l; |
|
38 | - |
|
39 | - /** @var IRequest */ |
|
40 | - protected $request; |
|
41 | - |
|
42 | - /** |
|
43 | - * @param IL10N $l |
|
44 | - * @param IRequest $request |
|
45 | - */ |
|
46 | - public function __construct(IL10N $l, IRequest $request) { |
|
47 | - $this->l = $l; |
|
48 | - $this->request = $request; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * @param IStorage $storage |
|
53 | - * @param string $path |
|
54 | - */ |
|
55 | - public function setFileInfo(IStorage $storage, $path) { |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * @param string $operator |
|
60 | - * @param string $value |
|
61 | - * @return bool |
|
62 | - */ |
|
63 | - public function executeCheck($operator, $value) { |
|
64 | - $size = $this->getFileSizeFromHeader(); |
|
65 | - |
|
66 | - $value = Util::computerFileSize($value); |
|
67 | - if ($size !== false) { |
|
68 | - switch ($operator) { |
|
69 | - case 'less': |
|
70 | - return $size < $value; |
|
71 | - case '!less': |
|
72 | - return $size >= $value; |
|
73 | - case 'greater': |
|
74 | - return $size > $value; |
|
75 | - case '!greater': |
|
76 | - return $size <= $value; |
|
77 | - } |
|
78 | - } |
|
79 | - return false; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @param string $operator |
|
84 | - * @param string $value |
|
85 | - * @throws \UnexpectedValueException |
|
86 | - */ |
|
87 | - public function validateCheck($operator, $value) { |
|
88 | - if (!in_array($operator, ['less', '!less', 'greater', '!greater'])) { |
|
89 | - throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
|
90 | - } |
|
91 | - |
|
92 | - if (!preg_match('/^[0-9]+[ ]?[kmgt]?b$/i', $value)) { |
|
93 | - throw new \UnexpectedValueException($this->l->t('The given file size is invalid'), 2); |
|
94 | - } |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @return string |
|
99 | - */ |
|
100 | - protected function getFileSizeFromHeader() { |
|
101 | - if ($this->size !== null) { |
|
102 | - return $this->size; |
|
103 | - } |
|
104 | - |
|
105 | - $size = $this->request->getHeader('OC-Total-Length'); |
|
106 | - if ($size === null) { |
|
107 | - if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
|
108 | - $size = $this->request->getHeader('Content-Length'); |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - if ($size === null) { |
|
113 | - $size = false; |
|
114 | - } |
|
115 | - |
|
116 | - $this->size = $size; |
|
117 | - return $this->size; |
|
118 | - } |
|
33 | + /** @var int */ |
|
34 | + protected $size; |
|
35 | + |
|
36 | + /** @var IL10N */ |
|
37 | + protected $l; |
|
38 | + |
|
39 | + /** @var IRequest */ |
|
40 | + protected $request; |
|
41 | + |
|
42 | + /** |
|
43 | + * @param IL10N $l |
|
44 | + * @param IRequest $request |
|
45 | + */ |
|
46 | + public function __construct(IL10N $l, IRequest $request) { |
|
47 | + $this->l = $l; |
|
48 | + $this->request = $request; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * @param IStorage $storage |
|
53 | + * @param string $path |
|
54 | + */ |
|
55 | + public function setFileInfo(IStorage $storage, $path) { |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * @param string $operator |
|
60 | + * @param string $value |
|
61 | + * @return bool |
|
62 | + */ |
|
63 | + public function executeCheck($operator, $value) { |
|
64 | + $size = $this->getFileSizeFromHeader(); |
|
65 | + |
|
66 | + $value = Util::computerFileSize($value); |
|
67 | + if ($size !== false) { |
|
68 | + switch ($operator) { |
|
69 | + case 'less': |
|
70 | + return $size < $value; |
|
71 | + case '!less': |
|
72 | + return $size >= $value; |
|
73 | + case 'greater': |
|
74 | + return $size > $value; |
|
75 | + case '!greater': |
|
76 | + return $size <= $value; |
|
77 | + } |
|
78 | + } |
|
79 | + return false; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @param string $operator |
|
84 | + * @param string $value |
|
85 | + * @throws \UnexpectedValueException |
|
86 | + */ |
|
87 | + public function validateCheck($operator, $value) { |
|
88 | + if (!in_array($operator, ['less', '!less', 'greater', '!greater'])) { |
|
89 | + throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
|
90 | + } |
|
91 | + |
|
92 | + if (!preg_match('/^[0-9]+[ ]?[kmgt]?b$/i', $value)) { |
|
93 | + throw new \UnexpectedValueException($this->l->t('The given file size is invalid'), 2); |
|
94 | + } |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @return string |
|
99 | + */ |
|
100 | + protected function getFileSizeFromHeader() { |
|
101 | + if ($this->size !== null) { |
|
102 | + return $this->size; |
|
103 | + } |
|
104 | + |
|
105 | + $size = $this->request->getHeader('OC-Total-Length'); |
|
106 | + if ($size === null) { |
|
107 | + if (in_array($this->request->getMethod(), ['POST', 'PUT'])) { |
|
108 | + $size = $this->request->getHeader('Content-Length'); |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + if ($size === null) { |
|
113 | + $size = false; |
|
114 | + } |
|
115 | + |
|
116 | + $this->size = $size; |
|
117 | + return $this->size; |
|
118 | + } |
|
119 | 119 | } |
@@ -29,101 +29,101 @@ |
||
29 | 29 | |
30 | 30 | class RequestTime implements ICheck { |
31 | 31 | |
32 | - const REGEX_TIME = '([0-1][0-9]|2[0-3]):([0-5][0-9])'; |
|
33 | - const REGEX_TIMEZONE = '([a-zA-Z]+(?:\\/[a-zA-Z\-\_]+)+)'; |
|
34 | - |
|
35 | - /** @var bool[] */ |
|
36 | - protected $cachedResults; |
|
37 | - |
|
38 | - /** @var IL10N */ |
|
39 | - protected $l; |
|
40 | - |
|
41 | - /** @var ITimeFactory */ |
|
42 | - protected $timeFactory; |
|
43 | - |
|
44 | - /** |
|
45 | - * @param ITimeFactory $timeFactory |
|
46 | - */ |
|
47 | - public function __construct(IL10N $l, ITimeFactory $timeFactory) { |
|
48 | - $this->l = $l; |
|
49 | - $this->timeFactory = $timeFactory; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @param IStorage $storage |
|
54 | - * @param string $path |
|
55 | - */ |
|
56 | - public function setFileInfo(IStorage $storage, $path) { |
|
57 | - // A different path doesn't change time, so nothing to do here. |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * @param string $operator |
|
62 | - * @param string $value |
|
63 | - * @return bool |
|
64 | - */ |
|
65 | - public function executeCheck($operator, $value) { |
|
66 | - $valueHash = md5($value); |
|
67 | - |
|
68 | - if (isset($this->cachedResults[$valueHash])) { |
|
69 | - return $this->cachedResults[$valueHash]; |
|
70 | - } |
|
71 | - |
|
72 | - $timestamp = $this->timeFactory->getTime(); |
|
73 | - |
|
74 | - $values = json_decode($value, true); |
|
75 | - $timestamp1 = $this->getTimestamp($timestamp, $values[0]); |
|
76 | - $timestamp2 = $this->getTimestamp($timestamp, $values[1]); |
|
77 | - |
|
78 | - if ($timestamp1 < $timestamp2) { |
|
79 | - $in = $timestamp1 <= $timestamp && $timestamp <= $timestamp2; |
|
80 | - } else { |
|
81 | - $in = $timestamp1 <= $timestamp || $timestamp <= $timestamp2; |
|
82 | - } |
|
83 | - |
|
84 | - return ($operator === 'in') ? $in : !$in; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @param int $currentTimestamp |
|
89 | - * @param string $value Format: "H:i e" |
|
90 | - * @return int |
|
91 | - */ |
|
92 | - protected function getTimestamp($currentTimestamp, $value) { |
|
93 | - list($time1, $timezone1) = explode(' ', $value); |
|
94 | - list($hour1, $minute1) = explode(':', $time1); |
|
95 | - $date1 = new \DateTime('now', new \DateTimeZone($timezone1)); |
|
96 | - $date1->setTimestamp($currentTimestamp); |
|
97 | - $date1->setTime($hour1, $minute1); |
|
98 | - |
|
99 | - return $date1->getTimestamp(); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $operator |
|
104 | - * @param string $value |
|
105 | - * @throws \UnexpectedValueException |
|
106 | - */ |
|
107 | - public function validateCheck($operator, $value) { |
|
108 | - if (!in_array($operator, ['in', '!in'])) { |
|
109 | - throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
|
110 | - } |
|
111 | - |
|
112 | - $regexValue = '\"' . self::REGEX_TIME . ' ' . self::REGEX_TIMEZONE . '\"'; |
|
113 | - $result = preg_match('/^\[' . $regexValue . ',' . $regexValue . '\]$/', $value, $matches); |
|
114 | - if (!$result) { |
|
115 | - throw new \UnexpectedValueException($this->l->t('The given time span is invalid'), 2); |
|
116 | - } |
|
117 | - |
|
118 | - $values = json_decode($value, true); |
|
119 | - $time1 = \DateTime::createFromFormat('H:i e', $values[0]); |
|
120 | - if ($time1 === false) { |
|
121 | - throw new \UnexpectedValueException($this->l->t('The given start time is invalid'), 3); |
|
122 | - } |
|
123 | - |
|
124 | - $time2 = \DateTime::createFromFormat('H:i e', $values[1]); |
|
125 | - if ($time2 === false) { |
|
126 | - throw new \UnexpectedValueException($this->l->t('The given end time is invalid'), 4); |
|
127 | - } |
|
128 | - } |
|
32 | + const REGEX_TIME = '([0-1][0-9]|2[0-3]):([0-5][0-9])'; |
|
33 | + const REGEX_TIMEZONE = '([a-zA-Z]+(?:\\/[a-zA-Z\-\_]+)+)'; |
|
34 | + |
|
35 | + /** @var bool[] */ |
|
36 | + protected $cachedResults; |
|
37 | + |
|
38 | + /** @var IL10N */ |
|
39 | + protected $l; |
|
40 | + |
|
41 | + /** @var ITimeFactory */ |
|
42 | + protected $timeFactory; |
|
43 | + |
|
44 | + /** |
|
45 | + * @param ITimeFactory $timeFactory |
|
46 | + */ |
|
47 | + public function __construct(IL10N $l, ITimeFactory $timeFactory) { |
|
48 | + $this->l = $l; |
|
49 | + $this->timeFactory = $timeFactory; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @param IStorage $storage |
|
54 | + * @param string $path |
|
55 | + */ |
|
56 | + public function setFileInfo(IStorage $storage, $path) { |
|
57 | + // A different path doesn't change time, so nothing to do here. |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * @param string $operator |
|
62 | + * @param string $value |
|
63 | + * @return bool |
|
64 | + */ |
|
65 | + public function executeCheck($operator, $value) { |
|
66 | + $valueHash = md5($value); |
|
67 | + |
|
68 | + if (isset($this->cachedResults[$valueHash])) { |
|
69 | + return $this->cachedResults[$valueHash]; |
|
70 | + } |
|
71 | + |
|
72 | + $timestamp = $this->timeFactory->getTime(); |
|
73 | + |
|
74 | + $values = json_decode($value, true); |
|
75 | + $timestamp1 = $this->getTimestamp($timestamp, $values[0]); |
|
76 | + $timestamp2 = $this->getTimestamp($timestamp, $values[1]); |
|
77 | + |
|
78 | + if ($timestamp1 < $timestamp2) { |
|
79 | + $in = $timestamp1 <= $timestamp && $timestamp <= $timestamp2; |
|
80 | + } else { |
|
81 | + $in = $timestamp1 <= $timestamp || $timestamp <= $timestamp2; |
|
82 | + } |
|
83 | + |
|
84 | + return ($operator === 'in') ? $in : !$in; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @param int $currentTimestamp |
|
89 | + * @param string $value Format: "H:i e" |
|
90 | + * @return int |
|
91 | + */ |
|
92 | + protected function getTimestamp($currentTimestamp, $value) { |
|
93 | + list($time1, $timezone1) = explode(' ', $value); |
|
94 | + list($hour1, $minute1) = explode(':', $time1); |
|
95 | + $date1 = new \DateTime('now', new \DateTimeZone($timezone1)); |
|
96 | + $date1->setTimestamp($currentTimestamp); |
|
97 | + $date1->setTime($hour1, $minute1); |
|
98 | + |
|
99 | + return $date1->getTimestamp(); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $operator |
|
104 | + * @param string $value |
|
105 | + * @throws \UnexpectedValueException |
|
106 | + */ |
|
107 | + public function validateCheck($operator, $value) { |
|
108 | + if (!in_array($operator, ['in', '!in'])) { |
|
109 | + throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
|
110 | + } |
|
111 | + |
|
112 | + $regexValue = '\"' . self::REGEX_TIME . ' ' . self::REGEX_TIMEZONE . '\"'; |
|
113 | + $result = preg_match('/^\[' . $regexValue . ',' . $regexValue . '\]$/', $value, $matches); |
|
114 | + if (!$result) { |
|
115 | + throw new \UnexpectedValueException($this->l->t('The given time span is invalid'), 2); |
|
116 | + } |
|
117 | + |
|
118 | + $values = json_decode($value, true); |
|
119 | + $time1 = \DateTime::createFromFormat('H:i e', $values[0]); |
|
120 | + if ($time1 === false) { |
|
121 | + throw new \UnexpectedValueException($this->l->t('The given start time is invalid'), 3); |
|
122 | + } |
|
123 | + |
|
124 | + $time2 = \DateTime::createFromFormat('H:i e', $values[1]); |
|
125 | + if ($time2 === false) { |
|
126 | + throw new \UnexpectedValueException($this->l->t('The given end time is invalid'), 4); |
|
127 | + } |
|
128 | + } |
|
129 | 129 | } |