X-Git-Url: https://git.cweiske.de/shpub.git/blobdiff_plain/63c0e2f9dc4ff5a13b474dcdca9e1de4b217bde3..d78790c613a4363aa0566c357c4444e17582dc23:/src/shpub/Command/Like.php diff --git a/src/shpub/Command/Like.php b/src/shpub/Command/Like.php index 8baeee7..e7c6052 100644 --- a/src/shpub/Command/Like.php +++ b/src/shpub/Command/Like.php @@ -4,18 +4,30 @@ namespace shpub; class Command_Like { /** - * @var Config_Host + * @var Config */ - protected $host; + protected $cfg; - public function __construct($host) + public function __construct($cfg) { - $this->host = $host; + $this->cfg = $cfg; } - public function run($url) + public static function opts(\Console_CommandLine $optParser) { - $url = Validator::url($url, 'url'); + $cmd = $optParser->addCommand('like'); + $cmd->addArgument( + 'url', + [ + 'optional' => false, + 'description' => 'URL that is liked', + ] + ); + } + + public function run($command) + { + $url = Validator::url($command->args['url'], 'url'); if ($url === false) { exit(10); } @@ -27,26 +39,17 @@ 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() - ); + $req = new Request($this->cfg->host, $this->cfg); + $res = $req->send($body); + $postUrl = $res->getHeader('Location'); + if ($postUrl === null) { + Log::err('Error: Server sent no "Location" header and said:'); Log::err($res->getBody()); - exit(11); + exit(20); + } else { + echo "Like created at server\n"; + echo $postUrl . "\n"; } - $postUrl = $res->getHeader('Location'); - echo "Like created at server\n"; - echo $postUrl . "\n"; } } ?>