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