sync could work if php would not crash
authorChristian Weiske <cweiske@cweiske.de>
Wed, 20 Aug 2014 04:27:58 +0000 (06:27 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 20 Aug 2014 04:27:58 +0000 (06:27 +0200)
appinfo/app.php
appinfo/application.php
appinfo/database.xml
appinfo/info.xml
appinfo/routes.php
appinfo/version
controller/apicontroller.php
controller/oauthcontroller.php
src [deleted symlink]

index 3e319dfdc833884baa1945109a8e479f9703423b..3da4f00e5fd2ce8204dcaec2a34974e4cadee6f7 100755 (executable)
@@ -1,13 +1,13 @@
 <?php
 <?php
-require_once (__DIR__ . '/../src/grauphel/Autoloader.php');
-grauphel\Autoloader::register();
-
 //OCP\App::registerAdmin( 'apptemplate', 'settings' );
 
 //OCP\App::registerAdmin( 'apptemplate', 'settings' );
 
-OCP\App::addNavigationEntry( array( 
-       'id' => 'grauphel',
-       'order' => 2342,
-       'href' => OCP\Util::linkTo( 'grauphel', 'index.php' ),
-       'icon' => OCP\Util::imagePath( 'grauphel', 'notes.png' ),
-       'name' => 'Tomboy notes'
-));
+OCP\App::addNavigationEntry(
+    array( 
+        'id' => 'grauphel',
+        'order' => 2342,
+        'href' => OCP\Util::linkTo('grauphel', 'index.php'),
+        'icon' => OCP\Util::imagePath('grauphel', 'notes.png'),
+        'name' => 'Tomboy notes'
+    )
+);
+?>
index 7a1691de9eed2f4b7317ca7005dfd9a9bfe3f05e..69d15722c18f301f1ca2c66c455846507c1c0a7c 100644 (file)
@@ -28,18 +28,8 @@ class Application extends App
                     = $c->query('ServerContainer')->getURLGenerator();
                 return new \OCA\Grauphel\Controller\ApiController(
                     $c->query('AppName'),
                     = $c->query('ServerContainer')->getURLGenerator();
                 return new \OCA\Grauphel\Controller\ApiController(
                     $c->query('AppName'),
-                    $c->query('Request')
-                );
-            }
-        );
-        $container->registerService(
-            'AccessController',
-            function($c) {
-                Dependencies::get()->urlGen
-                    = $c->query('ServerContainer')->getURLGenerator();
-                 return new \OCA\Grauphel\Controller\AccessController(
-                    $c->query('AppName'),
-                    $c->query('Request')
+                    $c->query('Request'),
+                    $c->query('Session')->getUser()
                 );
             }
         );
                 );
             }
         );
index ae089caedcf0deceb6b4b6875f88bb21e9dedae5..5bf61726a5b483c1ea14b6f9a950a67f5d672822 100755 (executable)
                                <notnull>true</notnull>
                                <length>128</length>
                        </field>
                                <notnull>true</notnull>
                                <length>128</length>
                        </field>
+                       <field>
+                               <name>note_last_sync_revision</name>
+                               <type>integer</type>
+                               <default>0</default>
+                               <notnull>true</notnull>
+                               <length>11</length>
+                       </field>
 
                        <field>
                                <name>note_create_date</name>
 
                        <field>
                                <name>note_create_date</name>
                        <field>
                                <name>syncdata_user</name>
                                <type>text</type>
                        <field>
                                <name>syncdata_user</name>
                                <type>text</type>
-                               <notnull>false</notnull>
+                               <notnull>true</notnull>
                                <length>64</length>
                        </field>
                        <field>
                                <length>64</length>
                        </field>
                        <field>
index ac0f9eca37a831e9a179f4bc20477c0c17807e74..1008496ca2e303859a4e566dde396fd42dbb4893 100755 (executable)
@@ -3,7 +3,7 @@
        <id>grauphel</id>
        <name>Grauphel: Tomboy note server</name>
        <description>Tomboy REST API server to sync notes between devices</description>
        <id>grauphel</id>
        <name>Grauphel: Tomboy note server</name>
        <description>Tomboy REST API server to sync notes between devices</description>
-       <version>0.1</version>
+       <version>0.1.1</version>
        <licence>AGPL3 or later</licence>
        <author>Christian Weiske</author>
        <requiremin>7</requiremin>
        <licence>AGPL3 or later</licence>
        <author>Christian Weiske</author>
        <requiremin>7</requiremin>
