PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Gdata/MediaMimeStream.php

https://bitbucket.org/luizbrandaoj/mini-blog
PHP | 190 lines | 70 code | 23 blank | 97 comment | 5 complexity | b5eaa9a28332e01e5f3fd7a743c35880 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Gdata
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: MediaMimeStream.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Gdata_MimeFile
  24. */
  25. require_once 'Zend/Gdata/MimeFile.php';
  26. /**
  27. * @see Zend_Gdata_MimeBodyString
  28. */
  29. require_once 'Zend/Gdata/MimeBodyString.php';
  30. /**
  31. * A streaming Media MIME class that allows for buffered read operations.
  32. *
  33. * @category Zend
  34. * @package Zend_Gdata
  35. * @subpackage Gdata
  36. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Gdata_MediaMimeStream
  40. {
  41. /**
  42. * A valid MIME boundary.
  43. *
  44. * @var string
  45. */
  46. protected $_boundaryString = null;
  47. /**
  48. * A handle to the file that is part of the message.
  49. *
  50. * @var resource
  51. */
  52. protected $_fileHandle = null;
  53. /**
  54. * The current part being read from.
  55. * @var integer
  56. */
  57. protected $_currentPart = 0;
  58. /**
  59. * The size of the MIME message.
  60. * @var integer
  61. */
  62. protected $_totalSize = 0;
  63. /**
  64. * An array of all the parts to be sent. Array members are either a
  65. * MimeFile or a MimeBodyString object.
  66. * @var array
  67. */
  68. protected $_parts = null;
  69. /**
  70. * Create a new MimeMediaStream object.
  71. *
  72. * @param string $xmlString The string corresponding to the XML section
  73. * of the message, typically an atom entry or feed.
  74. * @param string $filePath The path to the file that constitutes the binary
  75. * part of the message.
  76. * @param string $fileContentType The valid internet media type of the file.
  77. * @throws Zend_Gdata_App_IOException If the file cannot be read or does
  78. * not exist. Also if mbstring.func_overload has been set > 1.
  79. */
  80. public function __construct($xmlString = null, $filePath = null,
  81. $fileContentType = null)
  82. {
  83. if (!file_exists($filePath) || !is_readable($filePath)) {
  84. require_once 'Zend/Gdata/App/IOException.php';
  85. throw new Zend_Gdata_App_IOException('File to be uploaded at ' .
  86. $filePath . ' does not exist or is not readable.');
  87. }
  88. $this->_fileHandle = fopen($filePath, 'rb', TRUE);
  89. $this->_boundaryString = '=_' . md5(microtime(1) . rand(1,20));
  90. $entry = $this->wrapEntry($xmlString, $fileContentType);
  91. $closingBoundary = new Zend_Gdata_MimeBodyString("\r\n--{$this->_boundaryString}--\r\n");
  92. $file = new Zend_Gdata_MimeFile($this->_fileHandle);
  93. $this->_parts = array($entry, $file, $closingBoundary);
  94. $fileSize = filesize($filePath);
  95. $this->_totalSize = $entry->getSize() + $fileSize
  96. + $closingBoundary->getSize();
  97. }
  98. /**
  99. * Sandwiches the entry body into a MIME message
  100. *
  101. * @return void
  102. */
  103. private function wrapEntry($entry, $fileMimeType)
  104. {
  105. $wrappedEntry = "--{$this->_boundaryString}\r\n";
  106. $wrappedEntry .= "Content-Type: application/atom+xml\r\n\r\n";
  107. $wrappedEntry .= $entry;
  108. $wrappedEntry .= "\r\n--{$this->_boundaryString}\r\n";
  109. $wrappedEntry .= "Content-Type: $fileMimeType\r\n\r\n";
  110. return new Zend_Gdata_MimeBodyString($wrappedEntry);
  111. }
  112. /**
  113. * Read a specific chunk of the the MIME multipart message.
  114. *
  115. * @param integer $bufferSize The size of the chunk that is to be read,
  116. * must be lower than MAX_BUFFER_SIZE.
  117. * @return string A corresponding piece of the message. This could be
  118. * binary or regular text.
  119. */
  120. public function read($bytesRequested)
  121. {
  122. if($this->_currentPart >= count($this->_parts)) {
  123. return FALSE;
  124. }
  125. $activePart = $this->_parts[$this->_currentPart];
  126. $buffer = $activePart->read($bytesRequested);
  127. while(strlen($buffer) < $bytesRequested) {
  128. $this->_currentPart += 1;
  129. $nextBuffer = $this->read($bytesRequested - strlen($buffer));
  130. if($nextBuffer === FALSE) {
  131. break;
  132. }
  133. $buffer .= $nextBuffer;
  134. }
  135. return $buffer;
  136. }
  137. /**
  138. * Return the total size of the mime message.
  139. *
  140. * @return integer Total size of the message to be sent.
  141. */
  142. public function getTotalSize()
  143. {
  144. return $this->_totalSize;
  145. }
  146. /**
  147. * Close the internal file that we are streaming to the socket.
  148. *
  149. * @return void
  150. */
  151. public function closeFileHandle()
  152. {
  153. if ($this->_fileHandle !== null) {
  154. fclose($this->_fileHandle);
  155. }
  156. }
  157. /**
  158. * Return a Content-type header that includes the current boundary string.
  159. *
  160. * @return string A valid HTTP Content-Type header.
  161. */
  162. public function getContentType()
  163. {
  164. return 'multipart/related;boundary="' .
  165. $this->_boundaryString . '"' . "\r\n";
  166. }
  167. }