summaryrefslogtreecommitdiff
path: root/apps/mm/ext/MimeMailParser_attachment.class.php
blob: 6bd327c05ae19c6855d3273a37a6525680e9ab89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

/**
 * Model of an Attachment
 */
class MimeMailParser_attachment {

	/**
	 * @var $filename Filename
	 */
	public  $filename;
	/**
	 * @var $content_type Mime Type
	 */
	public  $content_type;
	/**
	 * @var $content File Content
	 */
	private  $content;
	/**
	 * @var $extension Filename extension
	 */
	private $extension;
	/**
	 * @var $content_disposition Content-Disposition (attachment or inline)
	 */
	public $content_disposition;
	/**
	 * @var $headers An Array of the attachment headers
	 */
	public $headers;
	
	private  $stream;

	public function __construct($filename, $content_type, $stream, $content_disposition, $headers) {
		$this->filename = $filename;
		$this->content_type = $content_type;
		$this->stream = $stream;
		$this->content = null;
		$this->content_disposition = $content_disposition;
		$this->headers = $headers;
	}
	
	/**
	 * retrieve the attachment filename
	 * @return String
	 */
	public function getFilename() {
		return $this->filename;
	}
	
	/**
	 * Retrieve the Attachment Content-Type
	 * @return String
	 */
	public function getContentType() {
		return $this->content_type;
	}
	
	/**
	 * Retrieve the Attachment Content-Disposition
	 * @return String
	 */
	public function getContentDisposition() {
		return $this->content_disposition;
	}
	
	/**
	 * Retrieve the Attachment Headers
	 * @return String
	 */
	public function getHeaders() {
		return $this->headers;
	}
	
	/**
	 * Retrieve the file extension
	 * @return String
	 */
	public function getFileExtension() {
		if (!$this->extension) {
			$ext = substr(strrchr($this->filename, '.'), 1);
			if ($ext == 'gz') {
				// special case, tar.gz
				// todo: other special cases?
				$ext = preg_match("/\.tar\.gz$/i", $ext) ? 'tar.gz' : 'gz';
			}
			$this->extension = $ext;
		}
		return $this->extension;
	}
	
	/**
	 * Read the contents a few bytes at a time until completed
	 * Once read to completion, it always returns false
	 * @return String
	 * @param $bytes Int[optional]
	 */
	public function read($bytes = 2082) {
		return feof($this->stream) ? false : fread($this->stream, $bytes);
	}
	
	/**
	 * Retrieve the file content in one go
	 * Once you retreive the content you cannot use MimeMailParser_attachment::read()
	 * @return String
	 */
	public function getContent() {
		if ($this->content === null) {
			fseek($this->stream, 0);
			while(($buf = $this->read()) !== false) { 
				$this->content .= $buf; 
			}
		}
		return $this->content;
	}
	
	/**
	 * Allow the properties 
	 * 	MimeMailParser_attachment::$name,
	 * 	MimeMailParser_attachment::$extension 
	 * to be retrieved as public properties
	 * @param $name Object
	 */
	public function __get($name) {
		if ($name == 'content') {
			return $this->getContent();
		} else if ($name == 'extension') {
			return $this->getFileExtension();
		}
		return null;
	}
	
}

?>