Commit a615828
committed
Add ini_bytes to translate ini directive byte shorthand to a count
With WordPress we have seen a variety of bugs that stem from the fact that we
don't reliably parse the memory size values from php.ini directives. We want
to know those values in order to communicate to site users what the file
upload limits are.
In this patch I'm adding a new function `ini_bytes( $shorthand )` to return
whatever PHP evaluates that value to be internally, thus exposing the actual
byte size to the outside world. This eliminates the need for WordPress and
other projects to port the config value parser since they could directly
call it. Additionally it will reliably reproduce any quirks due to the
platform build and overflow behaviors when handling the `g`, `m`, and `k`
suffixes on `post_max_size` and `upload_max_filesize` (and others).
This function is a shallow wrapper around `zend_atoi()` and could become
out of sync with the actual ini directive parsing. I considered a variation
of this function which would take a directive's name and return the stored
internal value directly, keeping it in sync.
```php
switch ( param_name ) {
case 'post_max_size':
RETVAL_LONG(SG(post_max_size));
}
```
After looking briefly at this option I dismissed it for being a bit too
coupled to those options, too demanding to add all the possible config
options to a big switch statement, and too complicated for returning all
of the possible return types stored in the SAPI globals.
This is my first attempt at patching PHP and I don't know what I'm doing :)
I'm not thrilled at the function name but I needed something.
I'm not aware of what I need to write for testing, documentation, etc...
but I'm happy to add whatever is required.
The expecation is to use it in situations roughly like this:
```php
function max_upload_size() {
$u_bytes = ini_bytes( ini_get( 'upload_max_filesize' ) || 0 );
$p_bytes = ini_bytes( ini_get( 'post_max_size' ) || 0 );
return min( $u_bytes, $p_bytes );
}
```
This patch also makes it easier to perform mathematic comparisons and
operations on the config values, which again previously required duplicating
the parsing outside of PHP and likely will fail for a variety of config values.
```php
function validate_upload_size() {
$u_bytes = ini_bytes( ini_get( 'upload_max_filesize' ) || 0 );
$p_bytes = ini_bytes( ini_get( 'post_max_size' ) || 0 );
return $u_bytes <= $p_bytes
? [ true ]
: [ false, 'upload_max_filesize must be smaller than post_max_size' ];
}
```1 parent 3648610 commit a615828
File tree
3 files changed
+22
-0
lines changed- ext/standard
3 files changed
+22
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1969 | 1969 | | |
1970 | 1970 | | |
1971 | 1971 | | |
| 1972 | + | |
| 1973 | + | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
1972 | 1986 | | |
1973 | 1987 | | |
1974 | 1988 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
493 | 493 | | |
494 | 494 | | |
495 | 495 | | |
| 496 | + | |
| 497 | + | |
496 | 498 | | |
497 | 499 | | |
498 | 500 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
484 | 484 | | |
485 | 485 | | |
486 | 486 | | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
487 | 491 | | |
488 | 492 | | |
489 | 493 | | |
| |||
2354 | 2358 | | |
2355 | 2359 | | |
2356 | 2360 | | |
| 2361 | + | |
2357 | 2362 | | |
2358 | 2363 | | |
2359 | 2364 | | |
| |||
2985 | 2990 | | |
2986 | 2991 | | |
2987 | 2992 | | |
| 2993 | + | |
2988 | 2994 | | |
2989 | 2995 | | |
2990 | 2996 | | |
| |||
0 commit comments