6fffcb2f7377db128d076d395e94d10156661c5b
[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         $res = new TemplateResponse('grauphel', 'tag');
161         $res->setParams(
162             array(
163                 'tag'    => $this->getPrettyTagName($rawtag),
164                 'rawtag' => $rawtag,
165                 'notes'  => $notes,
166             )
167         );
168         $this->addNavigation($res, $rawtag);
169
170         return $res;
171     }
172
173     /**
174      * Show access tokens
175      *
176      * @NoAdminRequired
177      * @NoCSRFRequired
178      */
179     public function tokens()
180     {
181         $tokens = new TokenStorage();
182         $res = new TemplateResponse('grauphel', 'tokens');
183         $res->setParams(
184             array(
185                 'tokens' => $tokens->loadForUser(
186                     $this->user->getUid(), 'access'
187                 ),
188                 'client' => new Client(),
189                 'username' => $this->user->getUid(),
190             )
191         );
192         $this->addNavigation($res, null);
193
194         return $res;
195     }
196
197     /**
198      * Allow the user to clear his database
199      *
200      * @NoAdminRequired
201      * @NoCSRFRequired
202      */
203     public function database($reset = null)
204     {
205         $res = new TemplateResponse('grauphel', 'gui-database');
206         $res->setParams(array('reset' => $reset));
207         $this->addNavigation($res, null);
208         $this->addStats($res);
209
210         return $res;
211     }
212
213     /**
214      * Resets the database by deleting all notes and deleting the user's
215      * sync data.
216      *
217      * @NoAdminRequired
218      */
219     public function databaseReset()
220     {
221         $reset = false;
222         if ($_POST['username'] != '' && $_POST['username'] == $this->user->getUid()) {
223             $notes = $this->getNotes();
224             $notes->deleteAll();
225             $notes->deleteSyncData();
226             $reset = true;
227         }
228
229         return $this->database($reset);
230     }
231
232     protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
233     {
234         $nav = new \OCP\Template('grauphel', 'appnavigation', '');
235         $nav->assign('apiroot', $this->getApiRootUrl());
236         $nav->assign('tags', array());
237
238         $params = $res->getParams();
239         $params['appNavigation'] = $nav;
240         $res->setParams($params);
241
242         if ($this->user === null) {
243             return;
244         }
245
246         $rawtags = $this->getNotes()->getTags();
247         sort($rawtags);
248         array_unshift(
249             $rawtags,
250             'grauphel:special:all', 'grauphel:special:untagged'
251         );
252
253         $tags = array();
254         foreach ($rawtags as $rawtag) {
255             $name = $this->getPrettyTagName($rawtag);
256             if ($name !== false) {
257                 $tags[] = array(
258                     'name' => $name,
259                     'id'   => $rawtag,
260                     'href' => $this->urlGen->linkToRoute(
261                         'grauphel.gui.tag', array('rawtag' => $rawtag)
262                     ),
263                     'selected' => $rawtag == $selectedRawtag,
264                 );
265             }
266         }
267         $nav->assign('tags', $tags);
268     }
269
270     protected function addStats(TemplateResponse $res)
271     {
272         if ($this->user === null) {
273             return;
274         }
275
276         $username = $this->user->getUid();
277         $notes  = $this->getNotes();
278         $tokens = new \OCA\Grauphel\Lib\TokenStorage();
279
280         $nav = new \OCP\Template('grauphel', 'indexStats', '');
281         $nav->assign('notes', count($notes->loadNotesOverview()));
282         $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
283         $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
284
285         $params = $res->getParams();
286         $params['stats'] = $nav;
287         $res->setParams($params);
288     }
289
290     protected function checkDeps()
291     {
292         if (!class_exists('OAuthProvider')) {
293             throw new \Exception('PHP extension "oauth" is required');
294         }
295     }
296
297     protected function getApiRootUrl()
298     {
299         //we need to remove the trailing / for tomdroid and conboy
300         return rtrim(
301             $this->urlGen->getAbsoluteURL(
302                 $this->urlGen->linkToRoute('grauphel.gui.index')
303             ),
304             '/'
305         );
306     }
307
308     protected function getNotes()
309     {
310         $username = $this->user->getUid();
311         $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
312         $notes->setUsername($username);
313         return $notes;
314     }
315
316     protected function getPrettyTagName($rawtag)
317     {
318         if (substr($rawtag, 0, 16) == 'system:notebook:') {
319             return substr($rawtag, 16);
320         } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
321             return '*' . substr($rawtag, 17) . '*';
322         }
323         return false;
324     }
325 }
326 ?>