do not error out on basic auth
[grauphel.git] / controller / guicontroller.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
16 use \OCP\AppFramework\Controller;
17 use \OCP\AppFramework\Http\TemplateResponse;
18 use \OCA\Grauphel\Lib\Client;
19 use \OCA\Grauphel\Lib\TokenStorage;
20 use \OCA\Grauphel\Lib\Response\ErrorResponse;
21
22 /**
23  * Owncloud frontend
24  *
25  * @category  Tools
26  * @package   Grauphel
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
32  */
33 class GuiController extends Controller
34 {
35     /**
36      * constructor of the controller
37      *
38      * @param string   $appName Name of the app
39      * @param IRequest $request Instance of the request
40      */
41     public function __construct($appName, \OCP\IRequest $request, $user, $urlGen)
42     {
43         parent::__construct($appName, $request);
44         $this->user   = $user;
45         $this->urlGen = $urlGen;
46
47         //default http header: we assume something is broken
48         header('HTTP/1.0 500 Internal Server Error');
49     }
50
51     /**
52      * Main page /
53      *
54      * Tomdroid wants this to be a public page. Sync fails otherwise.
55      *
56      * @NoAdminRequired
57      * @NoCSRFRequired
58      * @PublicPage
59      */
60     public function index()
61     {
62         $this->checkDeps();
63
64         $res = new TemplateResponse('grauphel', 'index');
65         $res->setParams(
66             array(
67                 'apiroot' => $this->getApiRootUrl(),
68                 'apiurl'  => $this->urlGen->linkToRoute('grauphel.api.index')
69             )
70         );
71         $this->addNavigation($res);
72         $this->addStats($res);
73         return $res;
74     }
75
76     /**
77      * Show contents of a note
78      *
79      * @NoAdminRequired
80      * @NoCSRFRequired
81      */
82     public function note($guid)
83     {
84         $res = new TemplateResponse('grauphel', 'gui-note');
85
86         $note = $this->getNotes()->load($guid, false);
87         if ($note === null) {
88             return new ErrorResponse('Note does not exist');
89         }
90
91         $converter = new \OCA\Grauphel\Converter\Html();
92         $converter->internalLinkHandler = array($this, 'noteLinkHandler');
93
94         try {
95             $contentHtml = $converter->convert($note->{'note-content'});
96         } catch (\OCA\Grauphel\Converter\Exception $e) {
97             $contentHtml = '<div class="error">'
98                 . '<p>There was an error converting the note to HTML:</p>'
99                 . '<blockquote><tt>' . htmlspecialchars($e->getMessage()) . '</tt></blockquote>'
100                 . '<p>Please open a bug report at'
101                 . ' <a class="lined" href="http://github.com/cweiske/grauphel/issues">'
102                 . 'github.com/cweiske/grauphel/issues</a>'
103                 . ' and attach the XML version of the note.'
104                 . '</div>';
105         }
106
107         $res->setParams(
108             array(
109                 'note' => $note,
110                 'note-content' => $contentHtml,
111                 'links' => array(
112                     'json' => $this->urlGen->linkToRoute(
113                         'grauphel.api.note', array(
114                             'guid' => $guid, 'username' => $this->user->getUid()
115                         )
116                     ),
117                     'xml' => $this->urlGen->linkToRoute(
118                         'grauphel.notes.xml', array('guid' => $guid)
119                     ),
120                 )
121             )
122         );
123
124         $selectedRawtag = 'grauphel:special:untagged';
125         if (count($note->tags) > 0) {
126             $selectedRawtag = $note->tags[0];
127         }
128
129         $this->addNavigation($res, $selectedRawtag);
130         return $res;
131     }
132
133     public function noteLinkHandler($noteTitle)
134     {
135         $guid = $this->getNotes()->loadGuidByTitle($noteTitle);
136         if ($guid === null) {
137             return '#';
138         }
139         return $this->urlGen->linkToRoute(
140             'grauphel.gui.note', array('guid' => $guid)
141         );
142     }
143
144     /**
145      * Show all notes of a tag
146      *
147      * @NoAdminRequired
148      * @NoCSRFRequired
149      */
150     public function tag($rawtag)
151     {
152         $notes = $this->getNotes()->loadNotesOverview(null, $rawtag, true);
153         usort(
154             $notes,
155             function($noteA, $noteB) {
156                 return strcmp($noteA['title'], $noteB['title']);
157             }
158         );
159
160         foreach ($notes as &$note) {
161             $diffInDays = intval(
162                 (time() - strtotime($note['last-change-date'])) / 86400
163             );
164             $value = 0 + $diffInDays;
165             if ($value > 160) {
166                 $value = 160;
167             }
168             $note['dateColor'] = '#' . str_repeat(sprintf('%02X', $value), 3);
169         }
170
171         $res = new TemplateResponse('grauphel', 'tag');
172         $res->setParams(
173             array(
174                 'tag'    => $this->getPrettyTagName($rawtag),
175                 'rawtag' => $rawtag,
176                 'notes'  => $notes,
177             )
178         );
179         $this->addNavigation($res, $rawtag);
180
181         return $res;
182     }
183
184     /**
185      * Show access tokens
186      *
187      * @NoAdminRequired
188      * @NoCSRFRequired
189      */
190     public function tokens()
191     {
192         $tokens = new TokenStorage();
193         $res = new TemplateResponse('grauphel', 'tokens');
194         $res->setParams(
195             array(
196                 'tokens' => $tokens->loadForUser(
197                     $this->user->getUid(), 'access'
198                 ),
199                 'client' => new Client(),
200                 'username' => $this->user->getUid(),
201             )
202         );
203         $this->addNavigation($res, null);
204
205         return $res;
206     }
207
208     /**
209      * Allow the user to clear his database
210      *
211      * @NoAdminRequired
212      * @NoCSRFRequired
213      */
214     public function database($reset = null)
215     {
216         $res = new TemplateResponse('grauphel', 'gui-database');
217         $res->setParams(array('reset' => $reset));
218         $this->addNavigation($res, null);
219         $this->addStats($res);
220
221         return $res;
222     }
223
224     /**
225      * Resets the database by deleting all notes and deleting the user's
226      * sync data.
227      *
228      * @NoAdminRequired
229      */
230     public function databaseReset()
231     {
232         $reset = false;
233         if ($_POST['username'] != '' && $_POST['username'] == $this->user->getUid()) {
234             $notes = $this->getNotes();
235             $notes->deleteAll();
236             $notes->deleteSyncData();
237             $reset = true;
238         }
239
240         return $this->database($reset);
241     }
242
243     protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
244     {
245         $nav = new \OCP\Template('grauphel', 'appnavigation', '');
246         $nav->assign('apiroot', $this->getApiRootUrl());
247         $nav->assign('tags', array());
248
249         $params = $res->getParams();
250         $params['appNavigation'] = $nav;
251         $res->setParams($params);
252
253         if ($this->user === null) {
254             return;
255         }
256
257         $rawtags = $this->getNotes()->getTags();
258         sort($rawtags);
259         array_unshift(
260             $rawtags,
261             'grauphel:special:all', 'grauphel:special:untagged'
262         );
263
264         $tags = array();
265         foreach ($rawtags as $rawtag) {
266             $name = $this->getPrettyTagName($rawtag);
267             if ($name !== false) {
268                 $tags[] = array(
269                     'name' => $name,
270                     'id'   => $rawtag,
271                     'href' => $this->urlGen->linkToRoute(
272                         'grauphel.gui.tag', array('rawtag' => $rawtag)
273                     ),
274                     'selected' => $rawtag == $selectedRawtag,
275                 );
276             }
277         }
278         $nav->assign('tags', $tags);
279     }
280
281     protected function addStats(TemplateResponse $res)
282     {
283         if ($this->user === null) {
284             return;
285         }
286
287         $username = $this->user->getUid();
288         $notes  = $this->getNotes();
289         $tokens = new \OCA\Grauphel\Lib\TokenStorage();
290
291         $nav = new \OCP\Template('grauphel', 'indexStats', '');
292         $nav->assign('notes', count($notes->loadNotesOverview()));
293         $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
294         $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
295
296         $params = $res->getParams();
297         $params['stats'] = $nav;
298         $res->setParams($params);
299     }
300
301     protected function checkDeps()
302     {
303         if (!class_exists('OAuthProvider')) {
304             throw new \Exception('PHP extension "oauth" is required');
305         }
306     }
307
308     protected function getApiRootUrl()
309     {
310         //we need to remove the trailing / for tomdroid and conboy
311         return rtrim(
312             $this->urlGen->getAbsoluteURL(
313                 $this->urlGen->linkToRoute('grauphel.gui.index')
314             ),
315             '/'
316         );
317     }
318
319     protected function getNotes()
320     {
321         $username = $this->user->getUid();
322         $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
323         $notes->setUsername($username);
324         return $notes;
325     }
326
327     protected function getPrettyTagName($rawtag)
328     {
329         if (substr($rawtag, 0, 16) == 'system:notebook:') {
330             return substr($rawtag, 16);
331         } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
332             return '*' . substr($rawtag, 17) . '*';
333         }
334         return false;
335     }
336 }
337 ?>