PageRenderTime 28ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Gdata/MediaMimeStream.php

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