ctucx.git: tinyDAV

[php] Cal-/ CardDAV server with a simple web-GUI based on SabreDAV

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201 
202 
203 
204 
205 
206 
207 
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
<?php

class GenericCollectionManager {
	private $db;

//	public $dataFolder         = PATH.'/data/';
//	public $collectionsFile    = 'addressbooks/addressbooks.json';
//	public $collectionsFolder  = 'addressbooks/';
//	public $collectionType     = 'Addressbook';
//	public $datafield          = 'carddata';


//	public function newCollection ($username, $uri, array $properties) {}
//	public function newObject ($collectionId, $uri, $data, $extraData) {}
//	public function newObjectUpdate ($collectionId, $uri, $data, $extraData) {}

	public function __construct (JSONDB $database) {
		$this->db = $database;
	}

	//
	//  Collections
	//

	public function collectionExists ($collectionId) {
		return (!$this->getCollection($collectionId)) ? false : true;
	}

	public function getCollection ($collectionId) {
		$result = $this->db->select('*')
			->from($this->collectionsFile)
			->where(['id' => $collectionId])
			->get();

		if (!isset($result[0])) return false;

		return $result[0];
	}

	public function getCollections ($username) {
		$result = $this->db->select('*')
			->from($this->collectionsFile)
			->where(['principaluri' => $username])
			->get();

		return $result;
	}

	public function getHighestCollectionId () {
		$data = $this->db->select('id')
			->from($this->collectionsFile)
			->order_by('id', JSONDB::ASC)
			->get();
		
		return end($data)['id'];
	}


	public function createCollection ($username, $uri, array $properties) {
		if(!preg_match('/^[\w-]+$/', $uri)) throw new Exception('URI contains not allowed characters.');

		$collections = $this->getCollections($username);
		foreach ($collections as $collection) {
			if ($collection['uri'] == $uri) throw new Exception('An '.strtolower($this->collectionType).' with this URI already exist.');
		}

		$collection = $this->newCollection($username, $uri, $properties);
		$this->db->insert($this->collectionsFile, $collection);
		mkdir($this->dataFolder.$this->collectionsFolder.$collection['id']);
		mkdir($this->dataFolder.$this->collectionsFolder.$collection['id'].'/data');

		return $collection['id'];
	}

	public function updateCollection ($collectionId, $properties) {
		if (!$this->exists($id)) throw new Exception($this->collectionType.' doesn\'t exist!');

		$this->db->update($properties)
			->from($this->collectionsFile)
			->where(['id' => $collectionId])
			->trigger();

		$this->addChange($collectionId, '', 2);
	}

	public function deleteCollection ($collectionId) {
		$this->db->delete()
			->from($this->collectionsFile)
			->where(['id' => $collectionId])
			->trigger();

		Helpers::delete($this->dataFolder.$this->collectionsFolder.$id);
	}


	//
	// Objects
	//

	public function getHighestObjectId ($collectionId) {
		$data = $this->db->select('id')
			->from($this->collectionsFolder.$collectionId.'/objects.json')
			->order_by('id', JSONDB::ASC)
			->get();
		
		return end($data)['id'];
	}

	public function getObject ($collectionId, $uri) {
		$objects = $result = $this->db->select('*')
			->from($this->collectionsFolder.$collectionId.'/objects.json')
			->where(['uri' => $uri])
			->get();

		if (!isset($objects[0])) return false;

		$result = $objects[0];
		$result[$this->datafield] = file_get_contents($this->dataFolder.$this->collectionsFolder.$collectionId.'/data/'.$objects[0]['uri']);

		return $result;
	}

	public function getObjects ($collectionId) {
		$objects = $this->db->select('*')
			->from($this->collectionsFolder.$collectionId.'/objects.json')
			->get();

		$results = [];
		foreach ($objects as $object) {
			$results[] = get_object_vars($object);
		}

		return $results;
	}

	public function createObject ($collectionId, $uri, $data, $extraData = NULL) {
		$object = $this->newObject($collectionId, $uri, $data, $extraData);
		
		$this->db->insert($this->collectionsFolder.$collectionId.'/objects.json', $object);
		file_put_contents($this->dataFolder.$this->collectionsFolder.$collectionId.'/data/'.$uri, $data);
        $this->addChange($collectionId, $uri, 1);

		return $object['etag'];
	}

	public function updateObject ($collectionId, $uri, $data, $extraData = NULL) {
		$update = $this->newObjectUpdate($collectionId, $uri, $data, $extraData);

		$this->db->update($update)
			->from($this->collectionsFolder.$collectionId.'/objects.json')
			->where(['uri' => $uri])
			->trigger();

		file_put_contents($this->dataFolder.$this->collectionsFolder.$collectionId.'/data/'.$uri, $data);
        $this->addChange($collectionId, $uri, 2);

		return $update['etag'];
	}

	public function deleteObject ($collectionId, $uri) {
		$result = $this->db->delete()
			->from($this->collectionsFolder.$collectionId.'/objects.json')
			->where(['uri' => $uri])
			->trigger();

		unlink($this->dataFolder.$this->collectionsFolder.$collectionId.'/data/'.$uri);
		$this->addChange($collectionId, $uri, 3);

		return $result;
	}

	public function addChange ($collectionId, $uri, $operation) {
		$synctoken = $this->getCollection($collectionId)['synctoken'];

		$this->db->insert($this->collectionsFolder.$collectionId.'/changes.json', [
				'synctoken'     => $synctoken,
				'operation'     => $operation,
				'uri'           => $uri,
			]);

		$this->db->update(['synctoken' => $synctoken+1])
			->from($this->collectionsFile)
			->where(['id' => $collectionId])
			->trigger();
	}

	public function getChanges ($collectionId, $syncToken, $syncLevel) {
		$currentToken = $this->getCollection($collectionId)['synctoken'];

		if (is_null($currentToken)) {
			return null;
		}

        	$result = [
				'syncToken' => $currentToken,
				'added'     => [],
				'modified'  => [],
				'deleted'   => [],
			];
		
        if ($syncToken) {
			$query = $this->db->select('*')
				->from($this->collectionsFolder.$collectionId.'/changes.json')
				->order_by('synctoken', JSONDB::ASC)
				->get();

			$changes = [];

			foreach ($query as $data) {
				if ($data['synctoken'] >= $syncToken) $changes[$data['uri']] = $data['operation'];
				if ($data['synctoken'] < $currentToken) $changes[$data['uri']] = $data['operation'];
			}

            foreach ($changes as $uri => $operation) {
                switch ($operation) {
                    case 1:
                        $result['added'][] = $uri;
                        break;
                    case 2:
                        $result['modified'][] = $uri;
                        break;
                    case 3:
                        $result['deleted'][] = $uri;
                        break;
                }
            }
        } else {
		$objects = $this->getObjects($collectionId);

		foreach ($objects as $object) {
	            $result['added'][] = $object['uri'];
	        }
        }

        return $result;
	}
}