Guest User

Untitled

a guest
Jul 23rd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. private function parsePut( )
  2. {
  3. $data = [];
  4. $files = [];
  5.  
  6. /* PUT data comes in on the stdin stream */
  7. $putdata = fopen("php://input", "r");
  8.  
  9. /* Open a file for writing */
  10. // $fp = fopen("myputfile.ext", "w");
  11.  
  12. $raw_data = '';
  13.  
  14. /* Read the data 1 KB at a time
  15. and write to the file */
  16. while ($chunk = fread($putdata, 1024))
  17. $raw_data .= $chunk;
  18.  
  19. /* Close the streams */
  20. fclose($putdata);
  21.  
  22. // Fetch content and determine boundary
  23. $boundary = substr($raw_data, 0, strpos($raw_data, "\r\n"));
  24.  
  25. if(empty($boundary)){
  26. parse_str($raw_data,$data);
  27. return $data;
  28. }
  29.  
  30. // Fetch each part
  31. $parts = array_slice(explode($boundary, $raw_data), 1);
  32.  
  33. foreach ($parts as $part) {
  34. // If this is the last part, break
  35. if ($part == "--\r\n") break;
  36.  
  37. // Separate content from headers
  38. $part = ltrim($part, "\r\n");
  39. list($raw_headers, $body) = explode("\r\n\r\n", $part, 2);
  40.  
  41. // Parse the headers list
  42. $raw_headers = explode("\r\n", $raw_headers);
  43. $headers = array();
  44. $value = null;
  45.  
  46. foreach ($raw_headers as $header) {
  47. list($name, $value) = explode(':', $header);
  48. $headers[strtolower($name)] = ltrim($value, ' ');
  49. }
  50.  
  51. // Parse the Content-Disposition to get the field name, etc.
  52. if (isset($headers['content-disposition'])) {
  53. $filename = null;
  54. $tmp_name = null;
  55. preg_match(
  56. '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/',
  57. $headers['content-disposition'],
  58. $matches
  59. );
  60.  
  61. //Parse File
  62. if( isset($matches[4]) )
  63. {
  64. //if labeled the same as previous, skip
  65. if( isset( $files[ $matches[ 2 ] ] ) )
  66. {
  67. continue;
  68. }
  69.  
  70. //get filename
  71. $filename = $matches[4];
  72.  
  73. //get tmp name
  74. $filename_parts = pathinfo( $filename );
  75. $tmp_name = tempnam( ini_get('upload_tmp_dir'), $filename_parts['filename']);
  76.  
  77. //populate $_FILES with information, size may be off in multibyte situation
  78. $files[ $matches[ 2 ] ] = array(
  79. 'error'=>0,
  80. 'name'=>$filename,
  81. 'tmp_name'=>$tmp_name,
  82. 'size'=>strlen( $body ),
  83. 'type'=> $value
  84. );
  85.  
  86. //place in temporary directory
  87. file_put_contents($tmp_name, $body);
  88. }
  89. //Parse Field
  90. else
  91. {
  92. $data[$matches[ 2 ]] = substr($body, 0, strlen($body) - 2);
  93. }
  94. }
  95.  
  96. }
  97.  
  98. $data = $this->format_form_data($data);
  99.  
  100. return ['put' => $data, 'files' => $files];
  101. }
  102.  
  103. private function format_form_data(array $data) {
  104.  
  105. $matches = array();
  106. $result = [];
  107.  
  108. foreach($data as $key => $value) {
  109. preg_match_all("/\[(.*?)\]/", $key, $matches);
  110. $matches = array_reverse($matches[1]);
  111. $matches[] = substr( $key, 0, strpos($key, '['));;
  112.  
  113. foreach ($matches as $match) {
  114. $value = [$match=>$value];
  115. }
  116.  
  117. $result = array_replace_recursive($result, $value);
  118. }
  119.  
  120. return $result;
  121. }
Add Comment
Please, Sign In to add comment