1<?php
2/**
3 * EGroupware Api: generic base class for SMTP
4 *
5 * @link http://www.egroupware.org
6 * @package api
7 * @subpackage mail
8 * @author Lars Kneschke <lkneschke@linux-at-work.de>
9 * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
10 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License Version 2+
11 * @version $Id$
12 */
13
14namespace EGroupware\Api\Mail;
15
16use EGroupware\Api;
17
18/**
19 * EMailAdmin generic base class for SMTP
20 */
21class Smtp
22{
23	/**
24	 * Label shown in EMailAdmin
25	 */
26	const DESCRIPTION = 'standard SMTP-Server';
27
28	/**
29	 * Capabilities of this class (pipe-separated): default, forward
30	 */
31	const CAPABILITIES = 'default';
32
33	/**
34	 * Attribute value to enable mail for an account, OR false if existense of attribute is enough to enable account
35	 *
36	 * Logical values uses inside EGroupware, different classes might store different values internally
37	 */
38	const MAIL_ENABLED = 'active';
39
40	/**
41	 * Attribute value to only forward mail
42	 *
43	 * Logical values uses inside EGroupware, different classes might store different values internally
44	 */
45	const FORWARD_ONLY = 'forwardOnly';
46
47	/**
48	 * Reference to global account object
49	 *
50	 * @var accounts
51	 */
52	protected $accounts;
53
54	/**
55	 * SmtpServerId
56	 *
57	 * @var int
58	 */
59	var $SmtpServerId;
60
61	var $smtpAuth = false;
62
63	var $editForwardingAddress = false;
64
65	var $host;
66
67	var $port;
68
69	var $username;
70
71	var $password;
72
73	var $defaultDomain;
74
75	var $loginType;
76
77	/**
78	 * Constructor
79	 *
80	 * @param string $defaultDomain =null
81	 */
82	function __construct($defaultDomain=null)
83	{
84		$this->defaultDomain = $defaultDomain ? $defaultDomain : $GLOBALS['egw_info']['server']['mail_suffix'];
85
86		$this->accounts = $GLOBALS['egw']->accounts;
87	}
88
89	/**
90	 * Return description for EMailAdmin
91	 *
92	 * @return string
93	 */
94	public static function description()
95	{
96		return static::DESCRIPTION;
97	}
98
99	/**
100	 * Hook called on account creation
101	 *
102	 * @param array $_hookValues values for keys 'account_email', 'account_firstname', 'account_lastname', 'account_lid'
103	 * @return boolean true on success, false on error writing to ldap
104	 */
105	function addAccount($_hookValues)
106	{
107		$mailLocalAddress = $_hookValues['account_email'] ? $_hookValues['account_email'] :
108			Api\Accounts::email($_hookValues['account_firstname'],
109				$_hookValues['account_lastname'],$_hookValues['account_lid'],$this->defaultDomain);
110
111		$account_id = !empty($_hookValues['account_id']) ? $_hookValues['account_id'] :
112			$this->accounts->name2id($_hookValues['account_lid'], 'account_lid', 'u');
113
114		if ($this->accounts->exists($account_id) != 1)
115		{
116			throw new Api\Exception\AssertionFailed("Account #$account_id ({$_hookValues['account_lid']}) does NOT exist!");
117		}
118		return $this->setUserData($account_id, array(), array(), null, self::MAIL_ENABLED, $mailLocalAddress, null);
119	}
120
121	/**
122	 * Hook called on account deletion
123	 *
124	 * @param array $_hookValues values for keys 'account_lid', 'account_id'
125	 * @return boolean true on success, false on error writing to ldap
126	 */
127	function deleteAccount($_hookValues)
128	{
129		unset($_hookValues);	// not used, but required by function signature
130
131		return true;
132	}
133
134	/**
135	 * Get all email addresses of an account
136	 *
137	 * @param string $_accountName
138	 * @return array
139	 */
140	function getAccountEmailAddress($_accountName)
141	{
142		$emailAddresses	= array();
143
144		if (($account_id = $this->accounts->name2id($_accountName, 'account_lid', 'u')))
145		{
146			$realName = trim($GLOBALS['egw_info']['user']['account_firstname'] . (!empty($GLOBALS['egw_info']['user']['account_firstname']) ? ' ' : '') . $GLOBALS['egw_info']['user']['account_lastname']);
147			$emailAddresses[] = array (
148				'name'		=> $realName,
149				'address'	=> $this->accounts->id2name($account_id, 'account_email'),
150				'type'		=> 'default',
151			);
152		}
153		return $emailAddresses;
154	}
155
156	/**
157	 * Get the data of a given user
158	 *
159	 * @param int|string $user numerical account-id, account-name or email address
160	 * @param boolean $match_uid_at_domain =true true: uid@domain matches, false only an email or alias address matches
161	 * @return array with values for keys 'mailLocalAddress', 'mailAlternateAddress' (array), 'mailForwardingAddress' (array),
162	 * 	'accountStatus' ("active"), 'quotaLimit' and 'deliveryMode' ("forwardOnly")
163	 */
164	function getUserData($user, $match_uid_at_domain=false)
165	{
166		unset($user, $match_uid_at_domain);	// not used, but required by function signature
167
168		$userData = array();
169
170		return $userData;
171	}
172
173
174	/**
175	 * Saves the forwarding information
176	 *
177	 * @param int $_accountID
178	 * @param string|array $_forwardingAddress
179	 * @param string $_keepLocalCopy 'yes'
180	 * @return boolean true on success, false on error writing
181	 */
182	function saveSMTPForwarding($_accountID, $_forwardingAddress, $_keepLocalCopy)
183	{
184		return $this->setUserData($_accountID, array(),
185			$_forwardingAddress ? (array)$_forwardingAddress : array(),
186			$_keepLocalCopy != 'yes' ? self::FORWARD_ONLY : null, null, null, null, true);
187	}
188
189	/**
190	 * Set the data of a given user
191	 *
192	 * @param int $_uidnumber numerical user-id
193	 * @param array $_mailAlternateAddress
194	 * @param array $_mailForwardingAddress
195	 * @param string $_deliveryMode
196	 * @param string $_accountStatus
197	 * @param string $_mailLocalAddress
198	 * @param int $_quota in MB
199	 * @param boolean $_forwarding_only =false true: store only forwarding info, used internally by saveSMTPForwarding
200	 * @param string $_setMailbox =null used only for account migration
201	 * @return boolean true on success, false on error writing to ldap
202	 */
203	function setUserData($_uidnumber, array $_mailAlternateAddress, array $_mailForwardingAddress, $_deliveryMode,
204		$_accountStatus, $_mailLocalAddress, $_quota, $_forwarding_only=false, $_setMailbox=null)
205	{
206		unset($_uidnumber, $_mailAlternateAddress, $_mailForwardingAddress,	// not used, but require by function signature
207			$_deliveryMode, $_accountStatus, $_mailLocalAddress, $_quota, $_forwarding_only, $_setMailbox);
208
209		return true;
210	}
211
212	/**
213	 * Hook called on account update
214	 *
215	 * @param array $_hookValues values for keys 'account_email', 'account_firstname', 'account_lastname', 'account_lid', 'account_id'
216	 * @return boolean true on success, false on error writing to ldap
217	 */
218	function updateAccount($_hookValues)
219	{
220		unset($_hookValues);	// not used, but required by function signature
221
222		return true;
223	}
224
225	/**
226	 * Build mailbox address for given account and mail_addr_type
227	 *
228	 * If $account is an array (with values for keys account_(id|lid|email), it does NOT call accounts class
229	 *
230	 * @param int|array $account account_id or whole account array with values for keys
231	 * @param string $domain=null domain, default use $this->defaultDomain
232	 * @param string $mail_login_type=null standard(uid), vmailmgr(uid@domain), email or uidNumber,
233	 * 	default use $this->loginType
234	 * @return string
235	 */
236	/*static*/ public function mailbox_addr($account,$domain=null,$mail_login_type=null)
237	{
238		if (is_null($domain)) $domain = $this->defaultDomain;
239		if (is_null($mail_login_type)) $mail_login_type = $this->loginType;
240
241		switch($mail_login_type)
242		{
243			case 'email':
244				$mbox = is_array($account) ? $account['account_email'] : $GLOBALS['egw']->accounts->id2name($account,'account_email');
245				break;
246
247			case 'uidNumber':
248				if (is_array($account)) $account = $account['account_id'];
249				$mbox = 'u'.$account.'@'.$domain;
250				break;
251
252			case 'standard':
253				$mbox = is_array($account) ? $account['account_lid'] : $GLOBALS['egw']->accounts->id2name($account);
254				break;
255
256			case 'vmailmgr':
257			default:
258				$mbox = is_array($account) ? $account['account_lid'] : $GLOBALS['egw']->accounts->id2name($account);
259				$mbox .= '@'.$domain;
260				break;
261		}
262		//error_log(__METHOD__."(".array2string($account).",'$domain','$mail_login_type') = '$mbox'");
263
264		return $mbox;
265	}
266}
267