1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* balloon |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Balloon\App\Api\Hook; |
13
|
|
|
|
14
|
|
|
use Balloon\Filesystem\Exception; |
15
|
|
|
use Balloon\Filesystem\Node\Collection; |
16
|
|
|
use Balloon\Filesystem\Node\File; |
17
|
|
|
use Balloon\Filesystem\Node\NodeInterface; |
18
|
|
|
use Balloon\Hook\AbstractHook; |
19
|
|
|
use MongoDB\BSON\ObjectId; |
20
|
|
|
|
21
|
|
|
class Lock extends AbstractHook |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function preCreateCollection(Collection $parent, string &$name, array &$attributes, bool $clone): void |
27
|
|
|
{ |
28
|
|
|
$this->validateRequest($parent); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function preCreateFile(Collection $parent, string &$name, array &$attributes, bool $clone): void |
35
|
|
|
{ |
36
|
|
|
$this->validateRequest($parent); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function preDeleteCollection(Collection $node, bool $force, ?string $recursion, bool $recursion_first): void |
43
|
|
|
{ |
44
|
|
|
$this->validateRequest($node); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function preRestoreFile(File $node, int $version): void |
51
|
|
|
{ |
52
|
|
|
$this->validateRequest($node); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function prePutFile(File $node, ObjectId $session): void |
59
|
|
|
{ |
60
|
|
|
$this->validateRequest($node); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function preDeleteFile(File $node, bool $force, ?string $recursion, bool $recursion_first): void |
67
|
|
|
{ |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function preSaveNodeAttributes( |
74
|
|
|
NodeInterface $node, |
75
|
|
|
array &$save_attributes, |
76
|
|
|
array &$remove_attributes, |
77
|
|
|
?string $recursion, |
78
|
|
|
bool $recursion_first |
79
|
|
|
): void { |
80
|
|
|
$this->validateRequest($node); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Validate request. |
85
|
|
|
*/ |
86
|
|
|
protected function validateRequest(NodeInterface $node): bool |
87
|
|
|
{ |
88
|
|
|
if (isset($_SERVER['ORIG_SCRIPT_NAME']) && preg_match('#^/index.php/api/#', $_SERVER['ORIG_SCRIPT_NAME']) && $node->isLocked()) { |
89
|
|
|
$token = $_SERVER['HTTP_LOCK_TOKEN'] ?? null; |
90
|
|
|
$lock = $node->getLock(); |
91
|
|
|
|
92
|
|
|
if ($token === null) { |
93
|
|
|
throw new Exception\Locked('node is temporarily locked and can not be changed'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($lock['id'] !== $token) { |
97
|
|
|
throw new Exception\LockIdMissmatch('node is temporarily locked and requested id does not match the lock token'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|