1<?php
2
3namespace RainLoop;
4
5class Api
6{
7	/**
8	 * @return void
9	 */
10	private function __construct()
11	{
12	}
13
14	/**
15	 * @return bool
16	 */
17	public static function RunResult()
18	{
19		return true;
20	}
21
22	/**
23	 * @staticvar bool $bOne
24	 * @return bool
25	 */
26	public static function Handle()
27	{
28		static $bOne = null;
29		if (null === $bOne)
30		{
31			$bOne = \class_exists('MailSo\Version');
32			if ($bOne)
33			{
34				\RainLoop\Api::SetupDefaultMailSoConfig();
35
36				$bOne = \RainLoop\Api::RunResult();
37			}
38		}
39
40		return $bOne;
41	}
42
43	/**
44	 * @return \RainLoop\Actions
45	 */
46	public static function Actions()
47	{
48		static $oActions = null;
49		if (null === $oActions)
50		{
51			$oActions = \RainLoop\Actions::NewInstance();
52		}
53
54		return $oActions;
55	}
56
57	/**
58	 * @return \RainLoop\Application
59	 */
60	public static function Config()
61	{
62		return \RainLoop\Api::Actions()->Config();
63	}
64
65	/**
66	 * @return \MailSo\Log\Logger
67	 */
68	public static function Logger()
69	{
70		return \RainLoop\Api::Actions()->Logger();
71	}
72
73	/**
74	 * @return string
75	 */
76	public static function SetupDefaultMailSoConfig()
77	{
78		if (\class_exists('MailSo\Config'))
79		{
80			if (\RainLoop\Api::Config()->Get('labs', 'disable_iconv_if_mbstring_supported', false) &&
81				 \MailSo\Base\Utils::IsMbStringSupported() && \MailSo\Config::$MBSTRING)
82			{
83				\MailSo\Config::$ICONV = false;
84			}
85
86			\MailSo\Config::$MessageListFastSimpleSearch =
87				!!\RainLoop\Api::Config()->Get('labs', 'imap_message_list_fast_simple_search', true);
88
89			\MailSo\Config::$MessageListCountLimitTrigger =
90				(int) \RainLoop\Api::Config()->Get('labs', 'imap_message_list_count_limit_trigger', 0);
91
92			\MailSo\Config::$MessageListDateFilter =
93				(int) \RainLoop\Api::Config()->Get('labs', 'imap_message_list_date_filter', 0);
94
95			\MailSo\Config::$MessageListPermanentFilter =
96				\trim(\RainLoop\Api::Config()->Get('labs', 'imap_message_list_permanent_filter', ''));
97
98			\MailSo\Config::$MessageAllHeaders =
99				!!\RainLoop\Api::Config()->Get('labs', 'imap_message_all_headers', false);
100
101			\MailSo\Config::$LargeThreadLimit =
102				(int) \RainLoop\Api::Config()->Get('labs', 'imap_large_thread_limit', 50);
103
104			\MailSo\Config::$ImapTimeout =
105				(int) \RainLoop\Api::Config()->Get('labs', 'imap_timeout', 300);
106
107			\MailSo\Config::$BoundaryPrefix = '_RainLoop_';
108
109			\MailSo\Config::$SystemLogger = \RainLoop\Api::Logger();
110
111			$sSslCafile = \RainLoop\Api::Config()->Get('ssl', 'cafile', '');
112			$sSslCapath = \RainLoop\Api::Config()->Get('ssl', 'capath', '');
113
114			\RainLoop\Utils::$CookieDefaultPath = \RainLoop\Api::Config()->Get('labs', 'cookie_default_path', '');
115			if (\RainLoop\Api::Config()->Get('labs', 'cookie_default_secure', false))
116			{
117				\RainLoop\Utils::$CookieDefaultSecure = true;
118			}
119
120			if (!empty($sSslCafile) || !empty($sSslCapath))
121			{
122				\MailSo\Hooks::Add('Net.NetClient.StreamContextSettings/Filter', function ($aStreamContextSettings) use ($sSslCafile, $sSslCapath) {
123					if (isset($aStreamContextSettings['ssl']) && \is_array($aStreamContextSettings['ssl']))
124					{
125						if (empty($aStreamContextSettings['ssl']['cafile']) && !empty($sSslCafile))
126						{
127							$aStreamContextSettings['ssl']['cafile'] = $sSslCafile;
128						}
129
130						if (empty($aStreamContextSettings['ssl']['capath']) && !empty($sSslCapath))
131						{
132							$aStreamContextSettings['ssl']['capath'] = $sSslCapath;
133						}
134					}
135				});
136			}
137
138			\MailSo\Config::$HtmlStrictDebug = !!\RainLoop\Api::Config()->Get('debug', 'enable', false);
139
140			\MailSo\Config::$CheckNewMessages = !!\RainLoop\Api::Config()->Get('labs', 'check_new_messages', true);
141
142			if (\RainLoop\Api::Config()->Get('labs', 'strict_html_parser', true))
143			{
144				\MailSo\Config::$HtmlStrictAllowedAttributes = array(
145					// rainloop
146					'data-wrp',
147					// defaults
148					'name',
149					'dir', 'lang', 'style', 'title',
150					'background', 'bgcolor', 'alt', 'height', 'width', 'src', 'href',
151					'border', 'bordercolor', 'charset', 'direction', 'language',
152					// a
153					'coords', 'download', 'hreflang', 'shape',
154					// body
155					'alink', 'bgproperties', 'bottommargin', 'leftmargin', 'link', 'rightmargin', 'text', 'topmargin', 'vlink',
156					'marginwidth', 'marginheight', 'offset',
157					// button,
158					'disabled', 'type', 'value',
159					// col
160					'align', 'valign',
161					// font
162					'color', 'face', 'size',
163					// form
164					'novalidate',
165					// hr
166					'noshade',
167					// img
168					'hspace', 'sizes', 'srcset', 'vspace', 'usemap',
169					// input, textarea
170					'checked', 'max', 'min', 'maxlength', 'multiple', 'pattern', 'placeholder', 'readonly', 'required', 'step', 'wrap',
171					// label
172					'for',
173					// meter
174					'low', 'high', 'optimum',
175					// ol
176					'reversed', 'start',
177					// option
178					'selected', 'label',
179					// table
180					'cols', 'rows', 'frame', 'rules', 'summary', 'cellpadding', 'cellspacing',
181					// td
182					'abbr', 'axis', 'colspan', 'rowspan', 'headers', 'nowrap'
183				);
184			}
185		}
186	}
187
188	/**
189	 * @return string
190	 */
191	public static function Version()
192	{
193		return APP_VERSION;
194	}
195
196	/**
197	 * @param string $sEmail
198	 * @param string $sPassword
199	 * @param array $aAdditionalOptions = array()
200	 * @param bool $bUseTimeout = true
201	 *
202	 * @return string
203	 */
204	public static function GetUserSsoHash($sEmail, $sPassword, $aAdditionalOptions = array(), $bUseTimeout = true)
205	{
206		$sSsoHash = \MailSo\Base\Utils::Sha1Rand(\md5($sEmail).\md5($sPassword));
207
208		return \RainLoop\Api::Actions()->Cacher()->Set(\RainLoop\KeyPathHelper::SsoCacherKey($sSsoHash),
209			\RainLoop\Utils::EncodeKeyValuesQ(array(
210				'Email' => $sEmail,
211				'Password' => $sPassword,
212				'AdditionalOptions' => $aAdditionalOptions,
213				'Time' => $bUseTimeout ? \time() : 0
214			))) ? $sSsoHash : '';
215	}
216
217	/**
218	 * @param string $sSsoHash
219	 *
220	 * @return bool
221	 */
222	public static function ClearUserSsoHash($sSsoHash)
223	{
224		return \RainLoop\Api::Actions()->Cacher()->Delete(\RainLoop\KeyPathHelper::SsoCacherKey($sSsoHash));
225	}
226
227	/**
228	 * @param string $sEmail
229	 *
230	 * @return bool
231	 */
232	public static function ClearUserData($sEmail)
233	{
234		if (0 < \strlen($sEmail))
235		{
236			$sEmail = \MailSo\Base\Utils::IdnToAscii($sEmail);
237
238			$oStorageProvider = \RainLoop\Api::Actions()->StorageProvider();
239			if ($oStorageProvider && $oStorageProvider->IsActive())
240			{
241				$oStorageProvider->DeleteStorage($sEmail);
242			}
243
244			if (\RainLoop\Api::Actions()->AddressBookProvider() &&
245				\RainLoop\Api::Actions()->AddressBookProvider()->IsActive())
246			{
247				\RainLoop\Api::Actions()->AddressBookProvider()->DeleteAllContacts($sEmail);
248			}
249
250			return true;
251		}
252
253		return false;
254	}
255
256	/**
257	 * @return bool
258	 */
259	public static function LogoutCurrentLogginedUser()
260	{
261		\RainLoop\Utils::ClearCookie('rlsession');
262		return true;
263	}
264
265	/**
266	 * @return void
267	 */
268	public static function ExitOnEnd()
269	{
270		if (!\defined('RAINLOOP_EXIT_ON_END'))
271		{
272			\define('RAINLOOP_EXIT_ON_END', true);
273		}
274	}
275}
276