3 require_once('Photo.php');
4 require_once('include/items.php');
5 require_once('view/acl_selectors.php');
7 function photos_init(&$a) {
11 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
18 $a->data['user'] = $r[0];
20 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d",
21 intval($a->data['user']['uid'])
25 $a->data['albums'] = $albums;
27 $o .= '<h4><a href="' . $a->get_baseurl() . '/profile/' . $a->data['user']['nickname'] . '">' . $a->data['user']['username'] . '</a></h4>';
28 $o .= '<h4>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
31 foreach($albums as $album) {
32 if((! strlen($album['album'])) || ($album['album'] == t('Contact Photos')))
34 $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" />' . $album['album'] . '</a></li>';
38 $a->page['aside'] .= $o;
46 function photos_post(&$a) {
50 notice( t('Permission denied.') . EOL );
56 $r = q("SELECT `contact`.* `user`.`nickname` FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid`
57 WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
61 $contact_record = $r[0];
64 if(($a->argc > 2) && ($a->argv[1] == 'album')) {
65 $album = hex2bin($a->argv[2]);
67 if($album == t('Profile Photos') || $album == t('Contact Photos')) {
68 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
72 $r = q("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
77 notice( t('Album not found.') . EOL);
78 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
82 $newalbum = notags(trim($_POST['albumname']));
83 if($newalbum != $album) {
84 q("UPDATE `photo` SET `album` = '%s' WHERE `album` = '%s' AND `uid` = %d",
89 $newurl = str_replace(bin2hex($album),bin2hex($newalbum),$_SESSION['photo_return']);
90 goaway($a->get_baseurl() . '/' . $newurl);
94 if($_POST['dropalbum'] == t('Delete Album')) {
97 $r = q("SELECT distinct(`resource-id`) as `rid` FROM `photo` WHERE `uid` = %d AND `album` = '%s'",
103 $res[] = "'" . dbesc($rr['rid']) . "'" ;
107 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
108 return; // NOTREACHED
110 $str_res = implode(',', $res);
112 q("DELETE FROM `photo` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
115 $r = q("SELECT `parent-uri` FROM `item` WHERE `resource-id` IN ( $str_res ) AND `uid` = %d",
120 q("UPDATE `item` SET `deleted` = 1, `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
121 dbesc(datetime_convert()),
122 dbesc($rr['parent-uri']),
126 $drop_id = intval($rr['id']);
127 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
129 // send the notification upstream/downstream as the case may be
132 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" & ",
138 goaway($a->get_baseurl() . '/photos/' . $a->data['user']['nickname']);
139 return; // NOTREACHED
142 if(($a->argc > 1) && (x($_POST,'delete')) && ($_POST['delete'] == t('Delete Photo'))) {
143 $r = q("SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' LIMIT 1",
148 q("DELETE FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
150 dbesc($r[0]['resource-id'])
152 $i = q("SELECT * FROM `item` WHERE `resource-id` = '%s' AND `uid` = %d LIMIT 1",
153 dbesc($r[0]['resource-id']),
157 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
158 dbesc(datetime_convert()),
159 dbesc(datetime_convert()),
164 $url = $a->get_baseurl();
165 $drop_id = intval($i[0]['id']);
166 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
168 // send the notification upstream/downstream as the case may be
171 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" & ",
176 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
177 return; // NOTREACHED
182 if(($a->argc > 1) && (x($_POST,'desc') !== false)) {
183 $desc = notags(trim($_POST['desc']));
184 $tags = notags(trim($_POST['tags']));
185 $item_id = intval($_POST['item_id']);
186 $resource_id = $a->argv[1];
188 $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
193 $r = q("UPDATE `photo` SET `desc` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d",
202 $basename = basename($filename);
203 $uri = item_new_uri($a->get_hostname(),get_uid());
204 // Create item container
208 $arr['uid'] = get_uid();
210 $arr['parent-uri'] = $uri;
211 $arr['type'] = 'photo';
213 $arr['resource-id'] = $p[0]['resource-id'];
214 $arr['contact-id'] = $contact_record['id'];
215 $arr['owner-name'] = $contact_record['name'];
216 $arr['owner-link'] = $contact_record['url'];
217 $arr['owner-avatar'] = $contact_record['thumb'];
218 $arr['title'] = $title;
219 $arr['allow_cid'] = $p[0]['allow_cid'];
220 $arr['allow_gid'] = $p[0]['allow_gid'];
221 $arr['deny_cid'] = $p[0]['deny_cid'];
222 $arr['deny_gid'] = $p[0]['deny_gid'];
223 $arr['last-child'] = 1;
224 $arr['body'] = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']'
225 . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.jpg' . '[/img]'
228 $item_id = item_store($arr);
232 $r = q("UPDATE `item` SET `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
234 dbesc(datetime_convert()),
235 dbesc(datetime_convert()),
240 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
241 return; // NOTREACHED
247 if(! x($_FILES,'userfile'))
250 if($_POST['partitionCount'])
253 $java_upload = false;
255 $album = notags(trim($_POST['album']));
256 $newalbum = notags(trim($_POST['newalbum']));
258 if(! strlen($album)) {
259 if(strlen($newalbum))
262 $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
265 $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
269 if((! count($r)) || ($album == t('Profile Photos')))
275 $str_group_allow = perms2str($_POST['group_allow']);
276 $str_contact_allow = perms2str($_POST['contact_allow']);
277 $str_group_deny = perms2str($_POST['group_deny']);
278 $str_contact_deny = perms2str($_POST['contact_deny']);
280 $src = $_FILES['userfile']['tmp_name'];
281 $filename = basename($_FILES['userfile']['name']);
282 $filesize = intval($_FILES['userfile']['size']);
284 $imagedata = @file_get_contents($src);
285 $ph = new Photo($imagedata);
287 if(! ($image = $ph->getImage())) {
288 notice( t('Unable to process image.') . EOL );
295 $width = $ph->getWidth();
296 $height = $ph->getHeight();
300 $photo_hash = photo_new_resource();
302 $r = $ph->store(get_uid(), 0, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
305 notice( t('Image upload failed.') . EOL );
309 if($width > 640 || $height > 640) {
310 $ph->scaleImage(640);
311 $ph->store(get_uid(), 0, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
315 if($width > 320 || $height > 320) {
316 $ph->scaleImage(320);
317 $ph->store(get_uid(), 0, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
321 $basename = basename($filename);
322 $uri = item_new_uri($a->get_hostname(), get_uid());
324 // Create item container
329 $arr['uid'] = get_uid();
331 $arr['parent-uri'] = $uri;
332 $arr['type'] = 'photo';
334 $arr['resource-id'] = $photo_hash;
335 $arr['contact-id'] = $contact_record['id'];
336 $arr['owner-name'] = $contact_record['name'];
337 $arr['owner-link'] = $contact_record['url'];
338 $arr['owner-avatar'] = $contact_record['thumb'];
339 $arr['title'] = $title;
340 $arr['allow_cid'] = $str_contact_allow;
341 $arr['allow_gid'] = $str_group_allow;
342 $arr['deny_cid'] = $str_contact_deny;
343 $arr['deny_gid'] = $str_group_deny;
344 $arr['last-child'] = 1;
345 $arr['visible'] = $visible;
346 $arr['body'] = '[url=' . $a->get_baseurl() . '/photos/' . $contact_record['nickname'] . '/image/' . $photo_hash . ']'
347 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]'
350 $item_id = item_store($arr);
353 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
354 return; // NOTREACHED
358 return; // NOTREACHED
364 function photos_content(&$a) {
368 // photos/name/upload
369 // photos/name/album/xxxxx
370 // photos/name/album/xxxxx/edit
371 // photos/name/image/xxxxx
372 // photos/name/image/xxxxx/edit
375 if(! x($a->data,'user')) {
376 notice( t('No photos selected') . EOL );
380 $_SESSION['photo_return'] = $a->cmd;
387 $datatype = $a->argv[2];
388 $datum = $a->argv[3];
390 elseif(($a->argc > 2) && ($a->argv[2] == 'upload'))
391 $datatype = 'upload';
393 $datatype = 'summary';
401 // Setup permissions structures
404 $owner_uid = $a->data['user']['uid'];
407 $contact_id = $_SESSION['visitor_id'];
408 $groups = init_groups_visitor($contact_id);
411 // default permissions - anonymous user
413 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
415 // Profile owner - everything is visible
417 if(local_user() && (get_uid() == $owner_uid)) {
420 elseif(remote_user()) {
421 // authenticated visitor - here lie dragons
422 $gs = '<<>>'; // should be impossible to match
424 foreach($groups as $g)
425 $gs .= '|<' . intval($g) . '>';
427 $sql_extra = sprintf(
428 " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
429 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
430 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
431 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s') ",
433 intval($_SESSION['visitor_id']),
434 intval($_SESSION['visitor_id']),
445 if($datatype == 'upload') {
446 if( ! (local_user() && (get_uid() == $a->data['user']['uid']))) {
447 notice( t('Permission denied.'));
450 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
452 $albumselect .= '<option value="" selected="selected" > </option>';
453 if(count($a->data['albums'])) {
454 foreach($a->data['albums'] as $album) {
455 if(($album['album'] == '') || ($album['album'] == t('Contact Photos')))
457 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
460 $albumselect .= '</select>';
461 $tpl = file_get_contents('view/photos_upload.tpl');
462 $o .= replace_macros($tpl,array(
463 '$pagename' => t('Upload Photos'),
464 '$sessid' => session_id(),
465 '$newalbum' => t('New album name: '),
466 '$existalbumtext' => t('or existing album name: '),
467 '$filestext' => t('Select files to upload: '),
468 '$albumselect' => $albumselect,
469 '$permissions' => t('Permissions'),
470 '$aclselect' => populate_acl($a->user),
471 '$archive' => $a->get_baseurl() . '/jumploader_z.jar',
472 '$nojava' => t('Use the following controls only if the Java uploader (above) fails to launch.'),
473 '$uploadurl' => $a->get_baseurl() . '/photos',
474 '$submit' => t('Submit')
481 if($datatype == 'album') {
483 $album = hex2bin($datum);
485 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
486 $sql_extra GROUP BY `resource-id`",
487 intval($a->data['user']['uid']),
491 $a->set_pager_total(count($r));
494 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
495 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
496 intval($a->data['user']['uid']),
498 intval($a->pager['start']),
499 intval($a->pager['itemspage'])
502 $o .= '<h3>' . $album . '</h3>';
505 if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
506 if(local_user() && (get_uid() == $a->data['user']['uid'])) {
507 $edit_tpl = file_get_contents('view/album_edit.tpl');
508 $o .= replace_macros($edit_tpl,array(
509 '$nametext' => t('New album name: '),
511 '$hexalbum' => bin2hex($album),
512 '$submit' => t('Submit'),
513 '$dropsubmit' => t('Delete Album')
519 if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
520 if(local_user() && (get_uid() == $a->data['user']['uid'])) {
521 $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/'
522 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">'
523 . t('Edit Album') . '</a></div>';
527 $tpl = file_get_contents('view/photo_album.tpl');
530 $o .= replace_macros($tpl,array(
532 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
533 '$phototitle' => t('View Photo'),
534 '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
535 '$imgalt' => $rr['filename']
539 $o .= '<div id="photo-album-end"></div>';
545 if($datatype == 'image') {
547 require_once('security.php');
548 require_once('bbcode.php');
550 // fetch image, item containing image, then comments
552 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
553 $sql_extra ORDER BY `scale` ASC ",
554 intval($a->data['user']['uid']),
559 notice( t('Photo not available') . EOL );
564 $hires = $lores = $ph[0];
566 if($ph[1]['scale'] == 2) {
567 // original is 640 or less, we can display it directly
568 $hires = $lores = $ph[0];
577 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']) . '">' . $ph[0]['album'] . '</a></h3>';
579 if(local_user() && ($ph[0]['uid'] == get_uid())) {
580 $o .= '<div id="photo-edit-link-wrap" ><a id="photo-edit-link" href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $datum . '/edit' . '">' . t('Edit photo') . '</a></div>';
584 $o .= '<a href="' . $a->get_baseurl() . '/photo/'
585 . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="'
586 . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/'
587 . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
590 // Do we have an item for this photo?
592 $i1 = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
597 $r = q("SELECT COUNT(*) AS `total`
598 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
599 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
600 AND NOT `item`.`type` IN ( 'remote', 'net-comment')
601 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
603 dbesc($i1[0]['uri']),
609 $a->set_pager_total($r[0]['total']);
612 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
613 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
614 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
615 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
616 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
617 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
618 AND NOT `item`.`type` IN ( 'remote', 'net-comment')
619 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
621 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
622 dbesc($i1[0]['uri']),
623 dbesc($i1[0]['uri']),
624 intval($a->pager['start']),
625 intval($a->pager['itemspage'])
630 $o .= '<div id="photo-caption" >' . $ph[0]['desc'] . '</div>';
632 if(count($i1) && strlen($i1[0]['tag'])) {
633 // parse tags and add links
634 $o .= '<div id="in-this-photo-text">' . t('In this photo: ') . '</div>';
635 $o .= '<div id="in-this-photo">' . $i1[0]['tag'] . '</div>';
639 $edit_tpl = file_get_contents('view/photo_edit.tpl');
640 $o .= replace_macros($edit_tpl, array(
641 '$id' => $ph[0]['id'],
642 '$resource_id' => $ph[0]['resource-id'],
643 '$capt_label' => t('Caption'),
644 '$caption' => $ph[0]['desc'],
645 '$tag_label' => t('Tags'),
646 '$tags' => $i1[0]['tag'],
647 '$item_id' => ((count($i1)) ? $i1[0]['id'] : 0),
648 '$submit' => t('Submit'),
649 '$delete' => t('Delete Photo')
655 // pull out how many people like the photo
657 $cmnt_tpl = file_get_contents('view/comment_item.tpl');
658 $tpl = file_get_contents('view/photo_item.tpl');
659 $return_url = $a->cmd;
661 if(can_write_wall($a,$a->data['user']['uid'])) {
662 if($i1[0]['last-child']) {
663 $o .= replace_macros($cmnt_tpl,array(
664 '$return_path' => $return_url,
665 '$type' => 'wall-comment',
666 '$id' => $i1[0]['id'],
667 '$parent' => $i1[0]['id'],
668 '$profile_uid' => $a->data['user']['uid'],
677 foreach($r as $item) {
681 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
683 if(can_write_wall($a,$a->data['user']['uid'])) {
684 if($item['last-child']) {
685 $comment = replace_macros($cmnt_tpl,array(
686 '$return_path' => $return_url,
687 '$type' => 'wall-comment',
688 '$id' => $item['item_id'],
689 '$parent' => $item['parent'],
690 '$profile_uid' => $a->data['user']['uid'],
696 $profile_url = $item['url'];
698 if(local_user() && ($item['contact-uid'] == get_uid())
699 && ($item['rel'] == REL_VIP || $item['rel'] == REL_BUD) && (! $item['self'] ))
700 $profile_url = $redirect_url;
702 $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
703 $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
704 $profile_link = $profile_url;
708 if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == get_uid()))
709 $drop = replace_macros(file_get_contents('view/wall_item_drop.tpl'), array('$id' => $item['id']));
712 $o .= replace_macros($template,array(
713 '$id' => $item['item_id'],
714 '$profile_url' => $profile_link,
715 '$name' => $profile_name,
716 '$thumb' => $profile_avatar,
717 '$title' => $item['title'],
718 '$body' => bbcode($item['body']),
719 '$ago' => relative_date($item['created']),
720 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
722 '$comment' => $comment
732 // Default - show recent photos with upload link (if applicable)
734 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s'
735 $sql_extra GROUP BY `resource-id`",
736 intval($a->data['user']['uid']),
737 dbesc( t('Contact Photos'))
740 $a->set_pager_total(count($r));
743 $r = q("SELECT `resource-id`, `album`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s'
744 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
745 intval($a->data['user']['uid']),
746 dbesc( t('Contact Photos')),
747 intval($a->pager['start']),
748 intval($a->pager['itemspage'])
751 $o .= '<h3>' . t('Recent Photos') . '</h3>';
753 if( local_user() && (get_uid() == $a->data['user']['uid'])) {
754 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/'
755 . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
758 $tpl = file_get_contents('view/photo_top.tpl');
761 $o .= replace_macros($tpl,array(
763 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname']
764 . '/image/' . $rr['resource-id'],
765 '$phototitle' => t('View Photo'),
766 '$imgsrc' => $a->get_baseurl() . '/photo/'
767 . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
768 '$albumlink' => $a->get_baseurl . '/photos/'
769 . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
770 '$albumname' => $rr['album'],
771 '$albumalt' => t('View Album'),
772 '$imgalt' => $rr['filename']
776 $o .= '<div id="photo-top-end"></div>';