10 protected $sendAsJson = false;
11 protected $directUpload = false;
12 protected $uploadsInfo = [];
13 protected $dedicatedBody = false;
15 protected $properties = [];
16 protected $type = null;
17 protected $action = null;
18 protected $url = null;
20 public function __construct($host, $cfg)
24 $this->req = $this->getHttpRequest(
25 $this->host->endpoints->micropub, $this->host->token
29 protected function getHttpRequest($url, $accessToken)
31 $req = new MyHttpRequest2($url, 'POST');
32 $req->setHeader('User-Agent: shpub');
33 if (version_compare(PHP_VERSION, '5.6.0', '<')) {
34 //correct ssl validation on php 5.5 is a pain, so disable
35 $req->setConfig('ssl_verify_host', false);
36 $req->setConfig('ssl_verify_peer', false);
38 $req->setHeader('Content-type', 'application/x-www-form-urlencoded');
39 $req->setHeader('authorization', 'Bearer ' . $accessToken);
43 public function send($body = null)
45 if ($this->sendAsJson) {
48 throw new \Exception('body already defined');
50 $this->req->setHeader('Content-Type: application/json');
52 if ($this->action !== null) {
53 $data['action'] = $this->action;
55 if ($this->url !== null) {
56 $data['url'] = $this->url;
58 if ($this->type !== null) {
59 $data['type'] = array('h-' . $this->type);
61 if (count($this->properties)) {
62 $data['properties'] = $this->properties;
64 $body = json_encode($data);
67 if ($this->type !== null) {
68 $this->req->addPostParameter('h', $this->type);
70 if ($this->action !== null) {
71 $this->req->addPostParameter('action', $this->action);
73 if ($this->url !== null) {
74 $this->req->addPostParameter('url', $this->url);
76 foreach ($this->properties as $propkey => $propval) {
77 if (isset($propval['html'])) {
78 //workaround for content[html]
79 $propkey = $propkey . '[html]';
80 $propval = $propval['html'];
81 } else if (count($propval) == 1) {
82 $propval = reset($propval);
84 $this->req->addPostParameter($propkey, $propval);
89 $this->dedicatedBody = true;
90 $this->req->setBody($body);
92 if ($this->cfg->debug) {
93 $cp = new CurlPrinter();
94 $cp->show($this->req, $this->uploadsInfo, $this->dedicatedBody);
96 $res = $this->req->send();
98 if (intval($res->getStatus() / 100) != 2) {
99 $this->displayErrorResponse($res);
104 protected function displayErrorResponse($res)
107 'Server returned an error status code ' . $res->getStatus()
111 if ($res->getHeader('content-type') == 'application/json') {
112 $errData = json_decode($res->getBody());
113 if (!isset($errData->error)) {
114 Log::err('Error response does not contain "error" property');
115 } else if (isset($errData->error_description)) {
116 Log::err($errData->error . ': ' . $errData->error_description);
119 Log::err($errData->error);
125 Log::err($res->getBody());
130 public function setAction($action)
132 $this->action = $action;
135 public function setType($type)
140 public function setUrl($url)
146 * @param string $fieldName name of file-upload field
147 * @param array $fileNames list of local file paths
149 public function addUpload($fieldName, $fileNames)
151 if ($this->directUpload && $this->sendAsJson) {
152 throw new \Exception(
153 'Cannot do direct upload with JSON requests'
157 if ($this->host->endpoints->media === null
158 || $this->directUpload
160 if ($this->sendAsJson) {
161 throw new \Exception(
162 'No media endpoint available, which is required for JSON'
165 if (count($fileNames) == 1) {
166 $fileNames = reset($fileNames);
168 $this->uploadsInfo[$fieldName] = $fileNames;
169 return $this->req->addUpload($fieldName, $fileNames);
173 foreach ($fileNames as $fileName) {
174 $urls[] = $this->uploadToMediaEndpoint($fileName);
176 if (count($urls) == 1) {
177 $urls = reset($urls);
179 $this->addProperty($fieldName, $urls);
183 * @return string URL at media endpoint
185 public function uploadToMediaEndpoint($fileName)
187 $httpReq = $this->getHttpRequest(
188 $this->host->endpoints->media, $this->host->token
190 $httpReq->addUpload('file', $fileName);
192 if ($this->cfg->debug) {
193 $cp = new CurlPrinter();
194 $cp->show($httpReq, ['file' => $fileName]);
196 $res = $httpReq->send();
197 if (intval($res->getStatus() / 100) != 2) {
199 'Media endpoint returned an error status code '
202 Log::err($res->getBody());
206 $location = $res->getHeader('location');
207 if ($location === null) {
208 Log::err('Media endpoint did not return a URL');
212 $base = new \Net_URL2($this->host->endpoints->media);
213 return (string) $base->resolve($location);
216 public function addContent($text, $isHtml)
220 'content', ['html' => $text]
223 $this->addProperty('content', $text);
229 * Adds a micropub property to the request.
231 * @param string $key Parameter name
232 * @param string|array $values One or multiple values
234 public function addProperty($key, $values)
236 $this->properties[$key] = (array) $values;
239 public function setSendAsJson($json)
241 $this->sendAsJson = $json;
244 public function setDirectUpload($directUpload)
246 $this->directUpload = $directUpload;