From: Christian Weiske Date: Tue, 6 Sep 2016 22:15:20 +0000 (+0200) Subject: reply support X-Git-Tag: v0.0.1~8 X-Git-Url: https://git.cweiske.de/shpub.git/commitdiff_plain/e53ff38824e76ee587c3c7b7fffebc551933e761 reply support --- diff --git a/src/shpub/Cli.php b/src/shpub/Cli.php index a57b287..5b46fcd 100644 --- a/src/shpub/Cli.php +++ b/src/shpub/Cli.php @@ -27,15 +27,27 @@ class Cli $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 '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; + default: var_dump($this->cfg->host, $res); Log::err('FIXME'); @@ -171,6 +183,7 @@ class Cli 'text', [ 'optional' => false, + 'multiple' => true, 'description' => 'Reply text', ] ); diff --git a/src/shpub/Command/Like.php b/src/shpub/Command/Like.php index 8baeee7..7daafc2 100644 --- a/src/shpub/Command/Like.php +++ b/src/shpub/Command/Like.php @@ -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"; diff --git a/src/shpub/Command/Reply.php b/src/shpub/Command/Reply.php new file mode 100644 index 0000000..cb09ba7 --- /dev/null +++ b/src/shpub/Command/Reply.php @@ -0,0 +1,38 @@ +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 index 0000000..2896c81 --- /dev/null +++ b/src/shpub/Request.php @@ -0,0 +1,33 @@ +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