Hubzilla core code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

73 lines
2.8 KiB

  1. <?php
  2. /**
  3. * @file include/opengraph.php
  4. * @brief Add Opengraph metadata and related functions.
  5. */
  6. /**
  7. * @brief Adds Opengraph meta tags into HTML head
  8. *
  9. * @param array $item
  10. * @param array $channel
  11. *
  12. */
  13. function opengraph_add_meta($item, $channel) {
  14. if(! empty($item)) {
  15. if(! empty($item['title']))
  16. $ogtitle = $item['title'];
  17. // find first image if exist
  18. if(preg_match("/\[[zi]mg(=[0-9]+x[0-9]+)?\]([^\[]+)/is", $item['body'], $matches)) {
  19. $ogimage = $matches[2];
  20. $ogimagetype = guess_image_type($ogimage);
  21. }
  22. // use summary as description if exist
  23. $ogdesc = (empty($item['summary']) ? $item['body'] : $item['summary'] );
  24. $ogdesc = str_replace("#^[", "[", $ogdesc);
  25. $ogdesc = bbcode($ogdesc, [ 'tryoembed' => false ]);
  26. $ogdesc = trim(html2plain($ogdesc, 0, true));
  27. $ogdesc = html_entity_decode($ogdesc, ENT_QUOTES, 'UTF-8');
  28. // remove all URLs
  29. $ogdesc = preg_replace("/https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,\@]+/", "", $ogdesc);
  30. // shorten description
  31. $ogdesc = substr($ogdesc, 0, 300);
  32. $ogdesc = str_replace("\n", " ", $ogdesc);
  33. while (strpos($ogdesc, " ") !== false)
  34. $ogdesc = str_replace(" ", " ", $ogdesc);
  35. $ogdesc = (strlen($ogdesc) < 298 ? $ogdesc : rtrim(substr($ogdesc, 0, strrpos($ogdesc, " ")), "?.,:;!-") . "...");
  36. $ogtype = "article";
  37. }
  38. if(! isset($ogdesc)) {
  39. if(App::$profile['about'] && perm_is_allowed($channel['channel_id'],get_observer_hash(),'view_profile')) {
  40. $ogdesc = App::$profile['about'];
  41. }
  42. else {
  43. $ogdesc = sprintf( t('This is the home page of %s.'), $channel['channel_name']);
  44. }
  45. }
  46. if(! isset($ogimage)) {
  47. $ogimage = $channel['xchan_photo_l'];
  48. $ogimagetype = $channel['xchan_photo_mimetype'];
  49. }
  50. App::$page['htmlhead'] .= '<meta property="og:title" content="' . htmlspecialchars((isset($ogtitle) ? $ogtitle : $channel['channel_name'])) . '">' . "\r\n";
  51. App::$page['htmlhead'] .= '<meta property="og:image" content="' . $ogimage . '">' . "\r\n";
  52. App::$page['htmlhead'] .= '<meta property="og:image:type" content="' . $ogimagetype . '">' . "\r\n";
  53. App::$page['htmlhead'] .= '<meta property="og:description" content="' . htmlspecialchars($ogdesc) . '">' . "\r\n";
  54. App::$page['htmlhead'] .= '<meta property="og:type" content="' . (isset($ogtype) ? $ogtype : "profile") . '">' . "\r\n";
  55. return true;
  56. }