Blame view
sources/apps/contacts/lib/sabre/validatorplugin.php
2.7 KB
|
03e52840d
|
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 |
<?php
use Sabre\VObject;
/**
* vCard validator
*
* Validates and tries to fix broken vCards before they're being
* handed over to Sabre and written to storage.
*
* @copyright Copyright (C) 2013 Thomas Tanghus
* @author Thomas Tanghus (http://tanghus.net/)
*/
class OC_Connector_Sabre_CardDAV_ValidatorPlugin extends Sabre_DAV_ServerPlugin {
/**
* Reference to Server class
*
* @var Sabre_DAV_Server
*/
protected $server;
/**
* Initializes the plugin and registers event handlers
*
* @param Sabre_DAV_Server $server
* @return void
*/
public function initialize(Sabre_DAV_Server $server) {
$this->server = $server;
$server->subscribeEvent('beforeWriteContent', array($this, 'beforeWriteContent'), 90);
$server->subscribeEvent('beforeCreateFile', array($this, 'beforeCreateFile'), 90);
}
/**
* This method is triggered before a file gets updated with new content.
*
* This plugin uses this method to ensure that Card nodes receive valid
* vcard data.
*
* @param string $path
* @param Sabre_DAV_IFile $node
* @param resource $data
* @return void
*/
public function beforeWriteContent($path, Sabre_DAV_IFile $node, &$data) {
if (!$node instanceof Sabre_CardDAV_ICard) {
return;
}
$this->validateVCard($data);
}
/**
* This method is triggered before a new file is created.
*
* This plugin uses this method to ensure that Card nodes receive valid
* vcard data.
*
* @param string $path
* @param resource $data
* @param Sabre_DAV_ICollection $parentNode
* @return void
*/
public function beforeCreateFile($path, &$data, Sabre_DAV_ICollection $parentNode) {
if (!$parentNode instanceof Sabre_CardDAV_IAddressBook) {
return;
}
$this->validateVCard($data);
}
/**
* Checks if the submitted vCard data is in fact, valid.
*
* An exception is thrown if it's not.
*
* @param resource|string $data
* @return void
*/
protected function validateVCard(&$data) {
// If it's a stream, we convert it to a string first.
if (is_resource($data)) {
$data = stream_get_contents($data);
}
// Converting the data to unicode, if needed.
$data = Sabre_DAV_StringUtil::ensureUTF8($data);
try {
$vobj = VObject\Reader::read($data);
} catch (VObject\ParseException $e) {
throw new Sabre_DAV_Exception_UnsupportedMediaType(
'This resource only supports valid vcard data. Parse error: ' . $e->getMessage()
);
}
if ($vobj->name !== 'VCARD') {
throw new Sabre_DAV_Exception_UnsupportedMediaType(
'This collection can only support vcard objects.'
);
}
if (!isset($vobj->UID)) {
$uid = substr(md5(rand().time()), 0, 10);
\OCP\Util::writeLog('contacts', __METHOD__.', Adding UID: ' . $uid, \OCP\Util::DEBUG);
$vobj->add('UID', $uid);
}
}
}
|