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