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