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');
8 function photos_init(&$a) {
11 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
18 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 LIMIT 1",
25 $a->data['user'] = $r[0];
27 $sql_extra = permissions_sql($a->data['user']['uid']);
29 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d $sql_extra ",
30 intval($a->data['user']['uid'])
34 $a->data['albums'] = $albums;
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>';
39 $o .= '<h4>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
42 foreach($albums as $album) {
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).
47 if((! strlen($album['album'])) || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
49 $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" />' . $album['album'] . '</a></li>';
54 if(! x($a->page,'aside'))
55 $a->page['aside'] = '';
56 $a->page['aside'] .= $o;
59 $a->page['htmlhead'] .= "<script> var ispublic = '" . t('everybody') . "';" ;
61 $a->page['htmlhead'] .= <<< EOT
63 $(document).ready(function() {
65 $('#contact_allow, #contact_deny, #group_allow, #group_deny').change(function() {
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();
73 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
74 $('#jot-public').show();
90 function photos_post(&$a) {
92 logger('mod/photos.php: photos_post(): begin' , 'LOGGER_DEBUG');
94 foreach($_REQUEST AS $key => $val) {
95 logger('mod/photos.php: photos_post(): $_REQUEST key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
98 foreach($_FILES AS $key => $val) {
99 logger('mod/photos.php: photos_post(): $_FILES key: ' . $key . ' val: ' . $val , 'LOGGER_DEBUG');
105 $page_owner_uid = $a->data['user']['uid'];
106 $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
108 if((local_user()) && (local_user() == $page_owner_uid))
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)
118 $visitor = remote_user();
124 notice( t('Permission denied.') . EOL );
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)
134 notice( t('Contact information unavailable') . EOL);
135 logger('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
139 $owner_record = $r[0];
142 if(($a->argc > 3) && ($a->argv[2] === 'album')) {
143 $album = hex2bin($a->argv[3]);
145 if($album === t('Profile Photos') || $album === 'Contact Photos' || $album === t('Contact Photos')) {
146 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
147 return; // NOTREACHED
150 $r = q("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
152 intval($page_owner_uid)
155 notice( t('Album not found.') . EOL);
156 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
157 return; // NOTREACHED
160 $newalbum = notags(trim($_POST['albumname']));
161 if($newalbum != $album) {
162 q("UPDATE `photo` SET `album` = '%s' WHERE `album` = '%s' AND `uid` = %d",
165 intval($page_owner_uid)
167 $newurl = str_replace(bin2hex($album),bin2hex($newalbum),$_SESSION['photo_return']);
168 goaway($a->get_baseurl() . '/' . $newurl);
169 return; // NOTREACHED
173 if($_POST['dropalbum'] == t('Delete Album')) {
177 // get the list of photos we are about to delete
180 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `album` = '%s'",
182 intval($page_owner_uid),
187 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'",
188 intval(local_user()),
194 $res[] = "'" . dbesc($rr['rid']) . "'" ;
198 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
199 return; // NOTREACHED
202 $str_res = implode(',', $res);
204 // remove the associated photos
206 q("DELETE FROM `photo` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
207 intval($page_owner_uid)
210 // find and delete the corresponding item with all the comments and likes/dislikes
212 $r = q("SELECT `parent-uri` FROM `item` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
213 intval($page_owner_uid)
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)
223 $drop_id = intval($rr['id']);
225 // send the notification upstream/downstream as the case may be
228 proc_run('php',"include/notifier.php","drop","$drop_id");
232 goaway($a->get_baseurl() . '/photos/' . $a->data['user']['nickname']);
233 return; // NOTREACHED
236 if(($a->argc > 2) && (x($_POST,'delete')) && ($_POST['delete'] == t('Delete Photo'))) {
238 // same as above but remove single photo
241 $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `resource-id` = '%s' LIMIT 1",
243 intval($page_owner_uid),
248 $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' LIMIT 1",
249 intval(local_user()),
254 q("DELETE FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
255 intval($page_owner_uid),
256 dbesc($r[0]['resource-id'])
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)
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()),
267 intval($page_owner_uid)
270 $url = $a->get_baseurl();
271 $drop_id = intval($i[0]['id']);
274 proc_run('php',"include/notifier.php","drop","$drop_id");
278 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
279 return; // NOTREACHED
282 if(($a->argc > 2) && ((x($_POST,'desc') !== false) || (x($_POST,'newtag') !== false)) || (x($_POST,'albname') !== false)) {
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']);
293 $resource_id = $a->argv[2];
295 if(! strlen($albname))
296 $albname = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
299 $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
301 intval($page_owner_uid)
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",
307 dbesc($str_contact_allow),
308 dbesc($str_group_allow),
309 dbesc($str_contact_deny),
310 dbesc($str_group_deny),
312 intval($page_owner_uid)
316 /* Don't make the item visible if the only change was the album name */
319 if($p[0]['desc'] !== $desc || strlen($rawtags))
324 // Create item container
327 $basename = basename($filename);
328 $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
332 $arr['uid'] = $page_owner_uid;
334 $arr['parent-uri'] = $uri;
335 $arr['type'] = 'photo';
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;
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]'
357 $item_id = item_store($arr);
362 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
364 intval($page_owner_uid)
368 $old_tag = $r[0]['tag'];
369 $old_inform = $r[0]['inform'];
372 if(strlen($rawtags)) {
377 // if the new tag doesn't have a namespace specifier (@foo or #foo) give it a hashtag
379 $x = substr($rawtags,0,1);
380 if($x !== '@' && $x !== '#')
381 $rawtags = '#' . $rawtags;
384 $tags = get_tags($rawtags);
387 foreach($tags as $tag) {
390 if(strpos($tag,'@') === 0) {
391 $name = substr($tag,1);
392 if((strpos($name,'@')) || (strpos($name,'http://'))) {
394 $links = @lrdd($name);
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']);
407 $taginfo[] = array($newname,$profile,$salmon);
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",
415 intval($page_owner_uid)
419 $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
422 intval($page_owner_uid)
426 $newname = $r[0]['name'];
427 $profile = $r[0]['url'];
428 $notify = 'cid:' . $r[0]['id'];
435 if(substr($notify,0,4) === 'cid:')
436 $taginfo[] = array($newname,$profile,$notify,$r[0],'@[url=' . str_replace(',','%2c',$profile) . ']' . $newname . '[/url]');
438 $taginfo[] = array($newname,$profile,$notify,null,$str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]');
439 if(strlen($str_tags))
441 $profile = str_replace(',','%2c',$profile);
442 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
449 if(strlen($newtag) && strlen($str_tags))
451 $newtag .= $str_tags;
453 $newinform = $old_inform;
454 if(strlen($newinform) && strlen($inform))
456 $newinform .= $inform;
458 $r = q("UPDATE `item` SET `tag` = '%s', `inform` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
461 dbesc(datetime_convert()),
462 dbesc(datetime_convert()),
464 intval($page_owner_uid)
468 foreach($p as $scales) {
469 if(intval($scales['scale']) == 2) {
473 if(intval($scales['scale']) == 4) {
479 if(count($taginfo)) {
480 foreach($taginfo as $tagged) {
482 $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
486 $arr['uid'] = $page_owner_uid;
488 $arr['parent-uri'] = $uri;
489 $arr['type'] = 'activity';
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'];
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;
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];
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" ;
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");
517 $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $tagged[3]['photo'] . '" />' . "\n");
518 $arr['object'] .= '</link></object>' . "\n";
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>';
524 $item_id = item_store($arr);
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),
532 proc_run('php',"include/notifier.php","tag","$item_id");
539 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
540 return; // NOTREACHED
545 * default post action - upload a photo
548 call_hooks('photo_post_init', $_POST);
551 * Determine the album to use
554 $album = notags(trim($_REQUEST['album']));
555 $newalbum = notags(trim($_REQUEST['newalbum']));
557 logger('mod/photos.php: photos_post(): album= ' . $album . ' newalbum= ' . $newalbum , 'LOGGER_DEBUG');
559 if(! strlen($album)) {
560 if(strlen($newalbum))
563 $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
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
576 $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR ",
578 intval($page_owner_uid)
580 if((! count($r)) || ($album == t('Profile Photos')))
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'])));
590 $ret = array('src' => '', 'filename' => '', 'filesize' => 0);
592 call_hooks('photo_post_file',$ret);
594 if(x($ret,'src') && x($ret,'filesize')) {
596 $filename = $ret['filename'];
597 $filesize = $ret['filesize'];
600 $src = $_FILES['userfile']['tmp_name'];
601 $filename = basename($_FILES['userfile']['name']);
602 $filesize = intval($_FILES['userfile']['size']);
606 logger('photos: upload: received file: ' . $filename . ' as ' . $src . ' ' . $filesize . ' bytes', LOGGER_DEBUG);
608 $maximagesize = get_config('system','maximagesize');
610 if(($maximagesize) && ($filesize > $maximagesize)) {
611 notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
614 call_hooks('photo_post_end',$foo);
619 notice( t('Image file is empty.') . EOL);
622 call_hooks('photo_post_end',$foo);
626 logger('mod/photos.php: photos_post(): loading the contents of ' . $src , 'LOGGER_DEBUG');
628 $imagedata = @file_get_contents($src);
629 $ph = new Photo($imagedata);
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 );
636 call_hooks('photo_post_end',$foo);
642 $width = $ph->getWidth();
643 $height = $ph->getHeight();
647 $photo_hash = photo_new_resource();
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);
652 logger('mod/photos.php: photos_post(): image store failed' , 'LOGGER_DEBUG');
653 notice( t('Image upload failed.') . EOL );
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);
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);
669 $basename = basename($filename);
670 $uri = item_new_uri($a->get_hostname(), $page_owner_uid);
672 // Create item container
676 $arr['uid'] = $page_owner_uid;
678 $arr['parent-uri'] = $uri;
679 $arr['type'] = 'photo';
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'];
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]'
700 $item_id = item_store($arr);
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),
711 proc_run('php', "include/notifier.php", 'wall-new', $item_id);
713 call_hooks('photo_post_end',intval($item_id));
715 // addon uploaders should call "killme()" [e.g. exit] within the photo_post_end hook
716 // if they do not wish to be redirected
718 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
724 function photos_content(&$a) {
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
735 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
736 notice( t('Public access denied.') . EOL);
741 require_once('include/bbcode.php');
742 require_once('include/security.php');
743 require_once('include/conversation.php');
745 if(! x($a->data,'user')) {
746 notice( t('No photos selected') . EOL );
750 $_SESSION['photo_return'] = $a->cmd;
757 $datatype = $a->argv[2];
758 $datum = $a->argv[3];
760 elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
761 $datatype = 'upload';
763 $datatype = 'summary';
771 // Setup permissions structures
777 $remote_contact = false;
779 $owner_uid = $a->data['user']['uid'];
781 $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
783 if((local_user()) && (local_user() == $owner_uid))
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()),
794 $remote_contact = true;
795 $visitor = remote_user();
800 // perhaps they're visiting - but not a community page, so they wouldn't have write access
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()),
811 $remote_contact = true;
815 if(! $remote_contact) {
817 $contact_id = $_SESSION['cid'];
818 $contact = $a->contact;
822 if($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (! $remote_contact)) {
823 notice( t('Access to this item is restricted.') . EOL);
827 $sql_extra = permissions_sql($owner_uid,$remote_contact,$groups);
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",
850 if($datatype === 'upload') {
852 notice( t('Permission denied.'));
855 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
857 $albumselect .= '<option value="" selected="selected" > </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')))
862 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
866 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
868 $albumselect .= '</select>';
872 $ret = array('post_url' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'],
873 'addon_text' => $uploader,
874 'default_upload' => true);
877 call_hooks('photo_upload_form',$ret);
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>';
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' => template_escape($albumselect),
893 '$permissions' => t('Permissions'),
894 '$aclselect' => (($visitor) ? '' : template_escape(populate_acl($a->user, $celeb))),
895 '$uploader' => $ret['addon_text'],
896 '$default' => (($ret['default_upload']) ? $default_upload : ''),
897 '$uploadurl' => $ret['post_url']
904 if($datatype === 'album') {
906 $album = hex2bin($datum);
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`",
914 $a->set_pager_total(count($r));
915 $a->set_pager_itemspage(20);
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",
922 intval($a->pager['start']),
923 intval($a->pager['itemspage'])
926 $o .= '<h3>' . $album . '</h3>';
928 if($cmd === 'edit') {
929 if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
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' => template_escape($album),
936 '$hexalbum' => bin2hex($album),
937 '$submit' => t('Submit'),
938 '$dropsubmit' => t('Delete Album')
944 if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
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>';
952 $tpl = get_markup_template('photo_album.tpl');
955 $o .= replace_macros($tpl,array(
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' => template_escape($rr['filename']),
961 '$desc'=> template_escape($rr['desc'])
965 $o .= '<div id="photo-album-end"></div>';
973 if($datatype === 'image') {
978 // fetch image, item containing image, then comments
980 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
981 $sql_extra ORDER BY `scale` ASC ",
987 notice( t('Photo not available') . EOL );
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']),
1000 if(count($prvnxt)) {
1001 for($z = 0; $z < count($prvnxt); $z++) {
1002 if($prvnxt[$z]['resource-id'] == $ph[0]['resource-id']) {
1006 $prv = count($prvnxt) - 1;
1007 if($nxt >= count($prvnxt))
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'] ;
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];
1030 $album_link = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']);
1034 if($can_post && ($ph[0]['uid'] == $owner_uid)) {
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')),
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')
1050 $prevlink = array($prevlink, '<div class="icon prev"></div>') ;
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'
1059 $nextlink = array($nextlink, '<div class="icon next"></div>');
1062 // Do we have an item for this photo?
1064 $linked_items = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
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
1075 dbesc($link_item['uri']),
1076 dbesc($link_item['uri']),
1077 intval($link_item['uid'])
1082 $a->set_pager_total($r[0]['total']);
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
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'])
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())
1113 if(count($linked_items) && strlen($link_item['tag'])) {
1114 $arr = explode(',',$link_item['tag']);
1115 // parse tags and add links
1117 foreach($arr as $t) {
1118 if(strlen($tag_str))
1120 $tag_str .= bbcode($t);
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]');
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' => template_escape($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' => template_escape($ph[0]['desc']),
1141 '$tag_label' => t('Add a Tag'),
1142 '$tags' => $link_item['tag'],
1143 '$permissions' => t('Permissions'),
1144 '$aclselect' => template_escape(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')
1152 if(count($linked_items)) {
1154 $cmnt_tpl = get_markup_template('comment_item.tpl');
1155 $tpl = get_markup_template('photo_item.tpl');
1156 $return_url = $a->cmd;
1158 $like_tpl = get_markup_template('like_noshare.tpl');
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')
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'),
1203 foreach($r as $item) {
1204 like_puller($a,$item,$alike,'like');
1205 like_puller($a,$item,$dlike,'dislike');
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']) : '');
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'),
1233 foreach($r as $item) {
1238 if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) && ($item['id'] != $item['parent']))
1241 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
1243 if($can_post || can_write_wall($a,$owner_uid)) {
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'),
1264 if(local_user() && ($item['contact-uid'] == local_user())
1265 && ($item['network'] == 'dfrn') && (! $item['self'] )) {
1266 $profile_url = $redirect_url;
1267 $sparkle = ' sparkle';
1270 $profile_url = $item['url'];
1274 $diff_author = (($item['url'] !== $item['author-link']) ? true : false);
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']);
1279 $profile_link = $profile_url;
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')));
1287 $comments .= replace_macros($template,array(
1288 '$id' => $item['item_id'],
1289 '$profile_url' => $profile_link,
1290 '$name' => template_escape($profile_name),
1291 '$thumb' => $profile_avatar,
1292 '$sparkle' => $sparkle,
1293 '$title' => template_escape($item['title']),
1294 '$body' => template_escape(bbcode($item['body'])),
1295 '$ago' => relative_date($item['created']),
1296 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
1298 '$comment' => $comment
1303 $paginate = paginate($a);
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,template_escape($ph[0]['album'])),
1313 '$prevlink' => $prevlink,
1314 '$nextlink' => $nextlink,
1315 '$desc' => $ph[0]['desc'],
1316 '$tags' => template_escape($tags),
1318 '$likebuttons' => $likebuttons,
1319 '$like' => template_escape($like),
1320 '$dislike' => template_escape($dislike),
1321 '$comments' => $comments,
1322 '$paginate' => $paginate,
1328 // Default - show recent photos with upload link (if applicable)
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'))
1338 $a->set_pager_total(count($r));
1339 $a->set_pager_itemspage(20);
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'])
1352 $o .= '<h3>' . t('Recent Photos') . '</h3>';
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>';
1359 $tpl = get_markup_template('photo_top.tpl');
1361 foreach($r as $rr) {
1362 $o .= replace_macros($tpl,array(
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' => template_escape($rr['album']),
1369 '$albumalt' => t('View Album'),
1370 '$imgalt' => template_escape($rr['filename'])
1374 $o .= '<div id="photo-top-end"></div>';