likes work!
authorChristian Weiske <cweiske@cweiske.de>
Tue, 6 Sep 2016 21:16:26 +0000 (23:16 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 6 Sep 2016 21:16:26 +0000 (23:16 +0200)
src/shpub/Cli.php
src/shpub/Command/Like.php [new file with mode: 0644]
src/shpub/Validator.php [new file with mode: 0644]

index fb22d3fb5db9c7b9bc4427c035f3c44add22a1ee..32dcbe2d929e5c0332571ebc3d372fa9924a3f38 100644 (file)
@@ -181,6 +181,8 @@ class Cli
                 'Server data incomplete. "shpub connect" first.'
             );
         }
                 'Server data incomplete. "shpub connect" first.'
             );
         }
+
+        $this->cfg->host->loadEndpoints();
     }
 }
 ?>
     }
 }
 ?>
diff --git a/src/shpub/Command/Like.php b/src/shpub/Command/Like.php
new file mode 100644 (file)
index 0000000..70e836f
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+namespace shpub;
+
+class Command_Like
+{
+    /**
+     * @var Config_Host
+     */
+    protected $host;
+
+    public function __construct($host)
+    {
+        $this->host = $host;
+    }
+
+    public function run($url)
+    {
+        $url = Validator::url($url, 'url');
+        if ($url === false) {
+            exit(10);
+        }
+
+        $body = http_build_query(
+            [
+                'h'       => 'entry',
+                'like-of' => $url,
+            ]
+        );
+
+        $req = new \HTTP_Request2($this->host->endpoints->micropub, 'POST');
+        $req->setHeader('Content-type', 'application/x-www-form-urlencoded');
+        $req->setHeader('Authorization', 'Bearer ' . $this->host->token);
+        $req->setBody($body);
+        $res = $req->send();
+        if (intval($res->getStatus() / 100) != 2) {
+            Log::err(
+                'Server returned an error status code ' . $res->getStatus()
+            );
+            Log::err($res->getBody());
+            exit(11);
+        }
+        $postUrl = $res->getHeader('Location');
+        echo "Like created at server\n";
+        echo $postUrl . "\n";
+    }
+}
+?>
diff --git a/src/shpub/Validator.php b/src/shpub/Validator.php
new file mode 100644 (file)
index 0000000..f7f3605
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+namespace shpub;
+
+class Validator
+{
+    public static function url($url, $helpName)
+    {
+        $parts = parse_url($url);
+        if (!isset($parts['scheme'])) {
+            $url = 'http://' . $url;
+        } else if ($parts['scheme'] != 'http' && $parts['scheme'] != 'https') {
+            Log::err(
+                'Invalid URL scheme in ' . $helpName . ': ' . $parts['scheme']
+            );
+            return false;
+        }
+
+        if (!isset($parts['host'])) {
+            Log::err('Invalid URL: No host in ' . $helpName);
+            return false;
+        }
+
+        return $url;
+    }
+}
+?>