Skip to content

Commit a312f34

Browse files
committed
Initial commit with the code.
1 parent 35df4a0 commit a312f34

File tree

6 files changed

+288
-0
lines changed

6 files changed

+288
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/*

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## What is brunodebarros/fix-php-post-input
2+
3+
`brunodebarros/fix-php-post-input` fixes your `$_POST` and `$_FILES` if they're empty and they shouldn't be.
4+
5+
**Why?** Some incorrectly setup servers will not pass the right information required for PHP to automatically process input data (e.g. they'll say the HTTP method was GET because of an internal redirect).
6+
7+
For apps where you control the environment, this is easy to resolve. For self-hosted apps, that anyone can install anywhere, it's important to make sure that things continue to work without having to ask less tech-savvy users to mess with their server configurations.
8+
9+
In those cases, this library will automatically fix the `$_POST` and `$_FILES` arrays for you so that you can continue working without having to worry about it.
10+
11+
As a bonus, it will also process JSON (`application/json`) input into `$_POST` for you.
12+
13+
## Requirements
14+
15+
* PHP 5.4 or HHVM
16+
17+
## Installation
18+
19+
`brunodebarros/fix-php-post-input` loads automatically with Composer.
20+
21+
```
22+
composer require brunodebarros/fix-php-post-input
23+
```
24+
25+
## Usage
26+
27+
Once required, the package will automatically fix your `$_POST`/`$_FILES` for you. If they don't need fixing, this package will do nothing.
28+
29+
## Suggestions, questions and complaints.
30+
31+
If you've got any suggestions, questions, or anything you don't like about this library, [you should create an issue here](https://github.com/BrunoDeBarros/fix-php-post-input/issues/new). Feel free to fork this project, if you want to contribute to it.

RawHttpRequest.php

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
namespace Brunodebarros\FixPhpPostInput;
4+
5+
/**
6+
* If your $_POST/$_FILES are empty and they shouldn't be, this library fixes them.
7+
*
8+
* @package Brunodebarros\FixPhpPostInput
9+
*/
10+
class FixPhpPostInput {
11+
12+
protected $temp_files = [];
13+
14+
protected function edit_array($array, $name, $value) {
15+
$name_parts = explode("[", $name);
16+
$array_part = &$array;
17+
18+
$is_last = false;
19+
$i = 1;
20+
foreach ($name_parts as $name_part) {
21+
if ($i == count($name_parts)) {
22+
$is_last = true;
23+
}
24+
25+
$name_part = str_ireplace(']', "", $name_part);
26+
27+
if (!isset($array_part[$name_part])) {
28+
if (empty($name_part)) {
29+
$array_part[] = ($is_last ? $value : []);
30+
} else {
31+
$array_part[$name_part] = ($is_last ? $value : []);
32+
}
33+
34+
}
35+
36+
if (!$is_last) {
37+
$array_part = &$array_part[$name_part];
38+
}
39+
40+
$i++;
41+
}
42+
43+
return $array;
44+
}
45+
46+
protected function store_temp($contents) {
47+
$tmp_name = tempnam(sys_get_temp_dir(), "file-");
48+
$this->temp_files[] = $tmp_name;
49+
file_put_contents($tmp_name, $contents);
50+
return $tmp_name;
51+
}
52+
53+
protected function map_name_to_array($name, $array) {
54+
if (substr($name, -2) == "[]") {
55+
if (isset($array[substr($name, 0, -2)])) {
56+
return $array[substr($name, 0, -2)];
57+
}
58+
}
59+
60+
if (isset($array[$name])) {
61+
return $array[$name];
62+
}
63+
64+
65+
$return = null;
66+
$code = 'if(isset($array' . implode("", $name) . ')) {$return = $array' . implode("", $name) . ';} else { $return = null; }';
67+
eval($code);
68+
return $return;
69+
}
70+
71+
function __construct(&$post = null, &$files = null, $server = null, $php_input = null) {
72+
73+
if ($post === null) {
74+
$post = &$_POST;
75+
}
76+
77+
if ($files === null) {
78+
$files = &$_FILES;
79+
}
80+
81+
if ($php_input === null) {
82+
$php_input = file_get_contents('php://input');
83+
}
84+
85+
if ($server === null) {
86+
$server = $_SERVER;
87+
}
88+
89+
if (count($post) == 0 || IS_DEBUGGING) {
90+
$is_post = false;
91+
foreach ($server as $key => $value) {
92+
$search = "REQUEST_METHOD";
93+
if (substr($key, -strlen($search)) == $search) {
94+
if ($value == "POST") {
95+
$is_post = true;
96+
}
97+
}
98+
}
99+
100+
if ($is_post) {
101+
if (!empty($php_input)) {
102+
$is_multipart = (stristr($server["CONTENT_TYPE"], "multipart/form-data") !== false);
103+
$is_json = (stristr($server["CONTENT_TYPE"], "application/json") !== false);
104+
105+
if ($is_json) {
106+
$post = json_decode($php_input, true);
107+
} elseif ($is_multipart) {
108+
$document = new \Riverline\MultiPartParser\Part("Content-Type: " . $server["CONTENT_TYPE"] . "\n\n" . $php_input);
109+
foreach ($document->getParts() as $part) {
110+
if ($part->isFile()) {
111+
$size = strlen($part->getBody());
112+
113+
$data = [
114+
"name" => $part->getFileName(),
115+
"type" => $size ? $part->getMimeType() : "",
116+
"tmp_name" => $size ? $this->store_temp($part->getBody()) : "",
117+
"error" => $size ? UPLOAD_ERR_OK : UPLOAD_ERR_NO_FILE,
118+
"size" => $size,
119+
];
120+
121+
foreach ($data as $key => $value) {
122+
$name = explode("[", $part->getName(), 2);
123+
$name = $name[0] . "[$key][" . $name[1];
124+
$files = $this->edit_array($files, $name, $value);
125+
}
126+
} else {
127+
$post = $this->edit_array($post, $part->getName(), $part->getBody());
128+
}
129+
}
130+
} else {
131+
parse_str($php_input, $post);
132+
}
133+
}
134+
}
135+
}
136+
}
137+
138+
function __destruct() {
139+
foreach ($this->temp_files as $temp_file) {
140+
if (file_exists($temp_file)) {
141+
unlink($temp_file);
142+
}
143+
}
144+
}
145+
146+
}

bootstrap.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
new Brunodebarros\FixPhpPostInput\FixPhpPostInput();

composer.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "brunodebarros/fix-php-post-input",
3+
"type": "library",
4+
"description": "If your $_POST/$_FILES are empty and they shouldn't be, this library fixes them.",
5+
"keywords": [
6+
"compatibility",
7+
"post",
8+
"files",
9+
"input"
10+
],
11+
"homepage": "https://github.com/BrunoDeBarros/fix-php-post-input",
12+
"license": "MIT",
13+
"authors": [
14+
{
15+
"name": "Bruno De Barros",
16+
"email": "[email protected]"
17+
}
18+
],
19+
"require": {
20+
"php": ">=5.4",
21+
"riverline/multipart-parser": "dev-master"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Brunodebarros\\FixPhpPostInput\\": ""
26+
},
27+
"files": [
28+
"bootstrap.php"
29+
]
30+
},
31+
"minimum-stability": "dev",
32+
"config": {
33+
"preferred-install": "dist",
34+
"sort-packages": true,
35+
"optimize-autoloader": true
36+
}
37+
}

composer.lock

+70
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)