Send 404 if a note cannot be found
[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 in tomboy XML format
52      *
53      * @link https://wiki.gnome.org/Apps/Tomboy/NoteXmlFormat
54      *
55      * @NoAdminRequired
56      * @NoCSRFRequired
57      */
58     public function xml($guid)
59     {
60         $note = $this->getNotes()->load($guid, false);
61         if ($note === null) {
62             $res = new ErrorResponse('Note does not exist');
63             $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
64             return $res;
65         }
66
67         $xw = new \XMLWriter();
68         $xw->openMemory();
69         $xw->startDocument('1.0', 'utf-8');
70
71         $xw->startElementNS(null, 'note', 'http://beatniksoftware.com/tomboy');
72         $xw->writeAttribute('version', '0.3');
73         $xw->writeAttribute('xmlns:link', 'http://beatniksoftware.com/tomboy/link');
74         $xw->writeAttribute('xmlns:size', 'http://beatniksoftware.com/tomboy/size');
75
76         $xw->writeElement('title', $note->title);
77         $xw->startElement('text');
78         $xw->writeAttribute('xml:space', 'preserve');
79
80         $xw->startElement('note-content');
81         $xw->writeAttribute('version', $note->{'note-content-version'});
82         $xw->writeRaw($note->{'note-content'});
83         $xw->endElement();//note-content
84         $xw->endElement();//text
85
86         $xw->writeElement('last-change-date', $note->{'last-change-date'});
87         $xw->writeElement('last-metadata-change-date', $note->{'last-metadata-change-date'});
88         $xw->writeElement('create-date', $note->{'create-date'});
89         $xw->writeElement('cursor-position', 0);
90         $xw->writeElement('width', 450);
91         $xw->writeElement('height', 360);
92         $xw->writeElement('x', 0);
93         $xw->writeElement('y', 0);
94         $xw->writeElement('open-on-startup', $note->{'open-on-startup'});
95
96         $xw->endElement();//note
97
98         return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
99     }
100
101     protected function getNotes()
102     {
103         $username = $this->user->getUid();
104         $notes  = new \OCA\Grauphel\Lib\NoteStorage($this->urlGen);
105         $notes->setUsername($username);
106         return $notes;
107     }
108 }
109 ?>