basic browsing and file retrieval for webdav working - uploads not yet. A lot of permissions stuff is in place so it's marginally (but probably not completely) permission controlled

This commit is contained in:
friendica 2014-01-02 17:49:39 -08:00
parent ad08561d84
commit a1c198814d
2 changed files with 127 additions and 60 deletions

View File

@ -75,9 +75,8 @@ class RedDirectory extends DAV\Node implements DAV\ICollection {
private $red_path; private $red_path;
private $ext_path; private $ext_path;
private $root_dir = ''; private $root_dir = '';
// private $dir_key;
private $auth; private $auth;
// private $channel_id;
function __construct($ext_path,&$auth_plugin) { function __construct($ext_path,&$auth_plugin) {
@ -101,9 +100,10 @@ class RedDirectory extends DAV\Node implements DAV\ICollection {
return; return;
} }
if(! perm_is_allowed($this->auth->channel_id,$this->auth->observer,'view_storage')) if(! perm_is_allowed($this->auth->channel_id,$this->auth->observer,'view_storage')) {
return array(); throw new DAV\Exception\Forbidden('Permission denied.');
return;
}
return RedCollectionData($this->red_path,$this->auth); return RedCollectionData($this->red_path,$this->auth);
@ -130,8 +130,7 @@ class RedDirectory extends DAV\Node implements DAV\ICollection {
return new RedDirectory('/cloud', $this->auth); return new RedDirectory('/cloud', $this->auth);
} }
$x = RedFileData($this->ext_path . '/' . $name, $this->auth);
$x = RedFileData('/cloud' . (($this->red_path === '/') ? '' : '/') . '/' . $name, $this->auth);
logger('RedFileData returns: ' . print_r($x,true)); logger('RedFileData returns: ' . print_r($x,true));
if($x) if($x)
return $x; return $x;
@ -141,16 +140,29 @@ class RedDirectory extends DAV\Node implements DAV\ICollection {
function getName() { function getName() {
logger('RedDirectory::getName : ' . print_r($this,true)); logger('RedDirectory::getName : ' . print_r($this,true));
logger('RedDirectory::getName returns: ' . basename($this->red_path));
return (basename($this->red_path)); return (basename($this->red_path));
} }
function createFile($name,$data = null) { function createFile($name,$data = null) {
if(! perm_is_allowed($this->auth->channel_id,$this->auth->observer,'write_storage')) {
throw new DAV\Exception\Forbidden('Permission denied.');
return;
}
} }
function createDirectory($name) { function createDirectory($name) {
if(! perm_is_allowed($this->auth->channel_id,$this->auth->observer,'write_storage')) {
throw new DAV\Exception\Forbidden('Permission denied.');
return;
}
@ -161,12 +173,13 @@ class RedDirectory extends DAV\Node implements DAV\ICollection {
logger('RedDirectory::childExists : ' . print_r($this->auth,true)); logger('RedDirectory::childExists : ' . print_r($this->auth,true));
$r = q("select distinct filename from attach where folder = '%s' and filename = '%s' and uid = %d group by filename", if($this->red_path === '/' && $name === 'cloud') {
dbesc($this->dir_key), return true;
dbesc($name), }
intval($this->auth->channel_id)
); $x = RedFileData($this->ext_path . '/' . $name, $this->auth);
if($r) logger('RedFileData returns: ' . print_r($x,true));
if($x)
return true; return true;
return false; return false;
@ -181,65 +194,101 @@ class RedFile extends DAV\Node implements DAV\IFile {
private $auth; private $auth;
private $name; private $name;
function __construct($name, &$auth) { function __construct($name, $data, &$auth) {
logger('RedFile::_construct: ' . $name); logger('RedFile::_construct: ' . $name);
$this->name = $name; $this->name = $name;
$this->data = $data;
$this->auth = $auth; $this->auth = $auth;
$this->data = RedFileData($name,$auth);
logger('RedFile::_construct: ' . print_r($this->data,true)); logger('RedFile::_construct: ' . print_r($this->data,true));
} }
function getName() { function getName() {
logger('RedFile::getName'); logger('RedFile::getName: ' . basename($this->name));
return basename($data); return basename($this->name);
} }
function put($data) {
function setName($newName) {
logger('RedFile::setName: ' . basename($this->name) . ' -> ' . $newName);
if((! $newName) || (! perm_is_allowed($this->auth->channel_id,$this->auth->observer,'write_storage'))) {
throw new DAV\Exception\Forbidden('Permission denied.');
return;
}
$newName = str_replace('/','%2F',$newName);
$r = q("update attach set filename = '%s' where hash = '%s' and id = %d limit 1",
dbesc($this->data['filename']),
intval($this->data['id'])
);
}
function put($data) {
logger('RedFile::put: ' . basename($this->name));
$r = q("update attach set data = '%s' where hash = '%s' and uid = %d limit 1",
dbesc($data),
dbesc($this->data['hash']),
intval($this->data['uid'])
);
} }
function get() { function get() {
logger('RedFile::get: ' . basename($this->name));
$r = q("select data from attach where hash = '%s' and uid = %d limit 1",
dbesc($this->data['hash']),
intval($this->data['uid'])
);
if($r) return $r[0]['data'];
} }
function getETag() { function getETag() {
logger('RedFile::getETag: ' . basename($this->name));
return $this->data['hash'];
} }
function getContentType() { function getContentType() {
$type = 'text/plain'; return $this->data['filetype'];
return $type;
// return $this->data['filetype'];
} }
function getSize() { function getSize() {
return 33122; return $this->data['filesize'];
// return $this->data['filesize'];
} }
function getLastModified() {
logger('RedFile::getLastModified: ' . basename($this->name));
return $this->data['edited'];
}
} }
function RedChannelList(&$auth) { function RedChannelList(&$auth) {
$ret = array(); $ret = array();
$r = q("select channel_address from channel where not (channel_pageflags & %d)", $r = q("select channel_id, channel_address from channel where not (channel_pageflags & %d)",
intval(PAGE_REMOVED) intval(PAGE_REMOVED)
); );
if($r) { if($r) {
foreach($r as $rr) { foreach($r as $rr) {
$ret[] = new RedDirectory('/cloud/' . $rr['channel_address'],$auth); if(perm_is_allowed($rr['channel_id'],$auth->observer,'view_storage')) {
$ret[] = new RedDirectory('/cloud/' . $rr['channel_address'],$auth);
}
} }
} }
return $ret; return $ret;
@ -251,12 +300,14 @@ function RedCollectionData($file,&$auth) {
$ret = array(); $ret = array();
$x = strpos($file,'/cloud'); $x = strpos($file,'/cloud');
if($x === 0) { if($x === 0) {
$file = substr($file,6); $file = substr($file,6);
} }
logger('RedCollectionData: ' . $file);
if((! $file) || ($file === '/')) { if((! $file) || ($file === '/')) {
return RedChannelList($auth); return RedChannelList($auth);
@ -270,9 +321,12 @@ function RedCollectionData($file,&$auth) {
$channel_name = $path_arr[0]; $channel_name = $path_arr[0];
$r = q("select channel_id from channel where channel_name = '%s' limit 1", $r = q("select channel_id from channel where channel_address = '%s' limit 1",
dbesc($channel_name) dbesc($channel_name)
); );
logger('dbg1: ' . print_r($r,true));
if(! $r) if(! $r)
return null; return null;
@ -283,7 +337,7 @@ function RedCollectionData($file,&$auth) {
$folder = ''; $folder = '';
for($x = 1; $x < count($path_arr); $x ++) { for($x = 1; $x < count($path_arr); $x ++) {
$r = q("hash, filename, flags from attach where folder = '%s' and (flags & %d)", $r = q("select id, hash, filename, flags from attach where folder = '%s' and (flags & %d)",
dbesc($folder), dbesc($folder),
intval($channel_id), intval($channel_id),
intval(ATTACH_FLAG_DIR) intval(ATTACH_FLAG_DIR)
@ -294,6 +348,8 @@ function RedCollectionData($file,&$auth) {
} }
} }
logger('dbg2: ' . print_r($r,true));
if($path !== '/' . $file) { if($path !== '/' . $file) {
logger("RedCollectionData: Path mismatch: $path !== /$file"); logger("RedCollectionData: Path mismatch: $path !== /$file");
return NULL; return NULL;
@ -301,17 +357,19 @@ function RedCollectionData($file,&$auth) {
$ret = array(); $ret = array();
$r = q("select filename from attach where folder = '%s' group by filename",
$r = q("select id, uid, hash, filename, filetype, filesize, revision, folder, flags, created, edited from attach where folder = '%s' and uid = %d group by filename",
dbesc($folder), dbesc($folder),
intval($channel_id), intval($channel_id)
intval(ATTACH_FLAG_DIR)
); );
logger('dbg2: ' . print_r($r,true));
foreach($r as $rr) { foreach($r as $rr) {
if($rr['flags'] & ATTACH_FLAG_DIR) if($rr['flags'] & ATTACH_FLAG_DIR)
$ret[] = new RedDirectory('/cloud' . $path . '/' . $rr['filename'],$auth); $ret[] = new RedDirectory('/cloud' . $path . '/' . $rr['filename'],$auth);
else else
$ret[] = newRedFile('/cloud' . $path . '/' . $rr['filename'],$auth); $ret[] = new RedFile('/cloud' . $path . '/' . $rr['filename'],$rr,$auth);
} }
return $ret; return $ret;
@ -347,13 +405,13 @@ logger('file=' . $file);
logger("file = $file - path = " . print_r($path_arr,true)); logger("file = $file - path = " . print_r($path_arr,true));
$channel_name = $path_arr[0]; $channel_name = $path_arr[0];
//dbg(1);
$r = q("select channel_id from channel where channel_address = '%s' limit 1", $r = q("select channel_id from channel where channel_address = '%s' limit 1",
dbesc($channel_name) dbesc($channel_name)
); );
//dbg(0); logger('dbg0: ' . print_r($r,true));
if(! $r) if(! $r)
return null; return null;
@ -364,46 +422,55 @@ logger('file=' . $file);
$folder = ''; $folder = '';
//dbg(1); //dbg(1);
require_once('include/security.php');
$perms = permissions_sql($channel_id);
$errors = false;
for($x = 1; $x < count($path_arr); $x ++) { for($x = 1; $x < count($path_arr); $x ++) {
$r = q("hash, filename, flags from attach where folder = '%s' and uid = %d and (flags & %d)", dbg(1);
$r = q("select id, hash, filename, flags from attach where folder = '%s' and uid = %d and (flags & %d) $perms",
dbesc($folder), dbesc($folder),
intval($channel_id), intval($channel_id),
intval(ATTACH_FLAG_DIR) intval(ATTACH_FLAG_DIR)
); );
dbg(0);
logger('dbg1: ' . print_r($r,true));
if($r && ( $r[0]['flags'] & ATTACH_FLAG_DIR)) { if($r && ( $r[0]['flags'] & ATTACH_FLAG_DIR)) {
$folder = $r[0]['hash']; $folder = $r[0]['hash'];
$path = $path . '/' . $r[0]['filename']; $path = $path . '/' . $r[0]['filename'];
} }
if(! $r) {
$r = q("select id, uid, hash, filename, filetype, filesize, revision, folder, flags, created, edited from attach
where folder = '%s' and filename = '%s' and uid = %d $perms group by filename limit 1",
dbesc($folder),
basename($file),
intval($channel_id)
);
}
if(! $r)
$errors = true;
} }
//dbg(0);
logger('dbg1: ' . print_r($r,true));
if($path === '/' . $file) { if($path === '/' . $file) {
// final component was a directory. // final component was a directory.
return new RedDirectory('/cloud/' . $file,$auth); return new RedDirectory('/cloud/' . $file,$auth);
} }
// //if($path !== dirname($file)) { if($errors) {
// logger("RedFileData: Path mismatch: $path !== dirname($file)"); throw new DAV\Exception\Forbidden('Permission denied.');
// return NULL; return;
// }
$ret = array();
//dbg(1);
$r = q("select filename from attach where folder = '%s' and filename = '%s' and uid = %d group by filename limit 1",
dbesc($folder),
basename($file),
intval($channel_id)
);
//dbg(0);
foreach($r as $rr) {
if($rr['flags'] & ATTACH_FLAG_DIR)
$ret[] = new RedDirectory($path . '/' . $rr['filename'],$auth);
else
$ret[] = newRedFile($path . '/' . $rr['filename'],$auth);
} }
return $ret[0]; if($r[0]['flags'] & ATTACH_FLAG_DIR)
return new RedDirectory('/cloud' . $path . '/' . $r[0]['filename'],$auth);
else
return new RedFile('/cloud' . $path . '/' . $r[0]['filename'],$r[0],$auth);
} }

View File

@ -102,7 +102,7 @@ function cloud_init(&$a) {
$auth = new RedBasicAuth(); $auth = new RedBasicAuth();
$rootDirectory = new RedDirectory('/cloud',$auth); $rootDirectory = new RedDirectory('/',$auth);
$server = new DAV\Server($rootDirectory); $server = new DAV\Server($rootDirectory);
$lockBackend = new DAV\Locks\Backend\File('store/data/locks'); $lockBackend = new DAV\Locks\Backend\File('store/data/locks');
$lockPlugin = new DAV\Locks\Plugin($lockBackend); $lockPlugin = new DAV\Locks\Plugin($lockBackend);