about summary refs log tree commit diff stats
path: root/superstack.php
blob: 306b7b3f921c38756c1ef9c54d80ac30bfdb8ff4 (plain)
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
<?php
$site = $_GET['page'];

if (empty($site)) {
	echo "Superstack!\n";
	return;
}

if (! preg_match('/substack\.com/', $site)) {
	echo "Requires a substack URL!";
	return;
}

$site = urldecode($site);

$ch = curl_init($site);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
if (curl_error($ch)) {
    echo curl_error($ch);
}
$doc = new DOMDocument();
$doc->loadHTML($page);
$title = $doc->getElementsByTagName('title')[0]->textContent;
?>
    <!DOCTYPE html>
        <html>
        <head><title><?php echo $title ?> -- Superstack</title>
       <style>
       body{max-width:42em;padding:1em;margin:auto;font:18px/1.4 sans-serif;}
img{max-width:100%;object-fit: contain;}
</style>
    <body>

<?php

    echo '<h1>' . $title . '</h1>';
echo '<p><em>' . $site . '</em>';
echo ' <a href="' . $site . '">[orig]</a></p>';

$content = $doc->getElementsBytagName('div');

foreach ($content as $el) {
    if (stripos($el->getAttribute('class'), 'body') !== false) {

        $ret = new DOMDocument();
        $node = $ret->importNode($el, true);
        $ret->appendChild($node);
        echo $ret->saveHTML();

        // echo $el->nodeValue;
        // echo '<tr>';
        // echo '<td>' . $el->getAttribute('class') . '</td>';
        // echo '<td>' . stripos($el->getAttribute('class'), 'body') . '</td>';
        // echo '</tr>';
    }
}

?>

    </body>
        </html>