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\TokenStorage;
20 use \OCA\Grauphel\Lib\Response\ErrorResponse;
23 * Owncloud frontend: Notes
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
33 class NotesController extends Controller
36 * constructor of the controller
38 * @param string $appName Name of the app
39 * @param IRequest $request Instance of the request
41 public function __construct($appName, \OCP\IRequest $request, $user)
43 parent::__construct($appName, $request);
46 //default http header: we assume something is broken
47 header('HTTP/1.0 500 Internal Server Error');
51 * Output a note as a standalone HTML file
56 public function html($guid)
58 $note = $this->getNotes()->load($guid, false);
60 $res = new ErrorResponse('Note does not exist');
61 $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
65 $xw = new \XMLWriter();
68 $xw->setIndentString(' ');
69 $xw->startDocument('1.0', 'utf-8');
71 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
72 . ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
76 $xw->startElementNS(null, 'html', 'http://www.w3.org/1999/xhtml');
79 $xw->startElement('head');
82 htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5)
85 $xw->startElement('meta');
86 $xw->writeAttribute('name', 'author');
87 $xw->writeAttribute('content', $this->user->getDisplayName());
90 $xw->startElement('meta');
91 $xw->writeAttribute('http-equiv', 'Content-Type');
92 $xw->writeAttribute('content', 'text/html; charset=utf-8');
95 $xw->startElement('link');
96 $xw->writeAttribute('rel', 'schema.DC');
97 $xw->writeAttribute('href', 'http://purl.org/dc/elements/1.1/');
100 $xw->startElement('meta');
101 $xw->writeAttribute('name', 'DC.date.created');
103 'content', date('c', strtotime($note->{'create-date'}))
107 $xw->startElement('meta');
108 $xw->writeAttribute('name', 'DC.date.modified');
110 'content', date('c', strtotime($note->{'last-change-date'}))
114 $xw->endElement();//head
117 $xw->startElement('body');
119 'h1', htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5)
122 $converter = new \OCA\Grauphel\Converter\CleanHtml();
123 $converter->internalLinkHandler = array($this, 'htmlNoteLinkHandler');
126 $converter->convert($note->{'note-content'})
128 } catch (\OCA\Grauphel\Converter\Exception $e) {
129 $res = new ErrorResponse(
130 'Error converting note to HTML.'
131 . ' Please report a bug to the grauphel developers.'
133 $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
137 $xw->endElement();//body
139 $xw->endElement();//html
140 return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
143 public function htmlNoteLinkHandler($noteTitle)
145 return urlencode($noteTitle) . '.html';
149 * Output a note as a standalone text file
154 public function text($guid)
156 $note = $this->getNotes()->load($guid, false);
157 if ($note === null) {
158 $res = new ErrorResponse('Note does not exist');
159 $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
163 $converter = new \OCA\Grauphel\Converter\ReStructuredText();
164 $converter->internalLinkHandler = array($this, 'textNoteLinkHandler');
166 $title = htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5);
167 $text = $title . "\n"
168 . str_repeat('*', strlen($title)) . "\n"
170 $text .= $converter->convert($note->{'note-content'});
171 return new \OCA\Grauphel\Response\TextResponse($text);
172 } catch (\OCA\Grauphel\Converter\Exception $e) {
173 $res = new ErrorResponse(
174 'Error converting note to reStructuredText.'
175 . ' Please report a bug to the grauphel developers.'
177 $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
182 public function textNoteLinkHandler($noteTitle)
188 * Output a note in tomboy XML format
190 * @link https://wiki.gnome.org/Apps/Tomboy/NoteXmlFormat
195 public function xml($guid)
197 $note = $this->getNotes()->load($guid, false);
198 if ($note === null) {
199 $res = new ErrorResponse('Note does not exist');
200 $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
204 $xw = new \XMLWriter();
206 $xw->startDocument('1.0', 'utf-8');
208 $xw->startElementNS(null, 'note', 'http://beatniksoftware.com/tomboy');
209 $xw->writeAttribute('version', '0.3');
210 $xw->writeAttribute('xmlns:link', 'http://beatniksoftware.com/tomboy/link');
211 $xw->writeAttribute('xmlns:size', 'http://beatniksoftware.com/tomboy/size');
213 $xw->writeElement('title', $note->title);
214 $xw->startElement('text');
215 $xw->writeAttribute('xml:space', 'preserve');
217 $xw->startElement('note-content');
218 $xw->writeAttribute('version', $note->{'note-content-version'});
219 $xw->writeRaw($note->{'note-content'});
220 $xw->endElement();//note-content
221 $xw->endElement();//text
223 $xw->writeElement('last-change-date', $note->{'last-change-date'});
224 $xw->writeElement('last-metadata-change-date', $note->{'last-metadata-change-date'});
225 $xw->writeElement('create-date', $note->{'create-date'});
226 $xw->writeElement('cursor-position', 0);
227 $xw->writeElement('width', 450);
228 $xw->writeElement('height', 360);
229 $xw->writeElement('x', 0);
230 $xw->writeElement('y', 0);
231 $xw->writeElement('open-on-startup', $note->{'open-on-startup'});
233 $xw->endElement();//note
235 return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
238 protected function getNotes()
240 $username = $this->user->getUid();
241 $notes = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
242 $notes->setUsername($username);