summaryrefslogtreecommitdiffstats
path: root/lib/model/Mailbox.php
blob: c29e1db0d467157667b83cf8bb8314f04d83e683 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php

/**
 * Subclass for representing a row from the 'mailbox' table.
 *
 * 
 *
 * @package lib.model
 */ 
class Mailbox extends BaseMailbox
{
	protected $imap = null;

	public function __construct()
	{
		// set defaults
		$this->setMaxQuota(sfConfig::get('app_domain_default_mailbox_quota'));
		$this->setMaxAddressCount(sfConfig::get('app_mailbox_max_address_count'));
	}

	public function getImapName()
	{
		return "user.".$this->getName();
	}

	public function getExistsOnImapServer()
	{
		$this->requireIMAPAdminConnection();
		$exists = $this->imap->getlist($this->getImapName(), '%');
		if(is_array($exists) && (count($exists)!=0))
		{
			return true;
		}
		return false;
	}

	protected function requireIMAPAdminConnection()
	{
		if(!$this->imap)
			$this->imap = IMAPManager::getAdminConnection();
	}

	protected $quota = null;
	protected function updateIMAPQuota()
	{
		$this->requireIMAPAdminConnection();
		$quota = $this->imap->getquota("user.".$this->getName());
		if($quota)
			$this->quota = $quota;
	}

	public function getQuota()
	{
		if($this->quota == null)
			$this->updateIMAPQuota();

		return $this->quota;
	}

	public function save($con = null)
	{
		// create or update resources on IMAP server
		if($this->isNew())
		{
			$this->requireIMAPAdminConnection();

			// mailbox
			$this->imap->create($this->getImapName());

			// acl
			$this->imap->setacl($this->getImapName(), IMAPManager::getAdminUsername(), $this->imap->getAvailableACL());

			// quota
			$this->imap->setquota($this->getImapName(), $this->getMaxQuota());

			// default folders

		}
		else // update
		{
			// mailbox name changed?
			if($this->isColumnModified(MailboxPeer::NAME))
			{
				$this->requireIMAPAdminConnection();
				
				$oldmailbox = MailboxPeer::retrieveByPk($this->getId());
				if($oldmailbox->getName() != $this->getName())
				{
					// requires "allowusermoves: 1" in /etc/imapd.conf
					// mailbox
					$this->imap->rename($oldmailbox->getImapName(), $this->getImapName());
				}
			}
			
			// quota changed?
			if($this->isColumnModified(MailboxPeer::MAX_QUOTA))
			{
				$this->requireIMAPAdminConnection();
				
				$this->imap->setquota($this->getImapName(), $this->getMaxQuota());
			}
		}

		return parent::save($con);
	}

	public function delete($con = null)
	{
		// remove resources on IMAP server
		if(!$this->isNew())
		{
			$this->requireIMAPAdminConnection();
			
			// grant all rights to admin to be able to delete mailbox
			$this->imap->setacl($this->getImapName(), IMAPManager::getAdminUsername(), $this->imap->getAvailableACL());

			// mailbox
			$this->imap->delete($this->getImapName());
		}

		return parent::delete($con);
	}

	// TODO: extract password logic into own library
	public function isPasswordEqual($password)
	{
		// check crypt type
		$server_settings = sfConfig::get('app_server_default');
		$crypt_type = $server_settings['pam']['crypt'];
		
		switch($crypt_type)
		{
			case 0: // plain
			return ($password == $this->getPassword());
			case 1: // crypt
			return (crypt($password, substr($this->getPassword(), 0, 2)) == $this->getPassword());
			case 2: // md5
			return (md5($password) == $this->getPassword());
			case 3: // sha1
			return (sha1($password) == $this->getPassword());
			break;
		}

		return false;
	}

	public function setPassword($password)
	{
		if($password=='')
			return false;
		// check crypt type
		$server_settings = sfConfig::get('app_server_default');
		$crypt_type = $server_settings['pam']['crypt'];

		switch($crypt_type)
		{
			case 0: // plain
			break;
			case 1: // crypt
			$password = crypt($password, substr($password, 0, 8));
			break;
			case 2: // md5
			$password = md5($password);
			break;
			case 3: // sha1
			$password = sha1($password);
			break;
		}
		
		return parent::setPassword($password);
	}

	public function setNewPassword($password)
	{
		return $this->setPassword($password);
	}

	public function getAddressCount()
	{
		return $this->countAddresss();
	}

	public function __toString()
	{
		return $this->getName();
	}
}