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 if(! intval($a->data['user']['hidewall'])) {
42 $o .= '<div id="side-bar-photos-albums" class="widget">';
43 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h3>';
46 foreach($albums as $album) {
48 // don't show contact photos. We once translated this name, but then you could still access it under
49 // a different language setting. Now we store the name in English and check in English (and translated for legacy albums).
51 if((! strlen($album['album'])) || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
53 $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" >' . $album['album'] . '</a></li>';
57 if(local_user() && $a->data['user']['uid'] == local_user()) {
58 $o .= '<div id="photo-albums-upload-link"><a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/upload" >' .t('Upload New Photos') . '</a></div>';
64 if(! x($a->page,'aside'))
65 $a->page['aside'] = '';
66 $a->page['aside'] .= $o;
69 $a->page['htmlhead'] .= "<script> var ispublic = '" . t('everybody') . "';" ;
71 $a->page['htmlhead'] .= <<< EOT
73 $(document).ready(function() {
75 $('#contact_allow, #contact_deny, #group_allow, #group_deny').change(function() {
77 $('#contact_allow option:selected, #contact_deny option:selected, #group_allow option:selected, #group_deny option:selected').each( function() {
78 selstr = $(this).text();
79 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
80 $('#jot-public').hide();
83 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
84 $('#jot-public').show();
100 function photos_post(&$a) {
102 logger('mod-photos: photos_post: begin' , LOGGER_DEBUG);
105 logger('mod_photos: REQUEST ' . print_r($_REQUEST,true), LOGGER_DATA);
106 logger('mod_photos: FILES ' . print_r($_FILES,true), LOGGER_DATA);
111 $page_owner_uid = $a->data['user']['uid'];
112 $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
114 if((local_user()) && (local_user() == $page_owner_uid))
117 if($community_page && remote_user()) {
118 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
119 intval(remote_user()),
120 intval($page_owner_uid)
124 $visitor = remote_user();
130 notice( t('Permission denied.') . EOL );
134 $r = q("SELECT `contact`.*, `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
135 WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
136 intval($page_owner_uid)
140 notice( t('Contact information unavailable') . EOL);
141 logger('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
145 $owner_record = $r[0];
148 if(($a->argc > 3) && ($a->argv[2] === 'album')) {
149 $album = hex2bin($a->argv[3]);
151 if($album === t('Profile Photos') || $album === 'Contact Photos' || $album === t('Contact Photos')) {
152 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
153 return; // NOTREACHED
156 $r = q("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
158 intval($page_owner_uid)
161 notice( t('Album not found.') . EOL);
162 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
163 return; // NOTREACHED
166 $newalbum = notags(trim($_POST['albumname']));
167 if($newalbum != $album) {
168 q("UPDATE `photo` SET `album` = '%s' WHERE `album` = '%s' AND `uid` = %d",
171 intval($page_owner_uid)
173 $newurl = str_replace(bin2hex($album),bin2hex($newalbum),$_SESSION['photo_return']);
174 goaway($a->get_baseurl() . '/' . $newurl);
175 return; // NOTREACHED
179 if($_POST['dropalbum'] == t('Delete Album')) {
183 // get the list of photos we are about to delete
186 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `album` = '%s'",
188 intval($page_owner_uid),
193 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'",
194 intval(local_user()),
200 $res[] = "'" . dbesc($rr['rid']) . "'" ;
204 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
205 return; // NOTREACHED
208 $str_res = implode(',', $res);
210 // remove the associated photos
212 q("DELETE FROM `photo` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
213 intval($page_owner_uid)
216 // find and delete the corresponding item with all the comments and likes/dislikes
218 $r = q("SELECT `parent-uri` FROM `item` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
219 intval($page_owner_uid)
223 q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
224 dbesc(datetime_convert()),
225 dbesc($rr['parent-uri']),
226 intval($page_owner_uid)
229 $drop_id = intval($rr['id']);
231 // send the notification upstream/downstream as the case may be
234 proc_run('php',"include/notifier.php","drop","$drop_id");
238 goaway($a->get_baseurl() . '/photos/' . $a->data['user']['nickname']);
239 return; // NOTREACHED
242 if(($a->argc > 2) && (x($_POST,'delete')) && ($_POST['delete'] == t('Delete Photo'))) {
244 // same as above but remove single photo
247 $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `contact-id` = %d AND `uid` = %d AND `resource-id` = '%s' LIMIT 1",
249 intval($page_owner_uid),
254 $r = q("SELECT `id`, `resource-id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' LIMIT 1",
255 intval(local_user()),
260 q("DELETE FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
261 intval($page_owner_uid),
262 dbesc($r[0]['resource-id'])
264 $i = q("SELECT * FROM `item` WHERE `resource-id` = '%s' AND `uid` = %d LIMIT 1",
265 dbesc($r[0]['resource-id']),
266 intval($page_owner_uid)
269 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
270 dbesc(datetime_convert()),
271 dbesc(datetime_convert()),
273 intval($page_owner_uid)
276 $url = $a->get_baseurl();
277 $drop_id = intval($i[0]['id']);
280 proc_run('php',"include/notifier.php","drop","$drop_id");
284 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
285 return; // NOTREACHED
288 if(($a->argc > 2) && ((x($_POST,'desc') !== false) || (x($_POST,'newtag') !== false)) || (x($_POST,'albname') !== false)) {
291 $desc = ((x($_POST,'desc')) ? notags(trim($_POST['desc'])) : '');
292 $rawtags = ((x($_POST,'newtag')) ? notags(trim($_POST['newtag'])) : '');
293 $item_id = ((x($_POST,'item_id')) ? intval($_POST['item_id']) : 0);
294 $albname = ((x($_POST,'albname')) ? notags(trim($_POST['albname'])) : '');
295 $str_group_allow = perms2str($_POST['group_allow']);
296 $str_contact_allow = perms2str($_POST['contact_allow']);
297 $str_group_deny = perms2str($_POST['group_deny']);
298 $str_contact_deny = perms2str($_POST['contact_deny']);
300 $resource_id = $a->argv[2];
302 if(! strlen($albname))
303 $albname = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
306 if((x($_POST,'rotate') !== false) && (intval($_POST['rotate']) == 1)) {
309 $r = q("select * from photo where `resource-id` = '%s' and uid = %d and scale = 0 limit 1",
311 intval($page_owner_uid)
314 $ph = new Photo($r[0]['data']);
315 if($ph->is_valid()) {
318 $width = $ph->getWidth();
319 $height = $ph->getHeight();
321 $x = q("update photo set data = '%s', height = %d, width = %d where `resource-id` = '%s' and uid = %d and scale = 0 limit 1",
322 dbesc($ph->imageString()),
326 intval($page_owner_uid)
329 if($width > 640 || $height > 640) {
330 $ph->scaleImage(640);
331 $width = $ph->getWidth();
332 $height = $ph->getHeight();
334 $x = q("update photo set data = '%s', height = %d, width = %d where `resource-id` = '%s' and uid = %d and scale = 1 limit 1",
335 dbesc($ph->imageString()),
339 intval($page_owner_uid)
343 if($width > 320 || $height > 320) {
344 $ph->scaleImage(320);
345 $width = $ph->getWidth();
346 $height = $ph->getHeight();
348 $x = q("update photo set data = '%s', height = %d, width = %d where `resource-id` = '%s' and uid = %d and scale = 2 limit 1",
349 dbesc($ph->imageString()),
353 intval($page_owner_uid)
360 $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
362 intval($page_owner_uid)
365 $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",
368 dbesc($str_contact_allow),
369 dbesc($str_group_allow),
370 dbesc($str_contact_deny),
371 dbesc($str_group_deny),
373 intval($page_owner_uid)
377 /* Don't make the item visible if the only change was the album name */
380 if($p[0]['desc'] !== $desc || strlen($rawtags))
385 // Create item container
388 $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
392 $arr['uid'] = $page_owner_uid;
394 $arr['parent-uri'] = $uri;
395 $arr['type'] = 'photo';
397 $arr['resource-id'] = $p[0]['resource-id'];
398 $arr['contact-id'] = $owner_record['id'];
399 $arr['owner-name'] = $owner_record['name'];
400 $arr['owner-link'] = $owner_record['url'];
401 $arr['owner-avatar'] = $owner_record['thumb'];
402 $arr['author-name'] = $owner_record['name'];
403 $arr['author-link'] = $owner_record['url'];
404 $arr['author-avatar'] = $owner_record['thumb'];
405 $arr['title'] = $title;
406 $arr['allow_cid'] = $p[0]['allow_cid'];
407 $arr['allow_gid'] = $p[0]['allow_gid'];
408 $arr['deny_cid'] = $p[0]['deny_cid'];
409 $arr['deny_gid'] = $p[0]['deny_gid'];
410 $arr['last-child'] = 1;
411 $arr['visible'] = $visibility;
414 $arr['body'] = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']'
415 . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.jpg' . '[/img]'
418 $item_id = item_store($arr);
423 $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
425 intval($page_owner_uid)
429 $old_tag = $r[0]['tag'];
430 $old_inform = $r[0]['inform'];
433 if(strlen($rawtags)) {
438 // if the new tag doesn't have a namespace specifier (@foo or #foo) give it a hashtag
440 $x = substr($rawtags,0,1);
441 if($x !== '@' && $x !== '#')
442 $rawtags = '#' . $rawtags;
445 $tags = get_tags($rawtags);
448 foreach($tags as $tag) {
451 if(strpos($tag,'@') === 0) {
452 $name = substr($tag,1);
453 if((strpos($name,'@')) || (strpos($name,'http://'))) {
455 $links = @lrdd($name);
457 foreach($links as $link) {
458 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
459 $profile = $link['@attributes']['href'];
460 if($link['@attributes']['rel'] === 'salmon') {
461 $salmon = '$url:' . str_replace(',','%sc',$link['@attributes']['href']);
468 $taginfo[] = array($newname,$profile,$salmon);
474 if(strrpos($newname,'+'))
475 $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
478 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
483 elseif(strstr($name,'_') || strstr($name,' ')) {
484 $newname = str_replace('_',' ',$name);
485 $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
487 intval($page_owner_uid)
491 $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
494 intval($page_owner_uid)
498 $newname = $r[0]['name'];
499 $profile = $r[0]['url'];
500 $notify = 'cid:' . $r[0]['id'];
507 if(substr($notify,0,4) === 'cid:')
508 $taginfo[] = array($newname,$profile,$notify,$r[0],'@[url=' . str_replace(',','%2c',$profile) . ']' . $newname . '[/url]');
510 $taginfo[] = array($newname,$profile,$notify,null,$str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]');
511 if(strlen($str_tags))
513 $profile = str_replace(',','%2c',$profile);
514 $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]';
521 if(strlen($newtag) && strlen($str_tags))
523 $newtag .= $str_tags;
525 $newinform = $old_inform;
526 if(strlen($newinform) && strlen($inform))
528 $newinform .= $inform;
530 $r = q("UPDATE `item` SET `tag` = '%s', `inform` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
533 dbesc(datetime_convert()),
534 dbesc(datetime_convert()),
536 intval($page_owner_uid)
540 foreach($p as $scales) {
541 if(intval($scales['scale']) == 2) {
545 if(intval($scales['scale']) == 4) {
551 if(count($taginfo)) {
552 foreach($taginfo as $tagged) {
554 $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
558 $arr['uid'] = $page_owner_uid;
560 $arr['parent-uri'] = $uri;
561 $arr['type'] = 'activity';
563 $arr['contact-id'] = $owner_record['id'];
564 $arr['owner-name'] = $owner_record['name'];
565 $arr['owner-link'] = $owner_record['url'];
566 $arr['owner-avatar'] = $owner_record['thumb'];
567 $arr['author-name'] = $owner_record['name'];
568 $arr['author-link'] = $owner_record['url'];
569 $arr['author-avatar'] = $owner_record['thumb'];
571 $arr['allow_cid'] = $p[0]['allow_cid'];
572 $arr['allow_gid'] = $p[0]['allow_gid'];
573 $arr['deny_cid'] = $p[0]['deny_cid'];
574 $arr['deny_gid'] = $p[0]['deny_gid'];
575 $arr['last-child'] = 1;
577 $arr['verb'] = ACTIVITY_TAG;
578 $arr['object-type'] = ACTIVITY_OBJ_PERSON;
579 $arr['target-type'] = ACTIVITY_OBJ_PHOTO;
580 $arr['tag'] = $tagged[4];
581 $arr['inform'] = $tagged[2];
583 $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]' ;
584 $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" ;
586 $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>';
587 $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n");
589 $arr['object'] .= xmlify('<link rel="photo" type="image/jpeg" href="' . $tagged[3]['photo'] . '" />' . "\n");
590 $arr['object'] .= '</link></object>' . "\n";
592 $arr['target'] = '<target><type>' . ACTIVITY_OBJ_PHOTO . '</type><title>' . $p[0]['desc'] . '</title><id>'
593 . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '</id>';
594 $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>';
596 $item_id = item_store($arr);
598 q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
599 dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
600 intval($page_owner_uid),
604 proc_run('php',"include/notifier.php","tag","$item_id");
611 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
612 return; // NOTREACHED
617 * default post action - upload a photo
620 call_hooks('photo_post_init', $_POST);
623 * Determine the album to use
626 $album = notags(trim($_REQUEST['album']));
627 $newalbum = notags(trim($_REQUEST['newalbum']));
629 logger('mod/photos.php: photos_post(): album= ' . $album . ' newalbum= ' . $newalbum , LOGGER_DEBUG);
631 if(! strlen($album)) {
632 if(strlen($newalbum))
635 $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
640 * We create a wall item for every photo, but we don't want to
641 * overwhelm the data stream with a hundred newly uploaded photos.
642 * So we will make the first photo uploaded to this album in the last several hours
643 * visible by default, the rest will become visible over time when and if
644 * they acquire comments, likes, dislikes, and/or tags
648 $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `created` > UTC_TIMESTAMP() - INTERVAL 3 HOUR ",
650 intval($page_owner_uid)
652 if((! count($r)) || ($album == t('Profile Photos')))
657 if(intval($_REQUEST['not_visible']) || $_REQUEST['not_visible'] === 'true')
660 $str_group_allow = perms2str(((is_array($_REQUEST['group_allow'])) ? $_REQUEST['group_allow'] : explode(',',$_REQUEST['group_allow'])));
661 $str_contact_allow = perms2str(((is_array($_REQUEST['contact_allow'])) ? $_REQUEST['contact_allow'] : explode(',',$_REQUEST['contact_allow'])));
662 $str_group_deny = perms2str(((is_array($_REQUEST['group_deny'])) ? $_REQUEST['group_deny'] : explode(',',$_REQUEST['group_deny'])));
663 $str_contact_deny = perms2str(((is_array($_REQUEST['contact_deny'])) ? $_REQUEST['contact_deny'] : explode(',',$_REQUEST['contact_deny'])));
665 $ret = array('src' => '', 'filename' => '', 'filesize' => 0);
667 call_hooks('photo_post_file',$ret);
669 if(x($ret,'src') && x($ret,'filesize')) {
671 $filename = $ret['filename'];
672 $filesize = $ret['filesize'];
675 $src = $_FILES['userfile']['tmp_name'];
676 $filename = basename($_FILES['userfile']['name']);
677 $filesize = intval($_FILES['userfile']['size']);
681 logger('photos: upload: received file: ' . $filename . ' as ' . $src . ' ' . $filesize . ' bytes', LOGGER_DEBUG);
683 $maximagesize = get_config('system','maximagesize');
685 if(($maximagesize) && ($filesize > $maximagesize)) {
686 notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
689 call_hooks('photo_post_end',$foo);
694 notice( t('Image file is empty.') . EOL);
697 call_hooks('photo_post_end',$foo);
701 logger('mod/photos.php: photos_post(): loading the contents of ' . $src , LOGGER_DEBUG);
703 $imagedata = @file_get_contents($src);
704 $ph = new Photo($imagedata);
706 if(! $ph->is_valid()) {
707 logger('mod/photos.php: photos_post(): unable to process image' , LOGGER_DEBUG);
708 notice( t('Unable to process image.') . EOL );
711 call_hooks('photo_post_end',$foo);
717 $width = $ph->getWidth();
718 $height = $ph->getHeight();
722 $photo_hash = photo_new_resource();
724 $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);
727 logger('mod/photos.php: photos_post(): image store failed' , LOGGER_DEBUG);
728 notice( t('Image upload failed.') . EOL );
732 if($width > 640 || $height > 640) {
733 $ph->scaleImage(640);
734 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
738 if($width > 320 || $height > 320) {
739 $ph->scaleImage(320);
740 $ph->store($page_owner_uid, $visitor, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
744 $basename = basename($filename);
745 $uri = item_new_uri($a->get_hostname(), $page_owner_uid);
747 // Create item container
751 $arr['uid'] = $page_owner_uid;
753 $arr['parent-uri'] = $uri;
754 $arr['type'] = 'photo';
756 $arr['resource-id'] = $photo_hash;
757 $arr['contact-id'] = $owner_record['id'];
758 $arr['owner-name'] = $owner_record['name'];
759 $arr['owner-link'] = $owner_record['url'];
760 $arr['owner-avatar'] = $owner_record['thumb'];
761 $arr['author-name'] = $owner_record['name'];
762 $arr['author-link'] = $owner_record['url'];
763 $arr['author-avatar'] = $owner_record['thumb'];
765 $arr['allow_cid'] = $str_contact_allow;
766 $arr['allow_gid'] = $str_group_allow;
767 $arr['deny_cid'] = $str_contact_deny;
768 $arr['deny_gid'] = $str_group_deny;
769 $arr['last-child'] = 1;
770 $arr['visible'] = $visible;
773 $arr['body'] = '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']'
774 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]'
777 $item_id = item_store($arr);
780 q("UPDATE `item` SET `plink` = '%s' WHERE `uid` = %d AND `id` = %d LIMIT 1",
781 dbesc($a->get_baseurl() . '/display/' . $owner_record['nickname'] . '/' . $item_id),
782 intval($page_owner_uid),
788 proc_run('php', "include/notifier.php", 'wall-new', $item_id);
790 call_hooks('photo_post_end',intval($item_id));
792 // addon uploaders should call "killme()" [e.g. exit] within the photo_post_end hook
793 // if they do not wish to be redirected
795 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
801 function photos_content(&$a) {
805 // photos/name/upload
806 // photos/name/upload/xxxxx (xxxxx is album name)
807 // photos/name/album/xxxxx
808 // photos/name/album/xxxxx/edit
809 // photos/name/image/xxxxx
810 // photos/name/image/xxxxx/edit
813 if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
814 notice( t('Public access denied.') . EOL);
819 require_once('include/bbcode.php');
820 require_once('include/security.php');
821 require_once('include/conversation.php');
823 if(! x($a->data,'user')) {
824 notice( t('No photos selected') . EOL );
828 $_SESSION['photo_return'] = $a->cmd;
835 $datatype = $a->argv[2];
836 $datum = $a->argv[3];
838 elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
839 $datatype = 'upload';
841 $datatype = 'summary';
849 // Setup permissions structures
855 $remote_contact = false;
857 $owner_uid = $a->data['user']['uid'];
859 $community_page = (($a->data['user']['page-flags'] == PAGE_COMMUNITY) ? true : false);
861 if((local_user()) && (local_user() == $owner_uid))
864 if($community_page && remote_user()) {
865 $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
866 intval(remote_user()),
872 $remote_contact = true;
873 $visitor = remote_user();
878 // perhaps they're visiting - but not a community page, so they wouldn't have write access
880 if(remote_user() && (! $visitor)) {
881 $contact_id = $_SESSION['visitor_id'];
882 $groups = init_groups_visitor($contact_id);
883 $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
884 intval(remote_user()),
889 $remote_contact = true;
893 if(! $remote_contact) {
895 $contact_id = $_SESSION['cid'];
896 $contact = $a->contact;
900 if($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (! $remote_contact)) {
901 notice( t('Access to this item is restricted.') . EOL);
905 $sql_extra = permissions_sql($owner_uid,$remote_contact,$groups);
910 $_is_owner = (local_user() && (local_user() == $owner_uid));
911 $o .= profile_tabs($a,$_is_owner, $a->data['user']['nickname']);
918 if($datatype === 'upload') {
920 notice( t('Permission denied.'));
925 $selname = (($datum) ? hex2bin($datum) : '');
928 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
931 $albumselect .= '<option value="" ' . ((! $selname) ? ' selected="selected" ' : '') . '> </option>';
932 if(count($a->data['albums'])) {
933 foreach($a->data['albums'] as $album) {
934 if(($album['album'] === '') || ($album['album'] === 'Contact Photos') || ($album['album'] === t('Contact Photos')))
936 $selected = (($selname === $album['album']) ? ' selected="selected" ' : '');
937 $albumselect .= '<option value="' . $album['album'] . '"' . $selected . '>' . $album['album'] . '</option>';
941 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
943 $albumselect .= '</select>';
947 $ret = array('post_url' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'],
948 'addon_text' => $uploader,
949 'default_upload' => true);
952 call_hooks('photo_upload_form',$ret);
954 $default_upload = '<input type="file" name="userfile" /> <div class="photos-upload-submit-wrapper" >
955 <input type="submit" name="submit" value="' . t('Submit') . '" id="photos-upload-submit" /> </div>';
960 $tpl = get_markup_template('photos_upload.tpl');
961 $o .= replace_macros($tpl,array(
962 '$pagename' => t('Upload Photos'),
963 '$sessid' => session_id(),
964 '$nickname' => $a->data['user']['nickname'],
965 '$newalbum' => t('New album name: '),
966 '$existalbumtext' => t('or existing album name: '),
967 '$nosharetext' => t('Do not show a status post for this upload'),
968 '$albumselect' => template_escape($albumselect),
969 '$permissions' => t('Permissions'),
970 '$aclselect' => (($visitor) ? '' : template_escape(populate_acl($a->user, $celeb))),
971 '$uploader' => $ret['addon_text'],
972 '$default' => (($ret['default_upload']) ? $default_upload : ''),
973 '$uploadurl' => $ret['post_url']
980 if($datatype === 'album') {
982 $album = hex2bin($datum);
984 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
985 AND `scale` <= 4 $sql_extra GROUP BY `resource-id`",
990 $a->set_pager_total(count($r));
991 $a->set_pager_itemspage(20);
994 $r = q("SELECT `resource-id`, `id`, `filename`, max(`scale`) AS `scale`, `desc` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
995 AND `scale` <= 4 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
998 intval($a->pager['start']),
999 intval($a->pager['itemspage'])
1002 $o .= '<h3>' . $album . '</h3>';
1004 if($cmd === 'edit') {
1005 if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
1007 $edit_tpl = get_markup_template('album_edit.tpl');
1008 $o .= replace_macros($edit_tpl,array(
1009 '$nametext' => t('New album name: '),
1010 '$nickname' => $a->data['user']['nickname'],
1011 '$album' => template_escape($album),
1012 '$hexalbum' => bin2hex($album),
1013 '$submit' => t('Submit'),
1014 '$dropsubmit' => t('Delete Album')
1020 if(($album !== t('Profile Photos')) && ($album !== 'Contact Photos') && ($album !== t('Contact Photos'))) {
1022 $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/'
1023 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">'
1024 . t('Edit Album') . '</a></div>';
1030 $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>';
1033 $tpl = get_markup_template('photo_album.tpl');
1035 $twist = 'rotright';
1036 foreach($r as $rr) {
1037 if($twist == 'rotright')
1040 $twist = 'rotright';
1042 $o .= replace_macros($tpl,array(
1044 '$twist' => ' ' . $twist . rand(2,4),
1045 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
1046 '$phototitle' => t('View Photo'),
1047 '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
1048 '$imgalt' => template_escape($rr['filename']),
1049 '$desc'=> template_escape($rr['desc'])
1053 $o .= '<div id="photo-album-end"></div>';
1061 if($datatype === 'image') {
1066 // fetch image, item containing image, then comments
1068 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
1069 $sql_extra ORDER BY `scale` ASC ",
1075 $ph = q("SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
1081 notice( t('Permission denied. Access to this item may be restricted.'));
1083 notice( t('Photo not available') . EOL );
1090 $prvnxt = q("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0
1091 $sql_extra ORDER BY `created` DESC ",
1092 dbesc($ph[0]['album']),
1096 if(count($prvnxt)) {
1097 for($z = 0; $z < count($prvnxt); $z++) {
1098 if($prvnxt[$z]['resource-id'] == $ph[0]['resource-id']) {
1102 $prv = count($prvnxt) - 1;
1103 if($nxt >= count($prvnxt))
1108 $edit_suffix = ((($cmd === 'edit') && ($can_post)) ? '/edit' : '');
1109 $prevlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] . $edit_suffix;
1110 $nextlink = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] . $edit_suffix;
1115 $hires = $lores = $ph[0];
1116 if(count($ph) > 1) {
1117 if($ph[1]['scale'] == 2) {
1118 // original is 640 or less, we can display it directly
1119 $hires = $lores = $ph[0];
1127 $album_link = $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']);
1131 if($can_post && ($ph[0]['uid'] == $owner_uid)) {
1133 'edit' => array($a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $datum . (($cmd === 'edit') ? '' : '/edit'), (($cmd === 'edit') ? t('View photo') : t('Edit photo'))),
1134 'profile'=>array($a->get_baseurl() . '/profile_photo/use/'.$ph[0]['resource-id'], t('Use as profile photo')),
1138 $lock = ( ( ($ph[0]['uid'] == local_user()) && (strlen($ph[0]['allow_cid']) || strlen($ph[0]['allow_gid'])
1139 || strlen($ph[0]['deny_cid']) || strlen($ph[0]['deny_gid'])) )
1140 ? t('Private Message')
1146 if(! $cmd !== 'edit') {
1147 $a->page['htmlhead'] .= '<script>
1148 $(document).keydown(function(event) {' . "\n";
1151 $a->page['htmlhead'] .= 'if(event.ctrlKey && event.keyCode == 37) { event.preventDefault(); window.location.href = \'' . $prevlink . '\'; }' . "\n";
1153 $a->page['htmlhead'] .= 'if(event.ctrlKey && event.keyCode == 39) { event.preventDefault(); window.location.href = \'' . $nextlink . '\'; }' . "\n";
1154 $a->page['htmlhead'] .= '});</script>';
1158 $prevlink = array($prevlink, '<div class="icon prev"></div>') ;
1161 'href' => $a->get_baseurl() . '/photo/' . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg',
1162 'title'=> t('View Full Size'),
1163 'src' => $a->get_baseurl() . '/photo/' . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '?f=&_u=' . datetime_convert('','','','ymdhis')
1167 $nextlink = array($nextlink, '<div class="icon next"></div>');
1170 // Do we have an item for this photo?
1172 $linked_items = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
1175 if(count($linked_items)) {
1176 $link_item = $linked_items[0];
1177 $r = q("SELECT COUNT(*) AS `total`
1178 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
1179 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0
1180 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1181 AND `item`.`uid` = %d
1183 dbesc($link_item['uri']),
1184 dbesc($link_item['uri']),
1185 intval($link_item['uid'])
1190 $a->set_pager_total($r[0]['total']);
1193 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
1194 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`network`,
1195 `contact`.`rel`, `contact`.`thumb`, `contact`.`self`,
1196 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
1197 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
1198 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0 and `item`.`moderated` = 0
1199 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
1200 AND `item`.`uid` = %d
1202 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
1203 dbesc($link_item['uri']),
1204 dbesc($link_item['uri']),
1205 intval($link_item['uid']),
1206 intval($a->pager['start']),
1207 intval($a->pager['itemspage'])
1211 if((local_user()) && (local_user() == $link_item['uid'])) {
1212 q("UPDATE `item` SET `unseen` = 0 WHERE `parent` = %d and `uid` = %d",
1213 intval($link_item['parent']),
1214 intval(local_user())
1221 if(count($linked_items) && strlen($link_item['tag'])) {
1222 $arr = explode(',',$link_item['tag']);
1223 // parse tags and add links
1225 foreach($arr as $t) {
1226 if(strlen($tag_str))
1228 $tag_str .= bbcode($t);
1230 $tags = array(t('Tags: '), $tag_str);
1231 if($cmd === 'edit') {
1232 $tags[] = $a->get_baseurl() . '/tagrm/' . $link_item['id'];
1233 $tags[] = t('[Remove any tag]');
1239 if(($cmd === 'edit') && ($can_post)) {
1240 $edit_tpl = get_markup_template('photo_edit.tpl');
1241 $edit = replace_macros($edit_tpl, array(
1242 '$id' => $ph[0]['id'],
1243 '$rotate' => t('Rotate CW'),
1244 '$album' => template_escape($ph[0]['album']),
1245 '$newalbum' => t('New album name'),
1246 '$nickname' => $a->data['user']['nickname'],
1247 '$resource_id' => $ph[0]['resource-id'],
1248 '$capt_label' => t('Caption'),
1249 '$caption' => template_escape($ph[0]['desc']),
1250 '$tag_label' => t('Add a Tag'),
1251 '$tags' => $link_item['tag'],
1252 '$permissions' => t('Permissions'),
1253 '$aclselect' => template_escape(populate_acl($ph[0])),
1254 '$help_tags' => t('Example: @bob, @Barbara_Jensen, @jim@example.com, #California, #camping'),
1255 '$item_id' => ((count($linked_items)) ? $link_item['id'] : 0),
1256 '$submit' => t('Submit'),
1257 '$delete' => t('Delete Photo')
1261 if(count($linked_items)) {
1263 $cmnt_tpl = get_markup_template('comment_item.tpl');
1264 $tpl = get_markup_template('photo_item.tpl');
1265 $return_url = $a->cmd;
1267 $like_tpl = get_markup_template('like_noshare.tpl');
1271 if($can_post || can_write_wall($a,$owner_uid)) {
1272 $likebuttons = replace_macros($like_tpl,array(
1273 '$id' => $link_item['id'],
1274 '$likethis' => t("I like this \x28toggle\x29"),
1275 '$nolike' => t("I don't like this \x28toggle\x29"),
1276 '$share' => t('Share'),
1277 '$wait' => t('Please wait')
1283 if($can_post || can_write_wall($a,$owner_uid)) {
1284 if($link_item['last-child']) {
1285 $comments .= replace_macros($cmnt_tpl,array(
1286 '$return_path' => '',
1287 '$jsreload' => $return_url,
1288 '$type' => 'wall-comment',
1289 '$id' => $link_item['id'],
1290 '$parent' => $link_item['id'],
1291 '$profile_uid' => $owner_uid,
1292 '$mylink' => $contact['url'],
1293 '$mytitle' => t('This is you'),
1294 '$myphoto' => $contact['thumb'],
1295 '$comment' => t('Comment'),
1296 '$submit' => t('Submit'),
1297 '$preview' => t('Preview'),
1313 foreach($r as $item) {
1314 like_puller($a,$item,$alike,'like');
1315 like_puller($a,$item,$dlike,'dislike');
1318 $like = ((isset($alike[$link_item['id']])) ? format_like($alike[$link_item['id']],$alike[$link_item['id'] . '-l'],'like',$link_item['id']) : '');
1319 $dislike = ((isset($dlike[$link_item['id']])) ? format_like($dlike[$link_item['id']],$dlike[$link_item['id'] . '-l'],'dislike',$link_item['id']) : '');
1323 if($can_post || can_write_wall($a,$owner_uid)) {
1324 if($link_item['last-child']) {
1325 $comments .= replace_macros($cmnt_tpl,array(
1326 '$return_path' => '',
1327 '$jsreload' => $return_url,
1328 '$type' => 'wall-comment',
1329 '$id' => $link_item['id'],
1330 '$parent' => $link_item['id'],
1331 '$profile_uid' => $owner_uid,
1332 '$mylink' => $contact['url'],
1333 '$mytitle' => t('This is you'),
1334 '$myphoto' => $contact['thumb'],
1335 '$comment' => t('Comment'),
1336 '$submit' => t('Submit'),
1343 foreach($r as $item) {
1348 if(((activity_match($item['verb'],ACTIVITY_LIKE)) || (activity_match($item['verb'],ACTIVITY_DISLIKE))) && ($item['id'] != $item['parent']))
1351 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
1353 if($can_post || can_write_wall($a,$owner_uid)) {
1355 if($item['last-child']) {
1356 $comments .= replace_macros($cmnt_tpl,array(
1357 '$return_path' => '',
1358 '$jsreload' => $return_url,
1359 '$type' => 'wall-comment',
1360 '$id' => $item['item_id'],
1361 '$parent' => $item['parent'],
1362 '$profile_uid' => $owner_uid,
1363 '$mylink' => $contact['url'],
1364 '$mytitle' => t('This is you'),
1365 '$myphoto' => $contact['thumb'],
1366 '$comment' => t('Comment'),
1367 '$submit' => t('Submit'),
1374 if(local_user() && ($item['contact-uid'] == local_user())
1375 && ($item['network'] == 'dfrn') && (! $item['self'] )) {
1376 $profile_url = $redirect_url;
1377 $sparkle = ' sparkle';
1380 $profile_url = $item['url'];
1384 $diff_author = (($item['url'] !== $item['author-link']) ? true : false);
1386 $profile_name = (((strlen($item['author-name'])) && $diff_author) ? $item['author-name'] : $item['name']);
1387 $profile_avatar = (((strlen($item['author-avatar'])) && $diff_author) ? $item['author-avatar'] : $item['thumb']);
1389 $profile_link = $profile_url;
1393 if(($item['contact-id'] == remote_user()) || ($item['uid'] == local_user()))
1394 $drop = replace_macros(get_markup_template('photo_drop.tpl'), array('$id' => $item['id'], '$delete' => t('Delete')));
1397 $comments .= replace_macros($template,array(
1398 '$id' => $item['item_id'],
1399 '$profile_url' => $profile_link,
1400 '$name' => template_escape($profile_name),
1401 '$thumb' => $profile_avatar,
1402 '$sparkle' => $sparkle,
1403 '$title' => template_escape($item['title']),
1404 '$body' => template_escape(bbcode($item['body'])),
1405 '$ago' => relative_date($item['created']),
1406 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
1408 '$comment' => $comment
1413 $paginate = paginate($a);
1416 $photo_tpl = get_markup_template('photo_view.tpl');
1417 $o .= replace_macros($photo_tpl, array(
1418 '$id' => $ph[0]['id'],
1419 '$album' => array($album_link,template_escape($ph[0]['album'])),
1423 '$prevlink' => $prevlink,
1424 '$nextlink' => $nextlink,
1425 '$desc' => $ph[0]['desc'],
1426 '$tags' => template_escape($tags),
1428 '$likebuttons' => $likebuttons,
1429 '$like' => template_escape($like),
1430 '$dislike' => template_escape($dislike),
1431 '$comments' => $comments,
1432 '$paginate' => $paginate,
1438 // Default - show recent photos with upload link (if applicable)
1441 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
1442 $sql_extra GROUP BY `resource-id`",
1443 intval($a->data['user']['uid']),
1444 dbesc('Contact Photos'),
1445 dbesc( t('Contact Photos'))
1448 $a->set_pager_total(count($r));
1449 $a->set_pager_itemspage(20);
1452 $r = q("SELECT `resource-id`, `id`, `filename`, `album`, max(`scale`) AS `scale` FROM `photo`
1453 WHERE `uid` = %d AND `album` != '%s' AND `album` != '%s'
1454 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
1455 intval($a->data['user']['uid']),
1456 dbesc('Contact Photos'),
1457 dbesc( t('Contact Photos')),
1458 intval($a->pager['start']),
1459 intval($a->pager['itemspage'])
1466 $twist = 'rotright';
1467 foreach($r as $rr) {
1468 if($twist == 'rotright')
1471 $twist = 'rotright';
1475 'twist' => ' ' . $twist . rand(2,4),
1476 'link' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
1477 'title' => t('View Photo'),
1478 'src' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . ((($rr['scale']) == 6) ? 4 : $rr['scale']) . '.jpg',
1479 'alt' => template_escape($rr['filename']),
1481 'link' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
1482 'name' => template_escape($rr['album']),
1483 'alt' => t('View Album'),
1490 $tpl = get_markup_template('photos_recent.tpl');
1491 $o .= replace_macros($tpl,array(
1492 '$title' => t('Recent Photos'),
1493 '$can_post' => $can_post,
1494 '$upload' => array(t('Upload New Photos'), $a->get_baseurl().'/photos/'.$a->data['user']['nickname'].'/upload'),
1495 '$photos' => $photos,