lots of stuff that was needed for successful photo import from red

This commit is contained in:
redmatrix 2015-08-24 20:58:12 -07:00
parent 0edac8810b
commit 5fa2aa6d48
2 changed files with 80 additions and 16 deletions

View file

@ -414,9 +414,19 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
$hash = (($arr && $arr['hash']) ? $arr['hash'] : null);
$upload_path = (($arr && $arr['directory']) ? $arr['directory'] : '');
logger('arr: ' . print_r($arr,true));
$observer = array();
if(! perm_is_allowed($channel_id,get_observer_hash(), 'write_storage')) {
if($observer_hash) {
$x = q("select * from xchan where xchan_hash = '%s' limit 1",
dbesc($observer_hash)
);
if($x)
$observer = $x[0];
}
logger('arr: ' . print_r($arr,true));
if(! perm_is_allowed($channel_id,$observer_hash, 'write_storage')) {
$ret['message'] = t('Permission denied.');
return $ret;
}
@ -434,7 +444,13 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
// revise or update must provide $arr['hash'] of the thing to revise/update
if($options !== 'update') {
if($options === 'import') {
$src = $arr['src'];
$filename = $arr['filename'];
$filesize = @filesize($src);
$hash = $arr['resource_id'];
}
elseif($options !== 'update') {
if(! x($_FILES,'userfile')) {
$ret['message'] = t('No source file.');
return $ret;
@ -480,12 +496,19 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
$def_extension = '';
$is_photo = 0;
$gis = @getimagesize($src);
logger('getimagesize: ' . print_r($gis,true), LOGGER_DATA);
if(($gis) && ($gis[2] === IMAGETYPE_GIF || $gis[2] === IMAGETYPE_JPEG || $gis[2] === IMAGETYPE_PNG)) {
$is_photo = 1;
if($gis[2] === IMAGETYPE_GIF)
$def_extension = '.gif';
if($gis[2] === IMAGETYPE_JPEG)
$def_extension = '.jpg';
if($gis[2] === IMAGETYPE_PNG)
$def_extension = '.png';
}
$pathname = '';
@ -527,7 +550,7 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
$folder_hash = '';
}
if(! $options) {
if((! $options) || ($options === 'import')) {
// A freshly uploaded file. Check for duplicate and resolve with the channel's overwrite settings.
@ -544,9 +567,18 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
$hash = $x[0]['hash'];
}
else {
if(strpos($filename,'.') !== false) {
$basename = substr($filename,0,strrpos($filename,'.'));
$ext = substr($filename,strrpos($filename,'.'));
}
else {
$basename = $filename;
$ext = $def_extension;
}
$r = q("select filename from attach where ( filename = '%s' OR filename like '%s' ) and folder = '%s' ",
dbesc($filename),
dbesc($filename . '(%)'),
dbesc($basename . $ext),
dbesc($basename . '(%)' . $ext),
dbesc($folder_hash)
);
@ -556,7 +588,7 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
do {
$found = false;
foreach($r as $rr) {
if($rr['filename'] === $filename . '(' . $x . ')') {
if($rr['filename'] === $basename . '(' . $x . ')' . $ext) {
$found = true;
break;
}
@ -565,8 +597,10 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
$x++;
}
while($found);
$filename = $filename . '(' . $x . ')';
$filename = $basename . '(' . $x . ')' . $ext;
}
else
$filename = $basename . $ext;
}
}
}
@ -614,7 +648,16 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
if($src)
@file_put_contents($os_basepath . $os_relpath,@file_get_contents($src));
$created = datetime_convert();
if(array_key_exists('created', $arr))
$created = $arr['created'];
else
$created = datetime_convert();
if(array_key_exists('edited', $arr))
$edited = $arr['edited'];
else
$edited = $created;
if($options === 'replace') {
$r = q("update attach set filename = '%s', filetype = '%s', folder = '%s', filesize = %d, os_storage = %d, is_photo = %d, data = '%s', edited = '%s' where id = %d and uid = %d",
@ -704,7 +747,19 @@ function attach_store($channel, $observer_hash, $options = '', $arr = null) {
$args['contact_deny'] = $arr['contact_deny'];
if($arr['group_deny'])
$args['group_deny'] = $arr['group_deny'];
$p = photo_upload($channel,get_app()->get_observer(),$args);
if(array_key_exists('allow_cid',$arr))
$args['allow_cid'] = $arr['allow_cid'];
if(array_key_exists('allow_gid',$arr))
$args['allow_gid'] = $arr['allow_gid'];
if(array_key_exists('deny_cid',$arr))
$args['deny_cid'] = $arr['deny_cid'];
if(array_key_exists('deny_gid',$arr))
$args['deny_gid'] = $arr['deny_gid'];
$args['created'] = $created;
$args['edited'] = $edited;
$p = photo_upload($channel,$observer,$args);
if($p['success']) {
$ret['body'] = $p['body'];
}

View file

@ -52,12 +52,21 @@ function photo_upload($channel, $observer, $args) {
// 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.
// 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
$str_group_allow = $channel['channel_allow_gid'];
$str_contact_allow = $channel['channel_allow_cid'];
$str_group_deny = $channel['channel_deny_gid'];
$str_contact_deny = $channel['channel_deny_cid'];
if(array_key_exists('allow_cid',$args)) {
$str_group_allow = $args['allow_gid'];
$str_contact_allow = $args['allow_cid'];
$str_group_deny = $args['deny_gid'];
$str_contact_deny = $args['deny_cid'];
}
else {
$str_group_allow = $channel['channel_allow_gid'];
$str_contact_allow = $channel['channel_allow_cid'];
$str_group_deny = $channel['channel_deny_gid'];
$str_contact_deny = $channel['channel_deny_cid'];
}
if($args['directory']) {
$str_group_allow = $args['directory']['allow_gid'];