1 | <?php |
||
2 | |||
3 | /* |
||
4 | * SPDX-License-Identifier: AGPL-3.0-only |
||
5 | * SPDX-FileCopyrightText: Copyright 2016 - 2018 Kopano b.v. |
||
6 | * SPDX-FileCopyrightText: Copyright 2020 - 2025 grommunio GmbH |
||
7 | * |
||
8 | * Class for handling sync state. |
||
9 | */ |
||
10 | |||
11 | namespace grommunio\DAV; |
||
12 | |||
13 | class GrommunioSyncState { |
||
14 | private $db; |
||
15 | private $logger; |
||
16 | |||
17 | /** |
||
18 | * Constructor. |
||
19 | * |
||
20 | * @param GLogger $logger |
||
21 | * @param string $dbstring |
||
22 | */ |
||
23 | public function __construct($logger, $dbstring) { |
||
24 | $this->logger = $logger; |
||
25 | $this->logger->trace("Using db %s", $dbstring); |
||
26 | $this->db = new \PDO($dbstring); |
||
27 | |||
28 | $query = "CREATE TABLE IF NOT EXISTS gdav_sync_state ( |
||
29 | id VARCHAR(255), folderid VARCHAR(255), value TEXT, |
||
30 | PRIMARY KEY (id, folderid)); |
||
31 | CREATE TABLE IF NOT EXISTS gdav_sync_appttsref ( |
||
32 | sourcekey VARCHAR(255), folderid VARCHAR(255), appttsref VARCHAR(255), |
||
33 | PRIMARY KEY (sourcekey, folderid)); |
||
34 | CREATE INDEX IF NOT EXISTS idx_appttsref ON gdav_sync_appttsref(appttsref);"; |
||
35 | |||
36 | $this->db->exec($query); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Fetch state information for a folderId (e.g. calenderId) and an id (uuid). |
||
41 | * |
||
42 | * @param string $folderid |
||
43 | * @param string $id |
||
44 | * |
||
45 | * @return null|string |
||
46 | */ |
||
47 | public function getState($folderid, $id) { |
||
48 | $query = "SELECT value FROM gdav_sync_state WHERE folderid = :folderid AND id = :id"; |
||
49 | $statement = $this->db->prepare($query); |
||
50 | $statement->bindParam(":folderid", $folderid); |
||
51 | $statement->bindParam(":id", $id); |
||
52 | $statement->execute(); |
||
53 | $result = $statement->fetch(); |
||
54 | if (!$result) { |
||
55 | return null; |
||
56 | } |
||
57 | |||
58 | return $result['value']; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Set state information for a folderId (e.g. calenderId) and an id (uuid). |
||
63 | * The state information is the sync token for ICS. |
||
64 | * |
||
65 | * @param string $folderid |
||
66 | * @param string $id |
||
67 | * @param string $value |
||
68 | */ |
||
69 | public function setState($folderid, $id, $value) { |
||
70 | $query = "REPLACE INTO gdav_sync_state (id, folderid, value) VALUES(:id, :folderid, :value)"; |
||
71 | $statement = $this->db->prepare($query); |
||
72 | $statement->bindParam(":folderid", $folderid); |
||
73 | $statement->bindParam(":id", $id); |
||
74 | $statement->bindParam(":value", $value); |
||
75 | $statement->execute(); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Set the APPTTSREF (custom URL) for a folderId and source key. |
||
80 | * This is needed for detecting the URL of deleted items reported by ICS. |
||
81 | * |
||
82 | * @param string $folderid |
||
83 | * @param string $sourcekey |
||
84 | * @param string $appttsref |
||
85 | */ |
||
86 | public function rememberAppttsref($folderid, $sourcekey, $appttsref) { |
||
87 | $query = "REPLACE INTO gdav_sync_appttsref (folderid, sourcekey, appttsref) VALUES(:folderid, :sourcekey, :appttsref)"; |
||
88 | $statement = $this->db->prepare($query); |
||
89 | $statement->bindParam(":folderid", $folderid); |
||
90 | $statement->bindParam(":sourcekey", $sourcekey); |
||
91 | $statement->bindParam(":appttsref", $appttsref); |
||
92 | $statement->execute(); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Get the APPTTSREF (custom URL) for a folderId and source key. |
||
97 | * This is needed for detecting the URL of deleted items reported by ICS. |
||
98 | * |
||
99 | * @param string $folderid |
||
100 | * @param string $sourcekey |
||
101 | * |
||
102 | * @return null|string |
||
103 | */ |
||
104 | public function getAppttsref($folderid, $sourcekey) { |
||
105 | $query = "SELECT appttsref FROM gdav_sync_appttsref WHERE folderid = :folderid AND sourcekey = :sourcekey"; |
||
106 | $statement = $this->db->prepare($query); |
||
107 | $statement->bindParam(":folderid", $folderid); |
||
108 | $statement->bindParam(":sourcekey", $sourcekey); |
||
109 | $statement->execute(); |
||
110 | $result = $statement->fetch(); |
||
111 | if (!$result) { |
||
112 | return null; |
||
113 | } |
||
114 | |||
115 | return $result['appttsref']; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Get the sourcekey from the saved APPTTSREF (custom URL) and a folderId. |
||
120 | * This is the last resort when searching for an item in the store fails. |
||
121 | * |
||
122 | * @param string $folderid |
||
123 | * @param string $appttsref |
||
124 | * |
||
125 | * @return null|string |
||
126 | */ |
||
127 | public function getSourcekey($folderid, $appttsref) { |
||
128 | $query = "SELECT sourcekey FROM gdav_sync_appttsref WHERE folderid = :folderid AND appttsref = :appttsref"; |
||
129 | $statement = $this->db->prepare($query); |
||
130 | $statement->bindParam(":folderid", $folderid); |
||
131 | $statement->bindParam(":appttsref", $appttsref); |
||
132 | $statement->execute(); |
||
133 | $result = $statement->fetch(); |
||
134 | if (!$result) { |
||
135 | return null; |
||
136 | } |
||
137 | |||
138 | return $result['sourcekey']; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Fetch id(token) information for a folderId (e.g. calenderId). |
||
143 | * |
||
144 | * @param string $folderid |
||
145 | * |
||
146 | * @return null|string |
||
147 | */ |
||
148 | public function getCurrentToken($folderId) { |
||
0 ignored issues
–
show
|
|||
149 | $query = "SELECT id FROM gdav_sync_state WHERE folderid = :folderid"; |
||
150 | $statement = $this->db->prepare($query); |
||
151 | $statement->bindParam(":folderid", $folderid); |
||
152 | $statement->execute(); |
||
153 | $result = $statement->fetch(); |
||
154 | if (!$result) { |
||
155 | return null; |
||
156 | } |
||
157 | |||
158 | return $result['id']; |
||
159 | } |
||
160 | } |
||
161 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.