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 .= '<div class="vcard">';
37 $o .= '<div class="fn">' . $a->data['user']['username'] . '</div>';
38 $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>';
41 $o .= '<div id="side-bar-photos-albums" class="widget">';
42 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h3>';
45 foreach($albums as $album) {
47 // don't show contact photos. We once translated this name, but then you could still access it under
48 // a different language setting. Now we store the name in English and check in English (and translated for legacy albums).
50 if((! strlen($album['album'])) || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
52 $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" >' . $album['album'] . '</a></li>';
56 if(local_user() && $a->data['user']['uid'] == local_user()) {
57 $o .= '<div id="photo-albums-upload-link"><a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/upload" >' .t('Upload New Photos') . '</a></div>';
63 if(! x($a->page,'aside'))
64 $a->page['aside'] = '';
65 $a->page['aside'] .= $o;
68 $a->page['htmlhead'] .= "<script> var ispublic = '" . t('everybody') . "';" ;
70 $a->page['htmlhead'] .= <<< EOT
72 $(document).ready(function() {
74 $('#contact_allow, #contact_deny, #group_allow, #group_deny').change(function() {
76 $('#contact_allow option:selected, #contact_deny option:selected, #group_allow option:selected, #group_deny option:selected').each( function() {
77 selstr = $(this).text();
78 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
79 $('#jot-public').hide();
82 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
83 $('#jot-public').show();
99 function photos_post(&$a) {
101 logger('mod-photos: photos_post: begin' , 'LOGGER_DEBUG');
104 logger('mod_photos: REQUEST ' . print_r($_REQUEST,true), LOGGER_DATA);
105 logger('mod_photos: FILES ' . print_r($_FILES,true), LOGGER_DATA);
110 $page_owner_uid = $a->data['user']['uid'];
111 $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
113 if((local_user()) && (local_user() == $page_owner_uid))
116 if($community_page && remote_user()) {
117 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
118 intval(remote_user()),
119 intval($page_owner_uid)
123 $visitor = remote_user();
129 notice( t('Permission denied.') . EOL );
133 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
134 WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
135 intval($page_owner_uid)
139 notice( t('Contact information unavailable') . EOL);
140 logger('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
144 $owner_record = $r[0];
147 if(($a->argc > 3) && ($a->argv[2] === 'album')) {
148 $album = hex2bin($a->argv[3]);
150 if($album === t('Profile Photos') || $album === 'Contact Photos' || $album === t('Contact Photos')) {
151 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
152 return; // NOTREACHED
155 $r = q("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
157 intval($page_owner_uid)
160 notice( t('Album not found.') . EOL);
161 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
162 return; // NOTREACHED
165 $newalbum = notags(trim($_POST['albumname']));
166 if($newalbum != $album) {
167 q("UPDATE `photo` SET `album` = '%s' WHERE `album` = '%s' AND `uid` = %d",
170 intval($page_owner_uid)
172 $newurl = str_replace(bin2hex($album),bin2hex($newalbum),$_SESSION['photo_return']);
173 goaway($a->get_baseurl() . '/' . $newurl);
174 return; // NOTREACHED
178 if($_POST['dropalbum'] == t('Delete Album')) {
182 // get the list of photos we are about to delete
185 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `album` = '%s'",
187 intval($page_owner_uid),
192 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'",
193 intval(local_user()),
199 $res[] = "'" . dbesc($rr['rid']) . "'" ;
203 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
204 return; // NOTREACHED
207 $str_res = implode(',', $res);
209 // remove the associated photos
211 q("DELETE FROM `photo` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
212 intval($page_owner_uid)
215 // find and delete the corresponding item with all the comments and likes/dislikes
217 $r = q("SELECT `parent-uri` FROM `item` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
218 intval($page_owner_uid)
222 q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
223 dbesc(datetime_convert()),
224 dbesc($rr['parent-uri']),
225 intval($page_owner_uid)
228 $drop_id = intval($rr['id']);
230 // send the notification upstream/downstream as the case may be
233 proc_run('php',"include/notifier.php","drop","$drop_id");
237 goaway($a->get_baseurl() . '/photos/' . $a->data['user']['nickname']);
238 return; // NOTREACHED
241 if(($a->argc > 2) && (x($_POST,'delete')) && ($_POST['delete'] == t('Delete Photo'))) {
243 // same as above but remove single photo
246 $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `resource-id` = '%s' LIMIT 1",
248 intval($page_owner_uid),
253 $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' LIMIT 1",
254 intval(local_user()),
259 q("DELETE FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
260 intval($page_owner_uid),
261 dbesc($r[0]['resource-id'])
263 $i = q("SELECT * FROM `item` WHERE `resource-id` = '%s' AND `uid` = %d LIMIT 1",
264 dbesc($r[0]['resource-id']),
265 intval($page_owner_uid)
268 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
269 dbesc(datetime_convert()),
270 dbesc(datetime_convert()),
272 intval($page_owner_uid)
275 $url = $a->get_baseurl();
276 $drop_id = intval($i[0]['id']);
279 proc_run('php',"include/notifier.php","drop","$drop_id");
283 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
284 return; // NOTREACHED
287 if(($a->argc > 2) && ((x($_POST,'desc') !== false) || (x($_POST,'newtag') !== false)) || (x($_POST,'albname') !== false)) {
289 $desc = ((x($_POST,'desc')) ? notags(trim($_POST['desc'])) : '');
290 $rawtags = ((x($_POST,'newtag')) ? notags(trim($_POST['newtag'])) : '');
291 $item_id = ((x($_POST,'item_id')) ? intval($_POST['item_id']) : 0);
292 $albname = ((x($_POST,'albname')) ? notags(trim($_POST['albname'])) : '');
293 $str_group_allow = perms2str($_POST['group_allow']);
294 $str_contact_allow = perms2str($_POST['contact_allow']);
295 $str_group_deny = perms2str($_POST['group_deny']);
296 $str_contact_deny = perms2str($_POST['contact_deny']);
298 $resource_id = $a->argv[2];
300 if(! strlen($albname))
301 $albname = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
304 $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
306 intval($page_owner_uid)
309 $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",
312 dbesc($str_contact_allow),
313 dbesc($str_group_allow),
314 dbesc($str_contact_deny),
315 dbesc($str_group_deny),
317 intval($page_owner_uid)
321 /* Don't make the item visible if the only change was the album name */
324 if($p[0]['desc'] !== $desc || strlen($rawtags))
329 // Create item container
332 $basename = basename($filename);
333 $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
337 $arr['uid'] = $page_owner_uid;
339 $arr['parent-uri'] = $uri;
340 $arr['type'] = 'photo';
342 $arr['resource-id'] = $p[0]['resource-id'];
343 $arr['contact-id'] = $owner_record['id'];
344 $arr['owner-name'] = $owner_record['name'];
345 $arr['owner-link'] = $owner_record['url'];
346 $arr['owner-avatar'] = $owner_record['thumb'];
347 $arr['author-name'] = $owner_record['name'];
348 $arr['author-link'] = $owner_record['url'];
349 $arr['author-avatar'] = $owner_record['thumb'];
350 $arr['title'] = $title;
351 $arr['allow_cid'] = $p[0]['allow_cid'];
352 $arr['allow_gid'] = $p[0]['allow_gid'];
353 $arr['deny_cid'] = $p[0]['deny_cid'];
354 $arr['deny_gid'] = $p[0]['deny_gid'];
355 $arr['last-child'] = 1;
356 $arr['visible'] = $visibility;
359 $arr['body'] = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']'
360 . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.jpg' . '[/img]'
363 $item_id = item_store($arr);
368 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
370 intval($page_owner_uid)
374 $old_tag = $r[0]['tag'];
375 $old_inform = $r[0]['inform'];
378 if(strlen($rawtags)) {
383 // if the new tag doesn't have a namespace specifier (@foo or #foo) give it a hashtag
385 $x = substr($rawtags,0,1);
386 if($x !== '@' && $x !== '#')
387 $rawtags = '#' . $rawtags;
390 $tags = get_tags($rawtags);
393 foreach($tags as $tag) {
396 if(strpos($tag,'@') === 0) {
397 $name = substr($tag,1);
398 if((strpos($name,'@')) || (strpos($name,'http://'))) {
400 $links = @lrdd($name);
402 foreach($links as $link) {
403 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
404 $profile = $link['@attributes']['href'];
405 if($link['@attributes']['rel'] === 'salmon') {
406 $salmon = '$url:' . str_replace(',','%sc',$link['@attributes']['href']);
413 $taginfo[] = array($newname,$profile,$salmon);
419 if(strrpos($newname,'+'))
420 $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
423 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
428 elseif(strstr($name,'_') || strstr($name,' ')) {
429 $newname = str_replace('_',' ',$name);
430 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
432 intval($page_owner_uid)
436 $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
439 intval($page_owner_uid)
443 $newname = $r[0]['name'];
444 $profile = $r[0]['url'];
445 $notify = 'cid:' . $r[0]['id'];
452 if(substr($notify,0,4) === 'cid:')
453 $taginfo[] = array($newname,$profile,$notify,$r[0],'@[url=' . str_replace(',','%2c',$profile) . ']' . $newname . '[/url]');
455 $taginfo[] = array($newname,$profile,$notify,null,$str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]');
456 if(strlen($str_tags))
458 $profile = str_replace(',','%2c',$profile);
459 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
466 if(strlen($newtag) && strlen($str_tags))
468 $newtag .= $str_tags;
470 $newinform = $old_inform;
471 if(strlen($newinform) && strlen($inform))
473 $newinform .= $inform;
475 $r = q("UPDATE `item` SET `tag` = '%s', `inform` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
478 dbesc(datetime_convert()),
479 dbesc(datetime_convert()),
481 intval($page_owner_uid)
485 foreach($p as $scales) {
486 if(intval($scales['scale']) == 2) {
490 if(intval($scales['scale']) == 4) {
496 if(count($taginfo)) {
497 foreach($taginfo as $tagged) {
499 $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
503 $arr['uid'] = $page_owner_uid;
505 $arr['parent-uri'] = $uri;
506 $arr['type'] = 'activity';
508 $arr['contact-id'] = $owner_record['id'];
509 $arr['owner-name'] = $owner_record['name'];
510 $arr['owner-link'] = $owner_record['url'];
511 $arr['owner-avatar'] = $owner_record['thumb'];
512 $arr['author-name'] = $owner_record['name'];
513 $arr['author-link'] = $owner_record['url'];
514 $arr['author-avatar'] = $owner_record['thumb'];
516 $arr['allow_cid'] = $p[0]['allow_cid'];
517 $arr['allow_gid'] = $p[0]['allow_gid'];
518 $arr['deny_cid'] = $p[0]['deny_cid'];
519 $arr['deny_gid'] = $p[0]['deny_gid'];
520 $arr['last-child'] = 1;
522 $arr['verb'] = ACTIVITY_TAG;
523 $arr['object-type'] = ACTIVITY_OBJ_PERSON;
524 $arr['target-type'] = ACTIVITY_OBJ_PHOTO;
525 $arr['tag'] = $tagged[4];
526 $arr['inform'] = $tagged[2];
528 $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]' ;
529 $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" ;
531 $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>';
532 $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n");
534 $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $tagged[3]['photo'] . '" />' . "\n");
535 $arr['object'] .= '</link></object>' . "\n";
537 $arr['target'] = '<target><type>' . ACTIVITY_OBJ_PHOTO . '</type><title>' . $p[0]['desc'] . '</title><id>'
538 . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '</id>';
539 $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>';
541 $item_id = item_store($arr);
543 q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
544 dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
545 intval($page_owner_uid),
549 proc_run('php',"include/notifier.php","tag","$item_id");
556 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
557 return; // NOTREACHED
562 * default post action - upload a photo
565 call_hooks('photo_post_init', $_POST);
568 * Determine the album to use
571 $album = notags(trim($_REQUEST['album']));
572 $newalbum = notags(trim($_REQUEST['newalbum']));
574 logger('mod/photos.php: photos_post(): album= ' . $album . ' newalbum= ' . $newalbum , 'LOGGER_DEBUG');
576 if(! strlen($album)) {
577 if(strlen($newalbum))
580 $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
585 * We create a wall item for every photo, but we don't want to
586 * overwhelm the data stream with a hundred newly uploaded photos.
587 * So we will make the first photo uploaded to this album in the last several hours
588 * visible by default, the rest will become visible over time when and if
589 * they acquire comments, likes, dislikes, and/or tags
593 $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR ",
595 intval($page_owner_uid)
597 if((! count($r)) || ($album == t('Profile Photos')))
602 if(intval($_REQUEST['not_visible']) || $_REQUEST['not_visible'] === 'true')
605 $str_group_allow = perms2str(((is_array($_REQUEST['group_allow'])) ? $_REQUEST['group_allow'] : explode(',',$_REQUEST['group_allow'])));
606 $str_contact_allow = perms2str(((is_array($_REQUEST['contact_allow'])) ? $_REQUEST['contact_allow'] : explode(',',$_REQUEST['contact_allow'])));
607 $str_group_deny = perms2str(((is_array($_REQUEST['group_deny'])) ? $_REQUEST['group_deny'] : explode(',',$_REQUEST['group_deny'])));
608 $str_contact_deny = perms2str(((is_array($_REQUEST['contact_deny'])) ? $_REQUEST['contact_deny'] : explode(',',$_REQUEST['contact_deny'])));
610 $ret = array('src' => '', 'filename' => '', 'filesize' => 0);
612 call_hooks('photo_post_file',$ret);
614 if(x($ret,'src') && x($ret,'filesize')) {
616 $filename = $ret['filename'];
617 $filesize = $ret['filesize'];
620 $src = $_FILES['userfile']['tmp_name'];
621 $filename = basename($_FILES['userfile']['name']);
622 $filesize = intval($_FILES['userfile']['size']);
626 logger('photos: upload: received file: ' . $filename . ' as ' . $src . ' ' . $filesize . ' bytes', LOGGER_DEBUG);
628 $maximagesize = get_config('system','maximagesize');
630 if(($maximagesize) && ($filesize > $maximagesize)) {
631 notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
634 call_hooks('photo_post_end',$foo);
639 notice( t('Image file is empty.') . EOL);
642 call_hooks('photo_post_end',$foo);
646 logger('mod/photos.php: photos_post(): loading the contents of ' . $src , 'LOGGER_DEBUG');
648 $imagedata = @file_get_contents($src);
649 $ph = new Photo($imagedata);
651 if(! $ph->is_valid()) {
652 logger('mod/photos.php: photos_post(): unable to process image' , 'LOGGER_DEBUG');
653 notice( t('Unable to process image.') . EOL );
656 call_hooks('photo_post_end',$foo);
662 $width = $ph->getWidth();
663 $height = $ph->getHeight();
667 $photo_hash = photo_new_resource();
669 $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);
672 logger('mod/photos.php: photos_post(): image store failed' , 'LOGGER_DEBUG');
673 notice( t('Image upload failed.') . EOL );
677 if($width > 640 || $height > 640) {
678 $ph->scaleImage(640);
679 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
683 if($width > 320 || $height > 320) {
684 $ph->scaleImage(320);
685 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
689 $basename = basename($filename);
690 $uri = item_new_uri($a->get_hostname(), $page_owner_uid);
692 // Create item container
696 $arr['uid'] = $page_owner_uid;
698 $arr['parent-uri'] = $uri;
699 $arr['type'] = 'photo';
701 $arr['resource-id'] = $photo_hash;
702 $arr['contact-id'] = $owner_record['id'];
703 $arr['owner-name'] = $owner_record['name'];
704 $arr['owner-link'] = $owner_record['url'];
705 $arr['owner-avatar'] = $owner_record['thumb'];
706 $arr['author-name'] = $owner_record['name'];
707 $arr['author-link'] = $owner_record['url'];
708 $arr['author-avatar'] = $owner_record['thumb'];
710 $arr['allow_cid'] = $str_contact_allow;
711 $arr['allow_gid'] = $str_group_allow;
712 $arr['deny_cid'] = $str_contact_deny;
713 $arr['deny_gid'] = $str_group_deny;
714 $arr['last-child'] = 1;
715 $arr['visible'] = $visible;
718 $arr['body'] = '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']'
719 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]'
722 $item_id = item_store($arr);
725 q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
726 dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
727 intval($page_owner_uid),
733 proc_run('php', "include/notifier.php", 'wall-new', $item_id);
735 call_hooks('photo_post_end',intval($item_id));
737 // addon uploaders should call "killme()" [e.g. exit] within the photo_post_end hook
738 // if they do not wish to be redirected
740 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
746 function photos_content(&$a) {
750 // photos/name/upload
751 // photos/name/upload/xxxxx (xxxxx is album name)
752 // photos/name/album/xxxxx
753 // photos/name/album/xxxxx/edit
754 // photos/name/image/xxxxx
755 // photos/name/image/xxxxx/edit
758 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
759 notice( t('Public access denied.') . EOL);
764 require_once('include/bbcode.php');
765 require_once('include/security.php');
766 require_once('include/conversation.php');
768 if(! x($a->data,'user')) {
769 notice( t('No photos selected') . EOL );
773 $_SESSION['photo_return'] = $a->cmd;
780 $datatype = $a->argv[2];
781 $datum = $a->argv[3];
783 elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
784 $datatype = 'upload';
786 $datatype = 'summary';
794 // Setup permissions structures
800 $remote_contact = false;
802 $owner_uid = $a->data['user']['uid'];
804 $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
806 if((local_user()) && (local_user() == $owner_uid))
809 if($community_page && remote_user()) {
810 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
811 intval(remote_user()),
817 $remote_contact = true;
818 $visitor = remote_user();
823 // perhaps they're visiting - but not a community page, so they wouldn't have write access
825 if(remote_user() && (! $visitor)) {
826 $contact_id = $_SESSION['visitor_id'];
827 $groups = init_groups_visitor($contact_id);
828 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
829 intval(remote_user()),
834 $remote_contact = true;
838 if(! $remote_contact) {
840 $contact_id = $_SESSION['cid'];
841 $contact = $a->contact;
845 if($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (! $remote_contact)) {
846 notice( t('Access to this item is restricted.') . EOL);
850 $sql_extra = permissions_sql($owner_uid,$remote_contact,$groups);
855 $_is_owner = (local_user() && (local_user() == $owner_uid));
856 $o .= profile_tabs($a,$_is_owner, $a->data['user']['nickname']);
863 if($datatype === 'upload') {
865 notice( t('Permission denied.'));
870 $selname = (($datum) ? hex2bin($datum) : '');
873 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
876 $albumselect .= '<option value="" ' . ((! $selname) ? ' selected="selected" ' : '') . '> </option>';
877 if(count($a->data['albums'])) {
878 foreach($a->data['albums'] as $album) {
879 if(($album['album'] === '') || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
881 $selected = (($selname === $album['album']) ? ' selected="selected" ' : '');
882 $albumselect .= '<option value="' . $album['album'] . '"' . $selected . '>' . $album['album'] . '</option>';
886 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
888 $albumselect .= '</select>';
892 $ret = array('post_url' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'],
893 'addon_text' => $uploader,
894 'default_upload' => true);
897 call_hooks('photo_upload_form',$ret);
899 $default_upload = '<input type="file" name="userfile" /> <div class="photos-upload-submit-wrapper" >
900 <input type="submit" name="submit" value="' . t('Submit') . '" id="photos-upload-submit" /> </div>';
905 $tpl = get_markup_template('photos_upload.tpl');
906 $o .= replace_macros($tpl,array(
907 '$pagename' => t('Upload Photos'),
908 '$sessid' => session_id(),
909 '$nickname' => $a->data['user']['nickname'],
910 '$newalbum' => t('New album name: '),
911 '$existalbumtext' => t('or existing album name: '),
912 '$nosharetext' => t('Do not show a status post for this upload'),
913 '$albumselect' => template_escape($albumselect),
914 '$permissions' => t('Permissions'),
915 '$aclselect' => (($visitor) ? '' : template_escape(populate_acl($a->user, $celeb))),
916 '$uploader' => $ret['addon_text'],
917 '$default' => (($ret['default_upload']) ? $default_upload : ''),
918 '$uploadurl' => $ret['post_url']
925 if($datatype === 'album') {
927 $album = hex2bin($datum);
929 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
930 AND `scale` <= 4 $sql_extra GROUP BY `resource-id`",
935 $a->set_pager_total(count($r));
936 $a->set_pager_itemspage(20);
939 $r = q("SELECT `resource-id`, `id`, `filename`, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
940 AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
943 intval($a->pager['start']),
944 intval($a->pager['itemspage'])
947 $o .= '<h3>' . $album . '</h3>';
949 if($cmd === 'edit') {
950 if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
952 $edit_tpl = get_markup_template('album_edit.tpl');
953 $o .= replace_macros($edit_tpl,array(
954 '$nametext' => t('New album name: '),
955 '$nickname' => $a->data['user']['nickname'],
956 '$album' => template_escape($album),
957 '$hexalbum' => bin2hex($album),
958 '$submit' => t('Submit'),
959 '$dropsubmit' => t('Delete Album')
965 if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
967 $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/'
968 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">'
969 . t('Edit Album') . '</a></div>';
975 $o .= '<div class="photos-upload-link" ><a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/upload/' . bin2hex($album) . '" >' . t('Upload New Photos') . '</a></div>';
978 $tpl = get_markup_template('photo_album.tpl');
981 $o .= replace_macros($tpl,array(
983 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
984 '$phototitle' => t('View Photo'),
985 '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
986 '$imgalt' => template_escape($rr['filename']),
987 '$desc'=> template_escape($rr['desc'])
991 $o .= '<div id="photo-album-end"></div>';
999 if($datatype === 'image') {
1004 // fetch image, item containing image, then comments
1006 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
1007 $sql_extra ORDER BY `scale` ASC ",
1013 $ph = q("SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
1019 notice( t('Permission denied. Access to this item may be restricted.'));
1021 notice( t('Photo not available') . EOL );
1028 $prvnxt = q("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0
1029 $sql_extra ORDER BY `created` DESC ",
1030 dbesc($ph[0]['album']),
1034 if(count($prvnxt)) {
1035 for($z = 0; $z < count($prvnxt); $z++) {
1036 if($prvnxt[$z]['resource-id'] == $ph[0]['resource-id']) {
1040 $prv = count($prvnxt) - 1;
1041 if($nxt >= count($prvnxt))
1046 $edit_suffix = ((($cmd === 'edit') && ($can_post)) ? '/edit' : '');
1047 $prevlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] . $edit_suffix;
1048 $nextlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] . $edit_suffix;
1053 $hires = $lores = $ph[0];
1054 if(count($ph) > 1) {
1055 if($ph[1]['scale'] == 2) {
1056 // original is 640 or less, we can display it directly
1057 $hires = $lores = $ph[0];
1065 $album_link = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']);
1069 if($can_post && ($ph[0]['uid'] == $owner_uid)) {
1071 'edit' => array($a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $datum . (($cmd === 'edit') ? '' : '/edit'), (($cmd === 'edit') ? t('View photo') : t('Edit photo'))),
1072 'profile'=>array($a->get_baseurl() . '/profile_photo/use/'.$ph[0]['resource-id'], t('Use as profile photo')),
1076 $lock = ( ( ($ph[0]['uid'] == local_user()) && (strlen($ph[0]['allow_cid']) || strlen($ph[0]['allow_gid'])
1077 || strlen($ph[0]['deny_cid']) || strlen($ph[0]['deny_gid'])) )
1078 ? t('Private Message')
1085 $prevlink = array($prevlink, '<div class="icon prev"></div>') ;
1088 'href' => $a->get_baseurl() . '/photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg',
1089 'title'=> t('View Full Size'),
1090 'src' => $a->get_baseurl() . '/photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg'
1094 $nextlink = array($nextlink, '<div class="icon next"></div>');
1097 // Do we have an item for this photo?
1099 $linked_items = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
1102 if(count($linked_items)) {
1103 $link_item = $linked_items[0];
1104 $r = q("SELECT COUNT(*) AS `total`
1105 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
1106 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0
1107 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1108 AND `item`.`uid` = %d
1110 dbesc($link_item['uri']),
1111 dbesc($link_item['uri']),
1112 intval($link_item['uid'])
1117 $a->set_pager_total($r[0]['total']);
1120 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
1121 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`,
1122 `contact`.`rel`, `contact`.`thumb`, `contact`.`self`,
1123 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
1124 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
1125 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0
1126 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1127 AND `item`.`uid` = %d
1129 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
1130 dbesc($link_item['uri']),
1131 dbesc($link_item['uri']),
1132 intval($link_item['uid']),
1133 intval($a->pager['start']),
1134 intval($a->pager['itemspage'])
1138 if((local_user()) && (local_user() == $link_item['uid'])) {
1139 q("UPDATE `item` SET `unseen` = 0 WHERE `parent` = %d and `uid` = %d",
1140 intval($link_item['parent']),
1141 intval(local_user())
1148 if(count($linked_items) && strlen($link_item['tag'])) {
1149 $arr = explode(',',$link_item['tag']);
1150 // parse tags and add links
1152 foreach($arr as $t) {
1153 if(strlen($tag_str))
1155 $tag_str .= bbcode($t);
1157 $tags = array(t('Tags: '), $tag_str);
1158 if($cmd === 'edit') {
1159 $tags[] = $a->get_baseurl() . '/tagrm/' . $link_item['id'];
1160 $tags[] = t('[Remove any tag]');
1166 if(($cmd === 'edit') && ($can_post)) {
1167 $edit_tpl = get_markup_template('photo_edit.tpl');
1168 $edit = replace_macros($edit_tpl, array(
1169 '$id' => $ph[0]['id'],
1170 '$album' => template_escape($ph[0]['album']),
1171 '$newalbum' => t('New album name'),
1172 '$nickname' => $a->data['user']['nickname'],
1173 '$resource_id' => $ph[0]['resource-id'],
1174 '$capt_label' => t('Caption'),
1175 '$caption' => template_escape($ph[0]['desc']),
1176 '$tag_label' => t('Add a Tag'),
1177 '$tags' => $link_item['tag'],
1178 '$permissions' => t('Permissions'),
1179 '$aclselect' => template_escape(populate_acl($ph[0])),
1180 '$help_tags' => t('Example: @bob, @Barbara_Jensen, @jim@example.com, #California, #camping'),
1181 '$item_id' => ((count($linked_items)) ? $link_item['id'] : 0),
1182 '$submit' => t('Submit'),
1183 '$delete' => t('Delete Photo')
1187 if(count($linked_items)) {
1189 $cmnt_tpl = get_markup_template('comment_item.tpl');
1190 $tpl = get_markup_template('photo_item.tpl');
1191 $return_url = $a->cmd;
1193 $like_tpl = get_markup_template('like_noshare.tpl');
1197 if($can_post || can_write_wall($a,$owner_uid)) {
1198 $likebuttons = replace_macros($like_tpl,array(
1199 '$id' => $link_item['id'],
1200 '$likethis' => t("I like this \x28toggle\x29"),
1201 '$nolike' => t("I don't like this \x28toggle\x29"),
1202 '$share' => t('Share'),
1203 '$wait' => t('Please wait')
1209 if($can_post || can_write_wall($a,$owner_uid)) {
1210 if($link_item['last-child']) {
1211 $comments .= replace_macros($cmnt_tpl,array(
1212 '$return_path' => '',
1213 '$jsreload' => $return_url,
1214 '$type' => 'wall-comment',
1215 '$id' => $link_item['id'],
1216 '$parent' => $link_item['id'],
1217 '$profile_uid' => $owner_uid,
1218 '$mylink' => $contact['url'],
1219 '$mytitle' => t('This is you'),
1220 '$myphoto' => $contact['thumb'],
1221 '$comment' => t('Comment'),
1222 '$submit' => t('Submit'),
1223 '$preview' => t('Preview'),
1239 foreach($r as $item) {
1240 like_puller($a,$item,$alike,'like');
1241 like_puller($a,$item,$dlike,'dislike');
1244 $like = ((isset($alike[$link_item['id']])) ? format_like($alike[$link_item['id']],$alike[$link_item['id'] . '-l'],'like',$link_item['id']) : '');
1245 $dislike = ((isset($dlike[$link_item['id']])) ? format_like($dlike[$link_item['id']],$dlike[$link_item['id'] . '-l'],'dislike',$link_item['id']) : '');
1249 if($can_post || can_write_wall($a,$owner_uid)) {
1250 if($link_item['last-child']) {
1251 $comments .= replace_macros($cmnt_tpl,array(
1252 '$return_path' => '',
1253 '$jsreload' => $return_url,
1254 '$type' => 'wall-comment',
1255 '$id' => $link_item['id'],
1256 '$parent' => $link_item['id'],
1257 '$profile_uid' => $owner_uid,
1258 '$mylink' => $contact['url'],
1259 '$mytitle' => t('This is you'),
1260 '$myphoto' => $contact['thumb'],
1261 '$comment' => t('Comment'),
1262 '$submit' => t('Submit'),
1269 foreach($r as $item) {
1274 if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) && ($item['id'] != $item['parent']))
1277 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
1279 if($can_post || can_write_wall($a,$owner_uid)) {
1281 if($item['last-child']) {
1282 $comments .= replace_macros($cmnt_tpl,array(
1283 '$return_path' => '',
1284 '$jsreload' => $return_url,
1285 '$type' => 'wall-comment',
1286 '$id' => $item['item_id'],
1287 '$parent' => $item['parent'],
1288 '$profile_uid' => $owner_uid,
1289 '$mylink' => $contact['url'],
1290 '$mytitle' => t('This is you'),
1291 '$myphoto' => $contact['thumb'],
1292 '$comment' => t('Comment'),
1293 '$submit' => t('Submit'),
1300 if(local_user() && ($item['contact-uid'] == local_user())
1301 && ($item['network'] == 'dfrn') && (! $item['self'] )) {
1302 $profile_url = $redirect_url;
1303 $sparkle = ' sparkle';
1306 $profile_url = $item['url'];
1310 $diff_author = (($item['url'] !== $item['author-link']) ? true : false);
1312 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
1313 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $item['thumb']);
1315 $profile_link = $profile_url;
1319 if(($item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
1320 $drop = replace_macros(get_markup_template('photo_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
1323 $comments .= replace_macros($template,array(
1324 '$id' => $item['item_id'],
1325 '$profile_url' => $profile_link,
1326 '$name' => template_escape($profile_name),
1327 '$thumb' => $profile_avatar,
1328 '$sparkle' => $sparkle,
1329 '$title' => template_escape($item['title']),
1330 '$body' => template_escape(bbcode($item['body'])),
1331 '$ago' => relative_date($item['created']),
1332 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
1334 '$comment' => $comment
1339 $paginate = paginate($a);
1342 $photo_tpl = get_markup_template('photo_view.tpl');
1343 $o .= replace_macros($photo_tpl, array(
1344 '$id' => $ph[0]['id'],
1345 '$album' => array($album_link,template_escape($ph[0]['album'])),
1349 '$prevlink' => $prevlink,
1350 '$nextlink' => $nextlink,
1351 '$desc' => $ph[0]['desc'],
1352 '$tags' => template_escape($tags),
1354 '$likebuttons' => $likebuttons,
1355 '$like' => template_escape($like),
1356 '$dislike' => template_escape($dislike),
1357 '$comments' => $comments,
1358 '$paginate' => $paginate,
1364 // Default - show recent photos with upload link (if applicable)
1367 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
1368 $sql_extra GROUP BY `resource-id`",
1369 intval($a->data['user']['uid']),
1370 dbesc('Contact Photos'),
1371 dbesc( t('Contact Photos'))
1374 $a->set_pager_total(count($r));
1375 $a->set_pager_itemspage(20);
1378 $r = q("SELECT `resource-id`, `id`, `filename`, `album`, max(`scale`) AS `scale` FROM `photo`
1379 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
1380 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
1381 intval($a->data['user']['uid']),
1382 dbesc('Contact Photos'),
1383 dbesc( t('Contact Photos')),
1384 intval($a->pager['start']),
1385 intval($a->pager['itemspage'])
1392 foreach($r as $rr) {
1395 'link' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
1396 'title' => t('View Photo'),
1397 'src' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.jpg',
1398 'alt' => template_escape($rr['filename']),
1400 'link' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
1401 'name' => template_escape($rr['album']),
1402 'alt' => t('View Album'),
1409 $tpl = get_markup_template('photos_recent.tpl');
1410 $o .= replace_macros($tpl,array(
1411 '$title' => t('Recent Photos'),
1412 '$can_post' => $can_post,
1413 '$upload' => array(t('Upload New Photos'), $a->get_baseurl().'/photos/'.$a->data['user']['nickname'].'/upload'),
1414 '$photos' => $photos,