forked from exitcas/pbpush
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
414 lines (337 loc) · 11.8 KB
/
functions.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
<?php
declare(strict_types=1);
$config = parse_ini_file('.config');
if ($config['debug_mode']) {
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
}
class TwtxtFile {
public $mainURL = ''; // First found URL
public $URLs = [];
public $nick = '';
public $avatar = '';
public $emoji = '';
public $description = '';
public $lang = 'en'; // Default language
public $links = [];
public $following = [];
public $twts = [];
}
class Twt {
public $originalTwtStr;
public $hash;
public $fullDate;
public $displayDate;
public $content;
public $replyToHash;
public $mentions;
public $avatar;
public $emoji;
public $nick;
public $mainURL;
}
# https://stackoverflow.com/a/39360281/13173382
# Confirm that this temorary fix is not skipping something
/*
stream_context_set_default([
'ssl' => [
'peer_name' => 'generic-server',
'verify_peer' => FALSE,
'verify_peer_name' => FALSE,
'allow_self_signed' => TRUE
]
]
);
curl_setopt($curl, CURLOPT_SSLVERSION, 4);
*/
/**
* The function searches for a key-value pair in a string and returns the value if found.
*
* @param keyToFind The key we want to find in the string.
* @param string The string in which to search for the key-value pair.
*
* @return the value of the key that matches the given keyToFind in the given string. If a match is
* found, the function returns the value of the key as a string after trimming any whitespace. If no
* match is found, the function returns null.
*/
function getSingleParameter($keyToFind, $string) {
if (!str_contains($string, $keyToFind)) {
return null;
}
$pattern = '/\s*' . $keyToFind . '\s*=\s*([^#\n]+)/';
$pattern = '/\s*' . $keyToFind . '\s*=\s*([^\s#]+)/';
preg_match($pattern, $string, $matches);
if (isset($matches[1])) {
return trim($matches[1]);
}
return null;
}
function getDoubleParameter($keywordToFind, $string) {
// Returns string or null
$pattern = '/#\s*' . preg_quote($keywordToFind, '/') . '\s*=\s*(\S+)\s*(\S+)/';
// Matches "# <keyword> = <value> <value>"
preg_match($pattern, $string, $matches);
if (isset($matches[1]) && isset($matches[2])) {
$result = array($matches[1], $matches[2]);
return $result;
}
return null;
}
function getReplyHashFromTwt(string $twtString): string {
// Extract the text between parentheses using regular expressions
$pattern = '/\(#([^\)]+)\)/'; // Matches "(#<text>)"
preg_match($pattern, $twtString, $matches);
if (isset($matches[1])) {
$textBetweenParentheses = $matches[1];
return $textBetweenParentheses;
}
return '';
}
function getMentionsFromTwt(string $twtString) {
$pattern = '/@<([^>]+)\s([^>]+)>/'; // Matches "@<nick url>"
preg_match_all($pattern, $twtString, $matches, PREG_SET_ORDER);
$result = array();
foreach ($matches as $match) {
$nick = $match[1];
$url = $match[2];
$result[] = array("nick" => $nick, "url" => $url);
}
return $result;
}
function replaceMentionsFromTwt(string $twtString): string {
// Example input: 'Hello @<eapl.mx https://eapl.mx/twtxt.txt>, how are you? @<nick https://server.com/something/twtxt.txt>';
// Example output: Hello <a href="?url=https://eapl.mx/twtxt.txt">@eapl.mx@eapl.mx/twtxt.txt</a>, how are you? <a href="?url=https://server.com/something/twtxt.txt">@nick@server.com/something/twtxt.txt</a>
#$pattern = '/@<([^ ]+)\s([^>]+)>/';
#$replacement = '<a href="?url=$2">@$1@$2</a>';
#$twtString = '@<nick https://eapl.mx/twtxt.txt>';
$pattern = '/@<([^ ]+) ([^>]+)>/';
$replacement = '@$1';
$result = preg_replace($pattern, $replacement, $twtString);
return $result;
}
function replaceLinksFromTwt(string $twtString) {
// Regular expression pattern to match URLs
$pattern = '/(?<!\S)(\b(https?|ftp|gemini|spartan|gopher):\/\/\S+|\b(?!:\/\/)\w+(?:\.\w+)+(?:\/\S+)?)(?!\S)/';
// Replace URLs with clickable links
$replacement = '<a href="$1">$1</a>';
$result = preg_replace($pattern, $replacement, $twtString);
return $result;
}
function replaceMarkdownLinksFromTwt(string $twtString) {
$pattern = '/\[([^\]]+)\]\(([^)]+)\)/';
$replacement = '<a href="$2">$1</a>';
$result = preg_replace($pattern, $replacement, $twtString);
return $result;
}
function replaceImagesFromTwt(string $twtString) {
$pattern = '/!\[(.*?)\]\((.*?)\)/';
$replacement = '<img src="$2" alt="$1">';
$result = preg_replace($pattern, $replacement, $twtString);
return $result;
}
function getTimeElapsedString($timestamp, $full = false) {
$now = new DateTime;
$ago = new DateTime;
$ago->setTimestamp($timestamp);
$agoText = 'ago';
if ($now < $ago) {
$agoText = 'in the future';
}
$diff = $now->diff($ago);
//$diff->w = floor($diff->d / 7);
$w = floor($diff->d / 7);
$d = $diff->d - ($w * 7);
//$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) { // k is key, and v is value... Obviously
if ($k === 'w') {
if ($w) {
$v = $w . ' ' . $v . ($w > 1 ? 's' : '');
} else {
unset($string[$k]);
}
} else {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . " $agoText" : 'just now';
}
function getCachedFileContentsOrUpdate($fileURL, $cacheDurationSecs = 15) {
# TODO: Process the Warning
# Warning: file_get_contents(https://eapl.mx/twtxt.net):
# failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in
$cacheFilePath = getCachedFileName($fileURL);
// Check if cache file exists and it's not expired
if (file_exists($cacheFilePath) && (time() - filemtime($cacheFilePath)) < $cacheDurationSecs) {
return file_get_contents($cacheFilePath);
}
// File doesn't exist in cache or has expired, so fetch and cache it
$contents = file_get_contents($fileURL);
file_put_contents($cacheFilePath, $contents);
return $contents;
}
function getCachedFileContents($filePath) {
$cacheFile = getCachedFileName($filePath);
// Check if cache file exists and it's not expired
if (file_exists($cacheFile)) {
return file_get_contents($cacheFile);
}
return null;
}
function updateCachedFile($filePath, $cacheDurationSecs = 15) {
$cacheFilePath = getCachedFileName($filePath);
// File doesn't exist in cache or has expired, so fetch and cache it
// TODO: Seems it's not working right!
$fileDoesntExist = !file_exists($cacheFilePath);
$fileIsOld = false;
if (!$fileDoesntExist) {
$fileIsOld = !((time() - filemtime($cacheFilePath)) < $cacheDurationSecs);
}
if ($fileDoesntExist || $fileIsOld) {
#echo "Loading Cached file $cacheFilePath<br>\n";
$contents = @file_get_contents($filePath);
if ($contents === false) {
// File loaded with errors, skip saving it
return;
}
file_put_contents($cacheFilePath, $contents);
}
}
function getTwtsFromTwtxtString($url) {
$fileContent = getCachedFileContents($url);
if (is_null($fileContent)) {
return null;
}
$fileContent = mb_convert_encoding($fileContent, 'UTF-8');
$fileLines = explode("\n", $fileContent);
$twtxtData = new TwtxtFile();
foreach ($fileLines as $currentLine) {
// Remove empty lines
if (empty($currentLine)) {
continue;
}
if (str_starts_with($currentLine, '#')) {
// Check if comments (starting with #) have some metadata
if (!is_null(getSingleParameter('url', $currentLine))) {
$currentURL = getSingleParameter('url', $currentLine);
if (empty($twtxtData->URLs)) {
$twtxtData->mainURL = $currentURL;
}
$twtxtData->URLs[] = $currentURL;
}
if (!is_null(getSingleParameter('nick', $currentLine))) {
$twtxtData->nick = getSingleParameter('nick', $currentLine);
}
if (!is_null(getSingleParameter('avatar', $currentLine))) {
$twtxtData->avatar = getSingleParameter('avatar', $currentLine);
}
if (!is_null(getSingleParameter('emoji', $currentLine))) {
$twtxtData->emoji = getSingleParameter('emoji', $currentLine);
}
if (!is_null(getSingleParameter('lang', $currentLine))) {
$twtxtData->lang = getSingleParameter('lang', $currentLine);
}
if (!is_null(getSingleParameter('description', $currentLine))) {
$twtxtData->description = getSingleParameter('description', $currentLine);
}
if (!is_null(getSingleParameter('follow', $currentLine))) {
$twtxtData->following[] = getSingleParameter('follow', $currentLine);
}
}
if (!str_starts_with($currentLine, '#')) {
$explodedLine = explode("\t", $currentLine);
if (count($explodedLine) >= 2) {
$dateStr = $explodedLine[0];
$twtContent = $explodedLine[1];
$twtContent = replaceMentionsFromTwt($twtContent);
// Convert HTML problematic characters
$twtContent = htmlentities($twtContent);
// Replace the Line separator character (U+2028)
// \u2028 is \xE2 \x80 \xA8 in UTF-8
// Check here: https://www.mclean.net.nz/ucf/
//$twtContent = str_replace("\xE2\x80\xA8", "<br>\n", $twtContent);
// For some reason I was having trouble finding this nomenclature
// that's why I leave the UTF-8 representation for future reference
$twtContent = str_replace("\u{2028}", "\n<br>\n", $twtContent);
$twtContent = replaceLinksFromTwt($twtContent);
$twtContent = replaceImagesFromTwt($twtContent);
$twtContent = replaceMarkdownLinksFromTwt($twtContent);
// Get and remote the hash
$hash = getReplyHashFromTwt($twtContent);
if ($hash) {
$twtContent = str_replace("(#$hash)", '', $twtContent);
}
// TODO: Get mentions
$mentions = getMentionsFromTwt($twtContent);
// Get Lang metadata
if (($timestamp = strtotime($dateStr)) === false) {
//echo "The string ($dateStr) is incorrect";
// Incorrect date string, skip this twt
continue;
} else {
$displayDate = getTimeElapsedString($timestamp);
}
// TODO: Only 1 twt by second is allowed here
$twt = new Twt();
$twt->originalTwtStr = $currentLine;
$twt->hash = getHashFromTwt($currentLine, $twtxtData->mainURL);
$twt->fullDate = date('j F Y h:i:s A', $timestamp) . ' (UTC)';
$twt->displayDate = $displayDate;
$twt->content = $twtContent;
$twt->replyToHash = $hash;
$twt->mentions = $mentions;
$twt->avatar = $twtxtData->avatar;
$twt->emoji = $twtxtData->emoji;
$twt->nick = $twtxtData->nick;
$twt->mainURL = $twtxtData->mainURL;
$twtxtData->twts[$timestamp] = $twt;
// TODO: Interpret the content as markdown
}
}
}
return $twtxtData;
}
function insertFollowingURL($urlString) {
// Check if it's a valid URL
// Retrieve the nickname, if didn't find a nick, ask for one
$originalCode = '
Lorem ipsum dolor sit amet,
#~~~#
consectetur adipiscing elit.';
$text = '#~~~#';
$newText = '123' . PHP_EOL . $text;
$result = str_replace('#~~~#', $newText, $originalCode);
echo $result;
}
function getCachedFileName($filePath) {
return __DIR__ . '/cache/' . hash('sha256', $filePath);
}
if (!function_exists('str_starts_with')) {
function str_starts_with($haystack, $needle) {
return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0;
}
}
if (!function_exists('str_ends_with')) {
function str_ends_with($haystack, $needle) {
return $needle !== '' && substr($haystack, -strlen($needle)) === (string)$needle;
}
}
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
}
}