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