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
|
|
|
/** |
16
|
|
|
* @method boolean getIsGuest() |
17
|
|
|
* @method string getEsId() |
18
|
|
|
* @method string getToken() |
19
|
|
|
* @method int getStatus() |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
class Member extends \OCA\Documents\Db{ |
23
|
|
|
|
24
|
|
|
const DB_TABLE = '`*PREFIX*documents_member`'; |
25
|
|
|
|
26
|
|
|
const ACTIVITY_THRESHOLD = 90; // 1.5 Minutes |
27
|
|
|
|
28
|
|
|
const MEMBER_STATUS_ACTIVE = 1; |
29
|
|
|
const MEMBER_STATUS_INACTIVE = 2; |
30
|
|
|
|
31
|
|
|
protected $tableName = '`*PREFIX*documents_member`'; |
32
|
|
|
|
33
|
|
|
protected $insertStatement = 'INSERT INTO `*PREFIX*documents_member` (`es_id`, `uid`, `color`, `last_activity`, `is_guest`, `token`) |
34
|
|
|
VALUES (?, ?, ?, ?, ?, ?)'; |
35
|
|
|
|
36
|
|
|
protected $loadStatement = 'SELECT * FROM `*PREFIX*documents_member` WHERE `member_id`= ?'; |
37
|
|
|
|
38
|
|
|
public static function getGuestPostfix(){ |
39
|
|
|
return '(' . \OC::$server->getL10n('documents')->t('guest') . ')'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
public function updateActivity($memberId){ |
44
|
|
|
return $this->execute( |
45
|
|
|
'UPDATE ' . $this->tableName . ' SET `last_activity`=?, `status`=? WHERE `member_id`=?', |
46
|
|
|
array( |
47
|
|
|
time(), |
48
|
|
|
self::MEMBER_STATUS_ACTIVE, |
49
|
|
|
$memberId |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
public function getActiveCollection($esId){ |
56
|
|
|
$result = $this->execute(' |
57
|
|
|
SELECT `es_id`, `member_id` |
58
|
|
|
FROM ' . self::DB_TABLE . ' |
59
|
|
|
WHERE `es_id`= ? |
60
|
|
|
AND `status`=? |
61
|
|
|
', |
62
|
|
|
array( |
63
|
|
|
$esId, |
64
|
|
|
self::MEMBER_STATUS_ACTIVE |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
$members = $result->fetchAll(); |
68
|
|
|
if (!is_array($members)){ |
69
|
|
|
$members = array(); |
70
|
|
|
} |
71
|
|
|
return $members; |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Mark members as inactive |
77
|
|
|
* @param string $esId - session Id |
78
|
|
|
* @return array - list of memberId that were marked as inactive |
79
|
|
|
*/ |
80
|
|
|
public function updateByTimeout($esId){ |
81
|
|
|
$time = $this->getInactivityPeriod(); |
82
|
|
|
|
83
|
|
|
$result = $this->execute(' |
84
|
|
|
SELECT `member_id` |
85
|
|
|
FROM ' . self::DB_TABLE . ' |
86
|
|
|
WHERE `es_id`= ? |
87
|
|
|
AND `last_activity`<? |
88
|
|
|
AND `status`=? |
89
|
|
|
', |
90
|
|
|
array( |
91
|
|
|
$esId, |
92
|
|
|
$time, |
93
|
|
|
self::MEMBER_STATUS_ACTIVE |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$deactivated = $result->fetchAll(); |
98
|
|
|
if (is_array($deactivated) && count($deactivated)){ |
99
|
|
|
$deactivated = array_map( |
100
|
|
|
function($x){ |
101
|
|
|
return ($x['member_id']); |
102
|
|
|
}, $deactivated |
103
|
|
|
); |
104
|
|
|
$this->deactivate($deactivated); |
105
|
|
|
} else { |
106
|
|
|
$deactivated = array(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $deactivated; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Update members to inactive state |
114
|
|
|
* @param array $memberIds |
115
|
|
|
*/ |
116
|
|
|
public function deactivate($memberIds){ |
117
|
|
|
$stmt = $this->buildInQuery('member_id', $memberIds); |
118
|
|
|
array_unshift($memberIds, self::MEMBER_STATUS_INACTIVE); |
119
|
|
|
$this->execute(' |
120
|
|
|
UPDATE ' . $this->tableName . ' |
121
|
|
|
SET `status`=? |
122
|
|
|
WHERE ' . $stmt, |
123
|
|
|
$memberIds |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function getInactivityPeriod(){ |
128
|
|
|
return time() - self::ACTIVITY_THRESHOLD; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|