]> git.mxchange.org Git - friendica.git/blob - src/Model/Storage/ExternalResource.php
Use signed requests for fetching private images
[friendica.git] / src / Model / Storage / ExternalResource.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\Model\Storage;
23
24 use BadMethodCallException;
25 use Friendica\Util\HTTPSignature;
26
27 /**
28  * External resource storage class
29  *
30  * This class is used to load external resources, like images.
31  * Is not intended to be selectable by admins as default storage class.
32  */
33 class ExternalResource implements IStorage
34 {
35         const NAME = 'ExternalResource';
36
37         /**
38          * @inheritDoc
39          */
40         public function get(string $reference)
41         {
42                 $data = json_decode($reference);
43                 if (empty($data->url)) {
44                         return "";
45                 }
46
47                 $parts = parse_url($data->url);
48                 if (empty($parts['scheme']) || empty($parts['host'])) {
49                         return "";
50                 }
51
52                 $fetchResult = HTTPSignature::fetchRaw($data->url, $data->uid);
53                 if ($fetchResult->isSuccess()) {
54                         return $fetchResult->getBody();
55                 } else {
56                         return "";
57                 }
58         }
59
60         /**
61          * @inheritDoc
62          */
63         public function put(string $data, string $reference = '')
64         {
65                 throw new BadMethodCallException();
66         }
67
68         public function delete(string $reference)
69         {
70                 throw new BadMethodCallException();
71         }
72
73         /**
74          * @inheritDoc
75          */
76         public function getOptions()
77         {
78                 return [];
79         }
80
81         /**
82          * @inheritDoc
83          */
84         public function saveOptions(array $data)
85         {
86                 return [];
87         }
88
89         /**
90          * @inheritDoc
91          */
92         public function __toString()
93         {
94                 return self::NAME;
95         }
96
97         /**
98          * @inheritDoc
99          */
100         public static function getName()
101         {
102                 return self::NAME;
103         }
104 }