richdocuments/controller/documentcontroller.php

326 lines
9.4 KiB
PHP
Raw Normal View History

2013-09-02 19:54:23 +03:00
<?php
/**
2015-12-16 17:57:44 +03:00
* ownCloud - Richdocuments App
2013-09-02 19:54:23 +03:00
*
* @author Victor Dubiniuk
* @copyright 2014 Victor Dubiniuk victor.dubiniuk@gmail.com
2013-09-02 19:54:23 +03:00
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
2015-12-16 17:57:44 +03:00
namespace OCA\Richdocuments\Controller;
use \OCP\AppFramework\Controller;
use \OCP\IRequest;
use \OCP\IConfig;
use \OCP\IL10N;
use \OCP\AppFramework\Http\ContentSecurityPolicy;
use \OCP\AppFramework\Http\JSONResponse;
use \OCP\AppFramework\Http\TemplateResponse;
2015-12-16 17:57:44 +03:00
use \OCA\Richdocuments\Db;
use \OCA\Richdocuments\Helper;
use \OCA\Richdocuments\Storage;
use \OCA\Richdocuments\Download;
use \OCA\Richdocuments\DownloadResponse;
use \OCA\Richdocuments\File;
2016-03-08 10:51:49 -04:00
use \OCA\Richdocuments\Genesis;
use \OC\Files\View;
2016-03-08 10:51:49 -04:00
use \OCP\ICacheFactory;
2013-09-02 19:54:23 +03:00
class DocumentController extends Controller{
2016-01-12 15:20:38 +01:00
private $uid;
private $l10n;
private $settings;
2016-03-08 10:51:49 -04:00
private $cache;
2016-01-12 15:20:38 +01:00
const ODT_TEMPLATE_PATH = '/assets/odttemplate.odt';
const CLOUDSUITE_TMP_PATH = '/documents-tmp/';
2016-01-12 15:20:38 +01:00
2016-03-08 10:51:49 -04:00
public function __construct($appName, IRequest $request, IConfig $settings, IL10N $l10n, $uid, ICacheFactory $cache){
parent::__construct($appName, $request);
$this->uid = $uid;
$this->l10n = $l10n;
$this->settings = $settings;
2016-03-08 10:51:49 -04:00
$this->cache = $cache->create($appName);
}
2016-01-12 15:20:38 +01:00
2015-08-26 19:09:34 +03:00
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index(){
2015-12-16 17:57:44 +03:00
\OC::$server->getNavigationManager()->setActiveEntry( 'richdocuments_index' );
2015-08-26 19:09:34 +03:00
$maxUploadFilesize = \OCP\Util::maxUploadFilesize("/");
2015-12-16 17:57:44 +03:00
$response = new TemplateResponse('richdocuments', 'documents', [
2015-08-26 19:09:34 +03:00
'enable_previews' => $this->settings->getSystemValue('enable_previews', true),
2015-12-16 17:57:44 +03:00
'useUnstable' => $this->settings->getAppValue('richdocuments', 'unstable', 'false'),
'savePath' => $this->settings->getUserValue($this->uid, 'richdocuments', 'save_path', '/'),
2015-08-26 19:09:34 +03:00
'uploadMaxFilesize' => $maxUploadFilesize,
'uploadMaxHumanFilesize' => \OCP\Util::humanFileSize($maxUploadFilesize),
'allowShareWithLink' => $this->settings->getAppValue('core', 'shareapi_allow_links', 'yes'),
2016-03-05 18:14:16 -04:00
'wopi_url' => $this->settings->getAppValue('richdocuments', 'wopi_url'),
2015-08-26 19:09:34 +03:00
]);
$policy = new ContentSecurityPolicy();
2016-03-08 10:51:49 -04:00
$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->addAllowedConnectDomain('ws://' . $_SERVER['SERVER_NAME'] . ':9980');
$policy->addAllowedImageDomain('*');
$policy->allowInlineScript(true);
$policy->addAllowedFontDomain('data:');
$response->setContentSecurityPolicy($policy);
2016-03-08 10:51:49 -04:00
if(is_null($this->cache->get('discovery.xml'))) {
// TODO GET http://domain/hosting/discovery
}
return $response;
2015-08-26 19:09:34 +03:00
}
2016-01-12 15:20:38 +01:00
/**
* @NoAdminRequired
*/
public function create(){
$mimetype = $this->request->post['mimetype'];
$view = new View('/' . $this->uid . '/files');
$dir = $this->settings->getUserValue($this->uid, $this->appName, 'save_path', '/');
if (!$view->is_dir($dir)){
$dir = '/';
}
$basename = $this->l10n->t('New Document.odt');
switch ($mimetype) {
case 'application/vnd.oasis.opendocument.spreadsheet':
$basename = $this->l10n->t('New Spreadsheet.ods');
break;
case 'application/vnd.oasis.opendocument.presentation':
$basename = $this->l10n->t('New Presentation.odp');
break;
default:
// to be safe
$mimetype = 'application/vnd.oasis.opendocument.text';
break;
}
$path = Helper::getNewFileName($view, $dir . '/' . $basename);
2016-01-12 15:20:38 +01:00
$content = '';
if (class_exists('\OC\Files\Type\TemplateManager')){
$manager = \OC_Helper::getFileTemplateManager();
$content = $manager->getTemplate($mimetype);
}
2016-01-12 15:20:38 +01:00
if (!$content){
$content = file_get_contents(dirname(__DIR__) . self::ODT_TEMPLATE_PATH);
}
2016-01-12 15:20:38 +01:00
if ($content && $view->file_put_contents($path, $content)){
$info = $view->getFileInfo($path);
$response = array(
'status' => 'success',
'fileid' => $info['fileid']
);
} else {
$response = array(
'status' => 'error',
'message' => (string) $this->l10n->t('Can\'t create document')
);
}
return $response;
2013-09-13 13:18:45 +03:00
}
/**
* @NoAdminRequired
* @PublicPage
* Copy the file to a temporary location that is shared between the
* cloudsuite server part and owncloud.
*/
public function localLoad($fileId){
$view = \OC\Files\Filesystem::getView();
$path = $view->getPath($fileId);
if (!$view->is_file($path)) {
return array(
'status' => 'error',
'message' => (string) $this->l10n->t('Unable to copy document for CloudSuite access.')
);
}
$filename = dirname(__DIR__) . self::CLOUDSUITE_TMP_PATH . 'ccs-' . $fileId;
if (file_exists($filename)) {
return array(
'status' => 'success', 'filename' => $filename,
'basename' => basename($filename)
);
}
$content = $view->file_get_contents($path);
file_put_contents($filename, $content);
// set the needed attribs
chmod($filename, 0660);
$modified = $view->filemtime($path);
if ($modified !== false)
touch($filename, $modified);
return array(
'status' => 'success', 'filename' => $filename,
'basename' => basename($filename)
);
}
2015-11-04 13:15:22 +01:00
/**
* @NoAdminRequired
* @PublicPage
* Copy the file to a temporary location that is shared between the
* cloudsuite server part and owncloud.
*/
public function localSave($fileId){
// get really just the basename for the case somebody tries to trick us
$basename = basename($this->request->post['basename']);
$filename = dirname(__DIR__) . self::CLOUDSUITE_TMP_PATH . $basename;
$view = \OC\Files\Filesystem::getView();
$path = $view->getPath($fileId);
if (!is_file($filename) || !$view->is_file($path)) {
return array(
'status' => 'error',
'message' => (string) $this->l10n->t('Unable to copy the document back from CloudSuite.')
);
}
$content = file_get_contents($filename);
$view->file_put_contents($path, $content);
return array(
'status' => 'success'
);
}
/**
* @NoAdminRequired
* @PublicPage
* Remove the temporary local copy of the document.
*/
public function localClose($fileId){
// get really just the basename for the case somebody tries to trick us
$basename = basename($this->request->post['basename']);
$filename = dirname(__DIR__) . self::CLOUDSUITE_TMP_PATH . $basename;
// remove temp file only when all edit instances are closed
$stat = stat($filename);
if ($stat['nlink'] == 1){
unlink($filename);
}
return array(
'status' => 'success'
);
}
2014-01-21 21:18:53 +03:00
/**
* @NoAdminRequired
* @PublicPage
2013-09-02 19:54:23 +03:00
* Process partial/complete file download
*/
public function serve($esId){
2014-08-04 20:51:50 +03:00
$session = new Db\Session();
$session->load($esId);
2016-01-12 15:20:38 +01:00
2014-04-11 23:12:23 +03:00
$filename = $session->getGenesisUrl() ? $session->getGenesisUrl() : '';
return new DownloadResponse($this->request, $session->getOwner(), $filename);
2013-09-02 19:54:23 +03:00
}
2016-01-12 15:20:38 +01:00
2014-10-28 01:59:49 +03:00
/**
* @NoAdminRequired
*/
public function download($path){
if (!$path){
$response = new JSONResponse();
$response->setStatus(Http::STATUS_BAD_REQUEST);
return $response;
}
2016-01-12 15:20:38 +01:00
$fullPath = '/files' . $path;
$fileInfo = \OC\Files\Filesystem::getFileInfo($path);
if ($fileInfo){
2015-12-16 17:57:44 +03:00
if($fileInfo->getMimeType() !== \OCA\Richdocuments\Filter\Office::NATIVE_MIMETYPE){
2014-10-28 01:59:49 +03:00
$file = new File($fileInfo->getId());
$genesis = new Genesis($file);
$fullPath = $genesis->getPath();
}
}
return new DownloadResponse($this->request, $this->uid, $fullPath);
2014-10-28 01:59:49 +03:00
}
2016-01-12 15:20:38 +01:00
/**
* @NoAdminRequired
*/
public function rename($fileId){
$name = $this->request->post['name'];
2014-04-09 18:54:22 +03:00
$view = \OC\Files\Filesystem::getView();
$path = $view->getPath($fileId);
2013-12-19 00:07:17 +03:00
if ($name && $view->is_file($path) && $view->isUpdatable($path)) {
$newPath = dirname($path) . '/' . $name;
if ($view->rename($path, $newPath)) {
return array('status' => 'success');
2013-12-19 00:07:17 +03:00
}
}
return array(
'status' => 'error',
'message' => (string) $this->l10n->t('You don\'t have permission to rename this document')
);
2013-12-19 00:07:17 +03:00
}
2013-09-02 19:54:23 +03:00
/**
* @NoAdminRequired
2013-09-02 19:54:23 +03:00
* lists the documents the user has access to (including shared files, once the code in core has been fixed)
* also adds session and member info for these files
*/
public function listAll(){
2014-04-14 18:05:18 +03:00
$found = Storage::getDocuments();
2013-09-02 19:54:23 +03:00
$fileIds = array();
2014-04-14 18:05:18 +03:00
$documents = array();
foreach ($found as $key=>$document) {
if (is_object($document)){
$documents[] = $document->getData();
} else {
$documents[$key] = $document;
}
$documents[$key]['icon'] = preg_replace('/\.png$/', '.svg', \OCP\Template::mimetype_icon($document['mimetype']));
$documents[$key]['hasPreview'] = \OC::$server->getPreviewManager()->isMimeSupported($document['mimetype']);
2013-09-02 19:54:23 +03:00
$fileIds[] = $document['fileid'];
}
2013-10-01 17:49:03 +03:00
usort($documents, function($a, $b){
return @$b['mtime']-@$a['mtime'];
});
2016-01-12 15:20:38 +01:00
2014-08-04 20:51:50 +03:00
$session = new Db\Session();
2013-09-27 18:43:10 +03:00
$sessions = $session->getCollectionBy('file_id', $fileIds);
2013-09-02 19:54:23 +03:00
$members = array();
2014-08-04 21:00:58 +03:00
$member = new Db\Member();
2016-01-12 15:20:38 +01:00
foreach ($sessions as $session) {
$members[$session['es_id']] = $member->getActiveCollection($session['es_id']);
2013-09-02 19:54:23 +03:00
}
return array(
'status' => 'success', 'documents' => $documents,'sessions' => $sessions,'members' => $members
);
2013-09-02 19:54:23 +03:00
}
2013-11-08 15:46:30 +00:00
}