-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
185 lines (158 loc) · 7.11 KB
/
plugin.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
<?php
/*
Plugin Name: (YBPVE) YOURLS Better PeerTube Video Embeds
Plugin URI: https://github.com/spikehidden/YBPVE
Description: Returns a Video Embed for Discord & Co.
Version: 1.0
Author: Spikehidden
Author URI: https://github.com/spikehidden/
LICENSE: MIT License
Support: https://spikey.biz/discord
Donate: https://spikey.biz/kofi
*/
if (!defined('PTV_DELAY')) {
define("PTV_DELAY", "5");
}
yourls_add_action('pre_redirect','spikey_discord_peertubemeta');
function spikey_discord_peertubemeta($args) {
$url = $args[0];
// Get User Agent
$agent = $_SERVER["HTTP_USER_AGENT"];
// Check if it is a bot or site scraper.
if (strpos($agent, "facebookexternalhit/") !== false || strpos($agent, "bot") !== false || strpos($agent, "Tool") !== false) {
// Do nothing
} else {
die();
}
$status = getUrlSatusCode($url);
echo "\n$status\n";
if ($status <= 199 && $status >= 400) {
die();
}
$url2 = parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST);
$base_url = trim($url2, '/');
// Fetch some Metas
$og = fetch_og($url);
$platform = $og['platform'];
$type = $og['type'];
// Check if it is a PeerTube Video
if ($platform == "PeerTube" && $type == "video") {
// Fetch rest Meta
$twitter = fetch_twitter($url);
$metas = get_meta_tags($url);
$desc = $metas['description'];
$title = $og['title'];
$site_name = $og['site_name'];
// PeerTube API
$video_ID = basename($og['video:url']);
$peertube = peertube_stream_url($base_url,$video_ID);
$stream = $peertube['files'][1]['fileUrl'] ?? null;
// $channel_name = $peertube['channel']['name'] ?? null;
// $channel_url = $peertube['channel']['url'] ?? null;
$author_name = $peertube['channel']['ownerAccount']['name'] ?? null;
$author_url = $peertube['channel']['ownerAccount']['url'] ?? null;
// To let it show correctly in Discord
$oembed = "https://embedl.ink/api/oembed?provider_name=" . $site_name . "&provider_url=" . $base_url . "&author_name=" . $author_name . "&author_url=" . $author_url;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo $title . " - " . $site_name; ?></title>
<link type="application/json+oembed" href="<?php echo $oembed ?>">
<!-- Meta -->
<!-- Forward in case a user see this site -->
<meta http-equiv="refresh" content="<?php echo PTV_DELAY; ?>; url=<?php echo $url; ?>" />
<!-- OG -->
<meta property="og:type" content="<?php print "$type"; ?>" />
<meta property="og:url" content="<?php echo "$url"; ?>" />
<meta property="og:title" content="<?php echo "$title"; ?>" />
<meta property="og:video" content="<?php echo "$stream"; ?>" />
<meta property="og:video:width" content="1280">
<meta property="og:video:height" content="720">
<meta property="og:video:type" content="video/mp4" />
<meta property="og:site_name" content="<?php echo "$site_name"; ?>" />
<meta property="og:image" content="<?php echo $og['image']; ?>"/>
<meta property="og:description" content="<?php echo "$desc"; ?>"/>
<!-- Normal -->
<meta name="description" content="<?php echo "$desc"; ?>"/>
<!-- Twitter Card -->
<meta property="twitter:card" content="player"/>
<meta property="twitter:title" content="<?php echo $twitter['title']; ?>"/>
<!-- <meta property="twitter:site" content="<?php echo $twitter['site']; ?>"/> -->
<meta property="twitter:site" content="<?php echo "@" . $author_name; ?>"/>
<meta property="twitter:description" content="<?php echo $twitter['description']; ?>"/>
<meta property="twitter:image" content="<?php echo $twitter['image']; ?>"/>
<meta property="twitter:player" content="<?php echo $og['video:url']; ?>"/>
<meta property="twitter:creator" content="<?php echo "@" . $author_name; ?>"/>
<meta property="twitter:player:width" content="<?php echo $og['video:width']; ?>"/>
<meta property="twitter:player:height" content="<?php echo $og['video:height']; ?>"/>
<meta property="twitter:player:stream" content="<?php echo "$stream"; ?>"/>
<meta property="twitter:secureurl:player_url" content="<?php echo $og['video:url']; ?>"/>
</head>
<body>
You are being redirected to <a href="<?php echo $url; ?>"><?php echo $url; ?></a>.
If you have not been redirected please <a href="<?php echo $url; ?>">Click Here</a>.
</body>
</html>
<?php
die();
}
}
function peertube_stream_url($base_url,$id) {
$api = $base_url . "/api/v1/videos/";
$url = $api . $id;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($curl);
curl_close($curl);
$data = json_decode($resp, true);
return $data ?? null;
}
// Function from https://gist.github.com/tutweb/8ae422dbb21370ab0715
function fetch_og($url) {
$data = file_get_contents($url);
$dom = new DomDocument;
@$dom->loadHTML($data);
$xpath = new DOMXPath($dom);
# query metatags dengan prefix og
$metas = $xpath->query('//*/meta[starts-with(@property, \'og:\')]');
$og = array();
foreach($metas as $meta){
# ambil nama properti tanpa menyertakan og
$property = str_replace('og:', '', $meta->getAttribute('property'));
# ambil konten dari properti tersebut
$content = $meta->getAttribute('content');
$og[$property] = $content;
}
return $og;
}
function fetch_twitter($url) {
$data = file_get_contents($url);
$dom = new DomDocument;
@$dom->loadHTML($data);
$xpath = new DOMXPath($dom);
# query metatags dengan prefix twitter
$metas = $xpath->query('//*/meta[starts-with(@property, \'twitter:\')]');
$twitter = array();
foreach($metas as $meta){
# ambil nama properti tanpa menyertakan twitter
$property = str_replace('twitter:', '', $meta->getAttribute('property'));
# ambil konten dari properti tersebut
$content = $meta->getAttribute('content');
$twitter[$property] = $content;
}
return $twitter;
}
function getUrlSatusCode($url, $timeout = 10) {
$ch = curl_init();
$opts = array(CURLOPT_RETURNTRANSFER => true, // do not output to browser
CURLOPT_URL => $url,
CURLOPT_NOBODY => true, // do a HEAD request only
CURLOPT_TIMEOUT => $timeout);
curl_setopt_array($ch, $opts);
curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $status;
}