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