|
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\Session; |
|
13
|
|
|
|
|
14
|
|
|
use Balloon\Filesystem\Node\Collection; |
|
15
|
|
|
use Balloon\Server\User; |
|
16
|
|
|
use Balloon\Session; |
|
17
|
|
|
use MongoDB\BSON\ObjectIdInterface; |
|
18
|
|
|
use MongoDB\BSON\UTCDateTime; |
|
19
|
|
|
use MongoDB\Database; |
|
20
|
|
|
|
|
21
|
|
|
class Factory |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Collection name. |
|
25
|
|
|
*/ |
|
26
|
|
|
public const COLLECTION_NAME = 'sessions'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Database. |
|
30
|
|
|
* |
|
31
|
|
|
* @var Database |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $db; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Initialize. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(Database $db) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->db = $db; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Prepare query. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function prepareQuery(User $user, ?array $query = null): array |
|
47
|
|
|
{ |
|
48
|
|
|
$filter = [ |
|
49
|
|
|
'owner' => $user->getId(), |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
if (!empty($query)) { |
|
53
|
|
|
$filter = [ |
|
54
|
|
|
'$and' => [$filter, $query], |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $filter; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get session. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getOne(User $user, ObjectIdInterface $id): SessionInterface |
|
65
|
|
|
{ |
|
66
|
|
|
$result = $this->db->{self::COLLECTION_NAME}->findOne([ |
|
67
|
|
|
'owner' => $user->getId(), |
|
68
|
|
|
'_id' => $id, |
|
69
|
|
|
]); |
|
70
|
|
|
|
|
71
|
|
|
if ($result === null) { |
|
72
|
|
|
throw new Exception\NotFound('session '.$id.' is not registered'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->build($result); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Delete by name. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function deleteOne(ObjectIdInterface $id): bool |
|
82
|
|
|
{ |
|
83
|
|
|
$this->db->{self::COLLECTION_NAME}->deleteOne([ |
|
84
|
|
|
'_id' => $id, |
|
85
|
|
|
]); |
|
86
|
|
|
|
|
87
|
|
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Update. |
|
92
|
|
|
*/ |
|
93
|
|
|
public function update(User $user, SessionInterface $resource, Collection $parent, $stream): bool |
|
94
|
|
|
{ |
|
95
|
|
|
if ($resource->isFinalized()) { |
|
96
|
|
|
throw new Exception\Closed('This session has been finalized and can not be updated.'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$ctx = $resource->getHashContext(); |
|
100
|
|
|
$size = md5_update_stream($ctx, $stream); |
|
101
|
|
|
rewind($stream); |
|
102
|
|
|
|
|
103
|
|
|
$storage = $parent->getStorage(); |
|
104
|
|
|
$session = $storage->storeTemporaryFile($stream, $user, $resource->getId()); |
|
|
|
|
|
|
105
|
|
|
$size = $resource->getSize() + $size; |
|
106
|
|
|
|
|
107
|
|
|
$data = [ |
|
108
|
|
|
'$set' => [ |
|
109
|
|
|
'size' => $size, |
|
110
|
|
|
'changed' => new UTCDateTime(), |
|
111
|
|
|
], |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
|
|
if ($size % 64 !== 0) { |
|
115
|
|
|
$data['$set']['hash'] = md5_final($ctx); |
|
116
|
|
|
} else { |
|
117
|
|
|
$data['$set']['context'] = serialize($ctx); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$resource->set(array_merge(['size' => $size], $data['$set'])); |
|
121
|
|
|
$this->db->{self::COLLECTION_NAME}->updateOne([ |
|
122
|
|
|
'_id' => $resource->getId(), |
|
123
|
|
|
'owner' => $user->getId(), |
|
124
|
|
|
], $data); |
|
125
|
|
|
|
|
126
|
|
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Add session. |
|
131
|
|
|
*/ |
|
132
|
|
|
public function add(User $user, Collection $parent, $stream): SessionInterface |
|
133
|
|
|
{ |
|
134
|
|
|
$ctx = md5_init(); |
|
135
|
|
|
$size = md5_update_stream($ctx, $stream); |
|
136
|
|
|
rewind($stream); |
|
137
|
|
|
|
|
138
|
|
|
$storage = $parent->getStorage(); |
|
139
|
|
|
$session = $storage->storeTemporaryFile($stream, $user); |
|
140
|
|
|
|
|
141
|
|
|
$resource = [ |
|
142
|
|
|
'_id' => $session, |
|
143
|
|
|
'kind' => 'Session', |
|
144
|
|
|
'created' => new UTCDateTime(), |
|
145
|
|
|
'changed' => new UTCDateTime(), |
|
146
|
|
|
'parent' => $parent->getId(), |
|
147
|
|
|
'size' => $size, |
|
148
|
|
|
'owner' => $user->getId(), |
|
149
|
|
|
]; |
|
150
|
|
|
|
|
151
|
|
|
if ($size % 64 !== 0) { |
|
152
|
|
|
$resource['hash'] = md5_final($ctx); |
|
153
|
|
|
} else { |
|
154
|
|
|
$resource['context'] = serialize($ctx); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
$this->db->{self::COLLECTION_NAME}->insertOne($resource); |
|
158
|
|
|
|
|
159
|
|
|
return $this->build($resource); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Build instance. |
|
164
|
|
|
*/ |
|
165
|
|
|
public function build(array $resource): SessionInterface |
|
166
|
|
|
{ |
|
167
|
|
|
return new Session($resource); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.