PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/email/vendor/swift/classes/Swift/Mime/Attachment.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 153 lines | 67 code | 15 blank | 71 comment | 3 complexity | 8f5b244338e658ce80fbd0d009977918 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. An attachment in Swift Mailer.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. //@require 'Swift/Mime/SimpleMimeEntity.php';
  16. //@require 'Swift/Mime/ContentEncoder.php';
  17. //@require 'Swift/Mime/HeaderSet.php';
  18. //@require 'Swift/FileStream.php';
  19. //@require 'Swift/KeyCache.php';
  20. /**
  21. * An attachment, in a multipart message.
  22. * @package Swift
  23. * @subpackage Mime
  24. * @author Chris Corbyn
  25. */
  26. class Swift_Mime_Attachment extends Swift_Mime_SimpleMimeEntity
  27. {
  28. /** Recognized MIME types */
  29. private $_mimeTypes = array();
  30. /**
  31. * Create a new Attachment with $headers, $encoder and $cache.
  32. * @param Swift_Mime_HeaderSet $headers
  33. * @param Swift_Mime_ContentEncoder $encoder
  34. * @param Swift_KeyCache $cache
  35. * @param array $mimeTypes optional
  36. */
  37. public function __construct(Swift_Mime_HeaderSet $headers,
  38. Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache,
  39. $mimeTypes = array())
  40. {
  41. parent::__construct($headers, $encoder, $cache);
  42. $this->setDisposition('attachment');
  43. $this->setContentType('application/octet-stream');
  44. $this->_mimeTypes = $mimeTypes;
  45. }
  46. /**
  47. * Get the nesting level used for this attachment.
  48. * Always returns {@link LEVEL_MIXED}.
  49. * @return int
  50. */
  51. public function getNestingLevel()
  52. {
  53. return self::LEVEL_MIXED;
  54. }
  55. /**
  56. * Get the Content-Disposition of this attachment.
  57. * By default attachments have a disposition of "attachment".
  58. * @return string
  59. */
  60. public function getDisposition()
  61. {
  62. return $this->_getHeaderFieldModel('Content-Disposition');
  63. }
  64. /**
  65. * Set the Content-Disposition of this attachment.
  66. * @param string $disposition
  67. */
  68. public function setDisposition($disposition)
  69. {
  70. if (!$this->_setHeaderFieldModel('Content-Disposition', $disposition))
  71. {
  72. $this->getHeaders()->addParameterizedHeader(
  73. 'Content-Disposition', $disposition
  74. );
  75. }
  76. return $this;
  77. }
  78. /**
  79. * Get the filename of this attachment when downloaded.
  80. * @return string
  81. */
  82. public function getFilename()
  83. {
  84. return $this->_getHeaderParameter('Content-Disposition', 'filename');
  85. }
  86. /**
  87. * Set the filename of this attachment.
  88. * @param string $filename
  89. */
  90. public function setFilename($filename)
  91. {
  92. $this->_setHeaderParameter('Content-Disposition', 'filename', $filename);
  93. $this->_setHeaderParameter('Content-Type', 'name', $filename);
  94. return $this;
  95. }
  96. /**
  97. * Get the file size of this attachment.
  98. * @return int
  99. */
  100. public function getSize()
  101. {
  102. return $this->_getHeaderParameter('Content-Disposition', 'size');
  103. }
  104. /**
  105. * Set the file size of this attachment.
  106. * @param int $size
  107. */
  108. public function setSize($size)
  109. {
  110. $this->_setHeaderParameter('Content-Disposition', 'size', $size);
  111. return $this;
  112. }
  113. /**
  114. * Set the file that this attachment is for.
  115. * @param Swift_FileStream $file
  116. * @param string $contentType optional
  117. */
  118. public function setFile(Swift_FileStream $file, $contentType = null)
  119. {
  120. $this->setFilename(basename($file->getPath()));
  121. $this->setBody($file, $contentType);
  122. if (!isset($contentType))
  123. {
  124. $extension = strtolower(substr(
  125. $file->getPath(), strrpos($file->getPath(), '.') + 1
  126. ));
  127. if (array_key_exists($extension, $this->_mimeTypes))
  128. {
  129. $this->setContentType($this->_mimeTypes[$extension]);
  130. }
  131. }
  132. return $this;
  133. }
  134. }