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 $nodes = $dom->getElementsByTagName('meta');
32 for ($i = 0; $i < $nodes->length; $i++) {
33 $node = $nodes->item($i);
34 if (!$node->hasAttributes()) {
37 $property = $node->attributes->getNamedItem('property');
39 if ($property === null || !preg_match(self::KEY_REGEX, $property->value, $matches)) {
40 // not property="og:something"
43 if (!isset(self::$property_map[$matches[1]])) {
44 // unknown metadata property, nothing we would care about anyway
48 $prop = self::$property_map[$matches[1]];
49 $obj->{$prop} = $node->attributes->getNamedItem('content')->value;
50 // I don't care right now if they're empty
52 if (isset($obj->type)) {
53 // Loop through each known OpenGraph type where we have a match in oEmbed
54 foreach (self::$type_regex_map as $pattern=>$replacement) {
56 if (preg_match($pattern, $obj->type, $matches)) {
57 $obj->type = is_int($replacement)
58 ? $matches[$replacement]
63 // If it's not known to our type map, we just pass it through in hopes of it getting handled anyway
64 } elseif (isset($obj->url)) {
65 // If no type is set but we have a URL, let's set type=link