]> git.mxchange.org Git - friendica.git/blob - mod/photos.php
Change logic in admin.php to load plugin settings form. Add statusnet settings form.
[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                                                 q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
515                                                         dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
516                                                         intval($page_owner_uid),
517                                                         intval($item_id)
518                                                 );
519
520                                                 proc_run('php',"include/notifier.php","tag","$item_id");
521                                         }
522                                 }
523
524                         }
525
526                 }
527                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
528                 return; // NOTREACHED
529         }
530
531
532         /**
533          * default post action - upload a photo
534          */
535
536         call_hooks('photo_post_init', $_POST);
537
538         /**
539          * Determine the album to use
540          */
541
542         $album    = notags(trim($_REQUEST['album']));
543         $newalbum = notags(trim($_REQUEST['newalbum']));
544
545         logger('mod/photos.php: photos_post(): album= ' . $album . ' newalbum= ' . $newalbum , 'LOGGER_DEBUG');
546
547         if(! strlen($album)) {
548                 if(strlen($newalbum))
549                         $album = $newalbum;
550                 else
551                         $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
552         }
553
554         /**
555          *
556          * We create a wall item for every photo, but we don't want to
557          * overwhelm the data stream with a hundred newly uploaded photos.
558          * So we will make the first photo uploaded to this album in the last several hours
559          * visible by default, the rest will become visible over time when and if
560          * they acquire comments, likes, dislikes, and/or tags 
561          *
562          */
563
564         $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR ",
565                 dbesc($album),
566                 intval($page_owner_uid)
567         );
568         if((! count($r)) || ($album == t('Profile Photos')))
569                 $visible = 1;
570         else
571                 $visible = 0;
572
573         $str_group_allow   = perms2str(((is_array($_REQUEST['group_allow']))   ? $_REQUEST['group_allow']   : explode(',',$_REQUEST['group_allow'])));
574         $str_contact_allow = perms2str(((is_array($_REQUEST['contact_allow'])) ? $_REQUEST['contact_allow'] : explode(',',$_REQUEST['contact_allow'])));
575         $str_group_deny    = perms2str(((is_array($_REQUEST['group_deny']))    ? $_REQUEST['group_deny']    : explode(',',$_REQUEST['group_deny'])));
576         $str_contact_deny  = perms2str(((is_array($_REQUEST['contact_deny']))  ? $_REQUEST['contact_deny']  : explode(',',$_REQUEST['contact_deny'])));
577
578         $ret = array('src' => '', 'filename' => '', 'filesize' => 0);
579
580         call_hooks('photo_post_file',$ret);
581
582         if(x($ret,'src') && x($ret,'filesize')) {
583                 $src      = $ret['src'];
584                 $filename = $ret['filename'];
585                 $filesize = $ret['filesize'];
586         }
587         else {
588                 $src        = $_FILES['userfile']['tmp_name'];
589                 $filename   = basename($_FILES['userfile']['name']);
590                 $filesize   = intval($_FILES['userfile']['size']);
591         }
592
593         $maximagesize = get_config('system','maximagesize');
594
595         if(($maximagesize) && ($filesize > $maximagesize)) {
596                 notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
597                 @unlink($src);
598                 $foo = 0;
599                 call_hooks('photo_post_end',$foo);
600                 return;
601         }
602
603         logger('mod/photos.php: photos_post(): loading the contents of ' . $src , 'LOGGER_DEBUG');
604
605         $imagedata = @file_get_contents($src);
606         $ph = new Photo($imagedata);
607
608         if(! $ph->is_valid()) {
609                 logger('mod/photos.php: photos_post(): unable to process image' , 'LOGGER_DEBUG');
610                 notice( t('Unable to process image.') . EOL );
611                 @unlink($src);
612                 $foo = 0;
613                 call_hooks('photo_post_end',$foo);
614                 killme();
615         }
616
617         @unlink($src);
618
619         $width  = $ph->getWidth();
620         $height = $ph->getHeight();
621
622         $smallest = 0;
623
624         $photo_hash = photo_new_resource();
625
626         $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);
627
628         if(! $r) {
629                 logger('mod/photos.php: photos_post(): image store failed' , 'LOGGER_DEBUG');
630                 notice( t('Image upload failed.') . EOL );
631                 killme();
632         }
633
634         if($width > 640 || $height > 640) {
635                 $ph->scaleImage(640);
636                 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
637                 $smallest = 1;
638         }
639
640         if($width > 320 || $height > 320) {
641                 $ph->scaleImage(320);
642                 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
643                 $smallest = 2;
644         }
645         
646         $basename = basename($filename);
647         $uri = item_new_uri($a->get_hostname(), $page_owner_uid);
648
649         // Create item container
650
651         $arr = array();
652
653         $arr['uid']           = $page_owner_uid;
654         $arr['uri']           = $uri;
655         $arr['parent-uri']    = $uri;
656         $arr['type']          = 'photo';
657         $arr['wall']          = 1;
658         $arr['resource-id']   = $photo_hash;
659         $arr['contact-id']    = $owner_record['id'];
660         $arr['owner-name']    = $owner_record['name'];
661         $arr['owner-link']    = $owner_record['url'];
662         $arr['owner-avatar']  = $owner_record['thumb'];
663         $arr['author-name']   = $owner_record['name'];
664         $arr['author-link']   = $owner_record['url'];
665         $arr['author-avatar'] = $owner_record['thumb'];
666         $arr['title']         = '';
667         $arr['allow_cid']     = $str_contact_allow;
668         $arr['allow_gid']     = $str_group_allow;
669         $arr['deny_cid']      = $str_contact_deny;
670         $arr['deny_gid']      = $str_group_deny;
671         $arr['last-child']    = 1;
672         $arr['visible']       = $visible;
673         $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']' 
674                                 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]' 
675                                 . '[/url]';
676
677         $item_id = item_store($arr);
678
679         if($item_id) {
680                 q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
681                         dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
682                         intval($page_owner_uid),
683                         intval($item_id)
684                 );
685         }
686         
687         if($visible) 
688                 proc_run('php', "include/notifier.php", 'wall-new', $item_id);
689
690         call_hooks('photo_post_end',intval($item_id));
691
692         // addon uploaders should call "killme()" [e.g. exit] within the photo_post_end hook
693         // if they do not wish to be redirected
694
695         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
696         // NOTREACHED
697 }
698
699
700
701 function photos_content(&$a) {
702
703         // URLs:
704         // photos/name
705         // photos/name/upload
706         // photos/name/album/xxxxx
707         // photos/name/album/xxxxx/edit
708         // photos/name/image/xxxxx
709         // photos/name/image/xxxxx/edit
710
711
712         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
713                 notice( t('Public access denied.') . EOL);
714                 return;
715         }
716
717
718         require_once('include/bbcode.php');
719         require_once('include/security.php');
720         require_once('include/conversation.php');
721
722         if(! x($a->data,'user')) {
723                 notice( t('No photos selected') . EOL );
724                 return;
725         }
726
727         $_SESSION['photo_return'] = $a->cmd;
728
729         //
730         // Parse arguments 
731         //
732
733         if($a->argc > 3) {
734                 $datatype = $a->argv[2];
735                 $datum = $a->argv[3];
736         }
737         elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
738                 $datatype = 'upload';
739         else
740                 $datatype = 'summary';
741
742         if($a->argc > 4)
743                 $cmd = $a->argv[4];
744         else
745                 $cmd = 'view';
746
747         //
748         // Setup permissions structures
749         //
750
751         $can_post       = false;
752         $visitor        = 0;
753         $contact        = null;
754         $remote_contact = false;
755
756         $owner_uid = $a->data['user']['uid'];
757
758
759
760         $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
761
762         if((local_user()) && (local_user() == $owner_uid))
763                 $can_post = true;
764         else {
765                 if($community_page && remote_user()) {
766                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
767                                 intval(remote_user()),
768                                 intval($owner_uid)
769                         );
770                         if(count($r)) {
771                                 $can_post = true;
772                                 $contact  = $r[0];
773                                 $remote_contact = true;
774                                 $visitor = remote_user();
775                         }
776                 }
777         }
778
779         // perhaps they're visiting - but not a community page, so they wouldn't have write access
780
781         if(remote_user() && (! $visitor)) {
782                 $contact_id = $_SESSION['visitor_id'];
783                 $groups = init_groups_visitor($contact_id);
784                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
785                         intval(remote_user()),
786                         intval($owner_uid)
787                 );
788                 if(count($r)) {
789                         $contact = $r[0];
790                         $remote_contact = true;
791                 }
792         }
793
794         if(! $remote_contact) {
795                 if(local_user()) {
796                         $contact_id = $_SESSION['cid'];
797                         $contact = $a->contact;
798                 }
799         }
800
801         $r = q("SELECT `hidewall` FROM `profile` WHERE `uid` = %d AND `is-default` = 1 LIMIT 1",
802                 intval($owner_uid)
803         );
804
805         if(count($r) && $r[0]['hidewall'] && (local_user() != $owner_uid) && (! $remote_contact)) {
806                 notice( t('Access to this item is restricted.') . EOL);
807                 return;
808         }
809
810         // default permissions - anonymous user
811
812         $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
813
814         // Profile owner - everything is visible
815
816         if(local_user() && (local_user() == $owner_uid)) {
817                 $sql_extra = '';        
818         }
819         elseif(remote_user()) {
820                 // authenticated visitor - here lie dragons
821                 $gs = '<<>>'; // should be impossible to match
822                 if(count($groups)) {
823                         foreach($groups as $g)
824                                 $gs .= '|<' . intval($g) . '>';
825                 } 
826                 $sql_extra = sprintf(
827                         " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' ) 
828                           AND ( `deny_cid`  = '' OR  NOT `deny_cid` REGEXP '<%d>' ) 
829                           AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
830                           AND ( `deny_gid`  = '' OR NOT `deny_gid` REGEXP '%s') ",
831
832                         intval(remote_user()),
833                         intval(remote_user()),
834                         dbesc($gs),
835                         dbesc($gs)
836                 );
837         }
838
839         //
840         // dispatch request
841         //
842
843
844         if($datatype === 'upload') {
845                 if(! ($can_post)) {
846                         notice( t('Permission denied.'));
847                         return;
848                 }
849                 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
850
851                 $albumselect .= '<option value="" selected="selected" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
852                 if(count($a->data['albums'])) {
853                         foreach($a->data['albums'] as $album) {
854                                 if(($album['album'] === '') || ($album['album'] == t('Contact Photos')))
855                                         continue;
856                                 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
857                         }
858                 }
859
860                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
861
862                 $albumselect .= '</select>';
863
864                 $uploader = '';
865
866                 $ret = array('post_url' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'],
867                                 'addon_text' => $uploader,
868                                 'default_upload' => true);
869
870
871                 call_hooks('photo_upload_form',$ret);
872
873                 $default_upload = '<input type="file" name="userfile" />        <div class="photos-upload-submit-wrapper" >
874                 <input type="submit" name="submit" value="' . t('Submit') . '" id="photos-upload-submit" /> </div>';
875
876
877  
878
879                 $tpl = get_markup_template('photos_upload.tpl');
880                 $o .= replace_macros($tpl,array(
881                         '$pagename' => t('Upload Photos'),
882                         '$sessid' => session_id(),
883                         '$nickname' => $a->data['user']['nickname'],
884                         '$newalbum' => t('New album name: '),
885                         '$existalbumtext' => t('or existing album name: '),
886                         '$albumselect' => $albumselect,
887                         '$permissions' => t('Permissions'),
888                         '$aclselect' => (($visitor) ? '' : populate_acl($a->user, $celeb)),
889                         '$uploader' => $ret['addon_text'],
890                         '$default' => (($ret['default_upload']) ? $default_upload : ''),
891                         '$uploadurl' => $ret['post_url']
892
893                 ));
894
895                 return $o; 
896         }
897
898         if($datatype === 'album') {
899
900                 $album = hex2bin($datum);
901
902                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
903                         $sql_extra GROUP BY `resource-id`",
904                         intval($owner_uid),
905                         dbesc($album)
906                 );
907                 if(count($r)) {
908                         $a->set_pager_total(count($r));
909                         $a->set_pager_itemspage(20);
910                 }
911
912                 $r = q("SELECT `resource-id`, `id`, `filename`, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
913                         $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
914                         intval($owner_uid),
915                         dbesc($album),
916                         intval($a->pager['start']),
917                         intval($a->pager['itemspage'])
918                 );
919
920                 $o .= '<h3>' . $album . '</h3>';
921                 
922                 if($cmd === 'edit') {           
923                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
924                                 if($can_post) {
925                                         $edit_tpl = get_markup_template('album_edit.tpl');
926                                         $o .= replace_macros($edit_tpl,array(
927                                                 '$nametext' => t('New album name: '),
928                                                 '$nickname' => $a->data['user']['nickname'],
929                                                 '$album' => $album,
930                                                 '$hexalbum' => bin2hex($album),
931                                                 '$submit' => t('Submit'),
932                                                 '$dropsubmit' => t('Delete Album')
933                                         ));
934                                 }
935                         }
936                 }
937                 else {
938                         if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
939                                 if($can_post) {
940                                         $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/' 
941                                                 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">' 
942                                                 . t('Edit Album') . '</a></div>';
943                                 }
944                         }
945                 }
946                 $tpl = get_markup_template('photo_album.tpl');
947                 if(count($r))
948                         foreach($r as $rr) {
949                                 $o .= replace_macros($tpl,array(
950                                         '$id' => $rr['id'],
951                                         '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
952                                         '$phototitle' => t('View Photo'),
953                                         '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
954                                         '$imgalt' => $rr['filename'],
955                                         '$desc'=> $rr['desc']
956                                 ));
957
958                 }
959                 $o .= '<div id="photo-album-end"></div>';
960                 $o .= paginate($a);
961
962                 return $o;
963
964         }       
965
966
967         if($datatype === 'image') {
968
969
970
971                 $o = '';
972                 // fetch image, item containing image, then comments
973
974                 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
975                         $sql_extra ORDER BY `scale` ASC ",
976                         intval($owner_uid),
977                         dbesc($datum)
978                 );
979
980                 if(! count($ph)) {
981                         notice( t('Photo not available') . EOL );
982                         return;
983                 }
984
985                 $prevlink = '';
986                 $nextlink = '';
987
988                 $prvnxt = q("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0 
989                         $sql_extra ORDER BY `created` DESC ",
990                         dbesc($ph[0]['album']),
991                         intval($owner_uid)
992                 ); 
993
994                 if(count($prvnxt)) {
995                         for($z = 0; $z < count($prvnxt); $z++) {
996                                 if($prvnxt[$z]['resource-id'] == $ph[0]['resource-id']) {
997                                         $prv = $z - 1;
998                                         $nxt = $z + 1;
999                                         if($prv < 0)
1000                                                 $prv = count($prvnxt) - 1;
1001                                         if($nxt >= count($prvnxt))
1002                                                 $nxt = 0;
1003                                         break;
1004                                 }
1005                         }
1006                         $prevlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] ;
1007                         $nextlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] ;
1008                 }
1009
1010
1011                 if(count($ph) == 1)
1012                         $hires = $lores = $ph[0];
1013                 if(count($ph) > 1) {
1014                         if($ph[1]['scale'] == 2) {
1015                                 // original is 640 or less, we can display it directly
1016                                 $hires = $lores = $ph[0];
1017                         }
1018                         else {
1019                         $hires = $ph[0];
1020                         $lores = $ph[1];
1021                         }
1022                 }
1023
1024                 $album_link = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']);
1025                 $tools = Null;
1026                 $lock = Null;
1027  
1028                 if($can_post && ($ph[0]['uid'] == $owner_uid)) {
1029                         $tools = array(
1030                                 'edit'  => array($a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $datum . '/edit', t('Edit photo')),
1031                                 'profile'=>array($a->get_baseurl() . '/profile_photo/use/'.$ph[0]['resource-id'], t('Use as profile photo')),
1032                         );
1033
1034                         // lock
1035                         $lock = ( ( ($ph[0]['uid'] == local_user()) && (strlen($ph[0]['allow_cid']) || strlen($ph[0]['allow_gid']) 
1036                                         || strlen($ph[0]['deny_cid']) || strlen($ph[0]['deny_gid'])) ) 
1037                                         ? t('Private Message')
1038                                         : Null);
1039                         
1040                         
1041                 }
1042
1043                 if($prevlink)
1044                         $prevlink = array($prevlink, '<div class="icon prev"></div>') ;
1045
1046                 $photo = array(
1047                         'href' => $a->get_baseurl() . '/photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg',
1048                         'title'=> t('View Full Size'),
1049                         'src'  => $a->get_baseurl() . '/photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg'
1050                 );
1051
1052                 if($nextlink)
1053                         $nextlink = array($nextlink, '<div class="icon next"></div>');
1054
1055
1056                 // Do we have an item for this photo?
1057
1058                 $linked_items = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
1059                         dbesc($datum)
1060                 );
1061                 if(count($linked_items)) {
1062                         $link_item = $linked_items[0];
1063                         $r = q("SELECT COUNT(*) AS `total`
1064                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
1065                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
1066                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1067                                 AND `item`.`uid` = %d 
1068                                 $sql_extra ",
1069                                 dbesc($link_item['uri']),
1070                                 dbesc($link_item['uri']),
1071                                 intval($link_item['uid'])
1072
1073                         );
1074
1075                         if(count($r))
1076                                 $a->set_pager_total($r[0]['total']);
1077
1078
1079                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
1080                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`, 
1081                                 `contact`.`rel`, `contact`.`thumb`, `contact`.`self`, 
1082                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
1083                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
1084                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
1085                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1086                                 AND `item`.`uid` = %d
1087                                 $sql_extra
1088                                 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
1089                                 dbesc($link_item['uri']),
1090                                 dbesc($link_item['uri']),
1091                                 intval($link_item['uid']),
1092                                 intval($a->pager['start']),
1093                                 intval($a->pager['itemspage'])
1094
1095                         );
1096                 
1097                         if((local_user()) && (local_user() == $link_item['uid'])) {
1098                                 q("UPDATE `item` SET `unseen` = 0 WHERE `parent` = %d and `uid` = %d",
1099                                         intval($link_item['parent']),
1100                                         intval(local_user())
1101                                 );
1102                         }
1103                 }
1104
1105                 $tags=Null;
1106                 if(count($linked_items) && strlen($link_item['tag'])) {
1107                         $arr = explode(',',$link_item['tag']);
1108                         // parse tags and add links
1109                         $tag_str = '';
1110                         foreach($arr as $t) {
1111                                 if(strlen($tag_str))
1112                                         $tag_str .= ', ';
1113                                 $tag_str .= bbcode($t);
1114                         } 
1115                         $tags = array(t('Tags: '), $tag_str);
1116                         if($cmd === 'edit') {
1117                                 $tags[] = $a->get_baseurl() . '/tagrm/' . $link_item['id'];
1118                                 $tags[] = t('[Remove any tag]');
1119                         }
1120                 }
1121
1122
1123                 $edit = Null;
1124                 if(($cmd === 'edit') && ($can_post)) {
1125                         $edit_tpl = get_markup_template('photo_edit.tpl');
1126                         $edit = replace_macros($edit_tpl, array(
1127                                 '$id' => $ph[0]['id'],
1128                                 '$album' => $ph[0]['album'],
1129                                 '$newalbum' => t('New album name'), 
1130                                 '$nickname' => $a->data['user']['nickname'],
1131                                 '$resource_id' => $ph[0]['resource-id'],
1132                                 '$capt_label' => t('Caption'),
1133                                 '$caption' => $ph[0]['desc'],
1134                                 '$tag_label' => t('Add a Tag'),
1135                                 '$tags' => $link_item['tag'],
1136                                 '$permissions' => t('Permissions'),
1137                                 '$aclselect' => populate_acl($ph[0]),
1138                                 '$help_tags' => t('Example: @bob, @Barbara_Jensen, @jim@example.com, #California, #camping'),
1139                                 '$item_id' => ((count($linked_items)) ? $link_item['id'] : 0),
1140                                 '$submit' => t('Submit'),
1141                                 '$delete' => t('Delete Photo')
1142                         ));
1143                 }
1144
1145                 if(count($linked_items)) {
1146
1147                         $cmnt_tpl = get_markup_template('comment_item.tpl');
1148                         $tpl = get_markup_template('photo_item.tpl');
1149                         $return_url = $a->cmd;
1150
1151                         $like_tpl = get_markup_template('like_noshare.tpl');
1152
1153                         $likebuttons = '';
1154
1155                         if($can_post || can_write_wall($a,$owner_uid)) {
1156                                 $likebuttons = replace_macros($like_tpl,array(
1157                                         '$id' => $link_item['id'],
1158                                         '$likethis' => t("I like this \x28toggle\x29"),
1159                                         '$nolike' => t("I don't like this \x28toggle\x29"),
1160                                         '$share' => t('Share'),
1161                                         '$wait' => t('Please wait')
1162                                 ));
1163                         }
1164
1165                         $comments = '';
1166                         if(! count($r)) {
1167                                 if($can_post || can_write_wall($a,$owner_uid)) {
1168                                         if($link_item['last-child']) {
1169                                                 $comments .= replace_macros($cmnt_tpl,array(
1170                                                         '$return_path' => '', 
1171                                                         '$jsreload' => $return_url,
1172                                                         '$type' => 'wall-comment',
1173                                                         '$id' => $link_item['id'],
1174                                                         '$parent' => $link_item['id'],
1175                                                         '$profile_uid' =>  $owner_uid,
1176                                                         '$mylink' => $contact['url'],
1177                                                         '$mytitle' => t('This is you'),
1178                                                         '$myphoto' => $contact['thumb'],
1179                                                         '$comment' => t('Comment'),
1180                                                         '$submit' => t('Submit'),
1181                                                         '$ww' => ''
1182                                                 ));
1183                                         }
1184                                 }
1185                         }
1186
1187                         $alike = array();
1188                         $dlike = array();
1189                         
1190                         $like = '';
1191                         $dislike = '';
1192
1193                         // display comments
1194                         if(count($r)) {
1195
1196                                 foreach($r as $item) {
1197                                         like_puller($a,$item,$alike,'like');
1198                                         like_puller($a,$item,$dlike,'dislike');
1199                                 }
1200
1201                                 $like    = ((isset($alike[$link_item['id']])) ? format_like($alike[$link_item['id']],$alike[$link_item['id'] . '-l'],'like',$link_item['id']) : '');
1202                                 $dislike = ((isset($dlike[$link_item['id']])) ? format_like($dlike[$link_item['id']],$dlike[$link_item['id'] . '-l'],'dislike',$link_item['id']) : '');
1203
1204
1205
1206                                 if($can_post || can_write_wall($a,$owner_uid)) {
1207                                         if($link_item['last-child']) {
1208                                                 $comments .= replace_macros($cmnt_tpl,array(
1209                                                         '$return_path' => '',
1210                                                         '$jsreload' => $return_url,
1211                                                         '$type' => 'wall-comment',
1212                                                         '$id' => $link_item['id'],
1213                                                         '$parent' => $link_item['id'],
1214                                                         '$profile_uid' =>  $owner_uid,
1215                                                         '$mylink' => $contact['url'],
1216                                                         '$mytitle' => t('This is you'),
1217                                                         '$myphoto' => $contact['thumb'],
1218                                                         '$comment' => t('Comment'),
1219                                                         '$submit' => t('Submit'),
1220                                                         '$ww' => ''
1221                                                 ));
1222                                         }
1223                                 }
1224
1225
1226                                 foreach($r as $item) {
1227                                         $comment = '';
1228                                         $template = $tpl;
1229                                         $sparkle = '';
1230
1231                                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) && ($item['id'] != $item['parent']))
1232                                                 continue;
1233
1234                                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
1235                         
1236                                         if($can_post || can_write_wall($a,$owner_uid)) {
1237
1238                                                 if($item['last-child']) {
1239                                                         $comments .= replace_macros($cmnt_tpl,array(
1240                                                                 '$return_path' => '',
1241                                                                 '$jsreload' => $return_url,
1242                                                                 '$type' => 'wall-comment',
1243                                                                 '$id' => $item['item_id'],
1244                                                                 '$parent' => $item['parent'],
1245                                                                 '$profile_uid' =>  $owner_uid,
1246                                                                 '$mylink' => $contact['url'],
1247                                                                 '$mytitle' => t('This is you'),
1248                                                                 '$myphoto' => $contact['thumb'],
1249                                                                 '$comment' => t('Comment'),
1250                                                                 '$submit' => t('Submit'),
1251                                                                 '$ww' => ''
1252                                                         ));
1253                                                 }
1254                                         }
1255
1256
1257                                         if(local_user() && ($item['contact-uid'] == local_user()) 
1258                                                 && ($item['network'] == 'dfrn') && (! $item['self'] )) {
1259                                                 $profile_url = $redirect_url;
1260                                                 $sparkle = ' sparkle';
1261                                         }
1262                                         else {
1263                                                 $profile_url = $item['url'];
1264                                                 $sparkle = '';
1265                                         }
1266  
1267                                         $diff_author = (($item['url'] !== $item['author-link']) ? true : false);
1268
1269                                         $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
1270                                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $item['thumb']);
1271
1272                                         $profile_link = $profile_url;
1273
1274                                         $drop = '';
1275
1276                                         if(($item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
1277                                                 $drop = replace_macros(get_markup_template('photo_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
1278
1279
1280                                         $comments .= replace_macros($template,array(
1281                                                 '$id' => $item['item_id'],
1282                                                 '$profile_url' => $profile_link,
1283                                                 '$name' => $profile_name,
1284                                                 '$thumb' => $profile_avatar,
1285                                                 '$sparkle' => $sparkle,
1286                                                 '$title' => $item['title'],
1287                                                 '$body' => bbcode($item['body']),
1288                                                 '$ago' => relative_date($item['created']),
1289                                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
1290                                                 '$drop' => $drop,
1291                                                 '$comment' => $comment
1292                                         ));
1293                                 }
1294                         }
1295
1296                         $paginate = paginate($a);
1297                 }
1298                 
1299                 $photo_tpl = get_markup_template('photo_view.tpl');
1300                 $o .= replace_macros($photo_tpl, array(
1301                         '$id' => $ph[0]['id'],
1302                         '$album' => array($album_link,$ph[0]['album']),
1303                         '$tools' => $tools,
1304                         '$lock' => $lock,
1305                         '$photo' => $photo,
1306                         '$prevlink' => $prevlink,
1307                         '$nextlink' => $nextlink,
1308                         '$desc' => $ph[0]['desc'],
1309                         '$tags' => $tags,
1310                         '$edit' => $edit,       
1311                         '$likebuttons' => $likebuttons,
1312                         '$like' => $like,
1313                         '$dislike' => $dislike,
1314                         '$comments' => $comments,
1315                         '$paginate' => $paginate,
1316                 ));
1317                 
1318                 return $o;
1319         }
1320
1321         // Default - show recent photos with upload link (if applicable)
1322         $o = '';
1323
1324         $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' 
1325                 $sql_extra GROUP BY `resource-id`",
1326                 intval($a->data['user']['uid']),
1327                 dbesc( t('Contact Photos'))
1328         );
1329         if(count($r)) {
1330                 $a->set_pager_total(count($r));
1331                 $a->set_pager_itemspage(20);
1332         }
1333
1334         $r = q("SELECT `resource-id`, `id`, `filename`, `album`, max(`scale`) AS `scale` FROM `photo`
1335                 WHERE `uid` = %d AND `album` != '%s' 
1336                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
1337                 intval($a->data['user']['uid']),
1338                 dbesc( t('Contact Photos')),
1339                 intval($a->pager['start']),
1340                 intval($a->pager['itemspage'])
1341         );
1342
1343         $o .= '<h3>' . t('Recent Photos') . '</h3>';
1344
1345         if($can_post) {
1346                 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/' 
1347                         . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
1348         }
1349
1350         $tpl = get_markup_template('photo_top.tpl');
1351         if(count($r)) {
1352                 foreach($r as $rr) {
1353                         $o .= replace_macros($tpl,array(
1354                                 '$id'         => $rr['id'],
1355                                 '$photolink'  => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
1356                                 '$phototitle' => t('View Photo'),
1357                                 '$imgsrc'     => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.jpg',
1358                                 '$albumlink'  => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
1359                                 '$albumname'  => $rr['album'],
1360                                 '$albumalt'   => t('View Album'),
1361                                 '$imgalt'     => $rr['filename']
1362                         ));
1363
1364                 }
1365                 $o .= '<div id="photo-top-end"></div>';
1366         }
1367         $o .= paginate($a);
1368         return $o;
1369 }
1370