richdocuments/controller/sessioncontroller.php

203 lines
5.1 KiB
PHP
Raw Normal View History

2013-09-02 19:54:23 +03:00
<?php
/**
* ownCloud - Documents App
*
* @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.
*/
namespace OCA\Documents\Controller;
use \OCP\AppFramework\Controller;
use \OCP\IRequest;
use \OCP\AppFramework\Http\JSONResponse;
use \OCA\Documents\Db;
use \OCA\Documents\File;
use \OCA\Documents\Helper;
use OCA\Documents\Filter;
2013-09-02 19:54:23 +03:00
class SessionController extends Controller{
protected $uid;
public function __construct($appName, IRequest $request, $uid){
parent::__construct($appName, $request);
$this->uid = $uid;
}
/**
* @NoAdminRequired
* @PublicPage
*/
public function joinAsGuest($token, $name){
$uid = substr($name, 0, 16);
2014-04-09 16:57:42 +03:00
2013-12-19 01:14:32 +03:00
try {
$file = File::getByShareToken($token);
2014-09-08 22:05:31 +03:00
if ($file->isPasswordProtected() && !$file->checkPassword('')){
throw new \Exception('Not authorized');
}
$response = array_merge(
Db\Session::start($uid, $file),
array('status'=>'success')
);
2013-12-19 01:14:32 +03:00
} catch (\Exception $e){
Helper::warnLog('Starting a session failed. Reason: ' . $e->getMessage());
$response = array (
'status'=>'error'
);
2013-12-19 01:14:32 +03:00
}
return $response;
}
/**
* @NoAdminRequired
*/
public function joinAsUser($fileId){
2013-12-19 01:14:32 +03:00
try {
2014-04-09 16:57:42 +03:00
$view = \OC\Files\Filesystem::getView();
$path = $view->getPath($fileId);
if ($view->isUpdatable($path)) {
$file = new File($fileId);
$response = Db\Session::start($this->uid, $file);
2013-12-19 01:14:32 +03:00
} else {
2014-08-12 19:32:32 +03:00
$info = $view->getFileInfo($path);
$response = array(
2014-04-09 16:57:42 +03:00
'permissions' => $info['permissions'],
2013-12-19 01:14:32 +03:00
'id' => $fileId
);
2013-12-19 01:14:32 +03:00
}
$response = array_merge(
$response,
array('status'=>'success')
);
2013-09-02 19:54:23 +03:00
} catch (\Exception $e){
Helper::warnLog('Starting a session failed. Reason: ' . $e->getMessage());
$response = array (
'status'=>'error'
);
2013-09-02 19:54:23 +03:00
}
return $response;
2013-09-02 19:54:23 +03:00
}
2013-12-19 01:14:32 +03:00
/**
* @NoAdminRequired
* @PublicPage
2013-09-02 19:54:23 +03:00
* Store the document content to its origin
*/
public function save(){
2013-09-03 00:06:21 +03:00
try {
$esId = $this->request->server['HTTP_WEBODF_SESSION_ID'];
2013-09-27 18:43:10 +03:00
if (!$esId){
2013-09-03 00:06:21 +03:00
throw new \Exception('Session id can not be empty');
}
$memberId = $this->request->server['HTTP_WEBODF_MEMBER_ID'];
2014-08-04 21:00:58 +03:00
$currentMember = new Db\Member();
2014-04-11 23:12:23 +03:00
$currentMember->load($memberId);
//check if member belongs to the session
2014-04-11 23:12:23 +03:00
if ($esId != $currentMember->getEsId()){
throw new \Exception($memberId . ' does not belong to session ' . $esId);
}
2014-05-14 00:06:10 +03:00
// Extra info for future usage
// $sessionRevision = $this->request->server['HTTP_WEBODF_SESSION_REVISION'];
2013-09-03 00:06:21 +03:00
//NB ouch! New document content is passed as an input stream content
$stream = fopen('php://input','r');
if (!$stream){
throw new \Exception('New content missing');
2013-09-03 00:06:21 +03:00
}
$content = stream_get_contents($stream);
2014-08-04 20:51:50 +03:00
$session = new Db\Session();
2013-09-27 18:43:10 +03:00
$session->load($esId);
2014-04-11 23:12:23 +03:00
if (!$session->getEsId()){
2013-09-03 00:06:21 +03:00
throw new \Exception('Session does not exist');
}
2014-04-11 23:12:23 +03:00
try {
2014-04-11 23:12:23 +03:00
if ($currentMember->getIsGuest()){
$file = File::getByShareToken($currentMember->getToken());
2014-04-11 00:59:51 +03:00
} else {
2014-04-11 23:12:23 +03:00
$file = new File($session->getFileId());
}
2014-06-26 18:21:51 +03:00
list($view, $path) = $file->getOwnerViewAndPath(true);
} catch (\Exception $e){
//File was deleted or unshared. We need to save content as new file anyway
//Sorry, but for guests it would be lost :(
if ($this->uid){
$view = new \OC\Files\View('/' . $this->uid . '/files');
$dir = \OCP\Config::getUserValue($this->uid, 'documents', 'save_path', '');
2014-05-14 00:06:10 +03:00
$path = Helper::getNewFileName($view, $dir . 'New Document.odt');
} else {
throw $e;
2014-05-14 00:06:10 +03:00
}
2013-09-02 19:54:23 +03:00
}
2013-09-03 00:06:21 +03:00
2014-08-04 21:00:58 +03:00
$member = new Db\Member();
$members = $member->getActiveCollection($esId);
$memberIds = array_map(
function($x){
return ($x['member_id']);
},
$members
);
// Active users except current user
$memberCount = count($memberIds) - 1;
2014-04-11 00:59:51 +03:00
if ($view->file_exists($path)){
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$currentHash = sha1($view->file_get_contents($path));
\OC_FileProxy::$enabled = $proxyStatus;
2014-04-11 23:12:23 +03:00
if (!Helper::isVersionsEnabled() && $currentHash !== $session->getGenesisHash()){
// Original file was modified externally. Save to a new one
$path = Helper::getNewFileName($view, $path, '-conflict');
}
2013-12-26 15:42:28 +00:00
$mimetype = $view->getMimeType($path);
} else {
$mimetype = Storage::MIMETYPE_LIBREOFFICE_WORDPROCESSOR;
}
2013-12-26 15:42:28 +00:00
$data = Filter::write($content, $mimetype);
if ($view->file_put_contents($path, $data['content'])){
// Not a last user
if ($memberCount>0){
// Update genesis hash to prevent conflicts
Helper::debugLog('Update hash');
2013-12-26 15:42:28 +00:00
$session->updateGenesisHash($esId, sha1($data['content']));
} else {
// Last user. Kill session data
2014-08-04 20:51:50 +03:00
Db\Session::cleanUp($esId);
}
2013-10-01 18:23:37 +03:00
$view->touch($path);
2013-09-11 16:58:01 +03:00
}
$response = array('status'=>'success');
2013-09-03 00:06:21 +03:00
} catch (\Exception $e){
Helper::warnLog('Saving failed. Reason:' . $e->getMessage());
\OC_Response::setStatus(500);
$response = array();
2013-09-02 19:54:23 +03:00
}
return $response;
2013-09-02 19:54:23 +03:00
}
}