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