public function testParseStackedMultipartDocument() { $parser = new phpillowToolMultipartParser(fopen('string://Content-Type: multipart/mixed; boundary=foo --foo Content-Type: multipart/mixed; boundary="bar" --bar Content-Type: text/text Hello world! --bar-- --foo--', 'r')); $this->assertSame(array('Content-Type' => 'multipart/mixed; boundary="bar"', 'body' => array(array('Content-Type' => 'text/text', 'body' => 'Hello world!'))), $parser->getDocument()); }
/** * Parse a single document * * Parse a single document, and return an array with all headers and the * document body. If the parsed document is of the type "multipart/mixed", * an array with all parts is returned as the type. * * @param string $string * @return array */ protected function parseDocument($string) { $document = array(); // Read document headers while (preg_match('(\\A(?P<header>[A-Za-z0-9-]+):\\s+(?P<value>.*)$)Sm', $string, $match)) { $document[$match['header']] = trim($match['value']); $string = substr($string, strlen($match[0]) + 1); } if (strpos($document['Content-Type'], 'multipart/mixed') === 0) { // Rebuild full document $body = fopen('string://', 'w'); foreach ($document as $key => $value) { fwrite($body, "{$key}: {$value}\r\n"); } fwrite($body, $string); fseek($body, 0); $document['body'] = array(); $parser = new phpillowToolMultipartParser($body); while (($part = $parser->getDocument()) !== false) { $document['body'][] = $part; } } else { $document['body'] = trim($string); } return $document; }
/** * Execute load command * * Returns a proper status code indicating successful execution of the * command. * * @return int */ public function load() { if ($this->printVersion()) { return 0; } if (!$this->parseConnectionInformation()) { return 1; } // Open input stream to read contents from $stream = isset($this->options['input']) ? fopen($this->options['input'], 'r') : STDIN; $multipartParser = new phpillowToolMultipartParser($stream); $db = new phpillowCustomConnection($this->connectionInfo['host'], $this->connectionInfo['port'], $this->connectionInfo['user'], $this->connectionInfo['pass']); // Create database if it does not exist yet try { $db->get($this->connectionInfo['path']); } catch (phpillowResponseNotFoundErrorException $e) { $db->put($this->connectionInfo['path']); } // Import the documents while (($document = $multipartParser->getDocument()) !== false) { try { $this->out("Loading document " . $document['Content-ID'] . "\n"); $path = $this->connectionInfo['path'] . '/' . $document['Content-ID']; $db->put($path, $this->getDocumentBody($document)); } catch (phpillowException $e) { fwrite($this->stderr, $document['Content-ID'] . ': ' . $e->getMessage() . "\n"); if (!isset($this->options['ignore-errors'])) { return 1; } } } return 0; }