aboutsummaryrefslogtreecommitdiff
path: root/controller/notescontroller.php
blob: a54cfe31f1c868eefe2c2184fb1856236d0d3942 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
 * Part of grauphel
 *
 * PHP version 5
 *
 * @category  Tools
 * @package   Grauphel
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @link      http://cweiske.de/grauphel.htm
 */
namespace OCA\Grauphel\Controller;

use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\TemplateResponse;
use \OCA\Grauphel\Lib\Client;
use \OCA\Grauphel\Lib\TokenStorage;
use \OCA\Grauphel\Lib\Response\ErrorResponse;

/**
 * Owncloud frontend: Notes
 *
 * @category  Tools
 * @package   Grauphel
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @version   Release: @package_version@
 * @link      http://cweiske.de/grauphel.htm
 */
class NotesController extends Controller
{
    /**
     * constructor of the controller
     *
     * @param string   $appName Name of the app
     * @param IRequest $request Instance of the request
     */
    public function __construct($appName, \OCP\IRequest $request, $user)
    {
        parent::__construct($appName, $request);
        $this->user   = $user;

        //default http header: we assume something is broken
        header('HTTP/1.0 500 Internal Server Error');
    }

    /**
     * Output a note as a standalone HTML file
     *
     * @NoAdminRequired
     * @NoCSRFRequired
     */
    public function html($guid)
    {
        $note = $this->getNotes()->load($guid, false);
        if ($note === null) {
            $res = new ErrorResponse('Note does not exist');
            $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
            return $res;
        }

        $xw = new \XMLWriter();
        $xw->openMemory();
        $xw->setIndent(true);
        $xw->setIndentString(' ');
        $xw->startDocument('1.0', 'utf-8');
        $xw->writeRaw(
            '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
            . ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
            . "\n"
        );

        $xw->startElementNS(null, 'html', 'http://www.w3.org/1999/xhtml');

        //head
        $xw->startElement('head');
        $xw->writeElement(
            'title',
            htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5)
        );

        $xw->startElement('meta');
        $xw->writeAttribute('name', 'author');
        $xw->writeAttribute('content', $this->user->getDisplayName());
        $xw->endElement();

        $xw->startElement('meta');
        $xw->writeAttribute('http-equiv', 'Content-Type');
        $xw->writeAttribute('content', 'text/html; charset=utf-8');
        $xw->endElement();

        $xw->startElement('link');
        $xw->writeAttribute('rel', 'schema.DC');
        $xw->writeAttribute('href', 'http://purl.org/dc/elements/1.1/');
        $xw->endElement();

        $xw->startElement('meta');
        $xw->writeAttribute('name', 'DC.date.created');
        $xw->writeAttribute(
            'content', date('c', strtotime($note->{'create-date'}))
        );
        $xw->endElement();

        $xw->startElement('meta');
        $xw->writeAttribute('name', 'DC.date.modified');
        $xw->writeAttribute(
            'content', date('c', strtotime($note->{'last-change-date'}))
        );
        $xw->endElement();

        $xw->endElement();//head

        //body
        $xw->startElement('body');
        $xw->writeElement(
            'h1', htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5)
        );

        $converter = new \OCA\Grauphel\Converter\CleanHtml();
        $converter->internalLinkHandler = array($this, 'htmlNoteLinkHandler');
        try {
            $xw->writeRaw(
                $converter->convert($note->{'note-content'})
            );
        } catch (\OCA\Grauphel\Converter\Exception $e) {
            $res = new ErrorResponse(
                'Error converting note to HTML.'
                . ' Please report a bug to the grauphel developers.'
            );
            $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
            return $res;
        }

        $xw->endElement();//body

        $xw->endElement();//html
        return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
    }

    public function htmlNoteLinkHandler($noteTitle)
    {
        return urlencode($noteTitle) . '.html';
    }

    /**
     * Output a note as a standalone text file
     *
     * @NoAdminRequired
     * @NoCSRFRequired
     */
    public function text($guid)
    {
        $note = $this->getNotes()->load($guid, false);
        if ($note === null) {
            $res = new ErrorResponse('Note does not exist');
            $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
            return $res;
        }

        $converter = new \OCA\Grauphel\Converter\ReStructuredText();
        $converter->internalLinkHandler = array($this, 'textNoteLinkHandler');
        try {
            $title = htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5);
            $text = $title . "\n"
                . str_repeat('*', strlen($title)) . "\n"
                . "\n";
            $text .= $converter->convert($note->{'note-content'});
            return new \OCA\Grauphel\Response\TextResponse($text);
        } catch (\OCA\Grauphel\Converter\Exception $e) {
            $res = new ErrorResponse(
                'Error converting note to reStructuredText.'
                . ' Please report a bug to the grauphel developers.'
            );
            $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
            return $res;
        }
    }

    public function textNoteLinkHandler($noteTitle)
    {
        return $noteTitle;
    }

    /**
     * Output a note in tomboy XML format
     *
     * @link https://wiki.gnome.org/Apps/Tomboy/NoteXmlFormat
     *
     * @NoAdminRequired
     * @NoCSRFRequired
     */
    public function xml($guid)
    {
        $note = $this->getNotes()->load($guid, false);
        if ($note === null) {
            $res = new ErrorResponse('Note does not exist');
            $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
            return $res;
        }

        $xw = new \XMLWriter();
        $xw->openMemory();
        $xw->startDocument('1.0', 'utf-8');

        $xw->startElementNS(null, 'note', 'http://beatniksoftware.com/tomboy');
        $xw->writeAttribute('version', '0.3');
        $xw->writeAttribute('xmlns:link', 'http://beatniksoftware.com/tomboy/link');
        $xw->writeAttribute('xmlns:size', 'http://beatniksoftware.com/tomboy/size');

        $xw->writeElement('title', $note->title);
        $xw->startElement('text');
        $xw->writeAttribute('xml:space', 'preserve');

        $xw->startElement('note-content');
        $xw->writeAttribute('version', $note->{'note-content-version'});
        $xw->writeRaw($note->{'note-content'});
        $xw->endElement();//note-content
        $xw->endElement();//text

        $xw->writeElement('last-change-date', $note->{'last-change-date'});
        $xw->writeElement('last-metadata-change-date', $note->{'last-metadata-change-date'});
        $xw->writeElement('create-date', $note->{'create-date'});
        $xw->writeElement('cursor-position', 0);
        $xw->writeElement('width', 450);
        $xw->writeElement('height', 360);
        $xw->writeElement('x', 0);
        $xw->writeElement('y', 0);
        $xw->writeElement('open-on-startup', $note->{'open-on-startup'});

        $xw->endElement();//note

        return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
    }

    protected function getNotes()
    {
        $username = $this->user->getUid();
        $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
        $notes->setUsername($username);
        return $notes;
    }
}
?>