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