00001 <?php
00026 require_once 'Zend/Mime/Decode.php';
00027
00031 require_once 'Zend/Mail/Part.php';
00032
00033
00040 class Zend_Mail_Part_File extends Zend_Mail_Part
00041 {
00042 protected $_contentPos = array();
00043 protected $_partPos = array();
00044 protected $_fh;
00045
00057 public function __construct(array $params)
00058 {
00059 if (empty($params['file'])) {
00063 require_once 'Zend/Mail/Exception.php';
00064 throw new Zend_Mail_Exception('no file given in params');
00065 }
00066
00067 if (!is_resource($params['file'])) {
00068 $this->_fh = fopen($params['file'], 'r');
00069 } else {
00070 $this->_fh = $params['file'];
00071 }
00072 if (!$this->_fh) {
00076 require_once 'Zend/Mail/Exception.php';
00077 throw new Zend_Mail_Exception('could not open file');
00078 }
00079 if (isset($params['startPos'])) {
00080 fseek($this->_fh, $params['startPos']);
00081 }
00082 $header = '';
00083 $endPos = isset($params['endPos']) ? $params['endPos'] : null;
00084 while (($endPos === null || ftell($this->_fh) < $endPos) && trim($line = fgets($this->_fh))) {
00085 $header .= $line;
00086 }
00087
00088 Zend_Mime_Decode::splitMessage($header, $this->_headers, $null);
00089
00090 $this->_contentPos[0] = ftell($this->_fh);
00091 if ($endPos !== null) {
00092 $this->_contentPos[1] = $endPos;
00093 } else {
00094 fseek($this->_fh, 0, SEEK_END);
00095 $this->_contentPos[1] = ftell($this->_fh);
00096 }
00097 if (!$this->isMultipart()) {
00098 return;
00099 }
00100
00101 $boundary = $this->getHeaderField('content-type', 'boundary');
00102 if (!$boundary) {
00106 require_once 'Zend/Mail/Exception.php';
00107 throw new Zend_Mail_Exception('no boundary found in content type to split message');
00108 }
00109
00110 $part = array();
00111 $pos = $this->_contentPos[0];
00112 fseek($this->_fh, $pos);
00113 while (!feof($this->_fh) && ($endPos === null || $pos < $endPos)) {
00114 $line = fgets($this->_fh);
00115 if ($line === false) {
00116 if (feof($this->_fh)) {
00117 break;
00118 }
00122 require_once 'Zend/Mail/Exception.php';
00123 throw new Zend_Mail_Exception('error reading file');
00124 }
00125
00126 $lastPos = $pos;
00127 $pos = ftell($this->_fh);
00128 $line = trim($line);
00129
00130 if ($line == '--' . $boundary) {
00131 if ($part) {
00132
00133 $part[1] = $lastPos;
00134 $this->_partPos[] = $part;
00135 }
00136 $part = array($pos);
00137 } else if ($line == '--' . $boundary . '--') {
00138 $part[1] = $lastPos;
00139 $this->_partPos[] = $part;
00140 break;
00141 }
00142 }
00143 $this->_countParts = count($this->_partPos);
00144
00145 }
00146
00147
00156 public function getContent($stream = null)
00157 {
00158 fseek($this->_fh, $this->_contentPos[0]);
00159 if ($stream !== null) {
00160 return stream_copy_to_stream($this->_fh, $stream, $this->_contentPos[1] - $this->_contentPos[0]);
00161 }
00162 $length = $this->_contentPos[1] - $this->_contentPos[0];
00163 return $length < 1 ? '' : fread($this->_fh, $length);
00164 }
00165
00173 public function getSize() {
00174 return $this->_contentPos[1] - $this->_contentPos[0];
00175 }
00176
00184 public function getPart($num)
00185 {
00186 --$num;
00187 if (!isset($this->_partPos[$num])) {
00191 require_once 'Zend/Mail/Exception.php';
00192 throw new Zend_Mail_Exception('part not found');
00193 }
00194
00195 return new self(array('file' => $this->_fh, 'startPos' => $this->_partPos[$num][0],
00196 'endPos' => $this->_partPos[$num][1]));
00197 }
00198 }