]> git.mxchange.org Git - friendica.git/blob - mod/photos.php
693d068e48c710bfec4c47fb8c2fc86ead1a8364
[friendica.git] / mod / photos.php
1 <?php
2
3 require_once('Photo.php');
4 require_once('include/items.php');
5 require_once('view/acl_selectors.php');
6
7 function photos_init(&$a) {
8
9         if($a->argc > 1) {
10                 $nick = $a->argv[1];
11                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
12                         dbesc($nick)
13                 );
14
15                 if(! count($r))
16                         return;
17
18                 $a->data['user'] = $r[0];
19
20                 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d",
21                         intval($a->data['user']['uid'])
22                 );
23
24                 if(count($albums)) {
25                         $a->data['albums'] = $albums;
26
27                         $o .= '<h4><a href="' . $a->get_baseurl() . '/profile/' . $a->data['user']['nickname'] . '">' . $a->data['user']['username'] . '</a></h4>';
28                         $o .= '<h4>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
29                 
30                         $o .= '<ul>';
31                         foreach($albums as $album) {
32                                 if((! strlen($album['album'])) || ($album['album'] == t('Contact Photos')))
33                                         continue;
34                                 $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" />' . $album['album'] . '</a></li>'; 
35                         }
36                         $o .= '</ul>';
37                 }
38                 $a->page['aside'] .= $o;
39         }
40         return;
41 }
42
43
44
45
46 function photos_post(&$a) {
47
48
49         if(! local_user()) {
50                 notice( t('Permission denied.') . EOL );
51                 killme();
52         }
53
54
55
56         $r = q("SELECT `contact`.* `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
57                 WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
58                 intval(get_uid())
59         );
60
61         $contact_record = $r[0];        
62
63
64         if(($a->argc > 2) && ($a->argv[1] === 'album')) {
65                 $album = hex2bin($a->argv[2]);
66
67                 if($album == t('Profile Photos') || $album == t('Contact Photos')) {
68                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
69                         return; // NOTREACHED
70                 }
71
72                 $r = q("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
73                         dbesc($album),
74                         intval(get_uid())
75                 );
76                 if(! count($r)) {
77                         notice( t('Album not found.') . EOL);
78                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
79                         return; // NOTREACHED
80                 }
81
82                 $newalbum = notags(trim($_POST['albumname']));
83                 if($newalbum != $album) {
84                         q("UPDATE `photo` SET `album` = '%s' WHERE `album` = '%s' AND `uid` = %d",
85                                 dbesc($newalbum),
86                                 dbesc($album),
87                                 intval(get_uid())
88                         );
89                         $newurl = str_replace(bin2hex($album),bin2hex($newalbum),$_SESSION['photo_return']);
90                         goaway($a->get_baseurl() . '/' . $newurl);
91                         return; // NOTREACHED
92                 }
93
94                 if($_POST['dropalbum'] == t('Delete Album')) {
95
96                         $res = array();
97                         $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'",
98                                 intval(get_uid()),
99                                 dbesc($album)
100                         );
101                         if(count($r)) {
102                                 foreach($r as $rr) {
103                                         $res[] = "'" . dbesc($rr['rid']) . "'" ;
104                                 }
105                         }
106                         else {
107                                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
108                                 return; // NOTREACHED
109                         }
110                         $str_res = implode(',', $res);
111
112                         q("DELETE FROM `photo` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
113                                 intval(get_uid())
114                         );
115                         $r = q("SELECT `parent-uri` FROM `item` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
116                                 intval(get_uid())
117                         );
118                         if(count($r)) {
119                                 foreach($r as $rr) {
120                                         q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
121                                                 dbesc(datetime_convert()),
122                                                 dbesc($rr['parent-uri']),
123                                                 intval(get_uid())
124                                         );
125
126                                         $drop_id = intval($rr['id']);
127                                         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
128                                         $proc_debug = get_config('system','proc_debug');
129
130                                         // send the notification upstream/downstream as the case may be
131
132                                         if($rr['visible'])
133                                                 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" $proc_debug & ",
134                                                         array(),$foo));
135
136                                 }
137                         }
138                 }
139                 goaway($a->get_baseurl() . '/photos/' . $a->data['user']['nickname']);
140                 return; // NOTREACHED
141         }
142
143         if(($a->argc > 1) && (x($_POST,'delete')) && ($_POST['delete'] == t('Delete Photo'))) {
144                 $r = q("SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' LIMIT 1",
145                         intval(get_uid()),
146                         dbesc($a->argv[1])
147                 );
148                 if(count($r)) {
149                         q("DELETE FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
150                                 intval(get_uid()),
151                                 dbesc($r[0]['resource-id'])
152                         );
153                         $i = q("SELECT * FROM `item` WHERE `resource-id` = '%s' AND `uid` = %d LIMIT 1",
154                                 dbesc($r[0]['resource-id']),
155                                 intval(get_uid())
156                         );
157                         if(count($i)) {
158                                 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
159                                         dbesc(datetime_convert()),
160                                         dbesc(datetime_convert()),
161                                         dbesc($i[0]['uri']),
162                                         intval(get_uid())
163                                 );
164
165                                 $url = $a->get_baseurl();
166                                 $drop_id = intval($i[0]['id']);
167                                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
168                                 
169                                 $proc_debug = get_config('system','proc_debug']);
170
171                                 // send the notification upstream/downstream as the case may be
172
173                                 if($i[0]['visible'])
174                                         proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" $proc_debug & ",
175                                                 array(),$foo));
176                         }
177                 }
178
179                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
180                 return; // NOTREACHED
181         }
182
183
184
185         if(($a->argc > 1) && (x($_POST,'desc') !== false)) {
186                 $desc = notags(trim($_POST['desc']));
187                 $tags = notags(trim($_POST['tags']));
188                 $item_id = intval($_POST['item_id']);
189                 $resource_id = $a->argv[1];
190
191                 $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
192                         dbesc($resource_id),
193                         intval(get_uid())
194                 );
195                 if(count($r)) {
196                         $r = q("UPDATE `photo` SET `desc` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d",
197                                 dbesc($desc),
198                                 dbesc($resource_id),
199                                 intval(get_uid())
200                         );
201                 }
202                 if(! $item_id) {
203
204                         $title = '';
205                         $basename = basename($filename);
206                         $uri = item_new_uri($a->get_hostname(),get_uid());
207                         // Create item container
208
209                         $arr = array();
210
211                         $arr['uid']          = get_uid();
212                         $arr['uri']          = $uri;
213                         $arr['parent-uri']   = $uri; 
214                         $arr['type']         = 'photo';
215                         $arr['wall']         = 1;
216                         $arr['resource-id']  = $p[0]['resource-id'];
217                         $arr['contact-id']   = $contact_record['id'];
218                         $arr['owner-name']   = $contact_record['name'];
219                         $arr['owner-link']   = $contact_record['url'];
220                         $arr['owner-avatar'] = $contact_record['thumb'];
221                         $arr['title']        = $title;
222                         $arr['allow_cid']    = $p[0]['allow_cid'];
223                         $arr['allow_gid']    = $p[0]['allow_gid'];
224                         $arr['deny_cid']     = $p[0]['deny_cid'];
225                         $arr['deny_gid']     = $p[0]['deny_gid'];
226                         $arr['last-child']   = 1;
227                         $arr['body']         = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']' 
228                                                 . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.jpg' . '[/img]' 
229                                                 . '[/url]';
230                 
231                         $item_id = item_store($arr);
232
233                 }
234
235                 $r = q("UPDATE `item` SET `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
236                         dbesc($tags),
237                         dbesc(datetime_convert()),
238                         dbesc(datetime_convert()),
239                         intval($item_id),
240                         intval(get_uid())
241                 );
242
243                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
244                 return; // NOTREACHED
245         }
246
247
248
249
250         if(! x($_FILES,'userfile'))
251                 killme();
252
253         if($_POST['partitionCount'])
254                 $java_upload = true;
255         else
256                 $java_upload = false;
257
258         $album =  notags(trim($_POST['album']));
259         $newalbum = notags(trim($_POST['newalbum']));
260
261         if(! strlen($album)) {
262                 if(strlen($newalbum))
263                         $album = $newalbum;
264                 else
265                         $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
266         }
267
268         $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
269                 dbesc($album),
270                 intval(get_uid())
271         );
272         if((! count($r)) || ($album == t('Profile Photos')))
273                 $visible = 1;
274         else
275                 $visibile = 0;
276
277
278         $str_group_allow   = perms2str($_POST['group_allow']);
279         $str_contact_allow = perms2str($_POST['contact_allow']);
280         $str_group_deny    = perms2str($_POST['group_deny']);
281         $str_contact_deny  = perms2str($_POST['contact_deny']);
282
283         $src               = $_FILES['userfile']['tmp_name'];
284         $filename          = basename($_FILES['userfile']['name']);
285         $filesize          = intval($_FILES['userfile']['size']);
286
287         $imagedata = @file_get_contents($src);
288         $ph = new Photo($imagedata);
289
290         if(! ($image = $ph->getImage())) {
291                 notice( t('Unable to process image.') . EOL );
292                 @unlink($src);
293                 killme();
294         }
295
296         @unlink($src);
297
298         $width = $ph->getWidth();
299         $height = $ph->getHeight();
300
301         $smallest = 0;
302
303         $photo_hash = photo_new_resource();
304
305         $r = $ph->store(get_uid(), 0, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
306
307         if(! $r) {
308                 notice( t('Image upload failed.') . EOL );
309                 killme();
310         }
311
312         if($width > 640 || $height > 640) {
313                 $ph->scaleImage(640);
314                 $ph->store(get_uid(), 0, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
315                 $smallest = 1;
316         }
317
318         if($width > 320 || $height > 320) {
319                 $ph->scaleImage(320);
320                 $ph->store(get_uid(), 0, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
321                 $smallest = 2;
322         }
323         
324         $basename = basename($filename);
325         $uri = item_new_uri($a->get_hostname(), get_uid());
326
327         // Create item container
328
329
330         $arr = array();
331
332         $arr['uid']          = get_uid();
333         $arr['uri']          = $uri;
334         $arr['parent-uri']   = $uri;
335         $arr['type']         = 'photo';
336         $arr['wall']         = 1;
337         $arr['resource-id']  = $photo_hash;
338         $arr['contact-id']   = $contact_record['id'];
339         $arr['owner-name']   = $contact_record['name'];
340         $arr['owner-link']   = $contact_record['url'];
341         $arr['owner-avatar'] = $contact_record['thumb'];
342         $arr['title']        = $title;
343         $arr['allow_cid']    = $str_contact_allow;
344         $arr['allow_gid']    = $str_group_allow;
345         $arr['deny_cid']     = $str_contact_deny;
346         $arr['deny_gid']     = $str_group_deny;
347         $arr['last-child']   = 1;
348         $arr['visible']      = $visible;
349         $arr['body']         = '[url=' . $a->get_baseurl() . '/photos/' . $contact_record['nickname'] . '/image/' . $photo_hash . ']' 
350                                 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]' 
351                                 . '[/url]';
352
353         $item_id = item_store($arr);
354
355         if(! $java_upload) {
356                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
357                 return; // NOTREACHED
358         }
359
360         killme();
361         return; // NOTREACHED
362
363 }
364
365
366
367 function photos_content(&$a) {
368
369         // URLs:
370         // photos/name
371         // photos/name/upload
372         // photos/name/album/xxxxx
373         // photos/name/album/xxxxx/edit
374         // photos/name/image/xxxxx
375         // photos/name/image/xxxxx/edit
376
377
378         if(! x($a->data,'user')) {
379                 notice( t('No photos selected') . EOL );
380                 return;
381         }
382
383         $_SESSION['photo_return'] = $a->cmd;
384
385         //
386         // Parse arguments 
387         //
388
389         if($a->argc > 3) {
390                 $datatype = $a->argv[2];
391                 $datum = $a->argv[3];
392         }
393         elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
394                 $datatype = 'upload';
395         else
396                 $datatype = 'summary';
397
398         if($a->argc > 4)
399                 $cmd = $a->argv[4];
400         else
401                 $cmd = 'view';
402
403         //
404         // Setup permissions structures
405         //
406
407         $owner_uid = $a->data['user']['uid'];
408
409         if(remote_user()) {
410                 $contact_id = $_SESSION['visitor_id'];
411                 $groups = init_groups_visitor($contact_id);
412         }
413
414         // default permissions - anonymous user
415
416         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
417
418         // Profile owner - everything is visible
419
420         if(local_user() && (get_uid() == $owner_uid)) {
421                 $sql_extra = '';        
422         }
423         elseif(remote_user()) {
424                 // authenticated visitor - here lie dragons
425                 $gs = '<<>>'; // should be impossible to match
426                 if(count($groups)) {
427                         foreach($groups as $g)
428                                 $gs .= '|<' . intval($g) . '>';
429                 } 
430                 $sql_extra = sprintf(
431                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
432                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
433                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
434                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
435
436                         intval($_SESSION['visitor_id']),
437                         intval($_SESSION['visitor_id']),
438                         dbesc($gs),
439                         dbesc($gs)
440                 );
441         }
442
443         //
444         // dispatch request
445         //
446
447
448         if($datatype === 'upload') {
449                 if( ! (local_user() && (get_uid() == $a->data['user']['uid']))) {
450                         notice( t('Permission denied.'));
451                         return;
452                 }
453                 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
454
455                 $albumselect .= '<option value="" selected="selected" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
456                 if(count($a->data['albums'])) {
457                         foreach($a->data['albums'] as $album) {
458                                 if(($album['album'] === '') || ($album['album'] == t('Contact Photos')))
459                                         continue;
460                                 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
461                         }
462                 }
463                 $albumselect .= '</select>';
464                 $tpl = load_view_file('view/photos_upload.tpl');
465                 $o .= replace_macros($tpl,array(
466                         '$pagename' => t('Upload Photos'),
467                         '$sessid' => session_id(),
468                         '$newalbum' => t('New album name: '),
469                         '$existalbumtext' => t('or existing album name: '),
470                         '$filestext' => t('Select files to upload: '),
471                         '$albumselect' => $albumselect,
472                         '$permissions' => t('Permissions'),
473                         '$aclselect' => populate_acl($a->user),
474                         '$archive' => $a->get_baseurl() . '/jumploader_z.jar',
475                         '$nojava' => t('Use the following controls only if the Java uploader (above) fails to launch.'),
476                         '$uploadurl' => $a->get_baseurl() . '/photos',
477                         '$submit' => t('Submit')
478                 ));
479
480                 return $o; 
481
482         }
483
484         if($datatype === 'album') {
485
486                 $album = hex2bin($datum);
487
488                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
489                         $sql_extra GROUP BY `resource-id`",
490                         intval($a->data['user']['uid']),
491                         dbesc($album)
492                 );
493                 if(count($r))
494                         $a->set_pager_total(count($r));
495
496
497                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
498                         $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
499                         intval($a->data['user']['uid']),
500                         dbesc($album),
501                         intval($a->pager['start']),
502                         intval($a->pager['itemspage'])
503                 );
504
505                 $o .= '<h3>' . $album . '</h3>';
506                 
507                 if($cmd === 'edit') {           
508                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
509                                 if(local_user() && (get_uid() == $a->data['user']['uid'])) {
510                                         $edit_tpl = load_view_file('view/album_edit.tpl');
511                                         $o .= replace_macros($edit_tpl,array(
512                                                 '$nametext' => t('New album name: '),
513                                                 '$album' => $album,
514                                                 '$hexalbum' => bin2hex($album),
515                                                 '$submit' => t('Submit'),
516                                                 '$dropsubmit' => t('Delete Album')
517                                         ));
518                                 }
519                         }
520                 }
521                 else {
522                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
523                                 if(local_user() && (get_uid() == $a->data['user']['uid'])) {
524                                         $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/' 
525                                                 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">' 
526                                                 . t('Edit Album') . '</a></div>';
527                                 }
528                         }
529                 }
530                 $tpl = load_view_file('view/photo_album.tpl');
531                 if(count($r))
532                         foreach($r as $rr) {
533                                 $o .= replace_macros($tpl,array(
534                                         '$id' => $rr['id'],
535                                         '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
536                                         '$phototitle' => t('View Photo'),
537                                         '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
538                                         '$imgalt' => $rr['filename']
539                                 ));
540
541                 }
542                 $o .= '<div id="photo-album-end"></div>';
543                 return $o;
544
545         }       
546
547
548         if($datatype === 'image') {
549
550                 require_once('security.php');
551                 require_once('bbcode.php');
552
553                 // fetch image, item containing image, then comments
554
555                 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
556                         $sql_extra ORDER BY `scale` ASC ",
557                         intval($a->data['user']['uid']),
558                         dbesc($datum)
559                 );
560
561                 if(! count($ph)) {
562                         notice( t('Photo not available') . EOL );
563                         return;
564                 }
565
566                 if(count($ph) == 1)
567                         $hires = $lores = $ph[0];
568                 if(count($ph) > 1) {
569                         if($ph[1]['scale'] == 2) {
570                                 // original is 640 or less, we can display it directly
571                                 $hires = $lores = $ph[0];
572                         }
573                         else {
574                         $hires = $ph[0];
575                         $lores = $ph[1];
576                         }
577                 }
578
579                 
580                 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']) . '">' . $ph[0]['album'] . '</a></h3>';
581  
582                 if(local_user() && ($ph[0]['uid'] == get_uid())) {
583                         $o .= '<div id="photo-edit-link-wrap" ><a id="photo-edit-link" href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $datum . '/edit' . '">' . t('Edit photo') . '</a></div>';
584                 }
585
586
587                 $o .= '<a href="' . $a->get_baseurl() . '/photo/' 
588                         . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="' 
589                         . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/' 
590                         . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
591
592
593                 // Do we have an item for this photo?
594
595                 $i1 = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
596                         dbesc($datum)
597                 );
598                 if(count($i1)) {
599
600                         $r = q("SELECT COUNT(*) AS `total`
601                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
602                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
603                                 AND NOT `item`.`type` IN ( 'remote', 'net-comment') 
604                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
605                                 $sql_extra ",
606                                 dbesc($i1[0]['uri']),
607                                 dbesc($i1[0]['uri'])
608
609                         );
610
611                         if(count($r))
612                                 $a->set_pager_total($r[0]['total']);
613
614
615                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
616                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
617                                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
618                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
619                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
620                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
621                                 AND NOT `item`.`type` IN ( 'remote', 'net-comment') 
622                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
623                                 $sql_extra
624                                 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
625                                 dbesc($i1[0]['uri']),
626                                 dbesc($i1[0]['uri']),
627                                 intval($a->pager['start']),
628                                 intval($a->pager['itemspage'])
629
630                         );
631                 }
632
633                 $o .= '<div id="photo-caption" >' . $ph[0]['desc'] . '</div>';
634
635                 if(count($i1) && strlen($i1[0]['tag'])) {
636                         // parse tags and add links     
637                         $o .= '<div id="in-this-photo-text">' . t('In this photo: ') . '</div>';
638                         $o .= '<div id="in-this-photo">' . $i1[0]['tag'] . '</div>';
639                 }
640
641                 if($cmd === 'edit') {
642                         $edit_tpl = load_view_file('view/photo_edit.tpl');
643                         $o .= replace_macros($edit_tpl, array(
644                                 '$id' => $ph[0]['id'],
645                                 '$resource_id' => $ph[0]['resource-id'],
646                                 '$capt_label' => t('Caption'),
647                                 '$caption' => $ph[0]['desc'],
648                                 '$tag_label' => t('Tags'),
649                                 '$tags' => $i1[0]['tag'],
650                                 '$item_id' => ((count($i1)) ? $i1[0]['id'] : 0),
651                                 '$submit' => t('Submit'),
652                                 '$delete' => t('Delete Photo')
653
654                         ));
655                 }
656
657                 if(count($i1)) {
658                         // pull out how many people like the photo
659
660                         $cmnt_tpl = load_view_file('view/comment_item.tpl');
661                         $tpl = load_view_file('view/photo_item.tpl');
662                         $return_url = $a->cmd;
663
664                         if(can_write_wall($a,$a->data['user']['uid'])) {
665                                 if($i1[0]['last-child']) {
666                                         $o .= replace_macros($cmnt_tpl,array(
667                                                 '$return_path' => $return_url,
668                                                 '$type' => 'wall-comment',
669                                                 '$id' => $i1[0]['id'],
670                                                 '$parent' => $i1[0]['id'],
671                                                 '$profile_uid' =>  $a->data['user']['uid'],
672                                                 '$ww' => ''
673                                         ));
674                                 }
675                         }
676
677
678                         // display comments
679                         if(count($r)) {
680                                 foreach($r as $item) {
681                                         $comment = '';
682                                         $template = $tpl;
683                         
684                                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
685                         
686                                         if(can_write_wall($a,$a->data['user']['uid'])) {
687                                                 if($item['last-child']) {
688                                                         $comment = replace_macros($cmnt_tpl,array(
689                                                                 '$return_path' => $return_url,
690                                                                 '$type' => 'wall-comment',
691                                                                 '$id' => $item['item_id'],
692                                                                 '$parent' => $item['parent'],
693                                                                 '$profile_uid' =>  $a->data['user']['uid'],
694                                                                 '$ww' => ''
695                                                         ));
696                                                 }
697                                         }
698
699                                         $profile_url = $item['url'];
700
701                                         if(local_user() && ($item['contact-uid'] == get_uid()) 
702                                                 && ($item['rel'] == REL_VIP || $item['rel'] == REL_BUD) && (! $item['self'] ))
703                                                 $profile_url = $redirect_url;
704  
705                                         $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
706                                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
707                                         $profile_link = $profile_url;
708
709                                         $drop = '';
710
711                                         if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == get_uid()))
712                                                 $drop = replace_macros(load_view_file('view/wall_item_drop.tpl'), array('$id' => $item['id']));
713
714
715                                         $o .= replace_macros($template,array(
716                                                 '$id' => $item['item_id'],
717                                                 '$profile_url' => $profile_link,
718                                                 '$name' => $profile_name,
719                                                 '$thumb' => $profile_avatar,
720                                                 '$title' => $item['title'],
721                                                 '$body' => bbcode($item['body']),
722                                                 '$ago' => relative_date($item['created']),
723                                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
724                                                 '$drop' => $drop,
725                                                 '$comment' => $comment
726                                         ));
727                                 }
728                         }
729
730                         $o .= paginate($a);
731                 }
732                 return $o;
733         }
734
735         // Default - show recent photos with upload link (if applicable)
736
737         $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
738                 $sql_extra GROUP BY `resource-id`",
739                 intval($a->data['user']['uid']),
740                 dbesc( t('Contact Photos'))
741         );
742         if(count($r))
743                 $a->set_pager_total(count($r));
744
745
746         $r = q("SELECT `resource-id`, `album`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
747                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
748                 intval($a->data['user']['uid']),
749                 dbesc( t('Contact Photos')),
750                 intval($a->pager['start']),
751                 intval($a->pager['itemspage'])
752         );
753
754         $o .= '<h3>' . t('Recent Photos') . '</h3>';
755
756         if( local_user() && (get_uid() == $a->data['user']['uid'])) {
757                 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/' 
758                         . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
759         }
760
761         $tpl = load_view_file('view/photo_top.tpl');
762         if(count($r)) {
763                 foreach($r as $rr) {
764                         $o .= replace_macros($tpl,array(
765                                 '$id' => $rr['id'],
766                                 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] 
767                                         . '/image/' . $rr['resource-id'],
768                                 '$phototitle' => t('View Photo'),
769                                 '$imgsrc' => $a->get_baseurl() . '/photo/' 
770                                         . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
771                                 '$albumlink' => $a->get_baseurl . '/photos/' 
772                                         . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
773                                 '$albumname' => $rr['album'],
774                                 '$albumalt' => t('View Album'),
775                                 '$imgalt' => $rr['filename']
776                         ));
777
778                 }
779                 $o .= '<div id="photo-top-end"></div>';
780         }
781         return $o;
782 }