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
14 namespace OCA\Grauphel\Controller;
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;
24 * Owncloud frontend: Notes
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
34 class NotesController extends Controller
37 * constructor of the controller
39 * @param string $appName Name of the app
40 * @param IRequest $request Instance of the request
42 public function __construct($appName, \OCP\IRequest $request, $user)
44 parent::__construct($appName, $request);
46 $this->deps = Dependencies::get();
48 //default http header: we assume something is broken
49 header('HTTP/1.0 500 Internal Server Error');
53 * Output a note as a standalone HTML file
58 public function html($guid)
60 $note = $this->getNotes()->load($guid, false);
62 $res = new ErrorResponse('Note does not exist');
63 $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
67 $xw = new \XMLWriter();
70 $xw->setIndentString(' ');
71 $xw->startDocument('1.0', 'utf-8');
73 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
74 . ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
78 $xw->startElementNS(null, 'html', 'http://www.w3.org/1999/xhtml');
81 $xw->startElement('head');
84 htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5)
87 $xw->startElement('meta');
88 $xw->writeAttribute('name', 'author');
89 $xw->writeAttribute('content', $this->user->getDisplayName());
92 $xw->startElement('meta');
93 $xw->writeAttribute('http-equiv', 'Content-Type');
94 $xw->writeAttribute('content', 'text/html; charset=utf-8');
97 $xw->startElement('link');
98 $xw->writeAttribute('rel', 'schema.DC');
99 $xw->writeAttribute('href', 'http://purl.org/dc/elements/1.1/');
102 $xw->startElement('meta');
103 $xw->writeAttribute('name', 'DC.date.created');
105 'content', date('c', strtotime($note->{'create-date'}))
109 $xw->startElement('meta');
110 $xw->writeAttribute('name', 'DC.date.modified');
112 'content', date('c', strtotime($note->{'last-change-date'}))
116 $xw->endElement();//head
119 $xw->startElement('body');
121 'h1', htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5)
124 $converter = new \OCA\Grauphel\Converter\CleanHtml();
125 $converter->internalLinkHandler = array($this, 'htmlNoteLinkHandler');
128 $converter->convert($note->{'note-content'})
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.'
135 $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
139 $xw->endElement();//body
141 $xw->endElement();//html
142 return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
145 public function htmlNoteLinkHandler($noteTitle)
147 return urlencode($noteTitle) . '.html';
151 * Output a note as a standalone text file
156 public function text($guid)
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);
165 $converter = new \OCA\Grauphel\Converter\ReStructuredText();
166 $converter->internalLinkHandler = array($this, 'textNoteLinkHandler');
168 $title = htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5);
169 $text = $title . "\n"
170 . str_repeat('*', strlen($title)) . "\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.'
179 $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
184 public function textNoteLinkHandler($noteTitle)
190 * Output a note in tomboy XML format
192 * @link https://wiki.gnome.org/Apps/Tomboy/NoteXmlFormat
197 public function xml($guid)
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);
206 $xw = new \XMLWriter();
208 $xw->startDocument('1.0', 'utf-8');
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');
215 $xw->writeElement('title', $note->title);
216 $xw->startElement('text');
217 $xw->writeAttribute('xml:space', 'preserve');
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
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'});
235 $xw->endElement();//note
237 return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
240 protected function getNotes()
242 $username = $this->user->getUid();
243 $notes = new \OCA\Grauphel\Lib\NoteStorage($this->deps->urlGen);
244 $notes->setUsername($username);