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