-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplugin.php
63 lines (49 loc) · 1.88 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
<?php
/*
Plugin Name: Open Graph Meta Scraper
Plugin URI: https://github.com/LK608/OG-Scraper
Description: Get Open Graph information for your shortened links
Version: 1.1
Author: Luke Steinfurth
Author URI: https://www.steinfurth.co/
*/
define( 'DELAY', '0' ); // delay to redirect
yourls_add_action( 'pre_redirect', 'og_scraper' );
function og_scraper( $args ) {
$url = $args[0];
$parsed_url = parse_url($url);
require 'user/plugins/vendor/autoload.php';
$web = new \Spekulatius\PHPScraper\PHPScraper;
$web->go($url);
$data = $web->openGraph;
$description = $web->openGraph['og:description'];
if(empty($description))
{
$meta = get_meta_tags($url);
$description = $meta['description'];
}
$description = $web->openGraph['og:title'];
if(empty($title))
{
$page = file_get_contents($url);
$title = preg_match('/<title[^>]*>(.*?)<\/title>/ims', $page, $match) ? $match[1] : null;
}
$image = $web->openGraph['og:image'];
if(substr($image, 0, 4) != "http" || substr($web->openGraph['og:image'], 0, 5) != "https") $image = $parsed_url['scheme'] . "://" . $parsed_url['host'] . $web->openGraph['og:image'];
?>
<html>
<head>
<?php
echo '<meta property="og:title" content="' . $title . '" />
<meta property="og:type" content="' . $web->openGraph['og:type'] . '" />
<meta property="og:url" content="' . $web->openGraph['og:location'] . '" />
<meta property="og:image" content="' . $image . '" />
<meta property="og:site_name" content="' . $web->openGraph['og:site_name'] . '" />
<meta property="og:description" content="' . $description . '" />';
?>
<meta http-equiv="refresh" content="<?php echo DELAY; ?>; url=<?php echo $url; ?>">
</head>
</html>
<?php
die();
}