1<?php
2
3namespace RainLoop\Model;
4
5use MailSo\Net\Enumerations\ConnectionSecurityType;
6
7class Domain
8{
9	const DEFAULT_FORWARDED_FLAG = '$Forwarded';
10
11	/**
12	 * @var string
13	 */
14	private $sName;
15
16	/**
17	 * @var string
18	 */
19	private $sIncHost;
20
21	/**
22	 * @var int
23	 */
24	private $iIncPort;
25
26	/**
27	 * @var int
28	 */
29	private $iIncSecure;
30
31	/**
32	 * @var bool
33	 */
34	private $bIncShortLogin;
35
36	/**
37	 * @var string
38	 */
39	private $sOutHost;
40
41	/**
42	 * @var int
43	 */
44	private $iOutPort;
45
46	/**
47	 * @var int
48	 */
49	private $iOutSecure;
50
51	/**
52	 * @var bool
53	 */
54	private $bOutShortLogin;
55
56	/**
57	 * @var bool
58	 */
59	private $bOutAuth;
60
61	/**
62	 * @var bool
63	 */
64	private $bOutUsePhpMail;
65
66	/**
67	 * @var bool
68	 */
69	private $bUseSieve;
70
71	/**
72	 * @var string
73	 */
74	private $sSieveHost;
75
76	/**
77	 * @var int
78	 */
79	private $iSievePort;
80
81	/**
82	 * @var int
83	 */
84	private $iSieveSecure;
85
86	/**
87	 * @var bool
88	 */
89	private $bSieveAllowRaw;
90
91	/**
92	 * @var string
93	 */
94	private $sWhiteList;
95
96	/**
97	 * @var string
98	 */
99	private $sAliasName;
100
101	/**
102	 * @param string $sName
103	 * @param string $sIncHost
104	 * @param int $iIncPort
105	 * @param int $iIncSecure
106	 * @param bool $bIncShortLogin
107	 * @param bool $bUseSieve
108	 * @param string $sSieveHost
109	 * @param int $iSievePort
110	 * @param int $iSieveSecure
111	 * @param string $sOutHost
112	 * @param int $iOutPort
113	 * @param int $iOutSecure
114	 * @param bool $bOutShortLogin
115	 * @param bool $bOutAuth
116	 * @param bool $bOutUsePhpMail = false
117	 * @param string $sWhiteList = ''
118	 */
119	private function __construct($sName,
120		$sIncHost, $iIncPort, $iIncSecure, $bIncShortLogin,
121		$bUseSieve, $sSieveHost, $iSievePort, $iSieveSecure,
122		$sOutHost, $iOutPort, $iOutSecure, $bOutShortLogin, $bOutAuth, $bOutUsePhpMail = false,
123		$sWhiteList = '')
124	{
125		$this->sName = $sName;
126		$this->sIncHost = $sIncHost;
127		$this->iIncPort = $iIncPort;
128		$this->iIncSecure = $iIncSecure;
129		$this->bIncShortLogin = $bIncShortLogin;
130
131		$this->sOutHost = $sOutHost;
132		$this->iOutPort = $iOutPort;
133		$this->iOutSecure = $iOutSecure;
134		$this->bOutShortLogin = $bOutShortLogin;
135		$this->bOutAuth = $bOutAuth;
136		$this->bOutUsePhpMail = $bOutUsePhpMail;
137
138		$this->bUseSieve = $bUseSieve;
139		$this->sSieveHost = $sSieveHost;
140		$this->iSievePort = $iSievePort;
141		$this->iSieveSecure = $iSieveSecure;
142
143		$this->bSieveAllowRaw = false;
144
145		$this->sWhiteList = \trim($sWhiteList);
146		$this->sAliasName = '';
147	}
148
149	/**
150	 * @param string $sName
151	 * @param string $sIncHost
152	 * @param int $iIncPort
153	 * @param int $iIncSecure
154	 * @param bool $bIncShortLogin
155	 * @param bool $bUseSieve
156	 * @param string $sSieveHost
157	 * @param int $iSievePort
158	 * @param int $iSieveSecure
159	 * @param string $sOutHost
160	 * @param int $iOutPort
161	 * @param int $iOutSecure
162	 * @param bool $bOutShortLogin
163	 * @param bool $bOutAuth
164	 * @param bool $bOutUsePhpMail = false
165	 * @param string $sWhiteList = ''
166	 *
167	 * @return \RainLoop\Model\Domain
168	 */
169	public static function NewInstance($sName,
170		$sIncHost, $iIncPort, $iIncSecure, $bIncShortLogin,
171		$bUseSieve, $sSieveHost, $iSievePort, $iSieveSecure,
172		$sOutHost, $iOutPort, $iOutSecure, $bOutShortLogin, $bOutAuth, $bOutUsePhpMail,
173		$sWhiteList = '')
174	{
175		return new self($sName,
176			$sIncHost, $iIncPort, $iIncSecure, $bIncShortLogin,
177			$bUseSieve, $sSieveHost, $iSievePort, $iSieveSecure,
178			$sOutHost, $iOutPort, $iOutSecure, $bOutShortLogin, $bOutAuth, $bOutUsePhpMail,
179			$sWhiteList);
180	}
181
182	/**
183	 * @param string $sName
184	 * @param array $aDomain
185	 *
186	 * @return \RainLoop\Model\Domain|null
187	 */
188	public static function NewInstanceFromDomainConfigArray($sName, $aDomain)
189	{
190		$oDomain = null;
191
192		if (0 < \strlen($sName) && \is_array($aDomain) && 0 < \strlen($aDomain['imap_host']) && 0 < \strlen($aDomain['imap_port']))
193		{
194			$sIncHost = (string) $aDomain['imap_host'];
195			$iIncPort = (int) $aDomain['imap_port'];
196			$iIncSecure = self::StrConnectionSecurityTypeToCons(
197				!empty($aDomain['imap_secure']) ? $aDomain['imap_secure'] : '');
198
199			$bUseSieve = isset($aDomain['sieve_use']) ? (bool) $aDomain['sieve_use'] : false;
200			$bSieveAllowRaw = isset($aDomain['sieve_allow_raw']) ? (bool) $aDomain['sieve_allow_raw'] : false;
201
202			$sSieveHost = empty($aDomain['sieve_host']) ? '' : (string) $aDomain['sieve_host'];
203			$iSievePort = empty($aDomain['sieve_port']) ? 4190 : (int) $aDomain['sieve_port'];
204			$iSieveSecure = self::StrConnectionSecurityTypeToCons(
205				!empty($aDomain['sieve_secure']) ? $aDomain['sieve_secure'] : '');
206
207			$sOutHost = empty($aDomain['smtp_host']) ? '' : (string) $aDomain['smtp_host'];
208			$iOutPort = empty($aDomain['smtp_port']) ? 25 : (int) $aDomain['smtp_port'];
209			$iOutSecure = self::StrConnectionSecurityTypeToCons(
210				!empty($aDomain['smtp_secure']) ? $aDomain['smtp_secure'] : '');
211
212			$bOutAuth = isset($aDomain['smtp_auth']) ? (bool) $aDomain['smtp_auth'] : true;
213			$bOutUsePhpMail = isset($aDomain['smtp_php_mail']) ? (bool) $aDomain['smtp_php_mail'] : false;
214			$sWhiteList = (string) (isset($aDomain['white_list']) ? $aDomain['white_list'] : '');
215
216			$bIncShortLogin = isset($aDomain['imap_short_login']) ? (bool) $aDomain['imap_short_login'] : false;
217			$bOutShortLogin = isset($aDomain['smtp_short_login']) ? (bool) $aDomain['smtp_short_login'] : false;
218
219			$oDomain = self::NewInstance($sName,
220				$sIncHost, $iIncPort, $iIncSecure, $bIncShortLogin,
221				$bUseSieve, $sSieveHost, $iSievePort, $iSieveSecure,
222				$sOutHost, $iOutPort, $iOutSecure, $bOutShortLogin, $bOutAuth, $bOutUsePhpMail,
223				$sWhiteList);
224
225			$oDomain->SetSieveAllowRaw($bSieveAllowRaw);
226		}
227
228		return $oDomain;
229	}
230
231	/**
232	 * @param string $sStr
233	 *
234	 * @return string
235	 */
236	private function encodeIniString($sStr)
237	{
238		return str_replace('"', '\\"', $sStr);
239	}
240
241	public function Normalize()
242	{
243		$this->sIncHost = \trim($this->sIncHost);
244		$this->sSieveHost = \trim($this->sSieveHost);
245		$this->sOutHost = \trim($this->sOutHost);
246		$this->sWhiteList = \trim($this->sWhiteList);
247
248		if ($this->iIncPort <= 0)
249		{
250			$this->iIncPort = 143;
251		}
252
253		if ($this->iSievePort <= 0)
254		{
255			$this->iSievePort = 4190;
256		}
257
258		if ($this->iOutPort <= 0)
259		{
260			$this->iOutPort = 25;
261		}
262	}
263
264	/**
265	 * @return string
266	 */
267	public function ToIniString()
268	{
269		$this->Normalize();
270		return \implode("\n", array(
271			'imap_host = "'.$this->encodeIniString($this->sIncHost).'"',
272			'imap_port = '.$this->iIncPort,
273			'imap_secure = "'.self::ConstConnectionSecurityTypeToStr($this->iIncSecure).'"',
274			'imap_short_login = '.($this->bIncShortLogin ? 'On' : 'Off'),
275			'sieve_use = '.($this->bUseSieve ? 'On' : 'Off'),
276			'sieve_allow_raw = '.($this->bSieveAllowRaw ? 'On' : 'Off'),
277			'sieve_host = "'.$this->encodeIniString($this->sSieveHost).'"',
278			'sieve_port = '.$this->iSievePort,
279			'sieve_secure = "'.self::ConstConnectionSecurityTypeToStr($this->iSieveSecure).'"',
280			'smtp_host = "'.$this->encodeIniString($this->sOutHost).'"',
281			'smtp_port = '.$this->iOutPort,
282			'smtp_secure = "'.self::ConstConnectionSecurityTypeToStr($this->iOutSecure).'"',
283			'smtp_short_login = '.($this->bOutShortLogin ? 'On' : 'Off'),
284			'smtp_auth = '.($this->bOutAuth ? 'On' : 'Off'),
285			'smtp_php_mail = '.($this->bOutUsePhpMail ? 'On' : 'Off'),
286			'white_list = "'.$this->encodeIniString($this->sWhiteList).'"'
287		));
288	}
289
290	/**
291	 * @param string $sType
292	 *
293	 * @return int
294	 */
295	public static function StrConnectionSecurityTypeToCons($sType)
296	{
297		$iSecurityType = ConnectionSecurityType::NONE;
298		switch (strtoupper($sType))
299		{
300			case 'SSL':
301				$iSecurityType = ConnectionSecurityType::SSL;
302				break;
303			case 'TLS':
304				$iSecurityType = ConnectionSecurityType::STARTTLS;
305				break;
306		}
307		return $iSecurityType;
308	}
309
310	/**
311	 * @param int $iSecurityType
312	 *
313	 * @return string
314	 */
315	public static function ConstConnectionSecurityTypeToStr($iSecurityType)
316	{
317		$sType = 'None';
318		switch ($iSecurityType)
319		{
320			case ConnectionSecurityType::SSL:
321				$sType = 'SSL';
322				break;
323			case ConnectionSecurityType::STARTTLS:
324				$sType = 'TLS';
325				break;
326		}
327
328		return $sType;
329	}
330
331	/**
332	 * @param string $sIncHost
333	 * @param int $iIncPort
334	 * @param int $iIncSecure
335	 * @param bool $bIncShortLogin
336	 * @param bool $bUseSieve
337	 * @param string $sOutHost
338	 * @param int $iOutPort
339	 * @param int $iOutSecure
340	 * @param bool $bOutShortLogin
341	 * @param bool $bOutAuth
342	 * @param bool $bOutUsePhpMail = false
343	 * @param string $sWhiteList = ''
344	 *
345	 * @return \RainLoop\Model\Domain
346	 */
347	public function UpdateInstance(
348		$sIncHost, $iIncPort, $iIncSecure, $bIncShortLogin,
349		$bUseSieve, $sSieveHost, $iSievePort, $iSieveSecure,
350		$sOutHost, $iOutPort, $iOutSecure, $bOutShortLogin, $bOutAuth, $bOutUsePhpMail,
351		$sWhiteList = '')
352	{
353		$this->sIncHost = \MailSo\Base\Utils::IdnToAscii($sIncHost);
354		$this->iIncPort = $iIncPort;
355		$this->iIncSecure = $iIncSecure;
356		$this->bIncShortLogin = $bIncShortLogin;
357
358		$this->bUseSieve = $bUseSieve;
359		$this->sSieveHost = \MailSo\Base\Utils::IdnToAscii($sSieveHost);
360		$this->iSievePort = $iSievePort;
361		$this->iSieveSecure = $iSieveSecure;
362
363		$this->sOutHost = \MailSo\Base\Utils::IdnToAscii($sOutHost);
364		$this->iOutPort = $iOutPort;
365		$this->iOutSecure = $iOutSecure;
366		$this->bOutShortLogin = $bOutShortLogin;
367		$this->bOutAuth = $bOutAuth;
368		$this->bOutUsePhpMail = $bOutUsePhpMail;
369
370		$this->sWhiteList = \trim($sWhiteList);
371
372		return $this;
373	}
374
375	/**
376	 * @return string
377	 */
378	public function Name()
379	{
380		return $this->sName;
381	}
382
383	/**
384	 * @return string
385	 */
386	public function IncHost()
387	{
388		return $this->sIncHost;
389	}
390
391	/**
392	 * @return int
393	 */
394	public function IncPort()
395	{
396		return $this->iIncPort;
397	}
398
399	/**
400	 * @return int
401	 */
402	public function IncSecure()
403	{
404		return $this->iIncSecure;
405	}
406
407	/**
408	 * @return bool
409	 */
410	public function IncShortLogin()
411	{
412		return $this->bIncShortLogin;
413	}
414
415	/**
416	 * @return bool
417	 */
418	public function UseSieve()
419	{
420		return $this->bUseSieve;
421	}
422
423	/**
424	 * @return string
425	 */
426	public function SieveHost()
427	{
428		return $this->sSieveHost;
429	}
430
431	/**
432	 * @return int
433	 */
434	public function SievePort()
435	{
436		return $this->iSievePort;
437	}
438
439	/**
440	 * @return int
441	 */
442	public function SieveSecure()
443	{
444		return $this->iSieveSecure;
445	}
446
447	/**
448	 * @return bool
449	 */
450	public function SieveAllowRaw()
451	{
452		return $this->bSieveAllowRaw;
453	}
454
455	/**
456	 * @param bool $bSieveAllowRaw
457	 */
458	public function SetSieveAllowRaw($bSieveAllowRaw)
459	{
460		$this->bSieveAllowRaw = !!$bSieveAllowRaw;
461	}
462
463	/**
464	 * @return string
465	 */
466	public function OutHost()
467	{
468		return $this->sOutHost;
469	}
470
471	/**
472	 * @return int
473	 */
474	public function OutPort()
475	{
476		return $this->iOutPort;
477	}
478
479	/**
480	 * @return int
481	 */
482	public function OutSecure()
483	{
484		return $this->iOutSecure;
485	}
486
487	/**
488	 * @return bool
489	 */
490	public function OutShortLogin()
491	{
492		return $this->bOutShortLogin;
493	}
494
495	/**
496	 * @return bool
497	 */
498	public function OutAuth()
499	{
500		return $this->bOutAuth;
501	}
502
503	/**
504	 * @return bool
505	 */
506	public function OutUsePhpMail()
507	{
508		return $this->bOutUsePhpMail;
509	}
510
511	/**
512	 * @return string
513	 */
514	public function WhiteList()
515	{
516		return $this->sWhiteList;
517	}
518
519	/**
520	 * @return string
521	 */
522	public function AliasName()
523	{
524		return $this->sAliasName;
525	}
526
527	/**
528	 * @param string $sAliasName
529	 * @return self
530	 */
531	public function SetAliasName($sAliasName)
532	{
533		$this->sAliasName = $sAliasName;
534
535		return $this;
536	}
537
538	/**
539	 * @param string $sEmail
540	 * @param string $sLogin = ''
541	 *
542	 * @return bool
543	 */
544	public function ValidateWhiteList($sEmail, $sLogin = '')
545	{
546		$sW = \trim($this->sWhiteList);
547		if (0 < strlen($sW))
548		{
549			$sEmail = \MailSo\Base\Utils::IdnToUtf8($sEmail, true);
550			$sLogin = \MailSo\Base\Utils::IdnToUtf8($sLogin, true);
551
552			$sW = \preg_replace('/([^\s]+)@[^\s]*/', '$1', $sW);
553			$sW = ' '.\trim(\preg_replace('/[\s;,\r\n\t]+/', ' ', $sW)).' ';
554
555			$sUserPart = \MailSo\Base\Utils::GetAccountNameFromEmail(0 < \strlen($sLogin) ? $sLogin : $sEmail);
556			return false !== \stripos($sW, ' '.$sUserPart.' ');
557		}
558
559		return true;
560	}
561
562	/**
563	 * @param bool $bAjax = false
564	 *
565	 * @return array
566	 */
567	public function ToSimpleJSON($bAjax = false)
568	{
569		return array(
570			'Name' => $bAjax ? \MailSo\Base\Utils::IdnToUtf8($this->Name()) : $this->Name(),
571			'IncHost' => $bAjax ? \MailSo\Base\Utils::IdnToUtf8($this->IncHost()) : $this->IncHost(),
572			'IncPort' => $this->IncPort(),
573			'IncSecure' => $this->IncSecure(),
574			'IncShortLogin' => $this->IncShortLogin(),
575			'UseSieve' => $this->UseSieve(),
576			'SieveHost' => $bAjax ? \MailSo\Base\Utils::IdnToUtf8($this->SieveHost()) : $this->SieveHost(),
577			'SievePort' => $this->SievePort(),
578			'SieveSecure' => $this->SieveSecure(),
579			'SieveAllowRaw' => $this->SieveAllowRaw(),
580			'OutHost' => $bAjax ? \MailSo\Base\Utils::IdnToUtf8($this->OutHost()) : $this->OutHost(),
581			'OutPort' => $this->OutPort(),
582			'OutSecure' => $this->OutSecure(),
583			'OutShortLogin' => $this->OutShortLogin(),
584			'OutAuth' => $this->OutAuth(),
585			'OutUsePhpMail' => $this->OutUsePhpMail(),
586			'WhiteList' => $this->WhiteList(),
587			'AliasName' => $this->AliasName()
588		);
589	}
590}
591