diff options
Diffstat (limited to 'lib/Controller/DocumentController.php')
-rw-r--r-- | lib/Controller/DocumentController.php | 172 |
1 files changed, 121 insertions, 51 deletions
diff --git a/lib/Controller/DocumentController.php b/lib/Controller/DocumentController.php index b961b126..4a93558c 100644 --- a/lib/Controller/DocumentController.php +++ b/lib/Controller/DocumentController.php @@ -11,8 +11,13 @@ namespace OCA\Richdocuments\Controller; -use OCA\Richdocuments\WOPI\DiscoveryManager; +use OCA\Richdocuments\TokenManager; +use OCA\Richdocuments\WOPI\Parser; use \OCP\AppFramework\Controller; +use OCP\AppFramework\Http\JSONResponse; +use OCP\Files\Folder; +use OCP\Files\IRootFolder; +use OCP\Files\Node; use \OCP\IRequest; use \OCP\IConfig; use \OCP\IL10N; @@ -21,57 +26,148 @@ use \OCP\AppFramework\Http\TemplateResponse; use \OCA\Richdocuments\AppConfig; use \OCA\Richdocuments\Helper; use \OC\Files\View; -use \OCP\ICacheFactory; +use OCP\Share\IManager; class DocumentController extends Controller { - + /** @var string */ private $uid; + /** @var IL10N */ private $l10n; + /** @var IConfig */ private $settings; + /** @var AppConfig */ private $appConfig; - private $cache; - /** @var DiscoveryManager */ - private $discoveryManager; + /** @var Parser */ + private $wopiParser; + /** @var IManager */ + private $shareManager; + /** @var TokenManager */ + private $tokenManager; + /** @var IRootFolder */ + private $rootFolder; + const ODT_TEMPLATE_PATH = '/assets/odttemplate.odt'; + /** + * @param string $appName + * @param IRequest $request + * @param IConfig $settings + * @param AppConfig $appConfig + * @param IL10N $l10n + * @param Parser $wopiParser + * @param IManager $shareManager + * @param TokenManager $tokenManager + * @param IRootFolder $rootFolder + * @param string $UserId + */ public function __construct($appName, - $UserId, IRequest $request, IConfig $settings, AppConfig $appConfig, IL10N $l10n, - ICacheFactory $cache, - DiscoveryManager $discoveryManager) { + Parser $wopiParser, + IManager $shareManager, + TokenManager $tokenManager, + IRootFolder $rootFolder, + $UserId) { parent::__construct($appName, $request); $this->uid = $UserId; $this->l10n = $l10n; $this->settings = $settings; $this->appConfig = $appConfig; - $this->cache = $cache->create($appName); - $this->discoveryManager = $discoveryManager; + $this->wopiParser = $wopiParser; + $this->shareManager = $shareManager; + $this->tokenManager = $tokenManager; + $this->rootFolder = $rootFolder; } /** * @NoAdminRequired - * @NoCSRFRequired * + * @param string $fileId * @return TemplateResponse */ - public function index(){ - $response = new TemplateResponse('richdocuments', 'documents'); - $policy = new ContentSecurityPolicy(); - $policy->addAllowedFrameDomain($this->appConfig->getAppValue('wopi_url')); - $response->setContentSecurityPolicy($policy); - return $response; + public function index($fileId) { + try { + $folder = $this->rootFolder->getUserFolder($this->uid); + $item = $folder->getById($fileId)[0]; + if(!($item instanceof Node)) { + throw new \Exception(); + } + list($urlSrc, $token) = $this->tokenManager->getToken($item->getId()); + $params = [ + 'permissions' => $item->getPermissions(), + 'title' => $item->getName(), + 'fileId' => $item->getId(), + 'token' => $token, + 'urlsrc' => $urlSrc, + 'path' => '/', + ]; + + $response = new TemplateResponse('richdocuments', 'documents', $params, 'empty'); + $policy = new ContentSecurityPolicy(); + $policy->addAllowedFrameDomain($this->appConfig->getAppValue('wopi_url')); + $policy->allowInlineScript(true); + $response->setContentSecurityPolicy($policy); + return $response; + } catch (\Exception $e) { + } + + return new TemplateResponse('core', '403', [], 'guest'); + } + + /** + * @PublicPage + * + * @param string $shareToken + * @param string $fileName + * @return TemplateResponse + * @throws \Exception + */ + public function publicPage($shareToken, $fileName) { + try { + $share = $this->shareManager->getShareByToken($shareToken); + $node = $share->getNode(); + if($node instanceof Folder) { + $item = $node->get($fileName); + } else { + $item = $node; + } + if ($item instanceof Node) { + list($urlSrc, $token) = $this->tokenManager->getToken($item->getId(), $shareToken); + $params = [ + 'permissions' => $share->getPermissions(), + 'title' => $item->getName(), + 'fileId' => $item->getId(), + 'token' => $token, + 'urlsrc' => $urlSrc, + 'path' => '/', + ]; + + $response = new TemplateResponse('richdocuments', 'documents', $params, 'empty'); + $policy = new ContentSecurityPolicy(); + $policy->addAllowedFrameDomain($this->appConfig->getAppValue('wopi_url')); + $policy->allowInlineScript(true); + $response->setContentSecurityPolicy($policy); + return $response; + } + } catch (\Exception $e) { + } + + return new TemplateResponse('core', '403', [], 'guest'); } /** * @NoAdminRequired + * + * @param string $mimetype + * @param string $filename + * @param string $dir + * @return JSONResponse */ - public function create(){ - $mimetype = $this->request->post['mimetype']; - $filename = $this->request->post['filename']; - $dir = $this->request->post['dir']; + public function create($mimetype, + $filename, + $dir){ $view = new View('/' . $this->uid . '/files'); if (!$dir){ @@ -117,36 +213,9 @@ class DocumentController extends Controller { $content = file_get_contents(dirname(__DIR__) . self::ODT_TEMPLATE_PATH); } - $discovery_parsed = null; - try { - $discovery = $this->discoveryManager->get(); - - $loadEntities = libxml_disable_entity_loader(true); - $discovery_parsed = simplexml_load_string($discovery); - libxml_disable_entity_loader($loadEntities); - - if ($discovery_parsed === false) { - $this->cache->remove('discovery.xml'); - $wopiRemote = $this->getWopiUrl(false); - - return array( - 'status' => 'error', - 'message' => $this->l10n->t('Collabora Online: discovery.xml from "%s" is not a well-formed XML string.', array($wopiRemote)), - 'hint' => $this->l10n->t('Please contact the "%s" administrator.', array($wopiRemote)) - ); - } - } - catch (ResponseException $e) { - return array( - 'status' => 'error', - 'message' => $e->getMessage(), - 'hint' => $e->getHint() - ); - } - - if ($content && $view->file_put_contents($path, $content)){ + if ($content && $view->file_put_contents($path, $content)) { $info = $view->getFileInfo($path); - $ret = $this->getWopiSrcUrl($discovery_parsed, $mimetype); + $ret = $this->wopiParser->getUrlSrc($mimetype); $response = array( 'status' => 'success', 'fileid' => $info['fileid'], @@ -161,6 +230,7 @@ class DocumentController extends Controller { 'message' => (string) $this->l10n->t('Can\'t create document') ); } + return $response; } } |