wip
[grauphel.git] / controller / apicontroller.php
1 <?php
2 /**
3  * Part of grauphel
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Grauphel
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
13  */
14 namespace OCA\Grauphel\Controller;
15 use \OCP\AppFramework\Controller;
16
17 /**
18  * Tomboy's REST API
19  *
20  * @category  Tools
21  * @package   Grauphel
22  * @author    Christian Weiske <cweiske@cweiske.de>
23  * @copyright 2014 Christian Weiske
24  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
25  * @version   Release: @package_version@
26  * @link      http://cweiske.de/grauphel.htm
27  */
28 class ApiController extends Controller
29 {
30     /**
31      * /api/1.0
32      */
33     public function index()
34     {
35         var_dump('asd');die();
36         $authenticated = false;
37         $oauth = new OAuth();
38         $oauth->setDeps($this->deps);
39         $urlGen = $this->deps->urlGen;
40
41         try {
42             $provider = new \OAuthProvider();
43             $oauth->registerHandler($provider)
44                 ->registerAccessTokenHandler($provider);
45             $provider->checkOAuthRequest($urlGen->fullPath());
46             $authenticated = true;
47             $token = $this->deps->tokens->load('access', $provider->token);
48             $username = $token->user;
49
50         } catch (OAuth_Exception $e) {
51             $this->deps->renderer->errorOut($e->getMessage());
52         } catch (\OAuthException $e) {
53             if ($e->getCode() != OAUTH_PARAMETER_ABSENT) {
54                 $oauth->error($e);
55             }
56         }
57
58         $data = array(
59             'oauth_request_token_url' => $urlGen->oauthRequestToken(),
60             'oauth_authorize_url'     => $urlGen->oauthAuthorize(),
61             'oauth_access_token_url'  => $urlGen->oauthAccessToken(),
62             'api-version' => '1.0',
63         );
64
65         if ($authenticated) {
66             $data['user-ref'] = array(
67                 'api-ref' => $urlGen->user($username),
68                 'href'    => $urlGen->userHtml($username),
69             );
70         }
71
72         $this->deps->renderer->sendJson($data);
73     }
74
75     /**
76      * GET /api/1.0/$user/notes/$noteguid
77      */
78     public function note()
79     {
80         $username = $this->deps->urlGen->loadUsername();
81         $guid     = $this->deps->urlGen->loadGuid();
82         $oauth = new OAuth();
83         $oauth->setDeps($this->deps);
84         $oauth->verifyOAuthUser($username, $this->deps->urlGen->note($username, $guid));
85
86         $note = $this->deps->notes->load($username, $guid, false);
87         if ($note === null) {
88             header('HTTP/1.0 404 Not Found');
89             header('Content-type: text/plain');
90             echo "Note does not exist\n";
91             exit(1);
92         }
93
94         $data = array('note' => array($note));
95         $this->deps->renderer->sendJson($data);
96     }
97
98     /**
99      * GET|PUT /api/1.0/$user/notes
100      */
101     public function notes()
102     {
103         $username = $this->deps->urlGen->loadUsername();
104         $oauth = new OAuth();
105         $oauth->setDeps($this->deps);
106         $oauth->verifyOAuthUser($username, $this->deps->urlGen->notes($username));
107
108         $syncdata = $this->deps->notes->loadSyncData($username);
109
110         $this->handleNoteSave($username, $syncdata);
111
112         $since = null;
113         if (isset($_GET['since'])) {
114             $since = (int) $_GET['since'];
115         }
116
117         if (isset($_GET['include_notes']) && $_GET['include_notes']) {
118             $notes = $this->deps->notes->loadNotesFull($username, $since);
119         } else {
120             $notes = $this->deps->notes->loadNotesOverview($username, $since);
121         }
122
123         //work around bug https://bugzilla.gnome.org/show_bug.cgi?id=734313
124         foreach ($notes as $note) {
125             if (isset($note->{'note-content-version'})) {
126                 $note->{'note-content-version'} = 0.3;
127             }
128         }
129
130         $data = array(
131             'latest-sync-revision' => $syncdata->latestSyncRevision,
132             'notes' => $notes,
133         );
134         $this->deps->renderer->sendJson($data);
135     }
136
137     protected function handleNoteSave($username, $syncdata)
138     {
139         if ($_SERVER['REQUEST_METHOD'] != 'PUT') {
140             return;
141         }
142
143         $data = file_get_contents('php://input');
144         $putObj = json_decode($data);
145         if ($putObj === NULL) {
146             errorOut('Invalid JSON data in PUT request');
147         }
148
149         //structural validation
150         if (!isset($putObj->{'latest-sync-revision'})) {
151             errorOut('Missing "latest-sync-revision"');
152         }
153         if (!isset($putObj->{'note-changes'})) {
154             errorOut('Missing "note-changes"');
155         }
156         foreach ($putObj->{'note-changes'} as $note) {
157             if (!isset($note->guid) || $note->guid == '') {
158                 errorOut('Missing "guid" on note');
159             }
160         }
161
162         //content validation
163         if ($putObj->{'latest-sync-revision'} != $syncdata->latestSyncRevision +1
164             && $syncdata->latestSyncRevision != -1
165         ) {
166             errorOut('Wrong "latest-sync-revision". You are not up to date.');
167         }
168
169         //update
170         ++$syncdata->latestSyncRevision;
171         foreach ($putObj->{'note-changes'} as $noteUpdate) {
172             $note = $this->deps->notes->load($username, $noteUpdate->guid);
173             if (isset($noteUpdate->command) && $noteUpdate->command == 'delete') {
174                 $this->deps->notes->delete($username, $noteUpdate->guid);
175             } else {
176                 $this->deps->notes->update(
177                     $note, $noteUpdate, $syncdata->latestSyncRevision
178                 );
179                 $this->deps->notes->save($username, $note);
180             }
181         }
182
183         $this->deps->notes->saveSyncData($username, $syncdata);
184     }
185
186     /**
187      * GET /api/1.0/$user
188      */
189     public function user()
190     {
191         $username = $this->deps->urlGen->loadUsername();
192
193         $oauth = new OAuth();
194         $oauth->setDeps($this->deps);
195         $oauth->verifyOAuthUser($username, $this->deps->urlGen->user($username));
196
197         $syncdata = $this->deps->notes->loadSyncData($username);
198
199         $data = array(
200             'user-name'  => $username,
201             'first-name' => null,
202             'last-name'  => null,
203             'notes-ref'  => array(
204                 'api-ref' => $this->deps->urlGen->notes($username),
205                 'href'    => null,
206             ),
207             'latest-sync-revision' => $syncdata->latestSyncRevision,
208             'current-sync-guid'    => $syncdata->currentSyncGuid,
209         );
210         $this->deps->renderer->sendJson($data);
211     }
212 }
213 ?>