Show client name in oauth authorization (works for tomdroid only)
[grauphel.git] / controller / apicontroller.php
index 691d8abbe0cdaf99b3586c922c31194f2a2fc460..ee16f801f8e77570fe4abd0a596a615e4c990e44 100644 (file)
@@ -105,6 +105,12 @@ class ApiController extends Controller
             'api-version' => '1.0',
         );
 
+        $client = $this->getClient();
+        if ($client !== false) {
+            $data['oauth_authorize_url'] .= '?client='
+                . urlencode($this->getNiceClientName($client));
+        }
+
         if ($authenticated) {
             $data['user-ref'] = array(
                 'api-ref' => $urlGen->getAbsoluteURL(
@@ -327,6 +333,31 @@ class ApiController extends Controller
         return new JSONResponse($note);
     }
 
+    protected function getClient()
+    {
+        if (isset($_SERVER['HTTP_X_TOMBOY_CLIENT'])) {
+            $client = $_SERVER['HTTP_X_TOMBOY_CLIENT'];
+            $doublepos = strpos($client, ', org.tomdroid');
+            if ($doublepos !== false) {
+                //https://bugs.launchpad.net/tomdroid/+bug/1375436
+                //X-Tomboy-Client header is sent twice
+                $client = substr($client, 0, $doublepos);
+            }
+            return $client;
+        }
+
+        return false;
+    }
+
+    protected function getNiceClientName($client)
+    {
+        if (substr($client, 0, 12) == 'org.tomdroid') {
+            //org.tomdroid v0.7.5, build 14, Android v4.4.2, innotek GmbH/VirtualBox
+            return 'Tomdroid';
+        }
+        return $client;
+    }
+
     /**
      * Checks if the given user is authorized (by oauth token or normal login)
      *