This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
nanopub.php
732 lines (652 loc) · 27.7 KB
/
nanopub.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
<?php
/**
* nanopub - MicroPub support for Static Blog Engine
*
* PHP version 7
*
* @author Daniel Goldsmith <[email protected]>
* @copyright © 2017-2019 Daniel Goldsmith <[email protected]>
* @license BSD 3-Clause Clear Licence
* @link https://github.com/dg01d/nanopub
* @category Micropub
* @version 2.0.0
*
* SPDX-License-Identifier: BSD-3-Clause-Clear
*
*/
require 'vendor/autoload.php';
require 'helpers.php';
use GuzzleHttp\Client;
use Forecast\Forecast;
use Symfony\Component\Yaml\Yaml;
/**
* Load the settings from the configuration file
*/
$configs = include 'configs.php';
$siteUrl = $configs->siteUrl;
$siteFeed = $configs->siteFeed;
$weatherToggle = $configs->weatherToggle;
date_default_timezone_set($configs->timezone);
define("FRONT", $configs->frontFormat);
$udate = date('U', time());
$cdate = date('c', time());
$xray = new p3k\XRay();
/**
* API call function. This could easily be used for any modern writable API
*
* @param string $url adressable url of the external API
* @param string $auth authorisation header for the API
* @param array $adata php array of the data to be sent
*
* @return HTTP response from API
*/
function post_to_api($url, $auth, $adata)
{
$fields = '';
foreach ($adata as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($adata));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
$post, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Authorization: '.$auth
)
);
$result = curl_exec($post);
curl_close($post);
return $result;
}
/**
* Replaces getallheaders() for nginx
*
* Replaces the getallheaders function which relies on Apache
*
* @return array incoming headers from _POST
*/
if (!function_exists('getallheaders')) {
function getallheaders()
{
$headers = [];
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
/**
* Test for associative arrays
*
* @param array $array to be tested
*
* @return boolean true if associative
*/
function isAssoc($array)
{
$array = array_keys($array);
return ($array !== array_keys($array));
}
/**
* Validate incoming requests, using IndieAuth
*
* This section largely adopted from rhiaro
*
* @param array $headers All headers from an incoming connection request
*
* @return boolean true if authorised
*/
function indieAuth($headers)
{
global $configs;
/**
* Check token is valid
*/
$token = $headers['authorization'];
$ch = curl_init($configs->tokenPoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
$ch, CURLOPT_HTTPHEADER, Array(
//"Content-Type: application/x-www-form-urlencoded",
"Accept: application/json",
"Authorization: $token"
)
);
$responseBody = strval(curl_exec($ch));
curl_close($ch);
$response = json_decode($responseBody, true, 2);
if (!is_array($response) || json_last_error() !== \JSON_ERROR_NONE) {
parse_str($responseBody, $response);
}
$scopes = isset($response['scope']) ? explode(' ', $response['scope']) : array();
if (empty($response) || isset($response['error'])) {
header("HTTP/1.1 401 Unauthorized");
echo 'The request lacks authentication credentials';
exit;
} elseif (!isset($response['me']) || $response['me'] !== $configs->siteUrl) {
header("HTTP/1.1 401 Unauthorized");
echo 'The request lacks valid authentication credentials';
exit;
} elseif (!in_array('create', $scopes) && !in_array('post', $scopes)) {
header("HTTP/1.1 403 Forbidden");
echo 'Client does not have access to this resource';
exit;
} else {
return true;
}
}
/**
* Function to replace keys in an array with values from a different one.
*
* Used here to rewrite keys from Hugo's format to microformats2 syntax.
*
* @param array $array the array of Hugo key => value frontmatter elements
* @param array $keys an associative array, pairing key values
* @param boolean $filter boolean switch, if true, values not present in $keys are removed
*
* @return array associative with keys in mf2 values
*/
function array_replace_keys($array, $keys, $filter)
{
$newArray = array();
foreach ($array as $key => $value) {
if (isset($keys[$key])) {
$newArray[$keys[$key]] = $value;
} elseif (!$filter) {
$newArray[$key] = $value;
}
}
return $newArray;
}
/**
* Reads existing Hugo files and rearranges them to the
* format required by the micropub specification.
*
* @param string $textFile the content file, with json/yaml frontmatter
* @param array $mfArray Array of SSG <=> mf2 field equivalents
* @param boolean $bool boolean to determine if non-equivalent keys are stripped
*
* @return array structured array from a text file with json frontmatter
*/
function decode_input($textFile, $mfArray, $bool)
{
$topArray = explode("\n\n", $textFile);
if (FRONT == "yaml") {
$fileArray = Yaml::parse($topArray[0]);
} else {
$fileArray = json_decode($topArray[0], true);
}
$fileArray["content"] = rtrim($topArray[1]);
$newArray = array();
/*
* All values must be arrays in mf2 syntax
*/
foreach ($fileArray as $key => $value) {
if (!is_array($value)) {
$value = [$value];
}
$newArray[$key] = $value;
}
$newArray = array_replace_keys($newArray, $mfArray, $bool);
return $newArray;
}
/**
* Rewrites micropub-compliant structure as a Hugo file.
*
* @param array $array array of mf2-compliant fieldnames
* @param array $mfArray array of SSG <=> mf2 field equivalents
* @return array $postarray with SSG fieldnames
*/
function recode_output($array, $mfArray)
{
$postArray = array();
// These cannot be arrays in Hugo syntax, but are in mf2
$singles = array("name", "published", "slug", "content");
foreach ($array as $key => $value) {
if (in_array($key, $singles)) {
$value = $value[0];
}
$postArray[$key] = $value;
}
$postArray = array_replace_keys($postArray, $mfArray, false);
return $postArray;
}
/**
* Writes dataset to file.
* @since 1.1
* Put here to allow extension for different post-types in future.
*
* @param array $frontmatter Frontmatter for the file (e.g. title, slug)
* @param string $content The content of the file/post/reply
* @param string $fn The filename of the file to be written
*/
function write_file($frontmatter, $content, $fn)
{
$frontmatter = array_filter($frontmatter);
if (FRONT == "yaml") {
$yaml = Yaml::dump($frontmatter);
$frontFinal = "---\n" . $yaml . "---\n\n";
} else {
$frontFinal = json_encode($frontmatter, $jsonFormat) . "\n\n";
}
$dir = pathinfo($fn, PATHINFO_DIRNAME);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
file_put_contents($fn, $frontFinal);
file_put_contents($fn, $content, FILE_APPEND | LOCK_EX);
}
// This variable is used for the json_encode() functions later in the script.
// You can change these depending on your needs.
$jsonFormat = JSON_PRETTY_PRINT | JSON_NUMBER_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
// This array pairs Hugo namespace with mf2 namespace.
$mfArray = array(
"date" => "published",
"tags" => "category",
"replyto" => "in-reply-to",
"link" => "bookmark-of",
"title" => "name",
"content" => "content"
);
// GET Requests:- config, syndicate to & source
// Tell Micropub clients where I can syndicate to
if (isset($_GET['q']) && $_GET['q'] == "syndicate-to") {
$array = array(
"syndicate-to" => array(
0 => array(
"uid" => "https://".$configs->mastodonInstance,
"name" => "Mastodon"
)
)
);
header('Content-Type: application/json');
echo json_encode($array, $jsonFormat);
exit;
}
// Offer micropub clients full configuration
if (isset($_GET['q']) && $_GET['q'] == "config") {
$array = array(
"media-endpoint" => $configs->mediaPoint,
"syndicate-to" => array(
0 => array(
"uid" => "https://".$configs->mastodonInstance,
"name" => "Mastodon"
)
)
);
header('Content-Type: application/json');
echo json_encode($array, $jsonFormat);
exit;
}
// Take headers and other incoming data
$headers = getallheaders();
if ($headers === false ) {
header("HTTP/1.1 400 Bad Request");
echo 'The request lacks valid headers';
exit;
}
$headers = array_change_key_case($headers, CASE_LOWER);
$data = array();
if (!empty($_POST['access_token'])) {
$token = "Bearer ".$_POST['access_token'];
$headers["authorization"] = $token;
}
if (isset($_POST['h'])) {
$h = $_POST['h'];
unset($_POST['h']);
$data = [
'type' => ['h-'.$h],
'properties' => array_map(
function ($a) {
return is_array($a) ? $a : [$a];
}, $_POST
)
];
} else {
$data = json_decode(file_get_contents('php://input'), true);
}
// Offer micropub clients source material
if (isset($_GET['q']) && $_GET['q'] == 'source') {
// As this is requesting the source of a post,
// seek indieAuth validation of the request
if (indieAuth($headers)) {
if (!empty($_GET['url'])) {
$subj = urldecode($_GET['url']);
$pattern = "#$siteUrl#";
$repl = "";
$srcUri = preg_replace($pattern, $repl, $subj);
$srcUri = rtrim($srcUri, "/");
if ($textFile = file_get_contents($configs->storageFolder . "/$srcUri.md")) {
//send file for decoding
$jsonArray = decode_input($textFile, $mfArray, true);
$respArray = array (
"type" => ["h-entry"],
"properties" => $jsonArray
);
header('Content-Type: application/json');
echo json_encode($respArray, 128);
exit;
} else {
header("HTTP/1.1 404 Not Found");
echo "Source file not found";
exit;
}
}
}
}
if (!empty($data)) {
if (indieAuth($headers)) {
if (empty($data['properties']['content']['0'])
&& empty($data['properties']['like-of']['0'])
&& empty($data['properties']['repost-of']['0'])
&& empty($data['properties']['checkin']['0']['type']['0'])
) {
// If this is a POST and there's no action listed, 400 exit
if (empty($data['action'])) {
header("HTTP/1.1 400 Bad Request");
echo "Missing content";
exit;
} else {
// now we need to start getting the information for actions
if (!empty($data['action'])) {
$action = $data['action'];
$subj = urldecode($data['url']);
}
// This is based on _my_ content position. This converts
// URLs to local disk
$pattern = "#".$siteUrl."#";
$repl = "";
$srcUri = preg_replace($pattern, $repl, $subj);
$srcUri = rtrim($srcUri, "/");
// First delete if asked to
if ($action == "delete") {
if (!is_dir($configs->trashFolder)) {
mkdir($configs->trashFolder, 0777, true);
}
rename($configs->storageFolder . "/$srcUri.md", $configs->trashFolder . "/$srcUri.md");
header("HTTP/1.1 204 No Content");
exit;
}
// then an undelete
if ($action == "undelete") {
rename($configs->trashFolder . "/$srcUri.md", $configs->storageFolder . "/$srcUri.md");
header("HTTP/1.1 201 Created");
header("Location: ".$siteUrl.$srcUri);
exit;
}
// Update can be one of a number of different actions
if ($action == "update") {
// Updating, so need to read the existing file
if ($textFile = file_get_contents($configs->storageFolder . "/$srcUri.md")) {
//send file for decoding
$jsonArray = decode_input($textFile, $mfArray, false);
// Now we perform the different update actions,
// Replace being first.
if (array_key_exists("replace", $data)) {
if (is_array($data['replace'])) {
foreach ($data['replace'] as $key => $value) {
$newVal = [$key => $value];
$jsonArray = array_replace($jsonArray, $newVal);
}
} else {
header("HTTP/1.1 400 Bad Request");
echo "Value of replace key must be an array";
exit;
}
}
// Adding a value
if (array_key_exists("add", $data)) {
if (is_array($data['add'])) {
foreach ($data['add'] as $key => $value) {
$newVal = [$key => $value['0']];
$jsonArray = array_merge_recursive($jsonArray, $newVal);
}
} else {
header("HTTP/1.1 400 Bad Request");
echo "Value of add key must be an array";
exit;
}
}
// Delete a property based on key
if (array_key_exists("delete", $data)) {
if (is_array($data['delete'])) {
if (isAssoc($data['delete'])) {
foreach ($data['delete'] as $key => $value) {
$newVal = [$key => $value['0']];
$pos = array_keys($newVal)['0'];
$jsonArray[$pos] = array_diff($jsonArray[$pos], $newVal);
}
} else { // delete an overall property
$pos = $data['delete']['0'];
unset($jsonArray[$pos]);
}
} else {
header("HTTP/1.1 400 Bad Request");
echo "Value of delete key must be an array";
exit;
}
}
// Tasks completed, write back to original file
$jsonArray = recode_output($jsonArray, array_flip($mfArray));
$content = $jsonArray['content'];
unset($jsonArray['content']);
$fn = $configs->storageFolder . "/$srcUri.md";
write_file($jsonArray, $content, $fn);
header("HTTP/1.1 200 OK");
echo json_encode($jsonArray, 128);
} else {
header("HTTP/1.1 404 Not Found");
echo "That url does not exist";
}
}
}
} else {
// This handles new publications.
// Starts setting up some variables used throughout
$frontmatter = [];
// Starting with checkins. These require a lot of metadata.
// Structure is based on OwnYourSwarm's json payload
if (!empty($data['properties']['checkin'])) {
$chkProperties = $data['properties']['checkin']['0']['properties'];
if (!empty($chkProperties['url']['1'])) {
$frontmatter['checkurl'] = $chkProperties['url']['1'];
} else {
$frontmatter['checkurl'] = $chkProperties['url']['0'];
}
$frontmatter['checkloc'] = $chkProperties['name']['0'];
if ($chkProperties['locality']['0'] != $chkProperties['region']['0']) {
$frontmatter['checkadd'] = $chkProperties['locality']['0'] . ', ' . $chkProperties['region']['0'];
} else {
$frontmatter['checkadd'] = $chkProperties['street-address']['0'] . ', ' . $chkProperties['locality']['0'];
}
$frontmatter['latitude'] = $chkProperties['latitude']['0'];
$frontmatter['longitude'] = $chkProperties['longitude']['0'];
// All properties are extracted, so the checkin can be deleted
unset($data['properties']['checkin']);
// Next bit creates a map and uploads it to media endpoint
$mapname = 'images/file-'.date('YmdHis').'-'.mt_rand(1000, 9999).'.png';
$url = 'http://atlas.p3k.io/map/img?marker[]=lat:'.$frontmatter['latitude'].';lng:'.$frontmatter['longitude'].';icon:small-red-cutout&basemap=osm&attribution=none&width=600&height=240&zoom=14';
file_put_contents($mapname, file_get_contents($url));
$frontmatter['map'] = $mapname;
// Now to take out the checkins usual properties
$content = $data['properties']['content']['0'] ?? null;
unset($data['properties']['content']);
$frontmatter['checkin'] = $data['properties']['syndication']['0'];
unset($data['properties']['syndication']);
$frontmatter['date'] = $data['properties']['published']['0'];
unset($data['properties']['published']['0']);
$frontmatter['slug'] = $udate;
unset($data['properties']['access_token']);
foreach ($data['properties'] as $key => $value) {
$frontmatter[$key] = $value;
}
} else {
// Begin Processing non-checkin material
$props = $data['properties'];
unset($props['access_token']);
// Client has syndication powers!
$synds = $props['mp-syndicate-to'] ?? null;
unset($props['mp-syndicate-to']);
// Non-notes tend to have a name or title
$frontmatter['title'] = $props['name']['0'] ?? null;
unset($props['name']);
// Bookmark-of
$frontmatter['link'] = $props['bookmark-of']['0'] ?? null;
unset($props['bookmark-of']);
// First attempt at 'like-of'
$frontmatter['like_of'] = $props['like-of'] ?? null;
if (is_array($frontmatter['like_of'])) {
$frontmatter['like_of'] = $frontmatter['like_of']['0'];
}
if (isset($frontmatter['like_of'])) {
$frontmatter['like_site'] = hostname_of_uri($frontmatter['like_of']);
$url_parse = xray_machine($frontmatter['like_of'], $frontmatter['like_site']);
if ($frontmatter['like_site'] == 'twitter.com') {
$synds['0'] = "https://twitter.com";
}
}
unset($props['like-of']);
// First attempt at 'repost-of'
$frontmatter['repost_of'] = $props['repost-of'] ?? null;
if (is_array($frontmatter['repost_of'])) {
$frontmatter['repost_of'] = $frontmatter['repost_of']['0'];
}
if (isset($frontmatter['repost_of'])) {
$frontmatter['repost_site'] = hostname_of_uri($frontmatter['repost_of']);
$url_parse = xray_machine($frontmatter['repost_of'], $frontmatter['repost_site']);
}
unset($props['repost-of']);
// indieweb 'reply-to'
$frontmatter['replytourl'] = $props['in-reply-to'] ?? null;
if (is_array($frontmatter['replytourl'])) {
$frontmatter['replytourl'] = $frontmatter['replytourl']['0'];
}
if (isset($frontmatter['replytourl'])) {
$frontmatter['replysite'] = hostname_of_uri($frontmatter['replytourl']);
$url_parse = xray_machine($frontmatter['replytourl'], $frontmatter['replysite']);
}
unset($props['in-reply-to']);
// server allows client to set a slug
if (!empty($props['mp-slug']['0'])) {
$frontmatter['slug'] = $props['mp-slug']['0'];
unset($props['mp-slug']);
} elseif (!empty($frontmatter['title']['0'])) {
$frontmatter['slug'] = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $frontmatter['title'])));
} else {
$frontmatter['slug'] = $udate;
}
// Bug: Issue #8 / #7 probably linked
if (is_array($frontmatter['slug'])) {
$frontmatter['slug'] = $frontmatter['slug']['0'];
}
// Hugo does not store content in the frontmatter
$content = $props['content']['0'] ?? null;
unset($props['content']);
if (is_array($content)) {
$content = $content['html'];
}
$frontmatter['summary'] = $props['summary']['0'] ?? null;
unset($props['summary']);
// server allows clients to set category, treats as tags
// Todo: need to link this to content/subject in Mastodon
$frontmatter['tags'] = $props['category'] ?? null;
unset($props['category']);
// Specific logic here for OwnYourGram
$frontmatter['photo'] = $props['photo'] ?? null;
unset($props['photo']);
$frontmatter['instagram'] = (isset($props['syndication']) && in_array("https://www.instagram.com/p", $props['syndication']['0'])) ? $props['syndication']['0'] : null;
// PESOS (like OYG / OYS) already has a datestamp
$frontmatter['date'] = $props['published']['0'] ?? $cdate;
unset($props['published']);
foreach ($props as $key => $value) {
$frontmatter[$key] = $value;
}
// First Attempt at Weather Data
if ($weatherToggle) {
$weather = getWeather();
$frontmatter = array_merge($frontmatter, $weather);
}
if (isset($url_parse)) {
$frontmatter = array_merge($frontmatter, $url_parse);
}
}
/* First established the type of Post in nested order bookmark->article->note
* Note that this is hardcoded to my site structure and post-kinds. Obviously,
* $fn will need to be changed for different structures/kinds
*/
if (!empty($frontmatter['title'])) {
// File locations are specific to my site for now.
if (!empty($frontmatter['link'])) {
$fn = $configs->storageFolder . "/link/" . $frontmatter['slug'] . ".md";
$canonical = $configs->siteUrl . "link/" . $frontmatter['slug'];
$synText = $frontmatter['title'];
} else {
$fn = $configs->storageFolder . "/article/" . $frontmatter['slug'] . ".md";
$canonical = $configs->siteUrl . "article/" . $frontmatter['slug'];
$synText = $frontmatter['title'];
}
} else {
if (!empty($frontmatter['repost_of'])) {
$fn = $configs->storageFolder . "/like/" . $frontmatter['slug'] . ".md";
$canonical = $configs->siteUrl . "like/" . $frontmatter['slug'];
$synText = $content;
} elseif (!empty($frontmatter['like_of'])) {
$fn = $configs->storageFolder . "/like/" . $frontmatter['slug'] . ".md";
$canonical = $configs->siteUrl . "like/" . $frontmatter['slug'];
$synText = $content;
} else {
$fn = $configs->storageFolder . "/micro/" . $frontmatter['slug'] . ".md";
$canonical = $configs->siteUrl . "micro/" . $frontmatter['slug'];
$synText = $content;
}
}
// Syndication Posting to different services
// first Mastodon, count limit 500
if (!empty($synds)) {
if (in_array("https://".$configs->mastodonInstance, $synds)) {
// this allows you to post to mastodon under a CW, using tags
$MastodonSubject = implode(", ", $frontmatter['tags']);
$MastodonText = str_replace("\'", "'", $synText);
$MastodonText = str_replace("\"", "\"", $MastodonText);
$MastodonText = urlencode($MastodonText);
$MastodonText = substr($MastodonText, 0, 450) . '… '. $canonical;
$mastodonToken = "bearer " . $configs->mastodonToken;
$mastodonUrl = "https://" . $configs->mastodonInstance . "/api/v1/statuses";
$postData = array(
"status" => $MastodonText,
"content-type" => "text/markdown",
"spoiler_text" => $MastodonSubject
);
// filters out an empty spoiler_text
$mdata = array_filter($postData);
// Calls the simple API from way back at the start
$result_mastodon = post_to_api($mastodonUrl, $mastodonToken, $mdata);
$array_mastodon = json_decode($result_mastodon, true);
// Sets up a variable linking to the toot
$frontmatter['mastodonlink'] = $array_mastodon['url'];
}
}
// All values obtained, we tidy up the array and convert to json
// Last part - writing the file to disk...
write_file($frontmatter, $content, $fn);
// Some way of triggering Site Generator needs to go in here.
// ping! First one to micro.blog
//if ($configs->pingMicro) {
// $feedArray = array ("url" => $siteFeed);
// post_to_api("https://micro.blog/ping", "null", "$feedArray");
//}
// ping! second one to switchboard
$switchArray = array ("hub.mode" => "publish", "hub.url" => $siteUrl);
post_to_api("https://switchboard.p3k.io/", "null", $switchArray);
// ... and Setting headers, return location to client.
header("HTTP/1.1 201 Created");
header("Location: ". $canonical);
}
}
}