|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ownCloud - CardDAV plugin |
|
4
|
|
|
* |
|
5
|
|
|
* The CardDAV plugin adds CardDAV functionality to the WebDAV server |
|
6
|
|
|
* |
|
7
|
|
|
* @author Thomas Tanghus |
|
8
|
|
|
* @copyright 2013-2014 Thomas Tanghus ([email protected]) |
|
9
|
|
|
* |
|
10
|
|
|
* This library is free software; you can redistribute it and/or |
|
11
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
|
12
|
|
|
* License as published by the Free Software Foundation; either |
|
13
|
|
|
* version 3 of the License, or any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This library is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public |
|
21
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace OCA\Contacts\CardDAV; |
|
26
|
|
|
|
|
27
|
|
|
use Sabre\VObject; |
|
28
|
|
|
use OCA\Contacts\VObject\VCard; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* This class overrides \Sabre\CardDAV\Plugin::validateVCard() to be able |
|
32
|
|
|
* to import partially invalid vCards by ignoring invalid lines and to |
|
33
|
|
|
* validate and upgrade using \OCA\Contacts\VCard. |
|
34
|
|
|
*/ |
|
35
|
|
|
class Plugin extends \Sabre\CardDAV\Plugin { |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Checks if the submitted vCard data is in fact, valid. |
|
39
|
|
|
* |
|
40
|
|
|
* An exception is thrown if it's not. |
|
41
|
|
|
* |
|
42
|
|
|
* @param resource|string $data |
|
43
|
|
|
* @param boolean $modified whether the data was modified |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function validateVCard(&$data, &$modified) { |
|
47
|
|
|
|
|
48
|
|
|
// If it's a stream, we convert it to a string first. |
|
49
|
|
|
if (is_resource($data)) { |
|
50
|
|
|
$data = stream_get_contents($data); |
|
51
|
|
|
} elseif (!is_string($data)) { |
|
52
|
|
|
throw new \Exception(__METHOD__ . ' argument 1 only supports string or stream resource.'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
try { |
|
56
|
|
|
$vobj = VObject\Reader::read($data, VObject\Reader::OPTION_IGNORE_INVALID_LINES); |
|
57
|
|
|
} catch (VObject\ParseException $e) { |
|
58
|
|
|
throw new \Sabre\DAV\Exception\UnsupportedMediaType('This resource only supports valid vcard data. Parse error: ' . $e->getMessage()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($vobj->name !== 'VCARD') { |
|
62
|
|
|
throw new \Sabre\DAV\Exception\UnsupportedMediaType('This collection can only support vcard objects.'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$modified = true; // FIXME: set to false if neither repair nor upgrade was done |
|
66
|
|
|
$vobj->validate(VCard::REPAIR|VCard::UPGRADE); |
|
67
|
|
|
$data = $vobj->serialize(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|