PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/zend/classes/Zend/Mail/Part/File.php

https://github.com/dari88/kohanaPress-Sample_Code_for_kohana_3.3
PHP | 198 lines | 92 code | 19 blank | 87 comment | 25 complexity | 5c062b7d1f98841e6cfdeecbd6d8fadb MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  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_Mail
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: File.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Mime_Decode
  23. */
  24. // require_once 'Zend/Mime/Decode.php';
  25. /**
  26. * @see Zend_Mail_Part
  27. */
  28. // require_once 'Zend/Mail/Part.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Mail
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Mail_Part_File extends Zend_Mail_Part
  36. {
  37. protected $_contentPos = array();
  38. protected $_partPos = array();
  39. protected $_fh;
  40. /**
  41. * Public constructor
  42. *
  43. * This handler supports the following params:
  44. * - file filename or open file handler with message content (required)
  45. * - startPos start position of message or part in file (default: current position)
  46. * - endPos end position of message or part in file (default: end of file)
  47. *
  48. * @param array $params full message with or without headers
  49. * @throws Zend_Mail_Exception
  50. */
  51. public function __construct(array $params)
  52. {
  53. if (empty($params['file'])) {
  54. /**
  55. * @see Zend_Mail_Exception
  56. */
  57. // require_once 'Zend/Mail/Exception.php';
  58. throw new Exception('no file given in params');
  59. }
  60. if (!is_resource($params['file'])) {
  61. $this->_fh = fopen($params['file'], 'r');
  62. } else {
  63. $this->_fh = $params['file'];
  64. }
  65. if (!$this->_fh) {
  66. /**
  67. * @see Zend_Mail_Exception
  68. */
  69. // require_once 'Zend/Mail/Exception.php';
  70. throw new Exception('could not open file');
  71. }
  72. if (isset($params['startPos'])) {
  73. fseek($this->_fh, $params['startPos']);
  74. }
  75. $header = '';
  76. $endPos = isset($params['endPos']) ? $params['endPos'] : null;
  77. while (($endPos === null || ftell($this->_fh) < $endPos) && trim($line = fgets($this->_fh))) {
  78. $header .= $line;
  79. }
  80. Zend_Mime_Decode::splitMessage($header, $this->_headers, $null);
  81. $this->_contentPos[0] = ftell($this->_fh);
  82. if ($endPos !== null) {
  83. $this->_contentPos[1] = $endPos;
  84. } else {
  85. fseek($this->_fh, 0, SEEK_END);
  86. $this->_contentPos[1] = ftell($this->_fh);
  87. }
  88. if (!$this->isMultipart()) {
  89. return;
  90. }
  91. $boundary = $this->getHeaderField('content-type', 'boundary');
  92. if (!$boundary) {
  93. /**
  94. * @see Zend_Mail_Exception
  95. */
  96. // require_once 'Zend/Mail/Exception.php';
  97. throw new Exception('no boundary found in content type to split message');
  98. }
  99. $part = array();
  100. $pos = $this->_contentPos[0];
  101. fseek($this->_fh, $pos);
  102. while (!feof($this->_fh) && ($endPos === null || $pos < $endPos)) {
  103. $line = fgets($this->_fh);
  104. if ($line === false) {
  105. if (feof($this->_fh)) {
  106. break;
  107. }
  108. /**
  109. * @see Zend_Mail_Exception
  110. */
  111. // require_once 'Zend/Mail/Exception.php';
  112. throw new Exception('error reading file');
  113. }
  114. $lastPos = $pos;
  115. $pos = ftell($this->_fh);
  116. $line = trim($line);
  117. if ($line == '--' . $boundary) {
  118. if ($part) {
  119. // not first part
  120. $part[1] = $lastPos;
  121. $this->_partPos[] = $part;
  122. }
  123. $part = array($pos);
  124. } else if ($line == '--' . $boundary . '--') {
  125. $part[1] = $lastPos;
  126. $this->_partPos[] = $part;
  127. break;
  128. }
  129. }
  130. $this->_countParts = count($this->_partPos);
  131. }
  132. /**
  133. * Body of part
  134. *
  135. * If part is multipart the raw content of this part with all sub parts is returned
  136. *
  137. * @return string body
  138. * @throws Zend_Mail_Exception
  139. */
  140. public function getContent($stream = null)
  141. {
  142. fseek($this->_fh, $this->_contentPos[0]);
  143. if ($stream !== null) {
  144. return stream_copy_to_stream($this->_fh, $stream, $this->_contentPos[1] - $this->_contentPos[0]);
  145. }
  146. $length = $this->_contentPos[1] - $this->_contentPos[0];
  147. return $length < 1 ? '' : fread($this->_fh, $length);
  148. }
  149. /**
  150. * Return size of part
  151. *
  152. * Quite simple implemented currently (not decoding). Handle with care.
  153. *
  154. * @return int size
  155. */
  156. public function getSize() {
  157. return $this->_contentPos[1] - $this->_contentPos[0];
  158. }
  159. /**
  160. * Get part of multipart message
  161. *
  162. * @param int $num number of part starting with 1 for first part
  163. * @return Zend_Mail_Part wanted part
  164. * @throws Zend_Mail_Exception
  165. */
  166. public function getPart($num)
  167. {
  168. --$num;
  169. if (!isset($this->_partPos[$num])) {
  170. /**
  171. * @see Zend_Mail_Exception
  172. */
  173. // require_once 'Zend/Mail/Exception.php';
  174. throw new Exception('part not found');
  175. }
  176. return new self(array('file' => $this->_fh, 'startPos' => $this->_partPos[$num][0],
  177. 'endPos' => $this->_partPos[$num][1]));
  178. }
  179. }