From c0e5c74e5c80f9b4977448847edb01b5722d89f6 Mon Sep 17 00:00:00 2001
From: Rasmus Fuhse <fuhse@data-quest.de>
Date: Thu, 22 Mar 2018 14:21:12 +0100
Subject: [PATCH] fix OwnCloud apache problem

---
 OwnCloudPlugin.class.php         |  6 +++---
 classes/OAuth.class.php          |  2 +-
 classes/OwncloudFolder.class.php | 34 +++++++++++++++++++++-----------
 migrations/1_init_plugin.php     |  2 +-
 readme.md                        |  3 +++
 5 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/OwnCloudPlugin.class.php b/OwnCloudPlugin.class.php
index d760997..64a542d 100755
--- a/OwnCloudPlugin.class.php
+++ b/OwnCloudPlugin.class.php
@@ -47,7 +47,7 @@ class OwnCloudPlugin extends StudIPPlugin implements FilesystemPlugin {
 
 
         $header = array();
-        $header[] = "Authorization: Bearer ".\Owncloud\OAuth::getAccessToken();
+        $header[] = OwnCloudFolder::getAuthHeader();
 
         $r = curl_init();
         curl_setopt($r, CURLOPT_CUSTOMREQUEST, "GET");
@@ -117,7 +117,7 @@ class OwnCloudPlugin extends StudIPPlugin implements FilesystemPlugin {
 
 
             $header = array();
-            $header[] = "Authorization: Bearer ".\Owncloud\OAuth::getAccessToken();
+            $header[] = OwnCloudFolder::getAuthHeader();
 
             $r = curl_init();
             curl_setopt($r, CURLOPT_CUSTOMREQUEST, "GET");
@@ -177,7 +177,7 @@ class OwnCloudPlugin extends StudIPPlugin implements FilesystemPlugin {
         }
         $webdav = $url . "remote.php/webdav/";
         $header = array();
-        $header[] = "Authorization: Bearer ".\Owncloud\OAuth::getAccessToken();
+        $header[] = OwnCloudFolder::getAuthHeader();
         $r = curl_init();
         curl_setopt($r, CURLOPT_CUSTOMREQUEST, "PROPFIND");
         curl_setopt($r, CURLOPT_URL, $webdav."/".$id);
diff --git a/classes/OAuth.class.php b/classes/OAuth.class.php
index fffb6e6..69e1bf0 100644
--- a/classes/OAuth.class.php
+++ b/classes/OAuth.class.php
@@ -13,7 +13,7 @@ class OAuth {
             throw new Exception("No valid access token. Please refresh you connection to Owncloud.");
         }
 
-        $header[] = "Authorization: Bearer ".$accessToken;
+        $header[] = OwnCloudFolder::getAuthHeader();
 
         $r = curl_init();
         curl_setopt($r, CURLOPT_URL, $url);
diff --git a/classes/OwncloudFolder.class.php b/classes/OwncloudFolder.class.php
index 4dd41e4..30bfc71 100644
--- a/classes/OwncloudFolder.class.php
+++ b/classes/OwncloudFolder.class.php
@@ -48,7 +48,7 @@ class OwncloudFolder extends VirtualFolderType {
 
             $webdav = $this->getWebDavURL();
             $header = array();
-            $header[] = "Authorization: Bearer ".\Owncloud\OAuth::getAccessToken();
+            $header[] = self::getAuthHeader();
             $header[] = "Destination: ". $webdav . $this->id;
     
             $r = curl_init();
@@ -76,7 +76,7 @@ class OwncloudFolder extends VirtualFolderType {
         $webdav = $this->getWebDavURL();
 
         $header = array();
-        $header[] = "Authorization: Bearer ".\Owncloud\OAuth::getAccessToken();
+        $header[] = self::getAuthHeader();
 
         $r = curl_init();
         curl_setopt($r, CURLOPT_CUSTOMREQUEST, "DELETE");
@@ -111,7 +111,7 @@ class OwncloudFolder extends VirtualFolderType {
         $file_ref_id = $this->id . (mb_strlen($this->id) ? '/' : '') . rawurlencode($filedata['name']);
 
         $header = array();
-        $header[] = "Authorization: Bearer ".\Owncloud\OAuth::getAccessToken();
+        $header[] = self::getAuthHeader();
 
         $url_template = "[InternetShortcut]\nURL=%s";
         if (is_a($filedata, "File")) {
@@ -174,7 +174,7 @@ class OwncloudFolder extends VirtualFolderType {
         $destination = $this->id . (mb_strlen($this->id) ? '/' : '') . rawurlencode($name);
 
         $header = array();
-        $header[] = "Authorization: Bearer ".\Owncloud\OAuth::getAccessToken();
+        $header[] = self::getAuthHeader();
         $header[] = "Destination: ". $webdav . $destination;
 
         $r = curl_init();
@@ -215,7 +215,7 @@ class OwncloudFolder extends VirtualFolderType {
         $destination = $this->id . (mb_strlen($this->id)?'/':'') . rawurlencode($name);
 
         $header = array();
-        $header[] = "Authorization: Bearer ".\Owncloud\OAuth::getAccessToken();
+        $header[] = self::getAuthHeader();
         $header[] = "Destination: ". $webdav . $destination;
 
         $r = curl_init();
@@ -256,7 +256,7 @@ class OwncloudFolder extends VirtualFolderType {
         $destination = $this->id . (mb_strlen($this->id)?'/':'') . rawurlencode($name);
 
         $header = array();
-        $header[] = "Authorization: Bearer ".\Owncloud\OAuth::getAccessToken();
+        $header[] = self::getAuthHeader();
         $header[] = "Destination: ". $webdav . $destination;
 
         $r = curl_init();
@@ -295,7 +295,7 @@ class OwncloudFolder extends VirtualFolderType {
         $destination = $this->id . (mb_strlen($this->id)?'/':'') . $name;
 
         $header = array();
-        $header[] = "Authorization: Bearer ".\Owncloud\OAuth::getAccessToken();
+        $header[] = self::getAuthHeader();
 
         $r = curl_init();
 
@@ -332,7 +332,7 @@ class OwncloudFolder extends VirtualFolderType {
 
 
         $header = array();
-        $header[] = "Authorization: Bearer ".\Owncloud\OAuth::getAccessToken();
+        $header[] = self::getAuthHeader();
         $header[] = "Depth: 1";
 
         $r = curl_init();
@@ -355,6 +355,14 @@ class OwncloudFolder extends VirtualFolderType {
         $doc = new DOMDocument();
         $doc->loadXML($xml);
 
+        foreach ($doc->getElementsByTagNameNS("DAV:", "error") as $error) {
+            foreach ($error->childNodes as $node) {
+                if (strtolower($node->tagName) === "s:message") {
+                    PageLayout::postError($node->nodeValue);
+                }
+            }
+        }
+
         foreach ($doc->getElementsByTagNameNS("DAV:","response") as $file) {
             //response
             //  -> href
@@ -393,7 +401,7 @@ class OwncloudFolder extends VirtualFolderType {
                         if ($prop->childNodes) {
                             foreach ($prop->childNodes as $attr) {
                                 if (strtolower($attr->tagName) === "d:resourcetype") {
-                                    $file_attributes['type'] = $attr->childNodes[0] && strtolower($attr->childNodes[0]->tagName) === "d:collection" ? "folder" : "file";
+                                    $file_attributes['type'] = $attr->childNodes->item(0) && strtolower($attr->childNodes->item(0)->tagName) === "d:collection" ? "folder" : "file";
                                 }
                                 if (strtolower($attr->tagName) === "d:getcontentlength") {
                                     $file_attributes['size'] = $attr->nodeValue;
@@ -433,7 +441,7 @@ class OwncloudFolder extends VirtualFolderType {
                         'mime_type' => $content_type,
                         'description' => "",
                         'chdate' => $file_attributes['chdate'],
-                        'download_url' => URLHelper::getURL("plugins.php/owncloudplugin/download/" . ($this->id ? $this->id . "/" : "") . $file_attributes['name'])
+                        'download_url' => URLHelper::getURL("plugins.php/owncloudplugin/download/" . ($this->id ? $this->id . "/" : "") . rawurlencode($file_attributes['name']))
                     );
                 }
             }
@@ -466,7 +474,7 @@ class OwncloudFolder extends VirtualFolderType {
             $this->folderdata['id'] = $request['name'];
         } else {
             $this->folderdata['id'] = $request['parent_id'] . '/' . $request['name'];
-        }        
+        }
         $this->folderdata['parent_id'] = $request['parent_id'];
         $this->folderdata['range_type'] = $plugin->getPluginId();
         $this->folderdata['range_id'] = $plugin->getPluginName();
@@ -503,4 +511,8 @@ class OwncloudFolder extends VirtualFolderType {
         return false;
     }
 
+    static public function getAuthHeader() {
+        return "Authorization: Bearer " . \Owncloud\OAuth::getAccessToken();
+    }
+
 }
diff --git a/migrations/1_init_plugin.php b/migrations/1_init_plugin.php
index c3d69bf..19605a2 100755
--- a/migrations/1_init_plugin.php
+++ b/migrations/1_init_plugin.php
@@ -6,7 +6,7 @@ class InitPlugin extends Migration {
         foreach ($configs as $config) {
             Config::get()->create($config, array(
                 'value' => "",
-                'type' => $config === "OWNCLOUD_ACTIVATED" ? "boolean" : "string",
+                'type' => in_array($config, ["OWNCLOUD_ACTIVATED"]) ? "boolean" : "string",
                 'range' => "user",
                 'section' => "Owncloud"
             ));
diff --git a/readme.md b/readme.md
index e15c36f..73e1152 100755
--- a/readme.md
+++ b/readme.md
@@ -35,4 +35,7 @@ Die n
 5. Die OwnCloud fragt Sie, ob Stud.IP in Ihrem Namen Daten abrufen und ver�ndern darf. Klicken Sie auf "Authorisieren".
 6. Jetzt landen Sie wieder in Stud.IP und die Schnittstelle zwischen Stud.IP und OwnCloud sollte eingerichtet sein.
 
+Probleme?
+
+* Manchmal scheint fast alles zu funktionieren. OAuth-Access-Token ist da, Refresh-Token ist da und der Refresh scheint auch zu klappen, aber es werden keine Dateien und Ordner angezeigt. Es erscheint stattdessen eine Fehlermeldung: "No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured, No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured, No 'Authorization: Bearer' header found. Either the client didn't send one, or the server is mis-configured". In dem Fall muss man vermutlich in die Datei .htacess die Zeile einbauen `SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1` 
 
-- 
GitLab