Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Gdata/MediaMimeStream.php
Go to the documentation of this file.
00001 <?php
00002 
00027 require_once 'Zend/Gdata/MimeFile.php';
00028 
00032 require_once 'Zend/Gdata/MimeBodyString.php';
00033 
00034 
00044 class Zend_Gdata_MediaMimeStream
00045 {
00046 
00052     protected $_boundaryString = null;
00053 
00059     protected $_fileHandle = null;
00060 
00065     protected $_currentPart = 0;
00066 
00071     protected $_totalSize = 0;
00072 
00078     protected $_parts = null;
00079 
00091     public function __construct($xmlString = null, $filePath = null,
00092         $fileContentType = null)
00093     {
00094         if (!file_exists($filePath) || !is_readable($filePath)) {
00095             require_once 'Zend/Gdata/App/IOException.php';
00096             throw new Zend_Gdata_App_IOException('File to be uploaded at ' .
00097                 $filePath . ' does not exist or is not readable.');
00098         }
00099 
00100         $this->_fileHandle = fopen($filePath, 'rb', TRUE);
00101         $this->_boundaryString = '=_' . md5(microtime(1) . rand(1,20));
00102         $entry = $this->wrapEntry($xmlString, $fileContentType);
00103         $closingBoundary = new Zend_Gdata_MimeBodyString("\r\n--{$this->_boundaryString}--\r\n");
00104         $file = new Zend_Gdata_MimeFile($this->_fileHandle);
00105         $this->_parts = array($entry, $file, $closingBoundary);
00106 
00107         $fileSize = filesize($filePath);
00108         $this->_totalSize = $entry->getSize() + $fileSize
00109           + $closingBoundary->getSize();
00110 
00111     }
00112 
00118     private function wrapEntry($entry, $fileMimeType)
00119     {
00120         $wrappedEntry = "--{$this->_boundaryString}\r\n";
00121         $wrappedEntry .= "Content-Type: application/atom+xml\r\n\r\n";
00122         $wrappedEntry .= $entry;
00123         $wrappedEntry .= "\r\n--{$this->_boundaryString}\r\n";
00124         $wrappedEntry .= "Content-Type: $fileMimeType\r\n\r\n";
00125         return new Zend_Gdata_MimeBodyString($wrappedEntry);
00126     }
00127 
00136     public function read($bytesRequested)
00137     {
00138         if($this->_currentPart >= count($this->_parts)) {
00139           return FALSE;
00140         }
00141 
00142         $activePart = $this->_parts[$this->_currentPart];
00143         $buffer = $activePart->read($bytesRequested);
00144 
00145         while(strlen($buffer) < $bytesRequested) {
00146           $this->_currentPart += 1;
00147           $nextBuffer = $this->read($bytesRequested - strlen($buffer));
00148           if($nextBuffer === FALSE) {
00149             break;
00150           }
00151           $buffer .= $nextBuffer;
00152         }
00153 
00154         return $buffer;
00155     }
00156 
00162     public function getTotalSize()
00163     {
00164         return $this->_totalSize;
00165     }
00166 
00172     public function closeFileHandle()
00173     {
00174         if ($this->_fileHandle !== null) {
00175             fclose($this->_fileHandle);
00176         }
00177     }
00178 
00184     public function getContentType()
00185     {
00186         return 'multipart/related;boundary="' .
00187             $this->_boundaryString . '"' . "\r\n";
00188     }
00189 
00190 }
 All Data Structures Namespaces Files Functions Variables Enumerations