diff options
Diffstat (limited to 'include/photos.php')
-rw-r--r-- | include/photos.php | 378 |
1 files changed, 257 insertions, 121 deletions
diff --git a/include/photos.php b/include/photos.php index ca8c53679..c7360a956 100644 --- a/include/photos.php +++ b/include/photos.php @@ -7,6 +7,7 @@ require_once('include/permissions.php'); require_once('include/items.php'); require_once('include/photo/photo_driver.php'); +require_once('include/text.php'); /** * @brief @@ -18,44 +19,61 @@ require_once('include/photo/photo_driver.php'); */ function photo_upload($channel, $observer, $args) { + $a = get_app(); + $ret = array('success' => false); $channel_id = $channel['channel_id']; $account_id = $channel['channel_account_id']; - if(! perm_is_allowed($channel_id, $observer['xchan_hash'], 'post_photos')) { + if(! perm_is_allowed($channel_id, $observer['xchan_hash'], 'write_storage')) { $ret['message'] = t('Permission denied.'); return $ret; } - call_hooks('photo_upload_begin', $args); +// call_hooks('photo_upload_begin', $args); /* * Determine the album to use */ $album = $args['album']; - $newalbum = $args['newalbum']; - - logger('photo_upload: album= ' . $album . ' newalbum= ' . $newalbum , LOGGER_DEBUG); - - if(! $album) { - if($newalbum) - $album = $newalbum; - else - $album = datetime_convert('UTC',date_default_timezone_get(),'now', 'Y-m'); - } if(intval($args['visible']) || $args['visible'] === 'true') $visible = 1; else $visible = 0; - $str_group_allow = perms2str(((is_array($args['group_allow'])) ? $args['group_allow'] : explode(',',$args['group_allow']))); - $str_contact_allow = perms2str(((is_array($args['contact_allow'])) ? $args['contact_allow'] : explode(',',$args['contact_allow']))); - $str_group_deny = perms2str(((is_array($args['group_deny'])) ? $args['group_deny'] : explode(',',$args['group_deny']))); - $str_contact_deny = perms2str(((is_array($args['contact_deny'])) ? $args['contact_deny'] : explode(',',$args['contact_deny']))); + // Set to default channel permissions. If the parent directory (album) has permissions set, + // use those instead. If we have specific permissions supplied, they take precedence over + // all other settings. 'allow_cid' being passed from an external source takes priority over channel settings. + // ...messy... needs re-factoring once the photos/files integration stabilises + + $acl = new AccessList($channel); + if(array_key_exists('directory',$args) && $args['directory']) + $acl->set($args['directory']); + if(array_key_exists('allow_cid',$args)) + $acl->set($args); + if( (array_key_exists('group_allow',$args)) + || (array_key_exists('contact_allow',$args)) + || (array_key_exists('group_deny',$args)) + || (array_key_exists('contact_deny',$args))) { + $acl->set_from_array($args); + } + + $ac = $acl->get(); + + $os_storage = 0; - if ($args['data']) { + if($args['os_path'] && $args['getimagesize']) { + $imagedata = @file_get_contents($args['os_path']); + $filename = $args['filename']; + $filesize = strlen($imagedata); + // this is going to be deleted if it exists + $src = '/tmp/deletemenow'; + $type = $args['getimagesize']['mime']; + $os_storage = 1; + } + elseif ($args['data']) { // allow an import from a binary string representing the image. // This bypasses the upload step and max size limit checking @@ -69,7 +87,7 @@ function photo_upload($channel, $observer, $args) { } else { $f = array('src' => '', 'filename' => '', 'filesize' => 0, 'type' => ''); - call_hooks('photo_upload_file',$f); +// call_hooks('photo_upload_file',$f); if (x($f,'src') && x($f,'filesize')) { $src = $f['src']; @@ -132,7 +150,7 @@ function photo_upload($channel, $observer, $args) { return $ret; } - $exif = $ph->orient($src); + $exif = $ph->orient(($args['os_path']) ? $args['os_path'] : $src); @unlink($src); @@ -156,9 +174,10 @@ function photo_upload($channel, $observer, $args) { $errors = false; $p = array('aid' => $account_id, 'uid' => $channel_id, 'xchan' => $visitor, 'resource_id' => $photo_hash, - 'filename' => $filename, 'album' => $album, 'scale' => 0, 'photo_flags' => PHOTO_NORMAL, - 'allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow, - 'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny + 'filename' => $filename, 'album' => $album, 'scale' => 0, 'photo_usage' => PHOTO_NORMAL, + 'allow_cid' => $ac['allow_cid'], 'allow_gid' => $ac['allow_gid'], + 'deny_cid' => $ac['deny_cid'], 'deny_gid' => $ac['deny_gid'], + 'os_storage' => $os_storage, 'os_path' => $args['os_path'] ); if($args['created']) $p['created'] = $args['created']; @@ -169,27 +188,66 @@ function photo_upload($channel, $observer, $args) { if($args['description']) $p['description'] = $args['description']; + $link = array(); + + $r0 = $ph->save($p); + $link[0] = array( + 'rel' => 'alternate', + 'type' => 'text/html', + 'href' => $url = rawurlencode(z_root() . '/photo/' . $photo_hash . '-0.' . $ph->getExt()), + 'width' => $ph->getWidth(), + 'height' => $ph->getHeight() + ); + if(! $r0) + $errors = true; + + unset($p['os_storage']); + unset($p['os_path']); + + if(($width > 1024 || $height > 1024) && (! $errors)) + $ph->scaleImage(1024); + + $p['scale'] = 1; $r1 = $ph->save($p); + $link[1] = array( + 'rel' => 'alternate', + 'type' => 'text/html', + 'href' => $url = rawurlencode(z_root() . '/photo/' . $photo_hash . '-1.' . $ph->getExt()), + 'width' => $ph->getWidth(), + 'height' => $ph->getHeight() + ); if(! $r1) $errors = true; - - if(($width > 640 || $height > 640) && (! $errors)) { + + if(($width > 640 || $height > 640) && (! $errors)) $ph->scaleImage(640); - $p['scale'] = 1; - $r2 = $ph->save($p); - $smallest = 1; - if(! $r2) - $errors = true; - } - if(($width > 320 || $height > 320) && (! $errors)) { + $p['scale'] = 2; + $r2 = $ph->save($p); + $link[2] = array( + 'rel' => 'alternate', + 'type' => 'text/html', + 'href' => $url = rawurlencode(z_root() . '/photo/' . $photo_hash . '-2.' . $ph->getExt()), + 'width' => $ph->getWidth(), + 'height' => $ph->getHeight() + ); + if(! $r2) + $errors = true; + + if(($width > 320 || $height > 320) && (! $errors)) $ph->scaleImage(320); - $p['scale'] = 2; - $r3 = $ph->save($p); - $smallest = 2; - if(! $r3) - $errors = true; - } + + $p['scale'] = 3; + $r3 = $ph->save($p); + $link[3] = array( + 'rel' => 'alternate', + 'type' => 'text/html', + 'href' => $url = rawurlencode(z_root() . '/photo/' . $photo_hash . '-3.' . $ph->getExt()), + 'width' => $ph->getWidth(), + 'height' => $ph->getHeight() + ); + if(! $r3) + $errors = true; if($errors) { q("delete from photo where resource_id = '%s' and uid = %d", @@ -202,13 +260,7 @@ function photo_upload($channel, $observer, $args) { return $ret; } - // This will be the width and height of the smallest representation - - $width_x_height = $ph->getWidth() . 'x' . $ph->getHeight(); - - $mid = item_message_id(); - - // Create item container + $item_hidden = (($visible) ? 0 : 1 ); $lat = $lon = null; @@ -219,66 +271,138 @@ function photo_upload($channel, $observer, $args) { } } - $item_flags = ITEM_WALL|ITEM_ORIGIN|ITEM_THREAD_TOP; - $item_restrict = (($visible) ? ITEM_VISIBLE : ITEM_HIDDEN); - $title = ''; - $mid = item_message_id(); + $title = (($args['description']) ? $args['description'] : $args['filename']); + + $large_photos = feature_enabled($channel['channel_id'], 'large_photos'); + + linkify_tags($a, $args['body'], $channel_id); + + if($large_photos) { + $scale = 1; + $width = $link[1]['width']; + $height = $link[1]['height']; + $tag = (($r1) ? '[zmg=' . $width . 'x' . $height . ']' : '[zmg]'); - $arr = array(); - if($lat && $lon) - $arr['coord'] = $lat . ' ' . $lon; - - $arr['aid'] = $account_id; - $arr['uid'] = $channel_id; - $arr['mid'] = $mid; - $arr['parent_mid'] = $mid; - $arr['item_flags'] = $item_flags; - $arr['item_restrict'] = $item_restrict; - $arr['resource_type'] = 'photo'; - $arr['resource_id'] = $photo_hash; - $arr['owner_xchan'] = $channel['channel_hash']; - $arr['author_xchan'] = $observer['xchan_hash']; - $arr['title'] = $title; - $arr['allow_cid'] = $str_contact_allow; - $arr['allow_gid'] = $str_group_allow; - $arr['deny_cid'] = $str_contact_deny; - $arr['deny_gid'] = $str_group_deny; - $arr['verb'] = ACTIVITY_POST; - - $arr['plink'] = z_root() . '/channel/' . $channel['channel_address'] . '/?f=&mid=' . $arr['mid']; - - // We should also put a width_x_height on large photos. Left as an exercise for - // devs looking fo simple stuff to fix. - - $larger = feature_enabled($channel['channel_id'], 'large_photos'); - if($larger) { - $tag = '[zmg]'; - if($r2) - $smallest = 1; - else - $smallest = 0; } else { - if ($width_x_height) - $tag = '[zmg=' . $width_x_height. ']'; - else - $tag = '[zmg]'; + $scale = 2; + $width = $link[2]['width']; + $height = $link[2]['height']; + $tag = (($r2) ? '[zmg=' . $width . 'x' . $height . ']' : '[zmg]'); } - $arr['body'] = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo_hash . ']' - . $tag . z_root() . "/photo/{$photo_hash}-{$smallest}.".$ph->getExt() . '[/zmg]' - . '[/zrl]'; + $body = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo_hash . ']' + . $tag . z_root() . "/photo/{$photo_hash}-{$scale}." . $ph->getExt() . '[/zmg]' + . '[/zrl]'; - $result = item_store($arr); - $item_id = $result['item_id']; + // Create item object + $object = array( + 'type' => ACTIVITY_OBJ_PHOTO, + 'title' => $title, + 'id' => rawurlencode(z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo_hash), + 'link' => $link, + 'bbcode' => $body + ); - if($visible) - proc_run('php', "include/notifier.php", 'wall-new', $item_id); + // Create item container + if($args['item']) { + foreach($args['item'] as $i) { + + $item = get_item_elements($i); + $force = false; + + if($item['mid'] === $item['parent_mid']) { + + $item['body'] = (($object) ? $args['body'] : $body . "\r\n" . $args['body']); + $item['obj_type'] = (($object) ? ACTIVITY_OBJ_PHOTO : ''); + $item['object'] = (($object) ? json_encode($object) : ''); + + if($item['author_xchan'] === $channel['channel_hash']) { + $item['sig'] = base64url_encode(rsa_sign($item['body'],$channel['channel_prvkey'])); + $item['item_verified'] = 1; + } + else { + $item['sig'] = ''; + } + $force = true; + + } + $r = q("select id, edited from item where mid = '%s' and uid = %d limit 1", + dbesc($item['mid']), + intval($channel['channel_id']) + ); + if($r) { + if(($item['edited'] > $r[0]['edited']) || $force) { + $item['id'] = $r[0]['id']; + $item['uid'] = $channel['channel_id']; + item_store_update($item); + continue; + } + } + else { + $item['aid'] = $channel['channel_account_id']; + $item['uid'] = $channel['channel_id']; + $item_result = item_store($item); + } + } + } + else { + $mid = item_message_id(); + + $arr = array(); + + if($lat && $lon) + $arr['coord'] = $lat . ' ' . $lon; + + $arr['aid'] = $account_id; + $arr['uid'] = $channel_id; + $arr['mid'] = $mid; + $arr['parent_mid'] = $mid; + $arr['item_hidden'] = $item_hidden; + $arr['resource_type'] = 'photo'; + $arr['resource_id'] = $photo_hash; + $arr['owner_xchan'] = $channel['channel_hash']; + $arr['author_xchan'] = $observer['xchan_hash']; + $arr['title'] = $title; + $arr['allow_cid'] = $ac['allow_cid']; + $arr['allow_gid'] = $ac['allow_gid']; + $arr['deny_cid'] = $ac['deny_cid']; + $arr['deny_gid'] = $ac['deny_gid']; + $arr['verb'] = ACTIVITY_POST; + $arr['obj_type'] = (($object) ? ACTIVITY_OBJ_PHOTO : ''); + $arr['object'] = (($object) ? json_encode($object) : ''); + $arr['item_wall'] = 1; + $arr['item_origin'] = 1; + $arr['item_thread_top'] = 1; + $arr['item_private'] = intval($acl->is_private()); + $arr['plink'] = z_root() . '/channel/' . $channel['channel_address'] . '/?f=&mid=' . $arr['mid']; + $arr['body'] = (($object) ? $args['body'] : $body . "\r\n" . $args['body']); + + + // this one is tricky because the item and the photo have the same permissions, those of the photo. + // Use the channel read_stream permissions to get the correct public_policy for the item and recalculate the + // private flag accordingly. This may cause subtle bugs due to custom permissions roles. We want to use + // public policy when federating items to other sites, but should probably ignore them when accessing the item + // in the photos pages - using the photos permissions instead. We need the public policy to keep the photo + // linked item from leaking into the feed when somebody has a channel with read_stream restrictions. + + $arr['public_policy'] = map_scope($channel['channel_r_stream'],true); + if($arr['public_policy']) + $arr['item_private'] = 1; + + + + $result = item_store($arr); + $item_id = $result['item_id']; + + if($visible) + proc_run('php', "include/notifier.php", 'wall-new', $item_id); + } $ret['success'] = true; $ret['item'] = $arr; - $ret['body'] = $arr['body']; + $ret['body'] = $body; $ret['resource_id'] = $photo_hash; $ret['photoitem_id'] = $item_id; @@ -294,7 +418,7 @@ function photo_upload($channel, $observer, $args) { * * @param array $channel * @param array $observer - * @return bool|array false if no view_photos permission or an array + * @return bool|array false if no view_storage permission or an array * * success (bool) * * albums (array) */ @@ -303,14 +427,14 @@ function photos_albums_list($channel, $observer) { $channel_id = $channel['channel_id']; $observer_xchan = (($observer) ? $observer['xchan_hash'] : ''); - if(! perm_is_allowed($channel_id, $observer_xchan, 'view_photos')) + if(! perm_is_allowed($channel_id, $observer_xchan, 'view_storage')) return false; /** @FIXME create a permissions SQL which works on arbitrary observers and channels, regardless of login or web status */ $sql_extra = permissions_sql($channel_id); - $albums = q("SELECT count( distinct resource_id ) as total, album from photo where uid = %d and ( photo_flags = %d or photo_flags = %d ) $sql_extra group by album order by max(created) desc", + $albums = q("SELECT count( distinct resource_id ) as total, album from photo where uid = %d and photo_usage IN ( %d, %d ) $sql_extra group by album order by max(created) desc", intval($channel_id), intval(PHOTO_NORMAL), intval(PHOTO_PROFILE) @@ -325,7 +449,7 @@ function photos_albums_list($channel, $observer) { $ret['albums'] = array(); foreach($albums as $k => $album) { $entry = array( - 'text' => $album['album'], + 'text' => (($album['album']) ? $album['album'] : '/'), 'total' => $album['total'], 'url' => z_root() . '/photos/' . $channel['channel_address'] . '/album/' . bin2hex($album['album']), 'urlencode' => urlencode($album['album']), @@ -359,7 +483,7 @@ function photos_album_widget($channelx,$observer,$albums = null) { '$title' => t('Photo Albums'), '$albums' => $albums['albums'], '$baseurl' => z_root(), - '$upload' => ((perm_is_allowed($channelx['channel_id'],(($observer) ? $observer['xchan_hash'] : ''),'post_photos')) + '$upload' => ((perm_is_allowed($channelx['channel_id'],(($observer) ? $observer['xchan_hash'] : ''),'write_storage')) ? t('Upload New Photos') : '') )); } @@ -380,7 +504,7 @@ function photos_list_photos($channel, $observer, $album = '') { $channel_id = $channel['channel_id']; $observer_xchan = (($observer) ? $observer['xchan_hash'] : ''); - if(! perm_is_allowed($channel_id,$observer_xchan,'view_photos')) + if(! perm_is_allowed($channel_id,$observer_xchan,'view_storage')) return false; $sql_extra = permissions_sql($channel_id); @@ -390,7 +514,7 @@ function photos_list_photos($channel, $observer, $album = '') { $ret = array('success' => false); - $r = q("select resource_id, created, edited, title, description, album, filename, type, height, width, size, scale, profile, photo_flags, allow_cid, allow_gid, deny_cid, deny_gid from photo where uid = %d and ( photo_flags = %d or photo_flags = %d ) $sql_extra ", + $r = q("select resource_id, created, edited, title, description, album, filename, type, height, width, size, scale, photo_usage, allow_cid, allow_gid, deny_cid, deny_gid from photo where uid = %d and photo_usage in ( %d, %d ) $sql_extra ", intval($channel_id), intval(PHOTO_NORMAL), intval(PHOTO_PROFILE) @@ -488,32 +612,34 @@ function photos_create_item($channel, $creator_hash, $photo, $visible = false) { // Create item container - $item_flags = ITEM_WALL|ITEM_ORIGIN|ITEM_THREAD_TOP; - $item_restrict = (($visible) ? ITEM_VISIBLE : ITEM_HIDDEN); + + $item_hidden = (($visible) ? 0 : 1 ); $mid = item_message_id(); $arr = array(); - $arr['aid'] = $channel['channel_account_id']; - $arr['uid'] = $channel['channel_id']; - $arr['mid'] = $mid; - $arr['parent_mid'] = $mid; - $arr['item_flags'] = $item_flags; - $arr['item_restrict'] = $item_restrict; - $arr['resource_type'] = 'photo'; - $arr['resource_id'] = $photo['resource_id']; - $arr['owner_xchan'] = $channel['channel_hash']; - $arr['author_xchan'] = $creator_hash; - - $arr['allow_cid'] = $photo['allow_cid']; - $arr['allow_gid'] = $photo['allow_gid']; - $arr['deny_cid'] = $photo['deny_cid']; - $arr['deny_gid'] = $photo['deny_gid']; - - $arr['plink'] = z_root() . '/channel/' . $channel['channel_address'] . '/?f=&mid=' . $arr['mid']; - - $arr['body'] = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo['resource_id'] . ']' + $arr['aid'] = $channel['channel_account_id']; + $arr['uid'] = $channel['channel_id']; + $arr['mid'] = $mid; + $arr['parent_mid'] = $mid; + $arr['item_wall'] = 1; + $arr['item_origin'] = 1; + $arr['item_thread_top'] = 1; + $arr['item_hidden'] = $item_hidden; + $arr['resource_type'] = 'photo'; + $arr['resource_id'] = $photo['resource_id']; + $arr['owner_xchan'] = $channel['channel_hash']; + $arr['author_xchan'] = $creator_hash; + + $arr['allow_cid'] = $photo['allow_cid']; + $arr['allow_gid'] = $photo['allow_gid']; + $arr['deny_cid'] = $photo['deny_cid']; + $arr['deny_gid'] = $photo['deny_gid']; + + $arr['plink'] = z_root() . '/channel/' . $channel['channel_address'] . '/?f=&mid=' . $arr['mid']; + + $arr['body'] = '[zrl=' . z_root() . '/photos/' . $channel['channel_address'] . '/image/' . $photo['resource_id'] . ']' . '[zmg]' . z_root() . '/photo/' . $photo['resource_id'] . '-' . $photo['scale'] . '[/zmg]' . '[/zrl]'; @@ -535,6 +661,16 @@ function getGps($exifCoord, $hemi) { return floatval($flip * ($degrees + ($minutes / 60) + ($seconds / 3600))); } +function getGpstimestamp($exifCoord) { + + $hours = count($exifCoord) > 0 ? gps2Num($exifCoord[0]) : 0; + $minutes = count($exifCoord) > 1 ? gps2Num($exifCoord[1]) : 0; + $seconds = count($exifCoord) > 2 ? gps2Num($exifCoord[2]) : 0; + + return sprintf('%02d:%02d:%02d',$hours,$minutes,$seconds); +} + + function gps2Num($coordPart) { $parts = explode('/', $coordPart); |