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