Link to maintainership/funding post
[grauphel.git] / controller / notescontroller.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\Dependencies;
20 use \OCA\Grauphel\Lib\TokenStorage;
21 use \OCA\Grauphel\Lib\Response\ErrorResponse;
22
23 /**
24  * Owncloud frontend: Notes
25  *
26  * @category  Tools
27  * @package   Grauphel
28  * @author    Christian Weiske <cweiske@cweiske.de>
29  * @copyright 2014 Christian Weiske
30  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
31  * @version   Release: @package_version@
32  * @link      http://cweiske.de/grauphel.htm
33  */
34 class NotesController extends Controller
35 {
36     /**
37      * constructor of the controller
38      *
39      * @param string   $appName Name of the app
40      * @param IRequest $request Instance of the request
41      */
42     public function __construct($appName, \OCP\IRequest $request, $user)
43     {
44         parent::__construct($appName, $request);
45         $this->user = $user;
46         $this->deps = Dependencies::get();
47
48         //default http header: we assume something is broken
49         header('HTTP/1.0 500 Internal Server Error');
50     }
51
52     /**
53      * Output a note as a standalone HTML file
54      *
55      * @NoAdminRequired
56      * @NoCSRFRequired
57      */
58     public function html($guid)
59     {
60         $note = $this->getNotes()->load($guid, false);
61         if ($note === null) {
62             $res = new ErrorResponse('Note does not exist');
63             $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
64             return $res;
65         }
66
67         $xw = new \XMLWriter();
68         $xw->openMemory();
69         $xw->setIndent(true);
70         $xw->setIndentString(' ');
71         $xw->startDocument('1.0', 'utf-8');
72         $xw->writeRaw(
73             '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
74             . ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
75             . "\n"
76         );
77
78         $xw->startElementNS(null, 'html', 'http://www.w3.org/1999/xhtml');
79
80         //head
81         $xw->startElement('head');
82         $xw->writeElement(
83             'title',
84             htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5)
85         );
86
87         $xw->startElement('meta');
88         $xw->writeAttribute('name', 'author');
89         $xw->writeAttribute('content', $this->user->getDisplayName());
90         $xw->endElement();
91
92         $xw->startElement('meta');
93         $xw->writeAttribute('http-equiv', 'Content-Type');
94         $xw->writeAttribute('content', 'text/html; charset=utf-8');
95         $xw->endElement();
96
97         $xw->startElement('link');
98         $xw->writeAttribute('rel', 'schema.DC');
99         $xw->writeAttribute('href', 'http://purl.org/dc/elements/1.1/');
100         $xw->endElement();
101
102         $xw->startElement('meta');
103         $xw->writeAttribute('name', 'DC.date.created');
104         $xw->writeAttribute(
105             'content', date('c', strtotime($note->{'create-date'}))
106         );
107         $xw->endElement();
108
109         $xw->startElement('meta');
110         $xw->writeAttribute('name', 'DC.date.modified');
111         $xw->writeAttribute(
112             'content', date('c', strtotime($note->{'last-change-date'}))
113         );
114         $xw->endElement();
115
116         $xw->endElement();//head
117
118         //body
119         $xw->startElement('body');
120         $xw->writeElement(
121             'h1', htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5)
122         );
123
124         $converter = new \OCA\Grauphel\Converter\CleanHtml();
125         $converter->internalLinkHandler = array($this, 'htmlNoteLinkHandler');
126         try {
127             $xw->writeRaw(
128                 $converter->convert($note->{'note-content'})
129             );
130         } catch (\OCA\Grauphel\Converter\Exception $e) {
131             $res = new ErrorResponse(
132                 'Error converting note to HTML.'
133                 . ' Please report a bug to the grauphel developers.'
134             );
135             $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
136             return $res;
137         }
138
139         $xw->endElement();//body
140
141         $xw->endElement();//html
142         return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
143     }
144
145     public function htmlNoteLinkHandler($noteTitle)
146     {
147         return urlencode($noteTitle) . '.html';
148     }
149
150     /**
151      * Output a note as a standalone text file
152      *
153      * @NoAdminRequired
154      * @NoCSRFRequired
155      */
156     public function text($guid)
157     {
158         $note = $this->getNotes()->load($guid, false);
159         if ($note === null) {
160             $res = new ErrorResponse('Note does not exist');
161             $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
162             return $res;
163         }
164
165         $converter = new \OCA\Grauphel\Converter\ReStructuredText();
166         $converter->internalLinkHandler = array($this, 'textNoteLinkHandler');
167         try {
168             $title = htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5);
169             $text = $title . "\n"
170                 . str_repeat('*', strlen($title)) . "\n"
171                 . "\n";
172             $text .= $converter->convert($note->{'note-content'});
173             return new \OCA\Grauphel\Response\TextResponse($text);
174         } catch (\OCA\Grauphel\Converter\Exception $e) {
175             $res = new ErrorResponse(
176                 'Error converting note to reStructuredText.'
177                 . ' Please report a bug to the grauphel developers.'
178             );
179             $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
180             return $res;
181         }
182     }
183
184     public function textNoteLinkHandler($noteTitle)
185     {
186         return $noteTitle;
187     }
188
189     /**
190      * Output a note in tomboy XML format
191      *
192      * @link https://wiki.gnome.org/Apps/Tomboy/NoteXmlFormat
193      *
194      * @NoAdminRequired
195      * @NoCSRFRequired
196      */
197     public function xml($guid)
198     {
199         $note = $this->getNotes()->load($guid, false);
200         if ($note === null) {
201             $res = new ErrorResponse('Note does not exist');
202             $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
203             return $res;
204         }
205
206         $xw = new \XMLWriter();
207         $xw->openMemory();
208         $xw->startDocument('1.0', 'utf-8');
209
210         $xw->startElementNS(null, 'note', 'http://beatniksoftware.com/tomboy');
211         $xw->writeAttribute('version', '0.3');
212         $xw->writeAttribute('xmlns:link', 'http://beatniksoftware.com/tomboy/link');
213         $xw->writeAttribute('xmlns:size', 'http://beatniksoftware.com/tomboy/size');
214
215         $xw->writeElement('title', $note->title);
216         $xw->startElement('text');
217         $xw->writeAttribute('xml:space', 'preserve');
218
219         $xw->startElement('note-content');
220         $xw->writeAttribute('version', $note->{'note-content-version'});
221         $xw->writeRaw($note->{'note-content'});
222         $xw->endElement();//note-content
223         $xw->endElement();//text
224
225         $xw->writeElement('last-change-date', $note->{'last-change-date'});
226         $xw->writeElement('last-metadata-change-date', $note->{'last-metadata-change-date'});
227         $xw->writeElement('create-date', $note->{'create-date'});
228         $xw->writeElement('cursor-position', 0);
229         $xw->writeElement('width', 450);
230         $xw->writeElement('height', 360);
231         $xw->writeElement('x', 0);
232         $xw->writeElement('y', 0);
233         $xw->writeElement('open-on-startup', $note->{'open-on-startup'});
234
235         $xw->endElement();//note
236
237         return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
238     }
239
240     protected function getNotes()
241     {
242         $username = $this->user->getUid();
243         $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->deps->urlGen);
244         $notes->setUsername($username);
245         return $notes;
246     }
247 }
248 ?>