11888: increase announced own cloud server version
authorMichael Spahn <m.spahn@metaways.de>
Fri, 3 Mar 2017 12:04:07 +0000 (13:04 +0100)
committerPhilipp Schüle <p.schuele@metaways.de>
Fri, 12 May 2017 09:56:00 +0000 (11:56 +0200)
https://forge.tine20.org/view.php?id=11888

Change-Id: Idd8e7a2a44be5dc60264be0c1b1530023923e7a2
Reviewed-on: http://gerrit.tine20.com/customers/4301
Tested-by: Jenkins CI (http://ci.tine20.com/)
Reviewed-by: Philipp Schüle <p.schuele@metaways.de>
tests/tine20/Tinebase/WebDav/Plugin/OwnCloudTest.php
tine20/Tinebase/WebDav/Plugin/OwnCloud.php
tine20/status.php

index 77af26f..84cae9c 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /**
  * Tine 2.0 - http://www.tine20.org
- * 
+ *
  * @package     Tinebase
  * @subpackage  Frontend
  * @license     http://www.gnu.org/licenses/agpl.html
  */
 class Tinebase_WebDav_Plugin_OwnCloudTest extends Tinebase_WebDav_Plugin_AbstractBaseTest
 {
+    const REQUEST_BODY = '<?xml version="1.0" encoding="utf-8"?>
+        <propfind xmlns="DAV:">
+            <prop>
+                <getlastmodified xmlns="DAV:"/>
+                <getcontentlength xmlns="DAV:"/>
+                <resourcetype xmlns="DAV:"/>
+                <getetag xmlns="DAV:"/>
+                <id xmlns="http://owncloud.org/ns"/>
+            </prop>
+        </propfind>';
+
     /**
      * Runs the test methods of this class.
      *
@@ -23,7 +34,7 @@ class Tinebase_WebDav_Plugin_OwnCloudTest extends Tinebase_WebDav_Plugin_Abstrac
      */
     public static function main()
     {
-        $suite  = new PHPUnit_Framework_TestSuite('Tine 2.0 Plugin OwnCloud Tests');
+        $suite = new PHPUnit_Framework_TestSuite('Tine 2.0 Plugin OwnCloud Tests');
         PHPUnit_TextUI_TestRunner::run($suite);
     }
 
@@ -36,9 +47,18 @@ class Tinebase_WebDav_Plugin_OwnCloudTest extends Tinebase_WebDav_Plugin_Abstrac
     protected function setUp()
     {
         parent::setUp();
-        
+
         $this->plugin = new Tinebase_WebDav_Plugin_OwnCloud();
-        
+
+        // Create request, there is no in tinebase while running unittests, but the owncloud plugin needs an user agent
+        $request = \Zend\Http\PhpEnvironment\Request::fromString(
+            "POST /index.php HTTP/1.1\r\n" .
+            "Host: localhost\r\n" .
+            "Content-Type: application/json\r\n" .
+            "User-Agent: Mozilla/5.0 (Macintosh) mirall/2.2.4 (build 3709)\r\n"
+        );
+        Tinebase_Core::set('request', $request);
+
         $this->server->addPlugin($this->plugin);
     }
 
@@ -57,85 +77,92 @@ class Tinebase_WebDav_Plugin_OwnCloudTest extends Tinebase_WebDav_Plugin_Abstrac
     public function testGetPluginName()
     {
         $pluginName = $this->plugin->getPluginName();
-        
+
         $this->assertEquals('Tinebase_WebDav_Plugin_OwnCloud', $pluginName);
     }
-    
+
     /**
      * test testGetProperties method
      */
     public function testGetProperties()
     {
-        $body = '<?xml version="1.0" encoding="utf-8"?>
-                 <propfind xmlns="DAV:">
-                    <prop>
-                        <getlastmodified xmlns="DAV:"/>
-                        <getcontentlength xmlns="DAV:"/>
-                        <resourcetype xmlns="DAV:"/>
-                        <getetag xmlns="DAV:"/>
-                        <id xmlns="http://owncloud.org/ns"/>
-                    </prop>
-                 </propfind>';
-
         $request = new Sabre\HTTP\Request(array(
             'REQUEST_METHOD' => 'PROPFIND',
-            'REQUEST_URI'    => '/remote.php/webdav/' . Tinebase_Core::getUser()->accountDisplayName,
-            'HTTP_DEPTH'     => '0',
+            'REQUEST_URI' => '/remote.php/webdav/' . Tinebase_Core::getUser()->accountDisplayName,
+            'HTTP_DEPTH' => '0',
         ));
-        $request->setBody($body);
+        $request->setBody(static::REQUEST_BODY);
 
         $this->server->httpRequest = $request;
         $this->server->exec();
         //var_dump($this->response->body);
         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
-        
+
         $responseDoc = new DOMDocument();
         $responseDoc->loadXML($this->response->body);
         //$responseDoc->formatOutput = true; echo $responseDoc->saveXML();
         $xpath = new DomXPath($responseDoc);
         $xpath->registerNamespace('owncloud', 'http://owncloud.org/ns');
-        
+
         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/owncloud:id');
         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
         $this->assertNotEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
     }
-    
+
+    /**
+     * test testGetProperties method with an invalid client
+     */
+    public function testInvalidOwnCloudVersion()
+    {
+        static::setExpectedException(InvalidArgumentException::class, sprintf(
+            'OwnCloud client min version is "%s"!',
+            Tinebase_WebDav_Plugin_OwnCloud::OWNCLOUD_MIN_VERSION
+        ));
+
+
+        // use old owncloud user agent!
+        $request = \Zend\Http\PhpEnvironment\Request::fromString(
+            "POST /index.php HTTP/1.1\r\n" .
+            "Host: localhost\r\n" .
+            "Content-Type: application/json\r\n" .
+            "User-Agent: Mozilla/5.0 (Macintosh) mirall/1.5.0 (build 3709)\r\n"
+        );
+        Tinebase_Core::set('request', $request);
+
+        $request = new Sabre\HTTP\Request(array(
+            'REQUEST_METHOD' => 'PROPFIND',
+            'REQUEST_URI' => '/remote.php/webdav/' . Tinebase_Core::getUser()->accountDisplayName,
+            'HTTP_DEPTH' => '0',
+        ));
+        $request->setBody(static::REQUEST_BODY);
+
+        $this->server->httpRequest = $request;
+        $this->server->exec();
+    }
+
     /**
      * test testGetProperties method with alternate loginname config
      */
     public function testGetProperties2()
     {
         Tinebase_Config::getInstance()->set(Tinebase_Config::USE_LOGINNAME_AS_FOLDERNAME, true);
-        
-        $body = '<?xml version="1.0" encoding="utf-8"?>
-                 <propfind xmlns="DAV:">
-                    <prop>
-                        <getlastmodified xmlns="DAV:"/>
-                        <getcontentlength xmlns="DAV:"/>
-                        <resourcetype xmlns="DAV:"/>
-                        <getetag xmlns="DAV:"/>
-                        <id xmlns="http://owncloud.org/ns"/>
-                    </prop>
-                 </propfind>';
-    
+
         $request = new Sabre\HTTP\Request(array(
-                'REQUEST_METHOD' => 'PROPFIND',
-                'REQUEST_URI'    => '/remote.php/webdav/' . Tinebase_Core::getUser()->accountLoginName,
-                'HTTP_DEPTH'     => '0',
+            'REQUEST_METHOD' => 'PROPFIND',
+            'REQUEST_URI' => '/remote.php/webdav/' . Tinebase_Core::getUser()->accountLoginName,
+            'HTTP_DEPTH' => '0',
         ));
-        $request->setBody($body);
-    
+        $request->setBody(static::REQUEST_BODY);
+
         $this->server->httpRequest = $request;
         $this->server->exec();
-        //var_dump($this->response->body);
         $this->assertEquals('HTTP/1.1 207 Multi-Status', $this->response->status);
-    
+
         $responseDoc = new DOMDocument();
         $responseDoc->loadXML($this->response->body);
-        //$responseDoc->formatOutput = true; echo $responseDoc->saveXML();
         $xpath = new DomXPath($responseDoc);
         $xpath->registerNamespace('owncloud', 'http://owncloud.org/ns');
-    
+
         $nodes = $xpath->query('//d:multistatus/d:response/d:propstat/d:prop/owncloud:id');
         $this->assertEquals(1, $nodes->length, $responseDoc->saveXML());
         $this->assertNotEmpty($nodes->item(0)->nodeValue, $responseDoc->saveXML());
index b7fd060..2162427 100644 (file)
@@ -6,7 +6,7 @@
  * @subpackage  WebDAV
  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
  * @author      Lars Kneschke <l.kneschke@metaways.de>
- * @copyright   Copyright (c) 2013-2013 Metaways Infosystems GmbH (http://www.metaways.de)
+ * @copyright   Copyright (c) 2013-2017 Metaways Infosystems GmbH (http://www.metaways.de)
  *
  */
 
  * @package     Tinebase
  * @subpackage  WebDAV
  */
-
-class Tinebase_WebDav_Plugin_OwnCloud extends Sabre\DAV\ServerPlugin {
+class Tinebase_WebDav_Plugin_OwnCloud extends Sabre\DAV\ServerPlugin
+{
 
     const NS_OWNCLOUD = 'http://owncloud.org/ns';
+
+    /**
+     * Min version of owncloud
+     */
+    const OWNCLOUD_MIN_VERSION = '2.0.0';
+
     /**
-     * Reference to server object 
-     * 
-     * @var Sabre\DAV\Server 
+     * Max version of owncloud
+     *
+     * Adjust max version of supported owncloud clients for tine
+     */
+    const OWNCLOUD_MAX_VERSION = '100.0.0';
+
+    /**
+     * Reference to server object
+     *
+     * @var Sabre\DAV\Server
      */
     private $server;
 
     /**
-     * Initializes the plugin 
-     * 
-     * @param Sabre\DAV\Server $server 
+     * Initializes the plugin
+     *
+     * @param Sabre\DAV\Server $server
      * @return void
      */
-    public function initialize(Sabre\DAV\Server $server) 
+    public function initialize(Sabre\DAV\Server $server)
     {
         $this->server = $server;
-        
+
         $server->subscribeEvent('beforeGetProperties', array($this, 'beforeGetProperties'));
-        
+
         /* Namespaces */
         $server->xmlNamespaces[self::NS_OWNCLOUD] = 'owncloud';
-        
+
         array_push($server->protectedProperties,
             '{' . self::NS_OWNCLOUD . '}id'
         );
     }
-    
+
     /**
      * Adds ownCloud specific properties
      *
      * @param string $path
-     * @param DAV\INode $node
+     * @param \Sabre\DAV\INode $node
      * @param array $requestedProperties
      * @param array $returnedProperties
      * @return void
+     * @throws \InvalidArgumentException
      */
-    public function beforeGetProperties($path, Sabre\DAV\INode $node, array &$requestedProperties, array &$returnedProperties) 
-    {
+    public function beforeGetProperties(
+        $path,
+        Sabre\DAV\INode $node,
+        array &$requestedProperties,
+        array &$returnedProperties
+    ) {
+        $version = $this->getOwnCloudVersion();
+        if ($version !== null && !$this->isValidOwnCloudVersion()) {
+            $message = sprintf(
+                '%s::%s OwnCloud client min version is "%s"!',
+                __METHOD__,
+                __LINE__,
+                static::OWNCLOUD_MIN_VERSION
+            );
+
+            Tinebase_Core::getLogger()->debug($message);
+            throw new InvalidArgumentException($message);
+        } elseif (!$version) {
+            // If it's not even an owncloud version, don't add any owncloud specific features here.
+            return;
+        }
+
         $id = '{' . self::NS_OWNCLOUD . '}id';
-        
+
         if (in_array($id, $requestedProperties)) {
             unset($requestedProperties[array_search($id, $requestedProperties)]);
             if ($node instanceof Tinebase_Frontend_WebDAV_Node) {
@@ -73,4 +106,56 @@ class Tinebase_WebDav_Plugin_OwnCloud extends Sabre\DAV\ServerPlugin {
             }
         }
     }
+
+    /**
+     * Return the actuall owncloud version number
+     * @throws \InvalidArgumentException
+     */
+    protected function isValidOwnCloudVersion()
+    {
+        $version  = $this->getOwnCloudVersion();
+
+        return version_compare($version, static::OWNCLOUD_MIN_VERSION, 'ge')
+            && version_compare($version, static::OWNCLOUD_MAX_VERSION, 'le');
+    }
+
+    /**
+     * Get owncloud version number
+     *
+     * @return mixed|null
+     */
+    protected function getOwnCloudVersion() {
+        // Mozilla/5.0 (Macintosh) mirall/2.2.4 (build 3709)
+        /* @var $request \Zend\Http\PhpEnvironment\Request */
+        $request = Tinebase_Core::get(Tinebase_Core::REQUEST);
+
+        // In some cases this is called not out of an request, for example some tests, therefore we should require it here
+        // If it's not an owncloud server, we don't need to determine the version!
+        if (!$request) {
+            return null;
+        }
+
+        $useragentHeader = $request->getHeader('useragent');
+
+        $useragent = $useragentHeader ? $useragentHeader->getFieldValue() : null;
+
+        // If no valid header, this is not an owncloud client
+        if ($useragent === null) {
+            return null;
+        }
+
+        $match = [];
+
+        if (!preg_match('/mirall\/(\d\.\d\.\d)/', $useragent, $match)) {
+            return null;
+        }
+
+        $version = array_pop($match);
+
+        if ($version === '') {
+            $version = null;
+        }
+
+        return $version;
+    }
 }
index 3073c08..07cce9e 100644 (file)
 
 $values = array(
     'installed'     => true,
-    'version'       => '5.0.20',
-    'versionstring' => '5.0.11',
+    'version'       => '8.1.12.2',
+    'versionstring' => '8.1.12',
+    'maintenance'   => false,
     'edition'       => ''
 );
 
+header('Content-Type', 'application/json');
 echo(json_encode($values));