From 13692a5d3b2ff30eefd3c5a89922bb57dd8168bf Mon Sep 17 00:00:00 2001 From: Henry Castro Date: Tue, 8 Mar 2016 18:35:44 -0400 Subject: [PATCH] Request discovery.xml from WOPI client --- appinfo/application.php | 3 +- controller/documentcontroller.php | 52 ++++++++++++++++++--- tests/controller/documentcontrollertest.php | 16 +++++-- 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/appinfo/application.php b/appinfo/application.php index a37351f6..fa7c1afb 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -49,7 +49,8 @@ class Application extends App { $c->query('CoreConfig'), $c->query('L10N'), $c->query('UserId'), - $c->query('ICacheFactory') + $c->query('ICacheFactory'), + $c->query('Logger') ); }); $container->registerService('SettingsController', function($c) { diff --git a/controller/documentcontroller.php b/controller/documentcontroller.php index f22cacb0..2033457a 100644 --- a/controller/documentcontroller.php +++ b/controller/documentcontroller.php @@ -28,6 +28,7 @@ use \OCA\Richdocuments\File; use \OCA\Richdocuments\Genesis; use \OC\Files\View; use \OCP\ICacheFactory; +use \OCP\ILogger; class DocumentController extends Controller{ @@ -35,16 +36,18 @@ class DocumentController extends Controller{ private $l10n; private $settings; private $cache; + private $logger; const ODT_TEMPLATE_PATH = '/assets/odttemplate.odt'; const CLOUDSUITE_TMP_PATH = '/documents-tmp/'; - public function __construct($appName, IRequest $request, IConfig $settings, IL10N $l10n, $uid, ICacheFactory $cache){ + public function __construct($appName, IRequest $request, IConfig $settings, IL10N $l10n, $uid, ICacheFactory $cache, ILogger $logger){ parent::__construct($appName, $request); $this->uid = $uid; $this->l10n = $l10n; $this->settings = $settings; $this->cache = $cache->create($appName); + $this->logger = $logger; } /** @@ -54,6 +57,7 @@ class DocumentController extends Controller{ public function index(){ \OC::$server->getNavigationManager()->setActiveEntry( 'richdocuments_index' ); $maxUploadFilesize = \OCP\Util::maxUploadFilesize("/"); + $wopiRemote = $this->settings->getAppValue('richdocuments', 'wopi_url'); $response = new TemplateResponse('richdocuments', 'documents', [ 'enable_previews' => $this->settings->getSystemValue('enable_previews', true), 'useUnstable' => $this->settings->getAppValue('richdocuments', 'unstable', 'false'), @@ -61,20 +65,54 @@ class DocumentController extends Controller{ 'uploadMaxFilesize' => $maxUploadFilesize, 'uploadMaxHumanFilesize' => \OCP\Util::humanFileSize($maxUploadFilesize), 'allowShareWithLink' => $this->settings->getAppValue('core', 'shareapi_allow_links', 'yes'), - 'wopi_url' => $this->settings->getAppValue('richdocuments', 'wopi_url'), + 'wopi_url' => $wopiRemote, ]); $policy = new ContentSecurityPolicy(); - $policy->addAllowedScriptDomain('\'self\' http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js http://cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.12/jquery.mousewheel.min.js \'unsafe-eval\' ' . $this->settings->getAppValue('richdocuments', 'wopi_url')); - $policy->addAllowedFrameDomain('\'self\' http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js http://cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.12/jquery.mousewheel.min.js \'unsafe-eval\' ' . $this->settings->getAppValue('richdocuments', 'wopi_url')); + $policy->addAllowedScriptDomain('\'self\' http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js http://cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.12/jquery.mousewheel.min.js \'unsafe-eval\' ' . $wopiRemote); + $policy->addAllowedFrameDomain('\'self\' http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js http://cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.12/jquery.mousewheel.min.js \'unsafe-eval\' ' . $wopiRemote); $policy->addAllowedConnectDomain('ws://' . $_SERVER['SERVER_NAME'] . ':9980'); $policy->addAllowedImageDomain('*'); $policy->allowInlineScript(true); $policy->addAllowedFontDomain('data:'); $response->setContentSecurityPolicy($policy); - - if(is_null($this->cache->get('discovery.xml'))) { - // TODO GET http://domain/hosting/discovery + $discovery = $this->cache->get('discovery.xml'); + + if(is_null($discovery)) { + if (($parts = parse_url($wopiRemote))) { + // Provides access to information about the capabilities of a WOPI client + // and the mechanisms for invoking those abilities through URIs. + // HTTP://server/hosting/discovery + $wopiDiscovery = sprintf( + "%s%s%s%s", + isset($parts['scheme']) ? $parts['scheme'] . "://" : '', + isset($parts['host']) ? $parts['host'] : '', + isset($parts['port']) ? ":" . $parts['port'] : '', + "/hosting/discovery" ); + + try { + $wopiClient = \OC::$server->getHTTPClientService()->newClient(); + $xml = $wopiClient->get($wopiDiscovery)->getBody(); + if ($xml) { + $loadEntities = libxml_disable_entity_loader(true); + $data = simplexml_load_string($xml); + libxml_disable_entity_loader($loadEntities); + if ($data !== false) { + // default ttl + $this->cache->set('discovery.xml', $xml); + } + else { + $this->logger->error('failure discovery.xml not well-formed XML string'); + } + } + else { + $this->logger->error('failure response discovery.xml'); + } + } catch (\Exception $e) { + $this->logger->error( + sprintf('Error getting discovery.xml: %s', $e->getMessage())); + } + } } return $response; diff --git a/tests/controller/documentcontrollertest.php b/tests/controller/documentcontrollertest.php index 42cbcf74..f72dee4d 100644 --- a/tests/controller/documentcontrollertest.php +++ b/tests/controller/documentcontrollertest.php @@ -17,6 +17,7 @@ class DocumentControllerTest extends \PHPUnit_Framework_TestCase { private $l10n; private $settings; private $cache; + private $logger; private $uid = 'jack_the_documents_tester'; private $password = 'password'; private $controller; @@ -34,17 +35,22 @@ class DocumentControllerTest extends \PHPUnit_Framework_TestCase { ->disableOriginalConstructor() ->getMock() ; - $this->cache = $this->getMockBuilder('\OCP\ICacheFactory') - ->disableOriginalConstructor() - ->getMock() - ; + $this->cache = $this->getMockBuilder('\OCP\ICacheFactory') + ->disableOriginalConstructor() + ->getMock() + ; + $this->logger = $this->getMockBuilder('\OCP\ILogger') + ->disableOriginalConstructor() + ->getMock() + ; $this->controller = new DocumentController( $this->appName, $this->request, $this->settings, $this->l10n, $this->uid, - $this->cache + $this->cache, + $this->logger ); $userManager = \OC::$server->getUserManager();