3 if (!defined('GNUSOCIAL')) { exit(1); }
7 * Utility class to get OpenGraph data from HTML DOMs etc.
11 const KEY_REGEX = '/^og\:(\w+(?:\:\w+)?)/';
12 protected static $property_map = [
13 'site_name' => 'provider_name',
15 'description' => 'html',
18 'image' => 'thumbnail_url',
19 'image:height' => 'thumbnail_height',
20 'image:width' => 'thumbnail_width',
23 // This regex map has: /pattern(match)/ => matchindex | string
24 protected static $type_regex_map = [
26 '/^image/' => 'photo',
29 static function ogFromHtml(DOMDocument $dom) {
30 $obj = new stdClass();
31 $obj->version = '1.0'; // fake it til u make it
33 $nodes = $dom->getElementsByTagName('meta');
34 for ($i = 0; $i < $nodes->length; $i++) {
35 $node = $nodes->item($i);
36 if (!$node->hasAttributes()) {
39 $property = $node->attributes->getNamedItem('property');
41 if ($property === null || !preg_match(self::KEY_REGEX, $property->value, $matches)) {
42 // not property="og:something"
45 if (!isset(self::$property_map[$matches[1]])) {
46 // unknown metadata property, nothing we would care about anyway
50 $prop = self::$property_map[$matches[1]];
51 $obj->{$prop} = $node->attributes->getNamedItem('content')->value;
52 // I don't care right now if they're empty
54 if (isset($obj->type)) {
55 // Loop through each known OpenGraph type where we have a match in oEmbed
56 foreach (self::$type_regex_map as $pattern=>$replacement) {
58 if (preg_match($pattern, $obj->type, $matches)) {
59 $obj->type = is_int($replacement)
60 ? $matches[$replacement]
65 // If it's not known to our type map, we just pass it through in hopes of it getting handled anyway
66 } elseif (isset($obj->url)) {
67 // If no type is set but we have a URL, let's set type=link