]> git.mxchange.org Git - friendica.git/blob - mod/photos.php
63300ebee0f148a41621ebbfcca95a7cd4d4e658
[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                         $tags = get_tags($rawtags);
263
264                         if(count($tags)) {
265                                 foreach($tags as $tag) {
266                                         if(strpos($tag,'@') === 0) {
267                                                 $name = substr($tag,1);
268                                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
269                                                         $newname = $name;
270                                                         $links = @lrdd($name);
271                                                         if(count($links)) {
272                                                                 foreach($links as $link) {
273                                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
274                                                         $profile = $link['@attributes']['href'];
275                                                                         if($link['@attributes']['rel'] === 'salmon') {
276                                                                                 if(strlen($inform))
277                                                                                         $inform .= ',';
278                                                         $inform .= 'url:' . str_replace(',','%2c',$link['@attributes']['href']);
279                                                                         }
280                                                                 }
281                                                         }
282                                                 }
283                                                 else {
284                                                         $newname = $name;
285                                                         if(strstr($name,'_')) {
286                                                                 $newname = str_replace('_',' ',$name);
287                                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
288                                                                         dbesc($newname),
289                                                                         intval(local_user())
290                                                                 );
291                                                         }
292                                                         else {
293                                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
294                                                                         dbesc($name),
295                                                                         intval(local_user())
296                                                                 );
297                                                         }
298                                                         if(count($r)) {
299                                                                 $profile = $r[0]['url'];
300                                                                 if(strlen($inform))
301                                                                         $inform .= ',';
302                                                                 $inform .= 'cid:' . $r[0]['id'];
303                                                         }
304                                                 }
305                                                 if($profile) {
306                                                         if(strlen($str_tags))
307                                                                 $str_tags .= ',';
308                                                         $profile = str_replace(',','%2c',$profile);
309                                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
310                                                 }
311                                         }
312                                 }
313                         }
314
315                         $newtag = $old_tag;
316                         if(strlen($newtag) && strlen($str_tags)) 
317                                 $newtag .= ',';
318                         $newtag .= $str_tags;
319
320                         $newinform = $old_inform;
321                         if(strlen($newinform) && strlen($inform))
322                                 $newinform .= ',';
323                         $newinform .= $inform;
324
325                         $r = q("UPDATE `item` SET `tag` = '%s', `inform` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
326                                 dbesc($newtag),
327                                 dbesc($newinform),
328                                 dbesc(datetime_convert()),
329                                 dbesc(datetime_convert()),
330                                 intval($item_id),
331                                 intval(local_user())
332                         );
333                 }
334                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
335                 return; // NOTREACHED
336         }
337
338
339
340
341         if(! x($_FILES,'userfile'))
342                 killme();
343
344         if($_POST['partitionCount'])
345                 $java_upload = true;
346         else
347                 $java_upload = false;
348
349         $album =  notags(trim($_POST['album']));
350         $newalbum = notags(trim($_POST['newalbum']));
351
352         if(! strlen($album)) {
353                 if(strlen($newalbum))
354                         $album = $newalbum;
355                 else
356                         $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
357         }
358
359         $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
360                 dbesc($album),
361                 intval(local_user())
362         );
363         if((! count($r)) || ($album == t('Profile Photos')))
364                 $visible = 1;
365         else
366                 $visibile = 0;
367
368
369         $str_group_allow   = perms2str($_POST['group_allow']);
370         $str_contact_allow = perms2str($_POST['contact_allow']);
371         $str_group_deny    = perms2str($_POST['group_deny']);
372         $str_contact_deny  = perms2str($_POST['contact_deny']);
373
374         $src               = $_FILES['userfile']['tmp_name'];
375         $filename          = basename($_FILES['userfile']['name']);
376         $filesize          = intval($_FILES['userfile']['size']);
377
378         $imagedata = @file_get_contents($src);
379         $ph = new Photo($imagedata);
380
381         if(! $ph->is_valid()) {
382                 notice( t('Unable to process image.') . EOL );
383                 @unlink($src);
384                 killme();
385         }
386
387         @unlink($src);
388
389         $width = $ph->getWidth();
390         $height = $ph->getHeight();
391
392         $smallest = 0;
393
394         $photo_hash = photo_new_resource();
395
396         $r = $ph->store(local_user(), 0, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
397
398         if(! $r) {
399                 notice( t('Image upload failed.') . EOL );
400                 killme();
401         }
402
403         if($width > 640 || $height > 640) {
404                 $ph->scaleImage(640);
405                 $ph->store(local_user(), 0, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
406                 $smallest = 1;
407         }
408
409         if($width > 320 || $height > 320) {
410                 $ph->scaleImage(320);
411                 $ph->store(local_user(), 0, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
412                 $smallest = 2;
413         }
414         
415         $basename = basename($filename);
416         $uri = item_new_uri($a->get_hostname(), local_user());
417
418         // Create item container
419
420
421         $arr = array();
422
423         $arr['uid']          = local_user();
424         $arr['uri']          = $uri;
425         $arr['parent-uri']   = $uri;
426         $arr['type']         = 'photo';
427         $arr['wall']         = 1;
428         $arr['resource-id']  = $photo_hash;
429         $arr['contact-id']   = $contact_record['id'];
430         $arr['owner-name']   = $contact_record['name'];
431         $arr['owner-link']   = $contact_record['url'];
432         $arr['owner-avatar'] = $contact_record['thumb'];
433         $arr['title']        = $title;
434         $arr['allow_cid']    = $str_contact_allow;
435         $arr['allow_gid']    = $str_group_allow;
436         $arr['deny_cid']     = $str_contact_deny;
437         $arr['deny_gid']     = $str_group_deny;
438         $arr['last-child']   = 1;
439         $arr['visible']      = $visible;
440         $arr['body']         = '[url=' . $a->get_baseurl() . '/photos/' . $contact_record['nickname'] . '/image/' . $photo_hash . ']' 
441                                 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]' 
442                                 . '[/url]';
443
444         $item_id = item_store($arr);
445
446         if(! $java_upload) {
447                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
448                 return; // NOTREACHED
449         }
450
451         killme();
452         return; // NOTREACHED
453
454 }
455
456
457
458 function photos_content(&$a) {
459
460         // URLs:
461         // photos/name
462         // photos/name/upload
463         // photos/name/album/xxxxx
464         // photos/name/album/xxxxx/edit
465         // photos/name/image/xxxxx
466         // photos/name/image/xxxxx/edit
467
468
469         if(! x($a->data,'user')) {
470                 notice( t('No photos selected') . EOL );
471                 return;
472         }
473
474         $_SESSION['photo_return'] = $a->cmd;
475
476         //
477         // Parse arguments 
478         //
479
480         if($a->argc > 3) {
481                 $datatype = $a->argv[2];
482                 $datum = $a->argv[3];
483         }
484         elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
485                 $datatype = 'upload';
486         else
487                 $datatype = 'summary';
488
489         if($a->argc > 4)
490                 $cmd = $a->argv[4];
491         else
492                 $cmd = 'view';
493
494         //
495         // Setup permissions structures
496         //
497
498         $owner_uid = $a->data['user']['uid'];
499
500
501
502         $contact = null;
503         $remote_contact = false;
504
505         if(remote_user()) {
506                 $contact_id = $_SESSION['visitor_id'];
507                 $groups = init_groups_visitor($contact_id);
508                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
509                         intval($contact_id),
510                         intval($owner_uid)
511                 );
512                 if(count($r)) {
513                         $contact = $r[0];
514                         $remote_contact = true;
515                 }
516         }
517
518         if(! $remote_contact) {
519                 if(local_user()) {
520                         $contact_id = $_SESSION['cid'];
521                         $contact = $a->contact;
522                 }
523         }
524
525
526         // default permissions - anonymous user
527
528         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
529
530         // Profile owner - everything is visible
531
532         if(local_user() && (local_user() == $owner_uid)) {
533                 $sql_extra = '';        
534         }
535         elseif(remote_user()) {
536                 // authenticated visitor - here lie dragons
537                 $gs = '<<>>'; // should be impossible to match
538                 if(count($groups)) {
539                         foreach($groups as $g)
540                                 $gs .= '|<' . intval($g) . '>';
541                 } 
542                 $sql_extra = sprintf(
543                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
544                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
545                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
546                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
547
548                         intval($_SESSION['visitor_id']),
549                         intval($_SESSION['visitor_id']),
550                         dbesc($gs),
551                         dbesc($gs)
552                 );
553         }
554
555         //
556         // dispatch request
557         //
558
559
560         if($datatype === 'upload') {
561                 if( ! (local_user() && (local_user() == $a->data['user']['uid']))) {
562                         notice( t('Permission denied.'));
563                         return;
564                 }
565                 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
566
567                 $albumselect .= '<option value="" selected="selected" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
568                 if(count($a->data['albums'])) {
569                         foreach($a->data['albums'] as $album) {
570                                 if(($album['album'] === '') || ($album['album'] == t('Contact Photos')))
571                                         continue;
572                                 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
573                         }
574                 }
575
576                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
577
578                 $albumselect .= '</select>';
579                 $tpl = load_view_file('view/photos_upload.tpl');
580                 $o .= replace_macros($tpl,array(
581                         '$pagename' => t('Upload Photos'),
582                         '$sessid' => session_id(),
583                         '$newalbum' => t('New album name: '),
584                         '$existalbumtext' => t('or existing album name: '),
585                         '$filestext' => t('Select files to upload: '),
586                         '$albumselect' => $albumselect,
587                         '$permissions' => t('Permissions'),
588                         '$aclselect' => populate_acl($a->user, $celeb),
589                         '$archive' => $a->get_baseurl() . '/jumploader_z.jar',
590                         '$nojava' => t('Use the following controls only if the Java uploader [above] fails to launch.'),
591                         '$uploadurl' => $a->get_baseurl() . '/photos',
592                         '$submit' => t('Submit')
593                 ));
594
595                 return $o; 
596
597         }
598
599         if($datatype === 'album') {
600
601                 $album = hex2bin($datum);
602
603                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
604                         $sql_extra GROUP BY `resource-id`",
605                         intval($a->data['user']['uid']),
606                         dbesc($album)
607                 );
608                 if(count($r))
609                         $a->set_pager_total(count($r));
610
611
612                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
613                         $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
614                         intval($a->data['user']['uid']),
615                         dbesc($album),
616                         intval($a->pager['start']),
617                         intval($a->pager['itemspage'])
618                 );
619
620                 $o .= '<h3>' . $album . '</h3>';
621                 
622                 if($cmd === 'edit') {           
623                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
624                                 if(local_user() && (local_user() == $a->data['user']['uid'])) {
625                                         $edit_tpl = load_view_file('view/album_edit.tpl');
626                                         $o .= replace_macros($edit_tpl,array(
627                                                 '$nametext' => t('New album name: '),
628                                                 '$album' => $album,
629                                                 '$hexalbum' => bin2hex($album),
630                                                 '$submit' => t('Submit'),
631                                                 '$dropsubmit' => t('Delete Album')
632                                         ));
633                                 }
634                         }
635                 }
636                 else {
637                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
638                                 if(local_user() && (local_user() == $a->data['user']['uid'])) {
639                                         $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/' 
640                                                 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">' 
641                                                 . t('Edit Album') . '</a></div>';
642                                 }
643                         }
644                 }
645                 $tpl = load_view_file('view/photo_album.tpl');
646                 if(count($r))
647                         foreach($r as $rr) {
648                                 $o .= replace_macros($tpl,array(
649                                         '$id' => $rr['id'],
650                                         '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
651                                         '$phototitle' => t('View Photo'),
652                                         '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
653                                         '$imgalt' => $rr['filename']
654                                 ));
655
656                 }
657                 $o .= '<div id="photo-album-end"></div>';
658                 return $o;
659
660         }       
661
662
663         if($datatype === 'image') {
664
665                 require_once('security.php');
666                 require_once('bbcode.php');
667
668                 // fetch image, item containing image, then comments
669
670                 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
671                         $sql_extra ORDER BY `scale` ASC ",
672                         intval($a->data['user']['uid']),
673                         dbesc($datum)
674                 );
675
676                 if(! count($ph)) {
677                         notice( t('Photo not available') . EOL );
678                         return;
679                 }
680
681                 if(count($ph) == 1)
682                         $hires = $lores = $ph[0];
683                 if(count($ph) > 1) {
684                         if($ph[1]['scale'] == 2) {
685                                 // original is 640 or less, we can display it directly
686                                 $hires = $lores = $ph[0];
687                         }
688                         else {
689                         $hires = $ph[0];
690                         $lores = $ph[1];
691                         }
692                 }
693
694                 
695                 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']) . '">' . $ph[0]['album'] . '</a></h3>';
696  
697                 if(local_user() && ($ph[0]['uid'] == local_user())) {
698                         $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>';
699                 }
700
701
702                 $o .= '<a href="' . $a->get_baseurl() . '/photo/' 
703                         . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="' 
704                         . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/' 
705                         . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
706
707
708                 // Do we have an item for this photo?
709
710                 $i1 = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
711                         dbesc($datum)
712                 );
713                 if(count($i1)) {
714
715                         $r = q("SELECT COUNT(*) AS `total`
716                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
717                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
718                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
719                                 AND `item`.`uid` = %d 
720                                 $sql_extra ",
721                                 dbesc($i1[0]['uri']),
722                                 dbesc($i1[0]['uri']),
723                                 intval($i1[0]['uid'])
724
725                         );
726
727                         if(count($r))
728                                 $a->set_pager_total($r[0]['total']);
729
730
731                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
732                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`, 
733                                 `contact`.`rel`, `contact`.`thumb`, `contact`.`self`, 
734                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
735                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
736                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
737                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
738                                 AND `item`.`uid` = %d
739                                 $sql_extra
740                                 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
741                                 dbesc($i1[0]['uri']),
742                                 dbesc($i1[0]['uri']),
743                                 intval($i1[0]['uid']),
744                                 intval($a->pager['start']),
745                                 intval($a->pager['itemspage'])
746
747                         );
748                 }
749
750                 $o .= '<div id="photo-caption" >' . $ph[0]['desc'] . '</div>';
751
752                 if(count($i1) && strlen($i1[0]['tag'])) {
753                         $arr = explode(',',$i1[0]['tag']);
754                         // parse tags and add links     
755                         $o .= '<div id="in-this-photo-text">' . t('Tags: ') . '</div>';
756                         $o .= '<div id="in-this-photo">';
757                         $tag_str = '';
758                         foreach($arr as $t) {
759                                 if(strlen($tag_str))
760                                         $tag_str .= ', ';
761                                 $tag_str .= bbcode($t);
762                         } 
763                         $o .= $tag_str . '</div>';
764                 }
765
766                 if($cmd === 'edit') {
767                         $edit_tpl = load_view_file('view/photo_edit.tpl');
768                         $o .= replace_macros($edit_tpl, array(
769                                 '$id' => $ph[0]['id'],
770                                 '$resource_id' => $ph[0]['resource-id'],
771                                 '$capt_label' => t('Caption'),
772                                 '$caption' => $ph[0]['desc'],
773                                 '$tag_label' => t('Add a Tag'),
774                                 '$tags' => $i1[0]['tag'],
775                                 '$help_tags' => t('Example: @bob, @Barbara_Jensen, @jim@example.com, #California, #camping'),
776                                 '$item_id' => ((count($i1)) ? $i1[0]['id'] : 0),
777                                 '$submit' => t('Submit'),
778                                 '$delete' => t('Delete Photo')
779
780                         ));
781                 }
782
783                 if(count($i1)) {
784
785                         $cmnt_tpl = load_view_file('view/comment_item.tpl');
786                         $tpl = load_view_file('view/photo_item.tpl');
787                         $return_url = $a->cmd;
788
789                         $like_tpl = load_view_file('view/like.tpl');
790
791                         if(can_write_wall($a,$a->data['user']['uid'])) {
792                                 if($i1[0]['last-child']) {
793                                         $o .= replace_macros($cmnt_tpl,array(
794                                                 '$return_path' => $return_url,
795                                                 '$type' => 'wall-comment',
796                                                 '$id' => $i1[0]['id'],
797                                                 '$parent' => $i1[0]['id'],
798                                                 '$profile_uid' =>  $a->data['user']['uid'],
799                                                 '$mylink' => $contact['url'],
800                                                 '$mytitle' => t('This is you'),
801                                                 '$myphoto' => $contact['thumb'],
802                                                 '$ww' => ''
803                                         ));
804                                 }
805                         }
806
807                         $alike = array();
808                         $dlike = array();
809
810                         // display comments
811                         if(count($r)) {
812
813                                 foreach($r as $item) {
814                                         like_puller($a,$item,$alike,'like');
815                                         like_puller($a,$item,$dlike,'dislike');
816                                 }
817
818                                 $likebuttons = '';
819
820                                 if(can_write_wall($a,$a->data['user']['uid']))
821                                         $likebuttons = replace_macros($like_tpl,array('$id' => $i1[0]['id']));
822
823                     $like    = ((isset($alike[$i1[0]['id']])) ? format_like($alike[$i1[0]['id']],$alike[$i1[0]['id'] . '-l'],'like',$i1[0]['id']) : '');
824                                 $dislike = ((isset($dlike[$i1[0]['id']])) ? format_like($dlike[$i1[0]['id']],$dlike[$i1[0]['id'] . '-l'],'dislike',$i1[0]['id']) : '');
825
826                                 $o .= $likebuttons;
827                                 $o .= $like;
828                                 $o .= $dislike;
829
830                                 foreach($r as $item) {
831                                         $comment = '';
832                                         $template = $tpl;
833                                         $sparkle = '';
834
835                                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) && ($item['id'] != $item['parent']))
836                                                 continue;
837
838                                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
839                         
840                                         if(can_write_wall($a,$a->data['user']['uid'])) {
841
842                                                 if($item['last-child']) {
843                                                         $comment = replace_macros($cmnt_tpl,array(
844                                                                 '$return_path' => $return_url,
845                                                                 '$type' => 'wall-comment',
846                                                                 '$id' => $item['item_id'],
847                                                                 '$parent' => $item['parent'],
848                                                                 '$profile_uid' =>  $a->data['user']['uid'],
849                                                                 '$mylink' => $contact['url'],
850                                                                 '$mytitle' => t('This is you'),
851                                                                 '$myphoto' => $contact['thumb'],
852                                                                 '$ww' => ''
853                                                         ));
854                                                 }
855                                         }
856
857
858                                         if(local_user() && ($item['contact-uid'] == local_user()) 
859                                                 && ($item['network'] == 'dfrn') && (! $item['self'] )) {
860                                                 $profile_url = $redirect_url;
861                                                 $sparkle = ' sparkle';
862                                         }
863                                         else {
864                                                 $profile_url = $item['url'];
865                                                 $sparkle = '';
866                                         }
867  
868                                         $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
869                                         $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
870                                         $profile_link = $profile_url;
871
872                                         $drop = '';
873
874                                         if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == local_user()))
875                                                 $drop = replace_macros(load_view_file('view/wall_item_drop.tpl'), array('$id' => $item['id']));
876
877
878                                         $o .= replace_macros($template,array(
879                                                 '$id' => $item['item_id'],
880                                                 '$profile_url' => $profile_link,
881                                                 '$name' => $profile_name,
882                                                 '$thumb' => $profile_avatar,
883                                                 '$sparkle' => $sparkle,
884                                                 '$title' => $item['title'],
885                                                 '$body' => bbcode($item['body']),
886                                                 '$ago' => relative_date($item['created']),
887                                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
888                                                 '$drop' => $drop,
889                                                 '$comment' => $comment
890                                         ));
891                                 }
892                         }
893
894                         $o .= paginate($a);
895                 }
896                 return $o;
897         }
898
899         // Default - show recent photos with upload link (if applicable)
900
901         $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
902                 $sql_extra GROUP BY `resource-id`",
903                 intval($a->data['user']['uid']),
904                 dbesc( t('Contact Photos'))
905         );
906         if(count($r))
907                 $a->set_pager_total(count($r));
908
909
910         $r = q("SELECT `resource-id`, `album`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
911                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
912                 intval($a->data['user']['uid']),
913                 dbesc( t('Contact Photos')),
914                 intval($a->pager['start']),
915                 intval($a->pager['itemspage'])
916         );
917
918         $o .= '<h3>' . t('Recent Photos') . '</h3>';
919
920         if( local_user() && (local_user() == $a->data['user']['uid'])) {
921                 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/' 
922                         . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
923         }
924
925         $tpl = load_view_file('view/photo_top.tpl');
926         if(count($r)) {
927                 foreach($r as $rr) {
928                         $o .= replace_macros($tpl,array(
929                                 '$id' => $rr['id'],
930                                 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] 
931                                         . '/image/' . $rr['resource-id'],
932                                 '$phototitle' => t('View Photo'),
933                                 '$imgsrc' => $a->get_baseurl() . '/photo/' 
934                                         . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
935                                 '$albumlink' => $a->get_baseurl . '/photos/' 
936                                         . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
937                                 '$albumname' => $rr['album'],
938                                 '$albumalt' => t('View Album'),
939                                 '$imgalt' => $rr['filename']
940                         ));
941
942                 }
943                 $o .= '<div id="photo-top-end"></div>';
944         }
945         return $o;
946 }