Add rsvp command
[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     protected $dedicatedBody = false;
11
12     public function __construct($host, $cfg)
13     {
14         $this->cfg = $cfg;
15         $this->req = new MyHttpRequest2($host->endpoints->micropub, 'POST');
16         $this->req->setHeader('User-Agent: shpub');
17         if (version_compare(PHP_VERSION, '5.6.0', '<')) {
18             //correct ssl validation on php 5.5 is a pain, so disable
19             $this->req->setConfig('ssl_verify_host', false);
20             $this->req->setConfig('ssl_verify_peer', false);
21         }
22         $this->req->setHeader('Content-type', 'application/x-www-form-urlencoded');
23         $this->req->setHeader('authorization', 'Bearer ' . $host->token);
24     }
25
26     public function send($body = null)
27     {
28         if ($body !== null) {
29             $this->dedicatedBody = true;
30             $this->req->setBody($body);
31         }
32         if ($this->cfg->debug) {
33             $this->printCurl();
34         }
35         $res = $this->req->send();
36
37         if (intval($res->getStatus() / 100) != 2) {
38             Log::err(
39                 'Server returned an error status code ' . $res->getStatus()
40             );
41             Log::err($res->getBody());
42             exit(11);
43         }
44         return $res;
45     }
46
47     /**
48      * @param string                $fieldName    name of file-upload field
49      * @param string|resource|array $filename     full name of local file,
50      *               pointer to open file or an array of files
51      */
52     public function addUpload($fieldName, $filename)
53     {
54         $this->uploadsInfo[$fieldName] = $filename;
55         return $this->req->addUpload($fieldName, $filename);
56     }
57
58     /**
59      * Add one or multiple POST parameters.
60      * Automatically adds them as array or as string.
61      *
62      * @param string       $key    Parameter name
63      * @param string|array $values One or multiple values
64      */
65     public function addPostParameter($key, $values)
66     {
67         if (count($values) == 1) {
68             $values = reset($values);
69         }
70         $this->req->addPostParameter($key, $values);
71     }
72
73     protected function printCurl()
74     {
75         $command = 'curl';
76         if ($this->req->getMethod() != 'GET') {
77             $command .= ' -X ' . $this->req->getMethod();
78         }
79         foreach ($this->req->getHeaders() as $key => $val) {
80             $caseKey = implode('-', array_map('ucfirst', explode('-', $key)));
81             $command .= ' -H ' . escapeshellarg($caseKey . ': ' . $val);
82         }
83
84         $postParams = $this->req->getPostParams();
85
86         if (count($this->uploadsInfo) == 0) {
87             foreach ($postParams as $k => $v) {
88                 if (!is_array($v)) {
89                     $command .= ' -d ' . escapeshellarg($k . '=' . $v);
90                 } else {
91                     foreach ($v as $ak => $av) {
92                         $command .= ' -d ' . escapeshellarg(
93                             $k . '[' . $ak . ']=' . $av
94                         );
95                     }
96                 }
97             }
98         } else {
99             foreach ($postParams as $k => $v) {
100                 $command .= ' -F ' . escapeshellarg($k . '=' . $v);
101             }
102             foreach ($this->uploadsInfo as $fieldName => $filename) {
103                 if (!is_array($filename)) {
104                     $command .= ' -F ' . escapeshellarg(
105                         $fieldName . '=@' . $filename
106                     );
107                 } else {
108                     foreach ($filename as $k => $realFilename) {
109                         $command .= ' -F ' . escapeshellarg(
110                             $fieldName . '[' . $k . ']=@' . $realFilename
111                         );
112                     }
113                 }
114             }
115         }
116
117         if ($this->dedicatedBody) {
118             $command .= ' --data ' . escapeshellarg($this->req->getBody());
119         }
120
121         $command .= ' ' . escapeshellarg((string) $this->req->getUrl());
122
123         Log::msg($command);
124     }
125 }