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