]> git.mxchange.org Git - friendica.git/blob - src/Object/OEmbed.php
Issue 13221: Diaspora posts are now stored correctly
[friendica.git] / src / Object / OEmbed.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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 $description      = '';
38         public $author_name      = '';
39         public $author_url       = '';
40         public $provider_name    = '';
41         public $provider_url     = '';
42         public $cache_age        = '';
43         public $thumbnail_url    = '';
44         public $thumbnail_width  = '';
45         public $thumbnail_height = '';
46         public $html             = '';
47         public $url              = '';
48         public $width            = '';
49         public $height           = '';
50
51         public function __construct($embed_url)
52         {
53                 $this->embed_url = $embed_url;
54         }
55
56         public function parseJSON($json_string)
57         {
58                 $properties = json_decode($json_string, true);
59
60                 if (empty($properties)) {
61                         return;
62                 }
63
64                 foreach ($properties as $key => $value) {
65                         if (in_array($key, ['thumbnail_width', 'thumbnail_height', 'width', 'height'])) {
66                                 // These values should be numbers, so ensure that they really are numbers.
67                                 $value = (int)$value;
68                         } elseif (is_array($value)) {
69                                 // Ignoring arrays.
70                         } elseif ($key != 'html') {
71                                 // Avoid being able to inject some ugly stuff through these fields.
72                                 $value = htmlentities($value);
73                         } else {
74                                 /// @todo Add a way to sanitize the html as well, possibly with an <iframe>?
75                                 $value = mb_convert_encoding($value, 'HTML-ENTITIES', mb_detect_encoding($value));
76                         }
77
78                         if (property_exists(__CLASS__, $key)) {
79                                 $this->{$key} = $value;
80                         }
81                 }
82         }
83 }