/AIS/IMAP/Attachment.php

https://github.com/snyderp/AIS_IMAP · PHP · 248 lines · 64 code · 28 blank · 156 comment · 4 complexity · 08b2f1e08b9e0559d5ee84f6b446f42f MD5 · raw file

  1. <?php
  2. /**
  3. * This package contains a series of files that wraps around the functionality
  4. * provided by the PHP IMAP extension, and provide an OO interface for interacting
  5. * with IMAP mail servers, their messages, and those messages' attachments.
  6. *
  7. * This package was developed by AISLabs Inc. in Chicago, IL
  8. * and written by Peter Snyder.
  9. *
  10. * PHP Version 5
  11. *
  12. * @category Mail
  13. * @package AIS_IMAP
  14. * @author Peter Snyder <psnyder@aislabs.com>
  15. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  16. * @version GIT: <git_id>
  17. * @link http://aislabs.com/
  18. * @link https://github.com/snyderp/AIS_IMAP
  19. *
  20. */
  21. /**
  22. * Instances of this class represent file attachments to an email. A single
  23. * message may have zero or more associated attachments.
  24. *
  25. * @category PHP
  26. * @package AIS_IMAP
  27. * @author Peter Snyder <psnyder@aislabs.com>
  28. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  29. * @link https://github.com/snyderp/AIS_IMAP
  30. *
  31. */
  32. class AIS_IMAP_Attachment extends AIS_Debugable
  33. {
  34. // ========================
  35. // ! Protected Properties
  36. // ========================
  37. /**
  38. * Holds a reference to the email that contains
  39. * this attachment
  40. *
  41. * @var AIS_IMAP_Email
  42. * @access protected
  43. */
  44. protected $email;
  45. /**
  46. * Whether this attachment was selected as being
  47. * inline or as an attachment
  48. *
  49. * @var bool
  50. * @access protected
  51. */
  52. protected $is_inline;
  53. /**
  54. * The raw data of the attachment
  55. *
  56. * @var mixed
  57. * @access protected
  58. */
  59. protected $attachment;
  60. /**
  61. * Filename of the file that was attached
  62. *
  63. * @var mixed
  64. * @access protected
  65. */
  66. protected $file_name;
  67. /**
  68. * Unique identifier of the attachment, useful for reconstructing
  69. * HTML body content
  70. *
  71. * @var string
  72. * @access protected
  73. */
  74. protected $attachment_id;
  75. /**
  76. * A string describing the attachment type, usually file extension
  77. *
  78. * @var string
  79. * @access protected
  80. */
  81. protected $attachment_type;
  82. /**
  83. * Integer describing the filesize of the attachment
  84. *
  85. * @var int
  86. * @access protected
  87. */
  88. protected $size;
  89. // ==================
  90. // ! Public Methods
  91. // ==================
  92. /**
  93. * Convenience initilizer, that allows for populating the object at
  94. * initilization, including a refernece to the email containing this
  95. * attachment.
  96. *
  97. * @param AIS_IMAP_Email $email A reference to the email message
  98. * that this attachment is with.
  99. * @param stdClass $imap_data An object, provided by the
  100. * imap_fetchstructure function,
  101. * that describes a part of an email
  102. * message.
  103. * @param string $attachment_data A binary string that contains the
  104. * data stored in this part of the
  105. * message.
  106. *
  107. * @return void
  108. * @see http://www.php.net/manual/en/function.imap-fetchstructure.php
  109. */
  110. public function __construct($email, $imap_data, $attachment_data)
  111. {
  112. $this->email = $email;
  113. $this->is_inline = (empty($imap_data->disposition) OR
  114. $imap_data->disposition !== 'inline');
  115. $this->attachment = base64_decode($attachment_data);
  116. $this->size = $imap_data->bytes;
  117. $this->attachment_id = (empty($imap_data->id))
  118. ? false
  119. : $imap_data->id;
  120. $this->attachment_type = (empty($imap_data->subtype))
  121. ? false
  122. : $imap_data->subtype;
  123. if ( ! empty($imap_data->parameters)) {
  124. foreach ($imap_data->parameters as $item) {
  125. if ($item->attribute === 'name') {
  126. $this->file_name = $item->value;
  127. }
  128. }
  129. }
  130. }
  131. // ===========
  132. // ! Getters
  133. // ===========
  134. /**
  135. * Returns a reference to the email object that
  136. * this attachment belongs to
  137. *
  138. * @access public
  139. * @return AIS_IMAP_Email
  140. */
  141. public function email()
  142. {
  143. return $this->email;
  144. }
  145. /**
  146. * Returns a boolean value, describing whether
  147. * the file is intended to be displayed in the file
  148. * or as an external file
  149. *
  150. * @access public
  151. * @return bool
  152. */
  153. public function isInline()
  154. {
  155. return $this->is_inline;
  156. }
  157. /**
  158. * Contains the raw data of the attachment
  159. *
  160. * @access public
  161. * @return string|binary
  162. */
  163. public function attachment()
  164. {
  165. return $this->attachment;
  166. }
  167. /**
  168. * Returns the name of the file as it was attached,
  169. * which, if the attachment is inline, likely differs
  170. * from how the file is refered to in the email body
  171. *
  172. * @access public
  173. * @return string
  174. */
  175. public function filename()
  176. {
  177. return $this->file_name;
  178. }
  179. /**
  180. * Returns a string used for uniquly identifiying an email
  181. * attachment within the body of the email (such as an image
  182. * attachement within in the body HTML). This value is often
  183. * wrapped in angle brackets (eg <ID>), which can optionally
  184. * be stripped off by passing TRUE for $strip_brackets
  185. *
  186. * @param bool $strip_brackets (default. false) Often the
  187. * identifier for each attachment is surrounded by
  188. * angle brackets. Passing in true here will automatically
  189. * strip off these brackets if they're present.
  190. *
  191. * @return string
  192. */
  193. public function attachmentId($strip_brackets = false)
  194. {
  195. if ( ! $strip_brackets) {
  196. return $this->attachment_id;
  197. } else {
  198. return trim($this->attachment_id, ' <>');
  199. }
  200. }
  201. /**
  202. * The type of the attachment, usually a three character
  203. * file extension (ex. JPG, XML, etc.)
  204. *
  205. * @return string
  206. */
  207. public function attachmentType()
  208. {
  209. return $this->attachment_type;
  210. }
  211. /**
  212. * The size of the file attachment
  213. *
  214. * @return int
  215. */
  216. public function size()
  217. {
  218. return $this->size;
  219. }
  220. }