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