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'] = '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) {
100 'Server returned an error status code ' . $res->getStatus()
102 Log::err($res->getBody());
108 public function setAction($action)
110 $this->action = $action;
113 public function setType($type)
118 public function setUrl($url)
124 * @param string $fieldName name of file-upload field
125 * @param array $fileNames list of local file paths
127 public function addUpload($fieldName, $fileNames)
129 if ($this->host->endpoints->media === null
130 || $this->directUpload
132 if ($this->sendAsJson) {
133 throw new \Exception(
134 'No media endpoint available, which is required for JSON'
137 if (count($fileNames) == 1) {
138 $fileNames = reset($fileNames);
140 $this->uploadsInfo[$fieldName] = $fileNames;
141 return $this->req->addUpload($fieldName, $fileNames);
145 foreach ($fileNames as $fileName) {
146 $urls[] = $this->uploadToMediaEndpoint($fileName);
148 if (count($urls) == 1) {
149 $urls = reset($urls);
151 $this->addProperty($fieldName, $urls);
155 * @return string URL at media endpoint
157 public function uploadToMediaEndpoint($fileName)
159 $httpReq = $this->getHttpRequest(
160 $this->host->endpoints->media, $this->host->token
162 $httpReq->addUpload('file', $fileName);
164 if ($this->cfg->debug) {
165 $cp = new CurlPrinter();
166 $cp->show($httpReq, ['file' => $fileName]);
168 $res = $httpReq->send();
169 if (intval($res->getStatus() / 100) != 2) {
171 'Media endpoint returned an error status code '
174 Log::err($res->getBody());
178 $location = $res->getHeader('location');
179 if ($location === null) {
180 Log::err('Media endpoint did not return a URL');
184 $base = new \Net_URL2($this->host->endpoints->media);
185 return (string) $base->resolve($location);
188 public function addContent($text, $isHtml)
192 'content', ['html' => $text]
195 $this->addProperty('content', $text);
201 * Adds a micropub property to the request.
203 * @param string $key Parameter name
204 * @param string|array $values One or multiple values
206 public function addProperty($key, $values)
208 $this->properties[$key] = (array) $values;
211 public function setSendAsJson($json)
213 $this->sendAsJson = $json;
216 public function setDirectUpload($directUpload)
218 $this->directUpload = $directUpload;