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