Example #1
0
 /**
  * {@inheritdoc}
  */
 public function decode() : array
 {
     $querystring = @file_get_contents($this->path, true);
     if ($querystring === false) {
         throw new \RuntimeException(\sndsgd\Error::createMessage("failed to read input stream"));
     }
     $decoder = new QueryStringDecoder($this->contentLength, $this->values);
     return $decoder->decode($querystring);
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function toArray() : array
 {
     $ret = parent::toArray();
     if ($this->type !== ValidatorOptions::DEFAULT_FIELD_TYPE) {
         $ret["parameter"] = ["type" => $this->type, "name" => $this->fieldName];
     } else {
         $ret[$this->type] = $this->fieldName;
     }
     return $ret;
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function decode() : array
 {
     $json = @file_get_contents($this->path, true);
     if ($json === false) {
         throw new \RuntimeException(\sndsgd\Error::createMessage("failed to read input stream"));
     }
     $maxNestingLevels = $this->options->getMaxNestingLevels();
     $data = json_decode($json, true, $maxNestingLevels);
     if ($data === null) {
         $message = json_last_error_msg();
         throw new \sndsgd\http\data\DecodeException("failed to decode JSON request data; {$message}");
     }
     return $data;
 }
Example #4
0
 /**
  * Copy file contents from the input stream to a temp file
  *
  * @param string $name The field name
  * @param string $filename The name of the uploaded file
  * @param string $unverifiedContentType The user provided content type
  * @return \sndsgd\http\UploadedFile
  */
 protected function getFileFromField(string $name, string $filename, string $unverifiedContentType)
 {
     # create and open a temp file to write the contents to
     $tempPath = $this->getTempFilePath();
     $tempHandle = fopen($tempPath, "w");
     if ($tempHandle === false) {
         fclose($this->fp);
         $message = \sndsgd\Error::createMessage("failed to open '{$tempPath}' for writing");
         throw new \RuntimeException($message);
     }
     # number of bytes read from the input stream in the last loop cycle
     $bytesRead = 0;
     # the total number of bytes written to the temp file
     $bytesWritten = 0;
     # if anything is left over from the previous field, add it to the file
     if ($this->buffer !== "") {
         $bytesRead = fwrite($tempHandle, $this->buffer);
         if ($bytesRead === false) {
             fclose($this->fp);
             fclose($tempHandle);
             throw new \RuntimeException("fwrite() failed to write to '{$tempPath}'");
         }
         $bytesWritten += $bytesRead;
     }
     while (($pos = strpos($this->buffer, $this->boundary)) === false) {
         $this->buffer = fread($this->fp, $this->bytesPerRead);
         $bytesRead = fwrite($tempHandle, $this->buffer);
         if ($bytesRead === false) {
             fclose($this->fp);
             fclose($tempHandle);
             throw new \RuntimeException("fwrite() failed to write to '{$tempPath}'");
         }
         $bytesWritten += $bytesRead;
     }
     # determine the size of the file based on the boundary position
     $size = $bytesWritten - $bytesRead + $pos - 2;
     # trim the excess contents of the local buffer to the object buffer
     $this->buffer = substr($this->buffer, $pos);
     # if the uploaded file was empty
     if ($size < 1) {
         return $this->fileUploadError(UPLOAD_ERR_NO_FILE, $tempPath, $tempHandle, $filename, $unverifiedContentType, 0);
     } elseif ($size > $this->maxFileSize) {
         return $this->fileUploadError(UPLOAD_ERR_INI_SIZE, $tempPath, $tempHandle, $filename, $unverifiedContentType, 0);
     }
     ftruncate($tempHandle, $size);
     fclose($tempHandle);
     return new \sndsgd\http\UploadedFile($filename, $unverifiedContentType, $size, $tempPath);
 }