]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Friendica/Photo.php
API: The legacy API finally moved
[friendica.git] / src / Factory / Api / Friendica / Photo.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Factory\Api\Friendica;
23
24 use Friendica\App\BaseURL;
25 use Friendica\BaseFactory;
26 use Friendica\Database\DBA;
27 use Friendica\Factory\Api\Twitter\Status;
28 use Friendica\Model\Photo as ModelPhoto;
29 use Friendica\Model\Post;
30 use Friendica\Network\HTTPException;
31 use Psr\Log\LoggerInterface;
32 use Friendica\Util\Images;
33
34 class Photo extends BaseFactory
35 {
36         /** @var BaseURL */
37         private $baseUrl;
38         /** @var Status */
39         private $status;
40         /** @var Activities */
41         private $activities;
42
43         public function __construct(LoggerInterface $logger, BaseURL $baseURL, Status $status, Activities $activities)
44         {
45                 parent::__construct($logger);
46
47                 $this->activities = $activities;
48                 $this->status     = $status;
49                 $this->baseUrl    = $baseURL;
50         }
51
52         /**
53          * @param string $photo_id
54          * @param int    $scale
55          * @param int    $uid
56          * @param string $type
57          * @return Array
58          */
59         public function createFromId(string $photo_id, int $scale = null, int $uid, string $type = 'json', bool $with_posts = true): array
60         {
61                 $fields = ['resource-id', 'created', 'edited', 'title', 'desc', 'album', 'filename','type',
62                 'height', 'width', 'datasize', 'profile', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid',
63                 'backend-class', 'backend-ref', 'id', 'scale'];
64
65                 $condition = ['uid' => $uid, 'resource-id' => $photo_id];
66                 if (is_int($scale)) {
67                         $fields = array_merge(['data'], $fields);
68                         $condition['scale'] = $scale;
69                 }
70
71                 $photos = ModelPhoto::selectToArray($fields, $condition);
72                 if (empty($photos)) {
73                         throw new HTTPException\NotFoundException();
74                 }
75                 $data = $photos[0];
76                 $data['id'] = $data['resource-id'];
77                 if (is_int($scale)) {
78                         $data['data'] = base64_encode(ModelPhoto::getImageDataForPhoto($data));
79                 } else {
80                         unset($data['datasize']); //needed only with scale param
81                 }
82
83                 if ($type == 'xml') {
84                         $data['links'] = [];
85                 } else {
86                         $data['link'] = [];
87                 }
88
89                 foreach ($photos as $id => $photo) {
90                         $link = $this->baseUrl->get() . '/photo/' . $data['resource-id'] . '-' . $photo['scale'] . Images::getExtensionByMimeType($data['type']);
91                         if ($type == 'xml') {
92                                 $data['links'][$photo['scale'] . ':link']['@attributes'] = [
93                                         'type' => $data['type'],
94                                         'scale' => $photo['scale'],
95                                         'href' => $link
96                                 ];
97                         } else {
98                                 $data['link'][$id] = $link;
99                         }
100                 }
101
102                 unset($data['backend-class']);
103                 unset($data['backend-ref']);
104                 unset($data['resource-id']);
105                 unset($data['scale']);
106         
107                 if ($with_posts) {
108                         // retrieve item element for getting activities (like, dislike etc.) related to photo
109                         $condition = ['uid' => $uid, 'resource-id' => $photo_id];
110                         $item = Post::selectFirst(['id', 'uid', 'uri', 'uri-id', 'parent', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid'], $condition);
111                 }
112                 if (!empty($item)) {
113                         $data['friendica_activities'] = $this->activities->createFromUriId($item['uri-id'], $item['uid'], $type);
114         
115                         // retrieve comments on photo
116                         $condition = ["`parent` = ? AND `uid` = ? AND `gravity` IN (?, ?)",
117                                 $item['parent'], $uid, GRAVITY_PARENT, GRAVITY_COMMENT];
118                 
119                         $statuses = Post::selectForUser($uid, [], $condition);
120                 
121                         // prepare output of comments
122                         $commentData = [];
123                         while ($status = DBA::fetch($statuses)) {
124                                 $commentData[] = $this->status->createFromUriId($status['uri-id'], $status['uid'])->toArray();
125                         }
126                         DBA::close($statuses);
127                 
128                         $comments = [];
129                         if ($type == 'xml') {
130                                 $k = 0;
131                                 foreach ($commentData as $comment) {
132                                         $comments[$k++ . ':comment'] = $comment;
133                                 }
134                         } else {
135                                 foreach ($commentData as $comment) {
136                                         $comments[] = $comment;
137                                 }
138                         }
139                         $data['friendica_comments'] = $comments;
140                 
141                         // include info if rights on photo and rights on item are mismatching
142                         $data['rights_mismatch'] = $data['allow_cid'] != $item['allow_cid'] ||
143                                 $data['deny_cid'] != $item['deny_cid'] ||
144                                 $data['allow_gid'] != $item['allow_gid'] ||
145                                 $data['deny_gid'] != $item['deny_gid'];
146                 } elseif ($with_posts) {
147                         $data['friendica_activities'] = [];
148                         $data['friendica_comments']   = [];
149                         $data['rights_mismatch'] = false;
150                 }
151         
152                 return $data;
153         }
154 }