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');
128 $proc_debug = get_config('system','proc_debug');
130 // send the notification upstream/downstream as the case may be
133 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" $proc_debug & ",
139 goaway($a->get_baseurl() . '/photos/' . $a->data['user']['nickname']);
140 return; // NOTREACHED
143 if(($a->argc > 1) && (x($_POST,'delete')) && ($_POST['delete'] == t('Delete Photo'))) {
144 $r = q("SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' LIMIT 1",
149 q("DELETE FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
151 dbesc($r[0]['resource-id'])
153 $i = q("SELECT * FROM `item` WHERE `resource-id` = '%s' AND `uid` = %d LIMIT 1",
154 dbesc($r[0]['resource-id']),
158 q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
159 dbesc(datetime_convert()),
160 dbesc(datetime_convert()),
165 $url = $a->get_baseurl();
166 $drop_id = intval($i[0]['id']);
167 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
169 $proc_debug = get_config('system','proc_debug']);
171 // send the notification upstream/downstream as the case may be
174 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"drop\" \"$drop_id\" $proc_debug & ",
179 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
180 return; // NOTREACHED
185 if(($a->argc > 1) && (x($_POST,'desc') !== false)) {
186 $desc = notags(trim($_POST['desc']));
187 $tags = notags(trim($_POST['tags']));
188 $item_id = intval($_POST['item_id']);
189 $resource_id = $a->argv[1];
191 $p = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d ORDER BY `scale` DESC",
196 $r = q("UPDATE `photo` SET `desc` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d",
205 $basename = basename($filename);
206 $uri = item_new_uri($a->get_hostname(),get_uid());
207 // Create item container
211 $arr['uid'] = get_uid();
213 $arr['parent-uri'] = $uri;
214 $arr['type'] = 'photo';
216 $arr['resource-id'] = $p[0]['resource-id'];
217 $arr['contact-id'] = $contact_record['id'];
218 $arr['owner-name'] = $contact_record['name'];
219 $arr['owner-link'] = $contact_record['url'];
220 $arr['owner-avatar'] = $contact_record['thumb'];
221 $arr['title'] = $title;
222 $arr['allow_cid'] = $p[0]['allow_cid'];
223 $arr['allow_gid'] = $p[0]['allow_gid'];
224 $arr['deny_cid'] = $p[0]['deny_cid'];
225 $arr['deny_gid'] = $p[0]['deny_gid'];
226 $arr['last-child'] = 1;
227 $arr['body'] = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']'
228 . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.jpg' . '[/img]'
231 $item_id = item_store($arr);
235 $r = q("UPDATE `item` SET `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
237 dbesc(datetime_convert()),
238 dbesc(datetime_convert()),
243 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
244 return; // NOTREACHED
250 if(! x($_FILES,'userfile'))
253 if($_POST['partitionCount'])
256 $java_upload = false;
258 $album = notags(trim($_POST['album']));
259 $newalbum = notags(trim($_POST['newalbum']));
261 if(! strlen($album)) {
262 if(strlen($newalbum))
265 $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
268 $r = q("SELECT * FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
272 if((! count($r)) || ($album == t('Profile Photos')))
278 $str_group_allow = perms2str($_POST['group_allow']);
279 $str_contact_allow = perms2str($_POST['contact_allow']);
280 $str_group_deny = perms2str($_POST['group_deny']);
281 $str_contact_deny = perms2str($_POST['contact_deny']);
283 $src = $_FILES['userfile']['tmp_name'];
284 $filename = basename($_FILES['userfile']['name']);
285 $filesize = intval($_FILES['userfile']['size']);
287 $imagedata = @file_get_contents($src);
288 $ph = new Photo($imagedata);
290 if(! ($image = $ph->getImage())) {
291 notice( t('Unable to process image.') . EOL );
298 $width = $ph->getWidth();
299 $height = $ph->getHeight();
303 $photo_hash = photo_new_resource();
305 $r = $ph->store(get_uid(), 0, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
308 notice( t('Image upload failed.') . EOL );
312 if($width > 640 || $height > 640) {
313 $ph->scaleImage(640);
314 $ph->store(get_uid(), 0, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
318 if($width > 320 || $height > 320) {
319 $ph->scaleImage(320);
320 $ph->store(get_uid(), 0, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
324 $basename = basename($filename);
325 $uri = item_new_uri($a->get_hostname(), get_uid());
327 // Create item container
332 $arr['uid'] = get_uid();
334 $arr['parent-uri'] = $uri;
335 $arr['type'] = 'photo';
337 $arr['resource-id'] = $photo_hash;
338 $arr['contact-id'] = $contact_record['id'];
339 $arr['owner-name'] = $contact_record['name'];
340 $arr['owner-link'] = $contact_record['url'];
341 $arr['owner-avatar'] = $contact_record['thumb'];
342 $arr['title'] = $title;
343 $arr['allow_cid'] = $str_contact_allow;
344 $arr['allow_gid'] = $str_group_allow;
345 $arr['deny_cid'] = $str_contact_deny;
346 $arr['deny_gid'] = $str_group_deny;
347 $arr['last-child'] = 1;
348 $arr['visible'] = $visible;
349 $arr['body'] = '[url=' . $a->get_baseurl() . '/photos/' . $contact_record['nickname'] . '/image/' . $photo_hash . ']'
350 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]'
353 $item_id = item_store($arr);
356 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
357 return; // NOTREACHED
361 return; // NOTREACHED
367 function photos_content(&$a) {
371 // photos/name/upload
372 // photos/name/album/xxxxx
373 // photos/name/album/xxxxx/edit
374 // photos/name/image/xxxxx
375 // photos/name/image/xxxxx/edit
378 if(! x($a->data,'user')) {
379 notice( t('No photos selected') . EOL );
383 $_SESSION['photo_return'] = $a->cmd;
390 $datatype = $a->argv[2];
391 $datum = $a->argv[3];
393 elseif(($a->argc > 2) && ($a->argv[2] === 'upload'))
394 $datatype = 'upload';
396 $datatype = 'summary';
404 // Setup permissions structures
407 $owner_uid = $a->data['user']['uid'];
410 $contact_id = $_SESSION['visitor_id'];
411 $groups = init_groups_visitor($contact_id);
414 // default permissions - anonymous user
416 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
418 // Profile owner - everything is visible
420 if(local_user() && (get_uid() == $owner_uid)) {
423 elseif(remote_user()) {
424 // authenticated visitor - here lie dragons
425 $gs = '<<>>'; // should be impossible to match
427 foreach($groups as $g)
428 $gs .= '|<' . intval($g) . '>';
430 $sql_extra = sprintf(
431 " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
432 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
433 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
434 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s') ",
436 intval($_SESSION['visitor_id']),
437 intval($_SESSION['visitor_id']),
448 if($datatype === 'upload') {
449 if( ! (local_user() && (get_uid() == $a->data['user']['uid']))) {
450 notice( t('Permission denied.'));
453 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
455 $albumselect .= '<option value="" selected="selected" > </option>';
456 if(count($a->data['albums'])) {
457 foreach($a->data['albums'] as $album) {
458 if(($album['album'] === '') || ($album['album'] == t('Contact Photos')))
460 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
463 $albumselect .= '</select>';
464 $tpl = load_view_file('view/photos_upload.tpl');
465 $o .= replace_macros($tpl,array(
466 '$pagename' => t('Upload Photos'),
467 '$sessid' => session_id(),
468 '$newalbum' => t('New album name: '),
469 '$existalbumtext' => t('or existing album name: '),
470 '$filestext' => t('Select files to upload: '),
471 '$albumselect' => $albumselect,
472 '$permissions' => t('Permissions'),
473 '$aclselect' => populate_acl($a->user),
474 '$archive' => $a->get_baseurl() . '/jumploader_z.jar',
475 '$nojava' => t('Use the following controls only if the Java uploader (above) fails to launch.'),
476 '$uploadurl' => $a->get_baseurl() . '/photos',
477 '$submit' => t('Submit')
484 if($datatype === 'album') {
486 $album = hex2bin($datum);
488 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
489 $sql_extra GROUP BY `resource-id`",
490 intval($a->data['user']['uid']),
494 $a->set_pager_total(count($r));
497 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
498 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
499 intval($a->data['user']['uid']),
501 intval($a->pager['start']),
502 intval($a->pager['itemspage'])
505 $o .= '<h3>' . $album . '</h3>';
507 if($cmd === 'edit') {
508 if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
509 if(local_user() && (get_uid() == $a->data['user']['uid'])) {
510 $edit_tpl = load_view_file('view/album_edit.tpl');
511 $o .= replace_macros($edit_tpl,array(
512 '$nametext' => t('New album name: '),
514 '$hexalbum' => bin2hex($album),
515 '$submit' => t('Submit'),
516 '$dropsubmit' => t('Delete Album')
522 if(($album != t('Profile Photos')) && ($album != t('Contact Photos'))) {
523 if(local_user() && (get_uid() == $a->data['user']['uid'])) {
524 $o .= '<div id="album-edit-link"><a href="'. $a->get_baseurl() . '/photos/'
525 . $a->data['user']['nickname'] . '/album/' . bin2hex($album) . '/edit' . '">'
526 . t('Edit Album') . '</a></div>';
530 $tpl = load_view_file('view/photo_album.tpl');
533 $o .= replace_macros($tpl,array(
535 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
536 '$phototitle' => t('View Photo'),
537 '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
538 '$imgalt' => $rr['filename']
542 $o .= '<div id="photo-album-end"></div>';
548 if($datatype === 'image') {
550 require_once('security.php');
551 require_once('bbcode.php');
553 // fetch image, item containing image, then comments
555 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
556 $sql_extra ORDER BY `scale` ASC ",
557 intval($a->data['user']['uid']),
562 notice( t('Photo not available') . EOL );
567 $hires = $lores = $ph[0];
569 if($ph[1]['scale'] == 2) {
570 // original is 640 or less, we can display it directly
571 $hires = $lores = $ph[0];
580 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']) . '">' . $ph[0]['album'] . '</a></h3>';
582 if(local_user() && ($ph[0]['uid'] == get_uid())) {
583 $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>';
587 $o .= '<a href="' . $a->get_baseurl() . '/photo/'
588 . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="'
589 . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/'
590 . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
593 // Do we have an item for this photo?
595 $i1 = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
600 $r = q("SELECT COUNT(*) AS `total`
601 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
602 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
603 AND NOT `item`.`type` IN ( 'remote', 'net-comment')
604 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
606 dbesc($i1[0]['uri']),
612 $a->set_pager_total($r[0]['total']);
615 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
616 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
617 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
618 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
619 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
620 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
621 AND NOT `item`.`type` IN ( 'remote', 'net-comment')
622 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
624 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
625 dbesc($i1[0]['uri']),
626 dbesc($i1[0]['uri']),
627 intval($a->pager['start']),
628 intval($a->pager['itemspage'])
633 $o .= '<div id="photo-caption" >' . $ph[0]['desc'] . '</div>';
635 if(count($i1) && strlen($i1[0]['tag'])) {
636 // parse tags and add links
637 $o .= '<div id="in-this-photo-text">' . t('In this photo: ') . '</div>';
638 $o .= '<div id="in-this-photo">' . $i1[0]['tag'] . '</div>';
641 if($cmd === 'edit') {
642 $edit_tpl = load_view_file('view/photo_edit.tpl');
643 $o .= replace_macros($edit_tpl, array(
644 '$id' => $ph[0]['id'],
645 '$resource_id' => $ph[0]['resource-id'],
646 '$capt_label' => t('Caption'),
647 '$caption' => $ph[0]['desc'],
648 '$tag_label' => t('Tags'),
649 '$tags' => $i1[0]['tag'],
650 '$item_id' => ((count($i1)) ? $i1[0]['id'] : 0),
651 '$submit' => t('Submit'),
652 '$delete' => t('Delete Photo')
658 // pull out how many people like the photo
660 $cmnt_tpl = load_view_file('view/comment_item.tpl');
661 $tpl = load_view_file('view/photo_item.tpl');
662 $return_url = $a->cmd;
664 if(can_write_wall($a,$a->data['user']['uid'])) {
665 if($i1[0]['last-child']) {
666 $o .= replace_macros($cmnt_tpl,array(
667 '$return_path' => $return_url,
668 '$type' => 'wall-comment',
669 '$id' => $i1[0]['id'],
670 '$parent' => $i1[0]['id'],
671 '$profile_uid' => $a->data['user']['uid'],
680 foreach($r as $item) {
684 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
686 if(can_write_wall($a,$a->data['user']['uid'])) {
687 if($item['last-child']) {
688 $comment = replace_macros($cmnt_tpl,array(
689 '$return_path' => $return_url,
690 '$type' => 'wall-comment',
691 '$id' => $item['item_id'],
692 '$parent' => $item['parent'],
693 '$profile_uid' => $a->data['user']['uid'],
699 $profile_url = $item['url'];
701 if(local_user() && ($item['contact-uid'] == get_uid())
702 && ($item['rel'] == REL_VIP || $item['rel'] == REL_BUD) && (! $item['self'] ))
703 $profile_url = $redirect_url;
705 $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
706 $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
707 $profile_link = $profile_url;
711 if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == get_uid()))
712 $drop = replace_macros(load_view_file('view/wall_item_drop.tpl'), array('$id' => $item['id']));
715 $o .= replace_macros($template,array(
716 '$id' => $item['item_id'],
717 '$profile_url' => $profile_link,
718 '$name' => $profile_name,
719 '$thumb' => $profile_avatar,
720 '$title' => $item['title'],
721 '$body' => bbcode($item['body']),
722 '$ago' => relative_date($item['created']),
723 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
725 '$comment' => $comment
735 // Default - show recent photos with upload link (if applicable)
737 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s'
738 $sql_extra GROUP BY `resource-id`",
739 intval($a->data['user']['uid']),
740 dbesc( t('Contact Photos'))
743 $a->set_pager_total(count($r));
746 $r = q("SELECT `resource-id`, `album`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s'
747 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
748 intval($a->data['user']['uid']),
749 dbesc( t('Contact Photos')),
750 intval($a->pager['start']),
751 intval($a->pager['itemspage'])
754 $o .= '<h3>' . t('Recent Photos') . '</h3>';
756 if( local_user() && (get_uid() == $a->data['user']['uid'])) {
757 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/'
758 . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
761 $tpl = load_view_file('view/photo_top.tpl');
764 $o .= replace_macros($tpl,array(
766 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname']
767 . '/image/' . $rr['resource-id'],
768 '$phototitle' => t('View Photo'),
769 '$imgsrc' => $a->get_baseurl() . '/photo/'
770 . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
771 '$albumlink' => $a->get_baseurl . '/photos/'
772 . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
773 '$albumname' => $rr['album'],
774 '$albumalt' => t('View Album'),
775 '$imgalt' => $rr['filename']
779 $o .= '<div id="photo-top-end"></div>';