aboutsummaryrefslogtreecommitdiffstats
path: root/include/opengraph.php
blob: 5360468ebb64ae9295fdedbd693c4cde7fae658e (plain) (blame)
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
<?php
/**
 * @file include/opengraph.php
 * @brief Add Opengraph metadata and related functions.
 */
 
 
 /**
 * @brief Adds Opengraph meta tags into HTML head
 *
 * @param array $item
 * @param array $profile
 *
 */
 
 function opengraph_add_meta($item, $profile) {

	if(! empty($item)) {

		if(! empty($item['title']))
			$ogtitle = $item['title'];

		// find first image if exist
		if(preg_match("/\[[zi]mg(=[0-9]+x[0-9]+)?\]([^\[]+)/is", $item['body'], $matches)) {
			$ogimage = $matches[2];
		    $ogimagetype = guess_image_type($ogimage);	
		}

		// use summary as description if exist
		$ogdesc = (empty($item['summary']) ? $item['body'] : $item['summary'] );
		
		$ogdesc = str_replace("#^[", "[", $ogdesc);
	
		$ogdesc = bbcode($ogdesc, [ 'tryoembed' => false ]);
		$ogdesc = trim(html2plain($ogdesc, 0, true));
		$ogdesc = html_entity_decode($ogdesc, ENT_QUOTES, 'UTF-8');
		
		// remove all URLs
		$ogdesc = preg_replace("/https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,\@]+/", "", $ogdesc);
		
		// shorten description
		$ogdesc = substr($ogdesc, 0, 300);
		$ogdesc = str_replace("\n", " ", $ogdesc);
		while (strpos($ogdesc, "  ") !== false)
			$ogdesc = str_replace("  ", " ", $ogdesc);
		$ogdesc = rtrim(substr($ogdesc, 0, strrpos($ogdesc, " ")), "?.,:;!-") . "...";
		
		$ogtype = "article";
	}

	$channel = channelx_by_n($profile['profile_uid']);

	if(! isset($ogdesc)) {
		if($profile['about'] && perm_is_allowed($channel['channel_id'],get_observer_hash(),'view_profile')) {
			$ogdesc = $profile['about'];
		}
		else {
			$ogdesc = sprintf( t('This is the home page of %s.'), $channel['channel_name']);
		}
	}
	
	if(! isset($ogimage)) {
		$ogimage = $channel['xchan_photo_l'];
	    $ogimagetype = $channel['xchan_photo_mimetype'];
	}

	App::$page['htmlhead'] .= '<meta property="og:title" content="' . htmlspecialchars((isset($ogtitle) ? $ogtitle : $channel['channel_name'])) . '">' . "\r\n";
	App::$page['htmlhead'] .= '<meta property="og:image" content="' . $ogimage . '">' . "\r\n";
	App::$page['htmlhead'] .= '<meta property="og:image:type" content="' . $ogimagetype . '">' . "\r\n";
	App::$page['htmlhead'] .= '<meta property="og:description" content="' . htmlspecialchars($ogdesc) . '">' . 	"\r\n";
	App::$page['htmlhead'] .= '<meta property="og:type" content="' . (isset($ogtype) ? $ogtype : "profile") . '">' . "\r\n";

	return true;
 }