]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Oembed/lib/opengraphhelper.php
OpenGraph image/thumbnail width and height
[quix0rs-gnu-social.git] / plugins / Oembed / lib / opengraphhelper.php
1 <?php
2
3 if (!defined('GNUSOCIAL')) { exit(1); }
4
5
6 /**
7  * Utility class to get OpenGraph data from HTML DOMs etc.
8  */
9 class OpenGraphHelper
10 {
11     const KEY_REGEX = '/^og\:(\w+(?:\:\w+)?)/';
12     protected static $property_map = [
13                             'site_name'      => 'provider_name',
14                             'title'          => 'title',
15                             'description'    => 'html',
16                             'type'           => 'type',
17                             'url'            => 'url',
18                             'image'          => 'thumbnail_url',
19                             'image:height'   => 'thumbnail_height',
20                             'image:width'    => 'thumbnail_width',
21                             ];
22
23     // This regex map has:    /pattern(match)/ => matchindex | string
24     protected static $type_regex_map = [
25                             '/^(video)/'   => 1,
26                             '/^image/'     => 'photo',
27                             ];
28
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()) {
35                 continue;
36             }
37             $property = $node->attributes->getNamedItem('property');
38             $matches = array();
39             if ($property === null || !preg_match(self::KEY_REGEX, $property->value, $matches)) {
40                 // not property="og:something"
41                 continue;
42             }
43             if (!isset(self::$property_map[$matches[1]])) {
44                 // unknown metadata property, nothing we would care about anyway
45                 continue;
46             }
47
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
51         }
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) {
55                 $matches = array();
56                 if (preg_match($pattern, $obj->type, $matches)) {
57                     $obj->type = is_int($replacement)
58                                     ? $matches[$replacement]
59                                     : $replacement;
60                     break;
61                 }
62             }
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
66             $obj->type = 'link';
67         }
68         return $obj;
69     }
70 }