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