]> git.mxchange.org Git - friendica.git/blob - mod/photos.php
Merge pull request #115 from fabrixxm/tr
[friendica.git] / mod / photos.php
1 <?php
2 require_once('include/Photo.php');
3 require_once('include/items.php');
4 require_once('include/acl_selectors.php');
5 require_once('include/bbcode.php');
6
7 function photos_init(&$a) {
8
9
10         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
11                 return;
12         }
13         $o = '';
14
15         if($a->argc > 1) {
16                 $nick = $a->argv[1];
17                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
18                         dbesc($nick)
19                 );
20
21                 if(! count($r))
22                         return;
23
24                 $a->data['user'] = $r[0];
25
26                 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d",
27                         intval($a->data['user']['uid'])
28                 );
29
30                 if(count($albums)) {
31                         $a->data['albums'] = $albums;
32
33                         $o .= '<h4><a href="' . $a->get_baseurl() . '/profile/' . $a->data['user']['nickname'] . '">' . $a->data['user']['username'] . '</a></h4>';
34                         $o .= '<h4>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
35                 
36                         $o .= '<ul>';
37                         foreach($albums as $album) {
38                                 if((! strlen($album['album'])) || ($album['album'] == t('Contact Photos')))
39                                         continue;
40                                 $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" />' . $album['album'] . '</a></li>'; 
41                         }
42                         $o .= '</ul>';
43                 }
44
45                 if(! x($a->page,'aside'))
46                         $a->page['aside'] = '';
47                 $a->page['aside'] .= $o;
48
49
50                 $a->page['htmlhead'] .= "<script> var ispublic = '" . t('everybody') . "';" ;
51
52                 $a->page['htmlhead'] .= <<< EOT
53
54                 $(document).ready(function() {
55
56                         $('#contact_allow, #contact_deny, #group_allow, #group_deny').change(function() {
57                                 var selstr;
58                                 $('#contact_allow option:selected, #contact_deny option:selected, #group_allow option:selected, #group_deny option:selected').each( function() {
59                                         selstr = $(this).text();
60                                         $('#jot-perms-icon').removeClass('unlock').addClass('lock');
61                                         $('#jot-public').hide();
62                                 });
63                                 if(selstr == null) { 
64                                         $('#jot-perms-icon').removeClass('lock').addClass('unlock');
65                                         $('#jot-public').show();
66                                 }
67
68                         }).trigger('change');
69
70                 });
71
72                 </script>
73 EOT;
74         }
75
76         return;
77 }
78
79
80
81 function photos_post(&$a) {
82
83 logger('mod/photos.php: photos_post(): begin' , 'LOGGER_DEBUG');
84
85 foreach($_REQUEST AS $key => $val) {
86         logger('mod/photos.php: photos_post(): $_REQUEST key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
87 }
88
89 foreach($_FILES AS $key => $val) {
90         logger('mod/photos.php: photos_post(): $_FILES key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
91 }
92
93         $can_post  = false;
94         $visitor   = 0;
95
96         $page_owner_uid = $a->data['user']['uid'];
97         $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
98
99         if((local_user()) && (local_user() == $page_owner_uid))
100                 $can_post = true;
101         else {
102                 if($community_page && remote_user()) {
103                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
104                                 intval(remote_user()),
105                                 intval($page_owner_uid)
106                         );
107                         if(count($r)) {
108                                 $can_post = true;
109                                 $visitor = remote_user();
110                         }
111                 }
112         }
113
114         if(! $can_post) {
115                 notice( t('Permission denied.') . EOL );
116                 killme();
117         }
118
119         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
120                 WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
121                 intval($page_owner_uid)
122         );
123
124         if(! count($r)) {
125                 notice( t('Contact information unavailable') . EOL);
126                 logger('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
127                 killme();
128         }
129
130         $owner_record = $r[0];  
131
132
133         if(($a->argc > 3) && ($a->argv[2] === 'album')) {
134                 $album = hex2bin($a->argv[3]);
135
136                 if($album == t('Profile Photos') || $album == t('Contact Photos')) {
137                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
138                         return; // NOTREACHED
139                 }
140
141                 $r = q("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
142                         dbesc($album),
143                         intval($page_owner_uid)
144                 );
145                 if(! count($r)) {
146                         notice( t('Album not found.') . EOL);
147                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
148                         return; // NOTREACHED
149                 }
150
151                 $newalbum = notags(trim($_POST['albumname']));
152                 if($newalbum != $album) {
153                         q("UPDATE `photo` SET `album` = '%s' WHERE `album` = '%s' AND `uid` = %d",
154                                 dbesc($newalbum),
155                                 dbesc($album),
156                                 intval($page_owner_uid)
157                         );
158                         $newurl = str_replace(bin2hex($album),bin2hex($newalbum),$_SESSION['photo_return']);
159                         goaway($a->get_baseurl() . '/' . $newurl);
160                         return; // NOTREACHED
161                 }
162
163
164                 if($_POST['dropalbum'] == t('Delete Album')) {
165
166                         $res = array();
167
168                         // get the list of photos we are about to delete
169
170                         if($visitor) {
171                                 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `album` = '%s'",
172                                         intval($visitor),
173                                         intval($page_owner_uid),
174                                         dbesc($album)
175                                 );
176                         }
177                         else {
178                                 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'",
179                                         intval(local_user()),
180                                         dbesc($album)
181                                 );
182                         }
183                         if(count($r)) {
184                                 foreach($r as $rr) {
185                                         $res[] = "'" . dbesc($rr['rid']) . "'" ;
186                                 }
187                         }
188                         else {
189                                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
190                                 return; // NOTREACHED
191                         }
192
193                         $str_res = implode(',', $res);
194
195                         // remove the associated photos
196
197                         q("DELETE FROM `photo` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
198                                 intval($page_owner_uid)
199                         );
200
201                         // find and delete the corresponding item with all the comments and likes/dislikes
202
203                         $r = q("SELECT `parent-uri` FROM `item` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
204                                 intval($page_owner_uid)
205                         );
206                         if(count($r)) {
207                                 foreach($r as $rr) {
208                                         q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
209                                                 dbesc(datetime_convert()),
210                                                 dbesc($rr['parent-uri']),
211                                                 intval($page_owner_uid)
212                                         );
213
214                                         $drop_id = intval($rr['id']);
215
216                                         // send the notification upstream/downstream as the case may be
217
218                                         if($rr['visible'])
219                                                 proc_run('php',"include/notifier.php","drop","$drop_id");
220                                 }
221                         }
222                 }
223                 goaway($a->get_baseurl() . '/photos/' . $a->data['user']['nickname']);
224                 return; // NOTREACHED
225         }
226
227         if(($a->argc > 2) && (x($_POST,'delete')) && ($_POST['delete'] == t('Delete Photo'))) {
228
229                 // same as above but remove single photo
230
231                 if($visitor) {
232                         $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `resource-id` = '%s' LIMIT 1",
233                                 intval($visitor),
234                                 intval($page_owner_uid),
235                                 dbesc($a->argv[2])
236                         );
237                 }
238                 else {
239                         $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' LIMIT 1",
240                                 intval(local_user()),
241                                 dbesc($a->argv[2])
242                         );
243                 }
244                 if(count($r)) {
245                         q("DELETE FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
246                                 intval($page_owner_uid),
247                                 dbesc($r[0]['resource-id'])
248                         );
249                         $i = q("SELECT * FROM `item` WHERE `resource-id` = '%s' AND `uid` = %d LIMIT 1",
250                                 dbesc($r[0]['resource-id']),
251                                 intval($page_owner_uid)
252                         );
253                         if(count($i)) {
254                                 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
255                                         dbesc(datetime_convert()),
256                                         dbesc(datetime_convert()),
257                                         dbesc($i[0]['uri']),
258                                         intval($page_owner_uid)
259                                 );
260
261                                 $url = $a->get_baseurl();
262                                 $drop_id = intval($i[0]['id']);
263
264                                 if($i[0]['visible'])
265                                         proc_run('php',"include/notifier.php","drop","$drop_id");
266                         }
267                 }
268
269                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
270                 return; // NOTREACHED
271         }
272
273         if(($a->argc > 2) && ((x($_POST,'desc') !== false) || (x($_POST,'newtag') !== false)) || (x($_POST,'albname') !== false)) {
274
275                 $desc        = ((x($_POST,'desc'))    ? notags(trim($_POST['desc']))    : '');
276                 $rawtags     = ((x($_POST,'newtag'))  ? notags(trim($_POST['newtag']))  : '');
277                 $item_id     = ((x($_POST,'item_id')) ? intval($_POST['item_id'])       : 0);
278                 $albname     = ((x($_POST,'albname')) ? notags(trim($_POST['albname'])) : '');
279                 $str_group_allow   = perms2str($_POST['group_allow']);
280                 $str_contact_allow = perms2str($_POST['contact_allow']);
281                 $str_group_deny    = perms2str($_POST['group_deny']);
282                 $str_contact_deny  = perms2str($_POST['contact_deny']);
283
284                 $resource_id = $a->argv[2];
285
286                 if(! strlen($albname))
287                         $albname = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
288                 
289
290                 $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
291                         dbesc($resource_id),
292                         intval($page_owner_uid)
293                 );
294                 if(count($p)) {
295                         $r = q("UPDATE `photo` SET `desc` = '%s', `album` = '%s', `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d",
296                                 dbesc($desc),
297                                 dbesc($albname),
298                                 dbesc($str_contact_allow),
299                                 dbesc($str_group_allow),
300                                 dbesc($str_contact_deny),
301                                 dbesc($str_group_deny),
302                                 dbesc($resource_id),
303                                 intval($page_owner_uid)
304                         );
305                 }
306
307                 /* Don't make the item visible if the only change was the album name */
308
309                 $visibility = 0;
310                 if($p[0]['desc'] !== $desc || strlen($rawtags))
311                         $visibility = 1;
312                 
313                 if(! $item_id) {
314
315                         // Create item container
316
317                         $title = '';
318                         $basename = basename($filename);
319                         $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
320
321                         $arr = array();
322
323                         $arr['uid']           = $page_owner_uid;
324                         $arr['uri']           = $uri;
325                         $arr['parent-uri']    = $uri; 
326                         $arr['type']          = 'photo';
327                         $arr['wall']          = 1;
328                         $arr['resource-id']   = $p[0]['resource-id'];
329                         $arr['contact-id']    = $owner_record['id'];
330                         $arr['owner-name']    = $owner_record['name'];
331                         $arr['owner-link']    = $owner_record['url'];
332                         $arr['owner-avatar']  = $owner_record['thumb'];
333                         $arr['author-name']   = $owner_record['name'];
334                         $arr['author-link']   = $owner_record['url'];
335                         $arr['author-avatar'] = $owner_record['thumb'];
336                         $arr['title']         = $title;
337                         $arr['allow_cid']     = $p[0]['allow_cid'];
338                         $arr['allow_gid']     = $p[0]['allow_gid'];
339                         $arr['deny_cid']      = $p[0]['deny_cid'];
340                         $arr['deny_gid']      = $p[0]['deny_gid'];
341                         $arr['last-child']    = 1;
342                         $arr['visible']       = $visibility;
343                         
344                         $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']' 
345                                                 . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.jpg' . '[/img]' 
346                                                 . '[/url]';
347                 
348                         $item_id = item_store($arr);
349
350                 }
351
352                 if($item_id) {
353                         $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
354                                 intval($item_id),
355                                 intval($page_owner_uid)
356                         );
357                 }
358                 if(count($r)) {
359                         $old_tag    = $r[0]['tag'];
360                         $old_inform = $r[0]['inform'];
361                 }
362
363                 if(strlen($rawtags)) {
364
365                         $str_tags = '';
366                         $inform   = '';
367
368                         // if the new tag doesn't have a namespace specifier (@foo or #foo) give it a hashtag
369
370                         $x = substr($rawtags,0,1);
371                         if($x !== '@' && $x !== '#')
372                                 $rawtags = '#' . $rawtags;
373
374                         $taginfo = array();
375                         $tags = get_tags($rawtags);
376
377                         if(count($tags)) {
378                                 foreach($tags as $tag) {
379                                         if(strpos($tag,'@') === 0) {
380                                                 $name = substr($tag,1);
381                                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
382                                                         $newname = $name;
383                                                         $links = @lrdd($name);
384                                                         if(count($links)) {
385                                                                 foreach($links as $link) {
386                                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
387                                                         $profile = $link['@attributes']['href'];
388                                                                         if($link['@attributes']['rel'] === 'salmon') {
389                                                                                 $salmon = '$url:' . str_replace(',','%sc',$link['@attributes']['href']);
390                                                                                 if(strlen($inform))
391                                                                                         $inform .= ',';
392                                                         $inform .= $salmon;
393                                                                         }
394                                                                 }
395                                                         }
396                                                         $taginfo[] = array($newname,$profile,$salmon);
397                                                 }
398                                                 else {
399                                                         $newname = $name;
400                                                         if(strstr($name,'_')) {
401                                                                 $newname = str_replace('_',' ',$name);
402                                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
403                                                                         dbesc($newname),
404                                                                         intval($page_owner_uid)
405                                                                 );
406                                                         }
407                                                         else {
408                                                                 $r = q("SELECT * FROM `contact` WHERE `nick` = '%s' AND `uid` = %d LIMIT 1",
409                                                                         dbesc($name),
410                                                                         intval($page_owner_uid)
411                                                                 );
412                                                         }
413                                                         if(count($r)) {
414                                                                 $newname = $r[0]['name'];
415                                                                 $profile = $r[0]['url'];
416                                                                 $notify = 'cid:' . $r[0]['id'];
417                                                                 if(strlen($inform))
418                                                                         $inform .= ',';
419                                                                 $inform .= $notify;
420                                                         }
421                                                 }
422                                                 if($profile) {
423                                                         if(substr($notify,0,4) === 'cid:')
424                                                                 $taginfo[] = array($newname,$profile,$notify,$r[0],'@[url=' . str_replace(',','%2c',$profile) . ']' . $newname  . '[/url]');
425                                                         else
426                                                                 $taginfo[] = array($newname,$profile,$notify,null,$str_tags .= '@[url=' . $profile . ']' . $newname     . '[/url]');
427                                                         if(strlen($str_tags))
428                                                                 $str_tags .= ',';
429                                                         $profile = str_replace(',','%2c',$profile);
430                                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
431                                                 }
432                                         }
433                                 }
434                         }
435
436                         $newtag = $old_tag;
437                         if(strlen($newtag) && strlen($str_tags)) 
438                                 $newtag .= ',';
439                         $newtag .= $str_tags;
440
441                         $newinform = $old_inform;
442                         if(strlen($newinform) && strlen($inform))
443                                 $newinform .= ',';
444                         $newinform .= $inform;
445
446                         $r = q("UPDATE `item` SET `tag` = '%s', `inform` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
447                                 dbesc($newtag),
448                                 dbesc($newinform),
449                                 dbesc(datetime_convert()),
450                                 dbesc(datetime_convert()),
451                                 intval($item_id),
452                                 intval($page_owner_uid)
453                         );
454
455                         $best = 0;
456                         foreach($p as $scales) {
457                                 if(intval($scales['scale']) == 2) {
458                                         $best = 2;
459                                         break;
460                                 }
461                                 if(intval($scales['scale']) == 4) {
462                                         $best = 4;
463                                         break;
464                                 }
465                         }
466
467                         if(count($taginfo)) {
468                                 foreach($taginfo as $tagged) {
469                 
470                                         $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
471
472                                         $arr = array();
473
474                                         $arr['uid']           = $page_owner_uid;
475                                         $arr['uri']           = $uri;
476                                         $arr['parent-uri']    = $uri;
477                                         $arr['type']          = 'activity';
478                                         $arr['wall']          = 1;
479                                         $arr['contact-id']    = $owner_record['id'];
480                                         $arr['owner-name']    = $owner_record['name'];
481                                         $arr['owner-link']    = $owner_record['url'];
482                                         $arr['owner-avatar']  = $owner_record['thumb'];
483                                         $arr['author-name']   = $owner_record['name'];
484                                         $arr['author-link']   = $owner_record['url'];
485                                         $arr['author-avatar'] = $owner_record['thumb'];
486                                         $arr['title']         = '';
487                                         $arr['allow_cid']     = $p[0]['allow_cid'];
488                                         $arr['allow_gid']     = $p[0]['allow_gid'];
489                                         $arr['deny_cid']      = $p[0]['deny_cid'];
490                                         $arr['deny_gid']      = $p[0]['deny_gid'];
491                                         $arr['last-child']    = 1;
492                                         $arr['visible']       = 1;
493                                         $arr['verb']          = ACTIVITY_TAG;
494                                         $arr['object-type']   = ACTIVITY_OBJ_PERSON;
495                                         $arr['target-type']   = ACTIVITY_OBJ_PHOTO;
496                                         $arr['tag']           = $tagged[4];
497                                         $arr['inform']        = $tagged[2];
498
499                                         $arr['body']          = '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]' . ' ' . t('was tagged in a') . ' ' . '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . t('photo') . '[/url]' . ' ' . t('by') . ' ' . '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]' ;
500                                         $arr['body'] .= "\n\n" . '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . '[img]' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.jpg' . '[/img][/url]' . "\n" ;
501
502                                         $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>';
503                                         $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n");
504                                         if($tagged[3])
505                                                 $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $tagged[3]['photo'] . '" />' . "\n");
506                                         $arr['object'] .= '</link></object>' . "\n";
507
508                                         $arr['target'] = '<target><type>' . ACTIVITY_OBJ_PHOTO . '</type><title>' . $p[0]['desc'] . '</title><id>'
509                                                 . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '</id>';
510                                         $arr['target'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '" />' . "\n" . '<link rel="preview" type="image/jpeg" href="' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.jpg' . '" />') . '</link></target>';
511
512                                         $item_id = item_store($arr);
513                                         if($item_id)
514                                                 proc_run('php',"include/notifier.php","tag","$item_id");
515                                 }
516
517                         }
518
519                 }
520                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
521                 return; // NOTREACHED
522         }
523
524
525         /**
526          * default post action - upload a photo
527          */
528
529         call_hooks('photo_post_init', $_POST);
530
531         /**
532          * Determine the album to use
533          */
534
535         $album    = notags(trim($_REQUEST['album']));
536         $newalbum = notags(trim($_REQUEST['newalbum']));
537
538         logger('mod/photos.php: photos_post(): album= ' . $album . ' newalbum= ' . $newalbum , 'LOGGER_DEBUG');
539
540         if(! strlen($album)) {
541                 if(strlen($newalbum))
542                         $album = $newalbum;
543                 else
544                         $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
545         }
546
547         /**
548          *
549          * We create a wall item for every photo, but we don't want to
550          * overwhelm the data stream with a hundred newly uploaded photos.
551          * So we will make the first photo uploaded to this album in the last several hours
552          * visible by default, the rest will become visible over time when and if
553          * they acquire comments, likes, dislikes, and/or tags 
554          *
555          */
556
557         $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR ",
558                 dbesc($album),
559                 intval($page_owner_uid)
560         );
561         if((! count($r)) || ($album == t('Profile Photos')))
562                 $visible = 1;
563         else
564                 $visible = 0;
565
566         $str_group_allow   = perms2str(((is_array($_REQUEST['group_allow']))   ? $_REQUEST['group_allow']   : explode(',',$_REQUEST['group_allow'])));
567         $str_contact_allow = perms2str(((is_array($_REQUEST['contact_allow'])) ? $_REQUEST['contact_allow'] : explode(',',$_REQUEST['contact_allow'])));
568         $str_group_deny    = perms2str(((is_array($_REQUEST['group_deny']))    ? $_REQUEST['group_deny']    : explode(',',$_REQUEST['group_deny'])));
569         $str_contact_deny  = perms2str(((is_array($_REQUEST['contact_deny']))  ? $_REQUEST['contact_deny']  : explode(',',$_REQUEST['contact_deny'])));
570
571         $ret = array('src' => '', 'filename' => '', 'filesize' => 0);
572
573         call_hooks('photo_post_file',$ret);
574
575         if(x($ret,'src') && x($ret,'filesize')) {
576                 $src      = $ret['src'];
577                 $filename = $ret['filename'];
578                 $filesize = $ret['filesize'];
579         }
580         else {
581                 $src        = $_FILES['userfile']['tmp_name'];
582                 $filename   = basename($_FILES['userfile']['name']);
583                 $filesize   = intval($_FILES['userfile']['size']);
584         }
585
586         $maximagesize = get_config('system','maximagesize');
587
588         if(($maximagesize) && ($filesize > $maximagesize)) {
589                 notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
590                 @unlink($src);
591                 $foo = 0;
592                 call_hooks('photo_post_end',$foo);
593                 return;
594         }
595
596         logger('mod/photos.php: photos_post(): loading the contents of ' . $src , 'LOGGER_DEBUG');
597
598         $imagedata = @file_get_contents($src);
599         $ph = new Photo($imagedata);
600
601         if(! $ph->is_valid()) {
602                 logger('mod/photos.php: photos_post(): unable to process image' , 'LOGGER_DEBUG');
603                 notice( t('Unable to process image.') . EOL );
604                 @unlink($src);
605                 $foo = 0;
606                 call_hooks('photo_post_end',$foo);
607                 killme();
608         }
609
610         @unlink($src);
611
612         $width  = $ph->getWidth();
613         $height = $ph->getHeight();
614
615         $smallest = 0;
616
617         $photo_hash = photo_new_resource();
618
619         $r = $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
620
621         if(! $r) {
622                 logger('mod/photos.php: photos_post(): image store failed' , 'LOGGER_DEBUG');
623                 notice( t('Image upload failed.') . EOL );
624                 killme();
625         }
626
627         if($width > 640 || $height > 640) {
628                 $ph->scaleImage(640);
629                 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
630                 $smallest = 1;
631         }
632
633         if($width > 320 || $height > 320) {
634                 $ph->scaleImage(320);
635                 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
636                 $smallest = 2;
637         }
638         
639         $basename = basename($filename);
640         $uri = item_new_uri($a->get_hostname(), $page_owner_uid);
641
642         // Create item container
643
644         $arr = array();
645
646         $arr['uid']           = $page_owner_uid;
647         $arr['uri']           = $uri;
648         $arr['parent-uri']    = $uri;
649         $arr['type']          = 'photo';
650         $arr['wall']          = 1;
651         $arr['resource-id']   = $photo_hash;
652         $arr['contact-id']    = $owner_record['id'];
653         $arr['owner-name']    = $owner_record['name'];
654         $arr['owner-link']    = $owner_record['url'];
655         $arr['owner-avatar']  = $owner_record['thumb'];
656         $arr['author-name']   = $owner_record['name'];
657         $arr['author-link']   = $owner_record['url'];
658         $arr['author-avatar'] = $owner_record['thumb'];
659         $arr['title']         = '';
660         $arr['allow_cid']     = $str_contact_allow;
661         $arr['allow_gid']     = $str_group_allow;
662         $arr['deny_cid']      = $str_contact_deny;
663         $arr['deny_gid']      = $str_group_deny;
664         $arr['last-child']    = 1;
665         $arr['visible']       = $visible;
666         $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']' 
667                                 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]' 
668                                 . '[/url]';
669
670         $item_id = item_store($arr);
671
672         if($item_id) {
673                 q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
674                         dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
675                         intval($page_owner_uid),
676                         intval($item_id)
677                 );
678         }
679         
680         if($visible) 
681                 proc_run('php', "include/notifier.php", 'wall-new', $item_id);
682
683         call_hooks('photo_post_end',intval($item_id));
684
685         // addon uploaders should call "killme()" [e.g. exit] within the photo_post_end hook
686         // if they do not wish to be redirected
687
688         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
689         // NOTREACHED
690 }
691
692
693
694 function photos_content(&$a) {
695
696         // URLs:
697         // photos/name
698         // photos/name/upload
699         // photos/name/album/xxxxx
700         // photos/name/album/xxxxx/edit
701         // photos/name/image/xxxxx
702         // photos/name/image/xxxxx/edit
703
704
705         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
706                 notice( t('Public access denied.') . EOL);
707                 return;
708         }
709
710
711         require_once('include/bbcode.php');
712         require_once('include/security.php');
713         require_once('include/conversation.php');
714
715         if(! x($a->data,'user')) {
716                 notice( t('No photos selected') . EOL );
717                 return;
718         }
719
720         $_SESSION['photo_return'] = $a->cmd;
721
722         //
723         // Parse arguments 
724         //
725
726         if($a->argc > 3) {
727                 $datatype = $a->argv[2];
728                 $datum = $a->argv[3];
729         }
730         elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
731                 $datatype = 'upload';
732         else
733                 $datatype = 'summary';
734
735         if($a->argc > 4)
736                 $cmd = $a->argv[4];
737         else
738                 $cmd = 'view';
739
740         //
741         // Setup permissions structures
742         //
743
744         $can_post       = false;
745         $visitor        = 0;
746         $contact        = null;
747         $remote_contact = false;
748
749         $owner_uid = $a->data['user']['uid'];
750
751
752
753         $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
754
755         if((local_user()) && (local_user() == $owner_uid))
756                 $can_post = true;
757         else {
758                 if($community_page && remote_user()) {
759                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
760                                 intval(remote_user()),
761                                 intval($owner_uid)
762                         );
763                         if(count($r)) {
764                                 $can_post = true;
765                                 $contact  = $r[0];
766                                 $remote_contact = true;
767                                 $visitor = remote_user();
768                         }
769                 }
770         }
771
772         // perhaps they're visiting - but not a community page, so they wouldn't have write access
773
774         if(remote_user() && (! $visitor)) {
775                 $contact_id = $_SESSION['visitor_id'];
776                 $groups = init_groups_visitor($contact_id);
777                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
778                         intval(remote_user()),
779                         intval($owner_uid)
780                 );
781                 if(count($r)) {
782                         $contact = $r[0];
783                         $remote_contact = true;
784                 }
785         }
786
787         if(! $remote_contact) {
788                 if(local_user()) {
789                         $contact_id = $_SESSION['cid'];
790                         $contact = $a->contact;
791                 }
792         }
793
794         $r = q("SELECT `hidewall` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
795                 intval($owner_uid)
796         );
797
798         if(count($r) && $r[0]['hidewall'] && (local_user() != $owner_uid) && (! $remote_contact)) {
799                 notice( t('Access to this item is restricted.') . EOL);
800                 return;
801         }
802
803         // default permissions - anonymous user
804
805         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
806
807         // Profile owner - everything is visible
808
809         if(local_user() && (local_user() == $owner_uid)) {
810                 $sql_extra = '';        
811         }
812         elseif(remote_user()) {
813                 // authenticated visitor - here lie dragons
814                 $gs = '<<>>'; // should be impossible to match
815                 if(count($groups)) {
816                         foreach($groups as $g)
817                                 $gs .= '|<' . intval($g) . '>';
818                 } 
819                 $sql_extra = sprintf(
820                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
821                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
822                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
823                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
824
825                         intval(remote_user()),
826                         intval(remote_user()),
827                         dbesc($gs),
828                         dbesc($gs)
829                 );
830         }
831
832         //
833         // dispatch request
834         //
835
836
837         if($datatype === 'upload') {
838                 if(! ($can_post)) {
839                         notice( t('Permission denied.'));
840                         return;
841                 }
842                 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
843
844                 $albumselect .= '<option value="" selected="selected" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
845                 if(count($a->data['albums'])) {
846                         foreach($a->data['albums'] as $album) {
847                                 if(($album['album'] === '') || ($album['album'] == t('Contact Photos')))
848                                         continue;
849                                 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
850                         }
851                 }
852
853                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
854
855                 $albumselect .= '</select>';
856
857                 $uploader = '';
858
859                 $ret = array('post_url' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'],
860                                 'addon_text' => $uploader,
861                                 'default_upload' => true);
862
863
864                 call_hooks('photo_upload_form',$ret);
865
866                 $default_upload = '<input type="file" name="userfile" />        <div class="photos-upload-submit-wrapper" >
867                 <input type="submit" name="submit" value="' . t('Submit') . '" id="photos-upload-submit" /> </div>';
868
869
870  
871
872                 $tpl = get_markup_template('photos_upload.tpl');
873                 $o .= replace_macros($tpl,array(
874                         '$pagename' => t('Upload Photos'),
875                         '$sessid' => session_id(),
876                         '$nickname' => $a->data['user']['nickname'],
877                         '$newalbum' => t('New album name: '),
878                         '$existalbumtext' => t('or existing album name: '),
879                         '$albumselect' => $albumselect,
880                         '$permissions' => t('Permissions'),
881                         '$aclselect' => (($visitor) ? '' : populate_acl($a->user, $celeb)),
882                         '$uploader' => $ret['addon_text'],
883                         '$default' => (($ret['default_upload']) ? $default_upload : ''),
884                         '$uploadurl' => $ret['post_url']
885
886                 ));
887
888                 return $o; 
889         }
890
891         if($datatype === 'album') {
892
893                 $album = hex2bin($datum);
894
895                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
896                         $sql_extra GROUP BY `resource-id`",
897                         intval($owner_uid),
898                         dbesc($album)
899                 );
900                 if(count($r)) {
901                         $a->set_pager_total(count($r));
902                         $a->set_pager_itemspage(20);
903                 }
904
905                 $r = q("SELECT `resource-id`, `id`, `filename`, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
906                         $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
907                         intval($owner_uid),
908                         dbesc($album),
909                         intval($a->pager['start']),
910                         intval($a->pager['itemspage'])
911                 );
912
913                 $o .= '<h3>' . $album . '</h3>';
914                 
915                 if($cmd === 'edit') {           
916                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
917                                 if($can_post) {
918                                         $edit_tpl = get_markup_template('album_edit.tpl');
919                                         $o .= replace_macros($edit_tpl,array(
920                                                 '$nametext' => t('New album name: '),
921                                                 '$nickname' => $a->data['user']['nickname'],
922                                                 '$album' => $album,
923                                                 '$hexalbum' => bin2hex($album),
924                                                 '$submit' => t('Submit'),
925                                                 '$dropsubmit' => t('Delete Album')
926                                         ));
927                                 }
928                         }
929                 }
930                 else {
931                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
932                                 if($can_post) {
933                                         $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/' 
934                                                 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">' 
935                                                 . t('Edit Album') . '</a></div>';
936                                 }
937                         }
938                 }
939                 $tpl = get_markup_template('photo_album.tpl');
940                 if(count($r))
941                         foreach($r as $rr) {
942                                 $o .= replace_macros($tpl,array(
943                                         '$id' => $rr['id'],
944                                         '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
945                                         '$phototitle' => t('View Photo'),
946                                         '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
947                                         '$imgalt' => $rr['filename'],
948                                         '$desc'=> $rr['desc']
949                                 ));
950
951                 }
952                 $o .= '<div id="photo-album-end"></div>';
953                 $o .= paginate($a);
954
955                 return $o;
956
957         }       
958
959
960         if($datatype === 'image') {
961
962
963
964                 $o = '';
965                 // fetch image, item containing image, then comments
966
967                 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
968                         $sql_extra ORDER BY `scale` ASC ",
969                         intval($owner_uid),
970                         dbesc($datum)
971                 );
972
973                 if(! count($ph)) {
974                         notice( t('Photo not available') . EOL );
975                         return;
976                 }
977
978                 $prevlink = '';
979                 $nextlink = '';
980
981                 $prvnxt = q("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0 
982                         $sql_extra ORDER BY `created` DESC ",
983                         dbesc($ph[0]['album']),
984                         intval($owner_uid)
985                 ); 
986
987                 if(count($prvnxt)) {
988                         for($z = 0; $z < count($prvnxt); $z++) {
989                                 if($prvnxt[$z]['resource-id'] == $ph[0]['resource-id']) {
990                                         $prv = $z - 1;
991                                         $nxt = $z + 1;
992                                         if($prv < 0)
993                                                 $prv = count($prvnxt) - 1;
994                                         if($nxt >= count($prvnxt))
995                                                 $nxt = 0;
996                                         break;
997                                 }
998                         }
999                         $prevlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] ;
1000                         $nextlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] ;
1001                 }
1002
1003
1004                 if(count($ph) == 1)
1005                         $hires = $lores = $ph[0];
1006                 if(count($ph) > 1) {
1007                         if($ph[1]['scale'] == 2) {
1008                                 // original is 640 or less, we can display it directly
1009                                 $hires = $lores = $ph[0];
1010                         }
1011                         else {
1012                         $hires = $ph[0];
1013                         $lores = $ph[1];
1014                         }
1015                 }
1016
1017                 $album_link = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']);
1018                 $tools = Null;
1019                 $lock = Null;
1020  
1021                 if($can_post && ($ph[0]['uid'] == $owner_uid)) {
1022                         $tools = array(
1023                                 'edit'  => array($a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $datum . '/edit', t('Edit photo')),
1024                                 'profile'=>array($a->get_baseurl() . '/profile_photo/use/'.$ph[0]['resource-id'], t('Use as profile photo')),
1025                         );
1026
1027                         // lock
1028                         $lock = ( ( ($ph[0]['uid'] == local_user()) && (strlen($ph[0]['allow_cid']) || strlen($ph[0]['allow_gid']) 
1029                                         || strlen($ph[0]['deny_cid']) || strlen($ph[0]['deny_gid'])) ) 
1030                                         ? t('Private Message')
1031                                         : Null);
1032                         
1033                         
1034                 }
1035
1036                 if($prevlink)
1037                         $prevlink = array($prevlink, t('<< Prev')) ;
1038
1039                 $photo = array(
1040                         'href' => $a->get_baseurl() . '/photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg',
1041                         'title'=> t('View Full Size'),
1042                         'src'  => $a->get_baseurl() . '/photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg'
1043                 );
1044
1045                 if($nextlink)
1046                         $nextlink = array($nextlink, t('Next >>'));
1047
1048
1049                 // Do we have an item for this photo?
1050
1051                 $linked_items = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
1052                         dbesc($datum)
1053                 );
1054                 if(count($linked_items)) {
1055                         $link_item = $linked_items[0];
1056                         $r = q("SELECT COUNT(*) AS `total`
1057                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
1058                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
1059                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1060                                 AND `item`.`uid` = %d 
1061                                 $sql_extra ",
1062                                 dbesc($link_item['uri']),
1063                                 dbesc($link_item['uri']),
1064                                 intval($link_item['uid'])
1065
1066                         );
1067
1068                         if(count($r))
1069                                 $a->set_pager_total($r[0]['total']);
1070
1071
1072                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
1073                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`, 
1074                                 `contact`.`rel`, `contact`.`thumb`, `contact`.`self`, 
1075                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
1076                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
1077                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
1078                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1079                                 AND `item`.`uid` = %d
1080                                 $sql_extra
1081                                 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
1082                                 dbesc($link_item['uri']),
1083                                 dbesc($link_item['uri']),
1084                                 intval($link_item['uid']),
1085                                 intval($a->pager['start']),
1086                                 intval($a->pager['itemspage'])
1087
1088                         );
1089                 
1090                         if((local_user()) && (local_user() == $link_item['uid'])) {
1091                                 q("UPDATE `item` SET `unseen` = 0 WHERE `parent` = %d and `uid` = %d",
1092                                         intval($link_item['parent']),
1093                                         intval(local_user())
1094                                 );
1095                         }
1096                 }
1097
1098                 $tags=Null;
1099                 if(count($linked_items) && strlen($link_item['tag'])) {
1100                         $arr = explode(',',$link_item['tag']);
1101                         // parse tags and add links
1102                         $tag_str = '';
1103                         foreach($arr as $t) {
1104                                 if(strlen($tag_str))
1105                                         $tag_str .= ', ';
1106                                 $tag_str .= bbcode($t);
1107                         } 
1108                         $tags = array(t('Tags: '), $tag_str);
1109                         if($cmd === 'edit')
1110                                 $tags[] = $a->get_baseurl() . '/tagrm/' . $link_item['id'];
1111                                 $tags[] = t('[Remove any tag]');
1112                 }
1113
1114
1115                 $edit = Null;
1116                 if(($cmd === 'edit') && ($can_post)) {
1117                         $edit_tpl = get_markup_template('photo_edit.tpl');
1118                         $edit = replace_macros($edit_tpl, array(
1119                                 '$id' => $ph[0]['id'],
1120                                 '$album' => $ph[0]['album'],
1121                                 '$newalbum' => t('New album name'), 
1122                                 '$nickname' => $a->data['user']['nickname'],
1123                                 '$resource_id' => $ph[0]['resource-id'],
1124                                 '$capt_label' => t('Caption'),
1125                                 '$caption' => $ph[0]['desc'],
1126                                 '$tag_label' => t('Add a Tag'),
1127                                 '$tags' => $link_item['tag'],
1128                                 '$permissions' => t('Permissions'),
1129                                 '$aclselect' => populate_acl($ph[0]),
1130                                 '$help_tags' => t('Example: @bob, @Barbara_Jensen, @jim@example.com, #California, #camping'),
1131                                 '$item_id' => ((count($linked_items)) ? $link_item['id'] : 0),
1132                                 '$submit' => t('Submit'),
1133                                 '$delete' => t('Delete Photo')
1134                         ));
1135                 }
1136
1137                 if(count($linked_items)) {
1138
1139                         $cmnt_tpl = get_markup_template('comment_item.tpl');
1140                         $tpl = get_markup_template('photo_item.tpl');
1141                         $return_url = $a->cmd;
1142
1143                         $like_tpl = get_markup_template('like_noshare.tpl');
1144
1145                         $likebuttons = '';
1146
1147                         if($can_post || can_write_wall($a,$owner_uid)) {
1148                                 $likebuttons = replace_macros($like_tpl,array(
1149                                         '$id' => $link_item['id'],
1150                                         '$likethis' => t("I like this \x28toggle\x29"),
1151                                         '$nolike' => t("I don't like this \x28toggle\x29"),
1152                                         '$share' => t('Share'),
1153                                         '$wait' => t('Please wait')
1154                                 ));
1155                         }
1156
1157                         $comments = '';
1158                         if(! count($r)) {
1159                                 if($can_post || can_write_wall($a,$owner_uid)) {
1160                                         if($link_item['last-child']) {
1161                                                 $comments .= replace_macros($cmnt_tpl,array(
1162                                                         '$return_path' => '', 
1163                                                         '$jsreload' => $return_url,
1164                                                         '$type' => 'wall-comment',
1165                                                         '$id' => $link_item['id'],
1166                                                         '$parent' => $link_item['id'],
1167                                                         '$profile_uid' =>  $owner_uid,
1168                                                         '$mylink' => $contact['url'],
1169                                                         '$mytitle' => t('This is you'),
1170                                                         '$myphoto' => $contact['thumb'],
1171                                                         '$comment' => t('Comment'),
1172                                                         '$submit' => t('Submit'),
1173                                                         '$ww' => ''
1174                                                 ));
1175                                         }
1176                                 }
1177                         }
1178
1179                         $alike = array();
1180                         $dlike = array();
1181                         
1182                         $like = '';
1183                         $dislike = '';
1184
1185                         // display comments
1186                         if(count($r)) {
1187
1188                                 foreach($r as $item) {
1189                                         like_puller($a,$item,$alike,'like');
1190                                         like_puller($a,$item,$dlike,'dislike');
1191                                 }
1192
1193                                 $like    = ((isset($alike[$link_item['id']])) ? format_like($alike[$link_item['id']],$alike[$link_item['id'] . '-l'],'like',$link_item['id']) : '');
1194                                 $dislike = ((isset($dlike[$link_item['id']])) ? format_like($dlike[$link_item['id']],$dlike[$link_item['id'] . '-l'],'dislike',$link_item['id']) : '');
1195
1196
1197
1198                                 if($can_post || can_write_wall($a,$owner_uid)) {
1199                                         if($link_item['last-child']) {
1200                                                 $comments .= replace_macros($cmnt_tpl,array(
1201                                                         '$return_path' => '',
1202                                                         '$jsreload' => $return_url,
1203                                                         '$type' => 'wall-comment',
1204                                                         '$id' => $link_item['id'],
1205                                                         '$parent' => $link_item['id'],
1206                                                         '$profile_uid' =>  $owner_uid,
1207                                                         '$mylink' => $contact['url'],
1208                                                         '$mytitle' => t('This is you'),
1209                                                         '$myphoto' => $contact['thumb'],
1210                                                         '$comment' => t('Comment'),
1211                                                         '$submit' => t('Submit'),
1212                                                         '$ww' => ''
1213                                                 ));
1214                                         }
1215                                 }
1216
1217
1218                                 foreach($r as $item) {
1219                                         $comment = '';
1220                                         $template = $tpl;
1221                                         $sparkle = '';
1222
1223                                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) && ($item['id'] != $item['parent']))
1224                                                 continue;
1225
1226                                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
1227                         
1228                                         if($can_post || can_write_wall($a,$owner_uid)) {
1229
1230                                                 if($item['last-child']) {
1231                                                         $comments .= replace_macros($cmnt_tpl,array(
1232                                                                 '$return_path' => '',
1233                                                                 '$jsreload' => $return_url,
1234                                                                 '$type' => 'wall-comment',
1235                                                                 '$id' => $item['item_id'],
1236                                                                 '$parent' => $item['parent'],
1237                                                                 '$profile_uid' =>  $owner_uid,
1238                                                                 '$mylink' => $contact['url'],
1239                                                                 '$mytitle' => t('This is you'),
1240                                                                 '$myphoto' => $contact['thumb'],
1241                                                                 '$comment' => t('Comment'),
1242                                                                 '$submit' => t('Submit'),
1243                                                                 '$ww' => ''
1244                                                         ));
1245                                                 }
1246                                         }
1247
1248
1249                                         if(local_user() && ($item['contact-uid'] == local_user()) 
1250                                                 && ($item['network'] == 'dfrn') && (! $item['self'] )) {
1251                                                 $profile_url = $redirect_url;
1252                                                 $sparkle = ' sparkle';
1253                                         }
1254                                         else {
1255                                                 $profile_url = $item['url'];
1256                                                 $sparkle = '';
1257                                         }
1258  
1259                                         $diff_author = (($item['url'] !== $item['author-link']) ? true : false);
1260
1261                                         $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
1262                                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $item['thumb']);
1263
1264                                         $profile_link = $profile_url;
1265
1266                                         $drop = '';
1267
1268                                         if(($item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
1269                                                 $drop = replace_macros(get_markup_template('wall_item_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
1270
1271
1272                                         $comments .= replace_macros($template,array(
1273                                                 '$id' => $item['item_id'],
1274                                                 '$profile_url' => $profile_link,
1275                                                 '$name' => $profile_name,
1276                                                 '$thumb' => $profile_avatar,
1277                                                 '$sparkle' => $sparkle,
1278                                                 '$title' => $item['title'],
1279                                                 '$body' => bbcode($item['body']),
1280                                                 '$ago' => relative_date($item['created']),
1281                                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
1282                                                 '$drop' => $drop,
1283                                                 '$comment' => $comment
1284                                         ));
1285                                 }
1286                         }
1287
1288                         $paginate = paginate($a);
1289                 }
1290                 
1291                 $photo_tpl = get_markup_template('photo_view.tpl');
1292                 $o .= replace_macros($photo_tpl, array(
1293                         '$id' => $ph[0]['id'],
1294                         '$album' => array($album_link,$ph[0]['album']),
1295                         '$tools' => $tools,
1296                         '$lock' => $lock,
1297                         '$photo' => $photo,
1298                         '$prevlink' => $prevlink,
1299                         '$nextlink' => $nextlink,
1300                         '$desc' => $ph[0]['desc'],
1301                         '$tags' => $tags,
1302                         '$edit' => $edit,       
1303                         '$likebuttons' => $likebuttons,
1304                         '$like' => $like,
1305                         '$dislike' => $dislike,
1306                         '$comments' => $comments,
1307                         '$paginate' => $paginate,
1308                 ));
1309                 
1310                 return $o;
1311         }
1312
1313         // Default - show recent photos with upload link (if applicable)
1314         $o = '';
1315
1316         $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
1317                 $sql_extra GROUP BY `resource-id`",
1318                 intval($a->data['user']['uid']),
1319                 dbesc( t('Contact Photos'))
1320         );
1321         if(count($r)) {
1322                 $a->set_pager_total(count($r));
1323                 $a->set_pager_itemspage(20);
1324         }
1325
1326         $r = q("SELECT `resource-id`, `id`, `filename`, `album`, max(`scale`) AS `scale` FROM `photo`
1327                 WHERE `uid` = %d AND `album` != '%s' 
1328                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
1329                 intval($a->data['user']['uid']),
1330                 dbesc( t('Contact Photos')),
1331                 intval($a->pager['start']),
1332                 intval($a->pager['itemspage'])
1333         );
1334
1335         $o .= '<h3>' . t('Recent Photos') . '</h3>';
1336
1337         if($can_post) {
1338                 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/' 
1339                         . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
1340         }
1341
1342         $tpl = get_markup_template('photo_top.tpl');
1343         if(count($r)) {
1344                 foreach($r as $rr) {
1345                         $o .= replace_macros($tpl,array(
1346                                 '$id'         => $rr['id'],
1347                                 '$photolink'  => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
1348                                 '$phototitle' => t('View Photo'),
1349                                 '$imgsrc'     => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.jpg',
1350                                 '$albumlink'  => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
1351                                 '$albumname'  => $rr['album'],
1352                                 '$albumalt'   => t('View Album'),
1353                                 '$imgalt'     => $rr['filename']
1354                         ));
1355
1356                 }
1357                 $o .= '<div id="photo-top-end"></div>';
1358         }
1359         $o .= paginate($a);
1360         return $o;
1361 }
1362