|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ownCloud - Documents App |
|
5
|
|
|
* |
|
6
|
|
|
* @author Victor Dubiniuk |
|
7
|
|
|
* @copyright 2013 Victor Dubiniuk [email protected] |
|
8
|
|
|
* |
|
9
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
10
|
|
|
* later. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace OCA\Documents\Db; |
|
14
|
|
|
|
|
15
|
|
|
use OCP\Security\ISecureRandom; |
|
16
|
|
|
|
|
17
|
|
|
use OCA\Documents\Filter; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Session management |
|
21
|
|
|
* |
|
22
|
|
|
* @method string getEsId() |
|
23
|
|
|
* @method int getFileId() |
|
24
|
|
|
* @method string getGenesisUrl() |
|
25
|
|
|
* @method string getOwner() |
|
26
|
|
|
* @method string getGenesisHash() |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
class Session extends \OCA\Documents\Db { |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* DB table |
|
33
|
|
|
*/ |
|
34
|
|
|
const DB_TABLE = '`*PREFIX*documents_session`'; |
|
35
|
|
|
protected $tableName = '`*PREFIX*documents_session`'; |
|
36
|
|
|
|
|
37
|
|
|
protected $insertStatement = 'INSERT INTO `*PREFIX*documents_session` (`es_id`, `genesis_url`, `genesis_hash`, `owner`, `file_id`) |
|
38
|
|
|
VALUES (?, ?, ?, ?, ?)'; |
|
39
|
|
|
|
|
40
|
|
|
protected $loadStatement = 'SELECT * FROM `*PREFIX*documents_session` WHERE `es_id`= ?'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Start a editing session or return an existing one |
|
44
|
|
|
* @param string $uid of the user starting a session |
|
45
|
|
|
* @param \OCA\Documents\File $file - file object |
|
46
|
|
|
* @return array |
|
47
|
|
|
* @throws \Exception |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function start($uid, $file){ |
|
50
|
|
|
// Create a directory to store genesis |
|
51
|
|
|
$genesis = new \OCA\Documents\Genesis($file); |
|
52
|
|
|
|
|
53
|
|
|
$oldSession = new Session(); |
|
54
|
|
|
$oldSession->loadBy('file_id', $file->getFileId()); |
|
55
|
|
|
|
|
56
|
|
|
//If there is no existing session we need to start a new one |
|
57
|
|
|
if (!$oldSession->hasData()){ |
|
58
|
|
|
$newSession = new Session(array( |
|
59
|
|
|
$genesis->getPath(), |
|
60
|
|
|
$genesis->getHash(), |
|
61
|
|
|
$file->getOwner(), |
|
62
|
|
|
$file->getFileId() |
|
63
|
|
|
)); |
|
64
|
|
|
|
|
65
|
|
|
if (!$newSession->insert()){ |
|
66
|
|
|
throw new \Exception('Failed to add session into database'); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$sessionData = $oldSession |
|
71
|
|
|
->loadBy('file_id', $file->getFileId()) |
|
72
|
|
|
->getData() |
|
73
|
|
|
; |
|
74
|
|
|
|
|
75
|
|
|
$memberColor = \OCA\Documents\Helper::getMemberColor($uid); |
|
76
|
|
|
$member = new \OCA\Documents\Db\Member([ |
|
77
|
|
|
$sessionData['es_id'], |
|
78
|
|
|
$uid, |
|
79
|
|
|
$memberColor, |
|
80
|
|
|
time(), |
|
81
|
|
|
intval($file->isPublicShare()), |
|
82
|
|
|
$file->getToken() |
|
83
|
|
|
]); |
|
84
|
|
|
|
|
85
|
|
|
if (!$member->insert()){ |
|
86
|
|
|
throw new \Exception('Failed to add member into database'); |
|
87
|
|
|
} |
|
88
|
|
|
$sessionData['member_id'] = (string) $member->getLastInsertId(); |
|
89
|
|
|
|
|
90
|
|
|
// Do we have OC_Avatar in out disposal? |
|
91
|
|
|
if (\OC::$server->getConfig()->getSystemValue('enable_avatars', true) !== true){ |
|
92
|
|
|
$imageUrl = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw=='; |
|
93
|
|
|
} else { |
|
94
|
|
|
$imageUrl = $uid; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$displayName = $file->isPublicShare() |
|
98
|
|
|
? $uid . ' ' . \OCA\Documents\Db\Member::getGuestPostfix() |
|
99
|
|
|
: \OC::$server->getUserSession()->getUser()->getDisplayName($uid) |
|
100
|
|
|
; |
|
101
|
|
|
$userId = $file->isPublicShare() ? $displayName : \OC::$server->getUserSession()->getUser()->getUID(); |
|
102
|
|
|
$op = new \OCA\Documents\Db\Op(); |
|
103
|
|
|
$op->addMember( |
|
104
|
|
|
$sessionData['es_id'], |
|
105
|
|
|
$sessionData['member_id'], |
|
106
|
|
|
$displayName, |
|
107
|
|
|
$userId, |
|
108
|
|
|
$memberColor, |
|
109
|
|
|
$imageUrl |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
$sessionData['title'] = basename($file->getPath()); |
|
113
|
|
|
$sessionData['permissions'] = $file->getPermissions(); |
|
114
|
|
|
|
|
115
|
|
|
return $sessionData; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public static function cleanUp($esId){ |
|
119
|
|
|
$session = new Session(); |
|
120
|
|
|
$session->deleteBy('es_id', $esId); |
|
121
|
|
|
|
|
122
|
|
|
$member = new \OCA\Documents\Db\Member(); |
|
123
|
|
|
$member->deleteBy('es_id', $esId); |
|
124
|
|
|
|
|
125
|
|
|
$op= new \OCA\Documents\Db\Op(); |
|
126
|
|
|
$op->deleteBy('es_id', $esId); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
public function syncOps($memberId, $currentHead, $clientHead, $clientOps){ |
|
131
|
|
|
$hasOps = count($clientOps)>0; |
|
132
|
|
|
$op = new \OCA\Documents\Db\Op(); |
|
133
|
|
|
|
|
134
|
|
|
// TODO handle the case ($currentHead == "") && ($seqHead != "") |
|
135
|
|
|
if ($clientHead == $currentHead) { |
|
136
|
|
|
// matching heads |
|
137
|
|
|
if ($hasOps) { |
|
138
|
|
|
// incoming ops without conflict |
|
139
|
|
|
// Add incoming ops, respond with a new head |
|
140
|
|
|
$newHead = \OCA\Documents\Db\Op::addOpsArray($this->getEsId(), $memberId, $clientOps); |
|
141
|
|
|
$result = array( |
|
142
|
|
|
'result' => 'added', |
|
143
|
|
|
'head_seq' => $newHead ? $newHead : $currentHead |
|
144
|
|
|
); |
|
145
|
|
|
} else { |
|
146
|
|
|
// no incoming ops (just checking for new ops...) |
|
147
|
|
|
$result = array( |
|
148
|
|
|
'result' => 'new_ops', |
|
149
|
|
|
'ops' => array(), |
|
150
|
|
|
'head_seq' => $currentHead |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
} else { // HEADs do not match |
|
154
|
|
|
$result = array( |
|
155
|
|
|
'result' => $hasOps ? 'conflict' : 'new_ops', |
|
156
|
|
|
'ops' => $op->getOpsAfterJson($this->getEsId(), $clientHead), |
|
157
|
|
|
'head_seq' => $currentHead, |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $result; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function insert(){ |
|
165
|
|
|
$esId = $this->getUniqueSessionId(); |
|
166
|
|
|
array_unshift($this->data, $esId); |
|
167
|
|
|
return parent::insert(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function updateGenesisHash($esId, $genesisHash){ |
|
171
|
|
|
return $this->execute( |
|
172
|
|
|
'UPDATE `*PREFIX*documents_session` SET `genesis_hash`=? WHERE `es_id`=?', |
|
173
|
|
|
array( |
|
174
|
|
|
$genesisHash, $esId |
|
175
|
|
|
) |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function getInfo($esId){ |
|
180
|
|
|
$result = $this->execute(' |
|
181
|
|
|
SELECT `s`.*, COUNT(`m`.`member_id`) AS `users` |
|
182
|
|
|
FROM ' . $this->tableName . ' AS `s` |
|
183
|
|
|
LEFT JOIN `*PREFIX*documents_member` AS `m` ON `s`.`es_id`=`m`.`es_id` |
|
184
|
|
|
AND `m`.`status`=' . Db\Member::MEMBER_STATUS_ACTIVE . ' |
|
185
|
|
|
AND `m`.`uid` != ? |
|
186
|
|
|
WHERE `s`.`es_id` = ? |
|
187
|
|
|
GROUP BY `m`.`es_id` |
|
188
|
|
|
', |
|
189
|
|
|
[ |
|
190
|
|
|
\OC::$server->getUserSession()->getUser()->getUID(), |
|
191
|
|
|
$esId |
|
192
|
|
|
] |
|
193
|
|
|
); |
|
194
|
|
|
|
|
195
|
|
|
$info = $result->fetch(); |
|
196
|
|
|
if (!is_array($info)){ |
|
197
|
|
|
$info = array(); |
|
198
|
|
|
} |
|
199
|
|
|
return $info; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
protected function getUniqueSessionId(){ |
|
203
|
|
|
$testSession = new Session(); |
|
204
|
|
|
do{ |
|
205
|
|
|
$id = \OC::$server->getSecureRandom() |
|
206
|
|
|
->getMediumStrengthGenerator() |
|
207
|
|
|
->generate(30, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS); |
|
208
|
|
|
} while ($testSession->load($id)->hasData()); |
|
209
|
|
|
|
|
210
|
|
|
return $id; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|