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