]> git.mxchange.org Git - friendica.git/blob - mod/photos.php
7b660bfa3d4f08c0623a9ea216de001b7d1f8561
[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
8 function photos_init(&$a) {
9
10
11         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
12                 return;
13         }
14         $o = '';
15
16         if($a->argc > 1) {
17                 $nick = $a->argv[1];
18                 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
19                         dbesc($nick)
20                 );
21
22                 if(! count($r))
23                         return;
24
25                 $a->data['user'] = $r[0];
26
27                 $sql_extra = permissions_sql($a->data['user']['uid']);
28
29                 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d $sql_extra ",
30                         intval($a->data['user']['uid'])
31                 );
32
33                 if(count($albums)) {
34                         $a->data['albums'] = $albums;
35
36                         $o .= '<h4><a href="' . $a->get_baseurl() . '/profile/' . $a->data['user']['nickname'] . '">' . $a->data['user']['username'] . '</a></h4>';
37                         $o .= '<div id="profile-photo-wrapper"><img class="photo" style="width: 175px; height: 175px;" src="' . $a->get_baseurl() . '/photo/profile/' . $a->data['user']['uid'] . '.jpg" alt="' . $a->data['user']['username'] . '" /></div>';
38
39                         $o .= '<h4>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
40                 
41                         $o .= '<ul>';
42                         foreach($albums as $album) {
43
44                                 // don't show contact photos. We once translated this name, but then you could still access it under
45                                 // a different language setting. Now we store the name in English and check in English (and translated for legacy albums).
46
47                                 if((! strlen($album['album'])) || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
48                                         continue;
49                                 $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" />' . $album['album'] . '</a></li>'; 
50                         }
51                         $o .= '</ul>';
52                 }
53
54                 if(! x($a->page,'aside'))
55                         $a->page['aside'] = '';
56                 $a->page['aside'] .= $o;
57
58
59                 $a->page['htmlhead'] .= "<script> var ispublic = '" . t('everybody') . "';" ;
60
61                 $a->page['htmlhead'] .= <<< EOT
62
63                 $(document).ready(function() {
64
65                         $('#contact_allow, #contact_deny, #group_allow, #group_deny').change(function() {
66                                 var selstr;
67                                 $('#contact_allow option:selected, #contact_deny option:selected, #group_allow option:selected, #group_deny option:selected').each( function() {
68                                         selstr = $(this).text();
69                                         $('#jot-perms-icon').removeClass('unlock').addClass('lock');
70                                         $('#jot-public').hide();
71                                 });
72                                 if(selstr == null) { 
73                                         $('#jot-perms-icon').removeClass('lock').addClass('unlock');
74                                         $('#jot-public').show();
75                                 }
76
77                         }).trigger('change');
78
79                 });
80
81                 </script>
82 EOT;
83         }
84
85         return;
86 }
87
88
89
90 function photos_post(&$a) {
91
92         logger('mod/photos.php: photos_post(): begin' , 'LOGGER_DEBUG');
93
94         foreach($_REQUEST AS $key => $val) {
95                 logger('mod/photos.php: photos_post(): $_REQUEST key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
96         }
97
98         foreach($_FILES AS $key => $val) {
99                 logger('mod/photos.php: photos_post(): $_FILES key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
100         }
101
102         $can_post  = false;
103         $visitor   = 0;
104
105         $page_owner_uid = $a->data['user']['uid'];
106         $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
107
108         if((local_user()) && (local_user() == $page_owner_uid))
109                 $can_post = true;
110         else {
111                 if($community_page && remote_user()) {
112                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
113                                 intval(remote_user()),
114                                 intval($page_owner_uid)
115                         );
116                         if(count($r)) {
117                                 $can_post = true;
118                                 $visitor = remote_user();
119                         }
120                 }
121         }
122
123         if(! $can_post) {
124                 notice( t('Permission denied.') . EOL );
125                 killme();
126         }
127
128         $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` 
129                 WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
130                 intval($page_owner_uid)
131         );
132
133         if(! count($r)) {
134                 notice( t('Contact information unavailable') . EOL);
135                 logger('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
136                 killme();
137         }
138
139         $owner_record = $r[0];  
140
141
142         if(($a->argc > 3) && ($a->argv[2] === 'album')) {
143                 $album = hex2bin($a->argv[3]);
144
145                 if($album === t('Profile Photos') || $album === 'Contact Photos' || $album === t('Contact Photos')) {
146                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
147                         return; // NOTREACHED
148                 }
149
150                 $r = q("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
151                         dbesc($album),
152                         intval($page_owner_uid)
153                 );
154                 if(! count($r)) {
155                         notice( t('Album not found.') . EOL);
156                         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
157                         return; // NOTREACHED
158                 }
159
160                 $newalbum = notags(trim($_POST['albumname']));
161                 if($newalbum != $album) {
162                         q("UPDATE `photo` SET `album` = '%s' WHERE `album` = '%s' AND `uid` = %d",
163                                 dbesc($newalbum),
164                                 dbesc($album),
165                                 intval($page_owner_uid)
166                         );
167                         $newurl = str_replace(bin2hex($album),bin2hex($newalbum),$_SESSION['photo_return']);
168                         goaway($a->get_baseurl() . '/' . $newurl);
169                         return; // NOTREACHED
170                 }
171
172
173                 if($_POST['dropalbum'] == t('Delete Album')) {
174
175                         $res = array();
176
177                         // get the list of photos we are about to delete
178
179                         if($visitor) {
180                                 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `album` = '%s'",
181                                         intval($visitor),
182                                         intval($page_owner_uid),
183                                         dbesc($album)
184                                 );
185                         }
186                         else {
187                                 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'",
188                                         intval(local_user()),
189                                         dbesc($album)
190                                 );
191                         }
192                         if(count($r)) {
193                                 foreach($r as $rr) {
194                                         $res[] = "'" . dbesc($rr['rid']) . "'" ;
195                                 }
196                         }
197                         else {
198                                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
199                                 return; // NOTREACHED
200                         }
201
202                         $str_res = implode(',', $res);
203
204                         // remove the associated photos
205
206                         q("DELETE FROM `photo` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
207                                 intval($page_owner_uid)
208                         );
209
210                         // find and delete the corresponding item with all the comments and likes/dislikes
211
212                         $r = q("SELECT `parent-uri` FROM `item` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
213                                 intval($page_owner_uid)
214                         );
215                         if(count($r)) {
216                                 foreach($r as $rr) {
217                                         q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
218                                                 dbesc(datetime_convert()),
219                                                 dbesc($rr['parent-uri']),
220                                                 intval($page_owner_uid)
221                                         );
222
223                                         $drop_id = intval($rr['id']);
224
225                                         // send the notification upstream/downstream as the case may be
226
227                                         if($rr['visible'])
228                                                 proc_run('php',"include/notifier.php","drop","$drop_id");
229                                 }
230                         }
231                 }
232                 goaway($a->get_baseurl() . '/photos/' . $a->data['user']['nickname']);
233                 return; // NOTREACHED
234         }
235
236         if(($a->argc > 2) && (x($_POST,'delete')) && ($_POST['delete'] == t('Delete Photo'))) {
237
238                 // same as above but remove single photo
239
240                 if($visitor) {
241                         $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `resource-id` = '%s' LIMIT 1",
242                                 intval($visitor),
243                                 intval($page_owner_uid),
244                                 dbesc($a->argv[2])
245                         );
246                 }
247                 else {
248                         $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' LIMIT 1",
249                                 intval(local_user()),
250                                 dbesc($a->argv[2])
251                         );
252                 }
253                 if(count($r)) {
254                         q("DELETE FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
255                                 intval($page_owner_uid),
256                                 dbesc($r[0]['resource-id'])
257                         );
258                         $i = q("SELECT * FROM `item` WHERE `resource-id` = '%s' AND `uid` = %d LIMIT 1",
259                                 dbesc($r[0]['resource-id']),
260                                 intval($page_owner_uid)
261                         );
262                         if(count($i)) {
263                                 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
264                                         dbesc(datetime_convert()),
265                                         dbesc(datetime_convert()),
266                                         dbesc($i[0]['uri']),
267                                         intval($page_owner_uid)
268                                 );
269
270                                 $url = $a->get_baseurl();
271                                 $drop_id = intval($i[0]['id']);
272
273                                 if($i[0]['visible'])
274                                         proc_run('php',"include/notifier.php","drop","$drop_id");
275                         }
276                 }
277
278                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
279                 return; // NOTREACHED
280         }
281
282         if(($a->argc > 2) && ((x($_POST,'desc') !== false) || (x($_POST,'newtag') !== false)) || (x($_POST,'albname') !== false)) {
283
284                 $desc        = ((x($_POST,'desc'))    ? notags(trim($_POST['desc']))    : '');
285                 $rawtags     = ((x($_POST,'newtag'))  ? notags(trim($_POST['newtag']))  : '');
286                 $item_id     = ((x($_POST,'item_id')) ? intval($_POST['item_id'])       : 0);
287                 $albname     = ((x($_POST,'albname')) ? notags(trim($_POST['albname'])) : '');
288                 $str_group_allow   = perms2str($_POST['group_allow']);
289                 $str_contact_allow = perms2str($_POST['contact_allow']);
290                 $str_group_deny    = perms2str($_POST['group_deny']);
291                 $str_contact_deny  = perms2str($_POST['contact_deny']);
292
293                 $resource_id = $a->argv[2];
294
295                 if(! strlen($albname))
296                         $albname = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
297                 
298
299                 $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
300                         dbesc($resource_id),
301                         intval($page_owner_uid)
302                 );
303                 if(count($p)) {
304                         $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",
305                                 dbesc($desc),
306                                 dbesc($albname),
307                                 dbesc($str_contact_allow),
308                                 dbesc($str_group_allow),
309                                 dbesc($str_contact_deny),
310                                 dbesc($str_group_deny),
311                                 dbesc($resource_id),
312                                 intval($page_owner_uid)
313                         );
314                 }
315
316                 /* Don't make the item visible if the only change was the album name */
317
318                 $visibility = 0;
319                 if($p[0]['desc'] !== $desc || strlen($rawtags))
320                         $visibility = 1;
321                 
322                 if(! $item_id) {
323
324                         // Create item container
325
326                         $title = '';
327                         $basename = basename($filename);
328                         $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
329
330                         $arr = array();
331
332                         $arr['uid']           = $page_owner_uid;
333                         $arr['uri']           = $uri;
334                         $arr['parent-uri']    = $uri; 
335                         $arr['type']          = 'photo';
336                         $arr['wall']          = 1;
337                         $arr['resource-id']   = $p[0]['resource-id'];
338                         $arr['contact-id']    = $owner_record['id'];
339                         $arr['owner-name']    = $owner_record['name'];
340                         $arr['owner-link']    = $owner_record['url'];
341                         $arr['owner-avatar']  = $owner_record['thumb'];
342                         $arr['author-name']   = $owner_record['name'];
343                         $arr['author-link']   = $owner_record['url'];
344                         $arr['author-avatar'] = $owner_record['thumb'];
345                         $arr['title']         = $title;
346                         $arr['allow_cid']     = $p[0]['allow_cid'];
347                         $arr['allow_gid']     = $p[0]['allow_gid'];
348                         $arr['deny_cid']      = $p[0]['deny_cid'];
349                         $arr['deny_gid']      = $p[0]['deny_gid'];
350                         $arr['last-child']    = 1;
351                         $arr['visible']       = $visibility;
352                         
353                         $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']' 
354                                                 . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.jpg' . '[/img]' 
355                                                 . '[/url]';
356                 
357                         $item_id = item_store($arr);
358
359                 }
360
361                 if($item_id) {
362                         $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
363                                 intval($item_id),
364                                 intval($page_owner_uid)
365                         );
366                 }
367                 if(count($r)) {
368                         $old_tag    = $r[0]['tag'];
369                         $old_inform = $r[0]['inform'];
370                 }
371
372                 if(strlen($rawtags)) {
373
374                         $str_tags = '';
375                         $inform   = '';
376
377                         // if the new tag doesn't have a namespace specifier (@foo or #foo) give it a hashtag
378
379                         $x = substr($rawtags,0,1);
380                         if($x !== '@' && $x !== '#')
381                                 $rawtags = '#' . $rawtags;
382
383                         $taginfo = array();
384                         $tags = get_tags($rawtags);
385
386                         if(count($tags)) {
387                                 foreach($tags as $tag) {
388                                         if(isset($profile))
389                                                 unset($profile);
390                                         if(strpos($tag,'@') === 0) {
391                                                 $name = substr($tag,1);
392                                                 if((strpos($name,'@')) || (strpos($name,'http://'))) {
393                                                         $newname = $name;
394                                                         $links = @lrdd($name);
395                                                         if(count($links)) {
396                                                                 foreach($links as $link) {
397                                                                         if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
398                                                         $profile = $link['@attributes']['href'];
399                                                                         if($link['@attributes']['rel'] === 'salmon') {
400                                                                                 $salmon = '$url:' . str_replace(',','%sc',$link['@attributes']['href']);
401                                                                                 if(strlen($inform))
402                                                                                         $inform .= ',';
403                                                         $inform .= $salmon;
404                                                                         }
405                                                                 }
406                                                         }
407                                                         $taginfo[] = array($newname,$profile,$salmon);
408                                                 }
409                                                 else {
410                                                         $newname = $name;
411                                                         if(strstr($name,'_') || strstr($name,' ')) {
412                                                                 $newname = str_replace('_',' ',$name);
413                                                                 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
414                                                                         dbesc($newname),
415                                                                         intval($page_owner_uid)
416                                                                 );
417                                                         }
418                                                         else {
419                                                                 $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
420                                                                         dbesc($name),
421                                                                         dbesc($name),
422                                                                         intval($page_owner_uid)
423                                                                 );
424                                                         }
425                                                         if(count($r)) {
426                                                                 $newname = $r[0]['name'];
427                                                                 $profile = $r[0]['url'];
428                                                                 $notify = 'cid:' . $r[0]['id'];
429                                                                 if(strlen($inform))
430                                                                         $inform .= ',';
431                                                                 $inform .= $notify;
432                                                         }
433                                                 }
434                                                 if($profile) {
435                                                         if(substr($notify,0,4) === 'cid:')
436                                                                 $taginfo[] = array($newname,$profile,$notify,$r[0],'@[url=' . str_replace(',','%2c',$profile) . ']' . $newname  . '[/url]');
437                                                         else
438                                                                 $taginfo[] = array($newname,$profile,$notify,null,$str_tags .= '@[url=' . $profile . ']' . $newname     . '[/url]');
439                                                         if(strlen($str_tags))
440                                                                 $str_tags .= ',';
441                                                         $profile = str_replace(',','%2c',$profile);
442                                                         $str_tags .= '@[url=' . $profile . ']' . $newname       . '[/url]';
443                                                 }
444                                         }
445                                 }
446                         }
447
448                         $newtag = $old_tag;
449                         if(strlen($newtag) && strlen($str_tags)) 
450                                 $newtag .= ',';
451                         $newtag .= $str_tags;
452
453                         $newinform = $old_inform;
454                         if(strlen($newinform) && strlen($inform))
455                                 $newinform .= ',';
456                         $newinform .= $inform;
457
458                         $r = q("UPDATE `item` SET `tag` = '%s', `inform` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
459                                 dbesc($newtag),
460                                 dbesc($newinform),
461                                 dbesc(datetime_convert()),
462                                 dbesc(datetime_convert()),
463                                 intval($item_id),
464                                 intval($page_owner_uid)
465                         );
466
467                         $best = 0;
468                         foreach($p as $scales) {
469                                 if(intval($scales['scale']) == 2) {
470                                         $best = 2;
471                                         break;
472                                 }
473                                 if(intval($scales['scale']) == 4) {
474                                         $best = 4;
475                                         break;
476                                 }
477                         }
478
479                         if(count($taginfo)) {
480                                 foreach($taginfo as $tagged) {
481                 
482                                         $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
483
484                                         $arr = array();
485
486                                         $arr['uid']           = $page_owner_uid;
487                                         $arr['uri']           = $uri;
488                                         $arr['parent-uri']    = $uri;
489                                         $arr['type']          = 'activity';
490                                         $arr['wall']          = 1;
491                                         $arr['contact-id']    = $owner_record['id'];
492                                         $arr['owner-name']    = $owner_record['name'];
493                                         $arr['owner-link']    = $owner_record['url'];
494                                         $arr['owner-avatar']  = $owner_record['thumb'];
495                                         $arr['author-name']   = $owner_record['name'];
496                                         $arr['author-link']   = $owner_record['url'];
497                                         $arr['author-avatar'] = $owner_record['thumb'];
498                                         $arr['title']         = '';
499                                         $arr['allow_cid']     = $p[0]['allow_cid'];
500                                         $arr['allow_gid']     = $p[0]['allow_gid'];
501                                         $arr['deny_cid']      = $p[0]['deny_cid'];
502                                         $arr['deny_gid']      = $p[0]['deny_gid'];
503                                         $arr['last-child']    = 1;
504                                         $arr['visible']       = 1;
505                                         $arr['verb']          = ACTIVITY_TAG;
506                                         $arr['object-type']   = ACTIVITY_OBJ_PERSON;
507                                         $arr['target-type']   = ACTIVITY_OBJ_PHOTO;
508                                         $arr['tag']           = $tagged[4];
509                                         $arr['inform']        = $tagged[2];
510
511                                         $arr['body']          = '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]' . ' ' . t('was tagged in a') . ' ' . '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . t('photo') . '[/url]' . ' ' . t('by') . ' ' . '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]' ;
512                                         $arr['body'] .= "\n\n" . '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . '[img]' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.jpg' . '[/img][/url]' . "\n" ;
513
514                                         $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>';
515                                         $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n");
516                                         if($tagged[3])
517                                                 $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $tagged[3]['photo'] . '" />' . "\n");
518                                         $arr['object'] .= '</link></object>' . "\n";
519
520                                         $arr['target'] = '<target><type>' . ACTIVITY_OBJ_PHOTO . '</type><title>' . $p[0]['desc'] . '</title><id>'
521                                                 . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '</id>';
522                                         $arr['target'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '" />' . "\n" . '<link rel="preview" type="image/jpeg" href="' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.jpg' . '" />') . '</link></target>';
523
524                                         $item_id = item_store($arr);
525                                         if($item_id) {
526                                                 q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
527                                                         dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
528                                                         intval($page_owner_uid),
529                                                         intval($item_id)
530                                                 );
531
532                                                 proc_run('php',"include/notifier.php","tag","$item_id");
533                                         }
534                                 }
535
536                         }
537
538                 }
539                 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
540                 return; // NOTREACHED
541         }
542
543
544         /**
545          * default post action - upload a photo
546          */
547
548         call_hooks('photo_post_init', $_POST);
549
550         /**
551          * Determine the album to use
552          */
553
554         $album    = notags(trim($_REQUEST['album']));
555         $newalbum = notags(trim($_REQUEST['newalbum']));
556
557         logger('mod/photos.php: photos_post(): album= ' . $album . ' newalbum= ' . $newalbum , 'LOGGER_DEBUG');
558
559         if(! strlen($album)) {
560                 if(strlen($newalbum))
561                         $album = $newalbum;
562                 else
563                         $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
564         }
565
566         /**
567          *
568          * We create a wall item for every photo, but we don't want to
569          * overwhelm the data stream with a hundred newly uploaded photos.
570          * So we will make the first photo uploaded to this album in the last several hours
571          * visible by default, the rest will become visible over time when and if
572          * they acquire comments, likes, dislikes, and/or tags 
573          *
574          */
575
576         $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR ",
577                 dbesc($album),
578                 intval($page_owner_uid)
579         );
580         if((! count($r)) || ($album == t('Profile Photos')))
581                 $visible = 1;
582         else
583                 $visible = 0;
584
585         $str_group_allow   = perms2str(((is_array($_REQUEST['group_allow']))   ? $_REQUEST['group_allow']   : explode(',',$_REQUEST['group_allow'])));
586         $str_contact_allow = perms2str(((is_array($_REQUEST['contact_allow'])) ? $_REQUEST['contact_allow'] : explode(',',$_REQUEST['contact_allow'])));
587         $str_group_deny    = perms2str(((is_array($_REQUEST['group_deny']))    ? $_REQUEST['group_deny']    : explode(',',$_REQUEST['group_deny'])));
588         $str_contact_deny  = perms2str(((is_array($_REQUEST['contact_deny']))  ? $_REQUEST['contact_deny']  : explode(',',$_REQUEST['contact_deny'])));
589
590         $ret = array('src' => '', 'filename' => '', 'filesize' => 0);
591
592         call_hooks('photo_post_file',$ret);
593
594         if(x($ret,'src') && x($ret,'filesize')) {
595                 $src      = $ret['src'];
596                 $filename = $ret['filename'];
597                 $filesize = $ret['filesize'];
598         }
599         else {
600                 $src        = $_FILES['userfile']['tmp_name'];
601                 $filename   = basename($_FILES['userfile']['name']);
602                 $filesize   = intval($_FILES['userfile']['size']);
603         }
604
605
606         logger('photos: upload: received file: ' . $filename . ' as ' . $src . ' ' . $filesize . ' bytes', LOGGER_DEBUG);
607
608         $maximagesize = get_config('system','maximagesize');
609
610         if(($maximagesize) && ($filesize > $maximagesize)) {
611                 notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
612                 @unlink($src);
613                 $foo = 0;
614                 call_hooks('photo_post_end',$foo);
615                 return;
616         }
617
618         if(! $filesize) {
619                 notice( t('Image file is empty.') . EOL);
620                 @unlink($src);
621                 $foo = 0;
622                 call_hooks('photo_post_end',$foo);
623                 return;
624         }
625
626         logger('mod/photos.php: photos_post(): loading the contents of ' . $src , 'LOGGER_DEBUG');
627
628         $imagedata = @file_get_contents($src);
629         $ph = new Photo($imagedata);
630
631         if(! $ph->is_valid()) {
632                 logger('mod/photos.php: photos_post(): unable to process image' , 'LOGGER_DEBUG');
633                 notice( t('Unable to process image.') . EOL );
634                 @unlink($src);
635                 $foo = 0;
636                 call_hooks('photo_post_end',$foo);
637                 killme();
638         }
639
640         @unlink($src);
641
642         $width  = $ph->getWidth();
643         $height = $ph->getHeight();
644
645         $smallest = 0;
646
647         $photo_hash = photo_new_resource();
648
649         $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);
650
651         if(! $r) {
652                 logger('mod/photos.php: photos_post(): image store failed' , 'LOGGER_DEBUG');
653                 notice( t('Image upload failed.') . EOL );
654                 killme();
655         }
656
657         if($width > 640 || $height > 640) {
658                 $ph->scaleImage(640);
659                 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
660                 $smallest = 1;
661         }
662
663         if($width > 320 || $height > 320) {
664                 $ph->scaleImage(320);
665                 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
666                 $smallest = 2;
667         }
668         
669         $basename = basename($filename);
670         $uri = item_new_uri($a->get_hostname(), $page_owner_uid);
671
672         // Create item container
673
674         $arr = array();
675
676         $arr['uid']           = $page_owner_uid;
677         $arr['uri']           = $uri;
678         $arr['parent-uri']    = $uri;
679         $arr['type']          = 'photo';
680         $arr['wall']          = 1;
681         $arr['resource-id']   = $photo_hash;
682         $arr['contact-id']    = $owner_record['id'];
683         $arr['owner-name']    = $owner_record['name'];
684         $arr['owner-link']    = $owner_record['url'];
685         $arr['owner-avatar']  = $owner_record['thumb'];
686         $arr['author-name']   = $owner_record['name'];
687         $arr['author-link']   = $owner_record['url'];
688         $arr['author-avatar'] = $owner_record['thumb'];
689         $arr['title']         = '';
690         $arr['allow_cid']     = $str_contact_allow;
691         $arr['allow_gid']     = $str_group_allow;
692         $arr['deny_cid']      = $str_contact_deny;
693         $arr['deny_gid']      = $str_group_deny;
694         $arr['last-child']    = 1;
695         $arr['visible']       = $visible;
696         $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']' 
697                                 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]' 
698                                 . '[/url]';
699
700         $item_id = item_store($arr);
701
702         if($item_id) {
703                 q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
704                         dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
705                         intval($page_owner_uid),
706                         intval($item_id)
707                 );
708         }
709         
710         if($visible) 
711                 proc_run('php', "include/notifier.php", 'wall-new', $item_id);
712
713         call_hooks('photo_post_end',intval($item_id));
714
715         // addon uploaders should call "killme()" [e.g. exit] within the photo_post_end hook
716         // if they do not wish to be redirected
717
718         goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
719         // NOTREACHED
720 }
721
722
723
724 function photos_content(&$a) {
725
726         // URLs:
727         // photos/name
728         // photos/name/upload
729         // photos/name/album/xxxxx
730         // photos/name/album/xxxxx/edit
731         // photos/name/image/xxxxx
732         // photos/name/image/xxxxx/edit
733
734
735         if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
736                 notice( t('Public access denied.') . EOL);
737                 return;
738         }
739
740
741         require_once('include/bbcode.php');
742         require_once('include/security.php');
743         require_once('include/conversation.php');
744
745         if(! x($a->data,'user')) {
746                 notice( t('No photos selected') . EOL );
747                 return;
748         }
749
750         $_SESSION['photo_return'] = $a->cmd;
751
752         //
753         // Parse arguments 
754         //
755
756         if($a->argc > 3) {
757                 $datatype = $a->argv[2];
758                 $datum = $a->argv[3];
759         }
760         elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
761                 $datatype = 'upload';
762         else
763                 $datatype = 'summary';
764
765         if($a->argc > 4)
766                 $cmd = $a->argv[4];
767         else
768                 $cmd = 'view';
769
770         //
771         // Setup permissions structures
772         //
773
774         $can_post       = false;
775         $visitor        = 0;
776         $contact        = null;
777         $remote_contact = false;
778
779         $owner_uid = $a->data['user']['uid'];
780
781         $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
782
783         if((local_user()) && (local_user() == $owner_uid))
784                 $can_post = true;
785         else {
786                 if($community_page && remote_user()) {
787                         $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
788                                 intval(remote_user()),
789                                 intval($owner_uid)
790                         );
791                         if(count($r)) {
792                                 $can_post = true;
793                                 $contact  = $r[0];
794                                 $remote_contact = true;
795                                 $visitor = remote_user();
796                         }
797                 }
798         }
799
800         // perhaps they're visiting - but not a community page, so they wouldn't have write access
801
802         if(remote_user() && (! $visitor)) {
803                 $contact_id = $_SESSION['visitor_id'];
804                 $groups = init_groups_visitor($contact_id);
805                 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
806                         intval(remote_user()),
807                         intval($owner_uid)
808                 );
809                 if(count($r)) {
810                         $contact = $r[0];
811                         $remote_contact = true;
812                 }
813         }
814
815         if(! $remote_contact) {
816                 if(local_user()) {
817                         $contact_id = $_SESSION['cid'];
818                         $contact = $a->contact;
819                 }
820         }
821
822         if($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (! $remote_contact)) {
823                 notice( t('Access to this item is restricted.') . EOL);
824                 return;
825         }
826
827         $sql_extra = permissions_sql($owner_uid,$remote_contact,$groups);
828
829         $o = "";
830
831         // tabs
832         $tpl = get_markup_template('profile_tabs.tpl');
833         $_is_owner = (local_user() && (local_user() == $owner_uid));
834         $o .= replace_macros($tpl,array(
835                 '$url' => $a->get_baseurl() . '/profile/' .$a->data['user']['nickname'],
836                 '$phototab' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'],
837                 '$status' => t('Status'),
838                 '$profile' => t('Profile'),
839                 '$photos' => t('Photos'),
840                 '$events' => (($_is_owner) ? t('Events') : ''),
841                 '$notes' => (($_is_owner) ?     t('Personal Notes') : ''),
842                 '$activetab' => "photos",
843         ));     
844
845         //
846         // dispatch request
847         //
848
849
850         if($datatype === 'upload') {
851                 if(! ($can_post)) {
852                         notice( t('Permission denied.'));
853                         return;
854                 }
855                 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
856
857                 $albumselect .= '<option value="" selected="selected" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
858                 if(count($a->data['albums'])) {
859                         foreach($a->data['albums'] as $album) {
860                                 if(($album['album'] === '') || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
861                                         continue;
862                                 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
863                         }
864                 }
865
866                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
867
868                 $albumselect .= '</select>';
869
870                 $uploader = '';
871
872                 $ret = array('post_url' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'],
873                                 'addon_text' => $uploader,
874                                 'default_upload' => true);
875
876
877                 call_hooks('photo_upload_form',$ret);
878
879                 $default_upload = '<input type="file" name="userfile" />        <div class="photos-upload-submit-wrapper" >
880                 <input type="submit" name="submit" value="' . t('Submit') . '" id="photos-upload-submit" /> </div>';
881
882
883  
884
885                 $tpl = get_markup_template('photos_upload.tpl');
886                 $o .= replace_macros($tpl,array(
887                         '$pagename' => t('Upload Photos'),
888                         '$sessid' => session_id(),
889                         '$nickname' => $a->data['user']['nickname'],
890                         '$newalbum' => t('New album name: '),
891                         '$existalbumtext' => t('or existing album name: '),
892                         '$albumselect' => $albumselect,
893                         '$permissions' => t('Permissions'),
894                         '$aclselect' => (($visitor) ? '' : populate_acl($a->user, $celeb)),
895                         '$uploader' => $ret['addon_text'],
896                         '$default' => (($ret['default_upload']) ? $default_upload : ''),
897                         '$uploadurl' => $ret['post_url']
898
899                 ));
900
901                 return $o; 
902         }
903
904         if($datatype === 'album') {
905
906                 $album = hex2bin($datum);
907
908                 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
909                         AND `scale` <= 4 $sql_extra GROUP BY `resource-id`",
910                         intval($owner_uid),
911                         dbesc($album)
912                 );
913                 if(count($r)) {
914                         $a->set_pager_total(count($r));
915                         $a->set_pager_itemspage(20);
916                 }
917
918                 $r = q("SELECT `resource-id`, `id`, `filename`, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s' 
919                         AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
920                         intval($owner_uid),
921                         dbesc($album),
922                         intval($a->pager['start']),
923                         intval($a->pager['itemspage'])
924                 );
925
926                 $o .= '<h3>' . $album . '</h3>';
927                 
928                 if($cmd === 'edit') {           
929                         if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
930                                 if($can_post) {
931                                         $edit_tpl = get_markup_template('album_edit.tpl');
932                                         $o .= replace_macros($edit_tpl,array(
933                                                 '$nametext' => t('New album name: '),
934                                                 '$nickname' => $a->data['user']['nickname'],
935                                                 '$album' => $album,
936                                                 '$hexalbum' => bin2hex($album),
937                                                 '$submit' => t('Submit'),
938                                                 '$dropsubmit' => t('Delete Album')
939                                         ));
940                                 }
941                         }
942                 }
943                 else {
944                         if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
945                                 if($can_post) {
946                                         $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/' 
947                                                 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">' 
948                                                 . t('Edit Album') . '</a></div>';
949                                 }
950                         }
951                 }
952                 $tpl = get_markup_template('photo_album.tpl');
953                 if(count($r))
954                         foreach($r as $rr) {
955                                 $o .= replace_macros($tpl,array(
956                                         '$id' => $rr['id'],
957                                         '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
958                                         '$phototitle' => t('View Photo'),
959                                         '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
960                                         '$imgalt' => $rr['filename'],
961                                         '$desc'=> $rr['desc']
962                                 ));
963
964                 }
965                 $o .= '<div id="photo-album-end"></div>';
966                 $o .= paginate($a);
967
968                 return $o;
969
970         }       
971
972
973         if($datatype === 'image') {
974
975
976
977                 //$o = '';
978                 // fetch image, item containing image, then comments
979
980                 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' 
981                         $sql_extra ORDER BY `scale` ASC ",
982                         intval($owner_uid),
983                         dbesc($datum)
984                 );
985
986                 if(! count($ph)) {
987                         notice( t('Photo not available') . EOL );
988                         return;
989                 }
990
991                 $prevlink = '';
992                 $nextlink = '';
993
994                 $prvnxt = q("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0 
995                         $sql_extra ORDER BY `created` DESC ",
996                         dbesc($ph[0]['album']),
997                         intval($owner_uid)
998                 ); 
999
1000                 if(count($prvnxt)) {
1001                         for($z = 0; $z < count($prvnxt); $z++) {
1002                                 if($prvnxt[$z]['resource-id'] == $ph[0]['resource-id']) {
1003                                         $prv = $z - 1;
1004                                         $nxt = $z + 1;
1005                                         if($prv < 0)
1006                                                 $prv = count($prvnxt) - 1;
1007                                         if($nxt >= count($prvnxt))
1008                                                 $nxt = 0;
1009                                         break;
1010                                 }
1011                         }
1012                         $prevlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] ;
1013                         $nextlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] ;
1014                 }
1015
1016
1017                 if(count($ph) == 1)
1018                         $hires = $lores = $ph[0];
1019                 if(count($ph) > 1) {
1020                         if($ph[1]['scale'] == 2) {
1021                                 // original is 640 or less, we can display it directly
1022                                 $hires = $lores = $ph[0];
1023                         }
1024                         else {
1025                         $hires = $ph[0];
1026                         $lores = $ph[1];
1027                         }
1028                 }
1029
1030                 $album_link = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']);
1031                 $tools = Null;
1032                 $lock = Null;
1033  
1034                 if($can_post && ($ph[0]['uid'] == $owner_uid)) {
1035                         $tools = array(
1036                                 'edit'  => array($a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $datum . '/edit', t('Edit photo')),
1037                                 'profile'=>array($a->get_baseurl() . '/profile_photo/use/'.$ph[0]['resource-id'], t('Use as profile photo')),
1038                         );
1039
1040                         // lock
1041                         $lock = ( ( ($ph[0]['uid'] == local_user()) && (strlen($ph[0]['allow_cid']) || strlen($ph[0]['allow_gid']) 
1042                                         || strlen($ph[0]['deny_cid']) || strlen($ph[0]['deny_gid'])) ) 
1043                                         ? t('Private Message')
1044                                         : Null);
1045                         
1046                         
1047                 }
1048
1049                 if($prevlink)
1050                         $prevlink = array($prevlink, '<div class="icon prev"></div>') ;
1051
1052                 $photo = array(
1053                         'href' => $a->get_baseurl() . '/photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg',
1054                         'title'=> t('View Full Size'),
1055                         'src'  => $a->get_baseurl() . '/photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg'
1056                 );
1057
1058                 if($nextlink)
1059                         $nextlink = array($nextlink, '<div class="icon next"></div>');
1060
1061
1062                 // Do we have an item for this photo?
1063
1064                 $linked_items = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
1065                         dbesc($datum)
1066                 );
1067                 if(count($linked_items)) {
1068                         $link_item = $linked_items[0];
1069                         $r = q("SELECT COUNT(*) AS `total`
1070                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
1071                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
1072                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1073                                 AND `item`.`uid` = %d 
1074                                 $sql_extra ",
1075                                 dbesc($link_item['uri']),
1076                                 dbesc($link_item['uri']),
1077                                 intval($link_item['uid'])
1078
1079                         );
1080
1081                         if(count($r))
1082                                 $a->set_pager_total($r[0]['total']);
1083
1084
1085                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
1086                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`, 
1087                                 `contact`.`rel`, `contact`.`thumb`, `contact`.`self`, 
1088                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
1089                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
1090                                 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
1091                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1092                                 AND `item`.`uid` = %d
1093                                 $sql_extra
1094                                 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
1095                                 dbesc($link_item['uri']),
1096                                 dbesc($link_item['uri']),
1097                                 intval($link_item['uid']),
1098                                 intval($a->pager['start']),
1099                                 intval($a->pager['itemspage'])
1100
1101                         );
1102                 
1103                         if((local_user()) && (local_user() == $link_item['uid'])) {
1104                                 q("UPDATE `item` SET `unseen` = 0 WHERE `parent` = %d and `uid` = %d",
1105                                         intval($link_item['parent']),
1106                                         intval(local_user())
1107                                 );
1108                         }
1109                 }
1110
1111                 $tags=Null;
1112
1113                 if(count($linked_items) && strlen($link_item['tag'])) {
1114                         $arr = explode(',',$link_item['tag']);
1115                         // parse tags and add links
1116                         $tag_str = '';
1117                         foreach($arr as $t) {
1118                                 if(strlen($tag_str))
1119                                         $tag_str .= ', ';
1120                                 $tag_str .= bbcode($t);
1121                         } 
1122                         $tags = array(t('Tags: '), $tag_str);
1123                         if($cmd === 'edit') {
1124                                 $tags[] = $a->get_baseurl() . '/tagrm/' . $link_item['id'];
1125                                 $tags[] = t('[Remove any tag]');
1126                         }
1127                 }
1128
1129
1130                 $edit = Null;
1131                 if(($cmd === 'edit') && ($can_post)) {
1132                         $edit_tpl = get_markup_template('photo_edit.tpl');
1133                         $edit = replace_macros($edit_tpl, array(
1134                                 '$id' => $ph[0]['id'],
1135                                 '$album' => $ph[0]['album'],
1136                                 '$newalbum' => t('New album name'), 
1137                                 '$nickname' => $a->data['user']['nickname'],
1138                                 '$resource_id' => $ph[0]['resource-id'],
1139                                 '$capt_label' => t('Caption'),
1140                                 '$caption' => $ph[0]['desc'],
1141                                 '$tag_label' => t('Add a Tag'),
1142                                 '$tags' => $link_item['tag'],
1143                                 '$permissions' => t('Permissions'),
1144                                 '$aclselect' => populate_acl($ph[0]),
1145                                 '$help_tags' => t('Example: @bob, @Barbara_Jensen, @jim@example.com, #California, #camping'),
1146                                 '$item_id' => ((count($linked_items)) ? $link_item['id'] : 0),
1147                                 '$submit' => t('Submit'),
1148                                 '$delete' => t('Delete Photo')
1149                         ));
1150                 }
1151
1152                 if(count($linked_items)) {
1153
1154                         $cmnt_tpl = get_markup_template('comment_item.tpl');
1155                         $tpl = get_markup_template('photo_item.tpl');
1156                         $return_url = $a->cmd;
1157
1158                         $like_tpl = get_markup_template('like_noshare.tpl');
1159
1160                         $likebuttons = '';
1161
1162                         if($can_post || can_write_wall($a,$owner_uid)) {
1163                                 $likebuttons = replace_macros($like_tpl,array(
1164                                         '$id' => $link_item['id'],
1165                                         '$likethis' => t("I like this \x28toggle\x29"),
1166                                         '$nolike' => t("I don't like this \x28toggle\x29"),
1167                                         '$share' => t('Share'),
1168                                         '$wait' => t('Please wait')
1169                                 ));
1170                         }
1171
1172                         $comments = '';
1173                         if(! count($r)) {
1174                                 if($can_post || can_write_wall($a,$owner_uid)) {
1175                                         if($link_item['last-child']) {
1176                                                 $comments .= replace_macros($cmnt_tpl,array(
1177                                                         '$return_path' => '', 
1178                                                         '$jsreload' => $return_url,
1179                                                         '$type' => 'wall-comment',
1180                                                         '$id' => $link_item['id'],
1181                                                         '$parent' => $link_item['id'],
1182                                                         '$profile_uid' =>  $owner_uid,
1183                                                         '$mylink' => $contact['url'],
1184                                                         '$mytitle' => t('This is you'),
1185                                                         '$myphoto' => $contact['thumb'],
1186                                                         '$comment' => t('Comment'),
1187                                                         '$submit' => t('Submit'),
1188                                                         '$ww' => ''
1189                                                 ));
1190                                         }
1191                                 }
1192                         }
1193
1194                         $alike = array();
1195                         $dlike = array();
1196                         
1197                         $like = '';
1198                         $dislike = '';
1199
1200                         // display comments
1201                         if(count($r)) {
1202
1203                                 foreach($r as $item) {
1204                                         like_puller($a,$item,$alike,'like');
1205                                         like_puller($a,$item,$dlike,'dislike');
1206                                 }
1207
1208                                 $like    = ((isset($alike[$link_item['id']])) ? format_like($alike[$link_item['id']],$alike[$link_item['id'] . '-l'],'like',$link_item['id']) : '');
1209                                 $dislike = ((isset($dlike[$link_item['id']])) ? format_like($dlike[$link_item['id']],$dlike[$link_item['id'] . '-l'],'dislike',$link_item['id']) : '');
1210
1211
1212
1213                                 if($can_post || can_write_wall($a,$owner_uid)) {
1214                                         if($link_item['last-child']) {
1215                                                 $comments .= replace_macros($cmnt_tpl,array(
1216                                                         '$return_path' => '',
1217                                                         '$jsreload' => $return_url,
1218                                                         '$type' => 'wall-comment',
1219                                                         '$id' => $link_item['id'],
1220                                                         '$parent' => $link_item['id'],
1221                                                         '$profile_uid' =>  $owner_uid,
1222                                                         '$mylink' => $contact['url'],
1223                                                         '$mytitle' => t('This is you'),
1224                                                         '$myphoto' => $contact['thumb'],
1225                                                         '$comment' => t('Comment'),
1226                                                         '$submit' => t('Submit'),
1227                                                         '$ww' => ''
1228                                                 ));
1229                                         }
1230                                 }
1231
1232
1233                                 foreach($r as $item) {
1234                                         $comment = '';
1235                                         $template = $tpl;
1236                                         $sparkle = '';
1237
1238                                         if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) && ($item['id'] != $item['parent']))
1239                                                 continue;
1240
1241                                         $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
1242                         
1243                                         if($can_post || can_write_wall($a,$owner_uid)) {
1244
1245                                                 if($item['last-child']) {
1246                                                         $comments .= replace_macros($cmnt_tpl,array(
1247                                                                 '$return_path' => '',
1248                                                                 '$jsreload' => $return_url,
1249                                                                 '$type' => 'wall-comment',
1250                                                                 '$id' => $item['item_id'],
1251                                                                 '$parent' => $item['parent'],
1252                                                                 '$profile_uid' =>  $owner_uid,
1253                                                                 '$mylink' => $contact['url'],
1254                                                                 '$mytitle' => t('This is you'),
1255                                                                 '$myphoto' => $contact['thumb'],
1256                                                                 '$comment' => t('Comment'),
1257                                                                 '$submit' => t('Submit'),
1258                                                                 '$ww' => ''
1259                                                         ));
1260                                                 }
1261                                         }
1262
1263
1264                                         if(local_user() && ($item['contact-uid'] == local_user()) 
1265                                                 && ($item['network'] == 'dfrn') && (! $item['self'] )) {
1266                                                 $profile_url = $redirect_url;
1267                                                 $sparkle = ' sparkle';
1268                                         }
1269                                         else {
1270                                                 $profile_url = $item['url'];
1271                                                 $sparkle = '';
1272                                         }
1273  
1274                                         $diff_author = (($item['url'] !== $item['author-link']) ? true : false);
1275
1276                                         $profile_name   = (((strlen($item['author-name']))   && $diff_author) ? $item['author-name']   : $item['name']);
1277                                         $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $item['thumb']);
1278
1279                                         $profile_link = $profile_url;
1280
1281                                         $drop = '';
1282
1283                                         if(($item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
1284                                                 $drop = replace_macros(get_markup_template('photo_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
1285
1286
1287                                         $comments .= replace_macros($template,array(
1288                                                 '$id' => $item['item_id'],
1289                                                 '$profile_url' => $profile_link,
1290                                                 '$name' => $profile_name,
1291                                                 '$thumb' => $profile_avatar,
1292                                                 '$sparkle' => $sparkle,
1293                                                 '$title' => $item['title'],
1294                                                 '$body' => bbcode($item['body']),
1295                                                 '$ago' => relative_date($item['created']),
1296                                                 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
1297                                                 '$drop' => $drop,
1298                                                 '$comment' => $comment
1299                                         ));
1300                                 }
1301                         }
1302
1303                         $paginate = paginate($a);
1304                 }
1305                 
1306                 $photo_tpl = get_markup_template('photo_view.tpl');
1307                 $o .= replace_macros($photo_tpl, array(
1308                         '$id' => $ph[0]['id'],
1309                         '$album' => array($album_link,$ph[0]['album']),
1310                         '$tools' => $tools,
1311                         '$lock' => $lock,
1312                         '$photo' => $photo,
1313                         '$prevlink' => $prevlink,
1314                         '$nextlink' => $nextlink,
1315                         '$desc' => $ph[0]['desc'],
1316                         '$tags' => $tags,
1317                         '$edit' => $edit,       
1318                         '$likebuttons' => $likebuttons,
1319                         '$like' => $like,
1320                         '$dislike' => $dislike,
1321                         '$comments' => $comments,
1322                         '$paginate' => $paginate,
1323                 ));
1324                 
1325                 return $o;
1326         }
1327
1328         // Default - show recent photos with upload link (if applicable)
1329         //$o = '';
1330
1331         $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s' 
1332                 $sql_extra GROUP BY `resource-id`",
1333                 intval($a->data['user']['uid']),
1334                 dbesc('Contact Photos'),
1335                 dbesc( t('Contact Photos'))
1336         );
1337         if(count($r)) {
1338                 $a->set_pager_total(count($r));
1339                 $a->set_pager_itemspage(20);
1340         }
1341
1342         $r = q("SELECT `resource-id`, `id`, `filename`, `album`, max(`scale`) AS `scale` FROM `photo`
1343                 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'  
1344                 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
1345                 intval($a->data['user']['uid']),
1346                 dbesc('Contact Photos'),
1347                 dbesc( t('Contact Photos')),
1348                 intval($a->pager['start']),
1349                 intval($a->pager['itemspage'])
1350         );
1351
1352         $o .= '<h3>' . t('Recent Photos') . '</h3>';
1353
1354         if($can_post) {
1355                 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/' 
1356                         . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
1357         }
1358
1359         $tpl = get_markup_template('photo_top.tpl');
1360         if(count($r)) {
1361                 foreach($r as $rr) {
1362                         $o .= replace_macros($tpl,array(
1363                                 '$id'         => $rr['id'],
1364                                 '$photolink'  => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
1365                                 '$phototitle' => t('View Photo'),
1366                                 '$imgsrc'     => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.jpg',
1367                                 '$albumlink'  => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
1368                                 '$albumname'  => $rr['album'],
1369                                 '$albumalt'   => t('View Album'),
1370                                 '$imgalt'     => $rr['filename']
1371                         ));
1372
1373                 }
1374                 $o .= '<div id="photo-top-end"></div>';
1375         }
1376         $o .= paginate($a);
1377         return $o;
1378 }
1379