9 * @author Christian Weiske <cweiske@cweiske.de>
10 * @copyright 2014 Christian Weiske
11 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12 * @link http://cweiske.de/grauphel.htm
14 namespace OCA\Grauphel\Controller;
16 use \OCP\AppFramework\Controller;
17 use \OCP\AppFramework\Http\JSONResponse;
19 use \OCA\Grauphel\Lib\NoteStorage;
20 use \OCA\Grauphel\Lib\OAuth;
21 use \OCA\Grauphel\Lib\Dependencies;
28 * @author Christian Weiske <cweiske@cweiske.de>
29 * @copyright 2014 Christian Weiske
30 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
31 * @version Release: @package_version@
32 * @link http://cweiske.de/grauphel.htm
34 class ApiController extends Controller
37 * constructor of the controller
39 * @param string $appName Name of the app
40 * @param IRequest $request Instance of the request
42 public function __construct($appName, \OCP\IRequest $request, $user)
44 parent::__construct($appName, $request);
46 $this->deps = Dependencies::get();
47 $this->notes = new NoteStorage($this->deps->urlGen);
49 //default http header: we assume something is broken
50 header('HTTP/1.0 500 Internal Server Error');
60 public function index($route = 'grauphel.api.index')
62 $deps = Dependencies::get();
63 $authenticated = false;
65 $oauth->setDeps($deps);
66 $urlGen = $deps->urlGen;
69 $provider = new \OAuthProvider();
70 $oauth->registerHandler($provider)
71 ->registerAccessTokenHandler($provider);
72 $provider->checkOAuthRequest(
73 $urlGen->getAbsoluteURL(
74 $urlGen->linkToRoute($route)
77 $authenticated = true;
78 $token = $deps->tokens->load('access', $provider->token);
79 $username = $token->user;
81 } catch (\OAuth_Exception $e) {
82 $deps->renderer->errorOut($e->getMessage());
83 } catch (\OAuthException $e) {
84 if ($e->getCode() != OAUTH_PARAMETER_ABSENT) {
87 if ($this->user !== null) {
88 $username = $this->user->getUID();
89 $authenticated = true;
94 'oauth_request_token_url' => $urlGen->getAbsoluteURL(
95 $urlGen->linkToRoute('grauphel.oauth.requestToken')
97 'oauth_authorize_url' => $urlGen->getAbsoluteURL(
98 $urlGen->linkToRoute('grauphel.oauth.authorize')
100 'oauth_access_token_url' => $urlGen->getAbsoluteURL(
101 $urlGen->linkToRoute('grauphel.oauth.accessToken')
103 'api-version' => '1.0',
106 if ($authenticated) {
107 $data['user-ref'] = array(
108 'api-ref' => $urlGen->getAbsoluteURL(
109 $urlGen->linkToRoute(
110 'grauphel.api.user', array('username' => $username)
113 'href' => null,//FIXME
117 return new JSONResponse($data);
127 public function indexSlash()
129 return $this->index('grauphel.api.indexSlash');
139 public function user($username)
141 $this->verifyUser($username);
142 $syncdata = $this->notes->loadSyncData($username);
145 'user-name' => $username,
146 'first-name' => null,
148 'notes-ref' => array(
149 'api-ref' => $this->deps->urlGen->getAbsoluteURL(
150 $this->deps->urlGen->linkToRoute(
151 'grauphel.api.notes', array('username' => $username)
156 'latest-sync-revision' => $syncdata->latestSyncRevision,
157 'current-sync-guid' => $syncdata->currentSyncGuid,
159 return new JSONResponse($data);
163 * GET /api/1.0/$user/notes
169 public function notes($username)
173 $this->deps->urlGen->getAbsoluteURL(
174 $this->deps->urlGen->linkToRoute(
175 'grauphel.api.notes', array('username' => $username)
179 $syncdata = $this->notes->loadSyncData($username);
180 return $this->fetchNotes($username, $syncdata);
184 * PUT /api/1.0/$user/notes
190 public function notesSave($username)
194 $this->deps->urlGen->getAbsoluteURL(
195 $this->deps->urlGen->linkToRoute(
196 'grauphel.api.user', array('username' => $username)
200 $syncdata = $this->notes->loadSyncData($username);
202 $this->handleNoteSave($username, $syncdata);
204 return $this->fetchNotes($username, $syncdata);
207 protected function fetchNotes($username, $syncdata)
210 if (isset($_GET['since'])) {
211 $since = (int) $_GET['since'];
214 if (isset($_GET['include_notes']) && $_GET['include_notes']) {
215 $notes = $this->notes->loadNotesFull($username, $since);
217 $notes = $this->notes->loadNotesOverview($username, $since);
220 //work around bug https://bugzilla.gnome.org/show_bug.cgi?id=734313
221 foreach ($notes as $note) {
222 if (isset($note->{'note-content-version'})) {
223 $note->{'note-content-version'} = 0.3;
228 'latest-sync-revision' => $syncdata->latestSyncRevision,
231 return new JSONResponse($data);
234 protected function handleNoteSave($username, $syncdata)
236 if ($_SERVER['REQUEST_METHOD'] != 'PUT') {
240 $data = file_get_contents('php://input');
241 $putObj = json_decode($data);
242 if ($putObj === NULL) {
243 errorOut('Invalid JSON data in PUT request');
246 //structural validation
247 if (!isset($putObj->{'latest-sync-revision'})) {
248 errorOut('Missing "latest-sync-revision"');
250 if (!isset($putObj->{'note-changes'})) {
251 errorOut('Missing "note-changes"');
253 foreach ($putObj->{'note-changes'} as $note) {
254 if (!isset($note->guid) || $note->guid == '') {
255 errorOut('Missing "guid" on note');
260 if ($putObj->{'latest-sync-revision'} != $syncdata->latestSyncRevision +1
261 && $syncdata->latestSyncRevision != -1
263 errorOut('Wrong "latest-sync-revision". You are not up to date.');
267 $deps = Dependencies::get();
268 ++$syncdata->latestSyncRevision;
269 foreach ($putObj->{'note-changes'} as $noteUpdate) {
270 $note = $deps->notes->load($username, $noteUpdate->guid);
271 if (isset($noteUpdate->command) && $noteUpdate->command == 'delete') {
272 $deps->notes->delete($username, $noteUpdate->guid);
274 $deps->notes->update(
275 $note, $noteUpdate, $syncdata->latestSyncRevision
277 $deps->notes->save($username, $note);
281 $deps->notes->saveSyncData($username, $syncdata);
285 * GET /api/1.0/$user/notes/$noteguid
291 public function note()
294 $deps = Dependencies::get();
295 $username = $deps->urlGen->loadUsername();
296 $guid = $deps->urlGen->loadGuid();
297 $oauth = new \OAuth();
298 $oauth->setDeps($deps);
299 $oauth->verifyOAuthUser($username, $deps->urlGen->note($username, $guid));
301 $note = $deps->notes->load($username, $guid, false);
302 if ($note === null) {
303 header('HTTP/1.0 404 Not Found');
304 header('Content-type: text/plain');
305 echo "Note does not exist\n";
309 $data = array('note' => array($note));
310 $deps->renderer->sendJson($data);
314 * Checks if the given user is authorized (by oauth token or normal login)
316 * @param string $username Username to verify
318 * @return boolean True if all is fine, Response in case of an error
320 protected function verifyUser($username, $curUrl)
322 if ($this->user !== null && $this->user->getUID() == $username) {
326 $oauth = new OAuth();
327 $oauth->setDeps($this->deps);
328 $oauth->verifyOAuthUser($username, $curUrl);