diff options
author | Mario <mario@mariovavti.com> | 2019-11-10 12:49:51 +0000 |
---|---|---|
committer | Mario <mario@mariovavti.com> | 2019-11-10 14:10:03 +0100 |
commit | 580c3f4ffe9608d2beb56d418c68b3b112420e76 (patch) | |
tree | 82335d01179ac361d3f547a4b8e8c598d302e9f3 /vendor/sabre/dav/lib/CardDAV/Plugin.php | |
parent | d22766f458a8539a40a57f3946459a9be1f21cd6 (diff) | |
download | volse-hubzilla-580c3f4ffe9608d2beb56d418c68b3b112420e76.tar.gz volse-hubzilla-580c3f4ffe9608d2beb56d418c68b3b112420e76.tar.bz2 volse-hubzilla-580c3f4ffe9608d2beb56d418c68b3b112420e76.zip |
another bulk of composer updates
(cherry picked from commit 6685381fd8db507493c3d7c1793f8c05c681bbce)
Diffstat (limited to 'vendor/sabre/dav/lib/CardDAV/Plugin.php')
-rw-r--r-- | vendor/sabre/dav/lib/CardDAV/Plugin.php | 480 |
1 files changed, 219 insertions, 261 deletions
diff --git a/vendor/sabre/dav/lib/CardDAV/Plugin.php b/vendor/sabre/dav/lib/CardDAV/Plugin.php index 272ae71fa..10398a4d2 100644 --- a/vendor/sabre/dav/lib/CardDAV/Plugin.php +++ b/vendor/sabre/dav/lib/CardDAV/Plugin.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace Sabre\CardDAV; use Sabre\DAV; @@ -9,10 +11,11 @@ use Sabre\DAVACL; use Sabre\HTTP; use Sabre\HTTP\RequestInterface; use Sabre\HTTP\ResponseInterface; +use Sabre\Uri; use Sabre\VObject; /** - * CardDAV plugin + * CardDAV plugin. * * The CardDAV plugin adds CardDAV functionality to the WebDAV server * @@ -20,15 +23,15 @@ use Sabre\VObject; * @author Evert Pot (http://evertpot.com/) * @license http://sabre.io/license/ Modified BSD License */ -class Plugin extends DAV\ServerPlugin { - +class Plugin extends DAV\ServerPlugin +{ /** - * Url to the addressbooks + * Url to the addressbooks. */ const ADDRESSBOOK_ROOT = 'addressbooks'; /** - * xml namespace for CardDAV elements + * xml namespace for CardDAV elements. */ const NS_CARDDAV = 'urn:ietf:params:xml:ns:carddav'; @@ -41,7 +44,7 @@ class Plugin extends DAV\ServerPlugin { public $directories = []; /** - * Server class + * Server class. * * @var DAV\Server */ @@ -55,41 +58,39 @@ class Plugin extends DAV\ServerPlugin { protected $maxResourceSize = 10000000; /** - * Initializes the plugin + * Initializes the plugin. * * @param DAV\Server $server - * @return void */ - function initialize(DAV\Server $server) { - + public function initialize(DAV\Server $server) + { /* Events */ - $server->on('propFind', [$this, 'propFindEarly']); - $server->on('propFind', [$this, 'propFindLate'], 150); - $server->on('report', [$this, 'report']); - $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']); - $server->on('beforeWriteContent', [$this, 'beforeWriteContent']); - $server->on('beforeCreateFile', [$this, 'beforeCreateFile']); - $server->on('afterMethod:GET', [$this, 'httpAfterGet']); + $server->on('propFind', [$this, 'propFindEarly']); + $server->on('propFind', [$this, 'propFindLate'], 150); + $server->on('report', [$this, 'report']); + $server->on('onHTMLActionsPanel', [$this, 'htmlActionsPanel']); + $server->on('beforeWriteContent', [$this, 'beforeWriteContent']); + $server->on('beforeCreateFile', [$this, 'beforeCreateFile']); + $server->on('afterMethod:GET', [$this, 'httpAfterGet']); $server->xml->namespaceMap[self::NS_CARDDAV] = 'card'; - $server->xml->elementMap['{' . self::NS_CARDDAV . '}addressbook-query'] = 'Sabre\\CardDAV\\Xml\\Request\\AddressBookQueryReport'; - $server->xml->elementMap['{' . self::NS_CARDDAV . '}addressbook-multiget'] = 'Sabre\\CardDAV\\Xml\\Request\\AddressBookMultiGetReport'; + $server->xml->elementMap['{'.self::NS_CARDDAV.'}addressbook-query'] = 'Sabre\\CardDAV\\Xml\\Request\\AddressBookQueryReport'; + $server->xml->elementMap['{'.self::NS_CARDDAV.'}addressbook-multiget'] = 'Sabre\\CardDAV\\Xml\\Request\\AddressBookMultiGetReport'; /* Mapping Interfaces to {DAV:}resourcetype values */ - $server->resourceTypeMapping['Sabre\\CardDAV\\IAddressBook'] = '{' . self::NS_CARDDAV . '}addressbook'; - $server->resourceTypeMapping['Sabre\\CardDAV\\IDirectory'] = '{' . self::NS_CARDDAV . '}directory'; + $server->resourceTypeMapping['Sabre\\CardDAV\\IAddressBook'] = '{'.self::NS_CARDDAV.'}addressbook'; + $server->resourceTypeMapping['Sabre\\CardDAV\\IDirectory'] = '{'.self::NS_CARDDAV.'}directory'; /* Adding properties that may never be changed */ - $server->protectedProperties[] = '{' . self::NS_CARDDAV . '}supported-address-data'; - $server->protectedProperties[] = '{' . self::NS_CARDDAV . '}max-resource-size'; - $server->protectedProperties[] = '{' . self::NS_CARDDAV . '}addressbook-home-set'; - $server->protectedProperties[] = '{' . self::NS_CARDDAV . '}supported-collation-set'; + $server->protectedProperties[] = '{'.self::NS_CARDDAV.'}supported-address-data'; + $server->protectedProperties[] = '{'.self::NS_CARDDAV.'}max-resource-size'; + $server->protectedProperties[] = '{'.self::NS_CARDDAV.'}addressbook-home-set'; + $server->protectedProperties[] = '{'.self::NS_CARDDAV.'}supported-collation-set'; $server->xml->elementMap['{http://calendarserver.org/ns/}me-card'] = 'Sabre\\DAV\\Xml\\Property\\Href'; $this->server = $server; - } /** @@ -99,10 +100,9 @@ class Plugin extends DAV\ServerPlugin { * * @return array */ - function getFeatures() { - + public function getFeatures() + { return ['addressbook']; - } /** @@ -113,117 +113,111 @@ class Plugin extends DAV\ServerPlugin { * implement them * * @param string $uri + * * @return array */ - function getSupportedReportSet($uri) { - + public function getSupportedReportSet($uri) + { $node = $this->server->tree->getNodeForPath($uri); if ($node instanceof IAddressBook || $node instanceof ICard) { return [ - '{' . self::NS_CARDDAV . '}addressbook-multiget', - '{' . self::NS_CARDDAV . '}addressbook-query', + '{'.self::NS_CARDDAV.'}addressbook-multiget', + '{'.self::NS_CARDDAV.'}addressbook-query', ]; } - return []; + return []; } - /** - * Adds all CardDAV-specific properties + * Adds all CardDAV-specific properties. * * @param DAV\PropFind $propFind - * @param DAV\INode $node - * @return void + * @param DAV\INode $node */ - function propFindEarly(DAV\PropFind $propFind, DAV\INode $node) { - - $ns = '{' . self::NS_CARDDAV . '}'; + public function propFindEarly(DAV\PropFind $propFind, DAV\INode $node) + { + $ns = '{'.self::NS_CARDDAV.'}'; if ($node instanceof IAddressBook) { - - $propFind->handle($ns . 'max-resource-size', $this->maxResourceSize); - $propFind->handle($ns . 'supported-address-data', function() { + $propFind->handle($ns.'max-resource-size', $this->maxResourceSize); + $propFind->handle($ns.'supported-address-data', function () { return new Xml\Property\SupportedAddressData(); }); - $propFind->handle($ns . 'supported-collation-set', function() { + $propFind->handle($ns.'supported-collation-set', function () { return new Xml\Property\SupportedCollationSet(); }); - } if ($node instanceof DAVACL\IPrincipal) { - $path = $propFind->getPath(); - $propFind->handle('{' . self::NS_CARDDAV . '}addressbook-home-set', function() use ($path) { - return new LocalHref($this->getAddressBookHomeForPrincipal($path) . '/'); - }); - - if ($this->directories) $propFind->handle('{' . self::NS_CARDDAV . '}directory-gateway', function() { - return new LocalHref($this->directories); + $propFind->handle('{'.self::NS_CARDDAV.'}addressbook-home-set', function () use ($path) { + return new LocalHref($this->getAddressBookHomeForPrincipal($path).'/'); }); + if ($this->directories) { + $propFind->handle('{'.self::NS_CARDDAV.'}directory-gateway', function () { + return new LocalHref($this->directories); + }); + } } if ($node instanceof ICard) { - // The address-data property is not supposed to be a 'real' // property, but in large chunks of the spec it does act as such. // Therefore we simply expose it as a property. - $propFind->handle('{' . self::NS_CARDDAV . '}address-data', function() use ($node) { + $propFind->handle('{'.self::NS_CARDDAV.'}address-data', function () use ($node) { $val = $node->get(); - if (is_resource($val)) + if (is_resource($val)) { $val = stream_get_contents($val); + } return $val; - }); - } - } /** - * This functions handles REPORT requests specific to CardDAV + * This functions handles REPORT requests specific to CardDAV. * - * @param string $reportName + * @param string $reportName * @param \DOMNode $dom - * @param mixed $path + * @param mixed $path + * * @return bool */ - function report($reportName, $dom, $path) { - + public function report($reportName, $dom, $path) + { switch ($reportName) { - case '{' . self::NS_CARDDAV . '}addressbook-multiget' : + case '{'.self::NS_CARDDAV.'}addressbook-multiget': $this->server->transactionType = 'report-addressbook-multiget'; $this->addressbookMultiGetReport($dom); + return false; - case '{' . self::NS_CARDDAV . '}addressbook-query' : + case '{'.self::NS_CARDDAV.'}addressbook-query': $this->server->transactionType = 'report-addressbook-query'; $this->addressBookQueryReport($dom); + return false; - default : + default: return; - } - - } /** - * Returns the addressbook home for a given principal + * Returns the addressbook home for a given principal. * * @param string $principal + * * @return string */ - protected function getAddressbookHomeForPrincipal($principal) { - - list(, $principalId) = \Sabre\HTTP\URLUtil::splitPath($principal); - return self::ADDRESSBOOK_ROOT . '/' . $principalId; + protected function getAddressbookHomeForPrincipal($principal) + { + list(, $principalId) = Uri\split($principal); + return self::ADDRESSBOOK_ROOT.'/'.$principalId; } - /** * This function handles the addressbook-multiget REPORT. * @@ -231,14 +225,13 @@ class Plugin extends DAV\ServerPlugin { * of urls. Effectively avoiding a lot of redundant requests. * * @param Xml\Request\AddressBookMultiGetReport $report - * @return void */ - function addressbookMultiGetReport($report) { - + public function addressbookMultiGetReport($report) + { $contentType = $report->contentType; $version = $report->version; if ($version) { - $contentType .= '; version=' . $version; + $contentType .= '; version='.$version; } $vcardType = $this->negotiateVCard( @@ -251,17 +244,13 @@ class Plugin extends DAV\ServerPlugin { $report->hrefs ); foreach ($this->server->getPropertiesForMultiplePaths($paths, $report->properties) as $props) { - - if (isset($props['200']['{' . self::NS_CARDDAV . '}address-data'])) { - - $props['200']['{' . self::NS_CARDDAV . '}address-data'] = $this->convertVCard( - $props[200]['{' . self::NS_CARDDAV . '}address-data'], + if (isset($props['200']['{'.self::NS_CARDDAV.'}address-data'])) { + $props['200']['{'.self::NS_CARDDAV.'}address-data'] = $this->convertVCard( + $props[200]['{'.self::NS_CARDDAV.'}address-data'], $vcardType ); - } $propertyList[] = $props; - } $prefer = $this->server->getHTTPPrefer(); @@ -269,8 +258,7 @@ class Plugin extends DAV\ServerPlugin { $this->server->httpResponse->setStatus(207); $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer'); - $this->server->httpResponse->setBody($this->server->generateMultiStatus($propertyList, $prefer['return'] === 'minimal')); - + $this->server->httpResponse->setBody($this->server->generateMultiStatus($propertyList, 'minimal' === $prefer['return'])); } /** @@ -279,20 +267,19 @@ class Plugin extends DAV\ServerPlugin { * This plugin uses this method to ensure that Card nodes receive valid * vcard data. * - * @param string $path + * @param string $path * @param DAV\IFile $node - * @param resource $data - * @param bool $modified Should be set to true, if this event handler - * changed &$data. - * @return void + * @param resource $data + * @param bool $modified should be set to true, if this event handler + * changed &$data */ - function beforeWriteContent($path, DAV\IFile $node, &$data, &$modified) { - - if (!$node instanceof ICard) + public function beforeWriteContent($path, DAV\IFile $node, &$data, &$modified) + { + if (!$node instanceof ICard) { return; + } $this->validateVCard($data, $modified); - } /** @@ -301,20 +288,19 @@ class Plugin extends DAV\ServerPlugin { * This plugin uses this method to ensure that Card nodes receive valid * vcard data. * - * @param string $path - * @param resource $data + * @param string $path + * @param resource $data * @param DAV\ICollection $parentNode - * @param bool $modified Should be set to true, if this event handler - * changed &$data. - * @return void + * @param bool $modified should be set to true, if this event handler + * changed &$data */ - function beforeCreateFile($path, &$data, DAV\ICollection $parentNode, &$modified) { - - if (!$parentNode instanceof IAddressBook) + public function beforeCreateFile($path, &$data, DAV\ICollection $parentNode, &$modified) + { + if (!$parentNode instanceof IAddressBook) { return; + } $this->validateVCard($data, $modified); - } /** @@ -323,12 +309,11 @@ class Plugin extends DAV\ServerPlugin { * An exception is thrown if it's not. * * @param resource|string $data - * @param bool $modified Should be set to true, if this event handler - * changed &$data. - * @return void + * @param bool $modified should be set to true, if this event handler + * changed &$data */ - protected function validateVCard(&$data, &$modified) { - + protected function validateVCard(&$data, &$modified) + { // If it's a stream, we convert it to a string first. if (is_resource($data)) { $data = stream_get_contents($data); @@ -337,10 +322,9 @@ class Plugin extends DAV\ServerPlugin { $before = $data; try { - // If the data starts with a [, we can reasonably assume we're dealing // with a jCal object. - if (substr($data, 0, 1) === '[') { + if ('[' === substr($data, 0, 1)) { $vobj = VObject\Reader::readJson($data); // Converting $data back to iCalendar, as that's what we @@ -350,21 +334,18 @@ class Plugin extends DAV\ServerPlugin { } else { $vobj = VObject\Reader::read($data); } - } catch (VObject\ParseException $e) { - - throw new DAV\Exception\UnsupportedMediaType('This resource only supports valid vCard or jCard data. Parse error: ' . $e->getMessage()); - + throw new DAV\Exception\UnsupportedMediaType('This resource only supports valid vCard or jCard data. Parse error: '.$e->getMessage()); } - if ($vobj->name !== 'VCARD') { + if ('VCARD' !== $vobj->name) { throw new DAV\Exception\UnsupportedMediaType('This collection can only support vcard objects.'); } $options = VObject\Node::PROFILE_CARDDAV; $prefer = $this->server->getHTTPPrefer(); - if ($prefer['handling'] !== 'strict') { + if ('strict' !== $prefer['handling']) { $options |= VObject\Node::REPAIR; } @@ -376,7 +357,6 @@ class Plugin extends DAV\ServerPlugin { // $messages contains a list of problems with the vcard, along with // their severity. foreach ($messages as $message) { - if ($message['level'] > $highestLevel) { // Recording the highest reported error level. $highestLevel = $message['level']; @@ -384,30 +364,27 @@ class Plugin extends DAV\ServerPlugin { } switch ($message['level']) { - - case 1 : + case 1: // Level 1 means that there was a problem, but it was repaired. $modified = true; break; - case 2 : + case 2: // Level 2 means a warning, but not critical break; - case 3 : + case 3: // Level 3 means a critical error - throw new DAV\Exception\UnsupportedMediaType('Validation error in vCard: ' . $message['message']); - + throw new DAV\Exception\UnsupportedMediaType('Validation error in vCard: '.$message['message']); } - } if ($warningMessage) { $this->server->httpResponse->setHeader( 'X-Sabre-Ew-Gross', - 'vCard validation warning: ' . $warningMessage + 'vCard validation warning: '.$warningMessage ); // Re-serializing object. $data = $vobj->serialize(); - if (!$modified && strcmp($data, $before) !== 0) { + if (!$modified && 0 !== strcmp($data, $before)) { // This ensures that the system does not send an ETag back. $modified = true; } @@ -417,23 +394,21 @@ class Plugin extends DAV\ServerPlugin { $vobj->destroy(); } - /** - * This function handles the addressbook-query REPORT + * This function handles the addressbook-query REPORT. * * This report is used by the client to filter an addressbook based on a * complex query. * * @param Xml\Request\AddressBookQueryReport $report - * @return void */ - protected function addressbookQueryReport($report) { - + protected function addressbookQueryReport($report) + { $depth = $this->server->getHTTPDepth(0); - if ($depth == 0) { + if (0 == $depth) { $candidateNodes = [ - $this->server->tree->getNodeForPath($this->server->getRequestUri()) + $this->server->tree->getNodeForPath($this->server->getRequestUri()), ]; if (!$candidateNodes[0] instanceof ICard) { throw new ReportNotSupported('The addressbook-query report is not supported on this url with Depth: 0'); @@ -444,7 +419,7 @@ class Plugin extends DAV\ServerPlugin { $contentType = $report->contentType; if ($report->version) { - $contentType .= '; version=' . $report->version; + $contentType .= '; version='.$report->version; } $vcardType = $this->negotiateVCard( @@ -453,9 +428,9 @@ class Plugin extends DAV\ServerPlugin { $validNodes = []; foreach ($candidateNodes as $node) { - - if (!$node instanceof ICard) + if (!$node instanceof ICard) { continue; + } $blob = $node->get(); if (is_resource($blob)) { @@ -472,31 +447,26 @@ class Plugin extends DAV\ServerPlugin { // We hit the maximum number of items, we can stop now. break; } - } $result = []; foreach ($validNodes as $validNode) { - - if ($depth == 0) { + if (0 == $depth) { $href = $this->server->getRequestUri(); } else { - $href = $this->server->getRequestUri() . '/' . $validNode->getName(); + $href = $this->server->getRequestUri().'/'.$validNode->getName(); } list($props) = $this->server->getPropertiesForPath($href, $report->properties, 0); - if (isset($props[200]['{' . self::NS_CARDDAV . '}address-data'])) { - - $props[200]['{' . self::NS_CARDDAV . '}address-data'] = $this->convertVCard( - $props[200]['{' . self::NS_CARDDAV . '}address-data'], + if (isset($props[200]['{'.self::NS_CARDDAV.'}address-data'])) { + $props[200]['{'.self::NS_CARDDAV.'}address-data'] = $this->convertVCard( + $props[200]['{'.self::NS_CARDDAV.'}address-data'], $vcardType, $report->addressDataProperties ); - } $result[] = $props; - } $prefer = $this->server->getHTTPPrefer(); @@ -504,26 +474,26 @@ class Plugin extends DAV\ServerPlugin { $this->server->httpResponse->setStatus(207); $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8'); $this->server->httpResponse->setHeader('Vary', 'Brief,Prefer'); - $this->server->httpResponse->setBody($this->server->generateMultiStatus($result, $prefer['return'] === 'minimal')); - + $this->server->httpResponse->setBody($this->server->generateMultiStatus($result, 'minimal' === $prefer['return'])); } /** * Validates if a vcard makes it throught a list of filters. * * @param string $vcardData - * @param array $filters - * @param string $test anyof or allof (which means OR or AND) + * @param array $filters + * @param string $test anyof or allof (which means OR or AND) + * * @return bool */ - function validateFilters($vcardData, array $filters, $test) { - - - if (!$filters) return true; + public function validateFilters($vcardData, array $filters, $test) + { + if (!$filters) { + return true; + } $vcard = VObject\Reader::read($vcardData); foreach ($filters as $filter) { - $isDefined = isset($vcard->{$filter['name']}); if ($filter['is-not-defined']) { if ($isDefined) { @@ -532,12 +502,9 @@ class Plugin extends DAV\ServerPlugin { $success = true; } } elseif ((!$filter['param-filters'] && !$filter['text-matches']) || !$isDefined) { - // We only need to check for existence $success = $isDefined; - } else { - $vProperties = $vcard->select($filter['name']); $results = []; @@ -546,44 +513,40 @@ class Plugin extends DAV\ServerPlugin { } if ($filter['text-matches']) { $texts = []; - foreach ($vProperties as $vProperty) + foreach ($vProperties as $vProperty) { $texts[] = $vProperty->getValue(); + } $results[] = $this->validateTextMatches($texts, $filter['text-matches'], $filter['test']); } - if (count($results) === 1) { + if (1 === count($results)) { $success = $results[0]; } else { - if ($filter['test'] === 'anyof') { + if ('anyof' === $filter['test']) { $success = $results[0] || $results[1]; } else { $success = $results[0] && $results[1]; } } - } // else // There are two conditions where we can already determine whether // or not this filter succeeds. - if ($test === 'anyof' && $success) { - + if ('anyof' === $test && $success) { // Destroy circular references to PHP will GC the object. $vcard->destroy(); return true; } - if ($test === 'allof' && !$success) { - + if ('allof' === $test && !$success) { // Destroy circular references to PHP will GC the object. $vcard->destroy(); return false; } - } // foreach - // Destroy circular references to PHP will GC the object. $vcard->destroy(); @@ -592,8 +555,7 @@ class Plugin extends DAV\ServerPlugin { // // This implies for 'anyof' that the test failed, and for 'allof' that // we succeeded. Sounds weird, but makes sense. - return $test === 'allof'; - + return 'allof' === $test; } /** @@ -602,19 +564,22 @@ class Plugin extends DAV\ServerPlugin { * @todo currently we're only validating the first parameter of the passed * property. Any subsequence parameters with the same name are * ignored. - * @param array $vProperties - * @param array $filters + * + * @param array $vProperties + * @param array $filters * @param string $test + * * @return bool */ - protected function validateParamFilters(array $vProperties, array $filters, $test) { - + protected function validateParamFilters(array $vProperties, array $filters, $test) + { foreach ($filters as $filter) { - $isDefined = false; foreach ($vProperties as $vProperty) { $isDefined = isset($vProperty[$filter['name']]); - if ($isDefined) break; + if ($isDefined) { + break; + } } if ($filter['is-not-defined']) { @@ -624,35 +589,32 @@ class Plugin extends DAV\ServerPlugin { $success = true; } - // If there's no text-match, we can just check for existence + // If there's no text-match, we can just check for existence } elseif (!$filter['text-match'] || !$isDefined) { - $success = $isDefined; - } else { - $success = false; foreach ($vProperties as $vProperty) { // If we got all the way here, we'll need to validate the // text-match filter. $success = DAV\StringUtil::textMatch($vProperty[$filter['name']]->getValue(), $filter['text-match']['value'], $filter['text-match']['collation'], $filter['text-match']['match-type']); - if ($success) break; + if ($success) { + break; + } } if ($filter['text-match']['negate-condition']) { $success = !$success; } - } // else // There are two conditions where we can already determine whether // or not this filter succeeds. - if ($test === 'anyof' && $success) { + if ('anyof' === $test && $success) { return true; } - if ($test === 'allof' && !$success) { + if ('allof' === $test && !$success) { return false; } - } // If we got all the way here, it means we haven't been able to @@ -660,40 +622,41 @@ class Plugin extends DAV\ServerPlugin { // // This implies for 'anyof' that the test failed, and for 'allof' that // we succeeded. Sounds weird, but makes sense. - return $test === 'allof'; - + return 'allof' === $test; } /** * Validates if a text-filter can be applied to a specific property. * - * @param array $texts - * @param array $filters + * @param array $texts + * @param array $filters * @param string $test + * * @return bool */ - protected function validateTextMatches(array $texts, array $filters, $test) { - + protected function validateTextMatches(array $texts, array $filters, $test) + { foreach ($filters as $filter) { - $success = false; foreach ($texts as $haystack) { $success = DAV\StringUtil::textMatch($haystack, $filter['value'], $filter['collation'], $filter['match-type']); // Breaking on the first match - if ($success) break; + if ($success) { + break; + } } if ($filter['negate-condition']) { $success = !$success; } - if ($success && $test === 'anyof') + if ($success && 'anyof' === $test) { return true; + } - if (!$success && $test == 'allof') + if (!$success && 'allof' == $test) { return false; - - + } } // If we got all the way here, it means we haven't been able to @@ -701,8 +664,7 @@ class Plugin extends DAV\ServerPlugin { // // This implies for 'anyof' that the test failed, and for 'allof' that // we succeeded. Sounds weird, but makes sense. - return $test === 'allof'; - + return 'allof' === $test; } /** @@ -712,24 +674,24 @@ class Plugin extends DAV\ServerPlugin { * propfind has been done. * * @param DAV\PropFind $propFind - * @param DAV\INode $node - * @return void + * @param DAV\INode $node */ - function propFindLate(DAV\PropFind $propFind, DAV\INode $node) { - + public function propFindLate(DAV\PropFind $propFind, DAV\INode $node) + { // If the request was made using the SOGO connector, we must rewrite // the content-type property. By default SabreDAV will send back // text/x-vcard; charset=utf-8, but for SOGO we must strip that last // part. - if (strpos($this->server->httpRequest->getHeader('User-Agent'), 'Thunderbird') === false) { + if (false === strpos((string) $this->server->httpRequest->getHeader('User-Agent'), 'Thunderbird')) { return; } $contentType = $propFind->get('{DAV:}getcontenttype'); - list($part) = explode(';', $contentType); - if ($part === 'text/x-vcard' || $part === 'text/vcard') { - $propFind->set('{DAV:}getcontenttype', 'text/x-vcard'); + if (null !== $contentType) { + list($part) = explode(';', $contentType); + if ('text/x-vcard' === $part || 'text/vcard' === $part) { + $propFind->set('{DAV:}getcontenttype', 'text/x-vcard'); + } } - } /** @@ -738,18 +700,20 @@ class Plugin extends DAV\ServerPlugin { * can use to create new addressbooks. * * @param DAV\INode $node - * @param string $output + * @param string $output + * * @return bool */ - function htmlActionsPanel(DAV\INode $node, &$output) { - - if (!$node instanceof AddressBookHome) + public function htmlActionsPanel(DAV\INode $node, &$output) + { + if (!$node instanceof AddressBookHome) { return; + } $output .= '<tr><td colspan="2"><form method="post" action=""> <h3>Create new address book</h3> <input type="hidden" name="sabreAction" value="mkcol" /> - <input type="hidden" name="resourceType" value="{DAV:}collection,{' . self::NS_CARDDAV . '}addressbook" /> + <input type="hidden" name="resourceType" value="{DAV:}collection,{'.self::NS_CARDDAV.'}addressbook" /> <label>Name (uri):</label> <input type="text" name="name" /><br /> <label>Display name:</label> <input type="text" name="{DAV:}displayname" /><br /> <input type="submit" value="create" /> @@ -757,7 +721,6 @@ class Plugin extends DAV\ServerPlugin { </td></tr>'; return false; - } /** @@ -765,13 +728,13 @@ class Plugin extends DAV\ServerPlugin { * * This is used to transform data into jCal, if this was requested. * - * @param RequestInterface $request + * @param RequestInterface $request * @param ResponseInterface $response - * @return void */ - function httpAfterGet(RequestInterface $request, ResponseInterface $response) { - - if (strpos($response->getHeader('Content-Type'), 'text/vcard') === false) { + public function httpAfterGet(RequestInterface $request, ResponseInterface $response) + { + $contentType = $response->getHeader('Content-Type'); + if (null === $contentType || false === strpos($contentType, 'text/vcard')) { return; } @@ -783,9 +746,8 @@ class Plugin extends DAV\ServerPlugin { ); $response->setBody($newBody); - $response->setHeader('Content-Type', $mimeType . '; charset=utf-8'); + $response->setHeader('Content-Type', $mimeType.'; charset=utf-8'); $response->setHeader('Content-Length', strlen($newBody)); - } /** @@ -800,11 +762,12 @@ class Plugin extends DAV\ServerPlugin { * * @param string $input * @param string $mimeType + * * @return string */ - protected function negotiateVCard($input, &$mimeType = null) { - - $result = HTTP\Util::negotiate( + protected function negotiateVCard($input, &$mimeType = null) + { + $result = HTTP\negotiateContentType( $input, [ // Most often used mime-type. Version 3 @@ -823,41 +786,41 @@ class Plugin extends DAV\ServerPlugin { $mimeType = $result; switch ($result) { - - default : - case 'text/x-vcard' : - case 'text/vcard' : - case 'text/vcard; version=3.0' : + default: + case 'text/x-vcard': + case 'text/vcard': + case 'text/vcard; version=3.0': $mimeType = 'text/vcard'; + return 'vcard3'; - case 'text/vcard; version=4.0' : + case 'text/vcard; version=4.0': return 'vcard4'; - case 'application/vcard+json' : + case 'application/vcard+json': return 'jcard'; // @codeCoverageIgnoreStart } // @codeCoverageIgnoreEnd - } /** * Converts a vcard blob to a different version, or jcard. * * @param string|resource $data - * @param string $target - * @param array $propertiesFilter + * @param string $target + * @param array $propertiesFilter + * * @return string */ - protected function convertVCard($data, $target, array $propertiesFilter = null) { - + protected function convertVCard($data, $target, array $propertiesFilter = null) + { if (is_resource($data)) { $data = stream_get_contents($data); } $input = VObject\Reader::read($data); if (!empty($propertiesFilter)) { $propertiesFilter = array_merge(['UID', 'VERSION', 'FN'], $propertiesFilter); - $keys = array_unique(array_map(function($child) { + $keys = array_unique(array_map(function ($child) { return $child->name; }, $input->children())); $keys = array_diff($keys, $propertiesFilter); @@ -868,38 +831,36 @@ class Plugin extends DAV\ServerPlugin { } $output = null; try { - switch ($target) { - default : - case 'vcard3' : - if ($input->getDocumentType() === VObject\Document::VCARD30) { + default: + case 'vcard3': + if (VObject\Document::VCARD30 === $input->getDocumentType()) { // Do nothing return $data; } $output = $input->convert(VObject\Document::VCARD30); + return $output->serialize(); - case 'vcard4' : - if ($input->getDocumentType() === VObject\Document::VCARD40) { + case 'vcard4': + if (VObject\Document::VCARD40 === $input->getDocumentType()) { // Do nothing return $data; } $output = $input->convert(VObject\Document::VCARD40); + return $output->serialize(); - case 'jcard' : + case 'jcard': $output = $input->convert(VObject\Document::VCARD40); - return json_encode($output); + return json_encode($output); } - } finally { - // Destroy circular references to PHP will GC the object. $input->destroy(); if (!is_null($output)) { $output->destroy(); } } - } /** @@ -910,10 +871,9 @@ class Plugin extends DAV\ServerPlugin { * * @return string */ - function getPluginName() { - + public function getPluginName() + { return 'carddav'; - } /** @@ -927,14 +887,12 @@ class Plugin extends DAV\ServerPlugin { * * @return array */ - function getPluginInfo() { - + public function getPluginInfo() + { return [ - 'name' => $this->getPluginName(), + 'name' => $this->getPluginName(), 'description' => 'Adds support for CardDAV (rfc6352)', - 'link' => 'http://sabre.io/dav/carddav/', + 'link' => 'http://sabre.io/dav/carddav/', ]; - } - } |