-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
147 lines (129 loc) · 3.57 KB
/
index.php
File metadata and controls
147 lines (129 loc) · 3.57 KB
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
<?php
require __DIR__ . '/vendor/autoload.php';
try {
\Dotenv\Dotenv::createUnsafeImmutable(__DIR__)->load();
} catch (\Throwable $th) {
trigger_error($th);
}
use Leaf\Blade;
use Leaf\Fetch;
//use function Leaf\fetch;
$blade = new Blade('views', 'views/cache');
function convertUrlsToLinks($text)
{
$pattern = '/\b((?:https?:\/\/|www\.)[^\s<]+)\b/i';
return preg_replace_callback($pattern, function ($matches) {
return '<a href="' . e($matches[0]) . '" target="_blank">' . e($matches[0]) . '</a>';
}, e($text));
}
app()->get('/', function () {
response()->redirect('/posts');
});
function extractContent($data)
{
$doc = new DOMDocument();
$doc->loadHTML($data);
$metas = $doc->getElementsByTagName('meta');
foreach ($metas as $meta) {
$name = $meta->getAttribute('name');
$content = $meta->getAttribute('content');
if ($name === "did:content") {
$postContent[] = $content;
}
}
return $postContent;
}
function isICOString($data)
{
$header = substr($data, 0, 4); // Extract the first 4 bytes
return $header === "\x00\x00\x01\x00";
}
function fetchPost($path)
{
$url = "http://" . $path;
$postContent = [];
try {
$res = Fetch::request([
"method" => "GET",
"url" => $url,
"rawResponse" => true,
"timeout" => 3
]);
$postContent = extractContent($res->data);
$db = new App\Db();
$db->savePostContent($path, $res->data);
} catch (Exception $e) {
}
return $postContent;
}
app()->get('/avatar', function () {
// Set max-age to a week to benefit from client caching (this is optional)
header('Cache-Control: max-age=604800');
$db = new App\Db();
$domain = request()->get('value');
$avatar = $db->getAvatarByDomain($domain);
if ($avatar) {
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mimeType = $finfo->buffer($avatar['favicon']);
header("Content-Type: $mimeType");
header("Content-Length: " . strlen($avatar['favicon']));
echo $avatar['favicon'];
return;
}
try {
$res = Fetch::request([
"method" => "GET",
"url" => "http://" . $domain . "/favicon.ico",
"rawResponse" => true,
"timeout" => 3
]);
if ($res->data && isICOString($res->data)) {
header('Content-Type: image/x-icon');
$db->saveAvatarForDomain($domain, $res->data);
echo $res->data;
return;
}
} catch (Exception $e) {
}
// Parse query string parameters
$value = request()->get('value');
$size = 128;
// Render icon
$icon = new \Jdenticon\Identicon();
$style = new \Jdenticon\IdenticonStyle();
$style->setPadding(0);
// $icon->configure(['padding' => 0]);
$icon->setStyle($style);
$icon->setValue($value);
$icon->setSize($size);
$db->saveAvatarForDomain($domain, $icon->getImageData());
$icon->displayImage('png');
});
app()->get('/rawindex', function () {
global $blade;
$db = new App\Db();
$posts = $db->getPostsRaw();
echo $blade->make('rawindex', ['posts' => $posts])->render();
});
app()->get('/posts', function () {
global $blade;
$db = new App\Db();
$posts = $db->getPosts();
foreach ($posts as &$post) {
$url = "http://" . $post["path"];
$dbPost = $db->getPostContent($post["path"]);
$content = "";
if ($dbPost) {
$content = extractContent($dbPost["content"]);
} else {
// TODO: move DB save and estractContent here
$content = fetchPost($post["path"]);
}
$post['content'] = $content;
$post['owner'] = explode("/", $post["path"])[0];
$post['url'] = $url;
}
echo $blade->make('posts', ['posts' => $posts])->render();
});
app()->config('debug', true);
app()->run();