]> git.mxchange.org Git - friendica.git/blob - mod/photos.php
Get rid of App->user completely
[friendica.git] / mod / photos.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 use Friendica\App;
23 use Friendica\Content\Feature;
24 use Friendica\Content\Nav;
25 use Friendica\Content\Pager;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Content\Widget;
28 use Friendica\Core\ACL;
29 use Friendica\Core\Addon;
30 use Friendica\Core\Hook;
31 use Friendica\Core\Logger;
32 use Friendica\Core\Renderer;
33 use Friendica\Core\Session;
34 use Friendica\Core\System;
35 use Friendica\Database\DBA;
36 use Friendica\DI;
37 use Friendica\Model\Contact;
38 use Friendica\Model\Item;
39 use Friendica\Model\Photo;
40 use Friendica\Model\Post;
41 use Friendica\Model\Profile;
42 use Friendica\Model\Tag;
43 use Friendica\Model\User;
44 use Friendica\Module\BaseProfile;
45 use Friendica\Network\Probe;
46 use Friendica\Object\Image;
47 use Friendica\Protocol\Activity;
48 use Friendica\Util\Crypto;
49 use Friendica\Util\DateTimeFormat;
50 use Friendica\Util\Images;
51 use Friendica\Util\Map;
52 use Friendica\Security\Security;
53 use Friendica\Util\Strings;
54 use Friendica\Util\Temporal;
55 use Friendica\Util\XML;
56 use Friendica\Network\HTTPException;
57
58 function photos_init(App $a) {
59
60         if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
61                 return;
62         }
63
64         Nav::setSelected('home');
65
66         if (DI::args()->getArgc() > 1) {
67                 $owner = User::getOwnerDataByNick(DI::args()->getArgv()[1]);
68
69                 $is_owner = (local_user() && (local_user() == $owner['uid']));
70
71                 $albums = Photo::getAlbums($owner['uid']);
72
73                 $albums_visible = ((intval($owner['hidewall']) && !Session::isAuthenticated()) ? false : true);
74
75                 // add various encodings to the array so we can just loop through and pick them out in a template
76                 $ret = ['success' => false];
77
78                 if ($albums) {
79                         if ($albums_visible) {
80                                 $ret['success'] = true;
81                         }
82
83                         $ret['albums'] = [];
84                         foreach ($albums as $k => $album) {
85                                 //hide profile photos to others
86                                 if (!$is_owner && !Session::getRemoteContactID($owner['uid']) && ($album['album'] == DI::l10n()->t('Profile Photos')))
87                                         continue;
88                                 $entry = [
89                                         'text'      => $album['album'],
90                                         'total'     => $album['total'],
91                                         'url'       => 'photos/' . $owner['nickname'] . '/album/' . bin2hex($album['album']),
92                                         'urlencode' => urlencode($album['album']),
93                                         'bin2hex'   => bin2hex($album['album'])
94                                 ];
95                                 $ret['albums'][] = $entry;
96                         }
97                 }
98
99                 if (local_user() && $owner['uid'] == local_user()) {
100                         $can_post = true;
101                 } else {
102                         $can_post = false;
103                 }
104
105                 if ($ret['success']) {
106                         $photo_albums_widget = Renderer::replaceMacros(Renderer::getMarkupTemplate('photo_albums.tpl'), [
107                                 '$nick'     => $owner['nickname'],
108                                 '$title'    => DI::l10n()->t('Photo Albums'),
109                                 '$recent'   => DI::l10n()->t('Recent Photos'),
110                                 '$albums'   => $ret['albums'],
111                                 '$upload'   => [DI::l10n()->t('Upload New Photos'), 'photos/' . $owner['nickname'] . '/upload'],
112                                 '$can_post' => $can_post
113                         ]);
114                 }
115
116                 if (empty(DI::page()['aside'])) {
117                         DI::page()['aside'] = '';
118                 }
119
120                 DI::page()['aside'] .= Widget\VCard::getHTML($owner);
121
122                 if (!empty($photo_albums_widget)) {
123                         DI::page()['aside'] .= $photo_albums_widget;
124                 }
125
126                 $tpl = Renderer::getMarkupTemplate("photos_head.tpl");
127
128                 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl,[
129                         '$ispublic' => DI::l10n()->t('everybody')
130                 ]);
131         }
132
133         return;
134 }
135
136 function photos_post(App $a)
137 {
138         $user = User::getByNickname(DI::args()->getArgv()[1]);
139         if (!DBA::isResult($user)) {
140                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
141         }
142
143         $phototypes = Images::supportedTypes();
144
145         $can_post  = false;
146         $visitor   = 0;
147
148         $page_owner_uid = intval($user['uid']);
149         $community_page = $user['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
150
151         if (local_user() && (local_user() == $page_owner_uid)) {
152                 $can_post = true;
153         } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
154                 $contact_id = Session::getRemoteContactID($page_owner_uid);
155                 $can_post = true;
156                 $visitor = $contact_id;
157         }
158
159         if (!$can_post) {
160                 notice(DI::l10n()->t('Permission denied.'));
161                 exit();
162         }
163
164         $owner_record = User::getOwnerDataById($page_owner_uid);
165
166         if (!$owner_record) {
167                 notice(DI::l10n()->t('Contact information unavailable'));
168                 DI::logger()->info('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
169                 exit();
170         }
171
172         $aclFormatter = DI::aclFormatter();
173         $str_contact_allow = isset($_REQUEST['contact_allow']) ? $aclFormatter->toString($_REQUEST['contact_allow']) : $owner_record['allow_cid'] ?? '';
174         $str_group_allow   = isset($_REQUEST['group_allow'])   ? $aclFormatter->toString($_REQUEST['group_allow'])   : $owner_record['allow_gid'] ?? '';
175         $str_contact_deny  = isset($_REQUEST['contact_deny'])  ? $aclFormatter->toString($_REQUEST['contact_deny'])  : $owner_record['deny_cid']  ?? '';
176         $str_group_deny    = isset($_REQUEST['group_deny'])    ? $aclFormatter->toString($_REQUEST['group_deny'])    : $owner_record['deny_gid']  ?? '';
177
178         $visibility = $_REQUEST['visibility'] ?? '';
179         if ($visibility === 'public') {
180                 // The ACL selector introduced in version 2019.12 sends ACL input data even when the Public visibility is selected
181                 $str_contact_allow = $str_group_allow = $str_contact_deny = $str_group_deny = '';
182         } else if ($visibility === 'custom') {
183                 // Since we know from the visibility parameter the item should be private, we have to prevent the empty ACL
184                 // case that would make it public. So we always append the author's contact id to the allowed contacts.
185                 // See https://github.com/friendica/friendica/issues/9672
186                 $str_contact_allow .= $aclFormatter->toString(Contact::getPublicIdByUserId($page_owner_uid));
187         }
188
189         if (DI::args()->getArgc() > 3 && DI::args()->getArgv()[2] === 'album') {
190                 if (!Strings::isHex(DI::args()->getArgv()[3])) {
191                         DI::baseUrl()->redirect('photos/' . $user['nickname'] . '/album');
192                 }
193                 $album = hex2bin(DI::args()->getArgv()[3]);
194
195                 if ($album === DI::l10n()->t('Profile Photos') || $album === Photo::CONTACT_PHOTOS || $album === DI::l10n()->t(Photo::CONTACT_PHOTOS)) {
196                         DI::baseUrl()->redirect($_SESSION['photo_return']);
197                         return; // NOTREACHED
198                 }
199
200                 $r = q("SELECT `album` FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
201                         DBA::escape($album),
202                         intval($page_owner_uid)
203                 );
204
205                 if (!DBA::isResult($r)) {
206                         notice(DI::l10n()->t('Album not found.'));
207                         DI::baseUrl()->redirect('photos/' . $user['nickname'] . '/album');
208                         return; // NOTREACHED
209                 }
210
211                 // Check if the user has responded to a delete confirmation query
212                 if (!empty($_REQUEST['canceled'])) {
213                         DI::baseUrl()->redirect('photos/' . $user['nickname'] . '/album/' . DI::args()->getArgv()[3]);
214                 }
215
216                 // RENAME photo album
217                 $newalbum = Strings::escapeTags(trim($_POST['albumname']));
218                 if ($newalbum != $album) {
219                         q("UPDATE `photo` SET `album` = '%s' WHERE `album` = '%s' AND `uid` = %d",
220                                 DBA::escape($newalbum),
221                                 DBA::escape($album),
222                                 intval($page_owner_uid)
223                         );
224                         // Update the photo albums cache
225                         Photo::clearAlbumCache($page_owner_uid);
226
227                         DI::baseUrl()->redirect('photos/' . $a->getNickname() . '/album/' . bin2hex($newalbum));
228                         return; // NOTREACHED
229                 }
230
231                 /*
232                  * DELETE all photos filed in a given album
233                  */
234                 if (!empty($_POST['dropalbum'])) {
235                         $res = [];
236
237                         // get the list of photos we are about to delete
238                         if ($visitor) {
239                                 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `album` = '%s'",
240                                         intval($visitor),
241                                         intval($page_owner_uid),
242                                         DBA::escape($album)
243                                 );
244                         } else {
245                                 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'",
246                                         intval(local_user()),
247                                         DBA::escape($album)
248                                 );
249                         }
250
251                         if (DBA::isResult($r)) {
252                                 foreach ($r as $rr) {
253                                         $res[] = $rr['rid'];
254                                 }
255
256                                 // remove the associated photos
257                                 Photo::delete(['resource-id' => $res, 'uid' => $page_owner_uid]);
258
259                                 // find and delete the corresponding item with all the comments and likes/dislikes
260                                 Item::deleteForUser(['resource-id' => $res, 'uid' => $page_owner_uid], $page_owner_uid);
261
262                                 // Update the photo albums cache
263                                 Photo::clearAlbumCache($page_owner_uid);
264                                 notice(DI::l10n()->t('Album successfully deleted'));
265                         } else {
266                                 notice(DI::l10n()->t('Album was empty.'));
267                         }
268                 }
269
270                 DI::baseUrl()->redirect('photos/' . $user['nickname'] . '/album');
271         }
272
273         if (DI::args()->getArgc() > 3 && DI::args()->getArgv()[2] === 'image') {
274                 // Check if the user has responded to a delete confirmation query for a single photo
275                 if (!empty($_POST['canceled'])) {
276                         DI::baseUrl()->redirect('photos/' . DI::args()->getArgv()[1] . '/image/' . DI::args()->getArgv()[3]);
277                 }
278
279                 if (!empty($_POST['delete'])) {
280                         // same as above but remove single photo
281                         if ($visitor) {
282                                 $condition = ['contact-id' => $visitor, 'uid' => $page_owner_uid, 'resource-id' => DI::args()->getArgv()[3]];
283
284                         } else {
285                                 $condition = ['uid' => local_user(), 'resource-id' => DI::args()->getArgv()[3]];
286                         }
287
288                         $photo = DBA::selectFirst('photo', ['resource-id'], $condition);
289
290                         if (DBA::isResult($photo)) {
291                                 Photo::delete(['uid' => $page_owner_uid, 'resource-id' => $photo['resource-id']]);
292
293                                 Item::deleteForUser(['resource-id' => $photo['resource-id'], 'uid' => $page_owner_uid], $page_owner_uid);
294
295                                 // Update the photo albums cache
296                                 Photo::clearAlbumCache($page_owner_uid);
297                         } else {
298                                 notice(DI::l10n()->t('Failed to delete the photo.'));
299                                 DI::baseUrl()->redirect('photos/' . DI::args()->getArgv()[1] . '/image/' . DI::args()->getArgv()[3]);
300                         }
301
302                         DI::baseUrl()->redirect('photos/' . DI::args()->getArgv()[1]);
303                         return; // NOTREACHED
304                 }
305         }
306
307         if (DI::args()->getArgc() > 2 && (!empty($_POST['desc']) || !empty($_POST['newtag']) || isset($_POST['albname']))) {
308                 $desc        = !empty($_POST['desc'])      ? Strings::escapeTags(trim($_POST['desc']))      : '';
309                 $rawtags     = !empty($_POST['newtag'])    ? Strings::escapeTags(trim($_POST['newtag']))    : '';
310                 $item_id     = !empty($_POST['item_id'])   ? intval($_POST['item_id'])                      : 0;
311                 $albname     = !empty($_POST['albname'])   ? trim($_POST['albname'])                        : '';
312                 $origaname   = !empty($_POST['origaname']) ? Strings::escapeTags(trim($_POST['origaname'])) : '';
313
314                 $resource_id = DI::args()->getArgv()[3];
315
316                 if (!strlen($albname)) {
317                         $albname = DateTimeFormat::localNow('Y');
318                 }
319
320                 if (!empty($_POST['rotate']) && (intval($_POST['rotate']) == 1 || intval($_POST['rotate']) == 2)) {
321                         Logger::log('rotate');
322
323                         $photo = Photo::getPhotoForUser($page_owner_uid, $resource_id);
324
325                         if (DBA::isResult($photo)) {
326                                 $image = Photo::getImageForPhoto($photo);
327
328                                 if ($image->isValid()) {
329                                         $rotate_deg = ((intval($_POST['rotate']) == 1) ? 270 : 90);
330                                         $image->rotate($rotate_deg);
331
332                                         $width  = $image->getWidth();
333                                         $height = $image->getHeight();
334
335                                         Photo::update(['height' => $height, 'width' => $width], ['resource-id' => $resource_id, 'uid' => $page_owner_uid, 'scale' => 0], $image);
336
337                                         if ($width > 640 || $height > 640) {
338                                                 $image->scaleDown(640);
339                                                 $width  = $image->getWidth();
340                                                 $height = $image->getHeight();
341
342                                                 Photo::update(['height' => $height, 'width' => $width], ['resource-id' => $resource_id, 'uid' => $page_owner_uid, 'scale' => 1], $image);
343                                         }
344
345                                         if ($width > 320 || $height > 320) {
346                                                 $image->scaleDown(320);
347                                                 $width  = $image->getWidth();
348                                                 $height = $image->getHeight();
349
350                                                 Photo::update(['height' => $height, 'width' => $width], ['resource-id' => $resource_id, 'uid' => $page_owner_uid, 'scale' => 2], $image);
351                                         }
352                                 }
353                         }
354                 }
355
356                 $photos_stmt = DBA::select('photo', [], ['resource-id' => $resource_id, 'uid' => $page_owner_uid], ['order' => ['scale' => true]]);
357
358                 $photos = DBA::toArray($photos_stmt);
359
360                 if (DBA::isResult($photos)) {
361                         $photo = $photos[0];
362                         $ext = $phototypes[$photo['type']];
363                         Photo::update(
364                                 ['desc' => $desc, 'album' => $albname, 'allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow, 'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny],
365                                 ['resource-id' => $resource_id, 'uid' => $page_owner_uid]
366                         );
367
368                         // Update the photo albums cache if album name was changed
369                         if ($albname !== $origaname) {
370                                 Photo::clearAlbumCache($page_owner_uid);
371                         }
372                         /* Don't make the item visible if the only change was the album name */
373
374                         $visibility = 0;
375                         if ($photo['desc'] !== $desc || strlen($rawtags)) {
376                                 $visibility = 1;
377                         }
378                 }
379
380                 if (DBA::isResult($photos) && !$item_id) {
381                         // Create item container
382                         $title = '';
383                         $uri = Item::newURI($page_owner_uid);
384
385                         $arr = [];
386                         $arr['guid']          = System::createUUID();
387                         $arr['uid']           = $page_owner_uid;
388                         $arr['uri']           = $uri;
389                         $arr['post-type']     = Item::PT_IMAGE;
390                         $arr['wall']          = 1;
391                         $arr['resource-id']   = $photo['resource-id'];
392                         $arr['contact-id']    = $owner_record['id'];
393                         $arr['owner-name']    = $owner_record['name'];
394                         $arr['owner-link']    = $owner_record['url'];
395                         $arr['owner-avatar']  = $owner_record['thumb'];
396                         $arr['author-name']   = $owner_record['name'];
397                         $arr['author-link']   = $owner_record['url'];
398                         $arr['author-avatar'] = $owner_record['thumb'];
399                         $arr['title']         = $title;
400                         $arr['allow_cid']     = $photo['allow_cid'];
401                         $arr['allow_gid']     = $photo['allow_gid'];
402                         $arr['deny_cid']      = $photo['deny_cid'];
403                         $arr['deny_gid']      = $photo['deny_gid'];
404                         $arr['visible']       = $visibility;
405                         $arr['origin']        = 1;
406
407                         $arr['body']          = '[url=' . DI::baseUrl() . '/photos/' . $user['nickname'] . '/image/' . $photo['resource-id'] . ']'
408                                                 . '[img]' . DI::baseUrl() . '/photo/' . $photo['resource-id'] . '-' . $photo['scale'] . '.'. $ext . '[/img]'
409                                                 . '[/url]';
410
411                         $item_id = Item::insert($arr);
412                 }
413
414                 if ($item_id) {
415                         $item = Post::selectFirst(['inform', 'uri-id'], ['id' => $item_id, 'uid' => $page_owner_uid]);
416
417                         if (DBA::isResult($item)) {
418                                 $old_inform = $item['inform'];
419                         }
420                 }
421
422                 if (strlen($rawtags)) {
423                         $inform   = '';
424
425                         // if the new tag doesn't have a namespace specifier (@foo or #foo) give it a hashtag
426                         $x = substr($rawtags, 0, 1);
427                         if ($x !== '@' && $x !== '#') {
428                                 $rawtags = '#' . $rawtags;
429                         }
430
431                         $taginfo = [];
432                         $tags = BBCode::getTags($rawtags);
433
434                         if (count($tags)) {
435                                 foreach ($tags as $tag) {
436                                         if (strpos($tag, '@') === 0) {
437                                                 $profile = '';
438                                                 $contact = null;
439                                                 $name = substr($tag,1);
440
441                                                 if ((strpos($name, '@')) || (strpos($name, 'http://'))) {
442                                                         $newname = $name;
443                                                         $links = @Probe::lrdd($name);
444
445                                                         if (count($links)) {
446                                                                 foreach ($links as $link) {
447                                                                         if ($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page') {
448                                                                                 $profile = $link['@attributes']['href'];
449                                                                         }
450
451                                                                         if ($link['@attributes']['rel'] === 'salmon') {
452                                                                                 $salmon = '$url:' . str_replace(',', '%sc', $link['@attributes']['href']);
453
454                                                                                 if (strlen($inform)) {
455                                                                                         $inform .= ',';
456                                                                                 }
457
458                                                                                 $inform .= $salmon;
459                                                                         }
460                                                                 }
461                                                         }
462
463                                                         $taginfo[] = [$newname, $profile, $salmon];
464                                                 } else {
465                                                         $newname = $name;
466                                                         $tagcid = 0;
467
468                                                         if (strrpos($newname, '+')) {
469                                                                 $tagcid = intval(substr($newname, strrpos($newname, '+') + 1));
470                                                         }
471
472                                                         if ($tagcid) {
473                                                                 $contact = DBA::selectFirst('contact', [], ['id' => $tagcid, 'uid' => $page_owner_uid]);
474                                                         } else {
475                                                                 $newname = str_replace('_',' ',$name);
476
477                                                                 //select someone from this user's contacts by name
478                                                                 $contact = DBA::selectFirst('contact', [], ['name' => $newname, 'uid' => $page_owner_uid]);
479                                                                 if (!DBA::isResult($contact)) {
480                                                                         //select someone by attag or nick and the name passed in
481                                                                         $contact = DBA::selectFirst('contact', [],
482                                                                                 ['(`attag` = ? OR `nick` = ?) AND `uid` = ?', $name, $name, $page_owner_uid],
483                                                                                 ['order' => ['attag' => true]]
484                                                                         );
485                                                                 }
486                                                         }
487
488                                                         if (DBA::isResult($contact)) {
489                                                                 $newname = $contact['name'];
490                                                                 $profile = $contact['url'];
491
492                                                                 $notify = 'cid:' . $contact['id'];
493                                                                 if (strlen($inform)) {
494                                                                         $inform .= ',';
495                                                                 }
496                                                                 $inform .= $notify;
497                                                         }
498                                                 }
499
500                                                 if ($profile) {
501                                                         if (!empty($contact)) {
502                                                                 $taginfo[] = [$newname, $profile, $notify, $contact];
503                                                         } else {
504                                                                 $taginfo[] = [$newname, $profile, $notify, null];
505                                                         }
506
507                                                         $profile = str_replace(',', '%2c', $profile);
508
509                                                         if (!empty($item['uri-id'])) {
510                                                                 Tag::store($item['uri-id'], Tag::MENTION, $newname, $profile);
511                                                         }
512                                                 }
513                                         } elseif (strpos($tag, '#') === 0) {
514                                                 $tagname = substr($tag, 1);
515                                                 if (!empty($item['uri-id'])) {
516                                                         Tag::store($item['uri-id'], Tag::HASHTAG, $tagname);
517                                                 }
518                                         }
519                                 }
520                         }
521
522                         $newinform = $old_inform ?? '';
523                         if (strlen($newinform) && strlen($inform)) {
524                                 $newinform .= ',';
525                         }
526                         $newinform .= $inform;
527
528                         $fields = ['inform' => $newinform, 'edited' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()];
529                         $condition = ['id' => $item_id];
530                         Item::update($fields, $condition);
531
532                         $best = 0;
533                         foreach ($photos as $scales) {
534                                 if (intval($scales['scale']) == 2) {
535                                         $best = 2;
536                                         break;
537                                 }
538
539                                 if (intval($scales['scale']) == 4) {
540                                         $best = 4;
541                                         break;
542                                 }
543                         }
544
545                         if (count($taginfo)) {
546                                 foreach ($taginfo as $tagged) {
547                                         $uri = Item::newURI($page_owner_uid);
548
549                                         $arr = [];
550                                         $arr['guid']          = System::createUUID();
551                                         $arr['uid']           = $page_owner_uid;
552                                         $arr['uri']           = $uri;
553                                         $arr['wall']          = 1;
554                                         $arr['contact-id']    = $owner_record['id'];
555                                         $arr['owner-name']    = $owner_record['name'];
556                                         $arr['owner-link']    = $owner_record['url'];
557                                         $arr['owner-avatar']  = $owner_record['thumb'];
558                                         $arr['author-name']   = $owner_record['name'];
559                                         $arr['author-link']   = $owner_record['url'];
560                                         $arr['author-avatar'] = $owner_record['thumb'];
561                                         $arr['title']         = '';
562                                         $arr['allow_cid']     = $photo['allow_cid'];
563                                         $arr['allow_gid']     = $photo['allow_gid'];
564                                         $arr['deny_cid']      = $photo['deny_cid'];
565                                         $arr['deny_gid']      = $photo['deny_gid'];
566                                         $arr['visible']       = 1;
567                                         $arr['verb']          = Activity::TAG;
568                                         $arr['gravity']       = GRAVITY_PARENT;
569                                         $arr['object-type']   = Activity\ObjectType::PERSON;
570                                         $arr['target-type']   = Activity\ObjectType::IMAGE;
571                                         $arr['inform']        = $tagged[2];
572                                         $arr['origin']        = 1;
573                                         $arr['body']          = DI::l10n()->t('%1$s was tagged in %2$s by %3$s', '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]', '[url=' . DI::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo['resource-id'] . ']' . DI::l10n()->t('a photo') . '[/url]', '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]') ;
574                                         $arr['body'] .= "\n\n" . '[url=' . DI::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo['resource-id'] . ']' . '[img]' . DI::baseUrl() . "/photo/" . $photo['resource-id'] . '-' . $best . '.' . $ext . '[/img][/url]' . "\n" ;
575
576                                         $arr['object'] = '<object><type>' . Activity\ObjectType::PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>';
577                                         $arr['object'] .= '<link>' . XML::escape('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n");
578                                         if ($tagged[3]) {
579                                                 $arr['object'] .= XML::escape('<link rel="photo" type="' . $photo['type'] . '" href="' . $tagged[3]['photo'] . '" />' . "\n");
580                                         }
581                                         $arr['object'] .= '</link></object>' . "\n";
582
583                                         $arr['target'] = '<target><type>' . Activity\ObjectType::IMAGE . '</type><title>' . $photo['desc'] . '</title><id>'
584                                                 . DI::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo['resource-id'] . '</id>';
585                                         $arr['target'] .= '<link>' . XML::escape('<link rel="alternate" type="text/html" href="' . DI::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo['resource-id'] . '" />' . "\n" . '<link rel="preview" type="' . $photo['type'] . '" href="' . DI::baseUrl() . "/photo/" . $photo['resource-id'] . '-' . $best . '.' . $ext . '" />') . '</link></target>';
586
587                                         Item::insert($arr);
588                                 }
589                         }
590                 }
591                 DI::baseUrl()->redirect($_SESSION['photo_return']);
592                 return; // NOTREACHED
593         }
594
595
596         // default post action - upload a photo
597         Hook::callAll('photo_post_init', $_POST);
598
599         // Determine the album to use
600         $album    = trim($_REQUEST['album'] ?? '');
601         $newalbum = trim($_REQUEST['newalbum'] ?? '');
602
603         Logger::info('album= ' . $album . ' newalbum= ' . $newalbum);
604
605         if (!strlen($album)) {
606                 if (strlen($newalbum)) {
607                         $album = $newalbum;
608                 } else {
609                         $album = DateTimeFormat::localNow('Y');
610                 }
611         }
612
613         /*
614          * We create a wall item for every photo, but we don't want to
615          * overwhelm the data stream with a hundred newly uploaded photos.
616          * So we will make the first photo uploaded to this album in the last several hours
617          * visible by default, the rest will become visible over time when and if
618          * they acquire comments, likes, dislikes, and/or tags
619          */
620
621         $r = Photo::selectToArray([], ['`album` = ? AND `uid` = ? AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR', $album, $page_owner_uid]);
622
623         if (!DBA::isResult($r) || ($album == DI::l10n()->t('Profile Photos'))) {
624                 $visible = 1;
625         } else {
626                 $visible = 0;
627         }
628
629         if (!empty($_REQUEST['not_visible']) && $_REQUEST['not_visible'] !== 'false') {
630                 $visible = 0;
631         }
632
633         $ret = ['src' => '', 'filename' => '', 'filesize' => 0, 'type' => ''];
634
635         Hook::callAll('photo_post_file', $ret);
636
637         if (!empty($ret['src']) && !empty($ret['filesize'])) {
638                 $src      = $ret['src'];
639                 $filename = $ret['filename'];
640                 $filesize = $ret['filesize'];
641                 $type     = $ret['type'];
642                 $error    = UPLOAD_ERR_OK;
643         } elseif (!empty($_FILES['userfile'])) {
644                 $src      = $_FILES['userfile']['tmp_name'];
645                 $filename = basename($_FILES['userfile']['name']);
646                 $filesize = intval($_FILES['userfile']['size']);
647                 $type     = $_FILES['userfile']['type'];
648                 $error    = $_FILES['userfile']['error'];
649         } else {
650                 $error    = UPLOAD_ERR_NO_FILE;
651         }
652
653         if ($error !== UPLOAD_ERR_OK) {
654                 switch ($error) {
655                         case UPLOAD_ERR_INI_SIZE:
656                                 notice(DI::l10n()->t('Image exceeds size limit of %s', ini_get('upload_max_filesize')));
657                                 break;
658                         case UPLOAD_ERR_FORM_SIZE:
659                                 notice(DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($_REQUEST['MAX_FILE_SIZE'] ?? 0)));
660                                 break;
661                         case UPLOAD_ERR_PARTIAL:
662                                 notice(DI::l10n()->t('Image upload didn\'t complete, please try again'));
663                                 break;
664                         case UPLOAD_ERR_NO_FILE:
665                                 notice(DI::l10n()->t('Image file is missing'));
666                                 break;
667                         case UPLOAD_ERR_NO_TMP_DIR:
668                         case UPLOAD_ERR_CANT_WRITE:
669                         case UPLOAD_ERR_EXTENSION:
670                                 notice(DI::l10n()->t('Server can\'t accept new file upload at this time, please contact your administrator'));
671                                 break;
672                 }
673                 @unlink($src);
674                 $foo = 0;
675                 Hook::callAll('photo_post_end', $foo);
676                 return;
677         }
678
679         $type = Images::getMimeTypeBySource($src, $filename, $type);
680
681         Logger::log('photos: upload: received file: ' . $filename . ' as ' . $src . ' ('. $type . ') ' . $filesize . ' bytes', Logger::DEBUG);
682
683         $maximagesize = DI::config()->get('system', 'maximagesize');
684
685         if ($maximagesize && ($filesize > $maximagesize)) {
686                 notice(DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)));
687                 @unlink($src);
688                 $foo = 0;
689                 Hook::callAll('photo_post_end', $foo);
690                 return;
691         }
692
693         if (!$filesize) {
694                 notice(DI::l10n()->t('Image file is empty.'));
695                 @unlink($src);
696                 $foo = 0;
697                 Hook::callAll('photo_post_end', $foo);
698                 return;
699         }
700
701         Logger::log('mod/photos.php: photos_post(): loading the contents of ' . $src , Logger::DEBUG);
702
703         $imagedata = @file_get_contents($src);
704
705         $image = new Image($imagedata, $type);
706
707         if (!$image->isValid()) {
708                 Logger::log('mod/photos.php: photos_post(): unable to process image' , Logger::DEBUG);
709                 notice(DI::l10n()->t('Unable to process image.'));
710                 @unlink($src);
711                 $foo = 0;
712                 Hook::callAll('photo_post_end',$foo);
713                 return;
714         }
715
716         $exif = $image->orient($src);
717         @unlink($src);
718
719         $max_length = DI::config()->get('system', 'max_image_length');
720         if (!$max_length) {
721                 $max_length = MAX_IMAGE_LENGTH;
722         }
723         if ($max_length > 0) {
724                 $image->scaleDown($max_length);
725         }
726
727         $width  = $image->getWidth();
728         $height = $image->getHeight();
729
730         $smallest = 0;
731
732         $resource_id = Photo::newResource();
733
734         $r = Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
735
736         if (!$r) {
737                 Logger::log('mod/photos.php: photos_post(): image store failed', Logger::DEBUG);
738                 notice(DI::l10n()->t('Image upload failed.'));
739                 return;
740         }
741
742         if ($width > 640 || $height > 640) {
743                 $image->scaleDown(640);
744                 Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
745                 $smallest = 1;
746         }
747
748         if ($width > 320 || $height > 320) {
749                 $image->scaleDown(320);
750                 Photo::store($image, $page_owner_uid, $visitor, $resource_id, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
751                 $smallest = 2;
752         }
753
754         $uri = Item::newURI($page_owner_uid);
755
756         // Create item container
757         $lat = $lon = null;
758         if (!empty($exif['GPS']) && Feature::isEnabled($page_owner_uid, 'photo_location')) {
759                 $lat = Photo::getGps($exif['GPS']['GPSLatitude'], $exif['GPS']['GPSLatitudeRef']);
760                 $lon = Photo::getGps($exif['GPS']['GPSLongitude'], $exif['GPS']['GPSLongitudeRef']);
761         }
762
763         $arr = [];
764         if ($lat && $lon) {
765                 $arr['coord'] = $lat . ' ' . $lon;
766         }
767
768         $arr['guid']          = System::createUUID();
769         $arr['uid']           = $page_owner_uid;
770         $arr['uri']           = $uri;
771         $arr['post-type']     = Item::PT_IMAGE;
772         $arr['wall']          = 1;
773         $arr['resource-id']   = $resource_id;
774         $arr['contact-id']    = $owner_record['id'];
775         $arr['owner-name']    = $owner_record['name'];
776         $arr['owner-link']    = $owner_record['url'];
777         $arr['owner-avatar']  = $owner_record['thumb'];
778         $arr['author-name']   = $owner_record['name'];
779         $arr['author-link']   = $owner_record['url'];
780         $arr['author-avatar'] = $owner_record['thumb'];
781         $arr['title']         = '';
782         $arr['allow_cid']     = $str_contact_allow;
783         $arr['allow_gid']     = $str_group_allow;
784         $arr['deny_cid']      = $str_contact_deny;
785         $arr['deny_gid']      = $str_group_deny;
786         $arr['visible']       = $visible;
787         $arr['origin']        = 1;
788
789         $arr['body']          = '[url=' . DI::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $resource_id . ']'
790                                 . '[img]' . DI::baseUrl() . "/photo/{$resource_id}-{$smallest}.".$image->getExt() . '[/img]'
791                                 . '[/url]';
792
793         $item_id = Item::insert($arr);
794         // Update the photo albums cache
795         Photo::clearAlbumCache($page_owner_uid);
796
797         Hook::callAll('photo_post_end', $item_id);
798
799         // addon uploaders should call "exit()" within the photo_post_end hook
800         // if they do not wish to be redirected
801
802         DI::baseUrl()->redirect($_SESSION['photo_return']);
803         // NOTREACHED
804 }
805
806 function photos_content(App $a)
807 {
808         // URLs:
809         // photos/name
810         // photos/name/upload
811         // photos/name/upload/xxxxx (xxxxx is album name)
812         // photos/name/album/xxxxx
813         // photos/name/album/xxxxx/edit
814         // photos/name/album/xxxxx/drop
815         // photos/name/image/xxxxx
816         // photos/name/image/xxxxx/edit
817         // photos/name/image/xxxxx/drop
818
819         $user = User::getByNickname(DI::args()->getArgv()[1]);
820         if (!DBA::isResult($user)) {
821                 throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
822         }
823
824         if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
825                 notice(DI::l10n()->t('Public access denied.'));
826                 return;
827         }
828
829         if (empty($user)) {
830                 notice(DI::l10n()->t('No photos selected'));
831                 return;
832         }
833
834         $profile = Profile::getByUID($user['uid']);
835
836         $phototypes = Images::supportedTypes();
837
838         $_SESSION['photo_return'] = DI::args()->getCommand();
839
840         // Parse arguments
841         $datum = null;
842         if (DI::args()->getArgc() > 3) {
843                 $datatype = DI::args()->getArgv()[2];
844                 $datum = DI::args()->getArgv()[3];
845         } elseif ((DI::args()->getArgc() > 2) && (DI::args()->getArgv()[2] === 'upload')) {
846                 $datatype = 'upload';
847         } else {
848                 $datatype = 'summary';
849         }
850
851         if (DI::args()->getArgc() > 4) {
852                 $cmd = DI::args()->getArgv()[4];
853         } else {
854                 $cmd = 'view';
855         }
856
857         // Setup permissions structures
858         $can_post       = false;
859         $visitor        = 0;
860         $contact        = null;
861         $remote_contact = false;
862         $contact_id     = 0;
863         $edit           = '';
864         $drop           = '';
865
866         $owner_uid = $user['uid'];
867
868         $community_page = (($user['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
869
870         if (local_user() && (local_user() == $owner_uid)) {
871                 $can_post = true;
872         } elseif ($community_page && !empty(Session::getRemoteContactID($owner_uid))) {
873                 $contact_id = Session::getRemoteContactID($owner_uid);
874                 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $owner_uid, 'blocked' => false, 'pending' => false]);
875
876                 if (DBA::isResult($contact)) {
877                         $can_post = true;
878                         $remote_contact = true;
879                         $visitor = $contact_id;
880                 }
881         }
882
883         // perhaps they're visiting - but not a community page, so they wouldn't have write access
884         if (!empty(Session::getRemoteContactID($owner_uid)) && !$visitor) {
885                 $contact_id = Session::getRemoteContactID($owner_uid);
886
887                 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $owner_uid, 'blocked' => false, 'pending' => false]);
888
889                 $remote_contact = DBA::isResult($contact);
890         }
891
892         if (!$remote_contact && local_user()) {
893                 $contact_id = $_SESSION['cid'];
894
895                 $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $owner_uid, 'blocked' => false, 'pending' => false]);
896         }
897
898         if ($user['hidewall'] && (local_user() != $owner_uid) && !$remote_contact) {
899                 notice(DI::l10n()->t('Access to this item is restricted.'));
900                 return;
901         }
902
903         $sql_extra = Security::getPermissionsSQLByUserId($owner_uid);
904
905         $o = "";
906
907         // tabs
908         $is_owner = (local_user() && (local_user() == $owner_uid));
909         $o .= BaseProfile::getTabsHTML($a, 'photos', $is_owner, $user['nickname'], $profile['hide-friends']);
910
911         // Display upload form
912         if ($datatype === 'upload') {
913                 if (!$can_post) {
914                         notice(DI::l10n()->t('Permission denied.'));
915                         return;
916                 }
917
918                 $selname = Strings::isHex($datum) ? hex2bin($datum) : '';
919
920                 $albumselect = '';
921
922                 $albumselect .= '<option value="" ' . (!$selname ? ' selected="selected" ' : '') . '>&lt;current year&gt;</option>';
923                 $albums = Photo::getAlbums($owner_uid);
924                 if (!empty($albums)) {
925                         foreach ($albums as $album) {
926                                 if (($album['album'] === '') || ($album['album'] === Photo::CONTACT_PHOTOS) || ($album['album'] === DI::l10n()->t(Photo::CONTACT_PHOTOS))) {
927                                         continue;
928                                 }
929                                 $selected = (($selname === $album['album']) ? ' selected="selected" ' : '');
930                                 $albumselect .= '<option value="' . $album['album'] . '"' . $selected . '>' . $album['album'] . '</option>';
931                         }
932                 }
933
934                 $uploader = '';
935
936                 $ret = ['post_url' => 'photos/' . $user['nickname'],
937                                 'addon_text' => $uploader,
938                                 'default_upload' => true];
939
940                 Hook::callAll('photo_upload_form',$ret);
941
942                 $default_upload_box = Renderer::replaceMacros(Renderer::getMarkupTemplate('photos_default_uploader_box.tpl'), []);
943                 $default_upload_submit = Renderer::replaceMacros(Renderer::getMarkupTemplate('photos_default_uploader_submit.tpl'), [
944                         '$submit' => DI::l10n()->t('Submit'),
945                 ]);
946
947                 $usage_message = '';
948
949                 $tpl = Renderer::getMarkupTemplate('photos_upload.tpl');
950
951                 $aclselect_e = ($visitor ? '' : ACL::getFullSelectorHTML(DI::page(), $a->getUserId()));
952
953                 $o .= Renderer::replaceMacros($tpl,[
954                         '$pagename' => DI::l10n()->t('Upload Photos'),
955                         '$sessid' => session_id(),
956                         '$usage' => $usage_message,
957                         '$nickname' => $user['nickname'],
958                         '$newalbum' => DI::l10n()->t('New album name: '),
959                         '$existalbumtext' => DI::l10n()->t('or select existing album:'),
960                         '$nosharetext' => DI::l10n()->t('Do not show a status post for this upload'),
961                         '$albumselect' => $albumselect,
962                         '$permissions' => DI::l10n()->t('Permissions'),
963                         '$aclselect' => $aclselect_e,
964                         '$lockstate' => ACL::getLockstateForUserId($a->getUserId()) ? 'lock' : 'unlock',
965                         '$alt_uploader' => $ret['addon_text'],
966                         '$default_upload_box' => ($ret['default_upload'] ? $default_upload_box : ''),
967                         '$default_upload_submit' => ($ret['default_upload'] ? $default_upload_submit : ''),
968                         '$uploadurl' => $ret['post_url'],
969
970                         // ACL permissions box
971                         '$return_path' => DI::args()->getQueryString(),
972                 ]);
973
974                 return $o;
975         }
976
977         // Display a single photo album
978         if ($datatype === 'album') {
979                 // if $datum is not a valid hex, redirect to the default page
980                 if (!Strings::isHex($datum)) {
981                         DI::baseUrl()->redirect('photos/' . $user['nickname']. '/album');
982                 }
983                 $album = hex2bin($datum);
984
985                 $total = 0;
986                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
987                         AND `scale` <= 4 $sql_extra GROUP BY `resource-id`",
988                         intval($owner_uid),
989                         DBA::escape($album)
990                 );
991                 if (DBA::isResult($r)) {
992                         $total = count($r);
993                 }
994
995                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 20);
996
997                 /// @TODO I have seen this many times, maybe generalize it script-wide and encapsulate it?
998                 $order_field = $_GET['order'] ?? '';
999                 if ($order_field === 'posted') {
1000                         $order = 'ASC';
1001                 } else {
1002                         $order = 'DESC';
1003                 }
1004
1005                 $r = q("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`,
1006                         ANY_VALUE(`type`) AS `type`, max(`scale`) AS `scale`, ANY_VALUE(`desc`) as `desc`,
1007                         ANY_VALUE(`created`) as `created`
1008                         FROM `photo` WHERE `uid` = %d AND `album` = '%s'
1009                         AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` $order LIMIT %d , %d",
1010                         intval($owner_uid),
1011                         DBA::escape($album),
1012                         $pager->getStart(),
1013                         $pager->getItemsPerPage()
1014                 );
1015
1016                 if ($cmd === 'drop') {
1017                         $drop_url = DI::args()->getQueryString();
1018
1019                         return Renderer::replaceMacros(Renderer::getMarkupTemplate('confirm.tpl'), [
1020                                 '$method' => 'post',
1021                                 '$message' => DI::l10n()->t('Do you really want to delete this photo album and all its photos?'),
1022                                 '$confirm' => DI::l10n()->t('Delete Album'),
1023                                 '$confirm_url' => $drop_url,
1024                                 '$confirm_name' => 'dropalbum',
1025                                 '$cancel' => DI::l10n()->t('Cancel'),
1026                         ]);
1027                 }
1028
1029                 // edit album name
1030                 if ($cmd === 'edit') {
1031                         if (($album !== DI::l10n()->t('Profile Photos')) && ($album !== Photo::CONTACT_PHOTOS) && ($album !== DI::l10n()->t(Photo::CONTACT_PHOTOS))) {
1032                                 if ($can_post) {
1033                                         $edit_tpl = Renderer::getMarkupTemplate('album_edit.tpl');
1034
1035                                         $album_e = $album;
1036
1037                                         $o .= Renderer::replaceMacros($edit_tpl,[
1038                                                 '$nametext' => DI::l10n()->t('New album name: '),
1039                                                 '$nickname' => $user['nickname'],
1040                                                 '$album' => $album_e,
1041                                                 '$hexalbum' => bin2hex($album),
1042                                                 '$submit' => DI::l10n()->t('Submit'),
1043                                                 '$dropsubmit' => DI::l10n()->t('Delete Album')
1044                                         ]);
1045                                 }
1046                         }
1047                 } else {
1048                         if (($album !== DI::l10n()->t('Profile Photos')) && ($album !== Photo::CONTACT_PHOTOS) && ($album !== DI::l10n()->t(Photo::CONTACT_PHOTOS)) && $can_post) {
1049                                 $edit = [DI::l10n()->t('Edit Album'), 'photos/' . $user['nickname'] . '/album/' . bin2hex($album) . '/edit'];
1050                                 $drop = [DI::l10n()->t('Drop Album'), 'photos/' . $user['nickname'] . '/album/' . bin2hex($album) . '/drop'];
1051                         }
1052                 }
1053
1054                 if ($order_field === 'posted') {
1055                         $order =  [DI::l10n()->t('Show Newest First'), 'photos/' . $user['nickname'] . '/album/' . bin2hex($album), 'oldest'];
1056                 } else {
1057                         $order = [DI::l10n()->t('Show Oldest First'), 'photos/' . $user['nickname'] . '/album/' . bin2hex($album) . '?order=posted', 'newest'];
1058                 }
1059
1060                 $photos = [];
1061
1062                 if (DBA::isResult($r)) {
1063                         // "Twist" is only used for the duepunto theme with style "slackr"
1064                         $twist = false;
1065                         foreach ($r as $rr) {
1066                                 $twist = !$twist;
1067
1068                                 $ext = $phototypes[$rr['type']];
1069
1070                                 $imgalt_e = $rr['filename'];
1071                                 $desc_e = $rr['desc'];
1072
1073                                 $photos[] = [
1074                                         'id' => $rr['id'],
1075                                         'twist' => ' ' . ($twist ? 'rotleft' : 'rotright') . rand(2,4),
1076                                         'link' => 'photos/' . $user['nickname'] . '/image/' . $rr['resource-id']
1077                                                 . ($order_field === 'posted' ? '?order=posted' : ''),
1078                                         'title' => DI::l10n()->t('View Photo'),
1079                                         'src' => 'photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.' .$ext,
1080                                         'alt' => $imgalt_e,
1081                                         'desc'=> $desc_e,
1082                                         'ext' => $ext,
1083                                         'hash'=> $rr['resource-id'],
1084                                 ];
1085                         }
1086                 }
1087
1088                 $tpl = Renderer::getMarkupTemplate('photo_album.tpl');
1089                 $o .= Renderer::replaceMacros($tpl, [
1090                         '$photos' => $photos,
1091                         '$album' => $album,
1092                         '$can_post' => $can_post,
1093                         '$upload' => [DI::l10n()->t('Upload New Photos'), 'photos/' . $user['nickname'] . '/upload/' . bin2hex($album)],
1094                         '$order' => $order,
1095                         '$edit' => $edit,
1096                         '$drop' => $drop,
1097                         '$paginate' => $pager->renderFull($total),
1098                 ]);
1099
1100                 return $o;
1101
1102         }
1103
1104         // Display one photo
1105         if ($datatype === 'image') {
1106                 // fetch image, item containing image, then comments
1107                 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
1108                         $sql_extra ORDER BY `scale` ASC ",
1109                         intval($owner_uid),
1110                         DBA::escape($datum)
1111                 );
1112
1113                 if (!DBA::isResult($ph)) {
1114                         if (DBA::exists('photo', ['resource-id' => $datum, 'uid' => $owner_uid])) {
1115                                 notice(DI::l10n()->t('Permission denied. Access to this item may be restricted.'));
1116                         } else {
1117                                 notice(DI::l10n()->t('Photo not available'));
1118                         }
1119                         return;
1120                 }
1121
1122                 if ($cmd === 'drop') {
1123                         $drop_url = DI::args()->getQueryString();
1124
1125                         return Renderer::replaceMacros(Renderer::getMarkupTemplate('confirm.tpl'), [
1126                                 '$method' => 'post',
1127                                 '$message' => DI::l10n()->t('Do you really want to delete this photo?'),
1128                                 '$confirm' => DI::l10n()->t('Delete Photo'),
1129                                 '$confirm_url' => $drop_url,
1130                                 '$confirm_name' => 'delete',
1131                                 '$cancel' => DI::l10n()->t('Cancel'),
1132                         ]);
1133                 }
1134
1135                 $prevlink = '';
1136                 $nextlink = '';
1137
1138                 /*
1139                  * @todo This query is totally bad, the whole functionality has to be changed
1140                  * The query leads to a really intense used index.
1141                  * By now we hide it if someone wants to.
1142                  */
1143                 if ($cmd === 'view' && !DI::config()->get('system', 'no_count', false)) {
1144                         $order_field = $_GET['order'] ?? '';
1145
1146                         if ($order_field === 'posted') {
1147                                 $order = 'ASC';
1148                         } else {
1149                                 $order = 'DESC';
1150                         }
1151
1152                         $prvnxt = q("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0
1153                                 $sql_extra ORDER BY `created` $order ",
1154                                 DBA::escape($ph[0]['album']),
1155                                 intval($owner_uid)
1156                         );
1157
1158                         if (DBA::isResult($prvnxt)) {
1159                                 $prv = null;
1160                                 $nxt = null;
1161                                 foreach ($prvnxt as $z => $entry) {
1162                                         if ($entry['resource-id'] == $ph[0]['resource-id']) {
1163                                                 $prv = $z - 1;
1164                                                 $nxt = $z + 1;
1165                                                 if ($prv < 0) {
1166                                                         $prv = count($prvnxt) - 1;
1167                                                 }
1168                                                 if ($nxt >= count($prvnxt)) {
1169                                                         $nxt = 0;
1170                                                 }
1171                                                 break;
1172                                         }
1173                                 }
1174
1175                                 if (!is_null($prv)) {
1176                                         $prevlink = 'photos/' . $user['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] . ($order_field === 'posted' ? '?order=posted' : '');
1177                                 }
1178                                 if (!is_null($nxt)) {
1179                                         $nextlink = 'photos/' . $user['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] . ($order_field === 'posted' ? '?order=posted' : '');
1180                                 }
1181
1182                                 $tpl = Renderer::getMarkupTemplate('photo_edit_head.tpl');
1183                                 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl,[
1184                                         '$prevlink' => $prevlink,
1185                                         '$nextlink' => $nextlink
1186                                 ]);
1187
1188                                 if ($prevlink) {
1189                                         $prevlink = [$prevlink, '<div class="icon prev"></div>'];
1190                                 }
1191
1192                                 if ($nextlink) {
1193                                         $nextlink = [$nextlink, '<div class="icon next"></div>'];
1194                                 }
1195                         }
1196                 }
1197
1198                 if (count($ph) == 1) {
1199                         $hires = $lores = $ph[0];
1200                 }
1201
1202                 if (count($ph) > 1) {
1203                         if ($ph[1]['scale'] == 2) {
1204                                 // original is 640 or less, we can display it directly
1205                                 $hires = $lores = $ph[0];
1206                         } else {
1207                                 $hires = $ph[0];
1208                                 $lores = $ph[1];
1209                         }
1210                 }
1211
1212                 $album_link = 'photos/' . $user['nickname'] . '/album/' . bin2hex($ph[0]['album']);
1213
1214                 $tools = null;
1215
1216                 if ($can_post && ($ph[0]['uid'] == $owner_uid)) {
1217                         $tools = [];
1218                         if ($cmd === 'edit') {
1219                                 $tools['view'] = ['photos/' . $user['nickname'] . '/image/' . $datum, DI::l10n()->t('View photo')];
1220                         } else {
1221                                 $tools['edit'] = ['photos/' . $user['nickname'] . '/image/' . $datum . '/edit', DI::l10n()->t('Edit photo')];
1222                                 $tools['delete'] = ['photos/' . $user['nickname'] . '/image/' . $datum . '/drop', DI::l10n()->t('Delete photo')];
1223                                 $tools['profile'] = ['settings/profile/photo/crop/' . $ph[0]['resource-id'], DI::l10n()->t('Use as profile photo')];
1224                         }
1225
1226                         if (
1227                                 $ph[0]['uid'] == local_user()
1228                                 && (strlen($ph[0]['allow_cid']) || strlen($ph[0]['allow_gid']) || strlen($ph[0]['deny_cid']) || strlen($ph[0]['deny_gid']))
1229                         ) {
1230                                 $tools['lock'] = DI::l10n()->t('Private Photo');
1231                         }
1232                 }
1233
1234                 $photo = [
1235                         'href' => 'photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.' . $phototypes[$hires['type']],
1236                         'title'=> DI::l10n()->t('View Full Size'),
1237                         'src'  => 'photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.' . $phototypes[$lores['type']] . '?_u=' . DateTimeFormat::utcNow('ymdhis'),
1238                         'height' => $hires['height'],
1239                         'width' => $hires['width'],
1240                         'album' => $hires['album'],
1241                         'filename' => $hires['filename'],
1242                 ];
1243
1244                 $map = null;
1245                 $link_item = [];
1246                 $total = 0;
1247
1248                 // Do we have an item for this photo?
1249
1250                 // FIXME! - replace following code to display the conversation with our normal
1251                 // conversation functions so that it works correctly and tracks changes
1252                 // in the evolving conversation code.
1253                 // The difference is that we won't be displaying the conversation head item
1254                 // as a "post" but displaying instead the photo it is linked to
1255
1256                 /// @todo Rewrite this query. To do so, $sql_extra must be changed
1257                 $linked_items = q("SELECT `id` FROM `post-user-view` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
1258                         DBA::escape($datum)
1259                 );
1260                 if (DBA::isResult($linked_items)) {
1261                         // This is a workaround to not being forced to rewrite the while $sql_extra handling
1262                         $link_item = Post::selectFirst([], ['id' => $linked_items[0]['id']]);
1263                 }
1264
1265                 if (!empty($link_item['parent']) && !empty($link_item['uid'])) {
1266                         $condition = ["`parent` = ? AND `gravity` = ?",  $link_item['parent'], GRAVITY_COMMENT];
1267                         $total = Post::count($condition);
1268
1269                         $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
1270
1271                         $params = ['order' => ['id'], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
1272                         $items = Post::toArray(Post::selectForUser($link_item['uid'], Item::ITEM_FIELDLIST, $condition, $params));
1273
1274                         if (local_user() == $link_item['uid']) {
1275                                 Item::update(['unseen' => false], ['parent' => $link_item['parent']]);
1276                         }
1277                 }
1278
1279                 if (!empty($link_item['coord'])) {
1280                         $map = Map::byCoordinates($link_item['coord']);
1281                 }
1282
1283                 $tags = null;
1284
1285                 if (!empty($link_item['id'])) {
1286                         $tag_text = Tag::getCSVByURIId($link_item['uri-id']);
1287                         $arr = explode(',', $tag_text);
1288                         // parse tags and add links
1289                         $tag_arr = [];
1290                         foreach ($arr as $tag) {
1291                                 $tag_arr[] = [
1292                                         'name' => BBCode::convert($tag),
1293                                         'removeurl' => '/tagrm/' . $link_item['id'] . '/' . bin2hex($tag)
1294                                 ];
1295                         }
1296                         $tags = ['title' => DI::l10n()->t('Tags: '), 'tags' => $tag_arr];
1297                         if ($cmd === 'edit') {
1298                                 $tags['removeanyurl'] = 'tagrm/' . $link_item['id'];
1299                                 $tags['removetitle'] = DI::l10n()->t('[Select tags to remove]');
1300                         }
1301                 }
1302
1303
1304                 $edit = Null;
1305                 if ($cmd === 'edit' && $can_post) {
1306                         $edit_tpl = Renderer::getMarkupTemplate('photo_edit.tpl');
1307
1308                         $album_e = $ph[0]['album'];
1309                         $caption_e = $ph[0]['desc'];
1310                         $aclselect_e = ACL::getFullSelectorHTML(DI::page(), $a->getUserId(), false, ACL::getDefaultUserPermissions($ph[0]));
1311
1312                         $edit = Renderer::replaceMacros($edit_tpl, [
1313                                 '$id' => $ph[0]['id'],
1314                                 '$album' => ['albname', DI::l10n()->t('New album name'), $album_e,''],
1315                                 '$caption' => ['desc', DI::l10n()->t('Caption'), $caption_e, ''],
1316                                 '$tags' => ['newtag', DI::l10n()->t('Add a Tag'), "", DI::l10n()->t('Example: @bob, @Barbara_Jensen, @jim@example.com, #California, #camping')],
1317                                 '$rotate_none' => ['rotate', DI::l10n()->t('Do not rotate'),0,'', true],
1318                                 '$rotate_cw' => ['rotate', DI::l10n()->t("Rotate CW \x28right\x29"),1,''],
1319                                 '$rotate_ccw' => ['rotate', DI::l10n()->t("Rotate CCW \x28left\x29"),2,''],
1320
1321                                 '$nickname' => $user['nickname'],
1322                                 '$resource_id' => $ph[0]['resource-id'],
1323                                 '$permissions' => DI::l10n()->t('Permissions'),
1324                                 '$aclselect' => $aclselect_e,
1325
1326                                 '$item_id' => $link_item['id'] ?? 0,
1327                                 '$submit' => DI::l10n()->t('Submit'),
1328                                 '$delete' => DI::l10n()->t('Delete Photo'),
1329
1330                                 // ACL permissions box
1331                                 '$return_path' => DI::args()->getQueryString(),
1332                         ]);
1333                 }
1334
1335                 $like = '';
1336                 $dislike = '';
1337                 $likebuttons = '';
1338                 $comments = '';
1339                 $paginate = '';
1340
1341                 if (!empty($link_item['id']) && !empty($link_item['uri'])) {
1342                         $cmnt_tpl = Renderer::getMarkupTemplate('comment_item.tpl');
1343                         $tpl = Renderer::getMarkupTemplate('photo_item.tpl');
1344                         $return_path = DI::args()->getCommand();
1345
1346                         if (!DBA::isResult($items)) {
1347                                 if (($can_post || Security::canWriteToUserWall($owner_uid))) {
1348                                         /*
1349                                          * Hmmm, code depending on the presence of a particular addon?
1350                                          * This should be better if done by a hook
1351                                          */
1352                                         $qcomment = null;
1353                                         if (Addon::isEnabled('qcomment')) {
1354                                                 $words = DI::pConfig()->get(local_user(), 'qcomment', 'words');
1355                                                 $qcomment = $words ? explode("\n", $words) : [];
1356                                         }
1357
1358                                         $comments .= Renderer::replaceMacros($cmnt_tpl, [
1359                                                 '$return_path' => '',
1360                                                 '$jsreload' => $return_path,
1361                                                 '$id' => $link_item['id'],
1362                                                 '$parent' => $link_item['id'],
1363                                                 '$profile_uid' =>  $owner_uid,
1364                                                 '$mylink' => $contact['url'],
1365                                                 '$mytitle' => DI::l10n()->t('This is you'),
1366                                                 '$myphoto' => $contact['thumb'],
1367                                                 '$comment' => DI::l10n()->t('Comment'),
1368                                                 '$submit' => DI::l10n()->t('Submit'),
1369                                                 '$preview' => DI::l10n()->t('Preview'),
1370                                                 '$loading' => DI::l10n()->t('Loading...'),
1371                                                 '$qcomment' => $qcomment,
1372                                                 '$rand_num' => Crypto::randomDigits(12)
1373                                         ]);
1374                                 }
1375                         }
1376
1377                         $conv_responses = [
1378                                 'like'        => [],
1379                                 'dislike'     => [],
1380                                 'attendyes'   => [],
1381                                 'attendno'    => [],
1382                                 'attendmaybe' => []
1383                         ];
1384
1385                         if (DI::pConfig()->get(local_user(), 'system', 'hide_dislike')) {
1386                                 unset($conv_responses['dislike']);
1387                         }
1388
1389                         // display comments
1390                         if (DBA::isResult($items)) {
1391                                 foreach ($items as $item) {
1392                                         builtin_activity_puller($item, $conv_responses);
1393                                 }
1394
1395                                 if (!empty($conv_responses['like'][$link_item['uri']])) {
1396                                         $like = format_activity($conv_responses['like'][$link_item['uri']]['links'], 'like', $link_item['id']);
1397                                 }
1398
1399                                 if (!empty($conv_responses['dislike'][$link_item['uri']])) {
1400                                         $dislike = format_activity($conv_responses['dislike'][$link_item['uri']]['links'], 'dislike', $link_item['id']);
1401                                 }
1402
1403                                 if (($can_post || Security::canWriteToUserWall($owner_uid))) {
1404                                         /*
1405                                          * Hmmm, code depending on the presence of a particular addon?
1406                                          * This should be better if done by a hook
1407                                          */
1408                                         $qcomment = null;
1409                                         if (Addon::isEnabled('qcomment')) {
1410                                                 $words = DI::pConfig()->get(local_user(), 'qcomment', 'words');
1411                                                 $qcomment = $words ? explode("\n", $words) : [];
1412                                         }
1413
1414                                         $comments .= Renderer::replaceMacros($cmnt_tpl,[
1415                                                 '$return_path' => '',
1416                                                 '$jsreload' => $return_path,
1417                                                 '$id' => $link_item['id'],
1418                                                 '$parent' => $link_item['id'],
1419                                                 '$profile_uid' =>  $owner_uid,
1420                                                 '$mylink' => $contact['url'],
1421                                                 '$mytitle' => DI::l10n()->t('This is you'),
1422                                                 '$myphoto' => $contact['thumb'],
1423                                                 '$comment' => DI::l10n()->t('Comment'),
1424                                                 '$submit' => DI::l10n()->t('Submit'),
1425                                                 '$preview' => DI::l10n()->t('Preview'),
1426                                                 '$qcomment' => $qcomment,
1427                                                 '$rand_num' => Crypto::randomDigits(12)
1428                                         ]);
1429                                 }
1430
1431                                 foreach ($items as $item) {
1432                                         $comment = '';
1433                                         $template = $tpl;
1434
1435                                         $activity = DI::activity();
1436
1437                                         if (($activity->match($item['verb'], Activity::LIKE) ||
1438                                              $activity->match($item['verb'], Activity::DISLIKE)) &&
1439                                             ($item['gravity'] != GRAVITY_PARENT)) {
1440                                                 continue;
1441                                         }
1442
1443                                         $author = ['uid' => 0, 'id' => $item['author-id'],
1444                                                 'network' => $item['author-network'], 'url' => $item['author-link']];
1445                                         $profile_url = Contact::magicLinkByContact($author);
1446                                         if (strpos($profile_url, 'redir/') === 0) {
1447                                                 $sparkle = ' sparkle';
1448                                         } else {
1449                                                 $sparkle = '';
1450                                         }
1451
1452                                         $dropping = (($item['contact-id'] == $contact_id) || ($item['uid'] == local_user()));
1453                                         $drop = [
1454                                                 'dropping' => $dropping,
1455                                                 'pagedrop' => false,
1456                                                 'select' => DI::l10n()->t('Select'),
1457                                                 'delete' => DI::l10n()->t('Delete'),
1458                                         ];
1459
1460                                         $title_e = $item['title'];
1461                                         $body_e = BBCode::convert($item['body']);
1462
1463                                         $comments .= Renderer::replaceMacros($template,[
1464                                                 '$id' => $item['id'],
1465                                                 '$profile_url' => $profile_url,
1466                                                 '$name' => $item['author-name'],
1467                                                 '$thumb' => $item['author-avatar'],
1468                                                 '$sparkle' => $sparkle,
1469                                                 '$title' => $title_e,
1470                                                 '$body' => $body_e,
1471                                                 '$ago' => Temporal::getRelativeDate($item['created']),
1472                                                 '$indent' => (($item['parent'] != $item['id']) ? ' comment' : ''),
1473                                                 '$drop' => $drop,
1474                                                 '$comment' => $comment
1475                                         ]);
1476
1477                                         if (($can_post || Security::canWriteToUserWall($owner_uid))) {
1478                                                 /*
1479                                                  * Hmmm, code depending on the presence of a particular addon?
1480                                                  * This should be better if done by a hook
1481                                                  */
1482                                                 $qcomment = null;
1483                                                 if (Addon::isEnabled('qcomment')) {
1484                                                         $words = DI::pConfig()->get(local_user(), 'qcomment', 'words');
1485                                                         $qcomment = $words ? explode("\n", $words) : [];
1486                                                 }
1487
1488                                                 $comments .= Renderer::replaceMacros($cmnt_tpl, [
1489                                                         '$return_path' => '',
1490                                                         '$jsreload' => $return_path,
1491                                                         '$id' => $item['id'],
1492                                                         '$parent' => $item['parent'],
1493                                                         '$profile_uid' =>  $owner_uid,
1494                                                         '$mylink' => $contact['url'],
1495                                                         '$mytitle' => DI::l10n()->t('This is you'),
1496                                                         '$myphoto' => $contact['thumb'],
1497                                                         '$comment' => DI::l10n()->t('Comment'),
1498                                                         '$submit' => DI::l10n()->t('Submit'),
1499                                                         '$preview' => DI::l10n()->t('Preview'),
1500                                                         '$qcomment' => $qcomment,
1501                                                         '$rand_num' => Crypto::randomDigits(12)
1502                                                 ]);
1503                                         }
1504                                 }
1505                         }
1506
1507                         $responses = [];
1508                         foreach ($conv_responses as $verb => $activity) {
1509                                 if (isset($activity[$link_item['uri']])) {
1510                                         $responses[$verb] = $activity[$link_item['uri']];
1511                                 }
1512                         }
1513
1514                         if ($cmd === 'view' && ($can_post || Security::canWriteToUserWall($owner_uid))) {
1515                                 $like_tpl = Renderer::getMarkupTemplate('like_noshare.tpl');
1516                                 $likebuttons = Renderer::replaceMacros($like_tpl, [
1517                                         '$id' => $link_item['id'],
1518                                         '$like' => DI::l10n()->t('Like'),
1519                                         '$like_title' => DI::l10n()->t('I like this (toggle)'),
1520                                         '$dislike' => DI::l10n()->t('Dislike'),
1521                                         '$wait' => DI::l10n()->t('Please wait'),
1522                                         '$dislike_title' => DI::l10n()->t('I don\'t like this (toggle)'),
1523                                         '$hide_dislike' => DI::pConfig()->get(local_user(), 'system', 'hide_dislike'),
1524                                         '$responses' => $responses,
1525                                         '$return_path' => DI::args()->getQueryString(),
1526                                 ]);
1527                         }
1528
1529                         $paginate = $pager->renderFull($total);
1530                 }
1531
1532                 $photo_tpl = Renderer::getMarkupTemplate('photo_view.tpl');
1533                 $o .= Renderer::replaceMacros($photo_tpl, [
1534                         '$id' => $ph[0]['id'],
1535                         '$album' => [$album_link, $ph[0]['album']],
1536                         '$tools' => $tools,
1537                         '$photo' => $photo,
1538                         '$prevlink' => $prevlink,
1539                         '$nextlink' => $nextlink,
1540                         '$desc' => $ph[0]['desc'],
1541                         '$tags' => $tags,
1542                         '$edit' => $edit,
1543                         '$map' => $map,
1544                         '$map_text' => DI::l10n()->t('Map'),
1545                         '$likebuttons' => $likebuttons,
1546                         '$like' => $like,
1547                         '$dislike' => $dislike,
1548                         '$comments' => $comments,
1549                         '$paginate' => $paginate,
1550                 ]);
1551
1552                 DI::page()['htmlhead'] .= "\n" . '<meta name="twitter:card" content="summary_large_image" />' . "\n";
1553                 DI::page()['htmlhead'] .= '<meta name="twitter:title" content="' . $photo["album"] . '" />' . "\n";
1554                 DI::page()['htmlhead'] .= '<meta name="twitter:image" content="' . DI::baseUrl() . "/" . $photo["href"] . '" />' . "\n";
1555                 DI::page()['htmlhead'] .= '<meta name="twitter:image:width" content="' . $photo["width"] . '" />' . "\n";
1556                 DI::page()['htmlhead'] .= '<meta name="twitter:image:height" content="' . $photo["height"] . '" />' . "\n";
1557
1558                 return $o;
1559         }
1560
1561         // Default - show recent photos with upload link (if applicable)
1562         //$o = '';
1563         $total = 0;
1564         $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
1565                 $sql_extra GROUP BY `resource-id`",
1566                 intval($user['uid']),
1567                 DBA::escape(Photo::CONTACT_PHOTOS),
1568                 DBA::escape(DI::l10n()->t(Photo::CONTACT_PHOTOS))
1569         );
1570         if (DBA::isResult($r)) {
1571                 $total = count($r);
1572         }
1573
1574         $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 20);
1575
1576         $r = q("SELECT `resource-id`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`filename`) AS `filename`,
1577                 ANY_VALUE(`type`) AS `type`, ANY_VALUE(`album`) AS `album`, max(`scale`) AS `scale`,
1578                 ANY_VALUE(`created`) AS `created` FROM `photo`
1579                 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
1580                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
1581                 intval($user['uid']),
1582                 DBA::escape(Photo::CONTACT_PHOTOS),
1583                 DBA::escape(DI::l10n()->t(Photo::CONTACT_PHOTOS)),
1584                 $pager->getStart(),
1585                 $pager->getItemsPerPage()
1586         );
1587
1588         $photos = [];
1589         if (DBA::isResult($r)) {
1590                 // "Twist" is only used for the duepunto theme with style "slackr"
1591                 $twist = false;
1592                 foreach ($r as $rr) {
1593                         //hide profile photos to others
1594                         if (!$is_owner && !Session::getRemoteContactID($owner_uid) && ($rr['album'] == DI::l10n()->t('Profile Photos'))) {
1595                                 continue;
1596                         }
1597
1598                         $twist = !$twist;
1599                         $ext = $phototypes[$rr['type']];
1600
1601                         $alt_e = $rr['filename'];
1602                         $name_e = $rr['album'];
1603
1604                         $photos[] = [
1605                                 'id'    => $rr['id'],
1606                                 'twist' => ' ' . ($twist ? 'rotleft' : 'rotright') . rand(2,4),
1607                                 'link'  => 'photos/' . $user['nickname'] . '/image/' . $rr['resource-id'],
1608                                 'title' => DI::l10n()->t('View Photo'),
1609                                 'src'   => 'photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.' . $ext,
1610                                 'alt'   => $alt_e,
1611                                 'album' => [
1612                                         'link' => 'photos/' . $user['nickname'] . '/album/' . bin2hex($rr['album']),
1613                                         'name' => $name_e,
1614                                         'alt'  => DI::l10n()->t('View Album'),
1615                                 ],
1616
1617                         ];
1618                 }
1619         }
1620
1621         $tpl = Renderer::getMarkupTemplate('photos_recent.tpl');
1622         $o .= Renderer::replaceMacros($tpl, [
1623                 '$title' => DI::l10n()->t('Recent Photos'),
1624                 '$can_post' => $can_post,
1625                 '$upload' => [DI::l10n()->t('Upload New Photos'), 'photos/' . $user['nickname'] . '/upload'],
1626                 '$photos' => $photos,
1627                 '$paginate' => $pager->renderFull($total),
1628         ]);
1629
1630         return $o;
1631 }