summaryrefslogtreecommitdiff
path: root/apps/mm/models/Message.class.php
blob: b2a2b7e03cbd9eaab0a6617b46693530730253cb (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
<?php
require_once('MimeMailParser.class.php');
require_once('Database.class.php');

class Message extends Model {
	public static function add($infile) {
		$parser = new MimeMailParser();
		$parser->setPath($infile);
		$id = preg_replace('/<(.*)>/', '$1',
		                   $parser->getHeader('message-id'));
		$id = str_replace('/', '', $id); // For security purposes

		$db = Database::getInstance();
		$msgdir = $db->getSysConf('msgdir');
		$msg_file = "$msgdir/$id";

		rename($infile, $msg_file);
		return new Message($id);
	}

	private $_msgid;
	private $_msgdir;
	private $_parser;

	public function __construct($msgid) {
		$this->_msgid = str_replace('/', '', $msgid);
		if (!file_exists($this->file())) {
			return false;
		}
	}

	public function msgid() {
		return $this->_msgid;
	}

	private function msgdir() {
		if (!isset($this->_msgdir)) {
			$db = Database::getInstance();
			$this->_msgdir = $db->getSysConf('msgdir');
		}
		return $this->_msgdir;
	}
	
	private function file() {
		return $this->msgdir().'/'.$this->msgid();
	}

	private function parser() {
		if (!isset($this->_parser)) {
			$this->_parser = new MimeMailParser();
			$this->_parser->setPath($this->file());
		}
		return $this->_parser;
	}

	/**
	 * Retrieve the Email Headers
	 * @return Array
	 */
	public function getHeaders() {
		return $this->parser()->getHeaders();
	}

	/**
	 * Retrieve the raw Email Headers
	 * @return string
	 */
	public function getHeadersRaw() {
		return $this->parser()->getHeadersRaw();
	}

	/**
	 * Retrieve a specific Email Header
	 * @return String
	 * @param $name String Header name
	 */
	public function getHeader($name) {
		return $this->parser()->getHeader($name);
	}

	/**
	 * Returns the part for the message body in the specified format
	 * @return Part or False if not found
	 * @param $type String[optional]
	 */
	public function getMessageBodyPart($type = 'text') {
		return $this->parser()->getMessageBodyPart($type);
	}

	/**
	 * Returns the email message body in the specified format
	 * @return Mixed String Body or False if not found
	 * @param $type Object[optional]
	 */
	public function getMessageBody($type = 'text') {
		return $this->parser()->getMessageBody($type);
	}

	/**
	 * get the headers for the message body part.
	 * @return Array
	 * @param $type Object[optional]
	 */
	public function getMessageBodyHeaders($type = 'text') {
		return $this->parser()->getMessageBodyHeaders($type);
	}

	/**
	 * Returns the attachments contents in order of appearance
	 * @return Array
	 * @param $type Object[optional]
	 */
	public function getAttachments() {
		return $this->parser()->getAttachments();
	}
}