Update copyright
[friendica.git/.git] / src / Object / OEmbed.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Object;
23
24 /**
25  * OEmbed data object
26  *
27  * @see https://oembed.com/#section2.3
28  *
29  * @author Hypolite Petovan <hypolite@mrpetovan.com>
30  */
31 class OEmbed
32 {
33         public $embed_url        = '';
34
35         public $type             = '';
36         public $title            = '';
37         public $author_name      = '';
38         public $author_url       = '';
39         public $provider_name    = '';
40         public $provider_url     = '';
41         public $cache_age        = '';
42         public $thumbnail_url    = '';
43         public $thumbnail_width  = '';
44         public $thumbnail_height = '';
45         public $html             = '';
46         public $url              = '';
47         public $width            = '';
48         public $height           = '';
49
50         public function __construct($embed_url)
51         {
52                 $this->embed_url = $embed_url;
53         }
54
55         public function parseJSON($json_string)
56         {
57                 $properties = json_decode($json_string, true);
58
59                 if (empty($properties)) {
60                         return;
61                 }
62
63                 foreach ($properties as $key => $value) {
64                         if (in_array($key, ['thumbnail_width', 'thumbnail_height', 'width', 'height'])) {
65                                 // These values should be numbers, so ensure that they really are numbers.
66                                 $value = (int)$value;
67                         } elseif (is_array($value)) {
68                                 // Ignoring arrays.
69                         } elseif ($key != 'html') {
70                                 // Avoid being able to inject some ugly stuff through these fields.
71                                 $value = htmlentities($value);
72                         } else {
73                                 /// @todo Add a way to sanitize the html as well, possibly with an <iframe>?
74                                 $value = mb_convert_encoding($value, 'HTML-ENTITIES', mb_detect_encoding($value));
75                         }
76
77                         if (property_exists(__CLASS__, $key)) {
78                                 $this->{$key} = $value;
79                         }
80                 }
81         }
82 }