Last active
August 10, 2023 15:55
-
-
Save thillsman/e0d755f114aef178d38e3d656385323d to your computer and use it in GitHub Desktop.
mstdn.link logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$sourceURL = $_GET['to']; | |
function startsWith($string, $startString) { | |
$len = strlen($startString); | |
return (substr($string, 0, $len) === $startString); | |
} | |
$html = file_get_contents($sourceURL); | |
if ($html === false) { | |
header("Location: $sourceURL"); | |
return; | |
} | |
$doc = new DomDocument(); | |
$doc->loadHtml($html); | |
$xml = simplexml_import_dom($doc); | |
$title = $xml->xpath('//title'); | |
$iconArr = $xml->xpath('//link[@rel="icon"]'); | |
$imageArr = $xml->xpath('//meta[@property="og:image"]'); | |
$descriptionArr = $xml->xpath('//meta[@property="og:description"]'); | |
$parse = parse_url($sourceURL); | |
$icon = $parse['scheme']."://".$parse['host'].$iconArr[0]['href']; | |
$titleOutput = explode(":", $title[0])[0]; | |
$image = $imageArr[0]['content']; | |
if (str_contains($image, "/avatars/")) { | |
$image = ""; | |
} | |
// Description, normalized if including "Attached: {something} \n" | |
$rawDescription = $descriptionArr[0]['content']; | |
$descriptionLines = explode("\n", $rawDescription); | |
if (startsWith($descriptionLines[0], "Attached: ") && $descriptionLines[1] == "") { | |
unset($descriptionLines[1]); | |
unset($descriptionLines[0]); | |
} | |
$description = htmlentities(implode("\n", $descriptionLines)); | |
?> | |
<html dir="ltr" lang="en"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0,viewport-fit=cover"> | |
<link rel="shortcut icon" href="<?php echo $icon; ?>"> | |
<link rel="apple-touch-icon" sizes="192x192" href="<?php echo $icon; ?>"> | |
<title> | |
<?php echo $description; ?> | |
</title> | |
<meta content="article" property="og:type" data-rh="true"> | |
<meta description="<?php echo $description; ?>" name="description" data-rh="true"> | |
<meta content="https://twitter.com/x/status/x" property="og:url" data-rh="true"> | |
<meta content="<?php echo $image; ?>" property="og:image" data-rh="true"> | |
<meta content="<?php echo $titleOutput; ?>" property="og:title" data-rh="true"> | |
<meta content="<?php echo $description; ?>" property="og:description" data-rh="true"> | |
<style> | |
body { | |
background: #ccd7e0; | |
} | |
@media (prefers-color-scheme: dark) { | |
body { | |
background: #313543; | |
} | |
} | |
</style> | |
</head> | |
<body> | |
<script>window.location.replace("<?php echo $sourceURL ?>");</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what powers https://mstdn.link, a once-useful site that made Mastodon post previews look nicer in Apple Messages by pretending to be Twitter URLs to get the special-handling that Messages used prior to iOS 16.4.