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