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