cache server endpoint URLs
authorChristian Weiske <cweiske@cweiske.de>
Tue, 6 Sep 2016 20:43:47 +0000 (22:43 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 6 Sep 2016 20:43:47 +0000 (22:43 +0200)
src/shpub/Command/Connect.php
src/shpub/Config.php
src/shpub/Config/Endpoints.php
src/shpub/Config/Host.php

index 2e7e266ffe810ca7dba4f93f1ce54fbd2b5bb91b..bd1606eb849c96f6d64b99d616ac37c4907d41a2 100644 (file)
@@ -17,9 +17,13 @@ class Command_Connect
     public function run($server, $user, $newKey, $force)
     {
         $host = $this->getHost($newKey != '' ? $newKey : $server, $force);
+        if ($host === null) {
+            //already taken
+            return;
+        }
         if ($host->endpoints->incomplete()) {
             $host->server = $server;
-            $this->discoverEndpoints($server, $host->endpoints);
+            $host->loadEndpoints();
         }
 
         list($redirect_uri, $socketStr) = $this->getHttpServerData();
@@ -116,50 +120,9 @@ class Command_Connect
                 return;
             }
         }
-        if ($host->endpoints === null) {
-            $host->endpoints = new Config_Endpoints();
-        }
         return $host;
     }
 
-    function discoverEndpoints($url, $cfg)
-    {
-        //TODO: discovery via link headers
-        $sx = simplexml_load_file($url);
-        if ($sx === false) {
-            Log::err('Error loading URL: ' . $url);
-            exit(1);
-        }
-        $sx->registerXPathNamespace('h', 'http://www.w3.org/1999/xhtml');
-
-        $auths = $sx->xpath(
-            '/h:html/h:head/h:link[@rel="authorization_endpoint" and @href]'
-        );
-        if (!count($auths)) {
-            Log::err('No authorization endpoint found');
-            exit(1);
-        }
-        $cfg->authorization = (string) $auths[0]['href'];
-
-        $tokens = $sx->xpath(
-            '/h:html/h:head/h:link[@rel="token_endpoint" and @href]'
-        );
-        if (!count($tokens)) {
-            Log::err('No token endpoint found');
-            exit(1);
-        }
-        $cfg->token = (string) $tokens[0]['href'];
-
-        $mps = $sx->xpath(
-            '/h:html/h:head/h:link[@rel="micropub" and @href]'
-        );
-        if (!count($mps)) {
-            Log::err('No micropub endpoint found');
-            exit(1);
-        }
-        $cfg->micropub = (string) $mps[0]['href'];
-    }
-
     protected function getHttpServerData()
     {
         //FIXME: get IP from SSH_CONNECTION
index 044b1504ab4ff87c7b21621d079e4187176bb155..352ab99da68b8194b865e841fa2486a338762e70 100644 (file)
@@ -15,6 +15,7 @@ class Config
     protected function getConfigFilePath()
     {
         if (!isset($_SERVER['HOME'])) {
+            Log::err('Cannot determine home directory');
             return false;
         }
 
@@ -70,7 +71,7 @@ class Config
         $cfgFilePath = $this->getConfigFilePath();
         $cfgDir = dirname($cfgFilePath);
         if (!is_dir($cfgDir)) {
-            mkdir($cfgDir);
+            mkdir($cfgDir, 0700);
         }
         file_put_contents($cfgFilePath, $str);
         //contains sensitive data; nobody else may read that
index c029b95c55c6a52af4b4e9687ca6331e87e6cf9e..62e7c4905ecd2698c572cc0bac605635747af726 100644 (file)
@@ -37,5 +37,98 @@ class Config_Endpoints
             || $this->token === null
             || $this->micropub === null;
     }
+
+    public function discover($server)
+    {
+        //TODO: discovery via link headers
+        $sx = simplexml_load_file($server);
+        if ($sx === false) {
+            Log::err('Error loading URL: ' . $server);
+            exit(1);
+        }
+        $sx->registerXPathNamespace('h', 'http://www.w3.org/1999/xhtml');
+
+        $auths = $sx->xpath(
+            '/h:html/h:head/h:link[@rel="authorization_endpoint" and @href]'
+        );
+        if (!count($auths)) {
+            Log::err('No authorization endpoint found');
+            exit(1);
+        }
+        $this->authorization = (string) $auths[0]['href'];
+
+        $tokens = $sx->xpath(
+            '/h:html/h:head/h:link[@rel="token_endpoint" and @href]'
+        );
+        if (!count($tokens)) {
+            Log::err('No token endpoint found');
+            exit(1);
+        }
+        $this->token = (string) $tokens[0]['href'];
+
+        $mps = $sx->xpath(
+            '/h:html/h:head/h:link[@rel="micropub" and @href]'
+        );
+        if (!count($mps)) {
+            Log::err('No micropub endpoint found');
+            exit(1);
+        }
+        $this->micropub = (string) $mps[0]['href'];
+    }
+
+    public function load($server)
+    {
+        $file = $this->getCacheFilePath($server, false);
+        if ($file === false || !file_exists($file)) {
+            return false;
+        }
+        $data = parse_ini_file($file);
+        foreach ($data as $prop => $val) {
+            if (!property_exists($this, $prop)) {
+                Log::err('Invalid cache config key "' . $prop . '"');
+                exit(1);
+            }
+            $this->$prop = $val;
+        }
+        return true;
+    }
+
+    public function save($server)
+    {
+        $file = $this->getCacheFilePath($server, true);
+        if ($file === false) {
+            return false;
+        }
+
+        file_put_contents(
+            $file,
+            'micropub=' . $this->micropub . "\n"
+            . 'media=' . $this->media . "\n"
+            . 'token=' . $this->token . "\n"
+             . 'authorization=' . $this->authorization . "\n"
+        );
+    }
+
+    public function getCacheFilePath($server, $create = false)
+    {
+        if (isset($_SERVER['XDG_CACHE_HOME'])
+            && $_SERVER['XDG_CACHE_HOME'] != ''
+        ) {
+            $cacheBaseDir = $_SERVER['XDG_CACHE_HOME'];
+        } else {
+            if (!isset($_SERVER['HOME']) || $_SERVER['HOME'] == '') {
+                Log::err('Cannot determine home directory');
+                return false;
+            }
+            $cacheBaseDir = $_SERVER['HOME'] . '/.cache';
+        }
+
+        $cacheDir = $cacheBaseDir . '/shpub';
+        if (!is_dir($cacheDir) && $create) {
+            mkdir($cacheDir, 0700, true);
+        }
+        $file = $cacheDir . '/' . urlencode($server) . '.ini';
+        return $file;
+    }
 }
 ?>
index 5dd23ddf0b7b316fe8aa4dd2eff592a2c7edc80b..51d0ff59650a770a4d1f9c38283809549ff401ba 100644 (file)
@@ -37,5 +37,20 @@ class Config_Host
      * @var boolean
      */
     public $default;
+
+    public function __construct()
+    {
+        $this->endpoints = new Config_Endpoints();
+    }
+
+    public function loadEndpoints()
+    {
+        $this->endpoints = new Config_Endpoints();
+        $this->endpoints->load($this->server);
+        if ($this->endpoints->incomplete()) {
+            $this->endpoints->discover($this->server);
+            $this->endpoints->save($this->server);
+        }
+    }
 }
 ?>