index 5567670fecec69fa01e4a8c5405f3ecd802d5dd9..31e5692e464d4b23024ad0b228d476b27ff1ba3c 100644 (file)
@@ -6,22 +6,6 @@ $application->registerRoutes(
     $this,
     array(
         'routes' => array(
     $this,
     array(
         'routes' => array(
-            array(
-                'url'  => '/test',
-                'name' => 'access#test',
-            ),
-
-            array(
-                'url'  => '/authorize',
-                'name' => 'access#authorize',
-                'verb' => 'POST',
-                ),
-            array(
-                'url'  => '/login',
-                'name' => 'access#login',
-                'verb' => 'GET',
-            ),
-
             array(
                 'url'  => '/oauth/access_token',
                 'name' => 'oauth#accessToken',
             array(
                 'url'  => '/oauth/access_token',
                 'name' => 'oauth#accessToken',
@@ -49,23 +33,28 @@ $application->registerRoutes(
                 'verb' => 'GET',
             ),
             array(
                 'verb' => 'GET',
             ),
             array(
-                'url'  => '/api/1.0/{user}/note/{guid}',
-                'name' => 'api#note',
+                'url'  => '/api/1.0/',
+                'name' => 'api#indexSlash',
                 'verb' => 'GET',
             ),
             array(
                 'verb' => 'GET',
             ),
             array(
-                'url'  => '/api/1.0/{user}/notes',
-                'name' => 'api#notes',
+                'url'  => '/api/1.0/{username}',
+                'name' => 'api#user',
                 'verb' => 'GET',
             ),
             array(
                 'verb' => 'GET',
             ),
             array(
-                'url'  => '/api/1.0/{user}/notes',
+                'url'  => '/api/1.0/{username}/notes',
                 'name' => 'api#notes',
                 'name' => 'api#notes',
-                'verb' => 'POST',
+                'verb' => 'GET',
             ),
             array(
             ),
             array(
-                'url'  => '/api/1.0/{user}',
-                'name' => 'api#user',
+                'url'  => '/api/1.0/{username}/notes',
+                'name' => 'api#notesSave',
+                'verb' => 'PUT',
+            ),
+            array(
+                'url'  => '/api/1.0/{username}/note/{guid}',
+                'name' => 'api#note',
                 'verb' => 'GET',
             ),
         )
                 'verb' => 'GET',
             ),
         )
index 49d59571fbf6e077eece30f8c418b6aad15e20b0..17e51c385ea382d4f2ef124b7032c1604845622d 100755 (executable)
@@ -1 +1 @@
-0.1
+0.1.1
index 8ce8997dc81476c88a29a784dc3e6585cdb6e47f..166436e1b2e5cde68e45105a0d42119e75a0b774 100644 (file)
@@ -16,6 +16,7 @@ namespace OCA\Grauphel\Controller;
 use \OCP\AppFramework\Controller;
 use \OCP\AppFramework\Http\JSONResponse;
 
 use \OCP\AppFramework\Controller;
 use \OCP\AppFramework\Http\JSONResponse;
 
+use \OCA\Grauphel\Lib\NoteStorage;
 use \OCA\Grauphel\Lib\OAuth;
 use \OCA\Grauphel\Lib\Dependencies;
 
 use \OCA\Grauphel\Lib\OAuth;
 use \OCA\Grauphel\Lib\Dependencies;
 
@@ -32,6 +33,23 @@ use \OCA\Grauphel\Lib\Dependencies;
  */
 class ApiController extends Controller
 {
  */
 class ApiController 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;
+        $this->deps  = Dependencies::get();
+        $this->notes = new NoteStorage($this->deps->urlGen);
+
+        //default http header: we assume something is broken
+        header('HTTP/1.0 500 Internal Server Error');
+    }
+
     /**
      * /api/1.0
      *
     /**
      * /api/1.0
      *
@@ -39,7 +57,7 @@ class ApiController extends Controller
      * @NoCSRFRequired
      * @PublicPage
      */
      * @NoCSRFRequired
      * @PublicPage
      */
-    public function index()
+    public function index($route = 'grauphel.api.index')
     {
         $deps = Dependencies::get();
         $authenticated = false;
     {
         $deps = Dependencies::get();
         $authenticated = false;
@@ -53,7 +71,7 @@ class ApiController extends Controller
                 ->registerAccessTokenHandler($provider);
             $provider->checkOAuthRequest(
                 $urlGen->getAbsoluteURL(
                 ->registerAccessTokenHandler($provider);
             $provider->checkOAuthRequest(
                 $urlGen->getAbsoluteURL(
-                    $urlGen->linkToRoute('grauphel.api.index')
+                    $urlGen->linkToRoute($route)
                 )
             );
             $authenticated = true;
                 )
             );
             $authenticated = true;
@@ -66,6 +84,10 @@ class ApiController extends Controller
             if ($e->getCode() != OAUTH_PARAMETER_ABSENT) {
                 $oauth->error($e);
             }
             if ($e->getCode() != OAUTH_PARAMETER_ABSENT) {
                 $oauth->error($e);
             }
+            if ($this->user !== null) {
+                $username = $this->user->getUID();
+                $authenticated = true;
+            }
         }
 
         $data = array(
         }
 
         $data = array(
@@ -85,7 +107,7 @@ class ApiController extends Controller
             $data['user-ref'] = array(
                 'api-ref' => $urlGen->getAbsoluteURL(
                     $urlGen->linkToRoute(
             $data['user-ref'] = array(
                 'api-ref' => $urlGen->getAbsoluteURL(
                     $urlGen->linkToRoute(
-                        'grauphel.api.user', array('user' => $username)
+                        'grauphel.api.user', array('username' => $username)
                     )
                 ),
                 'href' => null,//FIXME
                     )
                 ),
                 'href' => null,//FIXME
@@ -93,65 +115,106 @@ class ApiController extends Controller
         }
 
         return new JSONResponse($data);
         }
 
         return new JSONResponse($data);
-        $deps->renderer->sendJson($data);
     }
 
     /**
     }
 
     /**
-     * GET /api/1.0/$user/notes/$noteguid
+     * /api/1.0/
      *
      * @NoAdminRequired
      * @NoCSRFRequired
      * @PublicPage
      */
      *
      * @NoAdminRequired
      * @NoCSRFRequired
      * @PublicPage
      */
-    public function note()
+    public function indexSlash()
     {
     {
-        $deps = Dependencies::get();
-        $username = $deps->urlGen->loadUsername();
-        $guid     = $deps->urlGen->loadGuid();
-        $oauth = new \OAuth();
-        $oauth->setDeps($deps);
-        $oauth->verifyOAuthUser($username, $deps->urlGen->note($username, $guid));
+        return $this->index('grauphel.api.indexSlash');
+    }
 
 
-        $note = $deps->notes->load($username, $guid, false);
-        if ($note === null) {
-            header('HTTP/1.0 404 Not Found');
-            header('Content-type: text/plain');
-            echo "Note does not exist\n";
-            exit(1);
-        }
+    /**
+     * GET /api/1.0/$user
+     *
+     * @NoAdminRequired
+     * @NoCSRFRequired
+     * @PublicPage
+     */
+    public function user($username)
+    {
+        $this->verifyUser($username);
+        $syncdata = $this->notes->loadSyncData($username);
 
 
-        $data = array('note' => array($note));
-        $deps->renderer->sendJson($data);
+        $data = array(
+            'user-name'  => $username,
+            'first-name' => null,
+            'last-name'  => null,
+            'notes-ref'  => array(
+                'api-ref' => $this->deps->urlGen->getAbsoluteURL(
+                    $this->deps->urlGen->linkToRoute(
+                        'grauphel.api.notes', array('username' => $username)
+                    )
+                ),
+                'href'    => null,
+            ),
+            'latest-sync-revision' => $syncdata->latestSyncRevision,
+            'current-sync-guid'    => $syncdata->currentSyncGuid,
+        );
+        return new JSONResponse($data);
     }
 
     /**
     }
 
     /**
-     * GET|PUT /api/1.0/$user/notes
+     * GET /api/1.0/$user/notes
      *
      * @NoAdminRequired
      * @NoCSRFRequired
      * @PublicPage
      */
      *
      * @NoAdminRequired
      * @NoCSRFRequired
      * @PublicPage
      */
-    public function notes()
+    public function notes($username)
     {
     {
-        $deps = Dependencies::get();
-        $username = $deps->urlGen->loadUsername();
-        $oauth = new \OAuth();
-        $oauth->setDeps($deps);
-        $oauth->verifyOAuthUser($username, $deps->urlGen->notes($username));
-
-        $syncdata = $deps->notes->loadSyncData($username);
+        $this->verifyUser(
+            $username,
+            $this->deps->urlGen->getAbsoluteURL(
+                $this->deps->urlGen->linkToRoute(
+                    'grauphel.api.notes', array('username' => $username)
+                )
+            )
+        );
+        $syncdata = $this->notes->loadSyncData($username);
+        return $this->fetchNotes($username, $syncdata);
+    }
 
 
+    /**
+     * PUT /api/1.0/$user/notes
+     *
+     * @NoAdminRequired
+     * @NoCSRFRequired
+     * @PublicPage
+     */
+    public function notesSave($username)
+    {
+        $this->verifyUser(
+            $username,
+            $this->deps->urlGen->getAbsoluteURL(
+                $this->deps->urlGen->linkToRoute(
+                    'grauphel.api.user', array('username' => $username)
+                )
+            )
+        );
+        $syncdata = $this->notes->loadSyncData($username);
+        
         $this->handleNoteSave($username, $syncdata);
 
         $this->handleNoteSave($username, $syncdata);
 
+        return $this->fetchNotes($username, $syncdata);
+    }
+
+    protected function fetchNotes($username, $syncdata)
+    {
         $since = null;
         if (isset($_GET['since'])) {
             $since = (int) $_GET['since'];
         }
 
         if (isset($_GET['include_notes']) && $_GET['include_notes']) {
         $since = null;
         if (isset($_GET['since'])) {
             $since = (int) $_GET['since'];
         }
 
         if (isset($_GET['include_notes']) && $_GET['include_notes']) {
-            $notes = $deps->notes->loadNotesFull($username, $since);
+            $notes = $this->notes->loadNotesFull($username, $since);
         } else {
         } else {
-            $notes = $deps->notes->loadNotesOverview($username, $since);
+            $notes = $this->notes->loadNotesOverview($username, $since);
         }
 
         //work around bug https://bugzilla.gnome.org/show_bug.cgi?id=734313
         }
 
         //work around bug https://bugzilla.gnome.org/show_bug.cgi?id=734313
@@ -165,7 +228,7 @@ class ApiController extends Controller
             'latest-sync-revision' => $syncdata->latestSyncRevision,
             'notes' => $notes,
         );
             'latest-sync-revision' => $syncdata->latestSyncRevision,
             'notes' => $notes,
         );
-        $deps->renderer->sendJson($data);
+        return new JSONResponse($data);
     }
 
     protected function handleNoteSave($username, $syncdata)
     }
 
     protected function handleNoteSave($username, $syncdata)
@@ -219,35 +282,50 @@ class ApiController extends Controller
     }
 
     /**
     }
 
     /**
-     * GET /api/1.0/$user
+     * GET /api/1.0/$user/notes/$noteguid
      *
      * @NoAdminRequired
      * @NoCSRFRequired
      * @PublicPage
      */
      *
      * @NoAdminRequired
      * @NoCSRFRequired
      * @PublicPage
      */
-    public function user()
+    public function note()
     {
     {
+        //FIXME
         $deps = Dependencies::get();
         $username = $deps->urlGen->loadUsername();
         $deps = Dependencies::get();
         $username = $deps->urlGen->loadUsername();
-
+        $guid     = $deps->urlGen->loadGuid();
         $oauth = new \OAuth();
         $oauth->setDeps($deps);
         $oauth = new \OAuth();
         $oauth->setDeps($deps);
-        $oauth->verifyOAuthUser($username, $deps->urlGen->user($username));
+        $oauth->verifyOAuthUser($username, $deps->urlGen->note($username, $guid));
 
 
-        $syncdata = $deps->notes->loadSyncData($username);
+        $note = $deps->notes->load($username, $guid, false);
+        if ($note === null) {
+            header('HTTP/1.0 404 Not Found');
+            header('Content-type: text/plain');
+            echo "Note does not exist\n";
+            exit(1);
+        }
 
 
-        $data = array(
-            'user-name'  => $username,
-            'first-name' => null,
-            'last-name'  => null,
-            'notes-ref'  => array(
-                'api-ref' => $deps->urlGen->notes($username),
-                'href'    => null,
-            ),
-            'latest-sync-revision' => $syncdata->latestSyncRevision,
-            'current-sync-guid'    => $syncdata->currentSyncGuid,
-        );
+        $data = array('note' => array($note));
         $deps->renderer->sendJson($data);
     }
         $deps->renderer->sendJson($data);
     }
+
+    /**
+     * Checks if the given user is authorized (by oauth token or normal login)
+     *
+     * @param string $username Username to verify
+     *
+     * @return boolean True if all is fine, Response in case of an error
+     */
+    protected function verifyUser($username, $curUrl)
+    {
+        if ($this->user !== null && $this->user->getUID() == $username) {
+            return true;
+        }
+
+        $oauth = new OAuth();
+        $oauth->setDeps($this->deps);
+        $oauth->verifyOAuthUser($username, $curUrl);
+    }
 }
 ?>
 }
 ?>
index 1dfdec9854b9d4b0ccfcbfabde32bdd2d34fb014..ceaa935f2814de8f3e8ef7b41db973b42f79a753 100644 (file)
@@ -15,7 +15,6 @@ namespace OCA\Grauphel\Controller;
 
 use \OCP\AppFramework\Controller;
 use \OCP\AppFramework\Http;
 
 use \OCP\AppFramework\Controller;
 use \OCP\AppFramework\Http;
-use \OCP\AppFramework\Http\JSONResponse;
 use \OCP\AppFramework\Http\RedirectResponse;
 use \OCP\AppFramework\Http\TemplateResponse;
 
 use \OCP\AppFramework\Http\RedirectResponse;
 use \OCP\AppFramework\Http\TemplateResponse;
 
diff --git a/src b/src
deleted file mode 120000 (symlink)
index acc4488..0000000
--- a/src
+++ /dev/null
@@ -1 +0,0 @@
-../../grauphel/src
\ No newline at end of file