asset_og_renderer.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Process pages that support the open graph protocol
  4. *
  5. * @see http://ogp.me/
  6. *
  7. * @copyright (c) 2011 University of Geneva
  8. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  9. * @author Laurent Opprecht
  10. */
  11. class AssetOgRenderer extends AssetRenderer
  12. {
  13. /**
  14. * Renderer function. Take a http asset as input and return an array containing
  15. * various properties: metadata, html snippet, etc.
  16. *
  17. * @param HttpResource $asset
  18. * @return array
  19. */
  20. public function render($asset)
  21. {
  22. $type = $asset->get_meta('og:type');
  23. if (empty($type))
  24. {
  25. if ($video = $asset->get_meta('og:video'))
  26. {
  27. $type = 'video';
  28. }
  29. else if ($video = $asset->get_meta('og:image'))
  30. {
  31. $type = 'default';
  32. }
  33. }
  34. if (empty($type))
  35. {
  36. return array();
  37. }
  38. $type = explode('.', $type);
  39. $type = reset($type);
  40. $f = array($this, "render_$type");
  41. if (is_callable($f))
  42. {
  43. $result = call_user_func($f, $asset);
  44. }
  45. else
  46. {
  47. $result = $this->render_default($asset);
  48. }
  49. $result[self::TITLE] = $asset->get_meta('og:title');
  50. $result[self::THUMBNAIL] = $asset->get_meta('og:image');
  51. $result[self::LANGUAGE] = $asset->get_meta('og:language');
  52. return $result;
  53. }
  54. /**
  55. * @param HttpResource $asset
  56. * @return array
  57. */
  58. protected function render_video($asset)
  59. {
  60. $url = $asset->get_meta('og:video');
  61. $url = str_replace('?autoPlay=1', '?', $url);
  62. $url = str_replace('&autoPlay=1', '', $url);
  63. if (empty($url))
  64. {
  65. return array();
  66. }
  67. $type = $asset->get_meta('og:video:type');
  68. if ($type)
  69. {
  70. $type = ' type="' . $type . '" ';
  71. }
  72. $size = (int) $asset->config('size');
  73. $size = (24 <= $size && $size <= 800) ? $size : 300;
  74. $width = $asset->get_meta('og:video:width');
  75. $width = $width ? $width : $asset->get_meta('video_width');
  76. $height = $asset->get_meta('og:video:height');
  77. $height = $height ? $height : $asset->get_meta('video_height');
  78. if ($width)
  79. {
  80. $ratio = $height / $width;
  81. $base = min($size, $width);
  82. $width = $base;
  83. $height = $ratio * $base;
  84. $size = 'width="' . $width . '" height="' . $height . '"';
  85. }
  86. else
  87. {
  88. $size = 'width="' . $size . '"';
  89. }
  90. $embed = <<<EOT
  91. <embed $type $size src="$url" />
  92. EOT;
  93. $result[self::EMBED_TYPE] = $type;
  94. $result[self::EMBED_URL] = $url;
  95. $result[self::EMBED_SNIPPET] = $embed;
  96. $result[self::TAGS] = $asset->get_meta('og:video:tag');
  97. $result[self::CREATED_TIME] = $asset->get_meta('og:video:release_date');
  98. $result[self::DURATION] = $asset->get_meta('og:duration');
  99. return $result;
  100. }
  101. protected function render_article($asset)
  102. {
  103. $result = $this->render_default($asset);
  104. return $result;
  105. }
  106. protected function render_audio($asset)
  107. {
  108. $result = $this->render_default($asset);
  109. return $result;
  110. }
  111. protected function render_book($asset)
  112. {
  113. $result = $this->render_default($asset);
  114. return $result;
  115. }
  116. /**
  117. *
  118. * @param HttpResource $asset
  119. * @return array
  120. */
  121. protected function render_default($asset)
  122. {
  123. $url = $asset->get_meta('og:url');
  124. $url = htmlentities($url);
  125. $title = $asset->get_meta('og:title');
  126. $image = $asset->get_meta('og:image');
  127. $image = htmlentities($image);
  128. $width = $asset->get_meta('og:image:width');
  129. $height = $asset->get_meta('og:image:height');
  130. $description = $asset->get_meta('og:description');
  131. $description = $description ? $description : $asset->get_meta('description');
  132. $size = (int) $asset->config('size');
  133. $size = (24 <= $size && $size <= 800) ? $size : 300;
  134. if ($width)
  135. {
  136. $ratio = $height / $width;
  137. $base = min($size, $width);
  138. $width = $base;
  139. $height = $ratio * $base;
  140. $size = 'width="' . $width . '" height="' . $height . '"';
  141. }
  142. else
  143. {
  144. $size = 'width="' . $size . '"';
  145. }
  146. $embed = <<<EOT
  147. <div>
  148. <a href="$url" style="float:left; margin-right:5px; margin-bottom:5px; display:block;"><img src="{$image}" {$size} alt="{$title}" title="{$title}"></a>
  149. <div style="clear:both;"></div>
  150. </div>
  151. EOT;
  152. $result[self::EMBED_SNIPPET] = $embed;
  153. $result[self::DESCRIPTION] = $asset->get_meta('description');
  154. return $result;
  155. }
  156. /**
  157. * @param HttpResource $asset
  158. * @return array
  159. */
  160. protected function render_image($asset)
  161. {
  162. $size = (int) $asset->config('size');
  163. $size = (24 <= $size && $size <= 800) ? $size : 300;
  164. $title = $data['title'];
  165. $width = $data['width'];
  166. $height = $data['height'];
  167. $ratio = $height / $width;
  168. $base = min($size, $width);
  169. $width = $base;
  170. $height = $ratio * $base;
  171. $url = $data['url'];
  172. $embed = <<<EOT
  173. <a href="$url"><img src="{$url}" width="{$width}" height="{$height} "alt="{$title}" title="{$title}"></a>
  174. EOT;
  175. }
  176. }