PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPillow-0.6-beta/classes/tool/multipart_writer.php

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