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