]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Oembed/lib/opengraphhelper.php
Debugging output in OStatus for easier reading+greping
[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         $obj->version = '1.0';  // fake it til u make it
32
33         $nodes = $dom->getElementsByTagName('meta');
34         for ($i = 0; $i < $nodes->length; $i++) {
35             $node = $nodes->item($i);
36             if (!$node->hasAttributes()) {
37                 continue;
38             }
39             $property = $node->attributes->getNamedItem('property');
40             $matches = array();
41             if ($property === null || !preg_match(self::KEY_REGEX, $property->value, $matches)) {
42                 // not property="og:something"
43                 continue;
44             }
45             if (!isset(self::$property_map[$matches[1]])) {
46                 // unknown metadata property, nothing we would care about anyway
47                 continue;
48             }
49
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
53         }
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) {
57                 $matches = array();
58                 if (preg_match($pattern, $obj->type, $matches)) {
59                     $obj->type = is_int($replacement)
60                                     ? $matches[$replacement]
61                                     : $replacement;
62                     break;
63                 }
64             }
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
68             $obj->type = 'link';
69         }
70         return $obj;
71     }
72 }