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