]> git.mxchange.org Git - friendica.git/blob - mod/photos.php
c6892818afbff847015223546a27e345165faeee
[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 require_once('include/security.php');
7 require_once('include/redir.php');
8
9
10 function photos_init(&$a) {
11
12         if($a->argc > 1)
13                 auto_redir($a, $a->argv[1]);
14
15         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
16                 return;
17         }
18
19         $o = '';
20
21         if($a->argc > 1) {
22                 $nick = $a->argv[1];
23                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
24                         dbesc($nick)
25                 );
26
27                 if(! count($r))
28                         return;
29
30                 $a->data['user'] = $r[0];
31
32                 $o .= '<div class="vcard">';
33                 $o .= '<div class="fn">' . $a->data['user']['username'] . '</div>';
34                 $o .= '<div id="profile-photo-wrapper"><img class="photo" style="width: 175px; height: 175px;" src="' . $a->get_cached_avatar_image($a->get_baseurl() . '/photo/profile/' . $a->data['user']['uid'] . '.jpg') . '" alt="' . $a->data['user']['username'] . '" /></div>';
35                 $o .= '</div>';
36
37
38                 $sql_extra = permissions_sql($a->data['user']['uid']);
39
40                 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d $sql_extra order by created desc",
41                         intval($a->data['user']['uid'])
42                 );
43
44                 if(count($albums)) {
45                         $a->data['albums'] = $albums;
46
47                         $albums_visible = ((intval($a->data['user']['hidewall']) && (! local_user()) && (! remote_user())) ? false : true);     
48
49                         if($albums_visible) {
50                                 $o .= '<div id="side-bar-photos-albums" class="widget">';
51                                 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h3>';
52                                         
53                                 $o .= '<ul>';
54                                 foreach($albums as $album) {
55
56                                         // don't show contact photos. We once translated this name, but then you could still access it under
57                                         // a different language setting. Now we store the name in English and check in English (and translated for legacy albums).
58
59                                         if((! strlen($album['album'])) || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
60                                                 continue;
61                                         $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" >' . $album['album'] . '</a></li>'; 
62                                 }
63                                 $o .= '</ul>';
64                         }
65                         if(local_user() && $a->data['user']['uid'] == local_user()) {
66                                 $o .= '<div id="photo-albums-upload-link"><a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/upload" >' .t('Upload New Photos') . '</a></div>';
67                         }
68
69                         $o .= '</div>';
70                 }
71
72                 if(! x($a->page,'aside'))
73                         $a->page['aside'] = '';
74                 $a->page['aside'] .= $o;
75
76
77                 $tpl = get_markup_template("photos_head.tpl");
78                 $a->page['htmlhead'] .= replace_macros($tpl,array(
79                         '$ispublic' => t('everybody')
80                 ));
81
82         }
83
84         return;
85 }
86
87
88
89 function photos_post(&$a) {
90
91         logger('mod-photos: photos_post: begin' , LOGGER_DEBUG);
92
93
94         logger('mod_photos: REQUEST ' . print_r($_REQUEST,true), LOGGER_DATA);
95         logger('mod_photos: FILES '   . print_r($_FILES,true), LOGGER_DATA);
96
97         $phototypes = Photo::supportedTypes();
98
99         $can_post  = false;
100         $visitor   = 0;
101
102         $page_owner_uid = $a->data['user']['uid'];
103         $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
104
105         if((local_user()) && (local_user() == $page_owner_uid))
106                 $can_post = true;
107         else {
108                 if($community_page && remote_user()) {
109                         $cid = 0;
110                         if(is_array($_SESSION['remote'])) {
111                                 foreach($_SESSION['remote'] as $v) {
112                                         if($v['uid'] == $page_owner_uid) {
113                                                 $cid = $v['cid'];
114                                                 break;
115                                         }
116                                 }
117                         }
118                         if($cid) {
119
120                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
121                                         intval($cid),
122                                         intval($page_owner_uid)
123                                 );
124                                 if(count($r)) {
125                                         $can_post = true;
126                                         $visitor = $cid;
127                                 }
128                         }
129                 }
130         }
131
132         if(! $can_post) {
133                 notice( t('Permission denied.') . EOL );
134                 killme();
135         }
136
137         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
138                 WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
139                 intval($page_owner_uid)
140         );
141
142         if(! count($r)) {
143                 notice( t('Contact information unavailable') . EOL);
144                 logger('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
145                 killme();
146         }
147
148         $owner_record = $r[0];  
149
150
151         if(($a->argc > 3) && ($a->argv[2] === 'album')) {
152                 $album = hex2bin($a->argv[3]);
153
154                 if($album === t('Profile Photos') || $album === 'Contact Photos' || $album === t('Contact Photos')) {
155                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
156                         return; // NOTREACHED
157                 }
158
159                 $r = q("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
160                         dbesc($album),
161                         intval($page_owner_uid)
162                 );
163                 if(! count($r)) {
164                         notice( t('Album not found.') . EOL);
165                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
166                         return; // NOTREACHED
167                 }
168
169                 // Check if the user has responded to a delete confirmation query
170                 if($_REQUEST['canceled']) {
171                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
172                 }
173
174                 $newalbum = notags(trim($_POST['albumname']));
175                 if($newalbum != $album) {
176                         q("UPDATE `photo` SET `album` = '%s' WHERE `album` = '%s' AND `uid` = %d",
177                                 dbesc($newalbum),
178                                 dbesc($album),
179                                 intval($page_owner_uid)
180                         );
181                         $newurl = str_replace(bin2hex($album),bin2hex($newalbum),$_SESSION['photo_return']);
182                         goaway($a->get_baseurl() . '/' . $newurl);
183                         return; // NOTREACHED
184                 }
185
186
187                 if($_POST['dropalbum'] == t('Delete Album')) {
188
189                         // Check if we should do HTML-based delete confirmation
190                         if($_REQUEST['confirm']) {
191                                 $drop_url = $a->query_string;
192                                 $extra_inputs = array(
193                                         array('name' => 'albumname', 'value' => $_POST['albumname']),
194                                 );
195                                 $a->page['content'] = replace_macros(get_markup_template('confirm.tpl'), array(
196                                         '$method' => 'post',
197                                         '$message' => t('Do you really want to delete this photo album and all its photos?'),
198                                         '$extra_inputs' => $extra_inputs,
199                                         '$confirm' => t('Delete Album'),
200                                         '$confirm_url' => $drop_url,
201                                         '$confirm_name' => 'dropalbum', // Needed so that confirmation will bring us back into this if statement
202                                         '$cancel' => t('Cancel'),
203                                 ));
204                                 $a->error = 1; // Set $a->error so the other module functions don't execute
205                                 return;
206                         }
207
208                         $res = array();
209
210                         // get the list of photos we are about to delete
211
212                         if($visitor) {
213                                 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `album` = '%s'",
214                                         intval($visitor),
215                                         intval($page_owner_uid),
216                                         dbesc($album)
217                                 );
218                         }
219                         else {
220                                 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'",
221                                         intval(local_user()),
222                                         dbesc($album)
223                                 );
224                         }
225                         if(count($r)) {
226                                 foreach($r as $rr) {
227                                         $res[] = "'" . dbesc($rr['rid']) . "'" ;
228                                 }
229                         }
230                         else {
231                                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
232                                 return; // NOTREACHED
233                         }
234
235                         $str_res = implode(',', $res);
236
237                         // remove the associated photos
238
239                         q("DELETE FROM `photo` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
240                                 intval($page_owner_uid)
241                         );
242
243                         // find and delete the corresponding item with all the comments and likes/dislikes
244
245                         $r = q("SELECT `parent-uri` FROM `item` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
246                                 intval($page_owner_uid)
247                         );
248                         if(count($r)) {
249                                 foreach($r as $rr) {
250                                         q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
251                                                 dbesc(datetime_convert()),
252                                                 dbesc($rr['parent-uri']),
253                                                 intval($page_owner_uid)
254                                         );
255
256                                         $drop_id = intval($rr['id']);
257
258                                         // send the notification upstream/downstream as the case may be
259
260                                         if($rr['visible'])
261                                                 proc_run('php',"include/notifier.php","drop","$drop_id");
262                                 }
263                         }
264                 }
265                 goaway($a->get_baseurl() . '/photos/' . $a->data['user']['nickname']);
266                 return; // NOTREACHED
267         }
268
269
270         // Check if the user has responded to a delete confirmation query for a single photo
271         if(($a->argc > 2) && $_REQUEST['canceled']) {
272                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
273         }
274
275         if(($a->argc > 2) && (x($_POST,'delete')) && ($_POST['delete'] == t('Delete Photo'))) {
276
277                 // same as above but remove single photo
278
279                 // Check if we should do HTML-based delete confirmation
280                 if($_REQUEST['confirm']) {
281                         $drop_url = $a->query_string;
282                         $a->page['content'] = replace_macros(get_markup_template('confirm.tpl'), array(
283                                 '$method' => 'post',
284                                 '$message' => t('Do you really want to delete this photo?'),
285                                 '$extra_inputs' => array(),
286                                 '$confirm' => t('Delete Photo'),
287                                 '$confirm_url' => $drop_url,
288                                 '$confirm_name' => 'delete', // Needed so that confirmation will bring us back into this if statement
289                                 '$cancel' => t('Cancel'),
290                         ));
291                         $a->error = 1; // Set $a->error so the other module functions don't execute
292                         return;
293                 }
294
295                 if($visitor) {
296                         $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `resource-id` = '%s' LIMIT 1",
297                                 intval($visitor),
298                                 intval($page_owner_uid),
299                                 dbesc($a->argv[2])
300                         );
301                 }
302                 else {
303                         $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' LIMIT 1",
304                                 intval(local_user()),
305                                 dbesc($a->argv[2])
306                         );
307                 }
308                 if(count($r)) {
309                         q("DELETE FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
310                                 intval($page_owner_uid),
311                                 dbesc($r[0]['resource-id'])
312                         );
313                         $i = q("SELECT * FROM `item` WHERE `resource-id` = '%s' AND `uid` = %d LIMIT 1",
314                                 dbesc($r[0]['resource-id']),
315                                 intval($page_owner_uid)
316                         );
317                         if(count($i)) {
318                                 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
319                                         dbesc(datetime_convert()),
320                                         dbesc(datetime_convert()),
321                                         dbesc($i[0]['uri']),
322                                         intval($page_owner_uid)
323                                 );
324
325                                 $url = $a->get_baseurl();
326                                 $drop_id = intval($i[0]['id']);
327
328                                 if($i[0]['visible'])
329                                         proc_run('php',"include/notifier.php","drop","$drop_id");
330                         }
331                 }
332
333                 goaway($a->get_baseurl() . '/photos/' . $a->data['user']['nickname']);
334                 return; // NOTREACHED
335         }
336
337         if(($a->argc > 2) && ((x($_POST,'desc') !== false) || (x($_POST,'newtag') !== false)) || (x($_POST,'albname') !== false)) {
338
339
340                 $desc        = ((x($_POST,'desc'))    ? notags(trim($_POST['desc']))    : '');
341                 $rawtags     = ((x($_POST,'newtag'))  ? notags(trim($_POST['newtag']))  : '');
342                 $item_id     = ((x($_POST,'item_id')) ? intval($_POST['item_id'])       : 0);
343                 $albname     = ((x($_POST,'albname')) ? notags(trim($_POST['albname'])) : '');
344                 $str_group_allow   = perms2str($_POST['group_allow']);
345                 $str_contact_allow = perms2str($_POST['contact_allow']);
346                 $str_group_deny    = perms2str($_POST['group_deny']);
347                 $str_contact_deny  = perms2str($_POST['contact_deny']);
348
349                 $resource_id = $a->argv[2];
350
351                 if(! strlen($albname))
352                         $albname = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
353
354
355                 if((x($_POST,'rotate') !== false) && 
356                    ( (intval($_POST['rotate']) == 1) || (intval($_POST['rotate']) == 2) )) {
357                         logger('rotate');
358
359                         $r = q("select * from photo where `resource-id` = '%s' and uid = %d and scale = 0 limit 1",
360                                 dbesc($resource_id),
361                                 intval($page_owner_uid)
362                         );
363                         if(count($r)) {
364                                 $ph = new Photo($r[0]['data'], $r[0]['type']);
365                                 if($ph->is_valid()) {
366                                         $rotate_deg = ( (intval($_POST['rotate']) == 1) ? 270 : 90 );
367                                         $ph->rotate($rotate_deg);
368
369                                         $width  = $ph->getWidth();
370                                         $height = $ph->getHeight();
371
372                                         $x = q("update photo set data = '%s', height = %d, width = %d where `resource-id` = '%s' and uid = %d and scale = 0 limit 1",
373                                                 dbesc($ph->imageString()),
374                                                 intval($height),
375                                                 intval($width),
376                                                 dbesc($resource_id),
377                                                 intval($page_owner_uid)
378                                         );
379
380                                         if($width > 640 || $height > 640) {
381                                                 $ph->scaleImage(640);
382                                                 $width  = $ph->getWidth();
383                                                 $height = $ph->getHeight();
384                 
385                                                 $x = q("update photo set data = '%s', height = %d, width = %d where `resource-id` = '%s' and uid = %d and scale = 1 limit 1",
386                                                         dbesc($ph->imageString()),
387                                                         intval($height),
388                                                         intval($width),
389                                                         dbesc($resource_id),
390                                                         intval($page_owner_uid)
391                                                 );
392                                         }
393
394                                         if($width > 320 || $height > 320) {
395                                                 $ph->scaleImage(320);
396                                                 $width  = $ph->getWidth();
397                                                 $height = $ph->getHeight();
398
399                                                 $x = q("update photo set data = '%s', height = %d, width = %d where `resource-id` = '%s' and uid = %d and scale = 2 limit 1",
400                                                         dbesc($ph->imageString()),
401                                                         intval($height),
402                                                         intval($width),
403                                                         dbesc($resource_id),
404                                                         intval($page_owner_uid)
405                                                 );
406                                         }       
407                                 }
408                         }
409                 }
410
411                 $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
412                         dbesc($resource_id),
413                         intval($page_owner_uid)
414                 );
415                 if(count($p)) {
416                         $ext = $phototypes[$p[0]['type']];
417                         $r = q("UPDATE `photo` SET `desc` = '%s', `album` = '%s', `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d",
418                                 dbesc($desc),
419                                 dbesc($albname),
420                                 dbesc($str_contact_allow),
421                                 dbesc($str_group_allow),
422                                 dbesc($str_contact_deny),
423                                 dbesc($str_group_deny),
424                                 dbesc($resource_id),
425                                 intval($page_owner_uid)
426                         );
427                 }
428
429                 /* Don't make the item visible if the only change was the album name */
430
431                 $visibility = 0;
432                 if($p[0]['desc'] !== $desc || strlen($rawtags))
433                         $visibility = 1;
434                 
435                 if(! $item_id) {
436
437                         // Create item container
438
439                         $title = '';
440                         $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
441                         
442                         $arr = array();
443
444                         $arr['uid']           = $page_owner_uid;
445                         $arr['uri']           = $uri;
446                         $arr['parent-uri']    = $uri; 
447                         $arr['type']          = 'photo';
448                         $arr['wall']          = 1;
449                         $arr['resource-id']   = $p[0]['resource-id'];
450                         $arr['contact-id']    = $owner_record['id'];
451                         $arr['owner-name']    = $owner_record['name'];
452                         $arr['owner-link']    = $owner_record['url'];
453                         $arr['owner-avatar']  = $owner_record['thumb'];
454                         $arr['author-name']   = $owner_record['name'];
455                         $arr['author-link']   = $owner_record['url'];
456                         $arr['author-avatar'] = $owner_record['thumb'];
457                         $arr['title']         = $title;
458                         $arr['allow_cid']     = $p[0]['allow_cid'];
459                         $arr['allow_gid']     = $p[0]['allow_gid'];
460                         $arr['deny_cid']      = $p[0]['deny_cid'];
461                         $arr['deny_gid']      = $p[0]['deny_gid'];
462                         $arr['last-child']    = 1;
463                         $arr['visible']       = $visibility;
464                         $arr['origin']        = 1;
465                         
466                         $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']' 
467                                                 . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.'. $ext . '[/img]' 
468                                                 . '[/url]';
469                 
470                         $item_id = item_store($arr);
471
472                 }
473
474                 if($item_id) {
475                         $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
476                                 intval($item_id),
477                                 intval($page_owner_uid)
478                         );
479                 }
480                 if(count($r)) {
481                         $old_tag    = $r[0]['tag'];
482                         $old_inform = $r[0]['inform'];
483                 }
484
485                 if(strlen($rawtags)) {
486
487                         $str_tags = '';
488                         $inform   = '';
489
490                         // if the new tag doesn't have a namespace specifier (@foo or #foo) give it a hashtag
491
492                         $x = substr($rawtags,0,1);
493                         if($x !== '@' && $x !== '#')
494                                 $rawtags = '#' . $rawtags;
495
496                         $taginfo = array();
497                         $tags = get_tags($rawtags);
498
499                         if(count($tags)) {
500                                 foreach($tags as $tag) {
501                                         if(isset($profile))
502                                                 unset($profile);
503                                         if(strpos($tag,'@') === 0) {
504                                                 $name = substr($tag,1);
505                                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
506                                                         $newname = $name;
507                                                         $links = @lrdd($name);
508                                                         if(count($links)) {
509                                                                 foreach($links as $link) {
510                                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
511                                                         $profile = $link['@attributes']['href'];
512                                                                         if($link['@attributes']['rel'] === 'salmon') {
513                                                                                 $salmon = '$url:' . str_replace(',','%sc',$link['@attributes']['href']);
514                                                                                 if(strlen($inform))
515                                                                                         $inform .= ',';
516                                                         $inform .= $salmon;
517                                                                         }
518                                                                 }
519                                                         }
520                                                         $taginfo[] = array($newname,$profile,$salmon);
521                                                 }
522                                                 else {
523                                                         $newname = $name;
524                                                         $alias = '';
525                                                         $tagcid = 0;
526                                                         if(strrpos($newname,'+'))
527                                                                 $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
528
529                                                         if($tagcid) {
530                                                                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
531                                                                         intval($tagcid),
532                                                                         intval($profile_uid)
533                                                                 );
534                                                         }
535                                                         else {
536                                                                 $newname = str_replace('_',' ',$name);
537
538                                                                 //select someone from this user's contacts by name
539                                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
540                                                                                 dbesc($newname),
541                                                                                 intval($page_owner_uid)
542                                                                 );
543
544                                                                 if(! $r) {
545                                                                         //select someone by attag or nick and the name passed in
546                                                                         $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
547                                                                                         dbesc($name),
548                                                                                         dbesc($name),
549                                                                                         intval($page_owner_uid)
550                                                                         );
551                                                                 }
552                                                         }
553 /*                                                      elseif(strstr($name,'_') || strstr($name,' ')) {
554                                                                 $newname = str_replace('_',' ',$name);
555                                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
556                                                                         dbesc($newname),
557                                                                         intval($page_owner_uid)
558                                                                 );
559                                                         }
560                                                         else {
561                                                                 $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
562                                                                         dbesc($name),
563                                                                         dbesc($name),
564                                                                         intval($page_owner_uid)
565                                                                 );
566                                                         }*/
567                                                         if(count($r)) {
568                                                                 $newname = $r[0]['name'];
569                                                                 $profile = $r[0]['url'];
570                                                                 $notify = 'cid:' . $r[0]['id'];
571                                                                 if(strlen($inform))
572                                                                         $inform .= ',';
573                                                                 $inform .= $notify;
574                                                         }
575                                                 }
576                                                 if($profile) {
577                                                         if(substr($notify,0,4) === 'cid:')
578                                                                 $taginfo[] = array($newname,$profile,$notify,$r[0],'@[url=' . str_replace(',','%2c',$profile) . ']' . $newname  . '[/url]');
579                                                         else
580                                                                 $taginfo[] = array($newname,$profile,$notify,null,$str_tags .= '@[url=' . $profile . ']' . $newname     . '[/url]');
581                                                         if(strlen($str_tags))
582                                                                 $str_tags .= ',';
583                                                         $profile = str_replace(',','%2c',$profile);
584                                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
585                                                 }
586                                         }
587                                 }
588                         }
589
590                         $newtag = $old_tag;
591                         if(strlen($newtag) && strlen($str_tags)) 
592                                 $newtag .= ',';
593                         $newtag .= $str_tags;
594
595                         $newinform = $old_inform;
596                         if(strlen($newinform) && strlen($inform))
597                                 $newinform .= ',';
598                         $newinform .= $inform;
599
600                         $r = q("UPDATE `item` SET `tag` = '%s', `inform` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
601                                 dbesc($newtag),
602                                 dbesc($newinform),
603                                 dbesc(datetime_convert()),
604                                 dbesc(datetime_convert()),
605                                 intval($item_id),
606                                 intval($page_owner_uid)
607                         );
608
609                         $best = 0;
610                         foreach($p as $scales) {
611                                 if(intval($scales['scale']) == 2) {
612                                         $best = 2;
613                                         break;
614                                 }
615                                 if(intval($scales['scale']) == 4) {
616                                         $best = 4;
617                                         break;
618                                 }
619                         }
620
621                         if(count($taginfo)) {
622                                 foreach($taginfo as $tagged) {
623                 
624                                         $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
625
626                                         $arr = array();
627
628                                         $arr['uid']           = $page_owner_uid;
629                                         $arr['uri']           = $uri;
630                                         $arr['parent-uri']    = $uri;
631                                         $arr['type']          = 'activity';
632                                         $arr['wall']          = 1;
633                                         $arr['contact-id']    = $owner_record['id'];
634                                         $arr['owner-name']    = $owner_record['name'];
635                                         $arr['owner-link']    = $owner_record['url'];
636                                         $arr['owner-avatar']  = $owner_record['thumb'];
637                                         $arr['author-name']   = $owner_record['name'];
638                                         $arr['author-link']   = $owner_record['url'];
639                                         $arr['author-avatar'] = $owner_record['thumb'];
640                                         $arr['title']         = '';
641                                         $arr['allow_cid']     = $p[0]['allow_cid'];
642                                         $arr['allow_gid']     = $p[0]['allow_gid'];
643                                         $arr['deny_cid']      = $p[0]['deny_cid'];
644                                         $arr['deny_gid']      = $p[0]['deny_gid'];
645                                         $arr['last-child']    = 1;
646                                         $arr['visible']       = 1;
647                                         $arr['verb']          = ACTIVITY_TAG;
648                                         $arr['object-type']   = ACTIVITY_OBJ_PERSON;
649                                         $arr['target-type']   = ACTIVITY_OBJ_PHOTO;
650                                         $arr['tag']           = $tagged[4];
651                                         $arr['inform']        = $tagged[2];
652                                         $arr['origin']        = 1;
653                                         $arr['body']          = sprintf( t('%1$s was tagged in %2$s by %3$s'), '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]', '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . t('a photo') . '[/url]', '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]') ;
654                                         $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 . '.' . $ext . '[/img][/url]' . "\n" ;
655
656                                         $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>';
657                                         $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n");
658                                         if($tagged[3])
659                                                 $arr['object'] .= xmlify('<link rel="photo" type="'.$p[0]['type'].'" href="' . $tagged[3]['photo'] . '" />' . "\n");
660                                         $arr['object'] .= '</link></object>' . "\n";
661
662                                         $arr['target'] = '<target><type>' . ACTIVITY_OBJ_PHOTO . '</type><title>' . $p[0]['desc'] . '</title><id>'
663                                                 . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '</id>';
664                                         $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="'.$p[0]['type'].'" href="' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '" />') . '</link></target>';
665
666                                         $item_id = item_store($arr);
667                                         if($item_id) {
668                                                 q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
669                                                         dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
670                                                         intval($page_owner_uid),
671                                                         intval($item_id)
672                                                 );
673
674                                                 proc_run('php',"include/notifier.php","tag","$item_id");
675                                         }
676                                 }
677
678                         }
679
680                 }
681                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
682                 return; // NOTREACHED
683         }
684
685
686         /**
687          * default post action - upload a photo
688          */
689
690         call_hooks('photo_post_init', $_POST);
691
692         /**
693          * Determine the album to use
694          */
695
696         $album    = notags(trim($_REQUEST['album']));
697         $newalbum = notags(trim($_REQUEST['newalbum']));
698
699         logger('mod/photos.php: photos_post(): album= ' . $album . ' newalbum= ' . $newalbum , LOGGER_DEBUG);
700
701         if(! strlen($album)) {
702                 if(strlen($newalbum))
703                         $album = $newalbum;
704                 else
705                         $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
706         }
707
708         /**
709          *
710          * We create a wall item for every photo, but we don't want to
711          * overwhelm the data stream with a hundred newly uploaded photos.
712          * So we will make the first photo uploaded to this album in the last several hours
713          * visible by default, the rest will become visible over time when and if
714          * they acquire comments, likes, dislikes, and/or tags 
715          *
716          */
717
718         $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR ",
719                 dbesc($album),
720                 intval($page_owner_uid)
721         );
722         if((! count($r)) || ($album == t('Profile Photos')))
723                 $visible = 1;
724         else
725                 $visible = 0;
726         
727         if(intval($_REQUEST['not_visible']) || $_REQUEST['not_visible'] === 'true')
728                 $visible = 0;
729
730         $str_group_allow   = perms2str(((is_array($_REQUEST['group_allow']))   ? $_REQUEST['group_allow']   : explode(',',$_REQUEST['group_allow'])));
731         $str_contact_allow = perms2str(((is_array($_REQUEST['contact_allow'])) ? $_REQUEST['contact_allow'] : explode(',',$_REQUEST['contact_allow'])));
732         $str_group_deny    = perms2str(((is_array($_REQUEST['group_deny']))    ? $_REQUEST['group_deny']    : explode(',',$_REQUEST['group_deny'])));
733         $str_contact_deny  = perms2str(((is_array($_REQUEST['contact_deny']))  ? $_REQUEST['contact_deny']  : explode(',',$_REQUEST['contact_deny'])));
734
735         $ret = array('src' => '', 'filename' => '', 'filesize' => 0, 'type' => '');
736
737         call_hooks('photo_post_file',$ret);
738
739         if(x($ret,'src') && x($ret,'filesize')) {
740                 $src      = $ret['src'];
741                 $filename = $ret['filename'];
742                 $filesize = $ret['filesize'];
743                 $type     = $ret['type'];
744         }
745         else {
746                 $src        = $_FILES['userfile']['tmp_name'];
747                 $filename   = basename($_FILES['userfile']['name']);
748                 $filesize   = intval($_FILES['userfile']['size']);
749                 $type       = $_FILES['userfile']['type'];
750         }
751         if ($type=="") $type=guess_image_type($filename);
752
753         logger('photos: upload: received file: ' . $filename . ' as ' . $src . ' ('. $type . ') ' . $filesize . ' bytes', LOGGER_DEBUG);
754
755         $maximagesize = get_config('system','maximagesize');
756
757         if(($maximagesize) && ($filesize > $maximagesize)) {
758                 notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
759                 @unlink($src);
760                 $foo = 0;
761                 call_hooks('photo_post_end',$foo);
762                 return;
763         }
764
765         if(! $filesize) {
766                 notice( t('Image file is empty.') . EOL);
767                 @unlink($src);
768                 $foo = 0;
769                 call_hooks('photo_post_end',$foo);
770                 return;
771         }
772
773         logger('mod/photos.php: photos_post(): loading the contents of ' . $src , LOGGER_DEBUG);
774
775         $imagedata = @file_get_contents($src);
776
777
778
779         $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
780                 intval($a->data['user']['uid'])
781         );
782
783         $limit = service_class_fetch($a->data['user']['uid'],'photo_upload_limit');
784
785         if(($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
786                 notice( upgrade_message() . EOL );
787                 @unlink($src);
788                 $foo = 0;
789                 call_hooks('photo_post_end',$foo);
790                 killme();
791         }
792                 
793
794         $ph = new Photo($imagedata, $type);
795
796         if(! $ph->is_valid()) {
797                 logger('mod/photos.php: photos_post(): unable to process image' , LOGGER_DEBUG);
798                 notice( t('Unable to process image.') . EOL );
799                 @unlink($src);
800                 $foo = 0;
801                 call_hooks('photo_post_end',$foo);
802                 killme();
803         }
804
805         $ph->orient($src);
806         @unlink($src);
807
808         $max_length = get_config('system','max_image_length');
809         if(! $max_length)
810                 $max_length = MAX_IMAGE_LENGTH;
811         if($max_length > 0)
812                 $ph->scaleImage($max_length);
813
814         $width  = $ph->getWidth();
815         $height = $ph->getHeight();
816
817         $smallest = 0;
818
819         $photo_hash = photo_new_resource();
820
821         $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);
822
823         if(! $r) {
824                 logger('mod/photos.php: photos_post(): image store failed' , LOGGER_DEBUG);
825                 notice( t('Image upload failed.') . EOL );
826                 killme();
827         }
828
829         if($width > 640 || $height > 640) {
830                 $ph->scaleImage(640);
831                 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
832                 $smallest = 1;
833         }
834
835         if($width > 320 || $height > 320) {
836                 $ph->scaleImage(320);
837                 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
838                 $smallest = 2;
839         }
840         
841         $basename = basename($filename);
842         $uri = item_new_uri($a->get_hostname(), $page_owner_uid);
843
844         // Create item container
845
846         $arr = array();
847
848         $arr['uid']           = $page_owner_uid;
849         $arr['uri']           = $uri;
850         $arr['parent-uri']    = $uri;
851         $arr['type']          = 'photo';
852         $arr['wall']          = 1;
853         $arr['resource-id']   = $photo_hash;
854         $arr['contact-id']    = $owner_record['id'];
855         $arr['owner-name']    = $owner_record['name'];
856         $arr['owner-link']    = $owner_record['url'];
857         $arr['owner-avatar']  = $owner_record['thumb'];
858         $arr['author-name']   = $owner_record['name'];
859         $arr['author-link']   = $owner_record['url'];
860         $arr['author-avatar'] = $owner_record['thumb'];
861         $arr['title']         = '';
862         $arr['allow_cid']     = $str_contact_allow;
863         $arr['allow_gid']     = $str_group_allow;
864         $arr['deny_cid']      = $str_contact_deny;
865         $arr['deny_gid']      = $str_group_deny;
866         $arr['last-child']    = 1;
867         $arr['visible']       = $visible;
868         $arr['origin']        = 1;
869
870         $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']' 
871                                 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.".$ph->getExt() . '[/img]' 
872                                 . '[/url]';
873
874         $item_id = item_store($arr);
875
876         if($item_id) {
877                 q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
878                         dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
879                         intval($page_owner_uid),
880                         intval($item_id)
881                 );
882         }
883         
884         if($visible) 
885                 proc_run('php', "include/notifier.php", 'wall-new', $item_id);
886
887         call_hooks('photo_post_end',intval($item_id));
888
889         // addon uploaders should call "killme()" [e.g. exit] within the photo_post_end hook
890         // if they do not wish to be redirected
891
892         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
893         // NOTREACHED
894 }
895
896
897
898 function photos_content(&$a) {
899
900         // URLs:
901         // photos/name
902         // photos/name/upload
903         // photos/name/upload/xxxxx (xxxxx is album name)
904         // photos/name/album/xxxxx
905         // photos/name/album/xxxxx/edit
906         // photos/name/image/xxxxx
907         // photos/name/image/xxxxx/edit
908
909
910         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
911                 notice( t('Public access denied.') . EOL);
912                 return;
913         }
914         
915         
916         require_once('include/bbcode.php');
917         require_once('include/security.php');
918         require_once('include/conversation.php');
919
920         if(! x($a->data,'user')) {
921                 notice( t('No photos selected') . EOL );
922                 return;
923         }
924
925         $phototypes = Photo::supportedTypes();
926
927         $_SESSION['photo_return'] = $a->cmd;
928
929         //
930         // Parse arguments 
931         //
932
933         if($a->argc > 3) {
934                 $datatype = $a->argv[2];
935                 $datum = $a->argv[3];
936         }
937         elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
938                 $datatype = 'upload';
939         else
940                 $datatype = 'summary';
941
942         if($a->argc > 4)
943                 $cmd = $a->argv[4];
944         else
945                 $cmd = 'view';
946
947         //
948         // Setup permissions structures
949         //
950
951         $can_post       = false;
952         $visitor        = 0;
953         $contact        = null;
954         $remote_contact = false;
955         $contact_id     = 0;
956
957         $owner_uid = $a->data['user']['uid'];
958
959         $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
960
961         if((local_user()) && (local_user() == $owner_uid))
962                 $can_post = true;
963         else {
964                 if($community_page && remote_user()) {
965                         if(is_array($_SESSION['remote'])) {
966                                 foreach($_SESSION['remote'] as $v) {
967                                         if($v['uid'] == $owner_uid) {
968                                                 $contact_id = $v['cid'];
969                                                 break;
970                                         }
971                                 }
972                         }
973                         if($contact_id) {
974
975                                 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
976                                         intval($contact_id),
977                                         intval($owner_uid)
978                                 );
979                                 if(count($r)) {
980                                         $can_post = true;
981                                         $contact = $r[0];
982                                         $remote_contact = true;
983                                         $visitor = $cid;
984                                 }
985                         }
986                 }
987         }
988
989         // perhaps they're visiting - but not a community page, so they wouldn't have write access
990
991         if(remote_user() && (! $visitor)) {
992                 $contact_id = 0;
993                 if(is_array($_SESSION['remote'])) {
994                         foreach($_SESSION['remote'] as $v) {
995                                 if($v['uid'] == $owner_uid) {
996                                         $contact_id = $v['cid'];
997                                         break;
998                                 }
999                         }
1000                 }
1001                 if($contact_id) {
1002                         $groups = init_groups_visitor($contact_id);
1003                         $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
1004                                 intval($contact_id),
1005                                 intval($owner_uid)
1006                         );
1007                         if(count($r)) {
1008                                 $contact = $r[0];
1009                                 $remote_contact = true;
1010                         }
1011                 }
1012         }
1013
1014         if(! $remote_contact) {
1015                 if(local_user()) {
1016                         $contact_id = $_SESSION['cid'];
1017                         $contact = $a->contact;
1018                 }
1019         }
1020
1021         if($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (! $remote_contact)) {
1022                 notice( t('Access to this item is restricted.') . EOL);
1023                 return;
1024         }
1025
1026         $sql_extra = permissions_sql($owner_uid,$remote_contact,$groups);
1027
1028         $o = "";
1029
1030         // tabs
1031         $_is_owner = (local_user() && (local_user() == $owner_uid));
1032         $o .= profile_tabs($a,$_is_owner, $a->data['user']['nickname']);        
1033
1034         //
1035         // dispatch request
1036         //
1037
1038
1039         if($datatype === 'upload') {
1040                 if(! ($can_post)) {
1041                         notice( t('Permission denied.'));
1042                         return;
1043                 }
1044
1045
1046                 $selname = (($datum) ? hex2bin($datum) : '');
1047
1048
1049                 $albumselect = '';
1050
1051                 
1052                 $albumselect .= '<option value="" ' . ((! $selname) ? ' selected="selected" ' : '') . '>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
1053                 if(count($a->data['albums'])) {
1054                         foreach($a->data['albums'] as $album) {
1055                                 if(($album['album'] === '') || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
1056                                         continue;
1057                                 $selected = (($selname === $album['album']) ? ' selected="selected" ' : '');
1058                                 $albumselect .= '<option value="' . $album['album'] . '"' . $selected . '>' . $album['album'] . '</option>';
1059                         }
1060                 }
1061
1062                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
1063
1064                 $uploader = '';
1065
1066                 $ret = array('post_url' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'],
1067                                 'addon_text' => $uploader,
1068                                 'default_upload' => true);
1069
1070
1071                 call_hooks('photo_upload_form',$ret);
1072
1073                 $default_upload_box = replace_macros(get_markup_template('photos_default_uploader_box.tpl'), array());
1074                 $default_upload_submit = replace_macros(get_markup_template('photos_default_uploader_submit.tpl'), array(
1075                         '$submit' => t('Submit'),
1076                 ));
1077
1078                 $usage_message = '';
1079                 $limit = service_class_fetch($a->data['user']['uid'],'photo_upload_limit');
1080                 if($limit !== false) {
1081
1082                         $r = q("select sum(datasize) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
1083                                 intval($a->data['user']['uid'])
1084                         );
1085                         $usage_message = sprintf( t("You have used %1$.2f Mbytes of %2$.2f Mbytes photo storage."), $r[0]['total'] / 1024000, $limit / 1024000 );
1086                 }
1087
1088
1089                 // Private/public post links for the non-JS ACL form
1090                 $private_post = 1;
1091                 if($_REQUEST['public'])
1092                         $private_post = 0;
1093
1094                 $query_str = $a->query_string;
1095                 if(strpos($query_str, 'public=1') !== false)
1096                         $query_str = str_replace(array('?public=1', '&public=1'), array('', ''), $query_str);
1097
1098                 // I think $a->query_string may never have ? in it, but I could be wrong
1099                 // It looks like it's from the index.php?q=[etc] rewrite that the web
1100                 // server does, which converts any ? to &, e.g. suggest&ignore=61 for suggest?ignore=61
1101                 if(strpos($query_str, '?') === false)
1102                         $public_post_link = '?public=1';
1103                 else
1104                         $public_post_link = '&public=1';
1105
1106
1107
1108                 $tpl = get_markup_template('photos_upload.tpl');
1109
1110                 if($a->theme['template_engine'] === 'internal') {
1111                         $albumselect_e = template_escape($albumselect);
1112                         $aclselect_e = (($visitor) ? '' : template_escape(populate_acl($a->user, $celeb)));
1113                 }
1114                 else {
1115                         $albumselect_e = $albumselect;
1116                         $aclselect_e = (($visitor) ? '' : populate_acl($a->user, $celeb));
1117                 }
1118
1119                 $o .= replace_macros($tpl,array(
1120                         '$pagename' => t('Upload Photos'),
1121                         '$sessid' => session_id(),
1122                         '$usage' => $usage_message,
1123                         '$nickname' => $a->data['user']['nickname'],
1124                         '$newalbum' => t('New album name: '),
1125                         '$existalbumtext' => t('or existing album name: '),
1126                         '$nosharetext' => t('Do not show a status post for this upload'),
1127                         '$albumselect' => $albumselect_e,
1128                         '$permissions' => t('Permissions'),
1129                         '$aclselect' => $aclselect_e,
1130                         '$alt_uploader' => $ret['addon_text'],
1131                         '$default_upload_box' => (($ret['default_upload']) ? $default_upload_box : ''),
1132                         '$default_upload_submit' => (($ret['default_upload']) ? $default_upload_submit : ''),
1133                         '$uploadurl' => $ret['post_url'],
1134
1135                         // ACL permissions box
1136                         '$acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
1137                         '$group_perms' => t('Show to Groups'),
1138                         '$contact_perms' => t('Show to Contacts'),
1139                         '$private' => t('Private Photo'),
1140                         '$public' => t('Public Photo'),
1141                         '$is_private' => $private_post,
1142                         '$return_path' => $query_str,
1143                         '$public_link' => $public_post_link,
1144
1145                 ));
1146
1147                 return $o; 
1148         }
1149
1150         if($datatype === 'album') {
1151
1152                 $album = hex2bin($datum);
1153
1154                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
1155                         AND `scale` <= 4 $sql_extra GROUP BY `resource-id`",
1156                         intval($owner_uid),
1157                         dbesc($album)
1158                 );
1159                 if(count($r)) {
1160                         $a->set_pager_total(count($r));
1161                         $a->set_pager_itemspage(20);
1162                 }
1163
1164                 if($_GET['order'] === 'posted')
1165                         $order = 'ASC';
1166                 else
1167                         $order = 'DESC';
1168
1169                 $r = q("SELECT `resource-id`, `id`, `filename`, type, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
1170                         AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` $order LIMIT %d , %d",
1171                         intval($owner_uid),
1172                         dbesc($album),
1173                         intval($a->pager['start']),
1174                         intval($a->pager['itemspage'])
1175                 );
1176
1177                 $o .= '<h3>' . $album . '</h3>';
1178                 
1179                 if($cmd === 'edit') {           
1180                         if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
1181                                 if($can_post) {
1182                                         $edit_tpl = get_markup_template('album_edit.tpl');
1183
1184                                         if($a->theme['template_engine'] === 'internal') {
1185                                                 $album_e = template_escape($album);
1186                                         }
1187                                         else {
1188                                                 $album_e = $album;
1189                                         }
1190
1191                                         $o .= replace_macros($edit_tpl,array(
1192                                                 '$nametext' => t('New album name: '),
1193                                                 '$nickname' => $a->data['user']['nickname'],
1194                                                 '$album' => $album_e,
1195                                                 '$hexalbum' => bin2hex($album),
1196                                                 '$submit' => t('Submit'),
1197                                                 '$dropsubmit' => t('Delete Album')
1198                                         ));
1199                                 }
1200                         }
1201                 }
1202                 else {
1203                         if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
1204                                 if($can_post) {
1205                                         $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/' 
1206                                                 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">' 
1207                                                 . t('Edit Album') . '</a></div>';
1208                                 }
1209                         }
1210                 }
1211
1212                 if($_GET['order'] === 'posted')
1213                         $o .=  '<div class="photos-upload-link" ><a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '" >' . t('Show Newest First') . '</a></div>';
1214                 else
1215                         $o .= '<div class="photos-upload-link" ><a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '?f=&order=posted" >' . t('Show Oldest First') . '</a></div>';
1216
1217
1218                 if($can_post) {
1219                         $o .= '<div class="photos-upload-link" ><a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/upload/' . bin2hex($album) . '" >' . t('Upload New Photos') . '</a></div>';
1220                 }
1221
1222
1223                 $tpl = get_markup_template('photo_album.tpl');
1224                 if(count($r))
1225                         $twist = 'rotright';
1226                         foreach($r as $rr) {
1227                                 if($twist == 'rotright')
1228                                         $twist = 'rotleft';
1229                                 else
1230                                         $twist = 'rotright';
1231                                 
1232                                 $ext = $phototypes[$rr['type']];
1233
1234                                 if($a->theme['template_engine'] === 'internal') {
1235                                         $imgalt_e = template_escape($rr['filename']);
1236                                         $desc_e = template_escape($rr['desc']);
1237                                 }
1238                                 else {
1239                                         $imgalt_e = $rr['filename'];
1240                                         $desc_e = $rr['desc'];
1241                                 }
1242
1243                                 $o .= replace_macros($tpl,array(
1244                                         '$id' => $rr['id'],
1245                                         '$twist' => ' ' . $twist . rand(2,4),
1246                                         '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id']
1247                                                 . (($_GET['order'] === 'posted') ? '?f=&order=posted' : ''),
1248                                         '$phototitle' => t('View Photo'),
1249                                         '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.' .$ext,
1250                                         '$imgalt' => $imgalt_e,
1251                                         '$desc'=> $desc_e
1252                                 ));
1253
1254                 }
1255                 $o .= '<div id="photo-album-end"></div>';
1256                 $o .= paginate($a);
1257
1258                 return $o;
1259
1260         }       
1261
1262
1263         if($datatype === 'image') {
1264
1265
1266
1267                 //$o = '';
1268                 // fetch image, item containing image, then comments
1269
1270                 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
1271                         $sql_extra ORDER BY `scale` ASC ",
1272                         intval($owner_uid),
1273                         dbesc($datum)
1274                 );
1275
1276                 if(! count($ph)) {
1277                         $ph = q("SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
1278                                 LIMIT 1",
1279                                 intval($owner_uid),
1280                                 dbesc($datum)
1281                         );
1282                         if(count($ph)) 
1283                                 notice( t('Permission denied. Access to this item may be restricted.'));
1284                         else
1285                                 notice( t('Photo not available') . EOL );
1286                         return;
1287                 }
1288
1289                 $prevlink = '';
1290                 $nextlink = '';
1291
1292                 if($_GET['order'] === 'posted')
1293                         $order = 'ASC';
1294                 else
1295                         $order = 'DESC';
1296
1297
1298                 $prvnxt = q("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0 
1299                         $sql_extra ORDER BY `created` $order ",
1300                         dbesc($ph[0]['album']),
1301                         intval($owner_uid)
1302                 ); 
1303
1304                 if(count($prvnxt)) {
1305                         for($z = 0; $z < count($prvnxt); $z++) {
1306                                 if($prvnxt[$z]['resource-id'] == $ph[0]['resource-id']) {
1307                                         $prv = $z - 1;
1308                                         $nxt = $z + 1;
1309                                         if($prv < 0)
1310                                                 $prv = count($prvnxt) - 1;
1311                                         if($nxt >= count($prvnxt))
1312                                                 $nxt = 0;
1313                                         break;
1314                                 }
1315                         }
1316                         $edit_suffix = ((($cmd === 'edit') && ($can_post)) ? '/edit' : '');
1317                         $prevlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] . $edit_suffix . (($_GET['order'] === 'posted') ? '?f=&order=posted' : '');
1318                         $nextlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] . $edit_suffix . (($_GET['order'] === 'posted') ? '?f=&order=posted' : '');
1319                 }
1320
1321
1322                 if(count($ph) == 1)
1323                         $hires = $lores = $ph[0];
1324                 if(count($ph) > 1) {
1325                         if($ph[1]['scale'] == 2) {
1326                                 // original is 640 or less, we can display it directly
1327                                 $hires = $lores = $ph[0];
1328                         }
1329                         else {
1330                         $hires = $ph[0];
1331                         $lores = $ph[1];
1332                         }
1333                 }
1334
1335                 $album_link = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']);
1336                 $tools = Null;
1337                 $lock = Null;
1338  
1339                 if($can_post && ($ph[0]['uid'] == $owner_uid)) {
1340                         $tools = array(
1341                                 'edit'  => array($a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $datum . (($cmd === 'edit') ? '' : '/edit'), (($cmd === 'edit') ? t('View photo') : t('Edit photo'))),
1342                                 'profile'=>array($a->get_baseurl() . '/profile_photo/use/'.$ph[0]['resource-id'], t('Use as profile photo')),
1343                         );
1344
1345                         // lock
1346                         $lock = ( ( ($ph[0]['uid'] == local_user()) && (strlen($ph[0]['allow_cid']) || strlen($ph[0]['allow_gid']) 
1347                                         || strlen($ph[0]['deny_cid']) || strlen($ph[0]['deny_gid'])) ) 
1348                                         ? t('Private Message')
1349                                         : Null);
1350                         
1351                         
1352                 }
1353
1354                 if( $cmd === 'edit') {
1355                         $tpl = get_markup_template('photo_edit_head.tpl');
1356                         $a->page['htmlhead'] .= replace_macros($tpl,array(
1357                                 '$prevlink' => $prevlink,
1358                                 '$nextlink' => $nextlink
1359                         ));
1360                 }
1361
1362                 if($prevlink)
1363                         $prevlink = array($prevlink, '<div class="icon prev"></div>') ;
1364
1365                 $photo = array(
1366                         'href' => $a->get_baseurl() . '/photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.' . $phototypes[$hires['type']],
1367                         'title'=> t('View Full Size'),
1368                         'src'  => $a->get_baseurl() . '/photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.' . $phototypes[$lores['type']] . '?f=&_u=' . datetime_convert('','','','ymdhis')
1369                 );
1370
1371                 if($nextlink)
1372                         $nextlink = array($nextlink, '<div class="icon next"></div>');
1373
1374
1375                 // Do we have an item for this photo?
1376
1377                 // FIXME! - replace following code to display the conversation with our normal 
1378                 // conversation functions so that it works correctly and tracks changes
1379                 // in the evolving conversation code.
1380                 // The difference is that we won't be displaying the conversation head item
1381                 // as a "post" but displaying instead the photo it is linked to
1382
1383                 $linked_items = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
1384                         dbesc($datum)
1385                 );
1386                 if(count($linked_items)) {
1387                         $link_item = $linked_items[0];
1388                         $r = q("SELECT COUNT(*) AS `total`
1389                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
1390                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0
1391                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1392                                 AND `item`.`uid` = %d 
1393                                 $sql_extra ",
1394                                 dbesc($link_item['uri']),
1395                                 dbesc($link_item['uri']),
1396                                 intval($link_item['uid'])
1397
1398                         );
1399
1400                         if(count($r))
1401                                 $a->set_pager_total($r[0]['total']);
1402
1403
1404                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
1405                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`, 
1406                                 `contact`.`rel`, `contact`.`thumb`, `contact`.`self`, 
1407                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
1408                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
1409                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0
1410                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1411                                 AND `item`.`uid` = %d
1412                                 $sql_extra
1413                                 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
1414                                 dbesc($link_item['uri']),
1415                                 dbesc($link_item['uri']),
1416                                 intval($link_item['uid']),
1417                                 intval($a->pager['start']),
1418                                 intval($a->pager['itemspage'])
1419
1420                         );
1421                 
1422                         if((local_user()) && (local_user() == $link_item['uid'])) {
1423                                 q("UPDATE `item` SET `unseen` = 0 WHERE `parent` = %d and `uid` = %d",
1424                                         intval($link_item['parent']),
1425                                         intval(local_user())
1426                                 );
1427                         }
1428                 }
1429
1430                 $tags=Null;
1431
1432                 if(count($linked_items) && strlen($link_item['tag'])) {
1433                         $arr = explode(',',$link_item['tag']);
1434                         // parse tags and add links
1435                         $tag_str = '';
1436                         foreach($arr as $t) {
1437                                 if(strlen($tag_str))
1438                                         $tag_str .= ', ';
1439                                 $tag_str .= bbcode($t);
1440                         } 
1441                         $tags = array(t('Tags: '), $tag_str);
1442                         if($cmd === 'edit') {
1443                                 $tags[] = $a->get_baseurl() . '/tagrm/' . $link_item['id'];
1444                                 $tags[] = t('[Remove any tag]');
1445                         }
1446                 }
1447
1448
1449                 $edit = Null;
1450                 if(($cmd === 'edit') && ($can_post)) {
1451                         $edit_tpl = get_markup_template('photo_edit.tpl');
1452
1453                         // Private/public post links for the non-JS ACL form
1454                         $private_post = 1;
1455                         if($_REQUEST['public'])
1456                                 $private_post = 0;
1457
1458                         $query_str = $a->query_string;
1459                         if(strpos($query_str, 'public=1') !== false)
1460                                 $query_str = str_replace(array('?public=1', '&public=1'), array('', ''), $query_str);
1461
1462                         // I think $a->query_string may never have ? in it, but I could be wrong
1463                         // It looks like it's from the index.php?q=[etc] rewrite that the web
1464                         // server does, which converts any ? to &, e.g. suggest&ignore=61 for suggest?ignore=61
1465                         if(strpos($query_str, '?') === false)
1466                                 $public_post_link = '?public=1';
1467                         else
1468                                 $public_post_link = '&public=1';
1469
1470
1471                         if($a->theme['template_engine'] === 'internal') {
1472                                 $album_e = template_escape($ph[0]['album']);
1473                                 $caption_e = template_escape($ph[0]['desc']);
1474                                 $aclselect_e = template_escape(populate_acl($ph[0]));
1475                         }
1476                         else {
1477                                 $album_e = $ph[0]['album'];
1478                                 $caption_e = $ph[0]['desc'];
1479                                 $aclselect_e = populate_acl($ph[0]);
1480                         }
1481
1482                         $edit = replace_macros($edit_tpl, array(
1483                                 '$id' => $ph[0]['id'],
1484                                 '$rotatecw' => t('Rotate CW (right)'),
1485                                 '$rotateccw' => t('Rotate CCW (left)'),
1486                                 '$album' => $album_e,
1487                                 '$newalbum' => t('New album name'), 
1488                                 '$nickname' => $a->data['user']['nickname'],
1489                                 '$resource_id' => $ph[0]['resource-id'],
1490                                 '$capt_label' => t('Caption'),
1491                                 '$caption' => $caption_e,
1492                                 '$tag_label' => t('Add a Tag'),
1493                                 '$tags' => $link_item['tag'],
1494                                 '$permissions' => t('Permissions'),
1495                                 '$aclselect' => $aclselect_e,
1496                                 '$help_tags' => t('Example: @bob, @Barbara_Jensen, @jim@example.com, #California, #camping'),
1497                                 '$item_id' => ((count($linked_items)) ? $link_item['id'] : 0),
1498                                 '$submit' => t('Submit'),
1499                                 '$delete' => t('Delete Photo'),
1500
1501                                 // ACL permissions box
1502                                 '$acl_data' => construct_acl_data($a, $ph[0]), // For non-Javascript ACL selector
1503                                 '$group_perms' => t('Show to Groups'),
1504                                 '$contact_perms' => t('Show to Contacts'),
1505                                 '$private' => t('Private photo'),
1506                                 '$public' => t('Public photo'),
1507                                 '$is_private' => $private_post,
1508                                 '$return_path' => $query_str,
1509                                 '$public_link' => $public_post_link,
1510                         ));
1511                 }
1512
1513                 if(count($linked_items)) {
1514
1515                         $cmnt_tpl = get_markup_template('comment_item.tpl');
1516                         $tpl = get_markup_template('photo_item.tpl');
1517                         $return_url = $a->cmd;
1518
1519                         $like_tpl = get_markup_template('like_noshare.tpl');
1520
1521                         $likebuttons = '';
1522
1523                         if($can_post || can_write_wall($a,$owner_uid)) {
1524                                 $likebuttons = replace_macros($like_tpl,array(
1525                                         '$id' => $link_item['id'],
1526                                         '$likethis' => t("I like this \x28toggle\x29"),
1527                                         '$nolike' => (feature_enabled(local_user(), 'dislike') ? t("I don't like this \x28toggle\x29") : ''),
1528                                         '$share' => t('Share'),
1529                                         '$wait' => t('Please wait'),
1530                                         '$return_path' => $a->query_string,
1531                                 ));
1532                         }
1533
1534                         $comments = '';
1535                         if(! count($r)) {
1536                                 if($can_post || can_write_wall($a,$owner_uid)) {
1537                                         if($link_item['last-child']) {
1538                                                 $comments .= replace_macros($cmnt_tpl,array(
1539                                                         '$return_path' => '', 
1540                                                         '$jsreload' => $return_url,
1541                                                         '$type' => 'wall-comment',
1542                                                         '$id' => $link_item['id'],
1543                                                         '$parent' => $link_item['id'],
1544                                                         '$profile_uid' =>  $owner_uid,
1545                                                         '$mylink' => $contact['url'],
1546                                                         '$mytitle' => t('This is you'),
1547                                                         '$myphoto' => $contact['thumb'],
1548                                                         '$comment' => t('Comment'),
1549                                                         '$submit' => t('Submit'),
1550                                                         '$preview' => t('Preview'),
1551                                                         '$sourceapp' => t($a->sourcename),
1552                                                         '$ww' => '',
1553                                                         '$rand_num' => random_digits(12)
1554                                                 ));
1555                                         }
1556                                 }
1557                         }
1558
1559                         $alike = array();
1560                         $dlike = array();
1561                         
1562                         $like = '';
1563                         $dislike = '';
1564
1565
1566
1567                         // display comments
1568                         if(count($r)) {
1569
1570                                 foreach($r as $item) {
1571                                         like_puller($a,$item,$alike,'like');
1572                                         like_puller($a,$item,$dlike,'dislike');
1573                                 }
1574
1575                                 $like    = ((isset($alike[$link_item['id']])) ? format_like($alike[$link_item['id']],$alike[$link_item['id'] . '-l'],'like',$link_item['id']) : '');
1576                                 $dislike = ((isset($dlike[$link_item['id']])) ? format_like($dlike[$link_item['id']],$dlike[$link_item['id'] . '-l'],'dislike',$link_item['id']) : '');
1577
1578
1579
1580                                 if($can_post || can_write_wall($a,$owner_uid)) {
1581                                         if($link_item['last-child']) {
1582                                                 $comments .= replace_macros($cmnt_tpl,array(
1583                                                         '$return_path' => '',
1584                                                         '$jsreload' => $return_url,
1585                                                         '$type' => 'wall-comment',
1586                                                         '$id' => $link_item['id'],
1587                                                         '$parent' => $link_item['id'],
1588                                                         '$profile_uid' =>  $owner_uid,
1589                                                         '$mylink' => $contact['url'],
1590                                                         '$mytitle' => t('This is you'),
1591                                                         '$myphoto' => $contact['thumb'],
1592                                                         '$comment' => t('Comment'),
1593                                                         '$submit' => t('Submit'),
1594                                                         '$preview' => t('Preview'),
1595                                                         '$sourceapp' => t($a->sourcename),
1596                                                         '$ww' => '',
1597                                                         '$rand_num' => random_digits(12)
1598                                                 ));
1599                                         }
1600                                 }
1601
1602
1603                                 foreach($r as $item) {
1604                                         $comment = '';
1605                                         $template = $tpl;
1606                                         $sparkle = '';
1607
1608                                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) && ($item['id'] != $item['parent']))
1609                                                 continue;
1610
1611                                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
1612                         
1613
1614                                         if(local_user() && ($item['contact-uid'] == local_user()) 
1615                                                 && ($item['network'] == 'dfrn') && (! $item['self'] )) {
1616                                                 $profile_url = $redirect_url;
1617                                                 $sparkle = ' sparkle';
1618                                         }
1619                                         else {
1620                                                 $profile_url = $item['url'];
1621                                                 $sparkle = '';
1622                                         }
1623  
1624                                         $diff_author = (($item['url'] !== $item['author-link']) ? true : false);
1625
1626                                         $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
1627                                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $item['thumb']);
1628
1629                                         $profile_link = $profile_url;
1630
1631                                         $drop = '';
1632
1633                                         if(($item['contact-id'] == $contact_id) || ($item['uid'] == local_user()))
1634                                                 $drop = replace_macros(get_markup_template('photo_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
1635
1636
1637                                         if($a->theme['template_engine'] === 'internal') {
1638                                                 $name_e = template_escape($profile_name);
1639                                                 $title_e = template_escape($item['title']);
1640                                                 $body_e = template_escape(bbcode($item['body']));
1641                                         }
1642                                         else {
1643                                                 $name_e = $profile_name;
1644                                                 $title_e = $item['title'];
1645                                                 $body_e = bbcode($item['body']);
1646                                         }
1647
1648                                         $comments .= replace_macros($template,array(
1649                                                 '$id' => $item['item_id'],
1650                                                 '$profile_url' => $profile_link,
1651                                                 '$name' => $name_e,
1652                                                 '$thumb' => $profile_avatar,
1653                                                 '$sparkle' => $sparkle,
1654                                                 '$title' => $title_e,
1655                                                 '$body' => $body_e,
1656                                                 '$ago' => relative_date($item['created']),
1657                                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
1658                                                 '$drop' => $drop,
1659                                                 '$comment' => $comment
1660                                         ));
1661
1662                                         if($can_post || can_write_wall($a,$owner_uid)) {
1663
1664                                                 if($item['last-child']) {
1665                                                         $comments .= replace_macros($cmnt_tpl,array(
1666                                                                 '$return_path' => '',
1667                                                                 '$jsreload' => $return_url,
1668                                                                 '$type' => 'wall-comment',
1669                                                                 '$id' => $item['item_id'],
1670                                                                 '$parent' => $item['parent'],
1671                                                                 '$profile_uid' =>  $owner_uid,
1672                                                                 '$mylink' => $contact['url'],
1673                                                                 '$mytitle' => t('This is you'),
1674                                                                 '$myphoto' => $contact['thumb'],
1675                                                                 '$comment' => t('Comment'),
1676                                                                 '$submit' => t('Submit'),
1677                                                                 '$preview' => t('Preview'),
1678                                                                 '$sourceapp' => t($a->sourcename),
1679                                                                 '$ww' => '',
1680                                                                 '$rand_num' => random_digits(12)
1681                                                         ));
1682                                                 }
1683                                         }
1684                                 }
1685                         }
1686
1687                         $paginate = paginate($a);
1688                 }
1689                 
1690                 $photo_tpl = get_markup_template('photo_view.tpl');
1691
1692                 if($a->theme['template_engine'] === 'internal') {
1693                         $album_e = array($album_link,template_escape($ph[0]['album']));
1694                         $tags_e = template_escape($tags);
1695                         $like_e = template_escape($like);
1696                         $dislike_e = template_escape($dislike);
1697                 }
1698                 else {
1699                         $album_e = array($album_link,$ph[0]['album']);
1700                         $tags_e = $tags;
1701                         $like_e = $like;
1702                         $dislike_e = $dislike;
1703                 }
1704
1705                 $o .= replace_macros($photo_tpl, array(
1706                         '$id' => $ph[0]['id'],
1707                         '$album' => $album_e,
1708                         '$tools' => $tools,
1709                         '$lock' => $lock,
1710                         '$photo' => $photo,
1711                         '$prevlink' => $prevlink,
1712                         '$nextlink' => $nextlink,
1713                         '$desc' => $ph[0]['desc'],
1714                         '$tags' => $tags_e,
1715                         '$edit' => $edit,       
1716                         '$likebuttons' => $likebuttons,
1717                         '$like' => $like_e,
1718                         '$dislike' => $dikslike_e,
1719                         '$comments' => $comments,
1720                         '$paginate' => $paginate,
1721                 ));
1722                 
1723                 return $o;
1724         }
1725
1726         // Default - show recent photos with upload link (if applicable)
1727         //$o = '';
1728
1729         $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' 
1730                 $sql_extra GROUP BY `resource-id`",
1731                 intval($a->data['user']['uid']),
1732                 dbesc('Contact Photos'),
1733                 dbesc( t('Contact Photos'))
1734         );
1735         if(count($r)) {
1736                 $a->set_pager_total(count($r));
1737                 $a->set_pager_itemspage(20);
1738         }
1739
1740         $r = q("SELECT `resource-id`, `id`, `filename`, type, `album`, max(`scale`) AS `scale` FROM `photo`
1741                 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'  
1742                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
1743                 intval($a->data['user']['uid']),
1744                 dbesc('Contact Photos'),
1745                 dbesc( t('Contact Photos')),
1746                 intval($a->pager['start']),
1747                 intval($a->pager['itemspage'])
1748         );
1749
1750
1751
1752         $photos = array();
1753         if(count($r)) {
1754                 $twist = 'rotright';
1755                 foreach($r as $rr) {
1756                         if($twist == 'rotright')
1757                                 $twist = 'rotleft';
1758                         else
1759                                 $twist = 'rotright';
1760                         $ext = $phototypes[$rr['type']];
1761                         
1762                         if($a->theme['template_engine'] === 'internal') {
1763                                 $alt_e = template_escape($rr['filename']);
1764                                 $name_e = template_escape($rr['album']);
1765                         }
1766                         else {
1767                                 $alt_e = $rr['filename'];
1768                                 $name_e = $rr['album'];
1769                         }
1770
1771                         $photos[] = array(
1772                                 'id'       => $rr['id'],
1773                                 'twist'    => ' ' . $twist . rand(2,4),
1774                                 'link'          => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
1775                                 'title'         => t('View Photo'),
1776                                 'src'           => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.' . $ext,
1777                                 'alt'           => $alt_e,
1778                                 'album' => array(
1779                                         'link'  => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
1780                                         'name'  => $name_e,
1781                                         'alt'   => t('View Album'),
1782                                 ),
1783                                 
1784                         );
1785                 }
1786         }
1787         
1788         $tpl = get_markup_template('photos_recent.tpl'); 
1789         $o .= replace_macros($tpl, array(
1790                 '$title' => t('Recent Photos'),
1791                 '$can_post' => $can_post,
1792                 '$upload' => array(t('Upload New Photos'), $a->get_baseurl().'/photos/'.$a->data['user']['nickname'].'/upload'),
1793                 '$photos' => $photos,
1794         ));
1795
1796         
1797         $o .= paginate($a);
1798         return $o;
1799 }
1800