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