diff --git a/ajax/otpoll.php b/ajax/otpoll.php index fde9a451..eb951a1d 100644 --- a/ajax/otpoll.php +++ b/ajax/otpoll.php @@ -34,7 +34,6 @@ * @source: http://www.webodf.org/ * @source: http://gitorious.org/webodf/webodf/ */ - // OCP\JSON::checkLoggedIn(); // OCP\JSON::checkAppEnabled('office'); // session_write_close(); @@ -50,69 +49,78 @@ function bogusSession($i){ return $bs; } -$request = new OCA\Office\Request(); -$command = $request->getParam('command'); +try{ + $request = new OCA\Office\Request(); + $command = $request->getParam('command'); + $response = array(); + switch ($command){ + case 'session-list': + $response["session_list"] = array(bogusSession(0), bogusSession(1)); + break; + case 'join-session': + $response = "true"; // should fail when session is non-existent + break; + case 'sync-ops': + $seqHead = $request->getParam('args/seq_head'); + + if (!is_null($seqHead)){ + $esId = $request->getParam('args/es_id'); + $memberId = $request->getParam('args/member_id'); + $ops = $request->getParam('args/client_ops'); -$response = array(); -switch ($command){ - case 'session-list': - $response["session_list"] = array(bogusSession(0), bogusSession(1)); - break; - case 'join-session': - $response = "true"; // should fail when session is non-existent - break; - case 'sync-ops': - $seqHead = $request->getParam('args/seq_head'); - - if (!is_null($seqHead)){ - $esId = $request->getParam('args/es_id'); - $memberId = $request->getParam('args/member_id'); - $ops = $request->getParam('args/client_ops'); - - $currentHead = OCA\Office\Op::getHeadSeq($esId); + $currentHead = OCA\Office\Op::getHeadSeq($esId); - //if $postobject['args']['seq_head'] is the most recent op in the ops-table: - // append all ops in $postobject['args']['client_ops'] to the ops-table - if ($seqHead>$currentHead){ - foreach ($ops as $op){ - $op['opspec'] = json_encode($op['opspec']); - OCA\Office\Op::add($op); + //if $postobject['args']['seq_head'] is the most recent op in the ops-table: + // append all ops in $postobject['args']['client_ops'] to the ops-table + if ($seqHead > $currentHead){ + foreach ($ops as $op){ + $op['opspec'] = json_encode($op['opspec']); + try{ + OCA\Office\Op::add($op); + } catch (Exception $e){ + + } + } + } else { + // result: 'conflict', + // ops: a list of all ops since $postobject['args']['seq_head'] + // headSeq: the most recent op-seq + $response["result"] = 'conflict'; + $response["ops"] = OCA\Office\Op::getOpsAfter($esId, $seqHead); + $last = end($response["ops"]); + $response["headSeq"] = $last['seq']; } } else { - // result: 'conflict', - // ops: a list of all ops since $postobject['args']['seq_head'] - // headSeq: the most recent op-seq - $response["result"] = 'conflict'; - $response["ops"] = OCA\Office\Op::getOpsAfter($esId, $seqHead); - $last = end($response["ops"]); - $response["headSeq"] = $last['seq']; + // Error - empty seq_head passed :) + throw new BadRequestException(); } - - } else { - // Error :) - } - /* - * try { - * OCA\Office\Op::add( - * array( - * 'es_id' => ES_ID, - * 'seq' => SEQ, - * 'member' => MEMBER, - * 'opspec' => OPSPEC - * ) - * ); - * } catch (Exception $e) { - * - * } - */ - break; - default: - header('HTTP/1.1 400: BAD REQUEST'); - print(""); - print("{err:'bad request: [$postbody]'}"); - print(""); - exit(); + break; + default: + $ex = new BadRequestException(); + $ex->setBody("{err:'bad request: [" . $request->getRawRequest() . "]'}"); + throw $ex; + break; + } + + \OCP\JSON::success($response); +} catch (BadRequestException $e){ + header('HTTP/1.1 400: BAD REQUEST'); + print(""); + print($e->getBody()); + print(""); } +exit(); + +class BadRequestException extends Exception { + + protected $body = ""; + + public function setBody($body){ + $this->body = $body; + } -\OCP\JSON::success($response); + public function getBody(){ + return $this->body; + } +} \ No newline at end of file diff --git a/lib/request.php b/lib/request.php index 83d6ce4d..d6bbd8ba 100644 --- a/lib/request.php +++ b/lib/request.php @@ -5,10 +5,22 @@ namespace OCA\Office; class Request { protected $data = array(); + protected $rawRequest = ''; + public function __construct(){ - $this->data = json_decode(file_get_contents('php://input'), true); + $this->rawRequest = file_get_contents('php://input'); + $this->data = json_decode($this->rawRequest, true); + } + + public function getRawRequest(){ + return $this->rawRequest; } + public function getParam($name){ + if (empty($name)){ + return $this->data; + } + $path = explode('/', $name); reset($path);