reply support
authorChristian Weiske <cweiske@cweiske.de>
Tue, 6 Sep 2016 22:15:20 +0000 (00:15 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 6 Sep 2016 22:15:20 +0000 (00:15 +0200)
src/shpub/Cli.php
src/shpub/Command/Like.php
src/shpub/Command/Reply.php [new file with mode: 0644]
src/shpub/Request.php [new file with mode: 0644]

index a57b287fda084518f5f47f2066105507be58a45b..5b46fcdd1f80748c7ee7dd67c025068390d8112b 100644 (file)
@@ -27,15 +27,27 @@ class Cli
                     $res->command->options['force']
                 );
                 break;
                     $res->command->options['force']
                 );
                 break;
+
+            case 'server':
+                $cmd = new Command_Server($this->cfg);
+                $cmd->run($res->command->options['verbose']);
+                break;
+
             case 'like':
                 $this->requireValidHost();
                 $cmd = new Command_Like($this->cfg->host);
                 $cmd->run($res->command->args['url']);
                 break;
             case 'like':
                 $this->requireValidHost();
                 $cmd = new Command_Like($this->cfg->host);
                 $cmd->run($res->command->args['url']);
                 break;
-            case 'server':
-                $cmd = new Command_Server($this->cfg);
-                $cmd->run($res->command->options['verbose']);
+
+            case 'reply':
+                $this->requireValidHost();
+                $cmd = new Command_Reply($this->cfg->host);
+                $cmd->run(
+                    $res->command->args['url'],
+                    implode(' ', $res->command->args['text'])
+                );
                 break;
                 break;
+
             default:
                 var_dump($this->cfg->host, $res);
                 Log::err('FIXME');
             default:
                 var_dump($this->cfg->host, $res);
                 Log::err('FIXME');
@@ -171,6 +183,7 @@ class Cli
             'text',
             [
                 'optional'    => false,
             'text',
             [
                 'optional'    => false,
+                'multiple'    => true,
                 'description' => 'Reply text',
             ]
         );
                 'description' => 'Reply text',
             ]
         );
index 8baeee7e70901d47673ab2fb85941a14309cf72a..7daafc22d7ee4afb51ebe1729c72ec2d941fe9e5 100644 (file)
@@ -27,23 +27,8 @@ class Command_Like
             ]
         );
 
             ]
         );
 
-        $req = new \HTTP_Request2($this->host->endpoints->micropub, 'POST');
-        if (version_compare(PHP_VERSION, '5.6.0', '<')) {
-            //correct ssl validation on php 5.5 is a pain, so disable
-            $req->setConfig('ssl_verify_host', false);
-            $req->setConfig('ssl_verify_peer', false);
-        }
-        $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);
-        }
+        $req = new Request($this->host);
+        $res = $req->send($body);
         $postUrl = $res->getHeader('Location');
         echo "Like created at server\n";
         echo $postUrl . "\n";
         $postUrl = $res->getHeader('Location');
         echo "Like created at server\n";
         echo $postUrl . "\n";
diff --git a/src/shpub/Command/Reply.php b/src/shpub/Command/Reply.php
new file mode 100644 (file)
index 0000000..cb09ba7
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+namespace shpub;
+
+class Command_Reply
+{
+    /**
+     * @var Config_Host
+     */
+    protected $host;
+
+    public function __construct($host)
+    {
+        $this->host = $host;
+    }
+
+    public function run($url, $text)
+    {
+        $url = Validator::url($url, 'url');
+        if ($url === false) {
+            exit(10);
+        }
+
+        $body = http_build_query(
+            [
+                'h'           => 'entry',
+                'content'     => $text,
+                'in-reply-to' => $url,
+            ]
+        );
+
+        $req = new Request($this->host);
+        $res = $req->send($body);
+        $postUrl = $res->getHeader('Location');
+        echo "Reply created at server\n";
+        echo $postUrl . "\n";
+    }
+}
+?>
diff --git a/src/shpub/Request.php b/src/shpub/Request.php
new file mode 100644 (file)
index 0000000..2896c81
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+namespace shpub;
+
+class Request
+{
+    public $req;
+
+    public function __construct($host)
+    {
+        $this->req = new \HTTP_Request2($host->endpoints->micropub, 'POST');
+        if (version_compare(PHP_VERSION, '5.6.0', '<')) {
+            //correct ssl validation on php 5.5 is a pain, so disable
+            $this->req->setConfig('ssl_verify_host', false);
+            $this->req->setConfig('ssl_verify_peer', false);
+        }
+        $this->req->setHeader('Content-type', 'application/x-www-form-urlencoded');
+        $this->req->setHeader('Authorization', 'Bearer ' . $host->token);
+    }
+
+    public function send($body)
+    {
+        $this->req->setBody($body);
+        $res = $this->req->send();
+        if (intval($res->getStatus() / 100) != 2) {
+            Log::err(
+                'Server returned an error status code ' . $res->getStatus()
+            );
+            Log::err($res->getBody());
+            exit(11);
+        }
+        return $res;
+    }
+}
\ No newline at end of file