2013-10-26 21:48:03 +00:00
< ? php /** @file */
use Sabre\DAV ;
2014-01-02 00:07:36 +00:00
require_once ( 'vendor/autoload.php' );
2013-10-26 21:48:03 +00:00
2014-01-03 09:44:25 +00:00
require_once ( 'include/attach.php' );
2014-01-21 23:38:02 +00:00
class RedDirectory extends DAV\Node implements DAV\ICollection , DAV\IQuota {
2013-10-26 21:48:03 +00:00
private $red_path ;
2014-01-06 00:00:05 +00:00
private $folder_hash ;
2014-01-02 09:09:57 +00:00
private $ext_path ;
2014-01-02 00:07:36 +00:00
private $root_dir = '' ;
2013-10-26 21:48:03 +00:00
private $auth ;
2014-01-06 22:33:49 +00:00
private $os_path = '' ;
2013-10-26 21:48:03 +00:00
2014-01-02 09:09:57 +00:00
function __construct ( $ext_path , & $auth_plugin ) {
2014-01-09 21:54:18 +00:00
logger ( 'RedDirectory::__construct() ' . $ext_path , LOGGER_DEBUG );
2014-01-02 09:09:57 +00:00
$this -> ext_path = $ext_path ;
$this -> red_path = (( strpos ( $ext_path , '/cloud' ) === 0 ) ? substr ( $ext_path , 6 ) : $ext_path );
if ( ! $this -> red_path )
$this -> red_path = '/' ;
2013-10-26 21:48:03 +00:00
$this -> auth = $auth_plugin ;
2014-01-06 00:00:05 +00:00
$this -> folder_hash = '' ;
2014-01-02 09:09:57 +00:00
2014-01-06 00:00:05 +00:00
$this -> getDir ();
2014-01-09 21:54:18 +00:00
2014-01-06 03:25:56 +00:00
if ( $this -> auth -> browser )
$this -> auth -> browser -> set_writeable ();
2014-01-02 00:07:36 +00:00
2013-10-26 21:48:03 +00:00
}
2014-02-12 08:04:35 +00:00
function log () {
logger ( 'RedDirectory::log() ext_path ' . $this -> ext_path , LOGGER_DATA );
logger ( 'RedDirectory::log() os_path ' . $this -> os_path , LOGGER_DATA );
logger ( 'RedDirectory::log() red_path ' . $this -> red_path , LOGGER_DATA );
}
2013-10-26 21:48:03 +00:00
function getChildren () {
2014-02-12 08:04:35 +00:00
logger ( 'RedDirectory::getChildren() called for ' . $this -> ext_path , LOGGER_DATA );
$this -> log ();
2014-01-02 09:09:57 +00:00
if ( get_config ( 'system' , 'block_public' ) && ( ! $this -> auth -> channel_id ) && ( ! $this -> auth -> observer )) {
throw new DAV\Exception\Forbidden ( 'Permission denied.' );
return ;
2014-01-02 00:07:36 +00:00
}
2014-01-06 00:37:07 +00:00
if (( $this -> auth -> owner_id ) && ( ! perm_is_allowed ( $this -> auth -> owner_id , $this -> auth -> observer , 'view_storage' ))) {
2014-01-03 01:49:39 +00:00
throw new DAV\Exception\Forbidden ( 'Permission denied.' );
return ;
}
2014-01-02 00:07:36 +00:00
2014-01-06 00:37:07 +00:00
$contents = RedCollectionData ( $this -> red_path , $this -> auth );
return $contents ;
2013-10-26 21:48:03 +00:00
}
function getChild ( $name ) {
2014-01-02 00:07:36 +00:00
2014-01-09 21:54:18 +00:00
logger ( 'RedDirectory::getChild : ' . $name , LOGGER_DATA );
2014-01-02 00:07:36 +00:00
2014-01-02 09:09:57 +00:00
if ( get_config ( 'system' , 'block_public' ) && ( ! $this -> auth -> channel_id ) && ( ! $this -> auth -> observer )) {
2013-10-28 01:35:40 +00:00
throw new DAV\Exception\Forbidden ( 'Permission denied.' );
2013-10-26 21:48:03 +00:00
return ;
}
2014-01-02 09:09:57 +00:00
2014-01-06 01:42:56 +00:00
if (( $this -> auth -> owner_id ) && ( ! perm_is_allowed ( $this -> auth -> owner_id , $this -> auth -> observer , 'view_storage' ))) {
2014-01-02 09:09:57 +00:00
throw new DAV\Exception\Forbidden ( 'Permission denied.' );
return ;
2014-01-02 00:07:36 +00:00
}
2014-01-02 09:09:57 +00:00
if ( $this -> red_path === '/' && $name === 'cloud' ) {
return new RedDirectory ( '/cloud' , $this -> auth );
2014-01-02 00:07:36 +00:00
}
2014-01-03 01:49:39 +00:00
$x = RedFileData ( $this -> ext_path . '/' . $name , $this -> auth );
2014-01-02 09:09:57 +00:00
if ( $x )
return $x ;
2014-01-09 21:54:18 +00:00
2014-01-02 09:09:57 +00:00
throw new DAV\Exception\NotFound ( 'The file with name: ' . $name . ' could not be found' );
2013-10-26 21:48:03 +00:00
2014-01-02 00:07:36 +00:00
}
function getName () {
2014-01-09 21:54:18 +00:00
logger ( 'RedDirectory::getName returns: ' . basename ( $this -> red_path ), LOGGER_DATA );
2014-01-02 09:09:57 +00:00
return ( basename ( $this -> red_path ));
2013-10-26 21:48:03 +00:00
}
2014-01-03 01:49:39 +00:00
2013-10-26 21:48:03 +00:00
function createFile ( $name , $data = null ) {
2014-01-09 21:54:18 +00:00
logger ( 'RedDirectory::createFile : ' . $name , LOGGER_DEBUG );
2014-01-03 09:44:25 +00:00
2014-01-06 01:42:56 +00:00
if ( ! $this -> auth -> owner_id ) {
logger ( 'createFile: permission denied' );
throw new DAV\Exception\Forbidden ( 'Permission denied.' );
return ;
}
if ( ! perm_is_allowed ( $this -> auth -> owner_id , $this -> auth -> observer , 'write_storage' )) {
2014-01-03 09:44:25 +00:00
logger ( 'createFile: permission denied' );
2014-01-03 01:49:39 +00:00
throw new DAV\Exception\Forbidden ( 'Permission denied.' );
return ;
}
2014-01-03 09:44:25 +00:00
$mimetype = z_mime_content_type ( $name );
2014-01-15 11:12:47 +00:00
$c = q ( " select * from channel where channel_id = %d and not (channel_pageflags & %d) limit 1 " ,
2014-01-17 20:52:57 +00:00
intval ( $this -> auth -> owner_id ),
intval ( PAGE_REMOVED )
2014-01-03 09:44:25 +00:00
);
2014-01-09 21:54:18 +00:00
if ( ! $c ) {
logger ( 'createFile: no channel' );
throw new DAV\Exception\Forbidden ( 'Permission denied.' );
return ;
}
2014-01-03 09:44:25 +00:00
$filesize = 0 ;
$hash = random_string ();
2013-10-26 21:48:03 +00:00
2014-01-10 03:20:10 +00:00
$r = q ( " INSERT INTO attach ( aid, uid, hash, creator, filename, folder, flags, filetype, filesize, revision, data, created, edited, allow_cid, allow_gid, deny_cid, deny_gid )
VALUES ( % d , % d , '%s' , '%s' , '%s' , '%s' , '%s' , '%s' , % d , % d , '%s' , '%s' , '%s' , '%s' , '%s' , '%s' , '%s' ) " ,
2014-01-03 09:44:25 +00:00
intval ( $c [ 0 ][ 'channel_account_id' ]),
intval ( $c [ 0 ][ 'channel_id' ]),
dbesc ( $hash ),
2014-01-10 03:20:10 +00:00
dbesc ( $this -> auth -> observer ),
2014-01-03 09:44:25 +00:00
dbesc ( $name ),
2014-01-06 02:44:32 +00:00
dbesc ( $this -> folder_hash ),
2014-01-09 02:06:52 +00:00
dbesc ( ATTACH_FLAG_OS ),
2014-01-03 09:44:25 +00:00
dbesc ( $mimetype ),
intval ( $filesize ),
intval ( 0 ),
2014-01-09 02:06:52 +00:00
dbesc ( $this -> os_path . '/' . $hash ),
2014-01-03 09:44:25 +00:00
dbesc ( datetime_convert ()),
2014-01-06 01:54:20 +00:00
dbesc ( datetime_convert ()),
2014-01-06 02:18:39 +00:00
dbesc ( $c [ 0 ][ 'channel_allow_cid' ]),
dbesc ( $c [ 0 ][ 'channel_allow_gid' ]),
dbesc ( $c [ 0 ][ 'channel_deny_cid' ]),
2014-01-06 02:19:56 +00:00
dbesc ( $c [ 0 ][ 'channel_deny_gid' ])
2014-01-06 01:54:20 +00:00
2014-01-03 09:44:25 +00:00
);
2014-01-09 02:06:52 +00:00
$f = 'store/' . $this -> auth -> owner_nick . '/' . (( $this -> os_path ) ? $this -> os_path . '/' : '' ) . $hash ;
file_put_contents ( $f , $data );
$size = filesize ( $f );
2014-01-15 12:02:15 +00:00
$edited = datetime_convert ();
2013-10-26 21:48:03 +00:00
2014-01-15 12:02:15 +00:00
$d = q ( " update attach set filesize = '%s', edited = '%s' where hash = '%s' and uid = %d limit 1 " ,
2014-01-09 02:06:52 +00:00
dbesc ( $size ),
2014-01-15 12:02:15 +00:00
dbesc ( $edited ),
2014-01-06 03:49:45 +00:00
dbesc ( $hash ),
intval ( $c [ 0 ][ 'channel_id' ])
);
2014-01-03 09:44:25 +00:00
2014-01-15 12:02:15 +00:00
$e = q ( " update attach set edited = '%s' where folder = '%s' and uid = %d limit 1 " ,
dbesc ( $edited ),
dbesc ( $this -> folder_hash ),
intval ( $c [ 0 ][ 'channel_id' ])
);
2014-01-06 03:49:45 +00:00
$maxfilesize = get_config ( 'system' , 'maxfilesize' );
2014-01-09 02:06:52 +00:00
if (( $maxfilesize ) && ( $size > $maxfilesize )) {
attach_delete ( $c [ 0 ][ 'channel_id' ], $hash );
2014-01-06 03:49:45 +00:00
return ;
}
$limit = service_class_fetch ( $c [ 0 ][ 'channel_id' ], 'attach_upload_limit' );
if ( $limit !== false ) {
2014-01-07 22:10:28 +00:00
$x = q ( " select sum(filesize) as total from attach where aid = %d " ,
intval ( $c [ 0 ][ 'channel_account_id' ])
2014-01-06 03:49:45 +00:00
);
2014-01-09 02:06:52 +00:00
if (( $x ) && ( $x [ 0 ][ 'total' ] + $size > $limit )) {
2014-01-11 20:58:00 +00:00
logger ( 'reddav: service class limit exceeded for ' . $c [ 0 ][ 'channel_name' ] . ' total usage is ' . $x [ 0 ][ 'total' ] . ' limit is ' . $limit );
2014-01-09 02:06:52 +00:00
attach_delete ( $c [ 0 ][ 'channel_id' ], $hash );
2014-01-06 03:49:45 +00:00
return ;
}
}
2013-10-26 21:48:03 +00:00
}
2014-01-03 09:44:25 +00:00
2013-10-26 21:48:03 +00:00
function createDirectory ( $name ) {
2014-01-06 00:00:05 +00:00
2014-01-09 21:54:18 +00:00
logger ( 'RedDirectory::createDirectory: ' . $name , LOGGER_DEBUG );
2014-01-06 00:00:05 +00:00
if (( ! $this -> auth -> owner_id ) || ( ! perm_is_allowed ( $this -> auth -> owner_id , $this -> auth -> observer , 'write_storage' ))) {
2014-01-03 01:49:39 +00:00
throw new DAV\Exception\Forbidden ( 'Permission denied.' );
return ;
}
2013-10-26 21:48:03 +00:00
2014-01-15 11:12:47 +00:00
$r = q ( " select * from channel where channel_id = %d and not (channel_pageflags & %d) limit 1 " ,
2014-01-20 06:34:28 +00:00
intval ( $this -> auth -> owner_id ),
intval ( PAGE_REMOVED )
2014-01-06 00:00:05 +00:00
);
if ( $r ) {
$result = attach_mkdir ( $r [ 0 ], $this -> auth -> observer , array ( 'filename' => $name , 'folder' => $this -> folder_hash ));
2014-01-09 21:54:18 +00:00
if ( ! $result [ 'success' ])
logger ( 'RedDirectory::createDirectory: ' . print_r ( $result , true ), LOGGER_DEBUG );
2014-01-06 00:00:05 +00:00
}
2013-10-26 21:48:03 +00:00
}
function childExists ( $name ) {
2014-01-02 00:07:36 +00:00
2014-01-03 01:49:39 +00:00
if ( $this -> red_path === '/' && $name === 'cloud' ) {
2014-01-09 21:54:18 +00:00
logger ( 'RedDirectory::childExists /cloud: true' , LOGGER_DATA );
2014-01-03 01:49:39 +00:00
return true ;
}
2014-01-03 09:44:25 +00:00
$x = RedFileData ( $this -> ext_path . '/' . $name , $this -> auth , true );
2014-01-09 21:54:18 +00:00
logger ( 'RedFileData returns: ' . print_r ( $x , true ), LOGGER_DATA );
2014-01-03 01:49:39 +00:00
if ( $x )
2013-10-28 01:35:40 +00:00
return true ;
return false ;
2013-10-26 21:48:03 +00:00
}
2014-01-06 00:00:05 +00:00
function getDir () {
2014-01-09 21:54:18 +00:00
logger ( 'getDir: ' . $this -> ext_path , LOGGER_DEBUG );
2014-02-12 05:20:34 +00:00
$this -> auth -> log ();
2014-01-06 00:00:05 +00:00
2014-01-06 02:44:32 +00:00
$file = $this -> ext_path ;
2014-01-06 00:00:05 +00:00
2014-01-06 02:44:32 +00:00
$x = strpos ( $file , '/cloud' );
2014-01-06 00:00:05 +00:00
if ( $x === false )
return ;
if ( $x === 0 ) {
$file = substr ( $file , 6 );
}
if (( ! $file ) || ( $file === '/' )) {
return ;
}
$file = trim ( $file , '/' );
$path_arr = explode ( '/' , $file );
if ( ! $path_arr )
return ;
2014-01-06 02:44:32 +00:00
2014-02-12 08:04:35 +00:00
logger ( 'getDir(): path: ' . print_r ( $path_arr , true ), LOGGER_DEBUG );
2014-01-06 02:44:32 +00:00
2014-01-06 00:00:05 +00:00
$channel_name = $path_arr [ 0 ];
2014-01-15 11:12:47 +00:00
$r = q ( " select channel_id from channel where channel_address = '%s' and not ( channel_pageflags & %d ) limit 1 " ,
dbesc ( $channel_name ),
intval ( PAGE_REMOVED )
2014-01-06 00:00:05 +00:00
);
2014-01-15 11:12:47 +00:00
if ( ! $r ) {
throw new DAV\Exception\NotFound ( 'The file with name: ' . $channel_name . ' could not be found' );
2014-01-06 00:00:05 +00:00
2014-01-15 11:12:47 +00:00
return ;
}
2014-01-06 00:00:05 +00:00
$channel_id = $r [ 0 ][ 'channel_id' ];
$this -> auth -> owner_id = $channel_id ;
2014-01-09 02:06:52 +00:00
$this -> auth -> owner_nick = $channel_name ;
2014-01-06 00:00:05 +00:00
$path = '/' . $channel_name ;
$folder = '' ;
2014-01-06 22:33:49 +00:00
$os_path = '' ;
2014-01-06 00:00:05 +00:00
for ( $x = 1 ; $x < count ( $path_arr ); $x ++ ) {
2014-01-06 03:49:45 +00:00
2014-03-18 02:43:36 +00:00
$r = q ( " select id, hash, filename, flags from attach where folder = '%s' and filename = '%s' and uid = %d and (flags & %d) " ,
2014-01-06 00:00:05 +00:00
dbesc ( $folder ),
2014-01-06 02:44:32 +00:00
dbesc ( $path_arr [ $x ]),
2014-01-06 00:00:05 +00:00
intval ( $channel_id ),
intval ( ATTACH_FLAG_DIR )
);
2014-01-06 03:49:45 +00:00
2014-01-06 00:00:05 +00:00
if ( $r && ( $r [ 0 ][ 'flags' ] & ATTACH_FLAG_DIR )) {
$folder = $r [ 0 ][ 'hash' ];
2014-01-06 22:33:49 +00:00
if ( strlen ( $os_path ))
$os_path .= '/' ;
$os_path .= $folder ;
2014-01-06 00:00:05 +00:00
$path = $path . '/' . $r [ 0 ][ 'filename' ];
}
}
$this -> folder_hash = $folder ;
2014-01-06 22:33:49 +00:00
$this -> os_path = $os_path ;
2014-01-06 00:00:05 +00:00
return ;
}
2014-01-15 12:02:15 +00:00
function getLastModified () {
$r = q ( " select edited from attach where folder = '%s' and uid = %d order by edited desc limit 1 " ,
dbesc ( $this -> folder_hash ),
intval ( $this -> auth -> owner_id )
);
if ( $r )
return datetime_convert ( 'UTC' , 'UTC' , $r [ 0 ][ 'edited' ], 'U' );
return '' ;
}
2014-01-06 00:00:05 +00:00
2014-01-21 23:38:02 +00:00
public function getQuotaInfo () {
$limit = disk_total_space ( 'store' );
$free = disk_free_space ( 'store' );
if ( $this -> auth -> owner_id ) {
$c = q ( " select * from channel where channel_id = %d and not (channel_pageflags & %d) limit 1 " ,
intval ( $this -> auth -> owner_id ),
intval ( PAGE_REMOVED )
);
$ulimit = service_class_fetch ( $c [ 0 ][ 'channel_id' ], 'attach_upload_limit' );
$limit = (( $ulimit ) ? $ulimit : $limit );
$x = q ( " select sum(filesize) as total from attach where aid = %d " ,
intval ( $c [ 0 ][ 'channel_account_id' ])
);
$free = (( $x ) ? $limit - $x [ 0 ][ 'total' ] : 0 );
}
return array (
$limit - $free ,
$free
);
}
2014-01-06 00:00:05 +00:00
2013-10-26 21:48:03 +00:00
}
2014-01-02 00:07:36 +00:00
class RedFile extends DAV\Node implements DAV\IFile {
2013-10-26 21:48:03 +00:00
private $data ;
2014-01-02 00:07:36 +00:00
private $auth ;
private $name ;
2013-10-26 21:48:03 +00:00
2014-01-03 01:49:39 +00:00
function __construct ( $name , $data , & $auth ) {
2014-01-02 00:07:36 +00:00
$this -> name = $name ;
2014-01-03 01:49:39 +00:00
$this -> data = $data ;
2014-01-02 00:07:36 +00:00
$this -> auth = $auth ;
2013-10-26 21:48:03 +00:00
2014-01-09 21:54:18 +00:00
logger ( 'RedFile::_construct: ' . print_r ( $this -> data , true ), LOGGER_DATA );
2013-10-26 21:48:03 +00:00
}
2014-01-02 00:07:36 +00:00
function getName () {
2014-01-09 21:54:18 +00:00
logger ( 'RedFile::getName: ' . basename ( $this -> name ), LOGGER_DEBUG );
2014-01-03 01:49:39 +00:00
return basename ( $this -> name );
2014-01-02 00:07:36 +00:00
}
2013-10-26 21:48:03 +00:00
2014-01-03 01:49:39 +00:00
function setName ( $newName ) {
2014-01-09 21:54:18 +00:00
logger ( 'RedFile::setName: ' . basename ( $this -> name ) . ' -> ' . $newName , LOGGER_DEBUG );
2014-01-03 01:49:39 +00:00
2014-01-06 01:42:56 +00:00
if (( ! $newName ) || ( ! $this -> auth -> owner_id ) || ( ! perm_is_allowed ( $this -> auth -> owner_id , $this -> auth -> observer , 'write_storage' ))) {
2014-01-03 01:49:39 +00:00
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' ])
);
2013-10-26 21:48:03 +00:00
}
2014-01-03 01:49:39 +00:00
function put ( $data ) {
2014-01-09 21:54:18 +00:00
logger ( 'RedFile::put: ' . basename ( $this -> name ), LOGGER_DEBUG );
2014-01-03 09:44:25 +00:00
2014-01-15 11:12:47 +00:00
$c = q ( " select * from channel where channel_id = %d and not (channel_pageflags & %d) limit 1 " ,
intval ( PAGE_REMOVED ),
2014-01-11 03:18:30 +00:00
intval ( $this -> auth -> owner_id )
);
2014-01-15 12:02:15 +00:00
$r = q ( " select flags, folder, data from attach where hash = '%s' and uid = %d limit 1 " ,
2014-01-10 21:47:56 +00:00
dbesc ( $this -> data [ 'hash' ]),
2014-01-09 02:06:52 +00:00
intval ( $c [ 0 ][ 'channel_id' ])
);
if ( $r ) {
if ( $r [ 0 ][ 'flags' ] & ATTACH_FLAG_OS ) {
2014-01-11 03:18:30 +00:00
$f = 'store/' . $this -> auth -> owner_nick . '/' . (( $r [ 0 ][ 'data' ]) ? $r [ 0 ][ 'data' ] : '' );
2014-01-10 21:10:50 +00:00
@ file_put_contents ( $f , $data );
$size = @ filesize ( $f );
2014-01-10 21:47:56 +00:00
logger ( 'reddav: put() filename: ' . $f . ' size: ' . $size , LOGGER_DEBUG );
2014-01-09 02:06:52 +00:00
}
else {
$r = q ( " update attach set data = '%s' where hash = '%s' and uid = %d limit 1 " ,
dbesc ( stream_get_contents ( $data )),
dbesc ( $this -> data [ 'hash' ]),
intval ( $this -> data [ 'uid' ])
);
$r = q ( " select length(data) as fsize from attach where hash = '%s' and uid = %d limit 1 " ,
dbesc ( $this -> data [ 'hash' ]),
intval ( $this -> data [ 'uid' ])
);
if ( $r )
$size = $r [ 0 ][ 'fsize' ];
}
2014-01-15 12:02:15 +00:00
2014-01-09 02:06:52 +00:00
}
2014-01-15 12:02:15 +00:00
$edited = datetime_convert ();
$d = q ( " update attach set filesize = '%s', edited = '%s' where hash = '%s' and uid = %d limit 1 " ,
2014-01-09 02:06:52 +00:00
dbesc ( $size ),
2014-01-15 12:02:15 +00:00
dbesc ( $edited ),
2014-01-10 21:47:56 +00:00
dbesc ( $this -> data [ 'hash' ]),
2014-01-06 03:49:45 +00:00
intval ( $c [ 0 ][ 'channel_id' ])
);
2014-01-15 12:02:15 +00:00
$e = q ( " update attach set edited = '%s' where folder = '%s' and uid = %d limit 1 " ,
dbesc ( $edited ),
dbesc ( $r [ 0 ][ 'folder' ]),
intval ( $c [ 0 ][ 'channel_id' ])
);
2014-01-09 02:06:52 +00:00
2014-01-06 03:49:45 +00:00
$maxfilesize = get_config ( 'system' , 'maxfilesize' );
2014-01-09 02:06:52 +00:00
if (( $maxfilesize ) && ( $size > $maxfilesize )) {
2014-01-10 21:47:56 +00:00
attach_delete ( $c [ 0 ][ 'channel_id' ], $this -> data [ 'hash' ]);
2014-01-06 03:49:45 +00:00
return ;
}
$limit = service_class_fetch ( $c [ 0 ][ 'channel_id' ], 'attach_upload_limit' );
if ( $limit !== false ) {
2014-01-07 22:10:28 +00:00
$x = q ( " select sum(filesize) as total from attach where aid = %d " ,
intval ( $c [ 0 ][ 'channel_account_id' ])
2014-01-06 03:49:45 +00:00
);
2014-01-09 02:06:52 +00:00
if (( $x ) && ( $x [ 0 ][ 'total' ] + $size > $limit )) {
2014-01-11 20:58:00 +00:00
logger ( 'reddav: service class limit exceeded for ' . $c [ 0 ][ 'channel_name' ] . ' total usage is ' . $x [ 0 ][ 'total' ] . ' limit is ' . $limit );
2014-01-10 21:47:56 +00:00
attach_delete ( $c [ 0 ][ 'channel_id' ], $this -> data [ 'hash' ]);
2014-01-06 03:49:45 +00:00
return ;
}
}
2014-01-03 01:49:39 +00:00
}
2013-10-26 21:48:03 +00:00
function get () {
2014-01-09 21:54:18 +00:00
logger ( 'RedFile::get: ' . basename ( $this -> name ), LOGGER_DEBUG );
2013-10-26 21:48:03 +00:00
2014-02-09 22:56:52 +00:00
$r = q ( " select data, flags, filename, filetype from attach where hash = '%s' and uid = %d limit 1 " ,
2014-01-03 01:49:39 +00:00
dbesc ( $this -> data [ 'hash' ]),
intval ( $this -> data [ 'uid' ])
);
2014-01-07 02:13:02 +00:00
if ( $r ) {
2014-02-09 22:56:52 +00:00
$unsafe_types = array ( 'text/html' , 'text/css' , 'application/javascript' );
if ( in_array ( $r [ 0 ][ 'filetype' ], $unsafe_types )) {
header ( 'Content-disposition: attachment; filename="' . $r [ 0 ][ 'filename' ] . '"' );
header ( 'Content-type: text/plain' );
}
2014-01-07 02:13:02 +00:00
if ( $r [ 0 ][ 'flags' ] & ATTACH_FLAG_OS ) {
2014-01-09 02:06:52 +00:00
$f = 'store/' . $this -> auth -> owner_nick . '/' . (( $this -> os_path ) ? $this -> os_path . '/' : '' ) . $r [ 0 ][ 'data' ];
return fopen ( $f , 'rb' );
2014-01-07 02:13:02 +00:00
}
return $r [ 0 ][ 'data' ];
}
2013-10-26 21:48:03 +00:00
}
function getETag () {
2014-01-03 01:49:39 +00:00
return $this -> data [ 'hash' ];
2013-10-26 21:48:03 +00:00
}
function getContentType () {
2014-02-09 22:56:52 +00:00
$unsafe_types = array ( 'text/html' , 'text/css' , 'application/javascript' );
if ( in_array ( $this -> data [ 'filetype' ], $unsafe_types )) {
return 'text/plain' ;
}
2014-01-03 01:49:39 +00:00
return $this -> data [ 'filetype' ];
2013-10-26 21:48:03 +00:00
}
function getSize () {
2014-01-03 01:49:39 +00:00
return $this -> data [ 'filesize' ];
2013-10-26 21:48:03 +00:00
}
2014-01-03 01:49:39 +00:00
function getLastModified () {
2014-01-15 12:02:15 +00:00
return datetime_convert ( 'UTC' , 'UTC' , $this -> data [ 'edited' ], 'U' );
2014-01-03 01:49:39 +00:00
}
2014-01-06 22:33:49 +00:00
function delete () {
2014-01-08 02:47:33 +00:00
if (( ! $this -> auth -> owner_id ) || ( ! perm_is_allowed ( $this -> auth -> owner_id , $this -> auth -> observer , 'write_storage' ))) {
throw new DAV\Exception\Forbidden ( 'Permission denied.' );
return ;
2014-01-06 22:33:49 +00:00
}
2014-01-10 03:20:10 +00:00
if ( $this -> auth -> owner_id !== $this -> auth -> channel_id ) {
if (( $this -> auth -> observer !== $this -> data [ 'creator' ]) || ( $this -> data [ 'flags' ] & ATTACH_FLAG_DIR )) {
throw new DAV\Exception\Forbidden ( 'Permission denied.' );
return ;
}
}
2014-01-08 02:47:33 +00:00
attach_delete ( $this -> auth -> owner_id , $this -> data [ 'hash' ]);
2014-01-06 22:33:49 +00:00
}
2013-10-26 21:48:03 +00:00
}
2014-01-02 09:09:57 +00:00
function RedChannelList ( & $auth ) {
2013-10-26 21:48:03 +00:00
2014-01-02 09:09:57 +00:00
$ret = array ();
2014-01-02 00:07:36 +00:00
2014-01-15 11:12:47 +00:00
$r = q ( " select channel_id, channel_address from channel where not (channel_pageflags & %d) and not (channel_pageflags & %d) " ,
intval ( PAGE_REMOVED ),
intval ( PAGE_HIDDEN )
2014-01-02 09:09:57 +00:00
);
2013-10-26 21:48:03 +00:00
2014-01-02 09:09:57 +00:00
if ( $r ) {
foreach ( $r as $rr ) {
2014-01-03 01:49:39 +00:00
if ( perm_is_allowed ( $rr [ 'channel_id' ], $auth -> observer , 'view_storage' )) {
2014-02-12 08:04:35 +00:00
logger ( 'RedChannelList: ' . '/cloud/' . $rr [ 'channel_address' ], LOGGER_DATA );
2014-01-03 01:49:39 +00:00
$ret [] = new RedDirectory ( '/cloud/' . $rr [ 'channel_address' ], $auth );
}
2014-01-02 09:09:57 +00:00
}
}
return $ret ;
}
function RedCollectionData ( $file , & $auth ) {
$ret = array ();
$x = strpos ( $file , '/cloud' );
if ( $x === 0 ) {
$file = substr ( $file , 6 );
}
if (( ! $file ) || ( $file === '/' )) {
return RedChannelList ( $auth );
}
$file = trim ( $file , '/' );
$path_arr = explode ( '/' , $file );
2014-01-02 00:07:36 +00:00
if ( ! $path_arr )
return null ;
$channel_name = $path_arr [ 0 ];
2014-01-03 01:49:39 +00:00
$r = q ( " select channel_id from channel where channel_address = '%s' limit 1 " ,
2014-01-02 09:09:57 +00:00
dbesc ( $channel_name )
);
2014-01-03 01:49:39 +00:00
2014-01-02 09:09:57 +00:00
if ( ! $r )
return null ;
$channel_id = $r [ 0 ][ 'channel_id' ];
2014-01-06 22:33:49 +00:00
$perms = permissions_sql ( $channel_id );
2014-01-06 00:00:05 +00:00
$auth -> owner_id = $channel_id ;
2014-01-02 09:09:57 +00:00
$path = '/' . $channel_name ;
2014-01-02 00:07:36 +00:00
$folder = '' ;
2014-01-06 22:33:49 +00:00
$errors = false ;
$permission_error = false ;
2014-01-02 00:07:36 +00:00
2014-01-02 09:09:57 +00:00
for ( $x = 1 ; $x < count ( $path_arr ); $x ++ ) {
2014-02-12 08:04:35 +00:00
2014-03-18 02:41:58 +00:00
$r = q ( " select id, hash, filename, flags from attach where folder = '%s' and filename = '%s' and uid = %d and (flags & %d) $perms limit 1 " ,
2014-01-02 00:07:36 +00:00
dbesc ( $folder ),
2014-01-06 00:37:07 +00:00
dbesc ( $path_arr [ $x ]),
2014-03-18 02:41:58 +00:00
intval ( $channel_id ),
2014-01-02 09:09:57 +00:00
intval ( ATTACH_FLAG_DIR )
2014-01-02 00:07:36 +00:00
);
2014-01-06 22:33:49 +00:00
if ( ! $r ) {
// path wasn't found. Try without permissions to see if it was the result of permissions.
$errors = true ;
2014-03-18 23:50:46 +00:00
$r = q ( " select id, hash, filename, flags from attach where folder = '%s' and filename = '%s' and uid = %d and (flags & %d) limit 1 " ,
2014-01-06 22:33:49 +00:00
dbesc ( $folder ),
basename ( $path_arr [ $x ]),
2014-03-18 23:50:46 +00:00
intval ( $channel_id ),
2014-01-06 22:33:49 +00:00
intval ( ATTACH_FLAG_DIR )
);
if ( $r ) {
$permission_error = true ;
}
break ;
}
2014-01-02 09:09:57 +00:00
if ( $r && ( $r [ 0 ][ 'flags' ] & ATTACH_FLAG_DIR )) {
$folder = $r [ 0 ][ 'hash' ];
$path = $path . '/' . $r [ 0 ][ 'filename' ];
}
}
2014-01-02 00:07:36 +00:00
2014-01-06 22:33:49 +00:00
if ( $errors ) {
if ( $permission_error ) {
throw new DAV\Exception\Forbidden ( 'Permission denied.' );
return ;
}
else {
throw new DAV\Exception\NotFound ( 'A component of the request file path could not be found' );
return ;
}
}
2014-01-09 21:54:18 +00:00
// This should no longer be needed since we just returned errors for paths not found
2014-01-03 01:49:39 +00:00
2014-01-02 09:09:57 +00:00
if ( $path !== '/' . $file ) {
logger ( " RedCollectionData: Path mismatch: $path !== / $file " );
return NULL ;
}
$ret = array ();
2014-01-06 22:33:49 +00:00
$r = q ( " select id, uid, hash, filename, filetype, filesize, revision, folder, flags, created, edited from attach where folder = '%s' and uid = %d $perms group by filename " ,
2014-01-02 09:09:57 +00:00
dbesc ( $folder ),
2014-01-03 01:49:39 +00:00
intval ( $channel_id )
2014-01-02 09:09:57 +00:00
);
foreach ( $r as $rr ) {
2014-02-12 08:04:35 +00:00
logger ( 'RedCollectionData: filename: ' . $rr [ 'filename' ], LOGGER_DATA );
2014-01-02 09:09:57 +00:00
if ( $rr [ 'flags' ] & ATTACH_FLAG_DIR )
$ret [] = new RedDirectory ( '/cloud' . $path . '/' . $rr [ 'filename' ], $auth );
else
2014-01-03 01:49:39 +00:00
$ret [] = new RedFile ( '/cloud' . $path . '/' . $rr [ 'filename' ], $rr , $auth );
2014-01-02 09:09:57 +00:00
}
return $ret ;
}
2014-01-03 09:44:25 +00:00
function RedFileData ( $file , & $auth , $test = false ) {
2014-01-02 09:09:57 +00:00
2014-01-09 21:54:18 +00:00
logger ( 'RedFileData:' . $file . (( $test ) ? ' (test mode) ' : '' ), LOGGER_DEBUG );
2014-01-02 09:09:57 +00:00
$x = strpos ( $file , '/cloud' );
if ( $x === 0 ) {
$file = substr ( $file , 6 );
}
if (( ! $file ) || ( $file === '/' )) {
2014-01-30 00:02:02 +00:00
return new RedDirectory ( '/' , $auth );
2014-01-02 09:09:57 +00:00
}
$file = trim ( $file , '/' );
$path_arr = explode ( '/' , $file );
if ( ! $path_arr )
return null ;
$channel_name = $path_arr [ 0 ];
2014-01-03 01:49:39 +00:00
2014-01-02 09:09:57 +00:00
$r = q ( " select channel_id from channel where channel_address = '%s' limit 1 " ,
dbesc ( $channel_name )
);
if ( ! $r )
return null ;
$channel_id = $r [ 0 ][ 'channel_id' ];
$path = '/' . $channel_name ;
2014-01-06 00:00:05 +00:00
$auth -> owner_id = $channel_id ;
$permission_error = false ;
2014-01-02 09:09:57 +00:00
$folder = '' ;
2014-01-03 01:49:39 +00:00
require_once ( 'include/security.php' );
$perms = permissions_sql ( $channel_id );
$errors = false ;
2014-01-02 09:09:57 +00:00
for ( $x = 1 ; $x < count ( $path_arr ); $x ++ ) {
2014-01-06 00:37:07 +00:00
$r = q ( " select id, hash, filename, flags from attach where folder = '%s' and filename = '%s' and uid = %d and (flags & %d) $perms " ,
2014-01-02 09:09:57 +00:00
dbesc ( $folder ),
2014-01-06 00:37:07 +00:00
dbesc ( $path_arr [ $x ]),
2014-01-02 09:09:57 +00:00
intval ( $channel_id ),
intval ( ATTACH_FLAG_DIR )
);
2014-01-03 01:49:39 +00:00
2014-01-02 09:09:57 +00:00
if ( $r && ( $r [ 0 ][ 'flags' ] & ATTACH_FLAG_DIR )) {
$folder = $r [ 0 ][ 'hash' ];
$path = $path . '/' . $r [ 0 ][ 'filename' ];
}
2014-01-03 01:49:39 +00:00
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 ),
2014-03-17 03:34:21 +00:00
dbesc ( basename ( $file )),
2014-01-03 01:49:39 +00:00
intval ( $channel_id )
);
}
2014-01-06 00:00:05 +00:00
if ( ! $r ) {
2014-01-03 01:49:39 +00:00
$errors = true ;
2014-01-06 00:00:05 +00:00
$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 group by filename limit 1 " ,
dbesc ( $folder ),
2014-03-17 03:34:21 +00:00
dbesc ( basename ( $file )),
2014-01-06 00:00:05 +00:00
intval ( $channel_id )
);
if ( $r )
$permission_error = true ;
}
2014-01-02 09:09:57 +00:00
}
2014-01-03 01:49:39 +00:00
2014-01-02 09:09:57 +00:00
if ( $path === '/' . $file ) {
2014-01-06 00:00:05 +00:00
if ( $test )
return true ;
2014-01-02 09:09:57 +00:00
// final component was a directory.
return new RedDirectory ( '/cloud/' . $file , $auth );
}
2014-01-03 01:49:39 +00:00
if ( $errors ) {
2014-01-09 21:54:18 +00:00
logger ( 'RedFileData: not found' );
2014-01-03 09:44:25 +00:00
if ( $test )
return false ;
2014-01-06 00:00:05 +00:00
if ( $permission_error ) {
logger ( 'RedFileData: permission error' );
throw new DAV\Exception\Forbidden ( 'Permission denied.' );
}
2014-01-03 01:49:39 +00:00
return ;
2014-01-02 00:07:36 +00:00
}
2014-01-03 09:44:25 +00:00
if ( $r ) {
2014-01-06 00:00:05 +00:00
if ( $test )
return true ;
2014-01-03 09:44:25 +00:00
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 );
}
return false ;
2014-01-02 00:07:36 +00:00
}
2013-10-26 21:48:03 +00:00
2014-01-06 03:25:56 +00:00
class RedBasicAuth extends Sabre\DAV\Auth\Backend\AbstractBasic {
public $channel_name = '' ;
public $channel_id = 0 ;
public $channel_hash = '' ;
public $observer = '' ;
public $browser ;
public $owner_id ;
2014-01-09 02:06:52 +00:00
public $owner_nick = '' ;
2014-01-10 08:47:40 +00:00
public $timezone ;
2014-01-06 03:25:56 +00:00
protected function validateUserPass ( $username , $password ) {
2014-01-22 04:42:10 +00:00
2014-02-13 11:52:16 +00:00
2014-01-22 04:42:10 +00:00
if ( trim ( $password ) === '+++' ) {
logger ( 'reddav: validateUserPass: guest ' . $username );
return true ;
}
2014-01-06 03:25:56 +00:00
require_once ( 'include/auth.php' );
2014-01-30 22:36:56 +00:00
$record = account_verify_password ( $username , $password );
2014-01-06 03:25:56 +00:00
if ( $record && $record [ 'account_default_channel' ]) {
$r = q ( " select * from channel where channel_account_id = %d and channel_id = %d limit 1 " ,
intval ( $record [ 'account_id' ]),
intval ( $record [ 'account_default_channel' ])
);
if ( $r ) {
$this -> currentUser = $r [ 0 ][ 'channel_address' ];
$this -> channel_name = $r [ 0 ][ 'channel_address' ];
$this -> channel_id = $r [ 0 ][ 'channel_id' ];
$this -> channel_hash = $this -> observer = $r [ 0 ][ 'channel_hash' ];
2014-02-12 08:04:35 +00:00
$_SESSION [ 'uid' ] = $r [ 0 ][ 'channel_id' ];
2014-02-22 21:33:18 +00:00
$_SESSION [ 'account_id' ] = $r [ 0 ][ 'channel_account_id' ];
2014-02-12 08:04:35 +00:00
$_SESSION [ 'authenticated' ] = true ;
2014-01-06 03:25:56 +00:00
return true ;
}
}
$r = q ( " select * from channel where channel_address = '%s' limit 1 " ,
dbesc ( $username )
);
if ( $r ) {
$x = q ( " select * from account where account_id = %d limit 1 " ,
intval ( $r [ 0 ][ 'channel_account_id' ])
);
if ( $x ) {
foreach ( $x as $record ) {
if (( $record [ 'account_flags' ] == ACCOUNT_OK ) || ( $record [ 'account_flags' ] == ACCOUNT_UNVERIFIED )
&& ( hash ( 'whirlpool' , $record [ 'account_salt' ] . $password ) === $record [ 'account_password' ])) {
logger ( '(DAV) RedBasicAuth: password verified for ' . $username );
$this -> currentUser = $r [ 0 ][ 'channel_address' ];
$this -> channel_name = $r [ 0 ][ 'channel_address' ];
$this -> channel_id = $r [ 0 ][ 'channel_id' ];
$this -> channel_hash = $this -> observer = $r [ 0 ][ 'channel_hash' ];
2014-02-12 08:04:35 +00:00
$_SESSION [ 'uid' ] = $r [ 0 ][ 'channel_id' ];
2014-02-22 21:33:18 +00:00
$_SESSION [ 'account_id' ] = $r [ 0 ][ 'channel_account_id' ];
2014-02-12 08:04:35 +00:00
$_SESSION [ 'authenticated' ] = true ;
2014-01-06 03:25:56 +00:00
return true ;
}
}
}
}
logger ( '(DAV) RedBasicAuth: password failed for ' . $username );
return false ;
}
function setCurrentUser ( $name ) {
$this -> currentUser = $name ;
}
function setBrowserPlugin ( $browser ) {
$this -> browser = $browser ;
}
2014-02-12 05:20:34 +00:00
function log () {
2014-02-12 08:04:35 +00:00
logger ( 'dav: auth: channel_name ' . $this -> channel_name , LOGGER_DATA );
logger ( 'dav: auth: channel_id ' . $this -> channel_id , LOGGER_DATA );
logger ( 'dav: auth: channel_hash ' . $this -> channel_hash , LOGGER_DATA );
logger ( 'dav: auth: observer ' . $this -> observer , LOGGER_DATA );
logger ( 'dav: auth: owner_id ' . $this -> owner_id , LOGGER_DATA );
logger ( 'dav: auth: owner_nick ' . $this -> owner_nick , LOGGER_DATA );
2014-02-12 05:20:34 +00:00
}
2014-01-06 03:25:56 +00:00
}
class RedBrowser extends DAV\Browser\Plugin {
private $auth ;
function __construct ( & $auth ) {
$this -> auth = $auth ;
2014-02-13 11:06:48 +00:00
$this -> enableAssets = false ;
2014-01-06 03:25:56 +00:00
}
2014-01-06 22:33:49 +00:00
// The DAV browser is instantiated after the auth module and directory classes but before we know the current
// directory and who the owner and observer are. So we add a pointer to the browser into the auth module and vice
// versa. Then when we've figured out what directory is actually being accessed, we call the following function
// to decide whether or not to show web elements which include writeable objects.
2014-01-06 03:25:56 +00:00
function set_writeable () {
if ( ! $this -> auth -> owner_id )
$this -> enablePost = false ;
if ( ! perm_is_allowed ( $this -> auth -> owner_id , get_observer_hash (), 'write_storage' ))
$this -> enablePost = false ;
else
$this -> enablePost = true ;
}
2014-01-10 02:19:49 +00:00
public function generateDirectoryIndex ( $path ) {
2014-06-23 23:05:00 +00:00
$is_owner = (( local_user () && $this -> auth -> owner_id == local_user ()) ? true : false );
2014-01-10 08:47:40 +00:00
if ( $this -> auth -> timezone )
date_default_timezone_set ( $this -> auth -> timezone );
2014-01-10 02:19:49 +00:00
$version = '' ;
2014-04-06 10:47:53 +00:00
require_once ( 'include/conversation.php' );
2014-01-10 02:19:49 +00:00
2014-04-06 10:47:53 +00:00
if ( $this -> auth -> channel_name )
2014-06-23 23:05:00 +00:00
$html = profile_tabs ( get_app (),(( $is_owner ) ? true : false ), $this -> auth -> owner_nick );
2014-04-06 10:47:53 +00:00
$html .= "
2014-06-23 22:04:13 +00:00
< body >
< h1 > " .t('Files'). " : " . $this->escapeHTML ( $path ) . " /</ h1 >
< table id = \ " cloud-index \" >
< tr >
< th ></ th >
2014-06-25 09:54:10 +00:00
< th > " .t('Name'). " & nbsp ; & nbsp ; & nbsp ; </ th >
2014-06-23 22:04:13 +00:00
< th ></ th >< th ></ th >< th ></ th >
2014-06-25 09:54:10 +00:00
< th > " .t('Type'). " & nbsp ; & nbsp ; & nbsp ; </ th >
< th > " .t('Size'). " & nbsp ; & nbsp ; & nbsp ; </ th >
< th > " .t('Last modified'). " </ th >
2014-06-23 22:04:13 +00:00
</ tr >
< tr >< td colspan = \ " 8 \" ><hr /></td></tr> " ;
2014-01-10 02:19:49 +00:00
$files = $this -> server -> getPropertiesForPath ( $path , array (
'{DAV:}displayname' ,
'{DAV:}resourcetype' ,
'{DAV:}getcontenttype' ,
'{DAV:}getcontentlength' ,
'{DAV:}getlastmodified' ,
), 1 );
$parent = $this -> server -> tree -> getNodeForPath ( $path );
if ( $path ) {
list ( $parentUri ) = DAV\URLUtil :: splitPath ( $path );
$fullPath = DAV\URLUtil :: encodePath ( $this -> server -> getBaseUri () . $parentUri );
$icon = $this -> enableAssets ? '<a href="' . $fullPath . '"><img src="' . $this -> getAssetUrl ( 'icons/parent' . $this -> iconExtension ) . '" width="24" alt="Parent" /></a>' : '' ;
2014-06-25 09:54:10 +00:00
$html .= "
< tr >
< td > $icon </ td >
< td >< a href = \ " { $fullPath } \" >..</a></td>
< td ></ td >< td ></ td >< th ></ td >
< td > [ " .t('parent'). " ] </ td >
< td ></ td >
< td ></ td >
</ tr > " ;
2014-01-10 02:19:49 +00:00
}
foreach ( $files as $file ) {
// This is the current directory, we can skip it
if ( rtrim ( $file [ 'href' ], '/' ) == $path ) continue ;
list (, $name ) = DAV\URLUtil :: splitPath ( $file [ 'href' ]);
$type = null ;
if ( isset ( $file [ 200 ][ '{DAV:}resourcetype' ])) {
$type = $file [ 200 ][ '{DAV:}resourcetype' ] -> getValue ();
// resourcetype can have multiple values
if ( ! is_array ( $type )) $type = array ( $type );
foreach ( $type as $k => $v ) {
// Some name mapping is preferred
switch ( $v ) {
case '{DAV:}collection' :
2014-06-25 09:54:10 +00:00
$type [ $k ] = t ( 'Collection' );
2014-01-10 02:19:49 +00:00
break ;
case '{DAV:}principal' :
2014-06-25 09:54:10 +00:00
$type [ $k ] = t ( 'Principal' );
2014-01-10 02:19:49 +00:00
break ;
case '{urn:ietf:params:xml:ns:carddav}addressbook' :
2014-06-25 09:54:10 +00:00
$type [ $k ] = t ( 'Addressbook' );
2014-01-10 02:19:49 +00:00
break ;
case '{urn:ietf:params:xml:ns:caldav}calendar' :
2014-06-25 09:54:10 +00:00
$type [ $k ] = t ( 'Calendar' );
2014-01-10 02:19:49 +00:00
break ;
case '{urn:ietf:params:xml:ns:caldav}schedule-inbox' :
2014-06-25 09:54:10 +00:00
$type [ $k ] = t ( 'Schedule Inbox' );
2014-01-10 02:19:49 +00:00
break ;
case '{urn:ietf:params:xml:ns:caldav}schedule-outbox' :
2014-06-25 09:54:10 +00:00
$type [ $k ] = t ( 'Schedule Outbox' );
2014-01-10 02:19:49 +00:00
break ;
case '{http://calendarserver.org/ns/}calendar-proxy-read' :
$type [ $k ] = 'Proxy-Read' ;
break ;
case '{http://calendarserver.org/ns/}calendar-proxy-write' :
$type [ $k ] = 'Proxy-Write' ;
break ;
}
}
$type = implode ( ', ' , $type );
}
// If no resourcetype was found, we attempt to use
// the contenttype property
if ( ! $type && isset ( $file [ 200 ][ '{DAV:}getcontenttype' ])) {
$type = $file [ 200 ][ '{DAV:}getcontenttype' ];
}
if ( ! $type ) $type = 'Unknown' ;
$size = isset ( $file [ 200 ][ '{DAV:}getcontentlength' ]) ? ( int ) $file [ 200 ][ '{DAV:}getcontentlength' ] : '' ;
2014-01-10 08:47:40 +00:00
$lastmodified = (( isset ( $file [ 200 ][ '{DAV:}getlastmodified' ])) ? $file [ 200 ][ '{DAV:}getlastmodified' ] -> getTime () -> format ( 'Y-m-d H:i:s' ) : '' );
2014-01-10 02:19:49 +00:00
$fullPath = DAV\URLUtil :: encodePath ( '/' . trim ( $this -> server -> getBaseUri () . ( $path ? $path . '/' : '' ) . $name , '/' ));
$displayName = isset ( $file [ 200 ][ '{DAV:}displayname' ]) ? $file [ 200 ][ '{DAV:}displayname' ] : $name ;
$displayName = $this -> escapeHTML ( $displayName );
$type = $this -> escapeHTML ( $type );
$icon = '' ;
if ( $this -> enableAssets ) {
$node = $this -> server -> tree -> getNodeForPath (( $path ? $path . '/' : '' ) . $name );
foreach ( array_reverse ( $this -> iconMap ) as $class => $iconName ) {
if ( $node instanceof $class ) {
$icon = '<a href="' . $fullPath . '"><img src="' . $this -> getAssetUrl ( $iconName . $this -> iconExtension ) . '" alt="" width="24" /></a>' ;
break ;
}
}
}
2014-06-23 22:04:13 +00:00
$parentHash = " " ;
$owner = $this -> auth -> owner_id ;
$splitPath = split ( " / " , $fullPath );
if ( count ( $splitPath ) > 3 ) {
for ( $i = 3 ; $i < count ( $splitPath ); $i ++ ) {
$attachName = urldecode ( $splitPath [ $i ]);
$attachHash = $this -> findAttachHash ( $owner , $parentHash , $attachName );
$parentHash = $attachHash ;
}
}
$attachId = $this -> findAttachIdByHash ( $attachHash );
2014-06-24 10:05:11 +00:00
$fileStorageUrl = substr ( $fullPath , 0 , strpos ( $fullPath , " cloud/ " )) . " filestorage/ " . $this -> auth -> channel_name ;
2014-06-23 23:05:00 +00:00
$attachIcon = " " ; // "<a href=\"attach/".$attachHash."\" title=\"".$displayName."\"><i class=\"icon-download\"></i></a>";
2014-06-23 22:04:13 +00:00
$html .= " <tr>
< td > $icon </ td >
2014-06-24 10:05:11 +00:00
< td style = \ " min-width: 15em \" ><a href= \" { $fullPath } \" > { $displayName } </a></td> " ;
2014-06-23 23:05:00 +00:00
if ( $is_owner ) {
$html .= " <td> " . (( $size ) ? $attachIcon : '' ) . " </td>
< td >< a href = \ " " . $fileStorageUrl . " / " . $attachId . " /edit \" title= \" " . t ( 'Edit' ) . " \" ><i class= \" icon-pencil btn btn-default \" ></i></a></td>
2014-06-25 10:07:36 +00:00
< td >< a href = \ " " . $fileStorageUrl . " / " . $attachId . " /delete \" title= \" " . t ( 'Delete' ) . " \" onclick= \" return confirm(' " . t ( 'Are you sure you want to delete this item?' ) . " '); \" ><i class= \" icon-remove btn btn-default drop-icons \" ></i></a></td> " ;
2014-06-23 23:05:00 +00:00
}
else {
$html .= " <td></td><td></td><td></td> " ;
}
$html .=
" <td> { $type } </td>
2014-06-25 09:54:10 +00:00
< td > " . $this->userReadableSize ( $size ) . " </ td >
2014-06-23 22:04:13 +00:00
< td > " . (( $lastmodified ) ? datetime_convert('UTC', date_default_timezone_get(), $lastmodified ) : '') . " </ td >
</ tr > " ;
2014-01-10 02:19:49 +00:00
}
2014-06-25 09:54:10 +00:00
$html .= " <tr><td colspan= \" 8 \" ><hr /></td></tr>
</ table > " ;
2014-01-10 02:19:49 +00:00
2014-06-25 09:54:10 +00:00
$limit = service_class_fetch ( $owner , 'attach_upload_limit' );
$r = q ( " select sum(filesize) as total from attach where aid = %d " ,
intval ( $this -> auth -> channel_account_id )
);
$used = $r [ 0 ][ 'total' ];
if ( $used ) {
$quotaDesc = t ( '%1$s used' );
$quotaDesc = sprintf ( $quotaDesc ,
$this -> userReadableSize ( $used ));
}
if ( $limit && $used ) {
$quotaDesc = t ( '%1$s used of %2$s (%3$s%)' );
$quotaDesc = sprintf ( $quotaDesc ,
$this -> userReadableSize ( $used ),
$this -> userReadableSize ( $limit ),
round ( $used / $limit , 1 ));
}
if ( $limit || $used ) {
$html .= " <p><strong> " . t ( 'Total' ) . " </strong> " . $quotaDesc . " </p> " ;
}
$output = '' ;
2014-01-10 02:19:49 +00:00
if ( $this -> enablePost ) {
$this -> server -> broadcastEvent ( 'onHTMLActionsPanel' , array ( $parent , & $output ));
}
$html .= $output ;
2014-06-25 09:54:10 +00:00
get_app () -> page [ 'content' ] = $html ;
construct_page ( get_app ());
2014-01-10 02:19:49 +00:00
// return $html;
}
2014-06-25 09:54:10 +00:00
function userReadableSize ( $size ){
if ( is_numeric ( $size )) {
$incr = 0 ;
$k = 1024 ;
$unit = array ( 'bytes' , 'KB' , 'MB' , 'GB' , 'TB' , 'PB' );
while (( $size / $k ) >= 1 ){
$incr ++ ;
$size = round ( $size / $k , 2 );
}
return $size . " " . $unit [ $incr ];
} else {
return " " ;
}
}
2014-01-10 02:19:49 +00:00
public function htmlActionsPanel ( DAV\INode $node , & $output ) {
2014-05-05 13:54:56 +00:00
2014-06-23 22:04:13 +00:00
//Removed link to filestorage page
//if($this->auth->owner_id && $this->auth->owner_id == $this->auth->channel_id) {
// $channel = get_app()->get_channel();
// if($channel) {
// $output .= '<tr><td colspan="2"><a href="filestorage/' . $channel['channel_address'] . '" >' . t('Edit File properties') . '</a></td></tr><tr><td> </td></tr>';
// }
//}
2014-05-05 13:54:56 +00:00
2014-01-10 02:19:49 +00:00
if ( ! $node instanceof DAV\ICollection )
return ;
// We also know fairly certain that if an object is a non-extended
// SimpleCollection, we won't need to show the panel either.
if ( get_class ( $node ) === 'Sabre\\DAV\\SimpleCollection' )
return ;
2014-06-24 10:05:11 +00:00
$output .= ' < table >
< tr >
2014-06-25 09:54:10 +00:00
< td >< strong > '.t(' Create new folder ').' </ strong >& nbsp ; & nbsp ; & nbsp ; </ td >
2014-06-24 10:05:11 +00:00
< td >< form method = " post " action = " " >
< input type = " text " name = " name " />
2014-06-25 09:54:10 +00:00
< input type = " submit " value = " '.t('Create').' " />
2014-06-24 10:05:11 +00:00
< input type = " hidden " name = " sabreAction " value = " mkcol " />
</ form ></ td >
</ tr >< tr >
2014-06-25 09:54:10 +00:00
< td >< strong > '.t(' Upload file ').' </ strong >& nbsp ; & nbsp ; & nbsp ; </ td >
2014-06-24 10:05:11 +00:00
< td >< form method = " post " action = " " enctype = " multipart/form-data " >
< input type = " file " name = " file " style = " display: inline; " />
2014-06-25 09:54:10 +00:00
< input type = " submit " value = " '.t('Upload').' " />
2014-06-24 10:05:11 +00:00
< input type = " hidden " name = " sabreAction " value = " put " />
<!-- Name ( optional ) : < input type = " text " name = " name " /> we should rather provide a rename action in edit form -->
</ form ></ td >
</ tr >
</ table > ' ;
2014-01-10 02:19:49 +00:00
}
2014-02-15 20:48:40 +00:00
/**
* This method takes a path / name of an asset and turns it into url
* suiteable for http access .
*
* @ param string $assetName
* @ return string
*/
protected function getAssetUrl ( $assetName ) {
2014-02-15 21:25:14 +00:00
return z_root () . '/cloud/?sabreAction=asset&assetName=' . urlencode ( $assetName );
2014-02-15 20:48:40 +00:00
}
2014-01-10 02:19:49 +00:00
2014-06-23 22:04:13 +00:00
protected function findAttachHash ( $owner , $parentHash , $attachName ) {
$r = q ( " select * from attach where uid = %d and folder = '%s' and filename = '%s' order by edited desc limit 1 " ,
intval ( $owner ), dbesc ( $parentHash ), dbesc ( $attachName )
);
$hash = " " ;
if ( $r ) {
foreach ( $r as $rr ) {
$hash = $rr [ 'hash' ];
}
}
return $hash ;
}
protected function findAttachIdByHash ( $attachHash ) {
$r = q ( " select * from attach where hash = '%s' order by edited desc limit 1 " ,
dbesc ( $attachHash )
);
$id = " " ;
if ( $r ) {
foreach ( $r as $rr ) {
$id = $rr [ 'id' ];
}
}
return $id ;
}
2014-01-17 20:52:57 +00:00
}