richdocuments/ajax/controller.php

161 lines
3.7 KiB
PHP
Raw Normal View History

2013-08-08 17:05:58 +03:00
<?php
/**
* ownCloud - Office 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\Office;
class Controller {
2013-08-13 19:38:30 +03:00
/**
* Process partial/complete file download
* @param type $args - array containing session id as anelement with a key es_id
*/
2013-08-08 17:05:58 +03:00
public static function serve($args){
\OCP\JSON::checkLoggedIn();
$session = Session::getSession(@$args['es_id']);
$filename = isset($session['genesis_url']) ? $session['genesis_url'] : '';
$officeView = View::initOfficeView($session['owner']);
$download = new Download($officeView, $filename);
$download->sendResponse();
}
2013-08-13 19:38:30 +03:00
2013-08-08 17:05:58 +03:00
public static function startSession($args){
2013-08-08 19:10:51 +03:00
$uid = self::getUser();
2013-08-18 17:11:22 +03:00
$fileId = @$_POST['fileid'];
2013-08-08 17:05:58 +03:00
$officeView = View::initOfficeView($uid);
2013-08-18 17:11:22 +03:00
$genesisPath = View::storeDocument($uid, $fileId);
2013-08-08 17:05:58 +03:00
if ($genesisPath){
2013-08-18 17:11:22 +03:00
$session = Session::getSessionByFileId($fileId);
2013-08-10 01:00:25 +03:00
try {
if (!$session){
$hash = View::getHashByGenesis($uid, $genesisPath);
2013-08-18 17:11:22 +03:00
$session = Session::add($genesisPath, $hash, $fileId);
2013-08-10 01:00:25 +03:00
}
2013-08-12 20:14:42 +03:00
$session['member_id'] = (string) Member::add($session['es_id'], \OCP\User::getUser(), self::getRandomColor());
2013-08-10 01:00:25 +03:00
\OCP\JSON::success($session);
exit();
} catch (\Exception $e){
//TODO: Log
throw $e; //Debug
2013-08-08 17:05:58 +03:00
}
}
\OCP\JSON::error();
2013-08-10 01:00:25 +03:00
exit();
2013-08-08 17:05:58 +03:00
}
public static function joinSession($args){
$esId = @$args['es_id'];
\OCP\JSON::checkLoggedIn();
try {
if ($esId){
$session = Session::getSession($esId);
2013-08-12 20:14:42 +03:00
$session['member_id'] = (string) Member::add($session['es_id'], \OCP\User::getUser(), self::getRandomColor());
\OCP\JSON::success($session);
exit();
}
throw new \Exception();
} catch (\Exception $e){
//TODO: Log
2013-08-08 17:05:58 +03:00
}
\OCP\JSON::error();
}
2013-08-08 19:10:51 +03:00
2013-08-18 18:18:53 +03:00
public static function save(){
$uid = self::getUser();
$esId = @$_POST['es_id'];
$memberId = @$_POST['member_id'];
$content = @$_POST['content'];
if ($esId && $content){
$session = Session::getSession($esId);
$fileInfo = \OC\Files\Cache\Cache::getById($session['file_id']);
$path = $fileInfo[1];
$view = new \OC\Files\View('/' . $session['owner']);
$canWrite = ($view->file_exists($path) && $view->isUpdatable($path)) || $view->isCreatable($path);
if ($canWrite){
$view->file_put_contents($path, $content);
} else {
// TODO: report an error
}
}
}
2013-08-09 19:31:20 +03:00
public static function listSessions(){
self::getUser();
$sessions = Session::getAll();
2013-08-17 20:09:52 +03:00
if (!is_array($sessions)){
$sessions = array();
}
$preparedSessions = array_map(
function($x){return ($x['es_id']);},
$sessions
);
\OCP\JSON::success(array(
"session_list" => $preparedSessions
));
}
2013-08-18 17:11:22 +03:00
public static function sessionInfo(){
2013-08-17 20:09:52 +03:00
self::getUser();
2013-08-18 17:11:22 +03:00
$items = @$_POST['items'];
$info = array();
if (is_array($items)){
$info = Session::getInfoByFileid($items);
2013-08-09 19:31:20 +03:00
}
\OCP\JSON::success(array(
2013-08-18 17:11:22 +03:00
"info" => $info
2013-08-09 19:31:20 +03:00
));
}
2013-08-12 19:07:44 +03:00
public static function listSessionsHtml(){
self::getUser();
$sessions = Session::getAll();
if (!is_array($sessions)){
$sessions = array();
}
$preparedSessions = array_map(
function($x){return ($x['es_id']);},
$sessions
);
$invites = Invite::getAllInvites();
if (!is_array($invites)){
$invites = array();
}
$tmpl = new \OCP\Template('office', 'part.sessions', '');
$tmpl->assign('invites', $invites);
$tmpl->assign('sessions', $sessions);
echo $tmpl->fetchPage();
}
2013-08-12 20:14:42 +03:00
protected static function getRandomColor(){
$str = dechex(floor(rand(0, 16777215)));
$str = str_pad($str, 6, "0", STR_PAD_LEFT);
return '#' . $str;
}
2013-08-09 19:31:20 +03:00
2013-08-08 19:10:51 +03:00
protected static function getUser(){
\OCP\JSON::checkLoggedIn();
return \OCP\User::getUser();
}
2013-08-08 17:05:58 +03:00
}