disable ssl verification on PHP < 5.6
[shpub.git] / src / shpub / Command / Like.php
1 <?php
2 namespace shpub;
3
4 class Command_Like
5 {
6     /**
7      * @var Config_Host
8      */
9     protected $host;
10
11     public function __construct($host)
12     {
13         $this->host = $host;
14     }
15
16     public function run($url)
17     {
18         $url = Validator::url($url, 'url');
19         if ($url === false) {
20             exit(10);
21         }
22
23         $body = http_build_query(
24             [
25                 'h'       => 'entry',
26                 'like-of' => $url,
27             ]
28         );
29
30         $req = new \HTTP_Request2($this->host->endpoints->micropub, 'POST');
31         if (version_compare(PHP_VERSION, '5.6.0', '<')) {
32             //correct ssl validation on php 5.5 is a pain, so disable
33             $req->setConfig('ssl_verify_host', false);
34             $req->setConfig('ssl_verify_peer', false);
35         }
36         $req->setHeader('Content-type', 'application/x-www-form-urlencoded');
37         $req->setHeader('Authorization', 'Bearer ' . $this->host->token);
38         $req->setBody($body);
39         $res = $req->send();
40         if (intval($res->getStatus() / 100) != 2) {
41             Log::err(
42                 'Server returned an error status code ' . $res->getStatus()
43             );
44             Log::err($res->getBody());
45             exit(11);
46         }
47         $postUrl = $res->getHeader('Location');
48         echo "Like created at server\n";
49         echo $postUrl . "\n";
50     }
51 }
52 ?>