summaryrefslogtreecommitdiffstats
path: root/apps/admin/modules/address/actions/actions.class.php
blob: d042fbfc7b72a3d4bf9e420062739b921a775abc (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
<?php

/**
 * address actions.
 *
 * @package    mailadmin
 * @subpackage address
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class addressActions extends autoaddressActions
{
	public function executeList()
	{
		// pageination
		if($this->hasRequestParameter("max_per_page"))
			$this->getUser()->setResultsPerPage($this->getRequestParameter("max_per_page"));
		
		return parent::executeList();
	}

	public function executeCreate()
	{
		$this->setTemplate("edit");
		$result = $this->executeEdit();
		
		if($this->getRequest()->getMethod() == sfRequest::GET)
		{
			if(!$this->hasRequestParameter("id"))
			{
				$this->updateAddressFromRequest();
			}
			$this->address->setActive(true);
			$this->address->setSaveInMailbox(true);
		}
		
		return $result;
	}

	public function handleErrorCreate()
	{
		$this->setTemplate("edit");
		return $this->handleErrorEdit();
	}

	public function validateCreate()
	{
		return $this->validateEdit();
	}

	public function validateEdit()
	{
		$i18n = sfI18N::getInstance();
	
		if($this->getRequest()->getMethod() == sfRequest::GET)
			return true;

		$newaddress = $this->getRequestParameter('address');
		if($newaddress["domain_id"]=='')
		{
			$this->getRequest()->setError('address{alias}', 'A domain must be selected');
			return false;
		}

		$address = new Address();

		if($this->getRequest()->hasParameter('id'))
			$address = AddressPeer::retrieveByPk($this->getRequestParameter('id'));
		
		// if we got different values than the current record already has
		if($this->getActionName()=="create" ||
		   $address->getLocalpart() != $newaddress["localpart"] ||
		   $address->getDomainId() != $newaddress["domain_id"]
		)
		{
			// check if localpart@domain is already used
			$c = new Criteria();
			$c->add(AddressPeer::LOCALPART, $newaddress["localpart"]);
			$c->add(AddressPeer::DOMAIN_ID, $newaddress["domain_id"]);
			$checkaddress = AddressPeer::doSelectOne($c);
			if($checkaddress)
			{
				$this->getRequest()->setError('address{alias}', $i18n->__('The address "%1%" already exists', array("%1%"=>$checkaddress)));
				return false;
			}
		}
		
		// check if either save to mailbox option or destination is set to satisfy message routing
		if(!isset($newaddress["save_in_mailbox"]) && $newaddress["destination"]=='')
		{
			$this->getRequest()->setError('address{save_in_mailbox}', 'If messages are not saved in the mailbox, the forward destination has to be set');
			$this->getRequest()->setError('address{destination}', 'Destination can not be empty if the message is not saved in the mailbox');
			return false;

		}
		
		// verify destination list
		$ev = new sfEmailValidator();
		$destinations = Address::getDestinationAsArray($newaddress["destination"]);
		foreach($destinations as $dest)
		{
			if($dest=='')
				continue;

			// check email
			if(strpos($dest, "@") !== false)
			{
				$error = '';
				$ev->initialize($this->getContext(), array(
					'email_error' => $i18n->__('Invalid E-Mail address "%1%"', array("%1%" => $dest))
				));
				
				if(!$ev->execute($dest, $error))
				{
					$this->getRequest()->setError('address{destination}', $error);
					return false;
				}
			}
			else // check if mailbox name is valid
			{
				// superadmin has all freedom
				if($this->getUser()->hasCredential("superadmin"))
					continue;

				$c = new Criteria();
				$c->add(MailboxPeer::NAME, $dest);			
				$mailbox = MailboxPeer::doSelectOne($c);
				if(!$mailbox)
				{
					// no such target
					$this->getRequest()->setError('address{destination}', $i18n->__('Mailbox "%1%" does not exist', array("%1%" => $dest)));
					return false;
				}

				// check permissions
				$c = new Criteria();
				$c->add(DomainPermissionPeer::DOMAIN_ID, $mailbox->getDomainId());
				$permissions = $this->getUser()->getDomainPermissions($c);
				if(!$permissions)
				{
					$this->getRequest()->setError('address{destination}', $i18n->__('No permissions to use Mailbox "%1%" as destination', array("%1%" => $dest)));
					return false;
				}
			}
		}

		return true;
	}

	protected function updateAddressFromRequest()
	{
		$address = $this->getRequestParameter('address');

		if (isset($address['localpart']))
		{
			$this->address->setLocalpart($address['localpart']);
		}		
		if (isset($address['domain_id']))
		{
			$this->address->setDomainId($address['domain_id']);
		}
		
		return parent::updateAddressFromRequest();
	}
}