X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2Fapi%2FApiFormatBase.php;h=18c36deb076c0c666ef8ac90830e96e15c3dae4d;hp=5e14e307102dfc8171437144b6a69726b57860a9;hb=8fd5a99f4e261d37cd7237a1992b1bcf03a764d3;hpb=5bf7087cefdb77daaefd7a488ae68cae60b3e406 diff --git a/includes/api/ApiFormatBase.php b/includes/api/ApiFormatBase.php index 5e14e30710..18c36deb07 100644 --- a/includes/api/ApiFormatBase.php +++ b/includes/api/ApiFormatBase.php @@ -1,9 +1,5 @@ @gmail.com" * * This program is free software; you can redistribute it and/or modify @@ -69,8 +65,7 @@ abstract class ApiFormatBase extends ApiBase { * @note If $this->getIsWrappedHtml() || $this->getIsHtml(), you'll very * likely want to fall back to this class's version. * @since 1.27 - * @return string Generally this should be "api-result.$ext", and must be - * encoded for inclusion in a Content-Disposition header's filename parameter. + * @return string Generally this should be "api-result.$ext" */ public function getFilename() { if ( $this->getIsWrappedHtml() ) { @@ -216,10 +211,25 @@ abstract class ApiFormatBase extends ApiBase { // Set a Content-Disposition header so something downloading an API // response uses a halfway-sensible filename (T128209). + $header = 'Content-Disposition: inline'; $filename = $this->getFilename(); - $this->getMain()->getRequest()->response()->header( - "Content-Disposition: inline; filename=\"{$filename}\"" - ); + $compatFilename = mb_convert_encoding( $filename, 'ISO-8859-1' ); + if ( preg_match( '/^[0-9a-zA-Z!#$%&\'*+\-.^_`|~]+$/', $compatFilename ) ) { + $header .= '; filename=' . $compatFilename; + } else { + $header .= '; filename="' + . preg_replace( '/([\0-\x1f"\x5c\x7f])/', '\\\\$1', $compatFilename ) . '"'; + } + if ( $compatFilename !== $filename ) { + $value = "UTF-8''" . rawurlencode( $filename ); + // rawurlencode() encodes more characters than RFC 5987 specifies. Unescape the ones it allows. + $value = strtr( $value, [ + '%21' => '!', '%23' => '#', '%24' => '$', '%26' => '&', '%2B' => '+', '%5E' => '^', + '%60' => '`', '%7C' => '|', + ] ); + $header .= '; filename*=' . $value; + } + $this->getMain()->getRequest()->response()->header( $header ); } /**