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