8ee7c5a25f15b37ace9527b15b15195a157267f0
[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     /**
57      * Add one or multiple POST parameters.
58      * Automatically adds them as array or as string.
59      *
60      * @param string       $key    Parameter name
61      * @param string|array $values One or multiple values
62      */
63     public function addPostParameter($key, $values)
64     {
65         if (count($values) == 1) {
66             $values = reset($values);
67         }
68         $this->req->addPostParameter($key, $values);
69     }
70
71     protected function printCurl()
72     {
73         $command = 'curl';
74         if ($this->req->getMethod() != 'GET') {
75             $command .= ' -X ' . $this->req->getMethod();
76         }
77         foreach ($this->req->getHeaders() as $key => $val) {
78             $caseKey = implode('-', array_map('ucfirst', explode('-', $key)));
79             $command .= ' -H ' . escapeshellarg($caseKey . ': ' . $val);
80         }
81
82         $postParams = $this->req->getPostParams();
83
84         if (count($this->uploadsInfo) == 0) {
85             foreach ($postParams as $k => $v) {
86                 if (!is_array($v)) {
87                     $command .= ' -d ' . escapeshellarg($k . '=' . $v);
88                 } else {
89                     foreach ($v as $ak => $av) {
90                         $command .= ' -d ' . escapeshellarg(
91                             $k . '[' . $ak . ']=' . $av
92                         );
93                     }
94                 }
95             }
96         } else {
97             foreach ($postParams as $k => $v) {
98                 $command .= ' -F ' . escapeshellarg($k . '=' . $v);
99             }
100             foreach ($this->uploadsInfo as $fieldName => $filename) {
101                 if (!is_array($filename)) {
102                     $command .= ' -F ' . escapeshellarg(
103                         $fieldName . '=@' . $filename
104                     );
105                 } else {
106                     foreach ($filename as $k => $realFilename) {
107                         $command .= ' -F ' . escapeshellarg(
108                             $fieldName . '[' . $k . ']=@' . $realFilename
109                         );
110                     }
111                 }
112             }
113         }
114
115         $command .= ' ' . escapeshellarg((string) $this->req->getUrl());
116
117         Log::msg($command);
118     }
119 }