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