MediaWiki master
UploadDef.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
6use Psr\Http\Message\UploadedFileInterface;
7use UnexpectedValueException;
12
34class UploadDef extends TypeDef {
35
37 public function getValue( $name, array $settings, array $options ) {
38 $ret = $this->callbacks->getUploadedFile( $name, $options );
39
40 if ( $ret && $ret->getError() === UPLOAD_ERR_NO_FILE &&
41 !$this->callbacks->hasParam( $name, $options )
42 ) {
43 // This seems to be that the client explicitly specified "no file" for the field
44 // instead of just omitting the field completely. DWTM.
45 $ret = null;
46 } elseif ( !$ret && $this->callbacks->hasParam( $name, $options ) ) {
47 // The client didn't format their upload properly so it came in as an ordinary
48 // field. Convert it to an error.
49 $ret = new UploadedFile( [
50 'name' => '',
51 'type' => '',
52 'tmp_name' => '',
53 'error' => -42, // PHP's UPLOAD_ERR_* are all positive numbers.
54 'size' => 0,
55 ] );
56 }
57
58 return $ret;
59 }
60
70 protected function getIniSize() {
71 return ini_get( 'upload_max_filesize' );
72 }
73
75 public function validate( $name, $value, array $settings, array $options ) {
76 static $codemap = [
77 -42 => 'notupload', // Local from getValue()
78 UPLOAD_ERR_FORM_SIZE => 'formsize',
79 UPLOAD_ERR_PARTIAL => 'partial',
80 UPLOAD_ERR_NO_FILE => 'nofile',
81 UPLOAD_ERR_NO_TMP_DIR => 'notmpdir',
82 UPLOAD_ERR_CANT_WRITE => 'cantwrite',
83 UPLOAD_ERR_EXTENSION => 'phpext',
84 ];
85
86 if ( !$value instanceof UploadedFileInterface ) {
87 // Err?
88 $type = get_debug_type( $value );
89 throw new InvalidArgumentException( "\$value must be UploadedFileInterface, got $type" );
90 }
91
92 $err = $value->getError();
93 if ( $err === UPLOAD_ERR_OK ) {
94 return $value;
95 } elseif ( $err === UPLOAD_ERR_INI_SIZE ) {
96 static $prefixes = [
97 'g' => 1024 ** 3,
98 'm' => 1024 ** 2,
99 'k' => 1024 ** 1,
100 ];
101 $size = $this->getIniSize();
102 $last = strtolower( substr( $size, -1 ) );
103 $size = intval( $size, 10 ) * ( $prefixes[$last] ?? 1 );
104 $this->failure(
105 $this->failureMessage( 'badupload', [
106 'code' => 'inisize',
107 'size' => $size,
108 ], 'inisize' )->sizeParams( $size ),
109 $name, '', $settings, $options
110 );
111 } elseif ( isset( $codemap[$err] ) ) {
112 $this->failure(
113 $this->failureMessage( 'badupload', [ 'code' => $codemap[$err] ], $codemap[$err] ),
114 $name, '', $settings, $options
115 );
116 } else {
117 $constant = '';
118 foreach ( get_defined_constants() as $c => $v ) {
119 // @phan-suppress-next-line PhanTypeComparisonFromArray
120 if ( $v === $err && str_starts_with( $c, 'UPLOAD_ERR_' ) ) {
121 $constant = " ($c?)";
122 }
123 }
124 throw new UnexpectedValueException( "Unrecognized PHP upload error value $err$constant" );
125 }
126 }
127
129 public function checkSettings( string $name, $settings, array $options, array $ret ): array {
130 $ret = parent::checkSettings( $name, $settings, $options, $ret );
131
132 if ( isset( $settings[ParamValidator::PARAM_DEFAULT] ) ) {
133 $ret['issues'][ParamValidator::PARAM_DEFAULT] =
134 'Cannot specify a default for upload-type parameters';
135 }
136
137 if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
138 !isset( $ret['issues'][ParamValidator::PARAM_ISMULTI] )
139 ) {
140 $ret['issues'][ParamValidator::PARAM_ISMULTI] =
141 'PARAM_ISMULTI cannot be used for upload-type parameters';
142 }
143
144 return $ret;
145 }
146
148 public function stringifyValue( $name, $value, array $settings, array $options ) {
149 // Not going to happen.
150 return null;
151 }
152
154 public function getHelpInfo( $name, array $settings, array $options ) {
155 $info = parent::getHelpInfo( $name, $settings, $options );
156
157 $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-upload' );
158
159 return $info;
160 }
161
162}
Value object representing a message for i18n.
Service for formatting and validating API parameters.
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
const PARAM_DEFAULT
(mixed) Default value of the parameter.
const PARAM_TYPE
(string|array) Type of the parameter.
Type definition for upload types.
Definition UploadDef.php:34
checkSettings(string $name, $settings, array $options, array $ret)
Validate a parameter settings array.This is intended for validation of parameter settings during unit...
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.Keys in the returned array should generally corr...
getIniSize()
Fetch the value of PHP's upload_max_filesize ini setting.
Definition UploadDef.php:70
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.This is intended as the inverse of getValue() and validate...
getValue( $name, array $settings, array $options)
Get the value from the request.to overrideOnly override this if you need to use something other than ...
Definition UploadDef.php:37
validate( $name, $value, array $settings, array $options)
Validate the value.When ParamValidator is processing a multi-valued parameter, this will be called on...
Definition UploadDef.php:75
Base definition for ParamValidator types.
Definition TypeDef.php:19
failureMessage( $code, ?array $data=null, $suffix=null)
Create a DataMessageValue representing a failure.
Definition TypeDef.php:156
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition TypeDef.php:121
A simple implementation of UploadedFileInterface.