MediaWiki REL1_34
UploadDef.php
Go to the documentation of this file.
1<?php
2
4
5use Psr\Http\Message\UploadedFileInterface;
9
33class UploadDef extends TypeDef {
34
35 public function getValue( $name, array $settings, array $options ) {
36 $ret = $this->callbacks->getUploadedFile( $name, $options );
37
38 if ( $ret && $ret->getError() === UPLOAD_ERR_NO_FILE &&
39 !$this->callbacks->hasParam( $name, $options )
40 ) {
41 // This seems to be that the client explicitly specified "no file" for the field
42 // instead of just omitting the field completely. DWTM.
43 $ret = null;
44 } elseif ( !$ret && $this->callbacks->hasParam( $name, $options ) ) {
45 // The client didn't format their upload properly so it came in as an ordinary
46 // field. Convert it to an error.
47 $ret = new UploadedFile( [
48 'name' => '',
49 'type' => '',
50 'tmp_name' => '',
51 'error' => -42, // PHP's UPLOAD_ERR_* are all positive numbers.
52 'size' => 0,
53 ] );
54 }
55
56 return $ret;
57 }
58
68 protected function getIniSize() {
69 return ini_get( 'upload_max_filesize' );
70 }
71
72 public function validate( $name, $value, array $settings, array $options ) {
73 static $codemap = [
74 -42 => 'notupload', // Local from getValue()
75 UPLOAD_ERR_FORM_SIZE => 'formsize',
76 UPLOAD_ERR_PARTIAL => 'partial',
77 UPLOAD_ERR_NO_FILE => 'nofile',
78 UPLOAD_ERR_NO_TMP_DIR => 'notmpdir',
79 UPLOAD_ERR_CANT_WRITE => 'cantwrite',
80 UPLOAD_ERR_EXTENSION => 'phpext',
81 ];
82
83 if ( !$value instanceof UploadedFileInterface ) {
84 // Err?
85 throw new ValidationException( $name, $value, $settings, 'badupload', [] );
86 }
87
88 $err = $value->getError();
89 if ( $err === UPLOAD_ERR_OK ) {
90 return $value;
91 } elseif ( $err === UPLOAD_ERR_INI_SIZE ) {
92 static $prefixes = [
93 'g' => 1024 ** 3,
94 'm' => 1024 ** 2,
95 'k' => 1024 ** 1,
96 ];
97 $size = $this->getIniSize();
98 $last = strtolower( substr( $size, -1 ) );
99 $size = intval( $size, 10 ) * ( $prefixes[$last] ?? 1 );
100 throw new ValidationException( $name, $value, $settings, 'badupload-inisize', [
101 'size' => $size,
102 ] );
103 } elseif ( isset( $codemap[$err] ) ) {
104 throw new ValidationException( $name, $value, $settings, 'badupload-' . $codemap[$err], [] );
105 } else {
106 throw new ValidationException( $name, $value, $settings, 'badupload-unknown', [
107 'code' => $err,
108 ] );
109 }
110 }
111
112 public function stringifyValue( $name, $value, array $settings, array $options ) {
113 // Not going to happen.
114 return null;
115 }
116
117}
Type definition for upload types.
Definition UploadDef.php:33
getIniSize()
Fetch the value of PHP's upload_max_filesize ini setting.
Definition UploadDef.php:68
stringifyValue( $name, $value, array $settings, array $options)
Convert a value to a string representation.
getValue( $name, array $settings, array $options)
Get the value from the request.
Definition UploadDef.php:35
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition UploadDef.php:72
Base definition for ParamValidator types.
Definition TypeDef.php:15
A simple implementation of UploadedFileInterface.
$last