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