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