standlone HTML output
[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\TokenStorage;
20 use \OCA\Grauphel\Lib\Response\ErrorResponse;
21
22 /**
23  * Owncloud frontend: Notes
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 NotesController 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)
42     {
43         parent::__construct($appName, $request);
44         $this->user   = $user;
45
46         //default http header: we assume something is broken
47         header('HTTP/1.0 500 Internal Server Error');
48     }
49
50     /**
51      * Output a note as a standalone HTML file
52      *
53      * @NoAdminRequired
54      * @NoCSRFRequired
55      */
56     public function html($guid)
57     {
58         $note = $this->getNotes()->load($guid, false);
59         if ($note === null) {
60             $res = new ErrorResponse('Note does not exist');
61             $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
62             return $res;
63         }
64
65         $xw = new \XMLWriter();
66         $xw->openMemory();
67         $xw->setIndent(true);
68         $xw->setIndentString(' ');
69         $xw->startDocument('1.0', 'utf-8');
70         $xw->writeRaw(
71             '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
72             . ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
73             . "\n"
74         );
75
76         $xw->startElementNS(null, 'html', 'http://www.w3.org/1999/xhtml');
77
78         //head
79         $xw->startElement('head');
80         $xw->writeElement('title', $note->title);
81
82         $xw->startElement('meta');
83         $xw->writeAttribute('name', 'author');
84         $xw->writeAttribute('content', $this->user->getDisplayName());
85         $xw->endElement();
86
87         $xw->startElement('meta');
88         $xw->writeAttribute('http-equiv', 'Content-Type');
89         $xw->writeAttribute('content', 'text/html; charset=utf-8');
90         $xw->endElement();
91
92         $xw->startElement('link');
93         $xw->writeAttribute('rel', 'schema.DC');
94         $xw->writeAttribute('href', 'http://purl.org/dc/elements/1.1/');
95         $xw->endElement();
96
97         $xw->startElement('meta');
98         $xw->writeAttribute('name', 'DC.date.created');
99         $xw->writeAttribute(
100             'content', date('c', strtotime($note->{'create-date'}))
101         );
102         $xw->endElement();
103
104         $xw->startElement('meta');
105         $xw->writeAttribute('name', 'DC.date.modified');
106         $xw->writeAttribute(
107             'content', date('c', strtotime($note->{'last-change-date'}))
108         );
109         $xw->endElement();
110
111         $xw->endElement();//head
112
113         //body
114         $xw->startElement('body');
115
116         $xw->writeElement('h1', $note->title);
117
118         $converter = new \OCA\Grauphel\Converter\CleanHtml();
119         $converter->internalLinkHandler = array($this, 'htmlNoteLinkHandler');
120         try {
121             $xw->writeRaw(
122                 $converter->convert($note->{'note-content'})
123             );
124         } catch (\OCA\Grauphel\Converter\Exception $e) {
125             $res = new ErrorResponse(
126                 'Error converting note to HTML.'
127                 . ' Please repport a bug to the grauphel developers.'
128             );
129             $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
130             return $res;
131         }
132
133         $xw->endElement();//body
134
135         $xw->endElement();//html
136         return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
137     }
138
139     public function htmlNoteLinkHandler($noteTitle)
140     {
141         return urlencode($noteTitle) . '.html';
142     }
143
144     /**
145      * Output a note in tomboy XML format
146      *
147      * @link https://wiki.gnome.org/Apps/Tomboy/NoteXmlFormat
148      *
149      * @NoAdminRequired
150      * @NoCSRFRequired
151      */
152     public function xml($guid)
153     {
154         $note = $this->getNotes()->load($guid, false);
155         if ($note === null) {
156             $res = new ErrorResponse('Note does not exist');
157             $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
158             return $res;
159         }
160
161         $xw = new \XMLWriter();
162         $xw->openMemory();
163         $xw->startDocument('1.0', 'utf-8');
164
165         $xw->startElementNS(null, 'note', 'http://beatniksoftware.com/tomboy');
166         $xw->writeAttribute('version', '0.3');
167         $xw->writeAttribute('xmlns:link', 'http://beatniksoftware.com/tomboy/link');
168         $xw->writeAttribute('xmlns:size', 'http://beatniksoftware.com/tomboy/size');
169
170         $xw->writeElement('title', $note->title);
171         $xw->startElement('text');
172         $xw->writeAttribute('xml:space', 'preserve');
173
174         $xw->startElement('note-content');
175         $xw->writeAttribute('version', $note->{'note-content-version'});
176         $xw->writeRaw($note->{'note-content'});
177         $xw->endElement();//note-content
178         $xw->endElement();//text
179
180         $xw->writeElement('last-change-date', $note->{'last-change-date'});
181         $xw->writeElement('last-metadata-change-date', $note->{'last-metadata-change-date'});
182         $xw->writeElement('create-date', $note->{'create-date'});
183         $xw->writeElement('cursor-position', 0);
184         $xw->writeElement('width', 450);
185         $xw->writeElement('height', 360);
186         $xw->writeElement('x', 0);
187         $xw->writeElement('y', 0);
188         $xw->writeElement('open-on-startup', $note->{'open-on-startup'});
189
190         $xw->endElement();//note
191
192         return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
193     }
194
195     protected function getNotes()
196     {
197         $username = $this->user->getUid();
198         $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
199         $notes->setUsername($username);
200         return $notes;
201     }
202 }
203 ?>