Link to maintainership/funding post
[grauphel.git] / controller / notescontroller.php
index 893a1006f27dc3973d47818b08daa77eedc10fc3..b81e1627424f4e6d2e2f08b12bb996a54fa3b112 100644 (file)
@@ -16,6 +16,7 @@ namespace OCA\Grauphel\Controller;
 use \OCP\AppFramework\Controller;
 use \OCP\AppFramework\Http\TemplateResponse;
 use \OCA\Grauphel\Lib\Client;
+use \OCA\Grauphel\Lib\Dependencies;
 use \OCA\Grauphel\Lib\TokenStorage;
 use \OCA\Grauphel\Lib\Response\ErrorResponse;
 
@@ -41,7 +42,8 @@ class NotesController extends Controller
     public function __construct($appName, \OCP\IRequest $request, $user)
     {
         parent::__construct($appName, $request);
-        $this->user   = $user;
+        $this->user = $user;
+        $this->deps = Dependencies::get();
 
         //default http header: we assume something is broken
         header('HTTP/1.0 500 Internal Server Error');
@@ -77,7 +79,10 @@ class NotesController extends Controller
 
         //head
         $xw->startElement('head');
-        $xw->writeElement('title', $note->title);
+        $xw->writeElement(
+            'title',
+            htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5)
+        );
 
         $xw->startElement('meta');
         $xw->writeAttribute('name', 'author');
@@ -112,8 +117,9 @@ class NotesController extends Controller
 
         //body
         $xw->startElement('body');
-
-        $xw->writeElement('h1', $note->title);
+        $xw->writeElement(
+            'h1', htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5)
+        );
 
         $converter = new \OCA\Grauphel\Converter\CleanHtml();
         $converter->internalLinkHandler = array($this, 'htmlNoteLinkHandler');
@@ -124,7 +130,7 @@ class NotesController extends Controller
         } catch (\OCA\Grauphel\Converter\Exception $e) {
             $res = new ErrorResponse(
                 'Error converting note to HTML.'
-                . ' Please repport a bug to the grauphel developers.'
+                . ' Please report a bug to the grauphel developers.'
             );
             $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
             return $res;
@@ -141,6 +147,45 @@ class NotesController extends Controller
         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
      *
@@ -195,7 +240,7 @@ class NotesController extends Controller
     protected function getNotes()
     {
         $username = $this->user->getUid();
-        $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
+        $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->deps->urlGen);
         $notes->setUsername($username);
         return $notes;
     }