edca12aae84749c8d71b57cd462bbd251d797b2b
[shpub.git] / src / shpub / Request.php
1 <?php
2 namespace shpub;
3
4 class Request
5 {
6     public $req;
7     public $cfg;
8
9     protected $uploadsInfo = [];
10
11     public function __construct($host, $cfg)
12     {
13         $this->cfg = $cfg;
14         $this->req = new MyHttpRequest2($host->endpoints->micropub, 'POST');
15         $this->req->setHeader('User-Agent: shpub');
16         if (version_compare(PHP_VERSION, '5.6.0', '<')) {
17             //correct ssl validation on php 5.5 is a pain, so disable
18             $this->req->setConfig('ssl_verify_host', false);
19             $this->req->setConfig('ssl_verify_peer', false);
20         }
21         $this->req->setHeader('Content-type', 'application/x-www-form-urlencoded');
22         $this->req->setHeader('authorization', 'Bearer ' . $host->token);
23     }
24
25     public function send($body = null)
26     {
27         if ($body !== null) {
28             $this->req->setBody($body);
29         }
30         if ($this->cfg->debug) {
31             $this->printCurl();
32         }
33         $res = $this->req->send();
34
35         if (intval($res->getStatus() / 100) != 2) {
36             Log::err(
37                 'Server returned an error status code ' . $res->getStatus()
38             );
39             Log::err($res->getBody());
40             exit(11);
41         }
42         return $res;
43     }
44
45     /**
46      * @param string                $fieldName    name of file-upload field
47      * @param string|resource|array $filename     full name of local file,
48      *               pointer to open file or an array of files
49      */
50     public function addUpload($fieldName, $filename)
51     {
52         $this->uploadsInfo[$fieldName] = $filename;
53         return $this->req->addUpload($fieldName, $filename);
54     }
55
56     protected function printCurl()
57     {
58         $command = 'curl';
59         if ($this->req->getMethod() != 'GET') {
60             $command .= ' -X ' . $this->req->getMethod();
61         }
62         foreach ($this->req->getHeaders() as $key => $val) {
63             $caseKey = implode('-', array_map('ucfirst', explode('-', $key)));
64             $command .= ' -H ' . escapeshellarg($caseKey . ': ' . $val);
65         }
66
67         $postParams = $this->req->getPostParams();
68
69         if (count($this->uploadsInfo) == 0) {
70             foreach ($postParams as $k => $v) {
71                 if (!is_array($v)) {
72                     $command .= ' -d ' . escapeshellarg($k . '=' . $v);
73                 } else {
74                     foreach ($v as $ak => $av) {
75                         $command .= ' -d ' . escapeshellarg(
76                             $k . '[' . $ak . ']=' . $av
77                         );
78                     }
79                 }
80             }
81         } else {
82             foreach ($postParams as $k => $v) {
83                 $command .= ' -F ' . escapeshellarg($k . '=' . $v);
84             }
85             foreach ($this->uploadsInfo as $fieldName => $filename) {
86                 if (!is_array($filename)) {
87                     $command .= ' -F ' . escapeshellarg(
88                         $fieldName . '=@' . $filename
89                     );
90                 } else {
91                     foreach ($filename as $k => $realFilename) {
92                         $command .= ' -F ' . escapeshellarg(
93                             $fieldName . '[' . $k . ']=@' . $realFilename
94                         );
95                     }
96                 }
97             }
98         }
99
100         $command .= ' ' . escapeshellarg((string) $this->req->getUrl());
101
102         echo $command . "\n";
103     }
104 }