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