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