Debian Codegraph @

includes/libs/ParamValidator/TypeDef/UploadDef.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\ParamValidator\TypeDef;
4
5 use InvalidArgumentException;
6 use Psr\Http\Message\UploadedFileInterface;
7 use UnexpectedValueException;
8 use Wikimedia\Message\MessageValue;
9 use Wikimedia\ParamValidator\ParamValidator;
10 use Wikimedia\ParamValidator\TypeDef;
11 use Wikimedia\ParamValidator\Util\UploadedFile;
12
13 /**
14 * Type definition for upload types
15 *
16 * The result from validate() is an object implementing UploadedFileInterface.
17 *
18 * Failure codes:
19 * - 'badupload': The upload is not valid. Data:
20 * - 'code': A value indicating why the upload was not valid:
21 * - 'inisize': The upload exceeded the maximum in php.ini.
22 * - 'formsize': The upload exceeded the maximum in the form post.
23 * - 'partial': The file was only partially uploaded.
24 * - 'nofile': There was no file.
25 * - 'notmpdir': PHP has no temporary directory to store the upload.
26 * - 'cantwrite': PHP could not store the upload.
27 * - 'phpext': A PHP extension rejected the upload.
28 * - 'notupload': The field was present in the submission but was not encoded as an upload.
29 * - 'size': The configured size (in bytes), if 'code' is 'inisize'.
30 *
31 * @since 1.34
32 * @unstable
33 */
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
60 /**
61 * Fetch the value of PHP's upload_max_filesize ini setting
62 *
63 * This method exists so it can be mocked by unit tests that can't
64 * affect ini_get() directly.
65 *
66 * @codeCoverageIgnore
67 * @return string|false
68 */
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 = get_debug_type( $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 // @phan-suppress-next-line PhanTypeComparisonFromArray
118 if ( $v === $err && str_starts_with( $c, 'UPLOAD_ERR_' ) ) {
119 $constant = " ($c?)";
120 }
121 }
122 throw new UnexpectedValueException( "Unrecognized PHP upload error value $err$constant" );
123 }
124 }
125
126 public function checkSettings( string $name, $settings, array $options, array $ret ): array {
127 $ret = parent::checkSettings( $name, $settings, $options, $ret );
128
129 if ( isset( $settings[ParamValidator::PARAM_DEFAULT] ) ) {
130 $ret['issues'][ParamValidator::PARAM_DEFAULT] =
131 'Cannot specify a default for upload-type parameters';
132 }
133
134 if ( !empty( $settings[ParamValidator::PARAM_ISMULTI] ) &&
135 !isset( $ret['issues'][ParamValidator::PARAM_ISMULTI] )
136 ) {
137 $ret['issues'][ParamValidator::PARAM_ISMULTI] =
138 'PARAM_ISMULTI cannot be used for upload-type parameters';
139 }
140
141 return $ret;
142 }
143
144 public function stringifyValue( $name, $value, array $settings, array $options ) {
145 // Not going to happen.
146 return null;
147 }
148
149 public function getHelpInfo( $name, array $settings, array $options ) {
150 $info = parent::getHelpInfo( $name, $settings, $options );
151
152 $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-upload' );
153
154 return $info;
155 }
156
157 }