[Pkg-owncloud-commits] [php-sabredav] 87/220: Finaly some sanity in the href stuff.
David Prévot
taffit at moszumanska.debian.org
Thu May 12 01:21:11 UTC 2016
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository php-sabredav.
commit 7a9a5ec0298df4afb51233635a9fb0bbaec113bf
Author: Evert Pot <me at evertpot.com>
Date: Tue Mar 22 23:54:32 2016 -0400
Finaly some sanity in the href stuff.
---
lib/CalDAV/Plugin.php | 9 ++--
lib/CalDAV/Schedule/Plugin.php | 7 +--
lib/CalDAV/SharingPlugin.php | 5 +-
lib/CalDAV/Subscriptions/Subscription.php | 2 +-
lib/CardDAV/Plugin.php | 5 +-
lib/DAV/Xml/Property/Href.php | 17 ++-----
lib/DAV/Xml/Property/LocalHref.php | 48 ++++++++++++++++++
tests/Sabre/DAV/Xml/Property/HrefTest.php | 29 -----------
tests/Sabre/DAV/Xml/Property/LocalHrefTest.php | 69 ++++++++++++++++++++++++++
9 files changed, 136 insertions(+), 55 deletions(-)
diff --git a/lib/CalDAV/Plugin.php b/lib/CalDAV/Plugin.php
index 7979856..0a240f7 100644
--- a/lib/CalDAV/Plugin.php
+++ b/lib/CalDAV/Plugin.php
@@ -7,6 +7,7 @@ use Sabre\DAV;
use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\MkCol;
use Sabre\DAV\Xml\Property\Href;
+use Sabre\DAV\Xml\Property\LocalHref;
use Sabre\DAVACL;
use Sabre\VObject;
use Sabre\HTTP;
@@ -342,7 +343,7 @@ class Plugin extends DAV\ServerPlugin {
$calendarHomePath = $this->getCalendarHomeForPrincipal($principalUrl);
if (is_null($calendarHomePath)) return null;
- return new Href($calendarHomePath . '/');
+ return new LocalHref($calendarHomePath . '/');
});
// The calendar-user-address-set property is basically mapped to
@@ -350,7 +351,7 @@ class Plugin extends DAV\ServerPlugin {
$propFind->handle('{' . self::NS_CALDAV . '}calendar-user-address-set', function() use ($node) {
$addresses = $node->getAlternateUriSet();
$addresses[] = $this->server->getBaseUri() . $node->getPrincipalUrl() . '/';
- return new Href($addresses, false);
+ return new LocalHref($addresses);
});
// For some reason somebody thought it was a good idea to add
// another one of these properties. We're supporting it too.
@@ -395,8 +396,8 @@ class Plugin extends DAV\ServerPlugin {
}
- $propFind->set($propRead, new Href($readList));
- $propFind->set($propWrite, new Href($writeList));
+ $propFind->set($propRead, new LocalHref($readList));
+ $propFind->set($propWrite, new LocalHref($writeList));
}
diff --git a/lib/CalDAV/Schedule/Plugin.php b/lib/CalDAV/Schedule/Plugin.php
index 27f2a36..9066275 100644
--- a/lib/CalDAV/Schedule/Plugin.php
+++ b/lib/CalDAV/Schedule/Plugin.php
@@ -9,6 +9,7 @@ use Sabre\DAV\PropFind;
use Sabre\DAV\PropPatch;
use Sabre\DAV\INode;
use Sabre\DAV\Xml\Property\Href;
+use Sabre\DAV\Xml\Property\LocalHref;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use Sabre\VObject;
@@ -215,7 +216,7 @@ class Plugin extends ServerPlugin {
}
$outboxPath = $calendarHomePath . '/outbox/';
- return new Href($outboxPath);
+ return new LocalHref($outboxPath);
});
// schedule-inbox-URL property
@@ -227,7 +228,7 @@ class Plugin extends ServerPlugin {
}
$inboxPath = $calendarHomePath . '/inbox/';
- return new Href($inboxPath);
+ return new LocalHref($inboxPath);
});
@@ -256,7 +257,7 @@ class Plugin extends ServerPlugin {
if (!isset($child[200][$sccs]) || in_array('VEVENT', $child[200][$sccs]->getValue())) {
// Either there is no supported-calendar-component-set
// (which is fine) or we found one that supports VEVENT.
- return new Href($child['href']);
+ return new LocalHref($child['href']);
}
}
diff --git a/lib/CalDAV/SharingPlugin.php b/lib/CalDAV/SharingPlugin.php
index 5b181c7..7b9f67b 100644
--- a/lib/CalDAV/SharingPlugin.php
+++ b/lib/CalDAV/SharingPlugin.php
@@ -4,6 +4,7 @@ namespace Sabre\CalDAV;
use Sabre\DAV;
use Sabre\DAV\Xml\Property\Href;
+use Sabre\DAV\Xml\Property\LocalHref;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
@@ -127,7 +128,7 @@ class SharingPlugin extends DAV\ServerPlugin {
if ($node instanceof ISharedCalendar) {
$propFind->handle('{' . Plugin::NS_CALENDARSERVER . '}shared-url', function() use ($node) {
- return new Href(
+ return new LocalHref(
$node->getSharedUrl()
);
});
@@ -332,7 +333,7 @@ class SharingPlugin extends DAV\ServerPlugin {
$writer->openMemory();
$writer->startDocument();
$writer->startElement('{' . Plugin::NS_CALENDARSERVER . '}shared-as');
- $writer->write(new Href($url));
+ $writer->write(new LocalHref($url));
$writer->endElement();
$response->setHeader('Content-Type', 'application/xml');
$response->setBody($writer->outputMemory());
diff --git a/lib/CalDAV/Subscriptions/Subscription.php b/lib/CalDAV/Subscriptions/Subscription.php
index 1e4848c..c4ac310 100644
--- a/lib/CalDAV/Subscriptions/Subscription.php
+++ b/lib/CalDAV/Subscriptions/Subscription.php
@@ -154,7 +154,7 @@ class Subscription extends Collection implements ISubscription, IACL {
switch ($prop) {
case '{http://calendarserver.org/ns/}source' :
- $r[$prop] = new Href($this->subscriptionInfo['source'], false);
+ $r[$prop] = new Href($this->subscriptionInfo['source']);
break;
default :
if (array_key_exists($prop, $this->subscriptionInfo)) {
diff --git a/lib/CardDAV/Plugin.php b/lib/CardDAV/Plugin.php
index b8bded0..d1bc2f7 100644
--- a/lib/CardDAV/Plugin.php
+++ b/lib/CardDAV/Plugin.php
@@ -5,6 +5,7 @@ namespace Sabre\CardDAV;
use Sabre\DAV;
use Sabre\DAV\Exception\ReportNotSupported;
use Sabre\DAV\Xml\Property\Href;
+use Sabre\DAV\Xml\Property\LocalHref;
use Sabre\DAVACL;
use Sabre\HTTP;
use Sabre\HTTP\RequestInterface;
@@ -156,11 +157,11 @@ class Plugin extends DAV\ServerPlugin {
$path = $propFind->getPath();
$propFind->handle('{' . self::NS_CARDDAV . '}addressbook-home-set', function() use ($path) {
- return new Href($this->getAddressBookHomeForPrincipal($path) . '/');
+ return new LocalHref($this->getAddressBookHomeForPrincipal($path) . '/');
});
if ($this->directories) $propFind->handle('{' . self::NS_CARDDAV . '}directory-gateway', function() {
- return new Href($this->directories);
+ return new LocalHref($this->directories);
});
}
diff --git a/lib/DAV/Xml/Property/Href.php b/lib/DAV/Xml/Property/Href.php
index 538e98d..0027f72 100644
--- a/lib/DAV/Xml/Property/Href.php
+++ b/lib/DAV/Xml/Property/Href.php
@@ -7,6 +7,7 @@ use Sabre\DAV\Browser\HtmlOutputHelper;
use Sabre\Xml\Element;
use Sabre\Xml\Reader;
use Sabre\Xml\Writer;
+use Sabre\Uri;
/**
* Href property
@@ -32,13 +33,6 @@ class Href implements Element, HtmlOutput {
protected $hrefs;
/**
- * Automatically prefix the url with the server base directory
- *
- * @var bool
- */
- protected $autoPrefix = true;
-
- /**
* Constructor
*
* You must either pass a string for a single href, or an array of hrefs.
@@ -47,16 +41,13 @@ class Href implements Element, HtmlOutput {
* and not relative to the servers base uri.
*
* @param string|string[] $href
- * @param bool $autoPrefix
*/
- function __construct($hrefs, $autoPrefix = true) {
+ function __construct($hrefs) {
if (is_string($hrefs)) {
$hrefs = [$hrefs];
}
$this->hrefs = $hrefs;
- $this->autoPrefix = $autoPrefix;
-
}
@@ -104,9 +95,7 @@ class Href implements Element, HtmlOutput {
function xmlSerialize(Writer $writer) {
foreach ($this->getHrefs() as $href) {
- if ($this->autoPrefix) {
- $href = $writer->contextUri . \Sabre\HTTP\encodePath($href);
- }
+ $href = Uri\resolve($writer->contextUri, $href);
$writer->writeElement('{DAV:}href', $href);
}
diff --git a/lib/DAV/Xml/Property/LocalHref.php b/lib/DAV/Xml/Property/LocalHref.php
new file mode 100644
index 0000000..76a27b9
--- /dev/null
+++ b/lib/DAV/Xml/Property/LocalHref.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Sabre\DAV\Xml\Property;
+
+use Sabre\HTTP;
+
+/**
+ * LocalHref property
+ *
+ * Like the Href property, this element represents {DAV:}href. The difference
+ * is that this is used stricly for paths on the server. The LocalHref property
+ * will prepare the path so it's a valid URI.
+ *
+ * These two objects behave identically:
+ * new LocalHref($path)
+ * new Href(\Sabre\HTTP\encodePath($path))
+ *
+ * LocalPath basically ensures that your spaces are %20, and everything that
+ * needs to be is uri encoded.
+ *
+ * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
+ * @author Evert Pot (http://www.rooftopsolutions.nl/)
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+class LocalHref extends Href {
+
+ /**
+ * Constructor
+ *
+ * You must either pass a string for a single href, or an array of hrefs.
+ *
+ * If auto-prefix is set to false, the hrefs will be treated as absolute
+ * and not relative to the servers base uri.
+ *
+ * @param string|string[] $href
+ */
+ function __construct($hrefs) {
+
+ parent::__construct(array_map(
+ function($href) {
+ return \Sabre\HTTP\encodePath($href);
+ },
+ (array)$hrefs
+ ));
+
+ }
+
+}
diff --git a/tests/Sabre/DAV/Xml/Property/HrefTest.php b/tests/Sabre/DAV/Xml/Property/HrefTest.php
index 13db316..bf58853 100644
--- a/tests/Sabre/DAV/Xml/Property/HrefTest.php
+++ b/tests/Sabre/DAV/Xml/Property/HrefTest.php
@@ -30,35 +30,6 @@ class HrefTest extends XmlTest {
', $xml);
}
- function testSerializeSpace() {
-
- $href = new Href('path alsopath');
- $this->assertEquals('path alsopath', $href->getHref());
-
- $this->contextUri = '/bla/';
-
- $xml = $this->write(['{DAV:}anything' => $href]);
-
- $this->assertXmlStringEqualsXmlString(
-'<?xml version="1.0"?>
-<d:anything xmlns:d="DAV:"><d:href>/bla/path%20alsopath</d:href></d:anything>
-', $xml);
-
- }
-
- function testSerializeNoPrefix() {
-
- $href = new Href('path', false);
- $this->assertEquals('path', $href->getHref());
-
- $xml = $this->write(['{DAV:}anything' => $href]);
-
- $this->assertXmlStringEqualsXmlString(
-'<?xml version="1.0"?>
-<d:anything xmlns:d="DAV:"><d:href>path</d:href></d:anything>
-', $xml);
-
- }
function testUnserialize() {
diff --git a/tests/Sabre/DAV/Xml/Property/LocalHrefTest.php b/tests/Sabre/DAV/Xml/Property/LocalHrefTest.php
new file mode 100644
index 0000000..c3f69c9
--- /dev/null
+++ b/tests/Sabre/DAV/Xml/Property/LocalHrefTest.php
@@ -0,0 +1,69 @@
+<?php
+
+namespace Sabre\DAV\Xml\Property;
+
+use Sabre\DAV;
+use Sabre\DAV\Browser\HtmlOutputHelper;
+use Sabre\DAV\Xml\XmlTest;
+
+class LocalHrefTest extends XmlTest {
+
+ function testConstruct() {
+
+ $href = new LocalHref('path');
+ $this->assertEquals('path', $href->getHref());
+
+ }
+
+ function testSerialize() {
+
+ $href = new LocalHref('path');
+ $this->assertEquals('path', $href->getHref());
+
+ $this->contextUri = '/bla/';
+
+ $xml = $this->write(['{DAV:}anything' => $href]);
+
+ $this->assertXmlStringEqualsXmlString(
+'<?xml version="1.0"?>
+<d:anything xmlns:d="DAV:"><d:href>/bla/path</d:href></d:anything>
+', $xml);
+
+ }
+ function testSerializeSpace() {
+
+ $href = new LocalHref('path alsopath');
+ $this->assertEquals('path%20alsopath', $href->getHref());
+
+ $this->contextUri = '/bla/';
+
+ $xml = $this->write(['{DAV:}anything' => $href]);
+
+ $this->assertXmlStringEqualsXmlString(
+'<?xml version="1.0"?>
+<d:anything xmlns:d="DAV:"><d:href>/bla/path%20alsopath</d:href></d:anything>
+', $xml);
+
+ }
+ function testToHtml() {
+
+ $href = new LocalHref([
+ '/foo/bar',
+ 'foo/bar',
+ 'http://example.org/bar'
+ ]);
+
+ $html = new HtmlOutputHelper(
+ '/base/',
+ []
+ );
+
+ $expected =
+ '<a href="/foo/bar">/foo/bar</a><br />' .
+ '<a href="/base/foo/bar">/base/foo/bar</a><br />' .
+ '<a href="http://example.org/bar">http://example.org/bar</a>';
+ $this->assertEquals($expected, $href->toHtml($html));
+
+ }
+
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-php/php-sabredav.git
More information about the Pkg-owncloud-commits
mailing list