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