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 $r = q("SELECT * FROM `contact` LEFT JOIN `user` ON `user`.`uid` = `contact`.`uid` WHERE `user`.`uid` = %d AND `self` = 1 LIMIT 1",
55 intval($_SESSION['uid'])
58 $contact_record = $r[0];
60 if(! x($_FILES,'userfile'))
63 if($_POST['partitionCount'])
68 $album = notags(trim($_POST['album']));
69 $newalbum = notags(trim($_POST['newalbum']));
71 if(! strlen($album)) {
75 $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y');
78 $str_group_allow = '';
79 $group_allow = $_POST['group_allow'];
80 if(is_array($group_allow)) {
81 array_walk($group_allow,'sanitise_acl');
82 $str_group_allow = implode('',$group_allow);
85 $str_contact_allow = '';
86 $contact_allow = $_POST['contact_allow'];
87 if(is_array($contact_allow)) {
88 array_walk($contact_allow,'sanitise_acl');
89 $str_contact_allow = implode('',$contact_allow);
93 $group_deny = $_POST['group_deny'];
94 if(is_array($group_deny)) {
95 array_walk($group_deny,'sanitise_acl');
96 $str_group_deny = implode('',$group_deny);
99 $str_contact_deny = '';
100 $contact_deny = $_POST['contact_deny'];
101 if(is_array($contact_deny)) {
102 array_walk($contact_deny,'sanitise_acl');
103 $str_contact_deny = implode('',$contact_deny);
107 $src = $_FILES['userfile']['tmp_name'];
108 $filename = basename($_FILES['userfile']['name']);
109 $filesize = intval($_FILES['userfile']['size']);
111 $imagedata = @file_get_contents($src);
112 $ph = new Photo($imagedata);
114 if(! ($image = $ph->getImage())) {
115 notice( t('Unable to process image.') . EOL );
122 $width = $ph->getWidth();
123 $height = $ph->getHeight();
127 $photo_hash = hash('md5',uniqid(mt_rand(),true));
129 $r = $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 0 , 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
132 notice( t('Image upload failed.') . EOL );
136 if($width > 640 || $height > 640) {
137 $ph->scaleImage(640);
138 $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 1, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
142 if($width > 320 || $height > 320) {
143 $ph->scaleImage(320);
144 $ph->store($_SESSION['uid'], 0, $photo_hash, $filename, $album, 2, 0, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny);
148 $basename = basename($filename);
150 // Create item container
152 $body = '[url=' . $a->get_baseurl() . '/photos/' . $contact_record['nickname'] . '/image/' . $photo_hash . ']'
153 . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.jpg" . '[/img]'
158 $item_hash = random_string();
160 $uri = "urn:X-dfrn:" . $a->get_hostname() . ':' . $profile_uid . ':' . $item_hash;
162 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
166 } while($dups == true);
169 $r = q("INSERT INTO `item` (`uid`, `type`, `resource-id`, `contact-id`,`owner-name`,`owner-link`,`owner-avatar`, `created`,
170 `edited`, `uri`, `parent-uri`, `title`, `body`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`)
171 VALUES( %d, '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )",
172 intval($_SESSION['uid']),
175 intval($contact_record['id']),
176 dbesc($contact_record['name']),
177 dbesc($contact_record['url']),
178 dbesc($contact_record['thumb']),
185 dbesc($str_contact_allow),
186 dbesc($str_group_allow),
187 dbesc($str_contact_deny),
188 dbesc($str_group_deny)
193 $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
197 q("UPDATE `item` SET `parent` = %d, `last-child` = 1 WHERE `id` = %d LIMIT 1",
204 // if album has no featured photo, promote one.
208 goaway($a->get_baseurl() . '/' . $_SESSION['photo_return']);
209 return; // NOTREACHED
213 return; // NOTREACHED
219 function photos_content(&$a) {
223 // photos/name/upload
224 // photos/name/album/xxxxx
225 // photos/name/album/xxxxx/edit
226 // photos/name/album/xxxxx/drop
227 // photos/name/image/xxxxx
228 // photos/name/image/xxxxx/edit
229 // photos/name/image/xxxxx/drop
231 if(! x($a->data,'user')) {
232 notice( t('No photos selected') . EOL );
236 $_SESSION['photo_return'] = $a->cmd;
243 $datatype = $a->argv[2];
244 $datum = $a->argv[3];
246 elseif(($a->argc > 2) && ($a->argv[2] == 'upload'))
247 $datatype = 'upload';
249 $datatype = 'summary';
257 // Setup permissions structures
260 $owner_uid = $a->data['user']['uid'];
263 $contact_id = $_SESSION['visitor_id'];
264 $groups = init_groups_visitor($contact_id);
267 // default permissions - anonymous user
269 $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid` = '' AND `deny_gid` = '' ";
271 // Profile owner - everything is visible
273 if(local_user() && ($_SESSION['uid'] == $owner_uid)) {
276 elseif(remote_user()) {
277 // authenticated visitor - here lie dragons
278 $gs = '<<>>'; // should be impossible to match
280 foreach($groups as $g)
281 $gs .= '|<' . intval($g) . '>';
283 $sql_extra = sprintf(
284 " AND ( `allow_cid` = '' OR `allow_cid` REGEXP '<%d>' )
285 AND ( `deny_cid` = '' OR NOT `deny_cid` REGEXP '<%d>' )
286 AND ( `allow_gid` = '' OR `allow_gid` REGEXP '%s' )
287 AND ( `deny_gid` = '' OR NOT `deny_gid` REGEXP '%s') ",
289 intval($_SESSION['visitor_id']),
290 intval($_SESSION['visitor_id']),
301 if($datatype == 'upload') {
302 if( ! (local_user() && ($_SESSION['uid'] == $a->data['user']['uid']))) {
303 notice( t('Permission denied.'));
306 $albumselect = '<select id="photos-upload-album-select" name="album" size="4">';
308 $albumselect .= '<option value="" selected="selected" > </option>';
309 if(count($a->data['albums'])) {
310 foreach($a->data['albums'] as $album) {
311 if(($album['album'] == '') || ($album['album'] == t('Contact Photos')))
313 $albumselect .= '<option value="' . $album['album'] . '">' . $album['album'] . '</option>';
316 $albumselect .= '</select>';
317 $tpl = file_get_contents('view/photos_upload.tpl');
318 $o .= replace_macros($tpl,array(
319 '$pagename' => t('Upload Photos'),
320 '$sessid' => session_id(),
321 '$newalbum' => t('New album name: '),
322 '$existalbumtext' => t('or existing album name: '),
323 '$filestext' => t('Select files to upload: '),
324 '$albumselect' => $albumselect,
325 '$permissions' => t('Permissions'),
326 '$aclselect' => populate_acl(),
327 '$archive' => $a->get_baseurl() . '/jumploader_z.jar',
328 '$nojava' => t('Use the following controls only if the Java uploader (above) fails to launch.'),
329 '$uploadurl' => $a->get_baseurl() . '/photos',
330 '$submit' => t('Submit')
337 if($datatype == 'album') {
339 $album = hex2bin($datum);
341 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
342 $sql_extra GROUP BY `resource-id`",
343 intval($a->data['user']['uid']),
347 $a->set_pager_total(count($r));
350 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` = '%s'
351 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
352 intval($a->data['user']['uid']),
354 intval($a->pager['start']),
355 intval($a->pager['itemspage'])
358 $o .= '<h3>' . $album . '</h3>';
360 $tpl = file_get_contents('view/photo_album.tpl');
363 $o .= replace_macros($tpl,array(
365 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $rr['resource-id'],
366 '$phototitle' => t('View Photo'),
367 '$imgsrc' => $a->get_baseurl() . '/photo/' . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
368 '$imgalt' => $rr['filename']
372 $o .= '<div id="photo-album-end"></div>';
378 if($datatype == 'image') {
380 require_once('security.php');
381 require_once('bbcode.php');
383 // fetch image, item containing image, then comments
385 $ph = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'
386 $sql_extra ORDER BY `scale` ASC ",
387 intval($a->data['user']['uid']),
392 notice( t('Photo not available') . EOL );
397 $hires = $lores = $ph[0];
399 if($ph[1]['scale'] == 2) {
400 // original is 640 or less, we can display it directly
401 $hires = $lores = $ph[0];
410 $o .= '<h3>' . '<a href="' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/album/' . bin2hex($ph[0]['album']) . '">' . $ph[0]['album'] . '</a></h3>';
412 $o .= '<a href="' . $a->get_baseurl() . '/photo/'
413 . $hires['resource-id'] . '-' . $hires['scale'] . '.jpg" title="'
414 . t('View Full Size') . '" ><img src="' . $a->get_baseurl() . '/photo/'
415 . $lores['resource-id'] . '-' . $lores['scale'] . '.jpg' . '" /></a>';
417 // Do we have an item for this photo?
419 $i1 = q("SELECT * FROM `item` WHERE `resource-id` = '%s' $sql_extra LIMIT 1",
424 $r = q("SELECT COUNT(*) AS `total`
425 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
426 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
427 AND NOT `item`.`type` IN ( 'remote', 'net-comment')
428 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
430 dbesc($i1[0]['uri']),
436 $a->set_pager_total($r[0]['total']);
439 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
440 `contact`.`name`, `contact`.`photo`, `contact`.`url`,
441 `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
442 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
443 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
444 WHERE `parent-uri` = '%s' AND `uri` != '%s' AND `item`.`deleted` = 0
445 AND NOT `item`.`type` IN ( 'remote', 'net-comment')
446 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
448 ORDER BY `parent` DESC, `id` ASC LIMIT %d ,%d ",
449 dbesc($i1[0]['uri']),
450 dbesc($i1[0]['uri']),
451 intval($a->pager['start']),
452 intval($a->pager['itemspage'])
457 $o .= '<div id="photo-caption" >' . $ph[0]['desc'] . '</div>';
459 if(strlen($i1[0]['tag'])) {
460 // parse tags and add links
461 $o .= '<div id="in-this-photo-text">' . t('In this photo: ') . '</div>';
462 $o .= '<div id="in-this-photo">' . $i1[0]['tag'] . '</div>';
466 $edit_tpl = file_get_contents('view/photo_edit.tpl');
467 $o .= replace_macros($edit_tpl, array(
468 '$id' => $ph[0]['id']
472 // pull out how many people like the photo
474 $cmnt_tpl = file_get_contents('view/comment_item.tpl');
475 $tpl = file_get_contents('view/photo_item.tpl');
476 $return_url = $a->cmd;
478 if(can_write_wall($a,$a->data['user']['uid'])) {
479 if($i1[0]['last-child']) {
480 $o .= replace_macros($cmnt_tpl,array(
481 '$return_path' => $return_url,
482 '$type' => 'wall-comment',
483 '$id' => $i1[0]['id'],
484 '$parent' => $i1[0]['id'],
485 '$profile_uid' => $a->data['user']['uid'],
494 foreach($r as $item) {
498 $redirect_url = $a->get_baseurl() . '/redir/' . $item['cid'] ;
500 if(can_write_wall($a,$a->data['user']['uid'])) {
501 if($item['last-child']) {
502 $comment = replace_macros($cmnt_tpl,array(
503 '$return_path' => $return_url,
504 '$type' => 'wall-comment',
505 '$id' => $item['item_id'],
506 '$parent' => $item['parent'],
507 '$profile_uid' => $a->data['user']['uid'],
513 $profile_url = $item['url'];
516 if(local_user() && ($item['contact-uid'] == $_SESSION['uid']) && (strlen($item['dfrn-id'])) && (! $item['self'] ))
517 $profile_url = $redirect_url;
520 $profile_name = ((strlen($item['author-name'])) ? $item['author-name'] : $item['name']);
521 $profile_avatar = ((strlen($item['author-avatar'])) ? $item['author-avatar'] : $item['thumb']);
522 $profile_link = $profile_url;
526 if(($item['contact-id'] == $_SESSION['visitor_id']) || ($item['uid'] == $_SESSION['uid']))
527 $drop = replace_macros(file_get_contents('view/wall_item_drop.tpl'), array('$id' => $item['id']));
530 $o .= replace_macros($template,array(
531 '$id' => $item['item_id'],
532 '$profile_url' => $profile_link,
533 '$name' => $profile_name,
534 '$thumb' => $profile_avatar,
535 '$title' => $item['title'],
536 '$body' => bbcode($item['body']),
537 '$ago' => relative_date($item['created']),
538 '$indent' => (($item['parent'] != $item['item_id']) ? ' comment' : ''),
540 '$comment' => $comment
550 // Default - show recent photos with upload link (if applicable)
552 $r = q("SELECT `resource-id`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s'
553 $sql_extra GROUP BY `resource-id`",
554 intval($a->data['user']['uid']),
555 dbesc( t('Contact Photos'))
558 $a->set_pager_total(count($r));
561 $r = q("SELECT `resource-id`, `album`, max(`scale`) AS `scale` FROM `photo` WHERE `uid` = %d AND `album` != '%s'
562 $sql_extra GROUP BY `resource-id` ORDER BY `created` DESC LIMIT %d , %d",
563 intval($a->data['user']['uid']),
564 dbesc( t('Contact Photos')),
565 intval($a->pager['start']),
566 intval($a->pager['itemspage'])
569 $o .= '<h3>' . t('Recent Photos') . '</h3>';
571 if( local_user() && ($_SESSION['uid'] == $a->data['user']['uid'])) {
572 $o .= '<div id="photo-top-links"><a id="photo-top-upload-link" href="'. $a->get_baseurl() . '/photos/'
573 . $a->data['user']['nickname'] . '/upload' . '">' . t('Upload New Photos') . '</a></div>';
576 $tpl = file_get_contents('view/photo_top.tpl');
579 $o .= replace_macros($tpl,array(
581 '$photolink' => $a->get_baseurl() . '/photos/' . $a->data['user']['nickname']
582 . '/image/' . $rr['resource-id'],
583 '$phototitle' => t('View Photo'),
584 '$imgsrc' => $a->get_baseurl() . '/photo/'
585 . $rr['resource-id'] . '-' . $rr['scale'] . '.jpg',
586 '$albumlink' => $a->get_baseurl . '/photos/'
587 . $a->data['user']['nickname'] . '/album/' . bin2hex($rr['album']),
588 '$albumname' => $rr['album'],
589 '$albumalt' => t('View Album'),
590 '$imgalt' => $rr['filename']
594 $o .= '<div id="photo-top-end"></div>';