PageRenderTime 29ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/swift/lib/classes/Swift/Mime/Attachment.php

http://zoop.googlecode.com/
PHP | 143 lines | 67 code | 15 blank | 61 comment | 3 complexity | 7e4b16869435da9347b5e680811f2add MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. //@require 'Swift/Mime/SimpleMimeEntity.php';
  10. //@require 'Swift/Mime/ContentEncoder.php';
  11. //@require 'Swift/Mime/HeaderSet.php';
  12. //@require 'Swift/FileStream.php';
  13. //@require 'Swift/KeyCache.php';
  14. /**
  15. * An attachment, in a multipart message.
  16. * @package Swift
  17. * @subpackage Mime
  18. * @author Chris Corbyn
  19. */
  20. class Swift_Mime_Attachment extends Swift_Mime_SimpleMimeEntity
  21. {
  22. /** Recognized MIME types */
  23. private $_mimeTypes = array();
  24. /**
  25. * Create a new Attachment with $headers, $encoder and $cache.
  26. * @param Swift_Mime_HeaderSet $headers
  27. * @param Swift_Mime_ContentEncoder $encoder
  28. * @param Swift_KeyCache $cache
  29. * @param array $mimeTypes optional
  30. */
  31. public function __construct(Swift_Mime_HeaderSet $headers,
  32. Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache,
  33. $mimeTypes = array())
  34. {
  35. parent::__construct($headers, $encoder, $cache);
  36. $this->setDisposition('attachment');
  37. $this->setContentType('application/octet-stream');
  38. $this->_mimeTypes = $mimeTypes;
  39. }
  40. /**
  41. * Get the nesting level used for this attachment.
  42. * Always returns {@link LEVEL_MIXED}.
  43. * @return int
  44. */
  45. public function getNestingLevel()
  46. {
  47. return self::LEVEL_MIXED;
  48. }
  49. /**
  50. * Get the Content-Disposition of this attachment.
  51. * By default attachments have a disposition of "attachment".
  52. * @return string
  53. */
  54. public function getDisposition()
  55. {
  56. return $this->_getHeaderFieldModel('Content-Disposition');
  57. }
  58. /**
  59. * Set the Content-Disposition of this attachment.
  60. * @param string $disposition
  61. */
  62. public function setDisposition($disposition)
  63. {
  64. if (!$this->_setHeaderFieldModel('Content-Disposition', $disposition))
  65. {
  66. $this->getHeaders()->addParameterizedHeader(
  67. 'Content-Disposition', $disposition
  68. );
  69. }
  70. return $this;
  71. }
  72. /**
  73. * Get the filename of this attachment when downloaded.
  74. * @return string
  75. */
  76. public function getFilename()
  77. {
  78. return $this->_getHeaderParameter('Content-Disposition', 'filename');
  79. }
  80. /**
  81. * Set the filename of this attachment.
  82. * @param string $filename
  83. */
  84. public function setFilename($filename)
  85. {
  86. $this->_setHeaderParameter('Content-Disposition', 'filename', $filename);
  87. $this->_setHeaderParameter('Content-Type', 'name', $filename);
  88. return $this;
  89. }
  90. /**
  91. * Get the file size of this attachment.
  92. * @return int
  93. */
  94. public function getSize()
  95. {
  96. return $this->_getHeaderParameter('Content-Disposition', 'size');
  97. }
  98. /**
  99. * Set the file size of this attachment.
  100. * @param int $size
  101. */
  102. public function setSize($size)
  103. {
  104. $this->_setHeaderParameter('Content-Disposition', 'size', $size);
  105. return $this;
  106. }
  107. /**
  108. * Set the file that this attachment is for.
  109. * @param Swift_FileStream $file
  110. * @param string $contentType optional
  111. */
  112. public function setFile(Swift_FileStream $file, $contentType = null)
  113. {
  114. $this->setFilename(basename($file->getPath()));
  115. $this->setBody($file, $contentType);
  116. if (!isset($contentType))
  117. {
  118. $extension = strtolower(substr(
  119. $file->getPath(), strrpos($file->getPath(), '.') + 1
  120. ));
  121. if (array_key_exists($extension, $this->_mimeTypes))
  122. {
  123. $this->setContentType($this->_mimeTypes[$extension]);
  124. }
  125. }
  126. return $this;
  127. }
  128. }