]> git.mxchange.org Git - friendica.git/blob - mod/photos.php
35b03845d7703ef18b8600f366f4b879eb0c6c50
[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(get_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(get_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(get_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                                         $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(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                                 $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(get_uid())
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(get_uid())
199                         );
200                 }
201                 if(! $item_id) {
202
203                         $title = '';
204                         $basename = basename($filename);
205                         $uri = item_new_uri($a->get_hostname(),get_uid());
206                         // Create item container
207
208                         $arr = array();
209
210                         $arr['uid']          = get_uid();
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(get_uid())
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(get_uid())
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(get_uid(), 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(get_uid(), 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(get_uid(), 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(), get_uid());
325
326         // Create item container
327
328
329         $arr = array();
330
331         $arr['uid']          = get_uid();
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() && (get_uid() == $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() && (get_uid() == $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                 $albumselect .= '</select>';
484                 $tpl = load_view_file('view/photos_upload.tpl');
485                 $o .= replace_macros($tpl,array(
486                         '$pagename' => t('Upload Photos'),
487                         '$sessid' => session_id(),
488                         '$newalbum' => t('New album name: '),
489                         '$existalbumtext' => t('or existing album name: '),
490                         '$filestext' => t('Select files to upload: '),
491                         '$albumselect' => $albumselect,
492                         '$permissions' => t('Permissions'),
493                         '$aclselect' => populate_acl($a->user),
494                         '$archive' => $a->get_baseurl() . '/jumploader_z.jar',
495                         '$nojava' => t('Use the following controls only if the Java uploader [above] fails to launch.'),
496                         '$uploadurl' => $a->get_baseurl() . '/photos',
497                         '$submit' => t('Submit')
498                 ));
499
500                 return $o; 
501
502         }
503
504         if($datatype === 'album') {
505
506                 $album = hex2bin($datum);
507
508                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
509                         $sql_extra GROUP BY `resource-id`",
510                         intval($a->data['user']['uid']),
511                         dbesc($album)
512                 );
513                 if(count($r))
514                         $a->set_pager_total(count($r));
515
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` ORDER BY `created` DESC LIMIT %d , %d",
519                         intval($a->data['user']['uid']),
520                         dbesc($album),
521                         intval($a->pager['start']),
522                         intval($a->pager['itemspage'])
523                 );
524
525                 $o .= '<h3>' . $album . '</h3>';
526                 
527                 if($cmd === 'edit') {           
528                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
529                                 if(local_user() && (get_uid() == $a->data['user']['uid'])) {
530                                         $edit_tpl = load_view_file('view/album_edit.tpl');
531                                         $o .= replace_macros($edit_tpl,array(
532                                                 '$nametext' => t('New album name: '),
533                                                 '$album' => $album,
534                                                 '$hexalbum' => bin2hex($album),
535                                                 '$submit' => t('Submit'),
536                                                 '$dropsubmit' => t('Delete Album')
537                                         ));
538                                 }
539                         }
540                 }
541                 else {
542                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
543                                 if(local_user() && (get_uid() == $a->data['user']['uid'])) {
544                                         $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/' 
545                                                 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">' 
546                                                 . t('Edit Album') . '</a></div>';
547                                 }
548                         }
549                 }
550                 $tpl = load_view_file('view/photo_album.tpl');
551                 if(count($r))
552                         foreach($r as $rr) {
553                                 $o .= replace_macros($tpl,array(
554                                         '$id' => $rr['id'],
555                                         '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
556                                         '$phototitle' => t('View Photo'),
557                                         '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
558                                         '$imgalt' => $rr['filename']
559                                 ));
560
561                 }
562                 $o .= '<div id="photo-album-end"></div>';
563                 return $o;
564
565         }       
566
567
568         if($datatype === 'image') {
569
570                 require_once('security.php');
571                 require_once('bbcode.php');
572
573                 // fetch image, item containing image, then comments
574
575                 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
576                         $sql_extra ORDER BY `scale` ASC ",
577                         intval($a->data['user']['uid']),
578                         dbesc($datum)
579                 );
580
581                 if(! count($ph)) {
582                         notice( t('Photo not available') . EOL );
583                         return;
584                 }
585
586                 if(count($ph) == 1)
587                         $hires = $lores = $ph[0];
588                 if(count($ph) > 1) {
589                         if($ph[1]['scale'] == 2) {
590                                 // original is 640 or less, we can display it directly
591                                 $hires = $lores = $ph[0];
592                         }
593                         else {
594                         $hires = $ph[0];
595                         $lores = $ph[1];
596                         }
597                 }
598
599                 
600                 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']) . '">' . $ph[0]['album'] . '</a></h3>';
601  
602                 if(local_user() && ($ph[0]['uid'] == get_uid())) {
603                         $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>';
604                 }
605
606
607                 $o .= '<a href="' . $a->get_baseurl() . '/photo/' 
608                         . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="' 
609                         . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/' 
610                         . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
611
612
613                 // Do we have an item for this photo?
614
615                 $i1 = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
616                         dbesc($datum)
617                 );
618                 if(count($i1)) {
619
620                         $r = q("SELECT COUNT(*) AS `total`
621                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
622                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
623                                 AND NOT `item`.`type` IN ( 'remote', 'net-comment') 
624                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
625                                 $sql_extra ",
626                                 dbesc($i1[0]['uri']),
627                                 dbesc($i1[0]['uri'])
628
629                         );
630
631                         if(count($r))
632                                 $a->set_pager_total($r[0]['total']);
633
634
635                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
636                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, 
637                                 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, 
638                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
639                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
640                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
641                                 AND NOT `item`.`type` IN ( 'remote', 'net-comment') 
642                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
643                                 $sql_extra
644                                 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
645                                 dbesc($i1[0]['uri']),
646                                 dbesc($i1[0]['uri']),
647                                 intval($a->pager['start']),
648                                 intval($a->pager['itemspage'])
649
650                         );
651                 }
652
653                 $o .= '<div id="photo-caption" >' . $ph[0]['desc'] . '</div>';
654
655                 if(count($i1) && strlen($i1[0]['tag'])) {
656                         // parse tags and add links     
657                         $o .= '<div id="in-this-photo-text">' . t('In this photo: ') . '</div>';
658                         $o .= '<div id="in-this-photo">' . $i1[0]['tag'] . '</div>';
659                 }
660
661                 if($cmd === 'edit') {
662                         $edit_tpl = load_view_file('view/photo_edit.tpl');
663                         $o .= replace_macros($edit_tpl, array(
664                                 '$id' => $ph[0]['id'],
665                                 '$resource_id' => $ph[0]['resource-id'],
666                                 '$capt_label' => t('Caption'),
667                                 '$caption' => $ph[0]['desc'],
668                                 '$tag_label' => t('Tags'),
669                                 '$tags' => $i1[0]['tag'],
670                                 '$item_id' => ((count($i1)) ? $i1[0]['id'] : 0),
671                                 '$submit' => t('Submit'),
672                                 '$delete' => t('Delete Photo')
673
674                         ));
675                 }
676
677                 if(count($i1)) {
678                         // pull out how many people like the photo
679
680                         $cmnt_tpl = load_view_file('view/comment_item.tpl');
681                         $tpl = load_view_file('view/photo_item.tpl');
682                         $return_url = $a->cmd;
683
684                         if(can_write_wall($a,$a->data['user']['uid'])) {
685                                 if($i1[0]['last-child']) {
686                                         $o .= replace_macros($cmnt_tpl,array(
687                                                 '$return_path' => $return_url,
688                                                 '$type' => 'wall-comment',
689                                                 '$id' => $i1[0]['id'],
690                                                 '$parent' => $i1[0]['id'],
691                                                 '$profile_uid' =>  $a->data['user']['uid'],
692                                                 '$mylink' => $contact['url'],
693                                                 '$mytitle' => t('This is you'),
694                                                 '$myphoto' => $contact['thumb'],
695                                                 '$ww' => ''
696                                         ));
697                                 }
698                         }
699
700
701                         // display comments
702                         if(count($r)) {
703                                 foreach($r as $item) {
704                                         $comment = '';
705                                         $template = $tpl;
706                         
707                                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
708                         
709                                         if(can_write_wall($a,$a->data['user']['uid'])) {
710                                                 if($item['last-child']) {
711                                                         $comment = replace_macros($cmnt_tpl,array(
712                                                                 '$return_path' => $return_url,
713                                                                 '$type' => 'wall-comment',
714                                                                 '$id' => $item['item_id'],
715                                                                 '$parent' => $item['parent'],
716                                                                 '$profile_uid' =>  $a->data['user']['uid'],
717                                                                 '$mylink' => $contact['url'],
718                                                                 '$mytitle' => t('This is you'),
719                                                                 '$myphoto' => $contact['thumb'],
720                                                                 '$ww' => ''
721                                                         ));
722                                                 }
723                                         }
724
725
726                                         if(local_user() && ($item['contact-uid'] == get_uid()) 
727                                                 && ($item['rel'] == REL_VIP || $item['rel'] == REL_BUD) && (! $item['self'] )) {
728                                                 $profile_url = $redirect_url;
729                                                 $sparkle = ' sparkle';
730                                         }
731                                         else {
732                                                 $profile_url = $item['url'];
733                                                 $sparkle = '';
734                                         }
735  
736                                         $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
737                                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
738                                         $profile_link = $profile_url;
739
740                                         $drop = '';
741
742                                         if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == get_uid()))
743                                                 $drop = replace_macros(load_view_file('view/wall_item_drop.tpl'), array('$id' => $item['id']));
744
745
746                                         $o .= replace_macros($template,array(
747                                                 '$id' => $item['item_id'],
748                                                 '$profile_url' => $profile_link,
749                                                 '$name' => $profile_name,
750                                                 '$thumb' => $profile_avatar,
751                                                 '$sparkle' => $sparkle,
752                                                 '$title' => $item['title'],
753                                                 '$body' => bbcode($item['body']),
754                                                 '$ago' => relative_date($item['created']),
755                                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
756                                                 '$drop' => $drop,
757                                                 '$comment' => $comment
758                                         ));
759                                 }
760                         }
761
762                         $o .= paginate($a);
763                 }
764                 return $o;
765         }
766
767         // Default - show recent photos with upload link (if applicable)
768
769         $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
770                 $sql_extra GROUP BY `resource-id`",
771                 intval($a->data['user']['uid']),
772                 dbesc( t('Contact Photos'))
773         );
774         if(count($r))
775                 $a->set_pager_total(count($r));
776
777
778         $r = q("SELECT `resource-id`, `album`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
779                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
780                 intval($a->data['user']['uid']),
781                 dbesc( t('Contact Photos')),
782                 intval($a->pager['start']),
783                 intval($a->pager['itemspage'])
784         );
785
786         $o .= '<h3>' . t('Recent Photos') . '</h3>';
787
788         if( local_user() && (get_uid() == $a->data['user']['uid'])) {
789                 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/' 
790                         . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
791         }
792
793         $tpl = load_view_file('view/photo_top.tpl');
794         if(count($r)) {
795                 foreach($r as $rr) {
796                         $o .= replace_macros($tpl,array(
797                                 '$id' => $rr['id'],
798                                 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] 
799                                         . '/image/' . $rr['resource-id'],
800                                 '$phototitle' => t('View Photo'),
801                                 '$imgsrc' => $a->get_baseurl() . '/photo/' 
802                                         . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
803                                 '$albumlink' => $a->get_baseurl . '/photos/' 
804                                         . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
805                                 '$albumname' => $rr['album'],
806                                 '$albumalt' => t('View Album'),
807                                 '$imgalt' => $rr['filename']
808                         ));
809
810                 }
811                 $o .= '<div id="photo-top-end"></div>';
812         }
813         return $o;
814 }