3 require_once('Photo.php');
4 require_once('view/acl_selectors.php');
6 function photos_init(&$a) {
10 $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
17 $a->data['user'] = $r[0];
19 $albums = q("SELECT distinct(`album`) AS `album` FROM `photo` WHERE `uid` = %d",
20 intval($a->data['user']['uid'])
24 $a->data['albums'] = $albums;
26 $o .= '<h4><a href="' . $a->get_baseurl() . '/profile/' . $a->data['user']['nickname'] . '">' . $a->data['user']['username'] . '</a></h4>';
27 $o .= '<h4>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '">' . t('Photo Albums') . '</a></h4>';
30 foreach($albums as $album) {
31 if((! strlen($album['album'])) || ($album['album'] == t('Contact Photos')))
33 $o .= '<li>' . '<a href="photos/' . $a->argv[1] . '/album/' . bin2hex($album['album']) . '" />' . $album['album'] . '</a></li>';
37 $a->page['aside'] .= $o;
46 function photos_post(&$a) {
50 notice( t('Permission denied.') . EOL );
54 if(($a->argc > 1) && (x($_POST,'desc') !== false)) {
55 $desc = notags(trim($_POST['desc']));
56 $tags = notags(trim($_POST['tags']));
57 $item_id = intval($_POST['item_id']);
60 $r = q("UPDATE `photo` SET `desc` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
63 intval($_SESSION['uid'])
65 $r = q("UPDATE `item` SET `tag` = '%s' WHERE `id` = %d AND `uid` = %d LIMIT 1",
68 intval($_SESSION['uid'])
71 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
77 $r = q("SELECT * FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
78 intval($_SESSION['uid'])
81 $contact_record = $r[0];
83 if(! x($_FILES,'userfile'))
86 if($_POST['partitionCount'])
91 $album = notags(trim($_POST['album']));
92 $newalbum = notags(trim($_POST['newalbum']));
94 if(! strlen($album)) {
98 $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
101 $str_group_allow = '';
102 $group_allow = $_POST['group_allow'];
103 if(is_array($group_allow)) {
104 array_walk($group_allow,'sanitise_acl');
105 $str_group_allow = implode('',$group_allow);
108 $str_contact_allow = '';
109 $contact_allow = $_POST['contact_allow'];
110 if(is_array($contact_allow)) {
111 array_walk($contact_allow,'sanitise_acl');
112 $str_contact_allow = implode('',$contact_allow);
115 $str_group_deny = '';
116 $group_deny = $_POST['group_deny'];
117 if(is_array($group_deny)) {
118 array_walk($group_deny,'sanitise_acl');
119 $str_group_deny = implode('',$group_deny);
122 $str_contact_deny = '';
123 $contact_deny = $_POST['contact_deny'];
124 if(is_array($contact_deny)) {
125 array_walk($contact_deny,'sanitise_acl');
126 $str_contact_deny = implode('',$contact_deny);
130 $src = $_FILES['userfile']['tmp_name'];
131 $filename = basename($_FILES['userfile']['name']);
132 $filesize = intval($_FILES['userfile']['size']);
134 $imagedata = @file_get_contents($src);
135 $ph = new Photo($imagedata);
137 if(! ($image = $ph->getImage())) {
138 notice( t('Unable to process image.') . EOL );
145 $width = $ph->getWidth();
146 $height = $ph->getHeight();
150 $photo_hash = hash('md5',uniqid(mt_rand(),true));
152 $r = $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
155 notice( t('Image upload failed.') . EOL );
159 if($width > 640 || $height > 640) {
160 $ph->scaleImage(640);
161 $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
165 if($width > 320 || $height > 320) {
166 $ph->scaleImage(320);
167 $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
171 $basename = basename($filename);
173 // Create item container
175 $body = '[url=' . $a->get_baseurl() . '/photos/' . $contact_record['nickname'] . '/image/' . $photo_hash . ']'
176 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]'
181 $item_hash = random_string();
183 $uri = "urn:X-dfrn:" . $a->get_hostname() . ':' . $profile_uid . ':' . $item_hash;
185 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
189 } while($dups == true);
192 $r = q("INSERT INTO `item` (`uid`, `type`, `resource-id`, `contact-id`,`owner-name`,`owner-link`,`owner-avatar`, `created`,
193 `edited`, `uri`, `parent-uri`, `title`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
194 VALUES( %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
195 intval($_SESSION['uid']),
198 intval($contact_record['id']),
199 dbesc($contact_record['name']),
200 dbesc($contact_record['url']),
201 dbesc($contact_record['thumb']),
208 dbesc($str_contact_allow),
209 dbesc($str_group_allow),
210 dbesc($str_contact_deny),
211 dbesc($str_group_deny)
216 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
220 q("UPDATE `item` SET `parent` = %d, `last-child` = 1 WHERE `id` = %d LIMIT 1",
227 // if album has no featured photo, promote one.
231 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
232 return; // NOTREACHED
236 return; // NOTREACHED
242 function photos_content(&$a) {
246 // photos/name/upload
247 // photos/name/album/xxxxx
248 // photos/name/album/xxxxx/edit
249 // photos/name/album/xxxxx/drop
250 // photos/name/image/xxxxx
251 // photos/name/image/xxxxx/edit
252 // photos/name/image/xxxxx/drop
254 if(! x($a->data,'user')) {
255 notice( t('No photos selected') . EOL );
259 $_SESSION['photo_return'] = $a->cmd;
266 $datatype = $a->argv[2];
267 $datum = $a->argv[3];
269 elseif(($a->argc > 2) && ($a->argv[2] == 'upload'))
270 $datatype = 'upload';
272 $datatype = 'summary';
280 // Setup permissions structures
283 $owner_uid = $a->data['user']['uid'];
286 $contact_id = $_SESSION['visitor_id'];
287 $groups = init_groups_visitor($contact_id);
290 // default permissions - anonymous user
292 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
294 // Profile owner - everything is visible
296 if(local_user() && ($_SESSION['uid'] == $owner_uid)) {
299 elseif(remote_user()) {
300 // authenticated visitor - here lie dragons
301 $gs = '<<>>'; // should be impossible to match
303 foreach($groups as $g)
304 $gs .= '|<' . intval($g) . '>';
306 $sql_extra = sprintf(
307 " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
308 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
309 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
310 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s') ",
312 intval($_SESSION['visitor_id']),
313 intval($_SESSION['visitor_id']),
324 if($datatype == 'upload') {
325 if( ! (local_user() && ($_SESSION['uid'] == $a->data['user']['uid']))) {
326 notice( t('Permission denied.'));
329 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
331 $albumselect .= '<option value="" selected="selected" > </option>';
332 if(count($a->data['albums'])) {
333 foreach($a->data['albums'] as $album) {
334 if(($album['album'] == '') || ($album['album'] == t('Contact Photos')))
336 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
339 $albumselect .= '</select>';
340 $tpl = file_get_contents('view/photos_upload.tpl');
341 $o .= replace_macros($tpl,array(
342 '$pagename' => t('Upload Photos'),
343 '$sessid' => session_id(),
344 '$newalbum' => t('New album name: '),
345 '$existalbumtext' => t('or existing album name: '),
346 '$filestext' => t('Select files to upload: '),
347 '$albumselect' => $albumselect,
348 '$permissions' => t('Permissions'),
349 '$aclselect' => populate_acl($a->user),
350 '$archive' => $a->get_baseurl() . '/jumploader_z.jar',
351 '$nojava' => t('Use the following controls only if the Java uploader (above) fails to launch.'),
352 '$uploadurl' => $a->get_baseurl() . '/photos',
353 '$submit' => t('Submit')
360 if($datatype == 'album') {
362 $album = hex2bin($datum);
364 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
365 $sql_extra GROUP BY `resource-id`",
366 intval($a->data['user']['uid']),
370 $a->set_pager_total(count($r));
373 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
374 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
375 intval($a->data['user']['uid']),
377 intval($a->pager['start']),
378 intval($a->pager['itemspage'])
381 $o .= '<h3>' . $album . '</h3>';
383 $tpl = file_get_contents('view/photo_album.tpl');
386 $o .= replace_macros($tpl,array(
388 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
389 '$phototitle' => t('View Photo'),
390 '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
391 '$imgalt' => $rr['filename']
395 $o .= '<div id="photo-album-end"></div>';
401 if($datatype == 'image') {
403 require_once('security.php');
404 require_once('bbcode.php');
406 // fetch image, item containing image, then comments
408 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
409 $sql_extra ORDER BY `scale` ASC ",
410 intval($a->data['user']['uid']),
415 notice( t('Photo not available') . EOL );
420 $hires = $lores = $ph[0];
422 if($ph[1]['scale'] == 2) {
423 // original is 640 or less, we can display it directly
424 $hires = $lores = $ph[0];
433 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']) . '">' . $ph[0]['album'] . '</a></h3>';
435 if(local_user() && ($ph[0]['uid'] == $_SESSION['uid'])) {
436 $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>';
440 $o .= '<a href="' . $a->get_baseurl() . '/photo/'
441 . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="'
442 . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/'
443 . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
446 // Do we have an item for this photo?
448 $i1 = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
453 $r = q("SELECT COUNT(*) AS `total`
454 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
455 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
456 AND NOT `item`.`type` IN ( 'remote', 'net-comment')
457 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
459 dbesc($i1[0]['uri']),
465 $a->set_pager_total($r[0]['total']);
468 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
469 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
470 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
471 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
472 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
473 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
474 AND NOT `item`.`type` IN ( 'remote', 'net-comment')
475 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
477 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
478 dbesc($i1[0]['uri']),
479 dbesc($i1[0]['uri']),
480 intval($a->pager['start']),
481 intval($a->pager['itemspage'])
486 $o .= '<div id="photo-caption" >' . $ph[0]['desc'] . '</div>';
488 if(strlen($i1[0]['tag'])) {
489 // parse tags and add links
490 $o .= '<div id="in-this-photo-text">' . t('In this photo: ') . '</div>';
491 $o .= '<div id="in-this-photo">' . $i1[0]['tag'] . '</div>';
495 $edit_tpl = file_get_contents('view/photo_edit.tpl');
496 $o .= replace_macros($edit_tpl, array(
497 '$id' => $ph[0]['id'],
498 '$capt_label' => t('Caption'),
499 '$caption' => $ph[0]['desc'],
500 '$tag_label' => t('Tags'),
501 '$tags' => $i1[0]['tag'],
502 '$item_id' => $i1[0]['id'],
503 '$submit' => t('Submit')
508 // pull out how many people like the photo
510 $cmnt_tpl = file_get_contents('view/comment_item.tpl');
511 $tpl = file_get_contents('view/photo_item.tpl');
512 $return_url = $a->cmd;
514 if(can_write_wall($a,$a->data['user']['uid'])) {
515 if($i1[0]['last-child']) {
516 $o .= replace_macros($cmnt_tpl,array(
517 '$return_path' => $return_url,
518 '$type' => 'wall-comment',
519 '$id' => $i1[0]['id'],
520 '$parent' => $i1[0]['id'],
521 '$profile_uid' => $a->data['user']['uid'],
530 foreach($r as $item) {
534 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
536 if(can_write_wall($a,$a->data['user']['uid'])) {
537 if($item['last-child']) {
538 $comment = replace_macros($cmnt_tpl,array(
539 '$return_path' => $return_url,
540 '$type' => 'wall-comment',
541 '$id' => $item['item_id'],
542 '$parent' => $item['parent'],
543 '$profile_uid' => $a->data['user']['uid'],
549 $profile_url = $item['url'];
552 if(local_user() && ($item['contact-uid'] == $_SESSION['uid']) && (strlen($item['dfrn-id'])) && (! $item['self'] ))
553 $profile_url = $redirect_url;
556 $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
557 $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
558 $profile_link = $profile_url;
562 if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == $_SESSION['uid']))
563 $drop = replace_macros(file_get_contents('view/wall_item_drop.tpl'), array('$id' => $item['id']));
566 $o .= replace_macros($template,array(
567 '$id' => $item['item_id'],
568 '$profile_url' => $profile_link,
569 '$name' => $profile_name,
570 '$thumb' => $profile_avatar,
571 '$title' => $item['title'],
572 '$body' => bbcode($item['body']),
573 '$ago' => relative_date($item['created']),
574 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
576 '$comment' => $comment
586 // Default - show recent photos with upload link (if applicable)
588 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s'
589 $sql_extra GROUP BY `resource-id`",
590 intval($a->data['user']['uid']),
591 dbesc( t('Contact Photos'))
594 $a->set_pager_total(count($r));
597 $r = q("SELECT `resource-id`, `album`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s'
598 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
599 intval($a->data['user']['uid']),
600 dbesc( t('Contact Photos')),
601 intval($a->pager['start']),
602 intval($a->pager['itemspage'])
605 $o .= '<h3>' . t('Recent Photos') . '</h3>';
607 if( local_user() && ($_SESSION['uid'] == $a->data['user']['uid'])) {
608 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/'
609 . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
612 $tpl = file_get_contents('view/photo_top.tpl');
615 $o .= replace_macros($tpl,array(
617 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname']
618 . '/image/' . $rr['resource-id'],
619 '$phototitle' => t('View Photo'),
620 '$imgsrc' => $a->get_baseurl() . '/photo/'
621 . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
622 '$albumlink' => $a->get_baseurl . '/photos/'
623 . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
624 '$albumname' => $rr['album'],
625 '$albumalt' => t('View Album'),
626 '$imgalt' => $rr['filename']
630 $o .= '<div id="photo-top-end"></div>';