Arbit - project tracking

PHPillow - PHP CouchDB connector

Browse source code

File: / src/ classes/ tool/ multipart_writer.php

Type
text/plain text/plain
Last Author
kore
Version
184
Line Rev. Author Source
1 122 kore <?php
2 kore /**
3 kore * phpillow multipart parser
4 kore *
5 kore * This file is part of phpillow.
6 kore *
7 kore * phpillow is free software; you can redistribute it and/or modify it under
8 kore * the terms of the GNU Lesser General Public License as published by the Free
9 kore * Software Foundation; version 3 of the License.
10 kore *
11 kore * phpillow is distributed in the hope that it will be useful, but WITHOUT ANY
12 kore * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 kore * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 kore * more details.
15 kore *
16 kore * You should have received a copy of the GNU Lesser General Public License
17 kore * along with phpillow; if not, write to the Free Software Foundation, Inc., 51
18 kore * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 kore *
20 kore * @package Core
21 kore * @version $Revision: 114 $
22 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
23 kore */
24 kore
25 kore /**
26 kore * Writes a CouchDB dump into a multipart/mixed MIME file
27 kore *
28 kore * @package Core
29 kore * @version $Revision: 114 $
30 kore * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
31 kore */
32 kore class phpillowToolMultipartWriter
33 kore {
34 kore /**
35 kore * Stream to read from
36 kore *
37 kore * @var resource
38 kore */
39 kore protected $stream;
40 kore
41 kore /**
42 kore * Currently used boundary
43 kore *
44 kore * @var string
45 kore */
46 kore protected $boundary;
47 kore
48 kore /**
49 kore * Already used boundaries
50 kore *
51 kore * @param array
52 kore */
53 kore protected static $boundaries = array();
54 kore
55 kore /**
56 kore * Construct parser from input stream
57 kore *
58 kore * @param resource $stream
59 kore * @return void
60 kore */
61 kore public function __construct( $stream )
62 kore {
63 kore $this->stream = $stream;
64 kore
65 kore // Write document header
66 kore $this->boundary = $this->getBoundary();
67 kore fwrite( $this->stream, "Content-Type: multipart/mixed; boundary=\"" . $this->boundary . "\"\r\n\r\n" );
68 kore }
69 kore
70 kore /**
71 kore * Write stream end
72 kore *
73 kore * @return void
74 kore */
75 kore public function __destruct()
76 kore {
77 kore fwrite( $this->stream, "--" . $this->boundary . "--\r\n" );
78 kore }
79 kore
80 kore /**
81 kore * Get unique boudary
82 kore *
83 kore * Get a unique boundary string, which is not yet used by any wrapping
84 kore * document and most probably not occurs in any of the embedded documents.
85 kore * We cannot be entirely sure about that, but do not want waiting to define
86 kore * the boundary until we loaded all documents into memory.
87 kore *
88 kore * @return string
89 kore */
90 kore protected function getBoundary()
91 kore {
92 kore do {
93 kore $boundary = '==' . md5( microtime() ) . '==';
94 kore } while ( in_array( $boundary, self::$boundaries ) );
95 kore
96 kore return self::$boundaries[] = $boundary;
97 kore }
98 kore
99 kore /**
100 kore * Write a single document to the MIME file
101 kore *
102 kore * @param array $document
103 kore * @return void
104 kore */
105 kore protected function writeSimpleDocument( array $document )
106 kore {
107 kore $body = json_encode( $document );
108 kore
109 kore fwrite( $this->stream, "Content-ID: " . $document['_id'] . "\r\n" );
110 kore fwrite( $this->stream, "Content-Length: " . strlen( $body ) . "\r\n" );
111 kore fwrite( $this->stream, "Content-Type: application/json\r\n" );
112 kore fwrite( $this->stream, "\r\n" );
113 kore fwrite( $this->stream, "$body\r\n" );
114 kore }
115 kore
116 kore /**
117 kore * Write a single document to the MIME file
118 kore *
119 kore * @param array $document
120 kore * @return void
121 kore */
122 kore protected function writeMultipartDocument( array $document )
123 kore {
124 kore $body = json_encode( $document );
125 kore
126 kore $boundary = $this->getBoundary();
127 kore fwrite( $this->stream, "Content-ID: " . $document['_id'] . "\r\n" );
128 kore fwrite( $this->stream, "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\"\r\n\r\n" );
129 kore
130 kore $attachments = $document['_attachments'];
131 kore unset( $document['_attachments'] );
132 kore
133 kore // Write document first
134 kore fwrite( $this->stream, "--" . $boundary . "\r\n" );
135 kore $body = json_encode( $document );
136 kore
137 kore fwrite( $this->stream, "Content-Length: " . strlen( $body ) . "\r\n" );
138 kore fwrite( $this->stream, "Content-Type: application/json\r\n" );
139 kore fwrite( $this->stream, "\r\n" );
140 kore fwrite( $this->stream, "$body\r\n" );
141 kore
142 kore // Write all attachments
143 kore foreach ( $attachments as $name => $attachment )
144 kore {
145 kore fwrite( $this->stream, "--" . $boundary . "\r\n" );
146 184 kore $body = $attachment['data'];
147 122 kore
148 kore fwrite( $this->stream, "Content-ID: " . $name . "\r\n" );
149 kore fwrite( $this->stream, "Content-Length: " . strlen( $body ) . "\r\n" );
150 kore fwrite( $this->stream, "Content-Type: " . $attachment['content_type'] . "\r\n" );
151 kore fwrite( $this->stream, "\r\n" );
152 kore fwrite( $this->stream, "$body\r\n" );
153 kore }
154 kore
155 kore // End of multipart/mixed data
156 kore fwrite( $this->stream, "--" . $boundary . "--\r\n" );
157 kore }
158 kore
159 kore /**
160 kore * Write document to stream
161 kore *
162 kore * Write a single document to the stream. Can create multipart messages, if
163 kore * the document contains attachments.
164 kore *
165 150 kore * @param array $document
166 122 kore * @return void
167 kore */
168 150 kore public function writeDocument( array $document )
169 122 kore {
170 kore fwrite( $this->stream, "--" . $this->boundary . "\r\n" );
171 kore if ( isset( $document['_attachments'] ) )
172 kore {
173 kore $this->writeMultipartDocument( $document );
174 kore }
175 kore else
176 kore {
177 kore $this->writeSimpleDocument( $document );
178 kore }
179 kore }
180 kore }
181 kore