aboutsummaryrefslogtreecommitdiffstats
path: root/include/opengraph.php
diff options
context:
space:
mode:
authorMax Kostikov <max@kostikov.co>2019-10-12 19:03:21 +0200
committerMax Kostikov <max@kostikov.co>2019-10-12 19:03:21 +0200
commitf7e665c42f3f4678cce512416eeca6f032146fe1 (patch)
treef8c6bd6309057678f97f90bbb11bea8b3d082d24 /include/opengraph.php
parent3bc44ee451b5c10b1635ac340f425f2b2a98aa43 (diff)
downloadvolse-hubzilla-f7e665c42f3f4678cce512416eeca6f032146fe1.tar.gz
volse-hubzilla-f7e665c42f3f4678cce512416eeca6f032146fe1.tar.bz2
volse-hubzilla-f7e665c42f3f4678cce512416eeca6f032146fe1.zip
Add Opengraph function
Diffstat (limited to 'include/opengraph.php')
-rw-r--r--include/opengraph.php68
1 files changed, 68 insertions, 0 deletions
diff --git a/include/opengraph.php b/include/opengraph.php
new file mode 100644
index 000000000..b177673ac
--- /dev/null
+++ b/include/opengraph.php
@@ -0,0 +1,68 @@
+<?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];
+
+ // 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);
+ if (substr($ogdesc, -1) != "\n")
+ $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']);
+ }
+ }
+
+ 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="' . (isset($ogimage) ? $ogimage : $channel['xchan_photo_l']) . '">' . "\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;
+ }
+ \ No newline at end of file