44894c572124a5662042145855eb7d9283129520
[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                 $command .= ' -d ' . escapeshellarg($k . '=' . $v);
72             }
73         } else {
74             foreach ($postParams as $k => $v) {
75                 $command .= ' -F ' . escapeshellarg($k . '=' . $v);
76             }
77             foreach ($this->uploadsInfo as $fieldName => $filename) {
78                 if (!is_array($filename)) {
79                     $command .= ' -F ' . escapeshellarg(
80                         $fieldName . '=@' . $filename
81                     );
82                 } else {
83                     foreach ($filename as $k => $realFilename) {
84                         $command .= ' -F ' . escapeshellarg(
85                             $fieldName . '[' . $k . ']=@' . $realFilename
86                         );
87                     }
88                 }
89             }
90         }
91
92         $command .= ' ' . escapeshellarg((string) $this->req->getUrl());
93
94         echo $command . "\n";
95     }
96 }