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