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