Check-in [dd450f3109]
Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Workaround for Content-Location: header, add h: and -cap: attrs, fix [scope] unwrapping, accept mime types, refold properties[] into strings, correct error type for invalid token. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
dd450f3109ce609b64d91e48dd32ce48 |
User & Date: | mario 2021-04-10 22:47:31 |
Context
2021-04-14
| ||
07:22 | Basic error-to-ticket conversion backend. check-in: d3dbeea9f5 user: mario tags: trunk | |
2021-04-10
| ||
22:47 | Workaround for Content-Location: header, add h: and -cap: attrs, fix [scope] unwrapping, accept mime types, refold properties[] into strings, correct error type for invalid token. check-in: dd450f3109 user: mario tags: trunk | |
22:45 | charset= instead of encoding= check-in: d059f0486e user: mario tags: trunk | |
Changes
Changes to extroot/micropub.
1 2 3 4 5 6 7 8 | #!/usr/bin/php-cgi -dcgi.force_redirect=0 <?php # encoding: utf-8 # api: cgi # type: json # category: api # title: MicroPub API # description: Accepts blog/ticket/chat entries from micropub clients | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #!/usr/bin/php-cgi -dcgi.force_redirect=0 <?php # encoding: utf-8 # api: cgi # type: json # category: api # title: MicroPub API # description: Accepts blog/ticket/chat entries from micropub clients # version: 0.2 # state: incomplete # depends: php:sqlite # doc: https://micropub.spec.indieweb.org/#add, # https://github.com/indieweb/micropub-extensions/issues # config: - # # Supposed to feed micropub requests back into `fossil`. Currently supporting |
︙ | ︙ | |||
34 35 36 37 38 39 40 41 42 43 44 45 46 47 | # # Another limitation of fossils cgi handling: # ------------------------------------------ # ยท any presence of a Location: header will corrupt the response status # and body. # ยท hence using Content-Location: instead (= not micropub-compliant) # ยท not sure it can be changed using mod_headers `Header edit*` etc. # # MicroPub protocol # ----------------- # # Becomes a technote: # # POST .../ext/micropub HTTP/1.1 | > > | 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | # # Another limitation of fossils cgi handling: # ------------------------------------------ # ยท any presence of a Location: header will corrupt the response status # and body. # ยท hence using Content-Location: instead (= not micropub-compliant) # ยท not sure it can be changed using mod_headers `Header edit*` etc. # # Header add Location "expr=%{resp:Content-Location}" # # MicroPub protocol # ----------------- # # Becomes a technote: # # POST .../ext/micropub HTTP/1.1 |
︙ | ︙ | |||
97 98 99 100 101 102 103 | # also used for internal mapping of mf2 types onto fossil commands (hyphen in `-map` indicates internal use) $map = [ [ "type" => "note", # should also be 'h-entry' really, but this simplifies mapping wo/ checking for supplied properties "name" => "Blog/Technote", "h" => "entry", "-map" => "technote", | | > | 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | # also used for internal mapping of mf2 types onto fossil commands (hyphen in `-map` indicates internal use) $map = [ [ "type" => "note", # should also be 'h-entry' really, but this simplifies mapping wo/ checking for supplied properties "name" => "Blog/Technote", "h" => "entry", "-map" => "technote", "-cap" => "[bick]", "accepts-media" => TRUE, "properties" => [ [ "name" => "content", "label" => "Content", "type" => "textarea markdown", "value" => "## Note\n...", "required" => true, |
︙ | ︙ | |||
135 136 137 138 139 140 141 | "name" => "photo", "type" => "file", "accepts" => "image/*", "is-media" => true, "required" => false, ] ], | < > > > < < > > > | 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | "name" => "photo", "type" => "file", "accepts" => "image/*", "is-media" => true, "required" => false, ] ], ], [ "type" => "entry", "name" => "Wiki page", "h" => "entry", "-map" => "wiki", "-cap" => "[kmf]", "accepts-media" => TRUE, "properties" => [ [ "name" => "content", "label" => "Content", "type" => "textarea markdown", "value" => "## Note\n...", "required" => true, "-map" => "stdin", ], [ "name" => "title", "label" => "Title", "type" => "text", "pladeholder" => "PageTitle", "required" => true, ], [ "name" => "attachment", "label" => "Attachment", "type" => "file", "accepts" => "*/*", "is-media" => true, "required" => false, ], ], ], [ "type" => "issue", "name" => "Ticket", "h" => "entry", "-map" => "ticket", "-cap" => "n", "accepts-media" => FALSE, "properties" => [ [ "name" => "content", "label" => "Content", "type" => "textarea markdown", "placeholder" => "## Note\n...", "required" => true, |
︙ | ︙ | |||
220 221 222 223 224 225 226 | ], [ "name" => "contact", "type" => "text email url", "-map" => "private_contact", ], ], | < > > < | | | | | 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | ], [ "name" => "contact", "type" => "text email url", "-map" => "private_contact", ], ], ], [ "type" => "event", "name" => "Chat", "h" => "entry", "-map" => "chat", "-cap" => "C", "accepts-media" => TRUE, "properties" => [ [ "name" => "content", "label" => "Message", "type" => "text", "required" => true, "-map" => "content", ], [ "name" => "photo", "label" => "Image attachmennt", "type" => "file", "accepts" => "image/*", "is-media" => true, "required" => false, ], ], ], ]; #-- database (== fossil repo) function db($sql="", $params=[]) { static $db; if (empty($db)) { $db = new PDO("sqlite::memory:"); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $db->query("ATTACH DATABASE '$_SERVER[FOSSIL_REPOSITORY]' AS 'repo'"); } if ($params) { $stmt = $db->prepare($sql); $stmt->execute($params); return $stmt->fetchAll(PDO::FETCH_ASSOC); } else { return $db->query($sql); } } #-- JSON/form-encoded output function json_response($r, $status=false, $always_json=True) { if ($status) { header("Status: $status"); if (is_string($r)) { $r = ["error"=>"invalid_request", "error_description"=>$r]; } } if (preg_match("~^(?!.+/json).+/x-www-form~i", $_SERVER["HTTP_ACCEPT"])) { header("Content-Type: application/x-www-form-urlencoded"); array_walk($r, function(&$v, $k) { $v = "$k=" . urlencode($v); }); die(join("&", $r)); } elseif (preg_match("~json/|[/+]json~i", $_SERVER["HTTP_ACCEPT"]) || $always_json) { header("Content-Type: application/json; charset=utf-8"); die(json_encode($r, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)); } else { header("Content-Type: text/php-source"); die(var_export($r, True)); } } |
︙ | ︙ | |||
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | $code = $access_token ?: bearer(); return get_token_by_code($code)[0]; } #-- alias for generic types to fossil items function scope_map($type) { global $map; $type = preg_replace("/^\w-/", "", $type); foreach ($map as $row) { if ($row["type"] == $type) { return $row["-map"]; } } return $type; } # check if request action+type entry all match token scopes function verify_scope($token=[], $req_scopes=[]) { preg_match_all("/(\w+)/", $token["scope"], $allowed); | > | > | | | | > > > > > > > > > > > > > > | 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | $code = $access_token ?: bearer(); return get_token_by_code($code)[0]; } #-- alias for generic types to fossil items function scope_map($type) { global $map; if (is_array($type) && count($type)) { $type = $type[0]; } $type = preg_replace("/^\w-/", "", $type); foreach ($map as $row) { if ($row["type"] == $type) { return $row["-map"]; } } return $type; } # check if request action+type entry all match token scopes function verify_scope($token=[], $req_scopes=[]) { preg_match_all("/(\w+)/", $token["scope"], $allowed); $allowed = array_map("scope_map", $allowed[1]); $req_scopes = array_map("scope_map", array_filter($req_scopes)); #var_dump("TOKEN=", $token, " ALLOWED=", $allowed, " REQ=", $req_scopes); # comapare against $allowed token scope list $ok = NULL; foreach ($req_scopes as $t) { $ok = ($a !== False) && in_array($t, $allowed); } return $ok; } #-- transform $_POST to JSON body function request() { if (preg_match("~^\s*(\w+/json|json/\w+)(\+json)?\s*(;|$)~i", $_SERVER["CONTENT_TYPE"])) { $json = json_decode(file_get_contents("php://input"), True); } elseif (count($_POST)) { $json = [ "q" => "$_REQUEST[q]", "action" => "$_POST[action]", "type" => ["h-$_POST[h]"], "properties" => array_map(function ($v) { return [$v]; }, $_POST), ]; } return $_POST = $json; } #-- transform post to fossil command class create { public $token = []; public $url = ""; public $tmpfn = ""; # dispatch `type` onto functions function __construct($post, $token) { db("DETACH DATABASE 'repo';"); $this->token = $token; $action = $post["action"]; $type = scope_map($post["type"]); if (empty($post["properties"]["content"])) { json_response(["properties.content[] required", $post], "400 Basics"); } elseif (method_exists($this, $type)) { $this->fold($post["properties"]); $stdout = call_user_func([$this, $type], $post["properties"]); //if ($this->tmpfn) { unlink($this->tmpfn); } if (!$this->url) { $this->new_url("", $stdout); } //die("back in main, $this->url"); if ($this->url) { $this->redirect201($this->url); } else { json_response(["result"=>$stdout], "500 Oh noes, something went wrong"); } } else json_response("Unknown h-type: `$type`", "400 No"); } function fold(&$arr) { foreach ($arr as $k=>&$v) { if (in_array($k, ["category", "tag"])) { continue; } elseif ($k == "content") { $v = implode("\n", $v); } else { $v = implode(", ", $v); } } } function redirect201($url) { # fossil workaround: use C-L: header("Status: 201 Created"); header("Location: {$this->url}", True, 201); header("Content-Location: {$this->url}"); header("Content-Type: text/html"); die("<html><head><meta http-equiv=Location value=\"$this->url\"></head></html>"); |
︙ | ︙ | |||
426 427 428 429 430 431 432 | # h-note = technote function technote($p, $act="create") { # &title=|&summary= # &content= # &category[]= $args = [ "wiki", "$act", | | < | | < | | 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 | # h-note = technote function technote($p, $act="create") { # &title=|&summary= # &content= # &category[]= $args = [ "wiki", "$act", $p["title"] ?: $p["summary"] ?: $this->excerpt($p["content"]), "--technote", ($this->id ?: "now"), "--mimetype", "text/x-markdown" ]; if ($p["category"]) { $args[] = "--technote-tags"; $args[] = implode(",", $p["category"]); } list($r, $null) = [ $this->fossil($args, $p["content"]), $this->add_attachments(["attachment", "add", "%f", "-t", "now"]) ]; return $r; } # h-entry = wiki function wiki($p, $act="create") { # &title= # &content= $title = $this->id ?: $p["title"] ?: $this->excerpt($p["content"]); $args = ["wiki", "$act", "--mimetype", "text/x-markdown", $title]; list($r, $null) = [ $this->fossil($args, $p["content"]), $this->add_attachments(["attachment", "add", $title, "%f"]) ]; return $r; } # h-issue = ticket function ticket($p, $args=["ticket", "add"]) { foreach ($this->map_for("issue")["properties"] as $def) { if ($p[$def["name"]]) { |
︙ | ︙ | |||
501 502 503 504 505 506 507 508 509 510 511 512 513 514 | ["-R", create::esc($_SERVER["FOSSIL_REPOSITORY"])], ); $cmd = implode(" ", $args) . " 2>&1"; if ($input) { $cmd = "echo " . create::esc($input) . " | $cmd"; } #print_r($cmd); exec($cmd, $stdout, $errno); header("X-Debug: $errno: " . implode("//", $stdout)); if ($errno) { json_response(["errno"=>$errno, "stdout"=>$stdout], "500 err"); } return implode("\n", $stdout); } | > | 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | ["-R", create::esc($_SERVER["FOSSIL_REPOSITORY"])], ); $cmd = implode(" ", $args) . " 2>&1"; if ($input) { $cmd = "echo " . create::esc($input) . " | $cmd"; } #print_r($cmd); #exit(); exec($cmd, $stdout, $errno); header("X-Debug: $errno: " . implode("//", $stdout)); if ($errno) { json_response(["errno"=>$errno, "stdout"=>$stdout], "500 err"); } return implode("\n", $stdout); } |
︙ | ︙ | |||
570 571 572 573 574 575 576 577 578 579 | # ?q=actions class query { function __construct($token=null) { json_response(call_user_func([$this, "$_GET[q]"], $token)); } function config() { return [ "media-endpoint" => "https://$_SERVER[SERVER_NAME]$_SERVER[PHP_SELF]", # same "syndicate-to" => [], | > > > > > > > > > | | > > | < | > > > | 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 | # ?q=actions class query { function __construct($token=null) { json_response(call_user_func([$this, "$_GET[q]"], $token)); } function config() { global $map; foreach ($map as &$type) { $type["required-properties"] = []; $type["accepts-media"] = False; foreach ($type["properties"] as $prop) { if ($prop["required"]) { $type["required-properties"][] = $prop["name"]; } $type["accepts-media"] |= $prop["type"] == "file"; } } return [ "media-endpoint" => "https://$_SERVER[SERVER_NAME]$_SERVER[PHP_SELF]", # same "syndicate-to" => [], "q" => [ "config", "source" ], "extensions" => [ "unprefixed-type", "bug-local-auth-token-only", #"bug-me-auth-field", "bug-detached-auth-token-micropub", "http-link-rel-micropub", "http-content-location", "http-mime-major-json", "post-types", "post-types-h", "post-types-properties-dict-input", #"post-types-properties-list", ], "post-types" => $map, ]; } function properties($token) { return array_unique(array_merge(array_map(function($t) { return $t["properties"]; }, $this->config["post-types"]))); } function source($token) { new source($_GET, $token); } } #-- run |
︙ | ︙ | |||
616 617 618 619 620 621 622 | # realpath(tempnam("/tmp/", "fossil-micropub-php-")) # systemd PrivateTmp ); } elseif (!empty($_REQUEST["q"]) and $_REQUEST["q"] == "config") { new query(); } elseif (!$token = get_token($_REQUEST["access_token"])) { | | | 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 | # realpath(tempnam("/tmp/", "fossil-micropub-php-")) # systemd PrivateTmp ); } elseif (!empty($_REQUEST["q"]) and $_REQUEST["q"] == "config") { new query(); } elseif (!$token = get_token($_REQUEST["access_token"])) { json_response([ "error"=>"unauthorized", "error_description"=>"token expired/invalid"], "401 Token expired"); } elseif (!verify_scope($token, [$_POST["action"], $_POST["type"]])) { #print_r($_POST); json_response("scope insufficient", "403 Scope insufficient"); } elseif (!empty($_GET["q"])) { new query($token); |
︙ | ︙ |