Make Phame blog policies non-nullable
[phabricator.git] / externals / mimemailparser / attachment.class.php
blob2631a4fc44699a0f1bd73cdb1ccdcb377180db14
1 <?php
3 /**
4 * Model of an Attachment
5 */
6 class MimeMailParser_attachment {
8 /**
9 * @var $filename Filename
11 public $filename;
12 /**
13 * @var $content_type Mime Type
15 public $content_type;
16 /**
17 * @var $content File Content
19 private $content;
20 /**
21 * @var $extension Filename extension
23 private $extension;
24 /**
25 * @var $content_disposition Content-Disposition (attachment or inline)
27 public $content_disposition;
28 /**
29 * @var $headers An Array of the attachment headers
31 public $headers;
33 private $stream;
35 public function __construct($filename, $content_type, $stream, $content_disposition = 'attachment', $headers = array()) {
36 $this->filename = $filename;
37 $this->content_type = $content_type;
38 $this->stream = $stream;
39 $this->content = null;
40 $this->content_disposition = $content_disposition;
41 $this->headers = $headers;
44 /**
45 * retrieve the attachment filename
46 * @return String
48 public function getFilename() {
49 return $this->filename;
52 /**
53 * Retrieve the Attachment Content-Type
54 * @return String
56 public function getContentType() {
57 return $this->content_type;
60 /**
61 * Retrieve the Attachment Content-Disposition
62 * @return String
64 public function getContentDisposition() {
65 return $this->content_disposition;
68 /**
69 * Retrieve the Attachment Headers
70 * @return String
72 public function getHeaders() {
73 return $this->headers;
76 /**
77 * Retrieve the file extension
78 * @return String
80 public function getFileExtension() {
81 if (!$this->extension) {
82 $ext = substr(strrchr($this->filename, '.'), 1);
83 if ($ext == 'gz') {
84 // special case, tar.gz
85 // todo: other special cases?
86 $ext = preg_match("/\.tar\.gz$/i", $ext) ? 'tar.gz' : 'gz';
88 $this->extension = $ext;
90 return $this->extension;
93 /**
94 * Read the contents a few bytes at a time until completed
95 * Once read to completion, it always returns false
96 * @return String
97 * @param $bytes Int[optional]
99 public function read($bytes = 2082) {
100 return feof($this->stream) ? false : fread($this->stream, $bytes);
104 * Retrieve the file content in one go
105 * Once you retrieve the content you cannot use MimeMailParser_attachment::read()
106 * @return String
108 public function getContent() {
109 if ($this->content === null) {
110 fseek($this->stream, 0);
111 while(($buf = $this->read()) !== false) {
112 $this->content .= $buf;
115 return $this->content;
119 * Allow the properties
120 * MimeMailParser_attachment::$name,
121 * MimeMailParser_attachment::$extension
122 * to be retrieved as public properties
123 * @param $name Object
125 public function __get($name) {
126 if ($name == 'content') {
127 return $this->getContent();
128 } else if ($name == 'extension') {
129 return $this->getFileExtension();
131 return null;