source: branches/2.5/prototype/services/iCal.php @ 7801

Revision 7801, 117.4 KB checked in by douglas, 11 years ago (diff)

Ticket #3326 - Erro na criacao de .ics de tarefas impossibilitando a edicao de tarefas

RevLine 
[5341]1<?php
2
[7660]3//require_once ROOTPATH . '/plugins/icalcreator/iCalUtilityFunctions.class.php';
[6066]4require_once ROOTPATH . '/plugins/icalcreator/iCalcreator.class.php';
5require_once ROOTPATH . '/modules/calendar/constants.php';
6
[6528]7use prototype\api\Config as Config;
8
[5341]9//TODO:Timeout request
[6066]10set_time_limit(600);
11
12class iCal implements Formatter {
13
14    static $timezonesMap = array('(GMT-12.00) International Date Line West' => 'Etc/GMT+12', '(GMT-11.00) Midway Island / Samoa' => 'Pacific/Midway', '(GMT-10.00) Hawaii' => 'Pacific/Honolulu', '(GMT-09.00) Alaska' => 'America/Anchorage', '(GMT-08.00) Pacific Time (US & Canada) / Tijuana' => 'America/Los_Angeles', '(GMT-08.00) Tijuana / Baja California' => 'America/Tijuana', '(GMT-07.00) Arizona' => 'America/Phoenix', '(GMT-07.00) Chihuahua / La Paz / Mazatlan - Old' => 'America/Chihuahua', '(GMT-07.00) Mountain Time (US & Canada)' => 'America/Denver', '(GMT-06.00) Central America' => 'America/Guatemala', '(GMT-06.00) Central Time (US & Canada)' => 'America/Chicago', '(GMT-06.00) Guadalajara / Mexico City / Monterrey - Old' => 'America/Mexico_City', '(GMT-06.00) Saskatchewan' => 'America/Regina', '(GMT-05.00) Bogota / Lima / Quito' => 'America/Bogota', '(GMT-05.00) Eastern Time (US & Canada)' => 'America/New_York', '(GMT-05.00) Indiana (East)' => 'America/Indiana/Indianapolis', '(GMT-04.30) Caracas' => 'America/Caracas', '(GMT-04.00) Atlantic Time (Canada)' => 'America/Halifax', '(GMT-04.00) Georgetown' => 'America/Guyana', '(GMT-04.00) Caracas / La Paz' => 'America/La_Paz', '(GMT-04.00) Manaus' => 'America/Manaus', '(GMT-04.00) Santiago' => 'America/Santiago', '(GMT-03.30) Newfoundland' => 'America/St_Johns', '(GMT-03.00) Brasilia' => 'America/Sao_Paulo', 'GMT -0300 (Standard) / GMT -0200 (Daylight)' => 'America/Sao_Paulo', '(GMT-03.00) Buenos Aires / Georgetown' => 'America/Argentina/Buenos_Aires', '(GMT-03.00) Greenland' => 'America/Godthab', '(GMT-03.00) Montevideo' => 'America/Montevideo', '(GMT-02.00) Mid-Atlantic' => 'Atlantic/South_Georgia', '(GMT-01.00) Azores' => 'Atlantic/Azores', '(GMT-01.00) Cape Verde Is.' => 'Atlantic/Cape_Verde', '(GMT) Casablanca' => 'Africa/Casablanca', '(GMT) Greenwich Mean Time - Dublin / Edinburgh / Lisbon / London' => 'Europe/London', '(GMT) Casablanca / Monrovia' => 'Africa/Monrovia', '(GMT+01.00) Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna' => 'Europe/Berlin', '(GMT+01.00) Belgrade / Bratislava / Budapest / Ljubljana / Prague' => 'Europe/Belgrade', '(GMT+01.00) Brussels / Copenhagen / Madrid / Paris' => 'Europe/Brussels', '(GMT+01.00) Sarajevo / Skopje / Warsaw / Zagreb' => 'Europe/Warsaw', '(GMT+01.00) West Central Africa' => 'Africa/Algiers', '(GMT+02.00) Windhoek' => 'Africa/Windhoek', '(GMT+02.00) Amman' => 'Asia/Amman', '(GMT+02.00) Bucharest' => 'Europe/Athens', '(GMT+02.00) Beirut' => 'Asia/Beirut', '(GMT+02.00) Cairo' => 'Africa/Cairo', '(GMT+02.00) Harare / Pretoria' => 'Africa/Harare', '(GMT+02.00) Helsinki / Kyiv / Riga / Sofia / Tallinn / Vilnius' => 'Europe/Helsinki', '(GMT+02.00) Jerusalem' => 'Asia/Jerusalem', '(GMT+02.00) Minsk' => 'Europe/Minsk', '(GMT+03.00) Baghdad' => 'Asia/Baghdad', '(GMT+03.00) Kuwait / Riyadh' => 'Asia/Kuwait', '(GMT+03.00) Moscow / St. Petersburg / Volgograd' => 'Europe/Moscow', '(GMT+03.00) Nairobi' => 'Africa/Nairobi', '(GMT+04.00) Caucasus Standard Time' => 'Asia/Tbilisi', '(GMT+03.30) Tehran' => 'Asia/Tehran', '(GMT+04.00) Abu Dhabi / Muscat' => 'Asia/Muscat', '(GMT+04.00) Baku / Tbilisi / Yerevan' => 'Asia/Baku', '(GMT+04.00) Yerevan' => 'Asia/Yerevan', '(GMT+04.30) Kabul' => 'Asia/Kabul', '(GMT+05.00) Ekaterinburg' => 'Asia/Yekaterinburg', '(GMT+05.00) Islamabad / Karachi / Tashkent' => 'Asia/Karachi', '(GMT+05.00) Tashkent' => 'Asia/Tashkent', '(GMT+05.30) Chennai / Kolkata / Mumbai / New Delhi' => 'Asia/Kolkata', '(GMT+06.00) Sri Jayawardenepura' => 'Asia/Colombo', '(GMT+05.45) Kathmandu' => 'Asia/Katmandu', '(GMT+06.00) Almaty / Novosibirsk' => 'Asia/Novosibirsk', '(GMT+06.00) Astana / Dhaka' => 'Asia/Dhaka', '(GMT+06.30) Rangoon' => 'Asia/Rangoon', '(GMT+07.00) Bangkok / Hanoi / Jakarta' => 'Asia/Bangkok', '(GMT+07.00) Krasnoyarsk' => 'Asia/Krasnoyarsk', '(GMT+08.00) Beijing / Chongqing / Hong Kong / Urumqi' => 'Asia/Hong_Kong', '(GMT+08.00) Irkutsk / Ulaan Bataar' => 'Asia/Irkutsk', '(GMT+08.00) Kuala Lumpur / Singapore' => 'Asia/Kuala_Lumpur', '(GMT+08.00) Perth' => 'Australia/Perth', '(GMT+08.00) Taipei' => 'Asia/Taipei', '(GMT+09.00) Osaka / Sapporo / Tokyo' => 'Asia/Tokyo', '(GMT+09.00) Seoul' => 'Asia/Seoul', '(GMT+09.00) Yakutsk' => 'Asia/Yakutsk', '(GMT+09.30) Adelaide' => 'Australia/Adelaide', '(GMT+09.30) Darwin' => 'Australia/Darwin', '(GMT+10.00) Brisbane' => 'Australia/Brisbane', '(GMT+10.00) Canberra / Melbourne / Sydney' => 'Australia/Sydney', '(GMT+10.00) Guam / Port Moresby' => 'Pacific/Guam', '(GMT+10.00) Hobart' => 'Australia/Hobart', '(GMT+10.00) Vladivostok' => 'Asia/Vladivostok', '(GMT+11.00) Magadan / Solomon Is. / New Caledonia' => 'Asia/Magadan', '(GMT+12.00) Auckland / Wellington' => 'Pacific/Auckland', '(GMT+12.00) Fiji / Kamchatka / Marshall Is.' => 'Pacific/Fiji', '(GMT+13.00) Nuku\'alofa' => 'Pacific/Tongatapu', 'E. South America Standard Time' => 'America/Sao_Paulo', 'E. South America' => 'America/Sao_Paulo');
[5341]15//    static $timezonesOutlookID = array('Europe/London' => '1' ,'Europe/Brussels' => '3' ,'Europe/Berlin' => '4' ,'America/New_York' => '5' ,'Europe/Belgrade' => '6' ,'Europe/Minsk' => '7' ,'America/Sao_Paulo' => '8' ,'America/Halifax' => '9' ,'America/New_York' => '10' ,'America/Chicago' => '11' ,'America/Denver' => '12' ,'America/Los_Angeles' => '13' ,'America/Anchorage' => '14' ,'Pacific/Honolulu' => '15' ,'Pacific/Midway' => '16' ,'Pacific/Auckland' => '17' ,'Australia/Brisbane' => '18' ,'Australia/Adelaide' => '19' ,'Asia/Tokyo' => '20' ,'Asia/Hong_Kong' => '21' ,'Asia/Bangkok' => '22' ,'Asia/Kolkata' => '23' ,'Asia/Muscat' => '24' ,'Asia/Tehran' => '25' ,'Asia/Baghdad' => '26' ,'Asia/Jerusalem' => '27' ,'America/St_Johns' => '28' ,'Atlantic/Azores' => '29' ,'Atlantic/South_Georgia' => '30' ,'Africa/Casablanca' => '31' ,'America/La_Paz' => '33' ,'America/Indiana/Indianapolis' => '34' ,'America/Bogota' => '35' ,'America/Regina' => '36' ,'America/Mexico_City' => '37' ,'America/Phoenix' => '38' ,'Etc/GMT+12' => '39' ,'Pacific/Fiji' => '40' ,'Asia/Magadan' => '41' ,'Australia/Hobart' => '42' ,'Pacific/Guam' => '43' ,'Australia/Darwin' => '44' ,'Asia/Hong_Kong' => '45' ,'Asia/Novosibirsk' => '46' ,'Asia/Kabul' => '48' ,'Africa/Cairo' => '49' ,'Africa/Harare' => '50' ,'Europe/Moscow' => '51' ,'Australia/Sydney' => '52' ,'Australia/Sydney' => '53' ,'Australia/Adelaide' => '54' ,'Australia/Hobart' => '55' ,'America/Santiago' => '56' ,'Australia/Pert' => '57' ,'America/Tijuana' => '59' ,'Asia/Tbilisi' => '60' ,'Australia/Sydney' => '61' ,'America/Caracas' => '62' ,'Asia/Amman' => '63' ,'Asia/Baku' => '64' ,'Asia/Yerevan' => '65' ,'Europe/Moscow' => '66' ,'America/Argentina/Buenos_Aires' => '67' ,'America/Montevideo' => '72'); 
[6066]16    static $suportedTimzones = array('Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara', 'Africa/Asmera', 'Africa/Bamako', 'Africa/Bangui', 'Africa/Banjul', 'Africa/Bissau', 'Africa/Blantyre', 'Africa/Brazzaville', 'Africa/Bujumbura', 'Africa/Cairo', 'Africa/Casablanca', 'Africa/Ceuta', 'Africa/Conakry', 'Africa/Dakar', 'Africa/Dar_es_Salaam', 'Africa/Djibouti', 'Africa/Douala', 'Africa/El_Aaiun', 'Africa/Freetown', 'Africa/Gaborone', 'Africa/Harare', 'Africa/Johannesburg', 'Africa/Kampala', 'Africa/Khartoum', 'Africa/Kigali', 'Africa/Kinshasa', 'Africa/Lagos', 'Africa/Libreville', 'Africa/Lome', 'Africa/Luanda', 'Africa/Lubumbashi', 'Africa/Lusaka', 'Africa/Malabo', 'Africa/Maputo', 'Africa/Maseru', 'Africa/Mbabane', 'Africa/Mogadishu', 'Africa/Monrovia', 'Africa/Nairobi', 'Africa/Ndjamena', 'Africa/Niamey', 'Africa/Nouakchott', 'Africa/Ouagadougou', 'Africa/Porto-Novo', 'Africa/Sao_Tome', 'Africa/Timbuktu', 'Africa/Tripoli', 'Africa/Tunis', 'Africa/Windhoek', 'America/Adak', 'America/Anchorage', 'America/Anguilla', 'America/Antigua', 'America/Araguaina', 'America/Argentina/Buenos_Aires', 'America/Argentina/Catamarca', 'America/Argentina/ComodRivadavia', 'America/Argentina/Cordoba', 'America/Argentina/Jujuy', 'America/Argentina/La_Rioja', 'America/Argentina/Mendoza', 'America/Argentina/Rio_Gallegos', 'America/Argentina/Salta', 'America/Argentina/San_Juan', 'America/Argentina/San_Luis', 'America/Argentina/Tucuman', 'America/Argentina/Ushuaia', 'America/Aruba', 'America/Asuncion', 'America/Atikokan', 'America/Atka', 'America/Bahia', 'America/Barbados', 'America/Belem', 'America/Belize', 'America/Blanc-Sablon', 'America/Boa_Vista', 'America/Bogota', 'America/Boise', 'America/Buenos_Aires', 'America/Cambridge_Bay', 'America/Campo_Grande', 'America/Cancun', 'America/Caracas', 'America/Catamarca', 'America/Cayenne', 'America/Cayman', 'America/Chicago', 'America/Chihuahua', 'America/Coral_Harbour', 'America/Cordoba', 'America/Costa_Rica', 'America/Cuiaba', 'America/Curacao', 'America/Danmarkshavn', 'America/Dawson_Creek', 'America/Dawson', 'America/Denver', 'America/Detroit', 'America/Dominica', 'America/Edmonton', 'America/Eirunepe', 'America/El_Salvador', 'America/Ensenada', 'America/Fort_Wayne', 'America/Fortaleza', 'America/Glace_Bay', 'America/Godthab', 'America/Goose_Bay', 'America/Grand_Turk', 'America/Grenada', 'America/Guadeloupe', 'America/Guatemala', 'America/Guayaquil', 'America/Guyana', 'America/Halifax', 'America/Havana', 'America/Hermosillo', 'America/Indiana/Indianapolis', 'America/Indiana/Knox', 'America/Indiana/Marengo', 'America/Indiana/Petersburg', 'America/Indiana/Tell_City', 'America/Indiana/Vevay', 'America/Indiana/Vincennes', 'America/Indiana/Winamac', 'America/Indianapolis', 'America/Inuvik', 'America/Iqaluit', 'America/Jamaica', 'America/Jujuy', 'America/Juneau', 'America/Kentucky/Louisville', 'America/Kentucky/Monticello', 'America/Knox_IN', 'America/La_Paz', 'America/Lima', 'America/Los_Angeles', 'America/Louisville', 'America/Maceio', 'America/Managua', 'America/Manaus', 'America/Marigot', 'America/Martinique', 'America/Matamoros', 'America/Mazatlan', 'America/Mendoza', 'America/Menominee', 'America/Merida', 'America/Mexico_City', 'America/Miquelon', 'America/Moncton', 'America/Monterrey', 'America/Montevideo', 'America/Montreal', 'America/Montserrat', 'America/Nassau', 'America/New_York', 'America/Nipigon', 'America/Nome', 'America/Noronha', 'America/North_Dakota/Center', 'America/North_Dakota/New_Salem', 'America/Ojinaga', 'America/Panama', 'America/Pangnirtung', 'America/Paramaribo', 'America/Phoenix', 'America/Port_of_Spain', 'America/Port-au-Prince', 'America/Porto_Acre', 'America/Porto_Velho', 'America/Puerto_Rico', 'America/Rainy_River', 'America/Rankin_Inlet', 'America/Recife', 'America/Regina', 'America/Resolute', 'America/Rio_Branco', 'America/Rosario', 'America/Santa_Isabel', 'America/Santarem', 'America/Santiago', 'America/Santo_Domingo', 'America/Sao_Paulo', 'America/Scoresbysund', 'America/Shiprock', 'America/St_Barthelemy', 'America/St_Johns', 'America/St_Kitts', 'America/St_Lucia', 'America/St_Thomas', 'America/St_Vincent', 'America/Swift_Current', 'America/Tegucigalpa', 'America/Thule', 'America/Thunder_Bay', 'America/Tijuana', 'America/Toronto', 'America/Tortola', 'America/Vancouver', 'America/Virgin', 'America/Whitehorse', 'America/Winnipeg', 'America/Yakutat', 'America/Yellowknife', 'Antarctica/Casey', 'Antarctica/Davis', 'Antarctica/DumontDUrville', 'Antarctica/Macquarie', 'Antarctica/Mawson', 'Antarctica/McMurdo', 'Antarctica/Palmer', 'Antarctica/Rothera', 'Antarctica/South_Pole', 'Antarctica/Syowa', 'Antarctica/Vostok', 'Arctic/Longyearbyen', 'Asia/Aden', 'Asia/Almaty', 'Asia/Amman', 'Asia/Anadyr', 'Asia/Aqtau', 'Asia/Aqtobe', 'Asia/Ashgabat', 'Asia/Ashkhabad', 'Asia/Baghdad', 'Asia/Bahrain', 'Asia/Baku', 'Asia/Bangkok', 'Asia/Beirut', 'Asia/Bishkek', 'Asia/Brunei', 'Asia/Calcutta', 'Asia/Choibalsan', 'Asia/Chongqing', 'Asia/Chungking', 'Asia/Colombo', 'Asia/Dacca', 'Asia/Damascus', 'Asia/Dhaka', 'Asia/Dili', 'Asia/Dubai', 'Asia/Dushanbe', 'Asia/Gaza', 'Asia/Harbin', 'Asia/Ho_Chi_Minh', 'Asia/Hong_Kong', 'Asia/Hovd', 'Asia/Irkutsk', 'Asia/Istanbul', 'Asia/Jakarta', 'Asia/Jayapura', 'Asia/Jerusalem', 'Asia/Kabul', 'Asia/Kamchatka', 'Asia/Karachi', 'Asia/Kashgar', 'Asia/Kathmandu', 'Asia/Katmandu', 'Asia/Kolkata', 'Asia/Krasnoyarsk', 'Asia/Kuala_Lumpur', 'Asia/Kuching', 'Asia/Kuwait', 'Asia/Macao', 'Asia/Macau', 'Asia/Magadan', 'Asia/Makassar', 'Asia/Manila', 'Asia/Muscat', 'Asia/Nicosia', 'Asia/Novokuznetsk', 'Asia/Novosibirsk', 'Asia/Omsk', 'Asia/Oral', 'Asia/Phnom_Penh', 'Asia/Pontianak', 'Asia/Pyongyang', 'Asia/Qatar', 'Asia/Qyzylorda', 'Asia/Rangoon', 'Asia/Riyadh', 'Asia/Saigon', 'Asia/Sakhalin', 'Asia/Samarkand', 'Asia/Seoul', 'Asia/Shanghai', 'Asia/Singapore', 'Asia/Taipei', 'Asia/Tashkent', 'Asia/Tbilisi', 'Asia/Tehran', 'Asia/Tel_Aviv', 'Asia/Thimbu', 'Asia/Thimphu', 'Asia/Tokyo', 'Asia/Ujung_Pandang', 'Asia/Ulaanbaatar', 'Asia/Ulan_Bator', 'Asia/Urumqi', 'Asia/Vientiane', 'Asia/Vladivostok', 'Asia/Yakutsk', 'Asia/Yekaterinburg', 'Asia/Yerevan', 'Atlantic/Azores', 'Atlantic/Bermuda', 'Atlantic/Canary', 'Atlantic/Cape_Verde', 'Atlantic/Faeroe', 'Atlantic/Faroe', 'Atlantic/Jan_Mayen', 'Atlantic/Madeira', 'Atlantic/Reykjavik', 'Atlantic/South_Georgia', 'Atlantic/St_Helena', 'Atlantic/Stanley', 'Australia/ACT', 'Australia/Adelaide', 'Australia/Brisbane', 'Australia/Broken_Hill', 'Australia/Canberra', 'Australia/Currie', 'Australia/Darwin', 'Australia/Eucla', 'Australia/Hobart', 'Australia/LHI', 'Australia/Lindeman', 'Australia/Lord_Howe', 'Australia/Melbourne', 'Australia/NSW', 'Australia/North', 'Australia/Perth', 'Australia/Queensland', 'Australia/South', 'Australia/Sydney', 'Australia/Tasmania', 'Australia/Victoria', 'Australia/West', 'Australia/Yancowinna', 'Europe/Amsterdam', 'Europe/Andorra', 'Europe/Athens', 'Europe/Belfast', 'Europe/Belgrade', 'Europe/Berlin', 'Europe/Bratislava', 'Europe/Brussels', 'Europe/Bucharest', 'Europe/Budapest', 'Europe/Chisinau', 'Europe/Copenhagen', 'Europe/Dublin', 'Europe/Gibraltar', 'Europe/Guernsey', 'Europe/Helsinki', 'Europe/Isle_of_Man', 'Europe/Istanbul', 'Europe/Jersey', 'Europe/Kaliningrad', 'Europe/Kiev', 'Europe/Lisbon', 'Europe/Ljubljana', 'Europe/London', 'Europe/Luxembourg', 'Europe/Madrid', 'Europe/Malta', 'Europe/Mariehamn', 'Europe/Minsk', 'Europe/Monaco', 'Europe/Moscow', 'Europe/Nicosia', 'Europe/Oslo', 'Europe/Paris', 'Europe/Podgorica', 'Europe/Prague', 'Europe/Riga', 'Europe/Rome', 'Europe/Samara', 'Europe/San_Marino', 'Europe/Sarajevo', 'Europe/Simferopol', 'Europe/Skopje', 'Europe/Sofia', 'Europe/Stockholm', 'Europe/Tallinn', 'Europe/Tirane', 'Europe/Tiraspol', 'Europe/Uzhgorod', 'Europe/Vaduz', 'Europe/Vatican', 'Europe/Vienna', 'Europe/Vilnius', 'Europe/Volgograd', 'Europe/Warsaw', 'Europe/Zagreb', 'Europe/Zaporozhye', 'Europe/Zurich', 'Indian/Antananarivo', 'Indian/Chagos', 'Indian/Christmas', 'Indian/Cocos', 'Indian/Comoro', 'Indian/Kerguelen', 'Indian/Mahe', 'Indian/Maldives', 'Indian/Mauritius', 'Indian/Mayotte', 'Indian/Reunion', 'Pacific/Apia', 'Pacific/Auckland', 'Pacific/Chatham', 'Pacific/Easter', 'Pacific/Efate', 'Pacific/Enderbury', 'Pacific/Fakaofo', 'Pacific/Fiji', 'Pacific/Funafuti', 'Pacific/Galapagos', 'Pacific/Gambier', 'Pacific/Guadalcanal', 'Pacific/Guam', 'Pacific/Honolulu', 'Pacific/Johnston', 'Pacific/Kiritimati', 'Pacific/Kosrae', 'Pacific/Kwajalein', 'Pacific/Majuro', 'Pacific/Marquesas', 'Pacific/Midway', 'Pacific/Nauru', 'Pacific/Niue', 'Pacific/Norfolk', 'Pacific/Noumea', 'Pacific/Pago_Pago', 'Pacific/Palau', 'Pacific/Pitcairn', 'Pacific/Ponape', 'Pacific/Port_Moresby', 'Pacific/Rarotonga', 'Pacific/Saipan', 'Pacific/Samoa', 'Pacific/Tahiti', 'Pacific/Tarawa', 'Pacific/Tongatapu', 'Pacific/Truk', 'Pacific/Wake', 'Pacific/Wallis', 'Pacific/Yap', 'UTC');
[5341]17
[6791]18    public function format($data, $params = false)
19    {
20                $timezones = array_flip(self::$timezonesMap);
21                $sytemTimezone = (date_default_timezone_get()) ? date_default_timezone_get() : 'America/Sao_Paulo';
22                $params['defaultTZI'] = self::nomalizeTZID((isset($params['defaultTZI']) && $params['defaultTZI'] != 'null') ? $params['defaultTZI'] : $sytemTimezone );
23                $params['X-WR-TIMEZONE'] = isset($timezones[$params['defaultTZI']]) ? $timezones[$params['defaultTZI']] : $params['defaultTZI'];
24               
25                return (isset($params['compatible']) && $params['compatible']) ?
26                                        array('ical' => $this->createIcal($data, $params) , 'compatible' => $this->createCompatibleIcal($data, $params)) :
27                                        $this->createIcal($data, $params);
[6011]28
[6791]29    }
[5341]30
[6791]31    protected function createIcal($data, $params = false )
32    {
[7799]33        $ical = new icalCreatorVcalendar();
[6791]34                $ical->setProperty('method', isset($params['method']) ? $params['method'] : 'PUBLISH' );
[5341]35
[6791]36                /*
37                 * Seta propiedades obrigatorias para alguns softwares (Outlook)
38                 */
39                $ical->setProperty('x-wr-calname', 'Calendar Expresso');
40                $ical->setProperty('X-WR-CALDESC', 'Calendar Expresso');
41                $ical->setProperty('X-WR-TIMEZONE', $params['X-WR-TIMEZONE']);
[5341]42
[6791]43                foreach ($data as $i => $v) {
[5341]44
[6791]45                    switch ($v['type']) {
46                        case EVENT_ID:
47                            $vevent = $ical->newComponent('vevent');
[6066]48
[6791]49                            $vevent->setProperty('summary', $v['summary']);
50                            $vevent->setProperty('description', isset($v['description']) ? $v['description'] : '');
51                            $vevent->setProperty('location', $v['location']);
52                            $vevent->setProperty('tranp', (isset($v['tranparent']) && $v['tranparent'] == TRANSP_TRANSPARENT ) ? 'TRANSPARENT' : 'OPAQUE' );
[6066]53
[6791]54                            $timezone = new DateTimeZone('UTC');
55                            $apTimezone = self::nomalizeTZID(( isset($v['timezone']) && $v['timezone'] != 'null' ) ? $v['timezone'] : $params['defaultTZI']);
56                            $apTimezoneOBJ = new DateTimeZone($apTimezone);
[6066]57
[6791]58                            $sTime = new DateTime('@' . (int) ($v['startTime'] / 1000), $timezone);
59                            $sTime->setTimezone($apTimezoneOBJ);
[6066]60
[7243]61                $eTime = new DateTime('@' . (int) ($v['endTime'] / 1000), $timezone);
62                $eTime->setTimezone($apTimezoneOBJ);
63
[6791]64                            if (( isset($v['repeat']) ) && ( isset($v['repeat']['frequency']) && $v['repeat']['frequency'] && $v['repeat']['frequency'] != 'none' ))
65                                    $vevent->setProperty('rrule', $this->formatIcalRepeat($v['repeat']));
[6066]66
[7702]67                $vevent->setProperty('dtstamp', array('timestamp' => ($v['dtstamp'] / 1000) ));
68
[7068]69                            if (isset($v['allDay']) && $v['allDay'] == 1)
70                {
71                    $vevent->setProperty('dtstart', $sTime->format(DATE_RFC822), array("VALUE" => "DATE"));
72                    $vevent->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'TRUE');
[7243]73                    $vevent->setProperty('dtend', $eTime->format(DATE_RFC822), array("VALUE" => "DATE"));
74                    $vevent->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'TRUE');
[7068]75                            } else
76                {
77                    $vevent->setProperty('dtstart', $sTime->format(DATE_RFC822), array('TZID' => $apTimezone));
78                    $vevent->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'FALSE');
[7243]79                    $vevent->setProperty('dtend', $eTime->format(DATE_RFC822), array('TZID' => $apTimezone));
80                    $vevent->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'FALSE');
[6066]81                            }
[6791]82                                           
83                            if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
84                                $participants = $v['participants'];
85                            else
86                                        $participants = Controller::find(array('concept' => 'participant'), false, array('filter' => array('=', 'schedulable', $v['id'])));
87                           
88                                if (is_array($participants) && count($participants) > 0)
89                                        foreach ($participants as $ii => $vv) {
90                                       
91                                                if(isset($participants[$ii]['user']) && !is_array($participants[$ii]['user']))
92                                                {
93                                                        if ($vv['isExternal'] == 1)
94                                                                $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user'], 'service' => 'PostgreSQL'));
95                                                        else
96                                                                $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user']));
97                                                }
98                                       
99                                                if ($participants[$ii]['user']['id'] == Config::me('uidNumber'))
100                                                {
101                                                        $alarms = (isset($participants[$ii]['alarms'])) ? $participants[$ii]['alarms'] : Controller::find(array('concept' => 'alarm'), null, array('filter' => array('AND', array('=', 'participant', $vv['id']), array('=', 'schedulable', $v['id']))));
102                                                        if(is_array($alarms))
103                                                                self::createAlarms($alarms, $vevent);
104                                                }
105                                       
106                                        }
107                           
108                            if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
109                                        $this->createAttendee($v['participants'], $vevent);
[6066]110
[6791]111                            if (isset($v['attachments']) && is_array($v['attachments']) && count($v['attachments']) > 0)
112                                        $this->createAttachment($v['attachments'], $vevent);
113
[7579]114                            $vevent->setProperty('uid', $v['uid']);
115
116                $timezoneDayligth = Controller::read(array('concept' => 'timezones'), null, array('filter' => array('=', 'tzid', $apTimezone)));
117
118                if(!empty( $timezoneDayligth ) && count( $timezoneDayligth ) > 0){
119
120                    if(array_key_exists(0, $timezoneDayligth))
121                        $timezoneDayligth = $timezoneDayligth[0];
122
123                    date_default_timezone_set('UTC');
124
125                    require_once ROOTPATH . '/plugins/when/When.php';
126
127                    $r = new When();
128
129                    $start = new DateTime('1970-01-01 '.$timezoneDayligth['standardDtstart']);
130
131                    $r = new When();
132                    $r->recur($start, $timezoneDayligth['standardFrequency'])
133                        ->until($start->modify('+1 years'))
134                        ->bymonth(array( $timezoneDayligth['standardBymonth'] ))
135                        ->byday(array(  $timezoneDayligth['daylightByday'] ));
136
137                   $date = $r->next();
138
[7660]139                    $timezone = $ical->newComponent('vtimezone');
[7579]140                    $timezone->setProperty('tzid',$apTimezone );
141
[7660]142                    $standard  = $timezone->newComponent( "standard" );
[7579]143                    $standard->setProperty( "tzoffsetfrom", $timezoneDayligth['standardFrom'] );
144                    $standard->setProperty( "tzoffsetto", $timezoneDayligth['standardTo'] );
145
146
147                    $standard->setProperty( "dtstart", $date->format(DATE_RFC822), array("VALUE" => "DATE") );
148
149                    $rrule = array(
150                        'FREQ' => $timezoneDayligth['standardFrequency'],
151                        'BYMONTH' =>  $timezoneDayligth['standardBymonth'],
152                        'BYday' => array(preg_replace("/[^0-9]/", "", $timezoneDayligth['standardByday']),  "DAY" => preg_replace("/[^a-zA-Z\s]/", "", $timezoneDayligth['standardByday']))
153                    );
154
155                    $standard->setProperty('rrule', $rrule);
156
[7660]157                    $daylight  = $timezone->newComponent( "daylight" );
[7579]158
159                    $daylight->setProperty( "tzoffsetfrom", $timezoneDayligth['daylightFrom'] );
160                    $daylight->setProperty( "tzoffsetto", $timezoneDayligth['daylightTo'] );
161
162
163                    $start = new DateTime('1970-01-01 '.$timezoneDayligth['daylightDtstart']);
164
165                    $r->recur($start, $timezoneDayligth['daylightFrequency'])
166                        ->until($start->modify('+1 years'))
167                        ->bymonth(array( $timezoneDayligth['daylightBymonth'] ))
168                        ->byday(array(  $timezoneDayligth['daylightByday'] ));
169
170                    $date = $r->next();
171
172                    $daylight->setProperty( "dtstart", $date->format(DATE_RFC822), array("VALUE" => "DATE") );
173
174                    $rrule = array(
175                        'FREQ' => $timezoneDayligth['daylightFrequency'],
176                        'BYMONTH' =>  $timezoneDayligth['daylightBymonth'],
177                        'BYday' => array(preg_replace("/[^0-9]/", "", $timezoneDayligth['daylightByday']),  "DAY" => preg_replace("/[^a-zA-Z\s]/", "", $timezoneDayligth['daylightByday']))
178                    );
179
180                    $daylight->setProperty('rrule', $rrule);
181                }
182
[6791]183                            break;
[7028]184            case TODO_ID:
[6791]185
[7006]186                $todo = $ical->newComponent('todo');
187
188                $todo->setProperty('summary', $v['summary']);
189                $todo->setProperty('description', isset($v['description']) ? $v['description'] : '');
190                $todo->setProperty('priority', $v['priority']);
191                $todo->setProperty('percent-complete', $v['percentage']);
192                $todo->setProperty('status', $this->_getStatusTodo($v['status']));
193
194                $timezone = new DateTimeZone('UTC');
195                $apTimezone = self::nomalizeTZID(( isset($v['timezone']) && $v['timezone'] != 'null' ) ? $v['timezone'] : $params['defaultTZI']);
196                $apTimezoneOBJ = new DateTimeZone($apTimezone);
197
198                $sTime = new DateTime('@' . (int) ($v['startTime'] / 1000), $timezone);
199                $sTime->setTimezone($apTimezoneOBJ);
200                $eTime = new DateTime('@' . (int) ($v['endTime'] / 1000), $timezone);
201                $eTime->setTimezone($apTimezoneOBJ);
202
[7801]203                $todo->setProperty('dtstamp', array('timestamp' => ($v['dtstamp'] / 1000) ));
[7702]204
[7006]205                if (isset($v['allDay']) && $v['allDay'] == 1) {
206                    $todo->setProperty('dtstart', $sTime->format(DATE_RFC822), array("VALUE" => "DATE"));
207                    $todo->setProperty('dtend', $eTime->format(DATE_RFC822), array("VALUE" => "DATE"));
208                    //$todo->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'TRUE');
209                } else {
210                    $todo->setProperty('dtstart', $sTime->format(DATE_RFC822), array('TZID' => $apTimezone));
211                    $todo->setProperty('dtend', $eTime->format(DATE_RFC822), array('TZID' => $apTimezone));
212                    //$todo->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'FALSE');
213                }
214
[7028]215                if(isset($v['due']) && $v['due'] != '' && (int)$v['due'] > 0){
[7006]216                    $dueTime = new DateTime('@' . (int) ($v['due'] / 1000), $timezone);
217                    $dueTime->setTimezone($apTimezoneOBJ);
218
219                    $todo->setProperty('due', $dueTime->format(DATE_RFC822), array('TZID' => $apTimezone));
[7028]220                    $todo->setProperty('dueTime', $dueTime->format(DATE_RFC822), array('TZID' => $apTimezone));
221                }               
[7006]222                       
223                if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
224                    $participants = $v['participants'];
225                else
226                    $participants = Controller::find(array('concept' => 'participant'), false, array('filter' => array('=', 'schedulable', $v['id'])));
227               
228                if (is_array($participants) && count($participants) > 0)
229                    foreach ($participants as $ii => $vv) {
230                   
231                        if(isset($participants[$ii]['user']) && !is_array($participants[$ii]['user']))
232                        {
233                            if ($vv['isExternal'] == 1)
234                                $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user'], 'service' => 'PostgreSQL'));
235                            else
236                                $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user']));
237                        }
238                   
239                        if ($participants[$ii]['user']['id'] == Config::me('uidNumber'))
240                        {
241                            $alarms = (isset($participants[$ii]['alarms'])) ? $participants[$ii]['alarms'] : Controller::find(array('concept' => 'alarm'), null, array('filter' => array('AND', array('=', 'participant', $vv['id']), array('=', 'schedulable', $v['id']))));
242                            if(is_array($alarms))
243                                self::createAlarms($alarms, $todo);
244                        }
245                   
246                    }
247
248                if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
249                    $this->createAttendee($v['participants'], $todo);
250
251                if (isset($v['attachments']) && is_array($v['attachments']) && count($v['attachments']) > 0)
252                    $this->createAttachment($v['attachments'], $todo);
253
254                $todo->setProperty('uid', $v['uid']); 
255               
256                break;
[6791]257                        default:
258                            break;
[6066]259                    }
[7068]260                }
[6791]261                return $ical->createCalendar();
262    }
263
264    protected function createCompatibleIcal($data, $params = false )
265    {
[7799]266        $ical = new icalCreatorVcalendar();
[6791]267                $ical->setProperty('method', isset($params['method']) ? $params['method'] : 'PUBLISH' );
268
269                /*
270                 * Seta propiedades obrigatorias para alguns softwares (Outlook)
271                 */
272                $ical->setProperty('x-wr-calname', 'Calendar Expresso');
273                $ical->setProperty('X-WR-CALDESC', 'Calendar Expresso');
274                $ical->setProperty('X-WR-TIMEZONE', $params['X-WR-TIMEZONE']);
275
276                foreach ($data as $i => $v) {
277
278                    switch ($v['type']) {
279                        case EVENT_ID:
280                            $vevent = $ical->newComponent('vevent');
281
282                            $vevent->setProperty('summary', $v['summary']);
283                            $vevent->setProperty('description', isset($v['description']) ? $v['description'] : '');
284                            $vevent->setProperty('location', $v['location']);
285                            $vevent->setProperty('tranp', (isset($v['tranparent']) && $v['tranparent'] == TRANSP_TRANSPARENT ) ? 'TRANSPARENT' : 'OPAQUE' );
286
[7702]287                $vevent->setProperty('dtstamp', array('timestamp' => ($v['dtstamp'] / 1000) ));
288
[6791]289                            $timezone = new DateTimeZone('UTC');
290                            $sTime = new DateTime('@' . (int) ($v['startTime'] / 1000), $timezone);
291                            $eTime = new DateTime('@' . (int) ($v['endTime'] / 1000), $timezone);
292
293                            if (( isset($v['repeat']) ) && ( isset($v['repeat']['frequency']) && $v['repeat']['frequency'] && $v['repeat']['frequency'] != 'none' ))
294                                    $vevent->setProperty('rrule', $this->formatIcalRepeat($v['repeat']));
295
296                            if (isset($v['allDay']) && $v['allDay'] == 1) {
297                                $vevent->setProperty('dtstart', $sTime->format(DATE_RFC822), array("VALUE" => "DATE"));
298                                $vevent->setProperty('dtend', $eTime->format(DATE_RFC822), array("VALUE" => "DATE"));
299                                $vevent->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'TRUE');
300                            } else {
301                                $vevent->setProperty('dtstart', $sTime->format(DATE_RFC822));
302                                $vevent->setProperty('dtend', $eTime->format(DATE_RFC822));
303                                $vevent->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'FALSE');
304                            }
305                                           
306                            if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
307                                $participants = $v['participants'];
308                            else
309                                        $participants = Controller::find(array('concept' => 'participant'), false, array('filter' => array('=', 'schedulable', $v['id'])));
310                           
311                                if (is_array($participants) && count($participants) > 0)
312                                        foreach ($participants as $ii => $vv) {
313                                       
314                                                if(isset($participants[$ii]['user']) && !is_array($participants[$ii]['user']))
315                                                {
316                                                        if ($vv['isExternal'] == 1)
317                                                                $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user'], 'service' => 'PostgreSQL'));
318                                                        else
319                                                                $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user']));
320                                                }
321                                       
322                                                if ($participants[$ii]['user']['id'] == Config::me('uidNumber'))
323                                                {
324                                                        $alarms = (isset($participants[$ii]['alarms'])) ? $participants[$ii]['alarms'] : Controller::find(array('concept' => 'alarm'), null, array('filter' => array('AND', array('=', 'participant', $vv['id']), array('=', 'schedulable', $v['id']))));
325                                                        if(is_array($alarms))
326                                                                self::createAlarms($alarms, $vevent);
327                                                }
328                                       
[6094]329                                        }
[6791]330                           
[6066]331
[6791]332                            if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
333                                        $this->createAttendee($v['participants'], $vevent);
[6066]334
[6791]335                            if (isset($v['attachments']) && is_array($v['attachments']) && count($v['attachments']) > 0)
336                                        $this->createAttachment($v['attachments'], $vevent);
[6066]337
[6791]338                            $vevent->setProperty('uid', $v['uid']); 
339                           
340                            break;
[6066]341
[6791]342                        default:
343                            break;
[6996]344                       
[7028]345        case TODO_ID:
[6066]346
[6996]347            $todo = $ical->newComponent('todo');
348
349            $todo->setProperty('summary', $v['summary']);
350            $todo->setProperty('description', isset($v['description']) ? $v['description'] : '');
351            $todo->setProperty('priority', $v['priority']);
352            $todo->setProperty('percent-complete', $v['percentage']);
353            $todo->setProperty('status', $this->_getStatusTodo($v['status']));
354
[7801]355            $todo->setProperty('dtstamp', array('timestamp' => ($v['dtstamp'] / 1000) ));
[7702]356
[6996]357            $timezone = new DateTimeZone('UTC');
358            $apTimezone = self::nomalizeTZID(( isset($v['timezone']) && $v['timezone'] != 'null' ) ? $v['timezone'] : $params['defaultTZI']);
359            $apTimezoneOBJ = new DateTimeZone($apTimezone);
360
361            $sTime = new DateTime('@' . (int) ($v['startTime'] / 1000), $timezone);
362            $sTime->setTimezone($apTimezoneOBJ);
363
364            if (isset($v['allDay']) && $v['allDay'] == 1) {
365                $todo->setProperty('dtstart', $sTime->format(DATE_RFC822), array("VALUE" => "DATE"));
366                //$todo->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'TRUE');
367            } else {
368                $todo->setProperty('dtstart', $sTime->format(DATE_RFC822), array('TZID' => $apTimezone));
369                //$todo->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'FALSE');
370            }
371
[7028]372            if(isset($v['due']) && $v['due'] != '' && (int)$v['due'] > 0){
[6996]373                $dueTime = new DateTime('@' . (int) ($v['due'] / 1000), $timezone);
374                $dueTime->setTimezone($apTimezoneOBJ);
375
376                $todo->setProperty('due', $dueTime->format(DATE_RFC822), array('TZID' => $apTimezone));
377            }
378                   
379            if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
380                $participants = $v['participants'];
381            else
382                $participants = Controller::find(array('concept' => 'participant'), false, array('filter' => array('=', 'schedulable', $v['id'])));
383           
384            if (is_array($participants) && count($participants) > 0)
385                foreach ($participants as $ii => $vv) {
386               
387                    if(isset($participants[$ii]['user']) && !is_array($participants[$ii]['user']))
388                    {
389                        if ($vv['isExternal'] == 1)
390                            $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user'], 'service' => 'PostgreSQL'));
391                        else
392                            $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user']));
393                    }
394               
395                    if ($participants[$ii]['user']['id'] == Config::me('uidNumber'))
396                    {
397                        $alarms = (isset($participants[$ii]['alarms'])) ? $participants[$ii]['alarms'] : Controller::find(array('concept' => 'alarm'), null, array('filter' => array('AND', array('=', 'participant', $vv['id']), array('=', 'schedulable', $v['id']))));
398                        if(is_array($alarms))
399                            self::createAlarms($alarms, $todo);
400                    }
401               
402                }
403
404            if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
405                $this->createAttendee($v['participants'], $todo);
406
407            if (isset($v['attachments']) && is_array($v['attachments']) && count($v['attachments']) > 0)
408                $this->createAttachment($v['attachments'], $todo);
409
410            $todo->setProperty('uid', $v['uid']); 
411           
412            break;
413
414                default:
415                    break;
416            }
417        }
418
[6171]419       
[6996]420        return $ical->createCalendar();
[5341]421    }
[6791]422
423    protected function formatIcalRepeat($pRepeat)
424    {
425        $repeat = array();
426
427                foreach ($pRepeat as $ir => $rv) {
428                    if ($rv) {
[7579]429                if ($ir == 'frequency' && $rv !== 'none')
430                    $repeat['FREQ'] = $rv;
431                else if ($ir == 'endTime') {
432                    $time = new DateTime('@' . (int) ($rv / 1000), new DateTimeZone('UTC'));
433                    $time->setTimezone($apTimezoneOBJ);
434                    $repeat['until'] = $time->format(DATE_RFC822);
435                }else if ($ir == 'count')
436                    $repeat[$ir] = $rv;
437                else if ($ir == 'interval')
438                    $repeat[$ir] = $rv;
439                else if ($ir !== 'schedulable' && $ir !== 'id' && $ir !== 'startTime')
440                    $repeat[$ir] = explode(',', $rv);
[6791]441                    }
442                }
443                return $repeat;
444    }
445
[6094]446   
447    public function createAlarms($alarms, &$vevent)
448    {   
[7068]449            foreach ($alarms as $va)
[6094]450            {
451                $valarm = new valarm();
452                $valarm->setProperty('ACTION' , self::codeAlarmAction($va['type']));
453               
454                $duration = array();
455               
456                switch ($va['unit'])
457                {
458                        case 'h':
459                                $duration['hour'] = $va['time'];
460                        break;
461                        case 'm':
462                                $duration['min'] = $va['time'];
463                        break;
464                        case 's':
465                                $duration['sec'] = $va['time'];
466                                break;
467                }
468               
469                $valarm->setProperty('trigger' ,$duration);
470                $vevent->setComponent($valarm);
471            }   
472       
473    }
[6066]474
475    //Trata a criacao de anexos do ics
476    public function createAttachment($attachments, &$vevent) {
477        foreach ($attachments as $key => $attachment) {
478            $pParams = array("ENCODING" => "BASE64", "VALUE" => "BINARY",
479                "X-FILENAME" => $attachment['name']);
480
481            $vevent->setProperty("attach", $attachment['source'], $pParams);
[5514]482        }
[6066]483    }
484
485    //Trata a criacao de attendees com tratamento de delegate
486    public function createAttendee($attendees, &$vevent) {
487        $delegate = array();
488        foreach ($attendees as $di => $dv) {
489            if (isset($dv['delegatedFrom']) && $dv['delegatedFrom'] != 0) {
490                $delegate[$dv['delegatedFrom']] = $dv;
491            }
492        }
493
494        foreach ($attendees as $pi => $pv) {
495            $isResponseDelegated = false;
[6321]496            if ((isset($pv['delegatedFrom']) && $pv['delegatedFrom'] == 0) || !isset($pv['delegatedFrom']))  {
[6066]497                if ($pv['isOrganizer'] == 1)
498                    $vevent->setProperty('organizer', $pv['user']['mail'], array('CN' => $pv['user']['name']));
499                else {
500                    $pParams = array();
501                    $pParams['CN'] = $pv['user']['name'];
502                    $pParams['PARTSTAT'] = self::_getStatus($pv['status']);
503
504                    if (isset($pv['id']) && isset($delegate[$pv['id']])) {
505                        $pParams['PARTSTAT'] = self::_getStatus($delegate[$pv['id']]['status']);
506                        $pParams['DELEGATED-TO'] = $delegate[$pv['id']]['user']['mail'];
507                        $pParams['CN'] = $pv['user']['name'];
508
509                        $vevent->setProperty('attendee', $pv['user']['mail'], $pParams);
510
511                        if ($delegate[$pv['id']]['status'] == STATUS_UNANSWERED) {
512                            $pParams['RSVP'] = $pv['receiveNotification'] == 1 ? 'TRUE' : 'FALSE';
513                            unset($pParams['PARTSTAT']);
514                        }else
515                            $pParams['PARTSTAT'] = self::_getStatus($delegate[$pv['id']]['status']);
516
517                        unset($pParams['DELEGATED-TO']);
518                        $pParams['DELEGATED-FROM'] = $pv['user']['mail'];
519
520                        $vevent->setProperty('attendee', $delegate[$pv['id']]['user']['mail'], $pParams);
521                        continue;
522                    }
523                    $pParams['RSVP'] = 'TRUE';
524
525                    $vevent->setProperty('attendee', $pv['user']['mail'], $pParams);
[6094]526
[5514]527                }
[6066]528            }
[6094]529           
530           
[6066]531        }
532    }
[5514]533
[6066]534    public function parse($data, $params = false) {
535        Config::regSet('noAlarm', TRUE); //Evita o envio de notificação
[7799]536        $vcalendar = new icalCreatorVcalendar( );
537
[6066]538        $vcalendar->parse(trim($data));
539        $vcalendar->sort();
540
541        $return = array();
542        $method = $vcalendar->getProperty('method', FALSE, FALSE);
543        $params['prodid'] = $vcalendar->getProperty('prodid', false, false);
[6215]544        $params['X-WR-TIMEZONE'] = ( $xrTimezone = $vcalendar->getProperty('X-WR-TIMEZONE', false, false)) ? self::nomalizeTZID($xrTimezone[1]) : false ;
[6066]545
546        while ($component = $vcalendar->getComponent()) {
547            $interation = array();
548            $uid = $component->getProperty('uid', false, false); //Resgata o uid do componente
549
550            switch (strtoupper($component->objName)) {
551                case 'VEVENT':
552
[7702]553
[6066]554                    switch ($method) {
555                        case 'PUBLISH':
[6344]556                            //Caso o evento não exista o mesmo cria um novo evento, se já existir o mesmo referencia o evento com agenda
[7006]557                if (!$schedulable = self::_getSchedulable($uid))
558                                    $interation = self::_makeVEVENT($schedulable, $component, $params);
559                else{
560                    $links = Controller::read(array('concept' => 'calendarToSchedulable'), array('id'), array('filter' =>
561                                        array('AND',
562                                            array('=', 'calendar', $params['calendar']),
563                                            array('=', 'schedulable', $schedulable['id'])
564                                        )));
[6344]565
[7006]566                    if(!$links &&  !isset($links[0]))
567                        Controller::create(array('concept' => 'calendarToSchedulable'), array('calendar' => $params['calendar'], 'schedulable' => $schedulable['id']));
[6344]568
[7006]569                    }
[6066]570                            break;
571
572                        case 'REQUEST':
573                            $schedulable = self::_getSchedulable($uid);
[7695]574                $calendar = false;
[6066]575
576                            if ($schedulable) { //Caso o evento exista
[7695]577                    if (!($calendar = self::_existInMyCalendars($schedulable['id'], $params['owner']) )) {
578                        $calendarToSchedulable = array();
579                        $calendarToSchedulable['calendar'] = $params['calendar'];
580                        $calendarToSchedulable['schedulable'] = $schedulable['id'];
581                        $interation['calendarToSchedulable://' . mt_rand() . '(Formatter)'] = $calendarToSchedulable;
582
[7704]583                        if (isset($params['status']))
584                        {
585                            if($params['owner'] != Config::me("uidNumber"))
586                            {
[7695]587                                $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );
588                                $pID = self::_getParticipantByMail($user[0]['mail'], $schedulable['participants']);
[7704]589                            }
590                            else
591                            {
[7695]592                                $pID = self::_getParticipantByMail(Config::me('mail'), $schedulable['participants']);
593                            }
[7704]594                            //caso nõa seja participante adiciona a lista de participantes
595                            if(!$pID){
596                                $pID =  mt_rand() . '2(Formatter)';
597                                $interation['participant://' . $pID]['status'] = $params['status'];
598                                $interation['participant://' . $pID]['user'] = $params['owner'];
599                                $interation['participant://' . $pID]['isOrganizer'] = '0';
600                                $interation['participant://' . $pID]['schedulable'] = $schedulable['id'];
601                            }
602                            else
603                            {
604                                $interation['participant://' . $pID]['status'] = $params['status'];
605                            }
[7006]606                        }
[6066]607
[7695]608                        Config::regSet('noAlarm', FALSE); //reativa notificação
609                    } else {
[6066]610
[7695]611                        if (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence']){ //Organizador esta requisitando que você atualize o evento
[6066]612
[7695]613                            $params['calendar'] = $params['calendar'] == 'true' ? $calendar : $params['calendar'];
614                            $interation = self::_makeVEVENT($schedulable, $component, $params);
615                        }else if ($component->getProperty('sequence', false, false) === $schedulable['sequence']) {
616                            //Ler melhor rfc sobre isto 3.2.2.2
617                            //Aparentemente é para retornar um ical com o evento atualizado para o attende
618                        }
[5341]619
[7695]620                        if (isset($params['status'])) {
621                            if($params['owner'] != Config::me("uidNumber")){
622                               $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );
623                                $pID = self::_getParticipantByMail($user[0]['mail'], $schedulable['participants']);
624                            }else
625                                $pID = self::_getParticipantByMail(Config::me('mail'), $schedulable['participants']);
626                            //Verifica a importação de eventos em que não participo
627                            if ($pID) {
[7702]628
[7695]629                                $interation['participant://' . $pID]['status'] = $params['status'];
[7702]630
[7695]631                            }
632                        }
633                    }
634                } else { // Importar evento
635                    $interation = self::_makeVEVENT(array(), $component, $params);
[5916]636
[7695]637                    if (strpos($params['prodid'], 'kigkonsult.se') !== false) { //envia notificação para fora
[5682]638
[7695]639                        /* Data de Inicio */
640                        $startTime = $component->getProperty('dtstart', false, true);
[6215]641
[7695]642                        $tzid = isset($startTime['params']['TZID']) ? $startTime['params']['TZID'] : $params['X-WR-TIMEZONE'];
[5682]643
[7695]644                        /* Tiem zone do evento */
645                        if ($tzid)
646                        $sc['timezone'] = self::nomalizeTZID($tzid);
647                        else
648                        $sc['timezone'] = isset($params['calendar_timezone']) ? $params['calendar_timezone'] : 'America/Sao_Paulo';
[5682]649
[7695]650                        $objTimezone = new DateTimeZone($sc['timezone']);
[5682]651
[7695]652                        if (isset($startTime['params']['VALUE']) && $startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone'])) {
653                        $sc['allDay'] = 1;
654                        $sc['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $sc['timezone']) . '000';
655                        } elseif (isset($startTime['params']['TZID']) && !isset($startTime['value']['tz']))/* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
656                        $sc['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $startTime['params']['TZID']) . '000';
657                        else {
658                        $sc['startTime'] = self::date2timestamp($startTime['value']) . '000';
659                        if (strpos($params['prodid'], 'Outlook') !== false) {
660                            //Se o ics veio em utc não aplicar horario de verão
661                            $sTime = new DateTime('@' . (int) ($sc['startTime'] / 1000), new DateTimeZone('UTC'));
662                            $sTime->setTimezone($objTimezone);
663                            if ($sTime->format('I')) //Se o ics veio em utc não aplicar horario de verão
664                            $sc['startTime'] = $sc['startTime'] - 3600000;
665                        }
666                        }
[5682]667
668
[7695]669                        /* Data de Termino */
670                        $endTime = $component->getProperty('dtend', false, true);
[5682]671
[7695]672                        if (isset($endTime['params']['VALUE']) && $endTime['params']['VALUE'] === 'DATE')
673                        $sc['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $sc['timezone']) . '000';
674                        else if (isset($endTime['params']['TZID']) && !isset($endTime['value']['tz'])) /* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
675                        $sc['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $endTime['params']['TZID']) . '000';
676                        else {
677                        $sc['endTime'] = self::date2timestamp($endTime['value']) . '000';
678                        if (strpos($params['prodid'], 'Outlook') !== false) {
679                            //Se o ics veio em utc não aplicar horario de verão
680                            $eTime = new DateTime('@' . (int) ($sc['endTime'] / 1000), new DateTimeZone('UTC'));
681                            $eTime->setTimezone($objTimezone);
682                            if ($eTime->format('I'))
683                            $sc['endTime'] = $sc['endTime'] - 3600000;
684                        }
685                        }
[5682]686
687
[7695]688                        if ($uid = $component->getProperty('uid', false, false))
689                        ;
690                        $sc['uid'] = $uid;
[5682]691
692
[7695]693                        $sc['summary'] = mb_convert_encoding($component->getProperty('summary', false, false), 'UTF-8', 'UTF-8,ISO-8859-1');
[5682]694
[7695]695                        /* Definindo Description */
696                        if ($desc = $component->getProperty('description', false, false))
697                        $sc['description'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $desc), 'UTF-8', 'UTF-8,ISO-8859-1');
[5682]698
[7695]699                        /* Definindo location */
700                        if ($location = $component->getProperty('location', false, false))
701                        $sc['location'] = mb_convert_encoding($location, 'UTF-8', 'UTF-8,ISO-8859-1');
[5682]702
[5736]703
[5682]704
[7695]705                        if ($property = $component->getProperty('organizer', FALSE, TRUE)) {
706                        $participant = array();
707                        $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
[5682]708
[7695]709                        $participantID = mt_rand() . '2(Formatter)';
[5682]710
[7695]711                        $participant['isOrganizer'] = '1';
[5682]712
[7695]713                        $user = null;
[5341]714
[7695]715                        $participant['isExternal'] = 1;
716                        /* Gera um randon id para o contexto formater */
717                        $userID = mt_rand() . '4(Formatter)';
[5341]718
[7695]719                        $user['mail'] = $mailUser;
720                        $organizerMail = $mailUser;
[5341]721
[7695]722                        $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
723                        $user['isExternal'] = '1';
724                        $participant['user'] = $user;
[5341]725
[7695]726                        $sc['participants'][] = $participant;
727                        }
[5341]728
729
[7695]730                        $participant['status'] = isset($params['status']) ? $params['status'] : STATUS_ACCEPTED;
731                        $participant['isOrganizer'] = '0';
732                        $participant['isExternal'] = 0;
[7704]733
734                        $user = false;
735                        if($params['owner'] != Config::me("uidNumber"))
736                        {
737                            $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );
738                        }
739
740                        $participant['user'] = $user ?  array('mail' => $user['mail'], 'name' => $user['name']) : array('mail' => Config::me('mail'), 'name' => Config::me('cn'));
741
[7695]742                        $sc['participants'][] = $participant;
743                        $sc['type'] = EVENT_ID;
[5341]744
[5903]745
[7695]746                        $ical['source'] = Controller::format(array('service' => 'iCal'), array($sc), array('method' => 'REPLY'));
747                        $ical['type'] = 'application/ics';
748                        $ical['name'] = 'outlook.ics';
[5947]749
[7695]750                        $ical2['source'] = $ical['source'];
751                        $ical2['type'] = 'text/calendar; method=REPLY';
752                        $ical2['name'] = 'thunderbird.ics';
[5341]753
[7695]754                        $timezone = new DateTimeZone('UTC');
755                        $sTime = new DateTime('@' . (int) ($sc['startTime'] / 1000), $timezone);
756                        $eTime = new DateTime('@' . (int) ($sc['endTime'] / 1000), $timezone);
[5341]757
[7695]758                        if (isset($sc['timezone'])) {
759                        $sTime->setTimezone(new DateTimeZone($sc['timezone']));
760                        $eTime->setTimezone(new DateTimeZone($sc['timezone']));
761                        }
[5341]762
[7695]763                        $data = array('startDate' => date_format($sTime, 'd/m/Y'),
764                        'startTime' => (isset($sc['allDay']) && $sc['allDay'] ) ? '' : date_format($sTime, 'H:i'),
765                        'endDate' => date_format($eTime, 'd/m/Y'),
766                        'endTime' => isset($sc['allDay']) ? '' : date_format($eTime, 'H:i'),
767                        'eventTitle' => $sc['summary'],
768                        'eventLocation' => isset($sc['location']) ? $sc['location'] : '',
769                        'timezone' => ($sc['timezone']) ? $sc['timezone'] : 'UTC',
770                        'participant' => (isset($part['user']['name']) ? $part['user']['name'] : $part['user']['mail']));
[5341]771
[7695]772                        $subject['notificationType'] = 'Convite Aceito';
773                        $subject['eventTitle'] = mb_convert_encoding($sc['summary'], 'ISO-8859-1', 'ISO-8859-1,UTF-8');
774                        $subject['startDate'] = date_format($sTime, 'd/m/Y');
775                        $subject['startTime'] = ($sc['allDay']) ? '' : date_format($sTime, 'H:i');
776                        $subject['endDate'] = date_format($eTime, 'd/m/Y');
777                        $subject['endTime'] = ($sc['allDay']) ? '' : date_format($eTime, 'H:i');
778                        $subject['participant'] = Config::me('uid');
[5341]779
[7695]780                        $params['status'] = isset($params['status']) ? $params['status'] : STATUS_ACCEPTED;
[5344]781
[7695]782                        switch ($params['status']) {
783                        case STATUS_ACCEPTED:
784                            $tpl = 'notify_accept_body';
785                            $subject['notificationType'] = 'Convite Aceito';
786                            break;
787                        case STATUS_TENTATIVE:
788                            $tpl = 'notify_attempt_body';
789                            $subject['notificationType'] = 'Convite  aceito provisoriamente';
790                            break;
791                        case STATUS_CANCELLED:
792                            $tpl = 'notify_reject_body';
793                            $subject['notificationType'] = 'Convite rejeitado';
794                            break;
795                        }
796                        require_once ROOTPATH . '/api/parseTPL.php';
[5344]797
[7695]798                        $mail = array();
799                        $mail['attachments'][] = $ical;
800                        $mail['attachments'][] = $ical2;
[5344]801
[7695]802                        $mail['isHtml'] = true;
803                        $mail['body'] = parseTPL::load_tpl($data, ROOTPATH . '/modules/calendar/templates/' . $tpl . '.tpl');
804                        $mail['subject'] = parseTPL::load_tpl($subject, ROOTPATH . '/modules/calendar/templates/notify_subject.tpl');
805                        ;
[7704]806                        $mail['from'] = $user ? '"' . $user['name'] . '" <' . $user['mail'] . '>' : '"' . Config::me('cn') . '" <' . Config::me('mail') . '>';
[7695]807                        $mail['to'] = $organizerMail;
[5344]808
[7695]809                        Controller::create(array('service' => 'SMTP'), $mail);
810                    }
[6066]811                            }
812                            break;
[5344]813
[6066]814                        case 'REFRESH':
815                            break;
[5341]816
[6066]817                        case 'CANCEL':
818                            if ($schedulable = self::_getSchedulable($uid))
819                                $interation['schedulable://' . $schedulable['id']] = false;
820                            break;
[6861]821                               
[6066]822                        case 'ADD':
823                            break;
[5344]824
[6066]825                        case 'REPLY':
826                            if ($schedulable = self::_getSchedulable($uid)) {
827                                while ($property = $component->getProperty('attendee', FALSE, TRUE))
828                                    if ($pID = self::_getParticipantByMail(str_replace('MAILTO:', '', $property['value']), $schedulable['participants']))
829                                        $interation['participant://' . $pID] = array('id' => $pID, 'status' => constant('STATUS_' . strtoupper($property['params']['PARTSTAT'])));
[5344]830
[6066]831                                $interation['schedulable://' . $schedulable['id']]['sequence'] = $schedulable['sequence'] + 1;
832                            }
833                            break;
[5341]834
[6066]835                        case 'COUNTER':
836                            if ($params['acceptedSuggestion'] !== 'false') {
[5341]837
[6066]838                                $schedulable = self::_getSchedulable($uid);
[6331]839                                $params['calendar'] = self::_existInMyCalendars($schedulable['id'], $params['owner']);
[6066]840
841                                $interation = self::_makeCOUNTER($schedulable, $component, $params);
842                                Config::regSet('noAlarm', FALSE);
843                            } else {
844                                $response = array();
845                                $response['from'] = $params['from'];
846                                $response['type'] = 'suggestionResponse';
847                                $response['status'] = 'DECLINECOUNTER';
848                                $response['schedulable'] = self::_getSchedulable($uid);
849
850                                Controller::create(array('concept' => 'notification'), $response);
851                            }
852                            break;
853
854                        case 'DECLINECOUNTER':
855                            break;
856
857                        default:
858
859                            $schedulable = self::_getSchedulable($uid);
860
861                            if ($schedulable && ( self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence'])) { //Caso o evento exista
862                                $interation = self::_makeVEVENT($schedulable, $component, $params);
863
[6331]864                                if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {
[6066]865                                    $calendarToSchedulable = array();
866                                    $calendarToSchedulable['calendar'] = $params['calendar'];
867                                    $calendarToSchedulable['schedulable'] = $schedulable['id'];
868                                    $interation['calendarToSchedulable://' . mt_rand() . '(Formatter)'] = $calendarToSchedulable;
[5514]869                                }
[6066]870                            }
871                            else // Importar evento
872                                $interation = self::_makeVEVENT(array(), $component, $params);
873                            break;
874                    }
[7702]875
[6066]876                    $return[] = $interation;
877                    break;
[6996]878        /***********************************************************************TODO*******************************************************************************/
879        case 'VTODO':
880        switch ($method) {
881            case 'PUBLISH':
882                //Caso a tarefa não exista o mesmo cria um novo evento, se já existir o mesmo referencia o evento com agenda
883                if (!$schedulable = self::_getSchedulable($uid))
884                    $interation = self::_makeVTODO($schedulable, $component, $params);
885                else{
886                    $links = Controller::read(array('concept' => 'calendarToSchedulable'), array('id'), array('filter' =>
887                    array('AND',
888                        array('=', 'calendar', $params['calendar']),
889                        array('=', 'schedulable', $schedulable['id'])
890                    )));
[6066]891
[6996]892                    if(!$links &&  !isset($links[0]))
893                        Controller::create(array('concept' => 'calendarToSchedulable'), array('calendar' => $params['calendar'], 'schedulable' => $schedulable['id']));
894                }
895                break;
[6066]896
[6996]897            case 'REQUEST':
898                $schedulable = self::_getSchedulable($uid);
899
900                if ($schedulable) { //Caso tarefa exista
901                    if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {
902                       
903                        $calendarToSchedulable = array();
904                        $calendarToSchedulable['calendar'] = $params['calendar'];
905                        $calendarToSchedulable['schedulable'] = $schedulable['id'];
906                        $interation['calendarToSchedulable://' . mt_rand() . '(Formatter)'] = $calendarToSchedulable;
907       
908                        if (isset($params['status'])) {
909                            if($params['owner'] != Config::me("uidNumber")){                       
910                                $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );                       
911                                $pID = self::_getParticipantByMail($user[0]['mail'], $schedulable['participants']);
912                            }else
913                                $pID = self::_getParticipantByMail(Config::me('mail'), $schedulable['participants']);
914                                $interation['participant://' . $pID]['status'] = $params['status'];
915                        }
916                        Config::regSet('noAlarm', FALSE); //reativa notificação
917                    } else {
918
919                        if (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence']) //Organizador esta requisitando que você atualize o evento
920                            $interation = self::_makeVEVENT($schedulable, $component, $params);
921                        else if ($component->getProperty('sequence', false, false) === $schedulable['sequence']) {
922                        //Ler melhor rfc sobre isto 3.2.2.2
923                        //Aparentemente é para retornar um ical com o evento atualizado para o attende
924                        }
925
926                        if (isset($params['status'])) {
927                            if($params['owner'] != Config::me("uidNumber")){                       
928                               $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );                     
929                                $pID = self::_getParticipantByMail($user[0]['mail'], $schedulable['participants']);
[7028]930                            }else{
[6996]931                                $pID = self::_getParticipantByMail(Config::me('mail'), $schedulable['participants']);
[7028]932                                //Verifica a importação de tarefas em que não participo
933                                if ($pID) {
934                                    $pID =  mt_rand() . '2(Formatter)';
935                                    $interation['participant://' . $pID]['status'] = $params['status'];
936                                    $interation['participant://' . $pID]['user'] = $params['owner'];
937                                    $interation['participant://' . $pID]['isOrganizer'] = '0';
938                                    $interation['participant://' . $pID]['schedulable'] = $schedulable['id'];
939                                }else
940                                    $interation['participant://' . $pID]['status'] = $params['status'];
[6996]941                            }
942                        }
943                    }
944                } else { // Importar tarefa
[7028]945                    $interation = self::_makeVTODO(array(), $component, $params);
[6996]946
947                    if (strpos($params['prodid'], 'kigkonsult.se') !== false) { //envia notificação para fora
948
949                        /* Data de Inicio */
950                        $startTime = $component->getProperty('dtstart', false, true);
951                        $tzid = isset($startTime['params']['TZID']) ? $startTime['params']['TZID'] : $params['X-WR-TIMEZONE'];
952
953                        /* Tiem zone do evento */   
954                        if ($tzid)
955                            $sc['timezone'] = self::nomalizeTZID($tzid);
956                        else
957                            $sc['timezone'] = isset($params['calendar_timezone']) ? $params['calendar_timezone'] : 'America/Sao_Paulo';
958
959                        $objTimezone = new DateTimeZone($sc['timezone']);
960
961                        if (isset($startTime['params']['VALUE']) && $startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone'])) {
962                            $sc['allDay'] = 1;
963                            $sc['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $sc['timezone']) . '000';
964                        } elseif (isset($startTime['params']['TZID']) && !isset($startTime['value']['tz']))/* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
965                            $sc['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $startTime['params']['TZID']) . '000';
966                        else {
967                            $sc['startTime'] = self::date2timestamp($startTime['value']) . '000';
968                            if (strpos($params['prodid'], 'Outlook') !== false) {
969                                //Se o ics veio em utc não aplicar horario de verão
970                                $sTime = new DateTime('@' . (int) ($sc['startTime'] / 1000), new DateTimeZone('UTC'));
971                                $sTime->setTimezone($objTimezone);
972                                if ($sTime->format('I')) //Se o ics veio em utc não aplicar horario de verão
973                                $sc['startTime'] = $sc['startTime'] - 3600000;
974                            }
975                        }
976
977                        /* Data de Termino */
978                        $endTime = $component->getProperty('dtend', false, true);
979
980                        if (isset($endTime['params']['VALUE']) && $endTime['params']['VALUE'] === 'DATE')
981                            $sc['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $sc['timezone']) . '000';
982                        else if (isset($endTime['params']['TZID']) && !isset($endTime['value']['tz'])) /* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
983                            $sc['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $endTime['params']['TZID']) . '000';
984                        else {
985                            $sc['endTime'] = self::date2timestamp($endTime['value']) . '000';
986                            if (strpos($params['prodid'], 'Outlook') !== false) {
987                                //Se o ics veio em utc não aplicar horario de verão
988                                $eTime = new DateTime('@' . (int) ($sc['endTime'] / 1000), new DateTimeZone('UTC'));
989                                $eTime->setTimezone($objTimezone);
990                                if ($eTime->format('I'))
991                                    $sc['endTime'] = $sc['endTime'] - 3600000;
992                            }
993                        }
994
995
996                        if ($uid = $component->getProperty('uid', false, false))                   
997                            $sc['uid'] = $uid;
998
999                        $sc['summary'] = mb_convert_encoding($component->getProperty('summary', false, false), 'UTF-8', 'UTF-8,ISO-8859-1');
1000
1001                        /* Definindo Description */
1002                        if ($desc = $component->getProperty('description', false, false))
1003                            $sc['description'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $desc), 'UTF-8', 'UTF-8,ISO-8859-1');
1004
1005            if ($priority = $component->getProperty('priority', false, false))
1006                $sc['priority'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $priority), 'UTF-8', 'UTF-8,ISO-8859-1');
1007
1008            if ($status = $component->getProperty('status', false, false))
1009                $sc['status'] = $this->decodeStatusTodo(mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $status), 'UTF-8', 'UTF-8,ISO-8859-1'));
1010
1011            if ($percentage = $component->getProperty('percent-complete', false, false))
1012                $sc['percentage'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $percentage), 'UTF-8', 'UTF-8,ISO-8859-1');
1013
1014                        /* Definindo location */
1015                        if ($location = $component->getProperty('location', false, false))
1016                            $sc['location'] = mb_convert_encoding($location, 'UTF-8', 'UTF-8,ISO-8859-1');
1017
1018
1019
1020                        if ($property = $component->getProperty('organizer', FALSE, TRUE)) {
1021                            $participant = array();
1022                            $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
1023
1024                            $participantID = mt_rand() . '2(Formatter)';
1025
1026                            $participant['isOrganizer'] = '1';
1027
1028                            $user = null;
1029
1030                            $participant['isExternal'] = 1;
1031                            /* Gera um randon id para o contexto formater */
1032                            $userID = mt_rand() . '4(Formatter)';
1033
1034                            $user['mail'] = $mailUser;
1035                            $organizerMail = $mailUser;
1036
1037                            $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
1038                            $user['isExternal'] = '1';
1039                            $participant['user'] = $user;
1040
1041                            $sc['participants'][] = $participant;
1042                        }
1043
1044
1045                        $participant['status'] = isset($params['status']) ? $params['status'] : STATUS_ACCEPTED;
1046                        $participant['isOrganizer'] = '0';
1047                        $participant['isExternal'] = 0;
1048                        $participant['user'] = array('mail' => Config::me('mail'), 'name' => Config::me('cn'));
1049                        $sc['participants'][] = $participant;
1050                        $sc['type'] = TODO_ID;
1051
1052
1053                        $ical['source'] = Controller::format(array('service' => 'iCal'), array($sc), array('method' => 'REPLY'));
1054                        $ical['type'] = 'application/ics';
1055                        $ical['name'] = 'outlook.ics';
1056
1057                        $ical2['source'] = $ical['source'];
1058                        $ical2['type'] = 'text/calendar; method=REPLY';
1059                        $ical2['name'] = 'thunderbird.ics';
1060
1061                        $timezone = new DateTimeZone('UTC');
1062                        $sTime = new DateTime('@' . (int) ($sc['startTime'] / 1000), $timezone);
1063                        $eTime = new DateTime('@' . (int) ($sc['endTime'] / 1000), $timezone);
1064
1065                        if (isset($sc['timezone'])) {
1066                            $sTime->setTimezone(new DateTimeZone($sc['timezone']));
1067                            $eTime->setTimezone(new DateTimeZone($sc['timezone']));
1068                        }
1069
1070                        $data = array('startDate' => date_format($sTime, 'd/m/Y'),
1071                        'startTime' => (isset($sc['allDay']) && $sc['allDay'] ) ? '' : date_format($sTime, 'H:i'),
1072                        'endDate' => date_format($eTime, 'd/m/Y'),
1073                        'endTime' => isset($sc['allDay']) ? '' : date_format($eTime, 'H:i'),
1074                        'eventTitle' => $sc['summary'],
1075                        'eventLocation' => isset($sc['location']) ? $sc['location'] : '',
1076                        'timezone' => ($sc['timezone']) ? $sc['timezone'] : 'UTC',
1077                        'participant' => (isset($part['user']['name']) ? $part['user']['name'] : $part['user']['mail']));
1078
1079                        $subject['notificationType'] = 'Convite Aceito';
1080                        $subject['eventTitle'] = mb_convert_encoding($sc['summary'], 'ISO-8859-1', 'ISO-8859-1,UTF-8');
1081                        $subject['startDate'] = date_format($sTime, 'd/m/Y');
1082                        $subject['startTime'] = ($sc['allDay']) ? '' : date_format($sTime, 'H:i');
1083                        $subject['endDate'] = date_format($eTime, 'd/m/Y');
1084                        $subject['endTime'] = ($sc['allDay']) ? '' : date_format($eTime, 'H:i');
1085                        $subject['participant'] = Config::me('uid');
1086
1087                        $params['status'] = isset($params['status']) ? $params['status'] : STATUS_ACCEPTED;
1088
1089                        switch ($params['status']) {
1090                            case STATUS_ACCEPTED:
1091                                $tpl = 'notify_accept_body';
1092                                $subject['notificationType'] = 'Convite Aceito';
1093                                break;
1094                            case STATUS_TENTATIVE:
1095                                $tpl = 'notify_attempt_body';
1096                                $subject['notificationType'] = 'Convite  aceito provisoriamente';
1097                                break;
1098                            case STATUS_CANCELLED:
1099                                $tpl = 'notify_reject_body';
1100                                $subject['notificationType'] = 'Convite rejeitado';
1101                                break;
1102                        }
1103
1104                        require_once ROOTPATH . '/api/parseTPL.php';
1105
1106                        $mail = array();
1107                        $mail['attachments'][] = $ical;
1108                        $mail['attachments'][] = $ical2;
1109
1110                        $mail['isHtml'] = true;
1111                        $mail['body'] = parseTPL::load_tpl($data, ROOTPATH . '/modules/calendar/templates/' . $tpl . '.tpl');
1112                        $mail['subject'] = parseTPL::load_tpl($subject, ROOTPATH . '/modules/calendar/templates/notify_subject.tpl');
1113
1114                        $mail['from'] = '"' . Config::me('cn') . '" <' . Config::me('mail') . '>';
1115                        $mail['to'] = $organizerMail;
1116
1117
1118                        Controller::create(array('service' => 'SMTP'), $mail);
1119                    }
1120                }
1121                break;
1122
1123            case 'REFRESH':
1124                break;
1125
1126            case 'CANCEL':
1127                if ($schedulable = self::_getSchedulable($uid))
1128                    $interation['schedulable://' . $schedulable['id']] = false;
1129                break;
1130
1131            case 'ADD':
1132                break;
1133
1134            case 'REPLY':
1135                if ($schedulable = self::_getSchedulable($uid)) {
1136                    while ($property = $component->getProperty('attendee', FALSE, TRUE))
1137                        if ($pID = self::_getParticipantByMail(str_replace('MAILTO:', '', $property['value']), $schedulable['participants']))
1138                            $interation['participant://' . $pID] = array('id' => $pID, 'status' => constant('STATUS_' . strtoupper($property['params']['PARTSTAT'])));
1139
1140                    $interation['schedulable://' . $schedulable['id']]['sequence'] = $schedulable['sequence'] + 1;
1141                }
1142                break;
1143
1144            case 'COUNTER':
1145                if ($params['acceptedSuggestion'] !== 'false') {
1146
1147                    $schedulable = self::_getSchedulable($uid);
1148                    $params['calendar'] = self::_existInMyCalendars($schedulable['id'], $params['owner']);
1149
1150                    $interation = self::_makeCOUNTER($schedulable, $component, $params);
1151                    Config::regSet('noAlarm', FALSE);
1152                } else {
1153                    $response = array();
1154                    $response['from'] = $params['from'];
1155                    $response['type'] = 'suggestionResponse';
1156                    $response['status'] = 'DECLINECOUNTER';
1157                    $response['schedulable'] = self::_getSchedulable($uid);
1158
1159                    Controller::create(array('concept' => 'notification'), $response);
1160                }
1161                break;
1162
1163            case 'DECLINECOUNTER':
1164                break;
1165
1166            default:
1167
1168                $schedulable = self::_getSchedulable($uid);
1169
1170                if ($schedulable && ( self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence'])) { //Caso o evento exista
1171                    $interation = self::_makeVEVENT($schedulable, $component, $params);
1172
1173                    if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {
1174                        $calendarToSchedulable = array();
1175                        $calendarToSchedulable['calendar'] = $params['calendar'];
1176                        $calendarToSchedulable['schedulable'] = $schedulable['id'];
1177                        $interation['calendarToSchedulable://' . mt_rand() . '(Formatter)'] = $calendarToSchedulable;
1178                    }
1179                }
1180                else // Importar evento
1181                $interation = self::_makeVEVENT(array(), $component, $params);
1182
1183                break;
1184            }
1185   
1186            $return[] = $interation;
1187        break;
1188        /***********************************************************************TODO*******************************************************************************/
1189        case 'VTIMEZONE':
1190                break;
[6066]1191            }
1192        }
1193        return $return;
1194    }
1195
1196    public function analize($data, $params = false) {
[7799]1197        $vcalendar = new icalCreatorVcalendar( );
[6066]1198        $vcalendar->parse(trim($data));
1199        $vcalendar->sort();
1200
1201        $return = array();
1202        $method = $vcalendar->getProperty('method', FALSE, FALSE);
1203
1204        while ($component = $vcalendar->getComponent()) {
1205            $interation = array();
1206            $uid = $component->getProperty('uid', false, false); //Resgata o uid do componente
1207            switch (strtoupper($component->objName)) {
1208                case 'VEVENT':
1209
1210                    switch ($method) {
1211                        case 'PUBLISH':
[6996]1212                            $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'calendarIds');
[6066]1213                            break;
1214
1215                        case 'REQUEST':
1216                            $schedulable = self::_getSchedulable($uid);
1217                            if ($schedulable) { //Caso o evento exista
[7695]1218                    $isOrganizer = false;
1219                    $isParticipant = false;
[6066]1220
[7695]1221                    foreach ($schedulable['participants'] as $value){
[6066]1222
[7695]1223                        if ($value['user']['id'] == $params['owner']) {
1224                            $isParticipant = true;
1225                            if ($value['isOrganizer'])
1226                                $isOrganizer = true;
[5863]1227
[7695]1228                            if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {
1229                                $interation = ICAL_ACTION_UPDATE;
1230                                $interation = ( strrpos($value['acl'], ATTENDEE_ACL_PARTICIPATION_REQUIRED) ) ? ICAL_ACTION_IMPORT_REQUIRED : array('action' => ICAL_ACTION_IMPORT, 'type' => 'calendarIds');
1231                                break;
1232                            }
1233                        } else {
1234                            if (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence']) //Organizador esta requisitando que você atualize o evento
1235                                $interation = ($isOrganizer) ? ICAL_ACTION_ORGANIZER_UPDATE : ICAL_ACTION_UPDATE;
1236                            else
1237                                $interation = ($isOrganizer) ? ICAL_ACTION_ORGANIZER_NONE : ICAL_ACTION_NONE;
1238                        }
[6331]1239
[7695]1240                    }
1241
1242                    if (!$isParticipant){
1243                        if( self::_existInMyCalendars($schedulable['id'], $params['owner']) ){
1244
1245                            $interation = (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] ? ICAL_ACTION_UPDATE  :  ICAL_ACTION_NONE);
1246
1247                        }else{
1248
1249                            $interation =  self::_checkParticipantByPermissions($schedulable);
1250
1251                        }
1252
1253
1254                    }
1255                }else
1256                    $interation = array('action' => ICAL_ACTION_IMPORT_REQUIRED, 'type' => 'calendarIds');
1257
1258                    if(($interation != ICAL_ACTION_NONE) && ($interation != ICAL_ACTION_ORGANIZER_NONE) && ($interation != ICAL_ACTION_ORGANIZER_UPDATE) && ($interation != ICAL_ACTION_NONE) && ($interation != ICAL_ACTION_UPDATE) && (!is_array($interation ) )) {
1259                        if($params['owner'] != Config::me("uidNumber")){
1260                            $sig = Controller::find(array('concept' => 'calendarSignature'), array('calendar'), array('filter' => array('AND', array('=', 'user', $params['owner']), array('=', 'isOwner', '1'))));
1261                            $calendars = array();
1262                            foreach ($sig as $val)
1263                            $calendars[] = $val['calendar'];
1264
1265                            $calendarsPermission = Controller::find(array('concept' => 'calendarToPermission'), array('calendar'), array('filter' => array('AND', array('=', 'user', Config::me("uidNumber")), array('IN', 'calendar', $calendars))));
1266
1267                            foreach ($calendarsPermission as $val)
1268                            $ids[] = $val['calendar'];
1269
1270                            $interation = array('action' => ICAL_ACTION_IMPORT_FROM_PERMISSION ,'calendar' => $ids);
1271                        }
1272                    }
1273
[6066]1274                            break;
1275
1276                        case 'REFRESH':
1277                            break;
1278
1279                        case 'CANCEL':
1280                            $interation = ICAL_ACTION_DELETE;
1281                            break;
1282
1283                        case 'ADD':
1284                            break;
1285
1286                        case 'REPLY':
[6861]1287                                if ($schedulable = self::_getSchedulable($uid)) {
1288                                        while ($property = $component->getProperty('attendee', FALSE, TRUE))
1289                                            if ($attendee = self::_getParticipantByMail(str_replace('MAILTO:', '', $property['value']), $schedulable['participants'], true))
1290                                                        $interation = (constant('STATUS_' . strtoupper($property['params']['PARTSTAT'])) == $attendee['status']) ?  ICAL_ACTION_NONE : ICAL_ACTION_REPLY;
1291                            }else
1292                                $interation = ICAL_NOT_FOUND;
[6066]1293                            break;
1294
1295                        case 'COUNTER':
1296                            $interation = ICAL_ACTION_SUGGESTION;
1297                            break;
1298
1299                        case 'DECLINECOUNTER':
1300                            $interation = ICAL_ACTION_NONE;
1301                            break;
1302
1303                        default:
1304                            $schedulable = self::_getSchedulable($uid);
1305
1306                            if ($schedulable && ( self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence'])) //Caso o evento exista
1307                                $interation = ICAL_ACTION_UPDATE;
1308                            else if ($schedulable)
1309                                $interation = ICAL_ACTION_NONE;
1310                            else // Importar evento
[6996]1311                                $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'calendarIds');
[6066]1312
1313                            break;
1314                    }
1315
1316                    $return[$uid] = $interation;
1317                    break;
[6996]1318                case 'VTODO':
1319                    switch ($method) {
1320                        case 'PUBLISH':
1321                            $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'groupIds');
1322                            break;
[6066]1323
[6996]1324                        case 'REQUEST':
1325                            $schedulable = self::_getSchedulable($uid);
1326                           
1327                            if ($schedulable) { //Caso o evento exista
1328                                $isOrganizer = false;
1329                                $isParticipant = false;
[6066]1330
[6996]1331                                foreach ($schedulable['participants'] as $value)
1332                                    if ($value['user']['id'] == $params['owner']) {
1333                                        $isParticipant = true;
1334                                        if ($value['isOrganizer'])
1335                                            $isOrganizer = true;
1336
1337                                        if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {   
1338                                            $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'groupIds');
1339                                            break;
1340                                        }
1341                                    } else {
[7704]1342
1343                        ///Atualiza o Caldadav mesmo que o expresso não prescisse de atualização, pois os calendarios do caldav são independentes um de cada usuario diferente do expresso que so tem 1 evento e é compartilhado entre os usuarios
1344                        if (Config::module('useCaldav', 'expressoCalendar')) { //Ignorar Put dos eventos ja vindos do caldav
1345                            require_once ROOTPATH . '/modules/calendar/interceptors/DAViCalAdapter.php';
1346                            $calendars = self::schedulable2calendarToObject($schedulable['id'] , isset($params['owner']) ? $params['owner']: false ); //Busca os calendarios do usuario logado que contenham o evento
1347                            if (is_array($calendars))
1348                                foreach ($calendars as $calendar)
1349                                    DAViCalAdapter::putIcal($data, array('uid' => $schedulable['uid'], 'location' => $calendar['calendar_location']));
1350                        }
1351
[6996]1352                                        if (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence']) //Organizador esta requisitando que você atualize o evento
1353                                            $interation = ($isOrganizer) ? ICAL_ACTION_ORGANIZER_UPDATE : ICAL_ACTION_UPDATE;
1354                                        else
1355                                            $interation = ($isOrganizer) ? ICAL_ACTION_ORGANIZER_NONE : ICAL_ACTION_NONE;
1356                                    }
1357                                if (!$isParticipant){
1358                                      $interation = self::_checkParticipantByPermissions($schedulable);
1359                                    }
1360                            }else
1361                                $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'groupIds');
1362                            break;
1363
1364                        case 'REFRESH':
1365                            break;
1366
1367                        case 'CANCEL':
1368                            $interation = ICAL_ACTION_DELETE;
1369                            break;
1370
1371                        case 'ADD':
1372                            break;
1373
1374                        case 'REPLY':
1375                            $interation = ICAL_ACTION_REPLY;
1376                            break;
1377
1378                        case 'COUNTER':
1379                            $interation = ICAL_ACTION_SUGGESTION;
1380                            break;
1381
1382                        case 'DECLINECOUNTER':
1383                            $interation = ICAL_ACTION_NONE;
1384                            break;
1385
1386                        default:
1387                            $schedulable = self::_getSchedulable($uid);
1388
1389                            if ($schedulable && ( self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence'])) //Caso o evento exista
1390                                $interation = ICAL_ACTION_UPDATE;
1391                            else if ($schedulable)
1392                                $interation = ICAL_ACTION_NONE;
1393                            else // Importar evento
1394                                $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'groupIds');
1395
1396                            break;
1397                    }
1398
1399                    $return[$uid] = $interation;
[6066]1400                    break;
[6996]1401                case 'VTIMEZONE':
1402                break;
[6066]1403            }
1404        }
1405
1406        return $return;
1407    }
1408
1409    /* Helpers */
1410
1411    private static function _getTzOffset($rTz, $oTz = null, $time = 'now') {
1412        if ($oTz === null) {
1413            if (!is_string($oTz = date_default_timezone_get())) {
1414                return false; // A UTC timestamp was returned -- bail out!
1415            }
1416        }
1417        $origin_dtz = new DateTimeZone(self::nomalizeTZID($oTz));
1418        $remote_dtz = new DateTimeZone(self::nomalizeTZID($rTz));
1419        $origin_dt = new DateTime($time, $origin_dtz);
1420        $remote_dt = new DateTime("now", $remote_dtz);
1421
1422        $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
1423
1424
1425        return $offset;
1426    }
1427
1428    private function _getStatus($id) {
1429        $a = array(
1430            STATUS_CONFIRMED => 'ACCEPTED',
1431            STATUS_CANCELLED => 'CANCELLED',
1432            STATUS_TENTATIVE => 'TENTATIVE',
1433            STATUS_UNANSWERED => 'NEEDS-ACTION',
1434            STATUS_DELEGATED => 'DELEGATED'
1435        );
1436
1437        return isset($a[$id]) ? $a[$id] : 'NEEDS-ACTION';
1438    }
1439
[6996]1440    public function decodeStatusTodo( $action )
1441    {
1442     $a = array(
1443        'NEED_ACTION' => STATUS_TODO_NEED_ACTION,
1444        'IN_PROGRESS' => STATUS_TODO_IN_PROGRESS  ,
1445        'COMPLETED' =>  STATUS_TODO_COMPLETED ,
1446        'CANCELLED'  => STATUS_TODO_CANCELLED
1447    );
1448
[7028]1449    return isset($a[$action]) ? $a[$action] : 'STATUS_TODO_NEED_ACTION';
[6996]1450   
1451    }
1452
1453    private function _getStatusTodo($id) {
1454        $a = array(
1455            STATUS_TODO_NEED_ACTION => 'NEED_ACTION',
1456            STATUS_TODO_IN_PROGRESS => 'IN_PROGRESS',
1457            STATUS_TODO_COMPLETED => 'COMPLETED',
1458            STATUS_TODO_CANCELLED => 'CANCELLED'
1459        );
1460
1461    return isset($a[$id]) ? $a[$id] : 'NEED_ACTION';
1462    }
1463
[6295]1464    private static function _checkParticipantByPermissions($schedulable) {
1465
[7006]1466        $calendarIds = Controller::find(array('concept' => 'calendarSignature'), array('calendar'), array('filter' => array('AND', array('=','isOwner','0'), array('=', 'user', Config::me("uidNumber")))));
[6295]1467
[7006]1468        if($calendarIds && isset($calendarIds[0])){
1469            $ids = array();
1470            foreach($calendarIds as $value)
1471                    array_push($ids, $value['calendar']);
[6295]1472
[7006]1473            $signaturesOfOwners = Controller::find(array('concept' => 'calendarSignature'), false, array('filter' => array('AND', array('IN', 'calendar', $ids) , array('=','isOwner','1')), 'deepness' => 2 ));
1474           
1475            foreach($signaturesOfOwners as $value){
1476                        if(self::_getParticipantByMail($value['user']['mail'], $schedulable['participants'])){
1477                            $eventoFromCalendar = Controller::read( array( 'concept' => 'calendarToSchedulable') , false, array('filter' => array('AND', array('=','schedulable',$schedulable['id']), array('=','calendar', $value['calendar']['id']))));
[6295]1478
[7006]1479                            return  ($eventoFromCalendar && isset($eventoFromCalendar[0])) ? ICAL_ACTION_NONE_FROM_PERMISSION : array('action' => ICAL_ACTION_IMPORT_FROM_PERMISSION, 'calendar' => array($value['calendar']['id']) );
1480                        }
1481            }   
1482        }
1483        return array('action' => ICAL_ACTION_IMPORT, 'type' => 'calendarIds');
[6295]1484    }
[7704]1485
[7006]1486    private static function _getParticipantByMail($mail, &$participants, $isFull = false) {
[6066]1487        if ($participants && $participants != '')
1488            foreach ($participants as $i => $v)
1489                if ((is_array($v) && isset($v['user'])) && ($v['user']['mail'] == $mail || (isset($v['user']['mailAlternateAddress']) && in_array($mail, $v['user']['mailAlternateAddress']))))
[6861]1490                      return !!$isFull ? $v : $v['id'];
[6066]1491        return false;
1492    }
1493
1494    static private function nomalizeTZID($TZID) {
1495        if (isset(self::$timezonesMap[$TZID]))
1496            return self::$timezonesMap[$TZID];
1497        else if (in_array($TZID, self::$suportedTimzones))
1498            return $TZID;
1499        else
1500            return date_default_timezone_get();
1501    }
1502
1503    static private function date2timestamp($datetime, $tz = null) {
1504        if (!isset($datetime['hour']))
1505            $datetime['hour'] = '0';
1506        if (!isset($datetime['min']))
1507            $datetime['min'] = '0';
1508        if (!isset($datetime['sec']))
1509            $datetime['sec'] = '0';
1510
1511        foreach ($datetime as $dkey => $dvalue)
1512            if ('tz' != $dkey)
1513                $datetime[$dkey] = (integer) $dvalue;
1514
1515        if ($tz)
1516            $datetime['tz'] = $tz;
1517
1518        $offset = ( isset($datetime['tz']) && ( '' < trim($datetime['tz']))) ? iCalUtilityFunctions::_tz2offset($datetime['tz']) : 0;
1519
1520        return gmmktime($datetime['hour'], $datetime['min'], ($datetime['sec'] + $offset), $datetime['month'], $datetime['day'], $datetime['year']);
1521    }
1522
1523    static private function _makeCOUNTER($schedulable, $component, $params) {
1524        $interation = array();
1525        $eventID = isset($schedulable['id']) ? $schedulable['id'] : mt_rand() . '(Formatter)';
1526
1527        /* Data de Inicio */
1528        $startTime = $component->getProperty('dtstart', false, true);
1529
1530        /* Tiem zone do evento */
1531        if (isset($startTime['params']['TZID']))
1532            $schedulable['timezone'] = self::nomalizeTZID($startTime['params']['TZID']);
1533        else
1534            $schedulable['timezone'] = isset($params['calendar_timezone']) ? $params['calendar_timezone'] : 'America/Sao_Paulo';
1535
1536        $objTimezone = new DateTimeZone($schedulable['timezone']);
1537
1538        if ($startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone'])) {
1539            $schedulable['allDay'] = 1;
1540            $schedulable['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $schedulable['timezone'], '@' . self::date2timestamp($startTime['value'])) . '000';
1541        } elseif (isset($startTime['params']['TZID']) && !isset($startTime['value']['tz'])) {/* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
1542            $schedulable['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $startTime['params']['TZID'], '@' . self::date2timestamp($startTime['value'])) . '000';
1543            $schedulable['allDay'] = 0;
1544        } else {
1545            $schedulable['startTime'] = self::date2timestamp($startTime['value']) . '000';
1546            if (strpos($params['prodid'], 'Outlook') !== false) {
1547                //Se o ics veio em utc não aplicar horario de verão
1548                $sTime = new DateTime('@' . (int) ($schedulable['startTime'] / 1000), new DateTimeZone('UTC'));
1549                $sTime->setTimezone($objTimezone);
1550                if ($sTime->format('I')) //Se o ics veio em utc não aplicar horario de verão
1551                    $schedulable['startTime'] = $schedulable['startTime'] - 3600000;
1552            }
1553        }
1554
1555        /* Data de Termino */
1556        $endTime = $component->getProperty('dtend', false, true);
1557
1558        if ($endTime['params']['VALUE'] === 'DATE')
1559            $schedulable['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $schedulable['timezone'], '@' . self::date2timestamp($endTime['value'])) . '000';
1560        else if (isset($endTime['params']['TZID']) && !isset($endTime['value']['tz'])) /* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
1561            $schedulable['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $endTime['params']['TZID'], '@' . self::date2timestamp($endTime['value'])) . '000';
1562        else {
1563            $schedulable['endTime'] = self::date2timestamp($endTime['value']) . '000';
1564            if (strpos($params['prodid'], 'Outlook') !== false) {
1565                //Se o ics veio em utc não aplicar horario de verão
1566                $eTime = new DateTime('@' . (int) ($schedulable['endTime'] / 1000), new DateTimeZone('UTC'));
1567                $eTime->setTimezone($objTimezone);
1568                if ($eTime->format('I'))
1569                    $schedulable['endTime'] = $schedulable['endTime'] - 3600000;
1570            }
1571        }
1572        unset($schedulable['participants']);
1573        $interation['schedulable://' . $eventID] = $schedulable;
1574
1575        return $interation;
1576    }
1577
[7006]1578    static private function _makeVEVENT($schedulable, $component, $params) {
1579        $interation = array();
1580        $eventID = isset($schedulable['id']) ? $schedulable['id'] : mt_rand() . '(Formatter)';
[6066]1581
[7006]1582        /* Data de Inicio */
1583        $startTime = $component->getProperty('dtstart', false, true);
[6066]1584
[7006]1585        $tzid = (isset($startTime['params']['TZID']) ? $startTime['params']['TZID'] : $params['X-WR-TIMEZONE']);
[6248]1586
[7006]1587        /* Tiem zone do evento */
1588        if ($tzid){
1589            $tzid = self::nomalizeTZID($tzid);
1590            $schedulable['timezone'] = $tzid;
1591        }else
1592            $schedulable['timezone'] = isset($params['calendar_timezone']) ? $params['calendar_timezone'] : 'America/Sao_Paulo';
[6066]1593
[7006]1594        $objTimezone = new DateTimeZone($schedulable['timezone']);
[6066]1595
[7702]1596        if (isset($startTime['params']['VALUE']) && $startTime['params']['VALUE'] === 'DATE' ) {
[7006]1597            $schedulable['allDay'] = 1;
1598            $schedulable['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $schedulable['timezone'], '@' . self::date2timestamp($startTime['value'])) . '000';
1599        } elseif ($tzid && !isset($startTime['value']['tz'])) {/* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
1600            $schedulable['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $tzid, '@' . self::date2timestamp($startTime['value'])) . '000';
1601            $schedulable['allDay'] = 0;
1602        } else {
1603            $schedulable['startTime'] = self::date2timestamp($startTime['value']) . '000';
1604            if (strpos($params['prodid'], 'Outlook') !== false) {
1605                //Se o ics veio em utc não aplicar horario de verão
1606                $sTime = new DateTime('@' . (int) ($schedulable['startTime'] / 1000), new DateTimeZone('UTC'));
1607                $sTime->setTimezone($objTimezone);
1608                if ($sTime->format('I')) //Se o ics veio em utc não aplicar horario de verão
1609                    $schedulable['startTime'] = $schedulable['startTime'] - 3600000;
1610            }
1611        }
[6066]1612
[7006]1613        /* Data de Termino */
1614        $endTime = $component->getProperty('dtend', false, true);
[6066]1615
[7006]1616        $tzid = isset($endTime['params']['TZID']) ? $endTime['params']['TZID'] : $params['X-WR-TIMEZONE'];
1617       
1618        if($tzid)
1619            $tzid = self::nomalizeTZID($tzid);
[6215]1620
[7006]1621        if (isset($endTime['params']['VALUE']) && $endTime['params']['VALUE'] === 'DATE')
1622            $schedulable['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $schedulable['timezone'], '@' . self::date2timestamp($endTime['value'])) . '000';
1623        else if ($tzid && !isset($endTime['value']['tz'])) /* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
1624            $schedulable['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $tzid, '@' . self::date2timestamp($endTime['value'])) . '000';
1625        else {
1626            $schedulable['endTime'] = self::date2timestamp($endTime['value']) . '000';
1627            if (strpos($params['prodid'], 'Outlook') !== false) {
1628                //Se o ics veio em utc não aplicar horario de verão
1629                $eTime = new DateTime('@' . (int) ($schedulable['endTime'] / 1000), new DateTimeZone('UTC'));
1630                $eTime->setTimezone($objTimezone);
1631                if ($eTime->format('I'))
1632                    $schedulable['endTime'] = $schedulable['endTime'] - 3600000;
1633            }
1634        }
[6066]1635
1636
[7006]1637        $schedulable['summary'] = mb_convert_encoding($component->getProperty('summary', false, false), 'ISO-8859-1', 'UTF-8,ISO-8859-1');
[6066]1638
[7006]1639        /* Definindo Description */
1640        if ($desc = $component->getProperty('description', false, false))
1641            $schedulable['description'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $desc), 'ISO-8859-1', 'UTF-8,ISO-8859-1');
[6066]1642
[7006]1643        /* Definindo location */
1644        if ($location = $component->getProperty('location', false, false))
1645            $schedulable['location'] = mb_convert_encoding($location, 'ISO-8859-1', 'UTF-8,ISO-8859-1');
[6066]1646
1647
[7006]1648        /* Definindo Class */
1649        $class = $component->getProperty('class', false, false);
1650        if ($class && defined(constant(strtoupper('CLASS_' . $class))))
1651            $schedulable['class'] = constant(strtoupper('CLASS_' . $class));
1652        else if (!isset($schedulable['class']))
1653            $schedulable['class'] = CLASS_PRIVATE; // padrão classe private
[6066]1654
[7006]1655        /* Definindo RRULE */
1656        if ($rrule = $component->getProperty('rrule', false, false)) {
1657            /* Gera um randon id para o contexto formater */
1658            $repeatID = mt_rand() . '3(Formatter)';
[6066]1659
[7006]1660            $repeat = array();
1661            $repeat['schedulable'] = $eventID;
[7660]1662            $repeat['startTime'] = $schedulable['startTime'];
1663            foreach ($rrule as $i => $v) {
[7006]1664                if (strtolower($i) == 'freq')
1665                    $repeat['frequency'] = $v;
1666                else if (strtolower($i) == 'until')
[7660]1667            {
1668                $repeat['endTime'] = strtotime($v['year'].'-'.$v['month'].'-'.$v['day'].' '.$v['hour'].':'.$v['min'].':'.$v['sec'].' '.$v['tz']) .'000' ;
1669            }
[7006]1670                else
1671                    $repeat[strtolower($i)] = $v;
1672            }
[5415]1673
[7006]1674            $interation['repeat://' . $repeatID] = $repeat;
1675        }
[5415]1676
[7006]1677        $schedulable['calendar'] = $params['calendar'];
[5415]1678
[7006]1679        $participantsInEvent = array();
[5415]1680
[7006]1681        //TODO: Participants com delegated nao estao sendo levados em conta
1682        while ($property = $component->getProperty('attendee', FALSE, TRUE)) {
1683            $participant = array();
[5415]1684
[7006]1685            $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
[5415]1686
[7006]1687            $participantID = ($tpID = self::_getParticipantByMail($mailUser, $schedulable['participants'])) ? $tpID : mt_rand() . '2(Formatter)';
1688            $participant['schedulable'] = $eventID;
[5415]1689
[7006]1690            if (isset($params['status']) && $mailUser == Config::me('mail'))
1691                $participant['status'] = $params['status'];
1692            else
1693                $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_' . $property['params']['PARTSTAT']) !== null ) ? constant('STATUS_' . $property['params']['PARTSTAT']) : STATUS_UNANSWERED;
[5415]1694
1695
[7006]1696            $participant['isOrganizer'] = '0';
[5415]1697
[7006]1698            /* Verifica se este usuario é um usuario interno do ldap */
1699            $intUser = Controller::find(array('concept' => 'user'), array('id', 'isExternal'), array('filter' => array('OR', array('=', 'mail', $mailUser), array('=', 'mailAlternateAddress', $mailUser))));
[5415]1700
[7702]1701            $user = null;
[7006]1702            if ($intUser && count($intUser) > 0) {
1703                $participant['isExternal'] = isset($intUser[0]['isExternal']) ? $intUser[0]['isExternal'] : 0;
1704                $participant['user'] = $intUser[0]['id'];
1705            } else {
1706                $participant['isExternal'] = 1;
1707                /* Gera um randon id para o contexto formater */
1708                $userID = mt_rand() . '4(Formatter)';
[5415]1709
[7006]1710                $user['mail'] = $mailUser;
1711                $user['isExternal'] = '1';
1712                $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
1713                $user['participants'] = array($participantID);
1714                $participant['user'] = $userID;
1715                $interation['user://' . $userID] = $user;
1716            }
[5415]1717
[7006]1718            $interation['participant://' . $participantID] = $participant;
1719            $schedulable['participants'][] = $participantID;
1720        };
[5415]1721
[7006]1722        if ($property = $component->getProperty('organizer', FALSE, TRUE)) {
[7702]1723
[7006]1724            $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
[5415]1725
1726
[7702]1727            if($participant = self::_getParticipantByMail($mailUser, $schedulable['participants'], true)){
[5415]1728
[7702]1729                $participantID = $participant['id'];
1730
1731            }else{
1732
1733                $participant = array();
1734
1735                $participantID = mt_rand() . '2(Formatter)';
1736                $participant['schedulable'] = $eventID;
1737                $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_' . $property['params']['PARTSTAT']) !== null ) ? constant('STATUS_' . $property['params']['PARTSTAT']) : STATUS_UNANSWERED;
1738                $participant['isOrganizer'] = '1';
1739                $participant['acl'] = 'rowi';
1740            }
1741
[7006]1742            /* Verifica se este usuario é um usuario interno do ldap */
1743            $intUser = Controller::find(array('concept' => 'user'), array('id', 'isExternal'), array('filter' => array('OR', array('=', 'mail', $mailUser), array('=', 'mailAlternateAddress', $mailUser))));
1744            $user = null;
1745            if ($intUser && count($intUser) > 0) {
[7702]1746                $participant['isExternal'] = isset($intUser[0]['isExternal']) ? $intUser[0]['isExternal'] : 0;
1747                $participant['user'] = $intUser[0]['id'];
[7006]1748            } else {
[7702]1749                $participant['isExternal'] = 1;
1750                /* Gera um randon id para o contexto formater */
1751                $userID = mt_rand() . '4(Formatter)';
[5415]1752
[7702]1753                $user['mail'] = $mailUser;
1754                $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
1755                $user['participants'] = array($participantID);
1756                $user['isExternal'] = '1';
1757                $participant['user'] = $userID;
1758                $interation['user://' . $userID] = $user;
[7006]1759            }
[5415]1760
[7006]1761            $interation['participant://' . $participantID] = $participant;
1762            $schedulable['participants'][] = $participantID;
1763        } else if (!isset($schedulable['participants']) || !is_array($schedulable['participants']) || count($schedulable['participants']) < 1) {//caso não tenha organizador o usuario se torna organizador
1764            $user = Controller::read(array('concept' => 'user', 'id' => $params['owner']), array('mail'));
[5415]1765
[7006]1766            if (!self::_getParticipantByMail($user['mail'], $schedulable['participants'])) {
1767                        $participantID = mt_rand() . '2(Formatter)';
[5415]1768
[7006]1769                        $participant['schedulable'] = $eventID;
1770                        $participant['status'] = STATUS_CONFIRMED;
1771                        $participant['isOrganizer'] = '1';
1772                        $participant['acl'] = 'rowi';
1773                        $participant['isExternal'] = 0;
1774                        $participant['user'] = $params['owner'];
1775                        $interation['participant://' . $participantID] = $participant;
1776                        $schedulable['participants'][] = $participantID;
1777            }
1778        }
1779       
1780        $alarms = array();
1781       
1782        /* Definindo ALARMES */
1783        while ($alarmComp = $component->getComponent('valarm'))
1784        {
1785                $alarm = array();
1786                $alarmID = mt_rand() . '6(Formatter)';
1787                $action =  $alarmComp->getProperty('action', false, true);
1788                $trygger = $alarmComp->getProperty('trigger', false, true);
1789                $alarm['type'] = self::decodeAlarmAction($action['value']);
[5415]1790
[7006]1791                 if(isset($trygger['value']['day']))
1792                {
1793                        $alarm['time'] = $trygger['value']['day'];
1794                        $alarm['unit'] = 'd';
1795                }
1796                else if(isset($trygger['value']['hour']))
1797                {
1798                        $alarm['time'] = $trygger['value']['hour'];
1799                        $alarm['unit'] = 'h';
1800                }
1801                else if(isset($trygger['value']['min']))
1802                {
1803                        $alarm['time'] = $trygger['value']['min'];
1804                        $alarm['unit'] = 'm';
1805                }
1806               
1807                foreach ($interation as $iint => &$vint)
1808                {
1809                        if(isset($vint['user']) && $vint['user'] == Config::me('uidNumber'))
1810                        {
1811                                $alarm['participant'] = str_replace('participant://', '', $iint);       
1812                                $vint['alarms'][] = $alarmID;
1813                        }
1814                }
1815                $alarm['schedulable'] = $eventID;
1816                               
1817                $interation['alarm://' . $alarmID ] = $alarm;
1818               
1819        }
1820       
1821       
1822        /* Definindo DTSTAMP */
1823        if ($dtstamp = self::_getTime($component, 'dtstamp'))
1824            $schedulable['dtstamp'] = $dtstamp;
[5415]1825
[7006]1826        /* Definindo TRANSP */
1827        if (($tranp = $component->getProperty('transp', false, true)) && $tranp && is_string($tranp) && strtoupper($tranp) == 'OPAQUE')
1828            $schedulable['transparent'] = 1;
[5415]1829
[7006]1830        /* Definindo last_update */
1831        if ($lastUpdate = self::_getTime($component, 'LAST-MODIFIED'))
1832            $schedulable['lastUpdate'] = $lastUpdate;
[5415]1833
1834
[7006]1835        if ($sequence = $component->getProperty('SEQUENCE', false, false))
1836            $schedulable['sequence'] = $sequence;
[5415]1837
[7006]1838        if ($uid = $component->getProperty('uid', false, false))
1839            ;
1840        $schedulable['uid'] = $uid;
[5415]1841
[7006]1842        while ($attach = $component->getProperty('ATTACH', FALSE, TRUE)) {
[5415]1843
[7006]1844            $attachCurrent = array('name' => $attach['params']['X-FILENAME'],
1845                'size' => strlen($attach['value']),
1846                'type' => self::_getContentType($attach['params']['X-FILENAME'])
1847            );
[5514]1848
[7006]1849            $ids = Controller::find(array('concept' => 'attachment'), array('id'), array('filter' => array('AND', array('=', 'name', $attachCurrent['name']), array('=', 'size', $attachCurrent['size']), array('=', 'type', $attachCurrent['type']))));
[5514]1850
[7006]1851            if (!is_array($ids)) {
1852                $attachCurrent['source'] = $attach['value'];
1853                //insere o anexo no banco e pega id para colcar no relacionamento                               
1854                $idAttachment = Controller::create(array('concept' => 'attachment'), $attachCurrent);
1855            }else
1856                $idAttachment = array('id' => $ids[0]['id']);
[5514]1857
[7006]1858            $calendarToAttachmentId = mt_rand() . '2(Formatter)';
1859            $calendarToAttachment['attachment'] = $idAttachment['id'];
1860            $calendarToAttachment['schedulable'] = $eventID;
1861            $interation['schedulableToAttachment://' . $calendarToAttachmentId] = $calendarToAttachment;
[5514]1862
[7006]1863            $schedulable['attachments'][] = $calendarToAttachmentId;
1864        }
[5514]1865
[7006]1866        $schedulable['type'] = '1';
[5514]1867
[7006]1868        $interation['schedulable://' . $eventID] = $schedulable;
1869
[7702]1870        return $interation;
[6066]1871    }
[6996]1872   
1873    static private function _makeVTODO($schedulable, $component, $params) {
1874        $interation = array();
1875        $todoID = isset($schedulable['id']) ? $schedulable['id'] : mt_rand() . '(Formatter)';
[5514]1876
[6996]1877        /* Data de Inicio */
1878        $startTime = $component->getProperty('dtstart', false, true);
1879
1880        $tzid = (isset($startTime['params']['TZID']) ? $startTime['params']['TZID'] : $params['X-WR-TIMEZONE']);
1881
1882        /* Tiem zone do evento */
1883        if ($tzid){
1884            $tzid = self::nomalizeTZID($tzid);
1885            $schedulable['timezone'] = $tzid;
1886        }else
1887            $schedulable['timezone'] = isset($params['calendar_timezone']) ? $params['calendar_timezone'] : 'America/Sao_Paulo';
1888
1889        $objTimezone = new DateTimeZone($schedulable['timezone']);
1890
1891        if (isset($startTime['params']['VALUE']) && $startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone'])) {
1892            $schedulable['allDay'] = 1;
1893            $schedulable['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $schedulable['timezone'], '@' . self::date2timestamp($startTime['value'])) . '000';
1894        } elseif ($tzid && !isset($startTime['value']['tz'])) {/* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
1895            $schedulable['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $tzid, '@' . self::date2timestamp($startTime['value'])) . '000';
1896            $schedulable['allDay'] = 0;
1897        } else {
1898            $schedulable['startTime'] = self::date2timestamp($startTime['value']) . '000';
1899            if (strpos($params['prodid'], 'Outlook') !== false) {
1900                //Se o ics veio em utc não aplicar horario de verão
1901                $sTime = new DateTime('@' . (int) ($schedulable['startTime'] / 1000), new DateTimeZone('UTC'));
1902                $sTime->setTimezone($objTimezone);
1903                if ($sTime->format('I')) //Se o ics veio em utc não aplicar horario de verão
1904                    $schedulable['startTime'] = $schedulable['startTime'] - 3600000;
1905            }
1906        }
1907
1908        /* Data de Termino */
[7068]1909        if($due = $component->getProperty('due', false, true)){
[6996]1910
[7068]1911        $tzid = isset($due['params']['TZID']) ? $due['params']['TZID'] : $params['X-WR-TIMEZONE'];
[6996]1912
[7068]1913        if($tzid)
1914            $tzid = self::nomalizeTZID($tzid);
[6996]1915
[7680]1916        if (isset($due['params']['VALUE']) && $due['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone']))
[7068]1917            $schedulable['due'] = self::date2timestamp($due['value']) - self::_getTzOffset('UTC', $schedulable['timezone'], '@' . self::date2timestamp($due['value'])) . '000';
[7680]1918        else if ($tzid && !isset($due['value']['tz'])) /* Caso não tenha um tz na data mais exista um parametro TZID deve ser aplicado o timezone do TZID a data */
[7068]1919            $schedulable['due'] = self::date2timestamp($due['value']) - self::_getTzOffset('UTC', $tzid, '@' . self::date2timestamp($due['value'])) . '000';
1920        else {
1921            $schedulable['due'] = self::date2timestamp($due['value']) . '000';
1922            if (strpos($params['prodid'], 'Outlook') !== false) {
1923            //Se o ics veio em utc não aplicar horario de verão
1924            $dueTime = new DateTime('@' . (int) ($schedulable['due'] / 1000), new DateTimeZone('UTC'));
1925            $dueTime->setTimezone($objTimezone);
1926
1927            if ($dueTime->format('I'))
1928                $schedulable['due'] = $schedulable['due'] - 3600000;
1929            }
1930        }
1931        $schedulable['endTime'] = $schedulable['due'];
1932    }else
1933        $schedulable['endTime'] = $schedulable['startTime'];
1934
[6996]1935        $schedulable['type'] = '2'; //type schedulable
1936        $schedulable['summary'] = mb_convert_encoding($component->getProperty('summary', false, false), 'ISO-8859-1', 'UTF-8,ISO-8859-1');
1937
1938        /* Definindo Description */
1939        if ($desc = $component->getProperty('description', false, false))
1940            $schedulable['description'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $desc), 'ISO-8859-1', 'UTF-8,ISO-8859-1');
1941
1942        /* Definindo Class */
1943        $class = $component->getProperty('class', false, false);
1944        if ($class && defined(constant(strtoupper('CLASS_' . $class))))
1945            $schedulable['class'] = constant(strtoupper('CLASS_' . $class));
1946        else if (!isset($schedulable['class']))
1947            $schedulable['class'] = CLASS_PRIVATE; // padrão classe private
1948
1949        $schedulable['calendar'] = $params['calendar'];
1950
1951        $participantsInTodo = array();
1952
1953        //TODO: Participants com delegated nao estao sendo levados em conta
1954        while ($property = $component->getProperty('attendee', FALSE, TRUE)) {
1955            $participant = array();
1956
1957            $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
1958
1959            $participantID = ($tpID = self::_getParticipantByMail($mailUser, $schedulable['participants'])) ? $tpID : mt_rand() . '2(Formatter)';
[7028]1960            $participant['schedulable'] = $todoID;
[6996]1961
1962            if (isset($params['status']) && $mailUser == Config::me('mail'))
1963                $participant['status'] = $params['status'];
1964            else
1965                $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_' . $property['params']['PARTSTAT']) !== null ) ? constant('STATUS_' . $property['params']['PARTSTAT']) : STATUS_UNANSWERED;
1966
1967
1968            $participant['isOrganizer'] = '0';
1969
1970            /* Verifica se este usuario é um usuario interno do ldap */
1971            $intUser = Controller::find(array('concept' => 'user'), array('id', 'isExternal'), array('filter' => array('OR', array('=', 'mail', $mailUser), array('=', 'mailAlternateAddress', $mailUser))));
1972
1973            $user = null;
1974            if ($intUser && count($intUser) > 0) {
1975                $participant['isExternal'] = isset($intUser[0]['isExternal']) ? $intUser[0]['isExternal'] : 0;
1976                $participant['user'] = $intUser[0]['id'];
1977            } else {
1978                $participant['isExternal'] = 1;
1979                /* Gera um randon id para o contexto formater */
1980                $userID = mt_rand() . '4(Formatter)';
1981
1982                $user['mail'] = $mailUser;
1983                $user['isExternal'] = '1';
1984                $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
1985                $user['participants'] = array($participantID);
1986                $participant['user'] = $userID;
1987                $interation['user://' . $userID] = $user;
1988            }
1989
1990            $interation['participant://' . $participantID] = $participant;
1991            $schedulable['participants'][] = $participantID;
1992        };
1993
1994        if ($property = $component->getProperty('organizer', FALSE, TRUE)) {
1995            $participant = array();
1996            $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
1997
[7702]1998        if($participant = self::_getParticipantByMail($mailUser, $schedulable['participants'], true)){
[6996]1999
[7702]2000            $participantID = $participant['id'];
[6996]2001
[7702]2002        }else{
2003
2004            $participant = array();
2005
2006            $participantID = mt_rand() . '2(Formatter)';
2007            $participant['schedulable'] = $todoID;
2008            $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_' . $property['params']['PARTSTAT']) !== null ) ? constant('STATUS_' . $property['params']['PARTSTAT']) : STATUS_UNANSWERED;
2009            $participant['isOrganizer'] = '1';
2010            $participant['acl'] = 'rowi';
2011        }
2012
[6996]2013            /* Verifica se este usuario é um usuario interno do ldap */
2014            $intUser = Controller::find(array('concept' => 'user'), array('id', 'isExternal'), array('filter' => array('OR', array('=', 'mail', $mailUser), array('=', 'mailAlternateAddress', $mailUser))));
2015
2016            $user = null;
2017            if ($intUser && count($intUser) > 0) {
2018                $participant['isExternal'] = isset($intUser[0]['isExternal']) ? $intUser[0]['isExternal'] : 0;
2019                $participant['user'] = $intUser[0]['id'];
2020            } else {
2021                $participant['isExternal'] = 1;
2022                /* Gera um randon id para o contexto formater */
2023                $userID = mt_rand() . '4(Formatter)';
2024
2025                $user['mail'] = $mailUser;
2026                $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
2027                $user['participants'] = array($participantID);
2028                $user['isExternal'] = '1';
2029                $participant['user'] = $userID;
2030                $interation['user://' . $userID] = $user;
2031            }
2032
2033            $interation['participant://' . $participantID] = $participant;
2034            $schedulable['participants'][] = $participantID;
2035            } else if (!isset($schedulable['participants']) || !is_array($schedulable['participants']) || count($schedulable['participants']) < 1) {//caso não tenha organizador o usuario se torna organizador
2036            $user = Controller::read(array('concept' => 'user', 'id' => $params['owner']), array('mail'));
2037
2038            if (!self::_getParticipantByMail($user['mail'], $schedulable['participants'])) {
2039                $participantID = mt_rand() . '2(Formatter)';
2040
2041                $participant['schedulable'] = $todoID;
2042                $participant['status'] = STATUS_CONFIRMED;
2043                $participant['isOrganizer'] = '1';
2044                $participant['acl'] = 'rowi';
2045                $participant['isExternal'] = 0;
2046                $participant['user'] = $params['owner'];
2047                $interation['participant://' . $participantID] = $participant;
2048                $schedulable['participants'][] = $participantID;
2049            }
2050        }
2051       
2052        $alarms = array();
2053       
2054        /* Definindo ALARMES */
2055        while ($alarmComp = $component->getComponent('valarm'))
2056        {
2057                $alarm = array();
2058                $alarmID = mt_rand() . '6(Formatter)';
2059                $action =  $alarmComp->getProperty('action', false, true);
2060                $trygger = $alarmComp->getProperty('trigger', false, true);
2061                $alarm['type'] = self::decodeAlarmAction($action['value']);
2062
2063                 if(isset($trygger['value']['day']))
2064                {
2065                        $alarm['time'] = $trygger['value']['day'];
2066                        $alarm['unit'] = 'd';
2067                }
2068                else if(isset($trygger['value']['hour']))
2069                {
2070                        $alarm['time'] = $trygger['value']['hour'];
2071                        $alarm['unit'] = 'h';
2072                }
2073                else if(isset($trygger['value']['min']))
2074                {
2075                        $alarm['time'] = $trygger['value']['min'];
2076                        $alarm['unit'] = 'm';
2077                }
2078               
2079                foreach ($interation as $iint => &$vint)
2080                {
2081                        if(isset($vint['user']) && $vint['user'] == Config::me('uidNumber'))
2082                        {
2083                                $alarm['participant'] = str_replace('participant://', '', $iint);       
2084                                $vint['alarms'][] = $alarmID;
2085                        }
2086                }
2087                $alarm['schedulable'] = $eventID;
2088                               
2089                $interation['alarm://' . $alarmID ] = $alarm;
2090               
2091        }
2092       
2093       
2094        /* Definindo DTSTAMP */
2095        if ($dtstamp = self::_getTime($component, 'dtstamp'))
2096            $schedulable['dtstamp'] = $dtstamp;
2097
2098        /* Definindo TRANSP */
2099        if (($tranp = $component->getProperty('transp', false, true)) && $tranp && is_string($tranp) && strtoupper($tranp) == 'OPAQUE')
2100            $schedulable['transparent'] = 1;
2101
2102        /* Definindo last_update */
2103        if ($lastUpdate = self::_getTime($component, 'LAST-MODIFIED'))
2104            $schedulable['lastUpdate'] = $lastUpdate;
2105
2106
2107        if ($sequence = $component->getProperty('SEQUENCE', false, false))
2108            $schedulable['sequence'] = $sequence;
2109
2110        if ($uid = $component->getProperty('uid', false, false))
2111            ;
2112        $schedulable['uid'] = $uid;
2113
2114        while ($attach = $component->getProperty('ATTACH', FALSE, TRUE)) {
2115
2116            $attachCurrent = array('name' => $attach['params']['X-FILENAME'],
2117                'size' => strlen($attach['value']),
2118                'type' => self::_getContentType($attach['params']['X-FILENAME'])
2119            );
2120
2121            $ids = Controller::find(array('concept' => 'attachment'), array('id'), array('filter' => array('AND', array('=', 'name', $attachCurrent['name']), array('=', 'size', $attachCurrent['size']), array('=', 'type', $attachCurrent['type']))));
2122
2123            if (!is_array($ids)) {
2124                $attachCurrent['source'] = $attach['value'];
2125                //insere o anexo no banco e pega id para colcar no relacionamento                               
2126                $idAttachment = Controller::create(array('concept' => 'attachment'), $attachCurrent);
2127            }else
2128                $idAttachment = array('id' => $ids[0]['id']);
2129
2130            $calendarToAttachmentId = mt_rand() . '2(Formatter)';
2131            $calendarToAttachment['attachment'] = $idAttachment['id'];
2132            $calendarToAttachment['schedulable'] = $eventID;
2133            $interation['schedulableToAttachment://' . $calendarToAttachmentId] = $calendarToAttachment;
2134
2135            $schedulable['attachments'][] = $calendarToAttachmentId;
2136        }
2137
2138        $interation['schedulable://' . $todoID] = $schedulable;
2139
[7068]2140    return $interation;
[6996]2141    }
2142
[6066]2143    static private function _getSchedulable($uid) {
2144        $schedulable = Controller::find(array('concept' => 'schedulable'), false, array('filter' => array('=', 'uid', $uid), 'deepness' => 2));
2145        return (isset($schedulable[0])) ? $schedulable[0] : false;
2146    }
[5514]2147
[6331]2148    static private function _existInMyCalendars($id, $owner) {
[7219]2149        $sig = Controller::find(array('concept' => 'calendarSignature'), array('user', 'calendar', 'isOwner'), array('filter' => array('AND', array('=', 'isOwner', '1'), array('=', 'user', $owner))));
2150        $sig2 = Controller::find(array('concept' => 'calendarToPermission'), array('calendar'), array('filter' => array('AND', array('*', 'acl', 'w'), array('=', 'user', $owner))));
2151
[6066]2152        $calendars = array();
[7219]2153        if(is_array($sig))
2154                foreach ($sig as $val)
2155                    $calendars[] = $val['calendar'];
2156        if(is_array($sig2))
2157                foreach ($sig2 as $val)
2158                    $calendars[] = $val['calendar'];
[6066]2159
[7219]2160
[6066]2161        $return = Controller::find(array('concept' => 'calendarToSchedulable'), null, array('filter' => array('AND', array('IN', 'calendar', $calendars), array('=', 'schedulable', $id))));
2162
2163        return (isset($return[0])) ? $return[0]['calendar'] : false;
2164    }
2165
2166    static private function _getTime(&$component, $property) {
2167        if ($date = $component->getProperty($property, false, true))
2168            return (isset($date['params']['TZID']) && !isset($date['value']['tz'])) ? (self::date2timestamp($date['value']) - self::_getTzOffset('UTC', $date['params']['TZID'], '@' . self::date2timestamp($date['value']))) . '000' : self::date2timestamp($date['value']) . '000';
2169
2170        return false;
2171    }
2172
2173    static private function _getContentType($fileName) {
2174        $strFileType = strtolower(substr($fileName, strrpos($fileName, '.')));
2175
2176        switch ($strFileType) {
2177            case ".asf": return "video/x-ms-asf";
2178            case ".avi": return "video/avi";
2179            case ".doc": return "application/msword";
2180            case ".zip": return "application/zip";
2181            case ".xls": return "application/vnd.ms-excel";
2182            case ".gif": return "image/gif";
2183            case ".bmp": return "image/bmp";
2184            case ".jpeg":
2185            case ".jpg": return "image/jpeg";
2186            case ".wav": return "audio/wav";
2187            case ".mp3": return "audio/mpeg3";
2188            case ".mpeg":
2189            case ".mpg": return "video/mpeg";
2190            case ".rtf": return "application/rtf";
2191            case ".html":
2192            case ".htm": return "text/html";
2193            case ".xml": return "text/xml";
2194            case ".xsl": return "text/xsl";
2195            case ".css": return "text/css";
2196            case ".php": return "text/php";
2197            case ".asp": return "text/asp";
2198            case ".pdf": return "application/pdf";
2199            case ".png": return "image/png";
2200            case ".txt": return "text/plain";
2201            case ".log": return "text/plain";
2202            case ".wmv": return "video/x-ms-wmv";
2203            case ".sxc": return "application/vnd.sun.xml.calc";
2204            case ".odt": return "application/vnd.oasis.opendocument.text";
2205            case ".stc": return "application/vnd.sun.xml.calc.template";
2206            case ".sxd": return "application/vnd.sun.xml.draw";
2207            case ".std": return "application/vnd.sun.xml.draw.template";
2208            case ".sxi": return "application/vnd.sun.xml.impress";
2209            case ".sti": return "application/vnd.sun.xml.impress.template";
2210            case ".sxm": return "application/vnd.sun.xml.math";
2211            case ".sxw": return "application/vnd.sun.xml.writer";
2212            case ".sxq": return "application/vnd.sun.xml.writer.global";
2213            case ".stw": return "application/vnd.sun.xml.writer.template";
2214            case ".pps": return "application/vnd.ms-powerpoint";
2215            case ".odt": return "application/vnd.oasis.opendocument.text";
2216            case ".ott": return "application/vnd.oasis.opendocument.text-template";
2217            case ".oth": return "application/vnd.oasis.opendocument.text-web";
2218            case ".odm": return "application/vnd.oasis.opendocument.text-master";
2219            case ".odg": return "application/vnd.oasis.opendocument.graphics";
2220            case ".otg": return "application/vnd.oasis.opendocument.graphics-template";
2221            case ".odp": return "application/vnd.oasis.opendocument.presentation";
2222            case ".otp": return "application/vnd.oasis.opendocument.presentation-template";
2223            case ".ods": return "application/vnd.oasis.opendocument.spreadsheet";
2224            case ".ots": return "application/vnd.oasis.opendocument.spreadsheet-template";
2225            case ".odc": return "application/vnd.oasis.opendocument.chart";
2226            case ".odf": return "application/vnd.oasis.opendocument.formula";
2227            case ".odi": return "application/vnd.oasis.opendocument.image";
2228            case ".ndl": return "application/vnd.lotus-notes";
2229            case ".eml": return "text/plain";
2230            case ".ps" : return "application/postscript";
2231            default : return "application/octet-stream";
2232        }
2233    }
[6094]2234   
2235    public function codeAlarmAction( $action )
2236    {
2237        switch ($action)
2238        {
2239                case ALARM_MAIL :
2240                                return  'EMAIL';
2241                                break;
2242                case ALARM_ALERT :
2243                                return  'DISPLAY';
2244                                break;
2245                case 'mail' :
2246                                        return  'EMAIL';
2247                                        break;
2248                case 'alert'  :
2249                                        return  'DISPLAY';
2250                                        break;
2251        }
2252       
2253    }
2254   
2255    public function decodeAlarmAction( $action )
2256    {
2257        switch ( $action )
2258        {
2259                case 'EMAIL'  :
2260                        return  'mail';
2261                        break;
2262                case 'DISPLAY' :
2263                        return  'alert';
2264                        break;
[6066]2265
[6094]2266        }
2267   
2268    }
2269
[7704]2270    private static function schedulable2calendarToObject($Schedulable, $user = false) {
2271        return Controller::service('PostgreSQL')->execResultSql('SELECT calendar_to_calendar_object.id as calendar_to_calendar_Object , calendar.name as calendar_name ,calendar.location as calendar_location, calendar.id as calendar_id FROM calendar_to_calendar_object , calendar , calendar_signature'
2272            . ' WHERE calendar_signature.user_uidnumber = ' . $user ? $user : Config::me('uidNumber')
2273            //      .' AND calendar_signature.is_owner = 1'
2274            . ' AND calendar_signature.calendar_id = calendar.id'
2275            . ' AND calendar_to_calendar_object.calendar_id = calendar.id'
2276            . ' AND calendar_to_calendar_object.calendar_object_id = ' . addslashes($Schedulable));
2277    }
[5341]2278}
[6066]2279
[5341]2280?>
Note: See TracBrowser for help on using the repository browser.