forked from dg01d/nanopub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanopub.php
783 lines (697 loc) · 30 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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
<?php
/**
* nanopub - MicroPub support for Static Blog Engine
*
* PHP version 7
*
* @author Daniel Goldsmith <dgold@ascraeus.org>
* @license https://opensource.org/licenses/FPL-1.0.0 0BSD
* @link https://github.com/dg01d/nanopub
* @category Micropub
* @version 1.5
*/
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';
$twAPIkey = $configs->twAPIkey;
$twAPIsecret = $configs->twAPIsecret;
$twUserKey = $configs->twUserKey;
$twUserSecret = $configs->twUserSecret;
$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 $url adressable url of the external API
* @param $auth authorisation header for the API
* @param $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;
}
/**
* getallheaders() replacement 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
*
* @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 the array of Hugo key => value frontmatter elements
* @param $keys an associative array, pairing key values
* @param 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 $textFile the Hugo content file, loaded with json frontmatter
* @param $mfArray Array of Hugo <=> mf2 field equivalents
* @param $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 of mf2-compliant fieldnames
* @param $mfArray array of Hugo <=> mf2 field equivalents
* @return array with Hugo 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;
}
/**
* @since 1.1
* Writes dataset to file.
* Put here to allow extension for different post-types in future.
*
* @return boolean
*/
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, 32 | 64 | 128 | 256) . "\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 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://twitter.com",
"name" => "Twitter"
),
1 => array(
"uid" => "https://".$configs->mastodonInstance,
"name" => "Mastodon"
)
)
);
header('Content-Type: application/json');
echo json_encode($array, 32 | 64 | 128 | 256);
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://twitter.com",
"name" => "Twitter"
),
1 => array(
"uid" => "https://".$configs->mastodonInstance,
"name" => "Mastodon"
)
)
);
header('Content-Type: application/json');
echo json_encode($array, 32 | 64 | 128 | 256);
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']);
if ($frontmatter['repost_site'] == 'twitter.com') {
$synds['0'] = "https://twitter.com";
}
}
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']);
if ($frontmatter['replysite'] == 'twitter.com') {
$synds['0'] = "https://twitter.com";
}
}
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
$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)) {
$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";
$mdata = array(
"status" => $MastodonText,
);
// 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'];
}
// then twitter, with its useless 140 chars
if (in_array("https://twitter.com", $synds)) {
$TwText = substr($synText, 0, 260) . '… '. $canonical;
// Calls the external Twitter Library
$settings = array(
'consumer_key' => $twAPIkey,
'consumer_secret' => $twAPIsecret,
'oauth_access_token' => $twUserKey,
'oauth_access_token_secret' => $twUserSecret
);
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';
$postfields = array(
'status' => $TwText
);
if ((isset($frontmatter['replytourl']) && $frontmatter['replysite'] == "twitter.com")) {
$postfields['in_reply_to_status_id'] = tw_url_to_status_id($frontmatter['replytourl']);
$postfields['auto_populate_reply_metadata'] = true;
}
if ((isset($frontmatter['like_of'])) && ($frontmatter['like_site'] == "twitter.com")) {
$url = 'https://api.twitter.com/1.1/favorites/create.json';
$postfields['id'] = tw_url_to_status_id($frontmatter['like_of']);
unset($postfields['status']);
}
if ((isset($frontmatter['repost_of'])) && ($frontmatter['repost_site'] == "twitter.com")) {
$id = tw_url_to_status_id($frontmatter['repost_of']);
$url = 'https://api.twitter.com/1.1/statuses/retweet/' . $id . '.json';
$postfields['id'] = $id;
unset($postfields['status']);
}
//Perform a POST request and echo the response
$twitter = new TwitterAPIExchange($settings);
$twarray = json_decode(
$twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest()
);
if ((isset($frontmatter['repost_of'])) || (isset($frontmatter['like_of']))) {
$frontmatter['twitlink'] = isset($frontmatter['repost_of']) ? $frontmatter['repost_of'] : $frontmatter['like_of'];
} else {
$str = $twarray->id_str;
$nym = $twarray->user->screen_name;
$frontmatter['twitlink'] = "https://twitter.com/" . $nym . "/status/" . $str;
}
}
}
// 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);
}
}
}