• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List

E:/E/GEAMP/www/openbiz/openbiz/others/Zend/Mail/Part.php

00001 <?php
00026 require_once 'Zend/Mime/Decode.php';
00027 
00031 require_once 'Zend/Mail/Part/Interface.php';
00032 
00033 
00040 class Zend_Mail_Part implements RecursiveIterator, Zend_Mail_Part_Interface
00041 {
00046     protected $_headers;
00047 
00052     protected $_content;
00053 
00058     protected $_topLines = '';
00059 
00064     protected $_parts = array();
00065 
00070     protected $_countParts;
00071 
00076     protected $_iterationPos = 1;
00077 
00082     protected $_mail;
00083 
00088     protected $_messageNum = 0;
00089 
00104     public function __construct(array $params)
00105     {
00106         if (isset($params['handler'])) {
00107             if (!$params['handler'] instanceof Zend_Mail_Storage_Abstract) {
00111                 require_once 'Zend/Mail/Exception.php';
00112                 throw new Zend_Mail_Exception('handler is not a valid mail handler');
00113             }
00114             if (!isset($params['id'])) {
00118                 require_once 'Zend/Mail/Exception.php';
00119                 throw new Zend_Mail_Exception('need a message id with a handler');
00120             }
00121 
00122             $this->_mail       = $params['handler'];
00123             $this->_messageNum = $params['id'];
00124         }
00125 
00126         if (isset($params['raw'])) {
00127             Zend_Mime_Decode::splitMessage($params['raw'], $this->_headers, $this->_content);
00128         } else if (isset($params['headers'])) {
00129             if (is_array($params['headers'])) {
00130                 $this->_headers = $params['headers'];
00131             } else {
00132                 if (!empty($params['noToplines'])) {
00133                     Zend_Mime_Decode::splitMessage($params['headers'], $this->_headers, $null);
00134                 } else {
00135                     Zend_Mime_Decode::splitMessage($params['headers'], $this->_headers, $this->_topLines);
00136                 }
00137             }
00138             if (isset($params['content'])) {
00139                 $this->_content = $params['content'];
00140             }
00141         }
00142     }
00143 
00149     public function isMultipart()
00150     {
00151         try {
00152             return stripos($this->contentType, 'multipart/') === 0;
00153         } catch(Zend_Mail_Exception $e) {
00154             return false;
00155         }
00156     }
00157 
00158 
00167     public function getContent()
00168     {
00169         if ($this->_content !== null) {
00170             return $this->_content;
00171         }
00172 
00173         if ($this->_mail) {
00174             return $this->_mail->getRawContent($this->_messageNum);
00175         } else {
00179             require_once 'Zend/Mail/Exception.php';
00180             throw new Zend_Mail_Exception('no content');
00181         }
00182     }
00183 
00191     public function getSize() {
00192         return strlen($this->getContent());
00193     }
00194 
00195 
00202     protected function _cacheContent()
00203     {
00204         // caching content if we can't fetch parts
00205         if ($this->_content === null && $this->_mail) {
00206             $this->_content = $this->_mail->getRawContent($this->_messageNum);
00207         }
00208 
00209         if (!$this->isMultipart()) {
00210             return;
00211         }
00212 
00213         // split content in parts
00214         $boundary = $this->getHeaderField('content-type', 'boundary');
00215         if (!$boundary) {
00219             require_once 'Zend/Mail/Exception.php';
00220             throw new Zend_Mail_Exception('no boundary found in content type to split message');
00221         }
00222         $parts = Zend_Mime_Decode::splitMessageStruct($this->_content, $boundary);
00223         if ($parts === null) {
00224             return;
00225         }
00226         $counter = 1;
00227         foreach ($parts as $part) {
00228             $this->_parts[$counter++] = new self(array('headers' => $part['header'], 'content' => $part['body']));
00229         }
00230     }
00231 
00239     public function getPart($num)
00240     {
00241         if (isset($this->_parts[$num])) {
00242             return $this->_parts[$num];
00243         }
00244 
00245         if (!$this->_mail && $this->_content === null) {
00249             require_once 'Zend/Mail/Exception.php';
00250             throw new Zend_Mail_Exception('part not found');
00251         }
00252 
00253         if ($this->_mail && $this->_mail->hasFetchPart) {
00254             // TODO: fetch part
00255             // return
00256         }
00257 
00258         $this->_cacheContent();
00259 
00260         if (!isset($this->_parts[$num])) {
00264             require_once 'Zend/Mail/Exception.php';
00265             throw new Zend_Mail_Exception('part not found');
00266         }
00267 
00268         return $this->_parts[$num];
00269     }
00270 
00276     public function countParts()
00277     {
00278         if ($this->_countParts) {
00279             return $this->_countParts;
00280         }
00281 
00282         $this->_countParts = count($this->_parts);
00283         if ($this->_countParts) {
00284             return $this->_countParts;
00285         }
00286 
00287         if ($this->_mail && $this->_mail->hasFetchPart) {
00288             // TODO: fetch part
00289             // return
00290         }
00291 
00292         $this->_cacheContent();
00293 
00294         $this->_countParts = count($this->_parts);
00295         return $this->_countParts;
00296     }
00297 
00298 
00307     public function getHeaders()
00308     {
00309         if ($this->_headers === null) {
00310             if (!$this->_mail) {
00311                 $this->_headers = array();
00312             } else {
00313                 $part = $this->_mail->getRawHeader($this->_messageNum);
00314                 Zend_Mime_Decode::splitMessage($part, $this->_headers, $null);
00315             }
00316         }
00317 
00318         return $this->_headers;
00319     }
00320 
00332     public function getHeader($name, $format = null)
00333     {
00334         if ($this->_headers === null) {
00335             $this->getHeaders();
00336         }
00337 
00338         $lowerName = strtolower($name);
00339 
00340         if ($this->headerExists($name) == false) {
00341             $lowerName = strtolower(preg_replace('%([a-z])([A-Z])%', '\1-\2', $name));
00342             if($this->headerExists($lowerName) == false) {
00346                 require_once 'Zend/Mail/Exception.php';
00347                 throw new Zend_Mail_Exception("no Header with Name $name or $lowerName found");
00348             }
00349         }
00350         $name = $lowerName;
00351 
00352         $header = $this->_headers[$name];
00353 
00354         switch ($format) {
00355             case 'string':
00356                 if (is_array($header)) {
00357                     $header = implode(Zend_Mime::LINEEND, $header);
00358                 }
00359                 break;
00360             case 'array':
00361                 $header = (array)$header;
00362             default:
00363                 // do nothing
00364         }
00365 
00366         return $header;
00367     }
00368 
00375     public function headerExists($name)
00376     {
00377         $name = strtolower($name);
00378         if(isset($this->_headers[$name])) {
00379             return true;
00380         } else {
00381             return false;
00382         }
00383     }
00384 
00400     public function getHeaderField($name, $wantedPart = 0, $firstName = 0) {
00401         return Zend_Mime_Decode::splitHeaderField(current($this->getHeader($name, 'array')), $wantedPart, $firstName);
00402     }
00403 
00404 
00416     public function __get($name)
00417     {
00418         return $this->getHeader($name, 'string');
00419     }
00420 
00431     public function __isset($name)
00432     {
00433         return $this->headerExists($name);
00434     }
00435 
00441     public function __toString()
00442     {
00443         return $this->getContent();
00444     }
00445 
00451     public function hasChildren()
00452     {
00453         $current = $this->current();
00454         return $current && $current instanceof Zend_Mail_Part && $current->isMultipart();
00455     }
00456 
00462     public function getChildren()
00463     {
00464         return $this->current();
00465     }
00466 
00472     public function valid()
00473     {
00474         if ($this->_countParts === null) {
00475             $this->countParts();
00476         }
00477         return $this->_iterationPos && $this->_iterationPos <= $this->_countParts;
00478     }
00479 
00485     public function next()
00486     {
00487         ++$this->_iterationPos;
00488     }
00489 
00495     public function key()
00496     {
00497         return $this->_iterationPos;
00498     }
00499 
00505     public function current()
00506     {
00507         return $this->getPart($this->_iterationPos);
00508     }
00509 
00515     public function rewind()
00516     {
00517         $this->countParts();
00518         $this->_iterationPos = 1;
00519     }
00520 }

Generated on Thu Apr 19 2012 17:01:18 for openbiz by  doxygen 1.7.2