move groupdav to_ascii to translation class; unify content_header and content_disposition_header to be used by all downloads; adapt vfs_webdav_server thereof

This commit is contained in:
Klaus Leithoff 2014-02-25 15:28:36 +00:00
parent ed6c1980d4
commit a763dd6234
4 changed files with 60 additions and 31 deletions

View File

@ -1161,30 +1161,9 @@ class groupdav_principals extends groupdav_handler
$displayname = translation::convert($resource['name'], translation::charset(), 'utf-8');
return ($is_location ? 'locations/' : 'resources/').$resource['res_id'].'-'.
preg_replace('/[^a-z0-9]+/i','-', self::to_ascii($resource['name']));
preg_replace('/[^a-z0-9]+/i','-', translation::to_ascii($resource['name']));
}
/**
* Transliterate utf-8 filename to ascii, eg. 'Äpfel' --> 'Aepfel'
*
* @param string $str
* @return string
*/
protected static function to_ascii($str)
{
static $extra = array(
'ß' => 'ss',
);
$str = htmlentities($str,ENT_QUOTES,translation::charset());
$str = str_replace(array_keys($extra),array_values($extra),$str);
$str = preg_replace('/&([aAuUoO])uml;/','\\1e',$str); // replace german umlauts with the letter plus one 'e'
$str = preg_replace('/&([a-zA-Z])(grave|acute|circ|ring|cedil|tilde|slash|uml);/','\\1',$str); // remove all types of accents
$str = preg_replace('/&([a-zA-Z]+|#[0-9]+|);/','',$str); // remove all other entities
return $str;
}
/**
* Check if resource is a location
*

View File

@ -1055,7 +1055,7 @@ class html
{
if (!is_array($options)) $options = explode(',',$options);
if (!is_array($names)) $names = explode(',',$names);
//if (empty($options)) return "";//this should not be needed
foreach($options as $n => $val)
{
if ($val != '' && $names[$n] != '')
@ -1368,8 +1368,9 @@ class html
* @param string $mime='' mimetype or '' (default) to detect it from filename, using mime_magic::filename2mime()
* @param int $length=0 content length, default 0 = skip that header
* @param boolean $nocache=true send headers to disallow browser/proxies to cache the download
* @param boolean $forceDownload=true send headers to handle as attachment/download
*/
public static function content_header($fn,$mime='',$length=0,$nocache=True)
public static function content_header($fn,$mime='',$length=0,$nocache=True,$forceDownload=true)
{
// if no mime-type is given or it's the default binary-type, guess it from the extension
if(empty($mime) || $mime == 'application/octet-stream')
@ -1378,18 +1379,22 @@ class html
}
if($fn)
{
if ($forceDownload===true)
{
$attachment = ' attachment;';
}
else
{
$attachment = ' inline;';
}
// limit IE hack (no attachment in Content-disposition header) to IE < 9
if(self::$user_agent == 'msie' && self::$ua_version < 9)
{
$attachment = '';
}
else
{
$attachment = ' attachment;';
}
// Show this for all
header('Content-disposition:'.$attachment.' filename="'.$fn.'"');
self::content_disposition_header($fn,$forceDownload);
header('Content-type: '.$mime);
if($length)
@ -1407,6 +1412,31 @@ class html
}
}
/**
* Output content-disposition header for file downloads
*
* @param string $fn filename
* @param boolean $forceDownload=true send headers to handle as attachment/download
*/
public static function content_disposition_header($fn,$forceDownload=true)
{
if ($forceDownload===true)
{
$attachment = ' attachment;';
}
else
{
$attachment = ' inline;';
}
// limit IE hack (no attachment in Content-disposition header) to IE < 9
if(self::$user_agent == 'msie' && self::$ua_version < 9)
{
$attachment = '';
}
header('Content-disposition:'.$attachment.' filename="'.translation::to_ascii($fn).'"; filename*=utf-8\'\''.rawurlencode($fn));
}
/**
* split html by PRE tag, return array with all content pre-sections isolated in array elements
* @author Leithoff, Klaus

View File

@ -624,6 +624,26 @@ class translation
return $charsets;
}
/**
* Transliterate utf-8 filename to ascii, eg. 'Äpfel' --> 'Aepfel'
*
* @param string $str
* @return string
*/
static function to_ascii($str)
{
static $extra = array(
'&szlig;' => 'ss',
);
$str = htmlentities($str,ENT_QUOTES,self::charset());
$str = str_replace(array_keys($extra),array_values($extra),$str);
$str = preg_replace('/&([aAuUoO])uml;/','\\1e',$str); // replace german umlauts with the letter plus one 'e'
$str = preg_replace('/&([a-zA-Z])(grave|acute|circ|ring|cedil|tilde|slash|uml);/','\\1',$str); // remove all types of accents
$str = preg_replace('/&([a-zA-Z]+|#[0-9]+|);/','',$str); // remove all other entities
return $str;
}
/**
* converts a string $data from charset $from to charset $to
*

View File

@ -692,7 +692,7 @@ class vfs_webdav_server extends HTTP_WebDAV_Server_Filesystem
}
if ($this->force_download)
{
header('Content-disposition: attachment; filename="'.egw_vfs::basename($options['path']).'"');
html::content_disposition_header(egw_vfs::basename($options['path']),true);
}
}
return $ok;
@ -923,4 +923,4 @@ class vfs_webdav_server extends HTTP_WebDAV_Server_Filesystem
}
return parent::PUT($options);
}
}
}