Try to fix issue #10; work around bug https://bugs.php.net/bug.php?id=68168
[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
21 /**
22  * Owncloud frontend
23  *
24  * @category  Tools
25  * @package   Grauphel
26  * @author    Christian Weiske <cweiske@cweiske.de>
27  * @copyright 2014 Christian Weiske
28  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
29  * @version   Release: @package_version@
30  * @link      http://cweiske.de/grauphel.htm
31  */
32 class GuiController extends Controller
33 {
34     /**
35      * constructor of the controller
36      *
37      * @param string   $appName Name of the app
38      * @param IRequest $request Instance of the request
39      */
40     public function __construct($appName, \OCP\IRequest $request, $user, $urlGen)
41     {
42         parent::__construct($appName, $request);
43         $this->user   = $user;
44         $this->urlGen = $urlGen;
45
46         //default http header: we assume something is broken
47         header('HTTP/1.0 500 Internal Server Error');
48     }
49
50     /**
51      * Main page /
52      *
53      * Tomdroid wants this to be a public page. Sync fails otherwise.
54      *
55      * @NoAdminRequired
56      * @NoCSRFRequired
57      * @PublicPage
58      */
59     public function index()
60     {
61         $this->checkDeps();
62
63         $res = new TemplateResponse('grauphel', 'index');
64         $res->setParams(
65             array(
66                 'apiroot' => $this->getApiRootUrl(),
67                 'apiurl'  => $this->urlGen->linkToRoute('grauphel.api.index')
68             )
69         );
70         $this->addNavigation($res);
71         $this->addStats($res);
72         return $res;
73     }
74
75     /**
76      * Show all notes of a tag
77      *
78      * @NoAdminRequired
79      * @NoCSRFRequired
80      */
81     public function tag($rawtag)
82     {
83         $notes = $this->getNotes()->loadNotesOverview(null, $rawtag);
84
85         $res = new TemplateResponse('grauphel', 'tag');
86         $res->setParams(
87             array(
88                 'tag'    => $this->getPrettyTagName($rawtag),
89                 'rawtag' => $rawtag,
90                 'notes'  => $notes,
91             )
92         );
93         $this->addNavigation($res, $rawtag);
94
95         return $res;
96     }
97
98     /**
99      * Show access tokens
100      *
101      * @NoAdminRequired
102      * @NoCSRFRequired
103      */
104     public function tokens()
105     {
106         $tokens = new TokenStorage();
107         $res = new TemplateResponse('grauphel', 'tokens');
108         $res->setParams(
109             array(
110                 'tokens' => $tokens->loadForUser(
111                     $this->user->getUid(), 'access'
112                 ),
113                 'client' => new Client(),
114             )
115         );
116         $this->addNavigation($res, null);
117
118         return $res;
119     }
120
121     protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
122     {
123         $nav = new \OCP\Template('grauphel', 'appnavigation', '');
124         $nav->assign('apiroot', $this->getApiRootUrl());
125         $nav->assign('tags', array());
126
127         $params = $res->getParams();
128         $params['appNavigation'] = $nav;
129         $res->setParams($params);
130
131         if ($this->user === null) {
132             return;
133         }
134
135         $rawtags = $this->getNotes()->getTags();
136         sort($rawtags);
137         array_unshift(
138             $rawtags,
139             'grauphel:special:all', 'grauphel:special:untagged'
140         );
141
142         $tags = array();
143         foreach ($rawtags as $rawtag) {
144             $name = $this->getPrettyTagName($rawtag);
145             if ($name !== false) {
146                 $tags[] = array(
147                     'name' => $name,
148                     'id'   => $rawtag,
149                     'href' => $this->urlGen->linkToRoute(
150                         'grauphel.gui.tag', array('rawtag' => $rawtag)
151                     ),
152                 );
153             }
154         }
155         $nav->assign('tags', $tags);
156     }
157
158     protected function addStats(TemplateResponse $res)
159     {
160         if ($this->user === null) {
161             return;
162         }
163
164         $username = $this->user->getUid();
165         $notes  = $this->getNotes();
166         $tokens = new \OCA\Grauphel\Lib\TokenStorage();
167
168         $nav = new \OCP\Template('grauphel', 'indexStats', '');
169         $nav->assign('notes', count($notes->loadNotesOverview()));
170         $nav->assign('syncrev', $notes->loadSyncData()->latestSyncRevision);
171         $nav->assign('tokens', count($tokens->loadForUser($username, 'access')));
172
173         $params = $res->getParams();
174         $params['stats'] = $nav;
175         $res->setParams($params);
176     }
177
178     protected function checkDeps()
179     {
180         if (!class_exists('OAuthProvider')) {
181             throw new \Exception('PHP extension "oauth" is required');
182         }
183     }
184
185     protected function getApiRootUrl()
186     {
187         //we need to remove the trailing / for tomdroid and conboy
188         return rtrim(
189             $this->urlGen->getAbsoluteURL(
190                 $this->urlGen->linkToRoute('grauphel.gui.index')
191             ),
192             '/'
193         );
194     }
195
196     protected function getNotes()
197     {
198         $username = $this->user->getUid();
199         $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
200         $notes->setUsername($username);
201         return $notes;
202     }
203
204     protected function getPrettyTagName($rawtag)
205     {
206         if (substr($rawtag, 0, 16) == 'system:notebook:') {
207             return substr($rawtag, 16);
208         } else if (substr($rawtag, 0, 17) == 'grauphel:special:') {
209             return '*' . substr($rawtag, 17) . '*';
210         }
211         return false;
212     }
213 }
214 ?>