better filename parsing

The filename found in the URL will be used for old plugins missing a
base entry in their plugin.info.txt and lacking a subdirectory inside
the archive as well. This patch makes sure possible query strings aren't
included in the filename.

Note: io_download() will also try to get a filename from any
content-disposition header.

If no filename can be found we simply use an md5 sum of the URL and hope
the plugin will contain it's own hint for naming.
This commit is contained in:
Andreas Gohr 2014-01-08 20:28:17 +01:00
parent 4bad83d828
commit bc1b7a8a0f
2 changed files with 10 additions and 4 deletions

View File

@ -783,11 +783,17 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin {
*/
public function download($url) {
// check the url
$matches = array();
if(!preg_match('/[^\/]*$/', $url, $matches) || !$matches[0]) {
if(!preg_match('/https?:\/\//i', $url)){
throw new Exception($this->getLang('error_badurl'));
}
$file = $matches[0];
// try to get the file from the path (used as plugin name fallback)
$file = parse_url($url, PHP_URL_PATH);
if(is_null($file)){
$file = md5($url);
}else{
$file = utf8_basename($file);
}
// create tmp directory for download
if(!($tmp = $this->mkTmpDir())) {

View File

@ -82,7 +82,7 @@ $lang['update_available'] = '<strong>Update:</strong> New version %s
$lang['wrong_folder'] = '<strong>Plugin installed incorrectly:</strong> Rename plugin directory "%s" to "%s".';
$lang['url_change'] = '<strong>URL changed:</strong> Download URL has changed since last download. Check if the new URL is valid before updating the extension.<br />New: %s<br />Old: %s';
$lang['error_badurl'] = 'URL ends with slash - unable to determine file name from the url';
$lang['error_badurl'] = 'URLs should start with http or https';
$lang['error_dircreate'] = 'Unable to create temporary folder to receive download';
$lang['error_download'] = 'Unable to download the file: %s';
$lang['error_decompress'] = 'Unable to decompress the downloaded file. This maybe as a result of a bad download, in which case you should try again; or the compression format may be unknown, in which case you will need to download and install manually.';