richdocuments/ajax/sessionController.php

196 lines
4.9 KiB
PHP
Raw Normal View History

2013-09-02 19:54:23 +03:00
<?php
/**
* ownCloud - Documents App
*
* @author Victor Dubiniuk
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
namespace OCA\Documents;
class SessionController extends Controller{
public static function joinAsGuest($args){
self::preDispatchGuest();
2014-04-09 16:57:42 +03:00
$uid = Helper::getArrayValueByKey($_POST, 'name');
$guestUid = substr($uid, 0, 16);
2014-04-09 16:57:42 +03:00
2013-12-19 01:14:32 +03:00
try {
2014-04-09 16:57:42 +03:00
$token = Helper::getArrayValueByKey($args, 'token');
$fileId = File::getIdByShareToken($token);
$session = Db_Session::start($uid, $fileId, true);
\OCP\JSON::success($session);
2013-12-19 01:14:32 +03:00
} catch (\Exception $e){
Helper::warnLog('Starting a session failed. Reason: ' . $e->getMessage());
\OCP\JSON::error();
exit();
}
}
public static function joinAsUser($args){
2013-09-02 19:54:23 +03:00
$uid = self::preDispatch();
$fileId = intval(@$args['file_id']);
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)) {
$session = Db_Session::start($uid, $fileId, false);
\OCP\JSON::success($session);
2013-12-19 01:14:32 +03:00
} else {
2014-04-09 16:57:42 +03:00
$info = $view->getFileInfo();
2013-12-19 01:14:32 +03:00
\OCP\JSON::success(array(
2014-04-09 16:57:42 +03:00
'permissions' => $info['permissions'],
2013-12-19 01:14:32 +03:00
'id' => $fileId
));
}
2013-09-02 19:54:23 +03:00
exit();
} catch (\Exception $e){
Helper::warnLog('Starting a session failed. Reason: ' . $e->getMessage());
\OCP\JSON::error();
exit();
}
}
2013-12-19 01:14:32 +03:00
/**
2013-09-02 19:54:23 +03:00
* Store the document content to its origin
*/
public static function save(){
2013-09-03 00:06:21 +03:00
try {
2013-09-27 18:43:10 +03:00
$esId = @$_SERVER['HTTP_WEBODF_SESSION_ID'];
if (!$esId){
2013-09-03 00:06:21 +03:00
throw new \Exception('Session id can not be empty');
}
$memberId = @$_SERVER['HTTP_WEBODF_MEMBER_ID'];
$sessionRevision = @$_SERVER['HTTP_WEBODF_SESSION_REVISION'];
$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);
2013-09-27 18:43:10 +03:00
$session = new Db_Session();
$session->load($esId);
if (!$session->hasData()){
2013-09-03 00:06:21 +03:00
throw new \Exception('Session does not exist');
}
2013-09-27 18:43:10 +03:00
$sessionData = $session->getData();
try {
$file = new File($sessionData['file_id']);
if (!$file->isPublicShare()){
self::preDispatch();
} else {
self::preDispatchGuest();
}
list($view, $path) = $file->getOwnerViewAndPath();
} 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 :(
$uid = self::preDispatch();
$view = new \OC\Files\View('/' . $uid . '/files');
$dir = \OCP\Config::getUserValue(\OCP\User::getUser(), 'documents', 'save_path', '');
$path = Helper::getNewFileName($view, $dir . 'New Document.odt');
2013-09-02 19:54:23 +03:00
}
2013-09-03 00:06:21 +03:00
$member = new Db_Member();
$members = $member->getActiveCollection($esId);
$memberIds = array_map(
function($x){
return ($x['member_id']);
},
$members
);
//check if member belongs to the session
if (!in_array($memberId, $memberIds)){
throw new \Exception($memberId . ' does not belong to session ' . $esId);
}
// Active users except current user
$memberCount = count($memberIds) - 1;
2013-12-26 15:42:28 +00: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;
if (!Helper::isVersionsEnabled() && $currentHash !== $sessionData['genesis_hash']){
// 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
Db_Session::cleanUp($esId);
}
2013-10-01 18:23:37 +03:00
$view->touch($path);
2013-09-11 16:58:01 +03:00
}
2013-09-03 00:06:21 +03:00
\OCP\JSON::success();
} catch (\Exception $e){
Helper::warnLog('Saving failed. Reason:' . $e->getMessage());
//\OCP\JSON::error(array('message'=>$e->getMessage()));
\OC_Response::setStatus(500);
2013-09-02 19:54:23 +03:00
}
exit();
2013-09-02 19:54:23 +03:00
}
public static function info(){
self::preDispatch();
$items = @$_POST['items'];
$info = array();
if (is_array($items)){
2013-09-27 18:43:10 +03:00
$session = new Db_Session();
$info = $session->getInfoByFileId($items);
2013-09-02 19:54:23 +03:00
}
\OCP\JSON::success(array(
"info" => $info
));
}
public static function listAll(){
self::preDispatch();
2013-09-27 18:43:10 +03:00
$session = new Db_Session();
$sessions = $session->getCollection();
2013-09-02 19:54:23 +03:00
$preparedSessions = array_map(
function($x){
return ($x['es_id']);
}, $sessions
);
\OCP\JSON::success(array(
"session_list" => $preparedSessions
));
}
}