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