예제 #1
0
 public function testParametersPersistNameAndValues()
 {
     $parameters = new Parameters(array('foo' => 'bar'));
     $this->assertEquals('bar', $parameters['foo']);
     $this->assertEquals('bar', $parameters->foo);
     $parameters->offsetSet('baz', 5);
     $this->assertEquals(5, $parameters->baz);
     $parameters->fromArray(array('bar' => 'foo'));
     $this->assertEquals('foo', $parameters->bar);
     $parameters->fromString('bar=foo&five=5');
     $this->assertEquals('foo', $parameters->bar);
     $this->assertEquals('5', $parameters->five);
     $this->assertEquals(array('bar' => 'foo', 'five' => '5'), $parameters->toArray());
     $this->assertEquals('bar=foo&five=5', $parameters->toString());
     $parameters->fromArray(array());
     $parameters->set('foof', 'barf');
     $this->assertEquals('barf', $parameters->get('foof'));
     $this->assertEquals('barf', $parameters->foof);
 }
 /**
  * {@inheritdoc}
  */
 public function onDispatch(MvcEvent $e)
 {
     $this->registerDefaultProcessStrategy();
     //TEMP
     $routeMatch = $e->getRouteMatch();
     if ($routeMatch instanceof RouteMatch) {
         $message = $routeMatch->getParam('message', null);
         if ($message) {
             if (is_string($message)) {
                 $parameter = new Parameters();
                 $parameter->fromString($message);
                 $message = new Message();
                 $message->setContent($parameter->get('content'));
                 $message->setMetadata($parameter->get('metadata', []));
                 $routeMatch->setParam('message', $message);
             }
         } else {
             stream_set_blocking(STDIN, 0);
             $stdin = file_get_contents('php://stdin');
             $message = $this->getSerializer()->unserialize($stdin);
             $routeMatch->setParam('message', $message);
         }
         $cliPassthru = $routeMatch->getParam('cli-passthru', null);
         if ($cliPassthru) {
             $this->cliPassthru = sprintf('%s -f %s -- %s', PHP_BINARY, realpath($_SERVER['PHP_SELF']), $cliPassthru);
         }
     }
     return parent::onDispatch($e);
 }
 /**
  * Parse upload content from a content stream
  *
  * @param  resource $stream
  * @return array
  * @throws Exception\InvalidMultipartContentException
  */
 protected function parseFromStream($stream)
 {
     $buffer = '';
     $data = new Parameters();
     $files = new Parameters();
     $partInProgress = false;
     $inHeader = false;
     $headers = [];
     $header = false;
     $name = false;
     $content = '';
     $file = [];
     $filename = false;
     $mimeType = false;
     $tmpFile = false;
     $partBoundaryPatternStart = '/^--' . $this->boundary . '(--)?/';
     $partBoundaryPatternEnd = '/^--' . $this->boundary . '--$/';
     while (false !== ($line = fgets($stream))) {
         $trimmedLine = rtrim($line);
         if (preg_match($partBoundaryPatternStart, $trimmedLine)) {
             if ($partInProgress) {
                 // Time to handle the data we've already parsed!
                 // Data
                 if (!$filename) {
                     $content = rtrim($content, "\r\n");
                     $buffer .= sprintf('%s=%s&', $name, rawurlencode($content));
                 }
                 // File (successful upload so far)
                 if ($filename && $tmpFile) {
                     // Write the last line, stripping the EOL characters
                     if (false === fwrite($tmpFile, rtrim($lastline, "\r\n"))) {
                         // Ooops! error writing the very last line!
                         $file['error'] = UPLOAD_ERR_CANT_WRITE;
                         fclose($tmpFile);
                     } else {
                         // Success! Let's try and guess the MIME type based on the file written
                         fclose($tmpFile);
                         if ($mimeType === 'application/octet-stream' && function_exists('finfo_open')) {
                             $finfo = finfo_open(FILEINFO_MIME_TYPE);
                             $type = finfo_file($finfo, $file['tmp_name']);
                             if (false !== $type) {
                                 $file['type'] = $type;
                             }
                         }
                         // Finally, set the filesize
                         $file['size'] = filesize($file['tmp_name']);
                     }
                 }
                 if ($filename) {
                     // At this point, we can add the file entry, regardless of error condition
                     $files->set($name, $file);
                 }
             }
             // Is this a boundary end? If so, we're done
             if (preg_match($partBoundaryPatternEnd, $trimmedLine)) {
                 // Met the "end" boundary; time to stop!
                 break;
             }
             // New part to parse!
             $partInProgress = true;
             $inHeader = true;
             $headers = [];
             $header = '';
             continue;
         }
         if (!$partInProgress) {
             // We're not inside a part, so do nothing.
             continue;
         }
         if ($inHeader) {
             if (preg_match('/^\\s*$/s', $line)) {
                 // Headers are done; cleanup
                 $inHeader = false;
                 $content = '';
                 $file = ['error' => UPLOAD_ERR_OK];
                 $tmpFile = false;
                 $lastline = null;
                 // Parse headers
                 $name = $this->getNameFromHeaders($headers);
                 if (!$name) {
                     throw new Exception\InvalidMultipartContentException('Missing Content-Disposition header, or Content-Disposition header does not ' . 'contain a "name" field');
                 }
                 $filename = $this->getFilenameFromHeaders($headers);
                 $mimeType = $this->getMimeTypeFromHeaders($headers);
                 continue;
             }
             if (preg_match('/^(?P<header>[a-z]+[a-z0-9_-]+):\\s*(?P<value>.*)$/i', $trimmedLine, $matches)) {
                 $header = strtoupper($matches['header']);
                 $headers[$header] = $matches['value'];
                 continue;
             }
             if (!$header) {
                 throw new Exception\InvalidMultipartContentException('Malformed or missing MIME part header for multipart content');
             }
             $headers[$header] .= $trimmedLine;
             continue;
         }
         // In the body content...
         // Data only; aggregate.
         if (!$filename) {
             $content .= $line;
             continue;
         }
         // If we've had an error already with the upload, continue parsing
         // to the end of the MIME part
         if ($file['error'] !== UPLOAD_ERR_OK) {
             continue;
         }
         // Create a temporary file handle if we haven't already
         if (!$tmpFile) {
             // Sets the file entry
             $file['name'] = $filename;
             $file['type'] = $mimeType;
             $tmpDir = $this->getUploadTempDir();
             if (empty($tmpDir)) {
                 // Cannot ascertain temporary directory; this is an error
                 $file['error'] = UPLOAD_ERR_NO_TMP_DIR;
                 continue;
             }
             $file['tmp_name'] = tempnam($tmpDir, 'zfc');
             $tmpFile = fopen($file['tmp_name'], 'wb');
             if (false === $tmpFile) {
                 // Cannot open the temporary file for writing; this is an error
                 $file['error'] = UPLOAD_ERR_CANT_WRITE;
                 continue;
             }
         }
         // Off-by-one operation. Last line must be trimmed, so we'll write
         // the lines one iteration behind.
         if (null === $lastline) {
             $lastline = $line;
             continue;
         }
         if (false === fwrite($tmpFile, $lastline)) {
             $file['error'] = UPLOAD_ERR_CANT_WRITE;
             fclose($tmpFile);
             continue;
         }
         $lastline = $line;
     }
     fclose($stream);
     if ($files->count()) {
         $this->request->setFiles($files);
     }
     $data->fromString(rtrim($buffer, '&'));
     return $data->toArray();
 }