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\OAuth;
20 use \OCA\Grauphel\Lib\Dependencies;
27 * @author Christian Weiske <cweiske@cweiske.de>
28 * @copyright 2014 Christian Weiske
29 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
30 * @version Release: @package_version@
31 * @link http://cweiske.de/grauphel.htm
33 class ApiController extends Controller
42 public function index()
44 $deps = Dependencies::get();
45 $authenticated = false;
47 $oauth->setDeps($deps);
48 $urlGen = $deps->urlGen;
51 $provider = new \OAuthProvider();
52 $oauth->registerHandler($provider)
53 ->registerAccessTokenHandler($provider);
54 $provider->checkOAuthRequest(
55 $urlGen->getAbsoluteURL(
56 $urlGen->linkToRoute('grauphel.api.index')
59 $authenticated = true;
60 $token = $deps->tokens->load('access', $provider->token);
61 $username = $token->user;
63 } catch (\OAuth_Exception $e) {
64 $deps->renderer->errorOut($e->getMessage());
65 } catch (\OAuthException $e) {
66 if ($e->getCode() != OAUTH_PARAMETER_ABSENT) {
72 'oauth_request_token_url' => $urlGen->getAbsoluteURL(
73 $urlGen->linkToRoute('grauphel.oauth.requestToken')
75 'oauth_authorize_url' => $urlGen->getAbsoluteURL(
76 $urlGen->linkToRoute('grauphel.oauth.authorize')
78 'oauth_access_token_url' => $urlGen->getAbsoluteURL(
79 $urlGen->linkToRoute('grauphel.oauth.accessToken')
81 'api-version' => '1.0',
85 $data['user-ref'] = array(
86 'api-ref' => $urlGen->getAbsoluteURL(
88 'grauphel.api.user', array('user' => $username)
91 'href' => null,//FIXME
95 return new JSONResponse($data);
96 $deps->renderer->sendJson($data);
100 * GET /api/1.0/$user/notes/$noteguid
106 public function note()
108 $deps = Dependencies::get();
109 $username = $deps->urlGen->loadUsername();
110 $guid = $deps->urlGen->loadGuid();
111 $oauth = new \OAuth();
112 $oauth->setDeps($deps);
113 $oauth->verifyOAuthUser($username, $deps->urlGen->note($username, $guid));
115 $note = $deps->notes->load($username, $guid, false);
116 if ($note === null) {
117 header('HTTP/1.0 404 Not Found');
118 header('Content-type: text/plain');
119 echo "Note does not exist\n";
123 $data = array('note' => array($note));
124 $deps->renderer->sendJson($data);
128 * GET|PUT /api/1.0/$user/notes
134 public function notes()
136 $deps = Dependencies::get();
137 $username = $deps->urlGen->loadUsername();
138 $oauth = new \OAuth();
139 $oauth->setDeps($deps);
140 $oauth->verifyOAuthUser($username, $deps->urlGen->notes($username));
142 $syncdata = $deps->notes->loadSyncData($username);
144 $this->handleNoteSave($username, $syncdata);
147 if (isset($_GET['since'])) {
148 $since = (int) $_GET['since'];
151 if (isset($_GET['include_notes']) && $_GET['include_notes']) {
152 $notes = $deps->notes->loadNotesFull($username, $since);
154 $notes = $deps->notes->loadNotesOverview($username, $since);
157 //work around bug https://bugzilla.gnome.org/show_bug.cgi?id=734313
158 foreach ($notes as $note) {
159 if (isset($note->{'note-content-version'})) {
160 $note->{'note-content-version'} = 0.3;
165 'latest-sync-revision' => $syncdata->latestSyncRevision,
168 $deps->renderer->sendJson($data);
171 protected function handleNoteSave($username, $syncdata)
173 if ($_SERVER['REQUEST_METHOD'] != 'PUT') {
177 $data = file_get_contents('php://input');
178 $putObj = json_decode($data);
179 if ($putObj === NULL) {
180 errorOut('Invalid JSON data in PUT request');
183 //structural validation
184 if (!isset($putObj->{'latest-sync-revision'})) {
185 errorOut('Missing "latest-sync-revision"');
187 if (!isset($putObj->{'note-changes'})) {
188 errorOut('Missing "note-changes"');
190 foreach ($putObj->{'note-changes'} as $note) {
191 if (!isset($note->guid) || $note->guid == '') {
192 errorOut('Missing "guid" on note');
197 if ($putObj->{'latest-sync-revision'} != $syncdata->latestSyncRevision +1
198 && $syncdata->latestSyncRevision != -1
200 errorOut('Wrong "latest-sync-revision". You are not up to date.');
204 $deps = Dependencies::get();
205 ++$syncdata->latestSyncRevision;
206 foreach ($putObj->{'note-changes'} as $noteUpdate) {
207 $note = $deps->notes->load($username, $noteUpdate->guid);
208 if (isset($noteUpdate->command) && $noteUpdate->command == 'delete') {
209 $deps->notes->delete($username, $noteUpdate->guid);
211 $deps->notes->update(
212 $note, $noteUpdate, $syncdata->latestSyncRevision
214 $deps->notes->save($username, $note);
218 $deps->notes->saveSyncData($username, $syncdata);
228 public function user()
230 $deps = Dependencies::get();
231 $username = $deps->urlGen->loadUsername();
233 $oauth = new \OAuth();
234 $oauth->setDeps($deps);
235 $oauth->verifyOAuthUser($username, $deps->urlGen->user($username));
237 $syncdata = $deps->notes->loadSyncData($username);
240 'user-name' => $username,
241 'first-name' => null,
243 'notes-ref' => array(
244 'api-ref' => $deps->urlGen->notes($username),
247 'latest-sync-revision' => $syncdata->latestSyncRevision,
248 'current-sync-guid' => $syncdata->currentSyncGuid,
250 $deps->renderer->sendJson($data);