GrommunioSyncState::setState()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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