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