From 9f108dd1a969473375341d92a7b1252fa2cedc9a Mon Sep 17 00:00:00 2001 From: mszulecki Date: Thu, 14 Jun 2007 17:09:01 +0000 Subject: Initial import. git-svn-id: http://svn.sukimashita.com/repos/mailadmin/trunk@2 4281df72-ff29-0410-8fee-2d9ac0c5f5a7 --- lib/model/Mailbox.php | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 lib/model/Mailbox.php (limited to 'lib/model/Mailbox.php') diff --git a/lib/model/Mailbox.php b/lib/model/Mailbox.php new file mode 100644 index 0000000..c29e1db --- /dev/null +++ b/lib/model/Mailbox.php @@ -0,0 +1,187 @@ +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(); + } +} -- cgit v1.1-32-gdbae