source: trunk/prototype/services/iCal.php @ 7218

Revision 7218, 105.8 KB checked in by angelo, 12 years ago (diff)

Ticket #3103 - Inconsistencia com a sincronizacao com davical

Line 
1<?php
2
3require_once ROOTPATH . '/plugins/icalcreator/iCalUtilityFunctions.class.php';
4require_once ROOTPATH . '/plugins/icalcreator/iCalcreator.class.php';
5require_once ROOTPATH . '/modules/calendar/constants.php';
6
7use prototype\api\Config as Config;
8
9//TODO:Timeout request
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');
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'); 
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');
17
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);
28
29    }
30
31    protected function createIcal($data, $params = false )
32    {
33        $ical = new icalCreator();
34                $ical->setProperty('method', isset($params['method']) ? $params['method'] : 'PUBLISH' );
35
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']);
42
43                foreach ($data as $i => $v) {
44
45                    switch ($v['type']) {
46                        case EVENT_ID:
47                            $vevent = $ical->newComponent('vevent');
48
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' );
53
54                            $timezone = new DateTimeZone('UTC');
55                            $apTimezone = self::nomalizeTZID(( isset($v['timezone']) && $v['timezone'] != 'null' ) ? $v['timezone'] : $params['defaultTZI']);
56                            $apTimezoneOBJ = new DateTimeZone($apTimezone);
57
58                            $sTime = new DateTime('@' . (int) ($v['startTime'] / 1000), $timezone);
59                            $sTime->setTimezone($apTimezoneOBJ);
60
61                            if (( isset($v['repeat']) ) && ( isset($v['repeat']['frequency']) && $v['repeat']['frequency'] && $v['repeat']['frequency'] != 'none' ))
62                                    $vevent->setProperty('rrule', $this->formatIcalRepeat($v['repeat']));
63                           
64
65                            if (isset($v['allDay']) && $v['allDay'] == 1)
66                {
67                    $vevent->setProperty('dtstart', $sTime->format(DATE_RFC822), array("VALUE" => "DATE"));
68                    $vevent->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'TRUE');
69                            } else
70                {
71                    $vevent->setProperty('dtstart', $sTime->format(DATE_RFC822), array('TZID' => $apTimezone));
72                    $vevent->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'FALSE');
73                            }
74                                           
75                            if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
76                                $participants = $v['participants'];
77                            else
78                                        $participants = Controller::find(array('concept' => 'participant'), false, array('filter' => array('=', 'schedulable', $v['id'])));
79                           
80                                if (is_array($participants) && count($participants) > 0)
81                                        foreach ($participants as $ii => $vv) {
82                                       
83                                                if(isset($participants[$ii]['user']) && !is_array($participants[$ii]['user']))
84                                                {
85                                                        if ($vv['isExternal'] == 1)
86                                                                $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user'], 'service' => 'PostgreSQL'));
87                                                        else
88                                                                $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user']));
89                                                }
90                                       
91                                                if ($participants[$ii]['user']['id'] == Config::me('uidNumber'))
92                                                {
93                                                        $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']))));
94                                                        if(is_array($alarms))
95                                                                self::createAlarms($alarms, $vevent);
96                                                }
97                                       
98                                        }
99                           
100                            if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
101                                        $this->createAttendee($v['participants'], $vevent);
102
103                            if (isset($v['attachments']) && is_array($v['attachments']) && count($v['attachments']) > 0)
104                                        $this->createAttachment($v['attachments'], $vevent);
105
106                            $vevent->setProperty('uid', $v['uid']); 
107                            break;
108            case TODO_ID:
109
110                $todo = $ical->newComponent('todo');
111
112                $todo->setProperty('summary', $v['summary']);
113                $todo->setProperty('description', isset($v['description']) ? $v['description'] : '');
114                $todo->setProperty('priority', $v['priority']);
115                $todo->setProperty('percent-complete', $v['percentage']);
116                $todo->setProperty('status', $this->_getStatusTodo($v['status']));
117
118                $timezone = new DateTimeZone('UTC');
119                $apTimezone = self::nomalizeTZID(( isset($v['timezone']) && $v['timezone'] != 'null' ) ? $v['timezone'] : $params['defaultTZI']);
120                $apTimezoneOBJ = new DateTimeZone($apTimezone);
121
122                $sTime = new DateTime('@' . (int) ($v['startTime'] / 1000), $timezone);
123                $sTime->setTimezone($apTimezoneOBJ);
124                $eTime = new DateTime('@' . (int) ($v['endTime'] / 1000), $timezone);
125                $eTime->setTimezone($apTimezoneOBJ);
126
127                if (isset($v['allDay']) && $v['allDay'] == 1) {
128                    $todo->setProperty('dtstart', $sTime->format(DATE_RFC822), array("VALUE" => "DATE"));
129                    $todo->setProperty('dtend', $eTime->format(DATE_RFC822), array("VALUE" => "DATE"));
130                    //$todo->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'TRUE');
131                } else {
132                    $todo->setProperty('dtstart', $sTime->format(DATE_RFC822), array('TZID' => $apTimezone));
133                    $todo->setProperty('dtend', $eTime->format(DATE_RFC822), array('TZID' => $apTimezone));
134                    //$todo->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'FALSE');
135                }
136
137                if(isset($v['due']) && $v['due'] != '' && (int)$v['due'] > 0){
138                    $dueTime = new DateTime('@' . (int) ($v['due'] / 1000), $timezone);
139                    $dueTime->setTimezone($apTimezoneOBJ);
140
141                    $todo->setProperty('due', $dueTime->format(DATE_RFC822), array('TZID' => $apTimezone));
142                    $todo->setProperty('dueTime', $dueTime->format(DATE_RFC822), array('TZID' => $apTimezone));
143                }               
144                       
145                if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
146                    $participants = $v['participants'];
147                else
148                    $participants = Controller::find(array('concept' => 'participant'), false, array('filter' => array('=', 'schedulable', $v['id'])));
149               
150                if (is_array($participants) && count($participants) > 0)
151                    foreach ($participants as $ii => $vv) {
152                   
153                        if(isset($participants[$ii]['user']) && !is_array($participants[$ii]['user']))
154                        {
155                            if ($vv['isExternal'] == 1)
156                                $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user'], 'service' => 'PostgreSQL'));
157                            else
158                                $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user']));
159                        }
160                   
161                        if ($participants[$ii]['user']['id'] == Config::me('uidNumber'))
162                        {
163                            $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']))));
164                            if(is_array($alarms))
165                                self::createAlarms($alarms, $todo);
166                        }
167                   
168                    }
169
170                if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
171                    $this->createAttendee($v['participants'], $todo);
172
173                if (isset($v['attachments']) && is_array($v['attachments']) && count($v['attachments']) > 0)
174                    $this->createAttachment($v['attachments'], $todo);
175
176                $todo->setProperty('uid', $v['uid']); 
177               
178                break;
179                        default:
180                            break;
181                    }
182                }
183                return $ical->createCalendar();
184    }
185
186    protected function createCompatibleIcal($data, $params = false )
187    {
188        $ical = new icalCreator();
189                $ical->setProperty('method', isset($params['method']) ? $params['method'] : 'PUBLISH' );
190
191                /*
192                 * Seta propiedades obrigatorias para alguns softwares (Outlook)
193                 */
194                $ical->setProperty('x-wr-calname', 'Calendar Expresso');
195                $ical->setProperty('X-WR-CALDESC', 'Calendar Expresso');
196                $ical->setProperty('X-WR-TIMEZONE', $params['X-WR-TIMEZONE']);
197
198                foreach ($data as $i => $v) {
199
200                    switch ($v['type']) {
201                        case EVENT_ID:
202                            $vevent = $ical->newComponent('vevent');
203
204                            $vevent->setProperty('summary', $v['summary']);
205                            $vevent->setProperty('description', isset($v['description']) ? $v['description'] : '');
206                            $vevent->setProperty('location', $v['location']);
207                            $vevent->setProperty('tranp', (isset($v['tranparent']) && $v['tranparent'] == TRANSP_TRANSPARENT ) ? 'TRANSPARENT' : 'OPAQUE' );
208
209                            $timezone = new DateTimeZone('UTC');
210                            $sTime = new DateTime('@' . (int) ($v['startTime'] / 1000), $timezone);
211                            $eTime = new DateTime('@' . (int) ($v['endTime'] / 1000), $timezone);
212
213                            if (( isset($v['repeat']) ) && ( isset($v['repeat']['frequency']) && $v['repeat']['frequency'] && $v['repeat']['frequency'] != 'none' ))
214                                    $vevent->setProperty('rrule', $this->formatIcalRepeat($v['repeat']));
215
216                            if (isset($v['allDay']) && $v['allDay'] == 1) {
217                                $vevent->setProperty('dtstart', $sTime->format(DATE_RFC822), array("VALUE" => "DATE"));
218                                $vevent->setProperty('dtend', $eTime->format(DATE_RFC822), array("VALUE" => "DATE"));
219                                $vevent->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'TRUE');
220                            } else {
221                                $vevent->setProperty('dtstart', $sTime->format(DATE_RFC822));
222                                $vevent->setProperty('dtend', $eTime->format(DATE_RFC822));
223                                $vevent->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'FALSE');
224                            }
225                                           
226                            if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
227                                $participants = $v['participants'];
228                            else
229                                        $participants = Controller::find(array('concept' => 'participant'), false, array('filter' => array('=', 'schedulable', $v['id'])));
230                           
231                                if (is_array($participants) && count($participants) > 0)
232                                        foreach ($participants as $ii => $vv) {
233                                       
234                                                if(isset($participants[$ii]['user']) && !is_array($participants[$ii]['user']))
235                                                {
236                                                        if ($vv['isExternal'] == 1)
237                                                                $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user'], 'service' => 'PostgreSQL'));
238                                                        else
239                                                                $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user']));
240                                                }
241                                       
242                                                if ($participants[$ii]['user']['id'] == Config::me('uidNumber'))
243                                                {
244                                                        $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']))));
245                                                        if(is_array($alarms))
246                                                                self::createAlarms($alarms, $vevent);
247                                                }
248                                       
249                                        }
250                           
251
252                            if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
253                                        $this->createAttendee($v['participants'], $vevent);
254
255                            if (isset($v['attachments']) && is_array($v['attachments']) && count($v['attachments']) > 0)
256                                        $this->createAttachment($v['attachments'], $vevent);
257
258                            $vevent->setProperty('uid', $v['uid']); 
259                           
260                            break;
261
262                        default:
263                            break;
264                       
265        case TODO_ID:
266
267            $todo = $ical->newComponent('todo');
268
269            $todo->setProperty('summary', $v['summary']);
270            $todo->setProperty('description', isset($v['description']) ? $v['description'] : '');
271            $todo->setProperty('priority', $v['priority']);
272            $todo->setProperty('percent-complete', $v['percentage']);
273            $todo->setProperty('status', $this->_getStatusTodo($v['status']));
274
275            $timezone = new DateTimeZone('UTC');
276            $apTimezone = self::nomalizeTZID(( isset($v['timezone']) && $v['timezone'] != 'null' ) ? $v['timezone'] : $params['defaultTZI']);
277            $apTimezoneOBJ = new DateTimeZone($apTimezone);
278
279            $sTime = new DateTime('@' . (int) ($v['startTime'] / 1000), $timezone);
280            $sTime->setTimezone($apTimezoneOBJ);
281
282            if (isset($v['allDay']) && $v['allDay'] == 1) {
283                $todo->setProperty('dtstart', $sTime->format(DATE_RFC822), array("VALUE" => "DATE"));
284                //$todo->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'TRUE');
285            } else {
286                $todo->setProperty('dtstart', $sTime->format(DATE_RFC822), array('TZID' => $apTimezone));
287                //$todo->setProperty('X-MICROSOFT-CDO-ALLDAYEVENT', 'FALSE');
288            }
289
290            if(isset($v['due']) && $v['due'] != '' && (int)$v['due'] > 0){
291                $dueTime = new DateTime('@' . (int) ($v['due'] / 1000), $timezone);
292                $dueTime->setTimezone($apTimezoneOBJ);
293
294                $todo->setProperty('due', $dueTime->format(DATE_RFC822), array('TZID' => $apTimezone));
295            }
296                   
297            if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
298                $participants = $v['participants'];
299            else
300                $participants = Controller::find(array('concept' => 'participant'), false, array('filter' => array('=', 'schedulable', $v['id'])));
301           
302            if (is_array($participants) && count($participants) > 0)
303                foreach ($participants as $ii => $vv) {
304               
305                    if(isset($participants[$ii]['user']) && !is_array($participants[$ii]['user']))
306                    {
307                        if ($vv['isExternal'] == 1)
308                            $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user'], 'service' => 'PostgreSQL'));
309                        else
310                            $participants[$ii]['user'] = Controller::read(array('concept' => 'user', 'id' => $vv['user']));
311                    }
312               
313                    if ($participants[$ii]['user']['id'] == Config::me('uidNumber'))
314                    {
315                        $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']))));
316                        if(is_array($alarms))
317                            self::createAlarms($alarms, $todo);
318                    }
319               
320                }
321
322            if (isset($v['participants']) && is_array($v['participants']) && count($v['participants']) > 0)
323                $this->createAttendee($v['participants'], $todo);
324
325            if (isset($v['attachments']) && is_array($v['attachments']) && count($v['attachments']) > 0)
326                $this->createAttachment($v['attachments'], $todo);
327
328            $todo->setProperty('uid', $v['uid']); 
329           
330            break;
331
332                default:
333                    break;
334            }
335        }
336
337       
338        return $ical->createCalendar();
339    }
340
341    protected function formatIcalRepeat($pRepeat)
342    {
343        $repeat = array();
344
345                foreach ($pRepeat as $ir => $rv) {
346                    if ($rv) {
347                        if ($ir == 'frequency' && $rv !== 'none')
348                            $repeat['FREQ'] = $rv;
349                        else if ($ir == 'endTime') {
350                            $time = new DateTime('@' . (int) ($rv / 1000), new DateTimeZone('UTC'));
351                            $time->setTimezone($apTimezoneOBJ);
352                            $repeat['until'] = $time->format(DATE_RFC822);
353                        } else if ($ir == 'count')
354                            $repeat[$ir] = $rv;
355                        else if ($ir == 'interval')
356                            $repeat[$ir] = $rv;
357                        else if ($ir !== 'schedulable' && $ir !== 'id' && $ir !== 'startTime')
358                            $repeat[$ir] = explode(',', $rv);
359                    }
360                }
361                return $repeat;
362    }
363
364   
365    public function createAlarms($alarms, &$vevent)
366    {   
367            foreach ($alarms as $va)
368            {
369                $valarm = new valarm();
370                $valarm->setProperty('ACTION' , self::codeAlarmAction($va['type']));
371               
372                $duration = array();
373               
374                switch ($va['unit'])
375                {
376                        case 'h':
377                                $duration['hour'] = $va['time'];
378                        break;
379                        case 'm':
380                                $duration['min'] = $va['time'];
381                        break;
382                        case 's':
383                                $duration['sec'] = $va['time'];
384                                break;
385                }
386               
387                $valarm->setProperty('trigger' ,$duration);
388                $vevent->setComponent($valarm);
389            }   
390       
391    }
392
393    //Trata a criacao de anexos do ics
394    public function createAttachment($attachments, &$vevent) {
395        foreach ($attachments as $key => $attachment) {
396            $pParams = array("ENCODING" => "BASE64", "VALUE" => "BINARY",
397                "X-FILENAME" => $attachment['name']);
398
399            $vevent->setProperty("attach", $attachment['source'], $pParams);
400        }
401    }
402
403    //Trata a criacao de attendees com tratamento de delegate
404    public function createAttendee($attendees, &$vevent) {
405        $delegate = array();
406        foreach ($attendees as $di => $dv) {
407            if (isset($dv['delegatedFrom']) && $dv['delegatedFrom'] != 0) {
408                $delegate[$dv['delegatedFrom']] = $dv;
409            }
410        }
411
412        foreach ($attendees as $pi => $pv) {
413            $isResponseDelegated = false;
414            if ((isset($pv['delegatedFrom']) && $pv['delegatedFrom'] == 0) || !isset($pv['delegatedFrom']))  {
415                if ($pv['isOrganizer'] == 1)
416                    $vevent->setProperty('organizer', $pv['user']['mail'], array('CN' => $pv['user']['name']));
417                else {
418                    $pParams = array();
419                    $pParams['CN'] = $pv['user']['name'];
420                    $pParams['PARTSTAT'] = self::_getStatus($pv['status']);
421
422                    if (isset($pv['id']) && isset($delegate[$pv['id']])) {
423                        $pParams['PARTSTAT'] = self::_getStatus($delegate[$pv['id']]['status']);
424                        $pParams['DELEGATED-TO'] = $delegate[$pv['id']]['user']['mail'];
425                        $pParams['CN'] = $pv['user']['name'];
426
427                        $vevent->setProperty('attendee', $pv['user']['mail'], $pParams);
428
429                        if ($delegate[$pv['id']]['status'] == STATUS_UNANSWERED) {
430                            $pParams['RSVP'] = $pv['receiveNotification'] == 1 ? 'TRUE' : 'FALSE';
431                            unset($pParams['PARTSTAT']);
432                        }else
433                            $pParams['PARTSTAT'] = self::_getStatus($delegate[$pv['id']]['status']);
434
435                        unset($pParams['DELEGATED-TO']);
436                        $pParams['DELEGATED-FROM'] = $pv['user']['mail'];
437
438                        $vevent->setProperty('attendee', $delegate[$pv['id']]['user']['mail'], $pParams);
439                        continue;
440                    }
441                    $pParams['RSVP'] = 'TRUE';
442
443                    $vevent->setProperty('attendee', $pv['user']['mail'], $pParams);
444
445                }
446            }
447           
448           
449        }
450    }
451
452    public function parse($data, $params = false) {
453        Config::regSet('noAlarm', TRUE); //Evita o envio de notificação
454        $vcalendar = new icalCreator( );
455        $vcalendar->parse(trim($data));
456        $vcalendar->sort();
457
458        $return = array();
459        $method = $vcalendar->getProperty('method', FALSE, FALSE);
460        $params['prodid'] = $vcalendar->getProperty('prodid', false, false);
461        $params['X-WR-TIMEZONE'] = ( $xrTimezone = $vcalendar->getProperty('X-WR-TIMEZONE', false, false)) ? self::nomalizeTZID($xrTimezone[1]) : false ;
462
463        while ($component = $vcalendar->getComponent()) {
464            $interation = array();
465            $uid = $component->getProperty('uid', false, false); //Resgata o uid do componente
466
467            switch (strtoupper($component->objName)) {
468                case 'VEVENT':
469
470                    switch ($method) {
471                        case 'PUBLISH':
472                            //Caso o evento não exista o mesmo cria um novo evento, se já existir o mesmo referencia o evento com agenda
473                if (!$schedulable = self::_getSchedulable($uid))
474                                    $interation = self::_makeVEVENT($schedulable, $component, $params);
475                else{
476                    $links = Controller::read(array('concept' => 'calendarToSchedulable'), array('id'), array('filter' =>
477                                        array('AND',
478                                            array('=', 'calendar', $params['calendar']),
479                                            array('=', 'schedulable', $schedulable['id'])
480                                        )));
481
482                    if(!$links &&  !isset($links[0]))
483                        Controller::create(array('concept' => 'calendarToSchedulable'), array('calendar' => $params['calendar'], 'schedulable' => $schedulable['id']));
484
485                    }
486                            break;
487
488                        case 'REQUEST':
489                            $schedulable = self::_getSchedulable($uid);
490
491                            if ($schedulable) { //Caso o evento exista
492                                if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {
493                                    $calendarToSchedulable = array();
494                                    $calendarToSchedulable['calendar'] = $params['calendar'];
495                                    $calendarToSchedulable['schedulable'] = $schedulable['id'];
496                                    $interation['calendarToSchedulable://' . mt_rand() . '(Formatter)'] = $calendarToSchedulable;
497   
498                                    if (isset($params['status'])) {
499                                        if($params['owner'] != Config::me("uidNumber")){                                           
500                                            $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );                                           
501                                            $pID = self::_getParticipantByMail($user[0]['mail'], $schedulable['participants']);
502                                        }else{
503                                            $pID = self::_getParticipantByMail(Config::me('mail'), $schedulable['participants']);
504                            //caso nõa seja participante adiciona a lista de participantes
505                            if(!$pID){
506                                $pID =  mt_rand() . '2(Formatter)';
507                                $interation['participant://' . $pID]['status'] = $params['status'];
508                                $interation['participant://' . $pID]['user'] = $params['owner'];
509                                $interation['participant://' . $pID]['isOrganizer'] = '0';
510                                $interation['participant://' . $pID]['schedulable'] = $schedulable['id'];
511                            }else
512                                            $interation['participant://' . $pID]['status'] = $params['status'];
513                        }
514                                    }
515
516                                    Config::regSet('noAlarm', FALSE); //reativa notificação
517                                } else {
518
519                                    if (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence']) //Organizador esta requisitando que você atualize o evento
520                                        $interation = self::_makeVEVENT($schedulable, $component, $params);
521                                    else if ($component->getProperty('sequence', false, false) === $schedulable['sequence']) {
522                                        //Ler melhor rfc sobre isto 3.2.2.2
523                                        //Aparentemente é para retornar um ical com o evento atualizado para o attende
524                                    }
525
526                                    if (isset($params['status'])) {
527                                        if($params['owner'] != Config::me("uidNumber")){                                           
528                                           $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );                                     
529                                            $pID = self::_getParticipantByMail($user[0]['mail'], $schedulable['participants']);
530                                        }else
531                                            $pID = self::_getParticipantByMail(Config::me('mail'), $schedulable['participants']);
532                                        //Verifica a importação de eventos em que não participo
533                                        if ($pID) {
534                                            $interation['participant://' . $pID]['status'] = $params['status'];
535                                        }
536                                    }
537                                }
538                            } else { // Importar evento
539                                $interation = self::_makeVEVENT(array(), $component, $params);
540
541                                if (strpos($params['prodid'], 'kigkonsult.se') !== false) { //envia notificação para fora
542
543                                    /* Data de Inicio */
544                                    $startTime = $component->getProperty('dtstart', false, true);
545
546                                    $tzid = isset($startTime['params']['TZID']) ? $startTime['params']['TZID'] : $params['X-WR-TIMEZONE'];
547
548                                    /* Tiem zone do evento */   
549                                    if ($tzid)
550                                        $sc['timezone'] = self::nomalizeTZID($tzid);
551                                    else
552                                        $sc['timezone'] = isset($params['calendar_timezone']) ? $params['calendar_timezone'] : 'America/Sao_Paulo';
553
554                                    $objTimezone = new DateTimeZone($sc['timezone']);
555
556                                    if (isset($startTime['params']['VALUE']) && $startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone'])) {
557                                        $sc['allDay'] = 1;
558                                        $sc['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $sc['timezone']) . '000';
559                                    } 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 */
560                                        $sc['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $startTime['params']['TZID']) . '000';
561                                    else {
562                                        $sc['startTime'] = self::date2timestamp($startTime['value']) . '000';
563                                        if (strpos($params['prodid'], 'Outlook') !== false) {
564                                            //Se o ics veio em utc não aplicar horario de verão
565                                            $sTime = new DateTime('@' . (int) ($sc['startTime'] / 1000), new DateTimeZone('UTC'));
566                                            $sTime->setTimezone($objTimezone);
567                                            if ($sTime->format('I')) //Se o ics veio em utc não aplicar horario de verão
568                                                $sc['startTime'] = $sc['startTime'] - 3600000;
569                                        }
570                                    }
571
572
573                                    /* Data de Termino */
574                                    $endTime = $component->getProperty('dtend', false, true);
575
576                                    if (isset($endTime['params']['VALUE']) && $endTime['params']['VALUE'] === 'DATE')
577                                        $sc['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $sc['timezone']) . '000';
578                                    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 */
579                                        $sc['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $endTime['params']['TZID']) . '000';
580                                    else {
581                                        $sc['endTime'] = self::date2timestamp($endTime['value']) . '000';
582                                        if (strpos($params['prodid'], 'Outlook') !== false) {
583                                            //Se o ics veio em utc não aplicar horario de verão
584                                            $eTime = new DateTime('@' . (int) ($sc['endTime'] / 1000), new DateTimeZone('UTC'));
585                                            $eTime->setTimezone($objTimezone);
586                                            if ($eTime->format('I'))
587                                                $sc['endTime'] = $sc['endTime'] - 3600000;
588                                        }
589                                    }
590
591
592                                    if ($uid = $component->getProperty('uid', false, false))
593                                        ;
594                                    $sc['uid'] = $uid;
595
596
597                                    $sc['summary'] = mb_convert_encoding($component->getProperty('summary', false, false), 'UTF-8', 'UTF-8,ISO-8859-1');
598
599                                    /* Definindo Description */
600                                    if ($desc = $component->getProperty('description', false, false))
601                                        $sc['description'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $desc), 'UTF-8', 'UTF-8,ISO-8859-1');
602
603                                    /* Definindo location */
604                                    if ($location = $component->getProperty('location', false, false))
605                                        $sc['location'] = mb_convert_encoding($location, 'UTF-8', 'UTF-8,ISO-8859-1');
606
607
608
609                                    if ($property = $component->getProperty('organizer', FALSE, TRUE)) {
610                                        $participant = array();
611                                        $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
612
613                                        $participantID = mt_rand() . '2(Formatter)';
614
615                                        $participant['isOrganizer'] = '1';
616
617                                        $user = null;
618
619                                        $participant['isExternal'] = 1;
620                                        /* Gera um randon id para o contexto formater */
621                                        $userID = mt_rand() . '4(Formatter)';
622
623                                        $user['mail'] = $mailUser;
624                                        $organizerMail = $mailUser;
625
626                                        $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
627                                        $user['isExternal'] = '1';
628                                        $participant['user'] = $user;
629
630                                        $sc['participants'][] = $participant;
631                                    }
632
633
634                                    $participant['status'] = isset($params['status']) ? $params['status'] : STATUS_ACCEPTED;
635                                    $participant['isOrganizer'] = '0';
636                                    $participant['isExternal'] = 0;
637                                    $participant['user'] = array('mail' => Config::me('mail'), 'name' => Config::me('cn'));
638                                    $sc['participants'][] = $participant;
639                                    $sc['type'] = EVENT_ID;
640
641
642                                    $ical['source'] = Controller::format(array('service' => 'iCal'), array($sc), array('method' => 'REPLY'));
643                                    $ical['type'] = 'application/ics';
644                                    $ical['name'] = 'outlook.ics';
645
646                                    $ical2['source'] = $ical['source'];
647                                    $ical2['type'] = 'text/calendar; method=REPLY';
648                                    $ical2['name'] = 'thunderbird.ics';
649
650                                    $timezone = new DateTimeZone('UTC');
651                                    $sTime = new DateTime('@' . (int) ($sc['startTime'] / 1000), $timezone);
652                                    $eTime = new DateTime('@' . (int) ($sc['endTime'] / 1000), $timezone);
653
654                                    if (isset($sc['timezone'])) {
655                                        $sTime->setTimezone(new DateTimeZone($sc['timezone']));
656                                        $eTime->setTimezone(new DateTimeZone($sc['timezone']));
657                                    }
658
659                                    $data = array('startDate' => date_format($sTime, 'd/m/Y'),
660                                        'startTime' => (isset($sc['allDay']) && $sc['allDay'] ) ? '' : date_format($sTime, 'H:i'),
661                                        'endDate' => date_format($eTime, 'd/m/Y'),
662                                        'endTime' => isset($sc['allDay']) ? '' : date_format($eTime, 'H:i'),
663                                        'eventTitle' => $sc['summary'],
664                                        'eventLocation' => isset($sc['location']) ? $sc['location'] : '',
665                                        'timezone' => ($sc['timezone']) ? $sc['timezone'] : 'UTC',
666                                        'participant' => (isset($part['user']['name']) ? $part['user']['name'] : $part['user']['mail']));
667
668                                    $subject['notificationType'] = 'Convite Aceito';
669                                    $subject['eventTitle'] = mb_convert_encoding($sc['summary'], 'ISO-8859-1', 'ISO-8859-1,UTF-8');
670                                    $subject['startDate'] = date_format($sTime, 'd/m/Y');
671                                    $subject['startTime'] = ($sc['allDay']) ? '' : date_format($sTime, 'H:i');
672                                    $subject['endDate'] = date_format($eTime, 'd/m/Y');
673                                    $subject['endTime'] = ($sc['allDay']) ? '' : date_format($eTime, 'H:i');
674                                    $subject['participant'] = Config::me('uid');
675
676                                    $params['status'] = isset($params['status']) ? $params['status'] : STATUS_ACCEPTED;
677
678                                    switch ($params['status']) {
679                                        case STATUS_ACCEPTED:
680                                            $tpl = 'notify_accept_body';
681                                            $subject['notificationType'] = 'Convite Aceito';
682                                            break;
683                                        case STATUS_TENTATIVE:
684                                            $tpl = 'notify_attempt_body';
685                                            $subject['notificationType'] = 'Convite  aceito provisoriamente';
686                                            break;
687                                        case STATUS_CANCELLED:
688                                            $tpl = 'notify_reject_body';
689                                            $subject['notificationType'] = 'Convite rejeitado';
690                                            break;
691                                    }
692                                    require_once ROOTPATH . '/api/parseTPL.php';
693
694                                    $mail = array();
695                                    $mail['attachments'][] = $ical;
696                                    $mail['attachments'][] = $ical2;
697
698                                    $mail['isHtml'] = true;
699                                    $mail['body'] = parseTPL::load_tpl($data, ROOTPATH . '/modules/calendar/templates/' . $tpl . '.tpl');
700                                    $mail['subject'] = parseTPL::load_tpl($subject, ROOTPATH . '/modules/calendar/templates/notify_subject.tpl');
701                                    ;
702                                    $mail['from'] = '"' . Config::me('cn') . '" <' . Config::me('mail') . '>';
703                                    $mail['to'] = $organizerMail;
704
705
706                                    Controller::create(array('service' => 'SMTP'), $mail);
707                                }
708                            }
709                            break;
710
711                        case 'REFRESH':
712                            break;
713
714                        case 'CANCEL':
715                            if ($schedulable = self::_getSchedulable($uid))
716                                $interation['schedulable://' . $schedulable['id']] = false;
717                            break;
718                               
719                        case 'ADD':
720                            break;
721
722                        case 'REPLY':
723                            if ($schedulable = self::_getSchedulable($uid)) {
724                                while ($property = $component->getProperty('attendee', FALSE, TRUE))
725                                    if ($pID = self::_getParticipantByMail(str_replace('MAILTO:', '', $property['value']), $schedulable['participants']))
726                                        $interation['participant://' . $pID] = array('id' => $pID, 'status' => constant('STATUS_' . strtoupper($property['params']['PARTSTAT'])));
727
728                                $interation['schedulable://' . $schedulable['id']]['sequence'] = $schedulable['sequence'] + 1;
729                            }
730                            break;
731
732                        case 'COUNTER':
733                            if ($params['acceptedSuggestion'] !== 'false') {
734
735                                $schedulable = self::_getSchedulable($uid);
736                                $params['calendar'] = self::_existInMyCalendars($schedulable['id'], $params['owner']);
737
738                                $interation = self::_makeCOUNTER($schedulable, $component, $params);
739                                Config::regSet('noAlarm', FALSE);
740                            } else {
741                                $response = array();
742                                $response['from'] = $params['from'];
743                                $response['type'] = 'suggestionResponse';
744                                $response['status'] = 'DECLINECOUNTER';
745                                $response['schedulable'] = self::_getSchedulable($uid);
746
747                                Controller::create(array('concept' => 'notification'), $response);
748                            }
749                            break;
750
751                        case 'DECLINECOUNTER':
752                            break;
753
754                        default:
755
756                            $schedulable = self::_getSchedulable($uid);
757
758                            if ($schedulable && ( self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence'])) { //Caso o evento exista
759                                $interation = self::_makeVEVENT($schedulable, $component, $params);
760
761                                if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {
762                                    $calendarToSchedulable = array();
763                                    $calendarToSchedulable['calendar'] = $params['calendar'];
764                                    $calendarToSchedulable['schedulable'] = $schedulable['id'];
765                                    $interation['calendarToSchedulable://' . mt_rand() . '(Formatter)'] = $calendarToSchedulable;
766                                }
767                            }
768                            else // Importar evento
769                                $interation = self::_makeVEVENT(array(), $component, $params);
770                            break;
771                    }
772       
773                   
774                    $return[] = $interation;
775                    break;
776        /***********************************************************************TODO*******************************************************************************/
777        case 'VTODO':
778        switch ($method) {
779            case 'PUBLISH':
780                //Caso a tarefa não exista o mesmo cria um novo evento, se já existir o mesmo referencia o evento com agenda
781                if (!$schedulable = self::_getSchedulable($uid))
782                    $interation = self::_makeVTODO($schedulable, $component, $params);
783                else{
784                    $links = Controller::read(array('concept' => 'calendarToSchedulable'), array('id'), array('filter' =>
785                    array('AND',
786                        array('=', 'calendar', $params['calendar']),
787                        array('=', 'schedulable', $schedulable['id'])
788                    )));
789
790                    if(!$links &&  !isset($links[0]))
791                        Controller::create(array('concept' => 'calendarToSchedulable'), array('calendar' => $params['calendar'], 'schedulable' => $schedulable['id']));
792                }
793                break;
794
795            case 'REQUEST':
796                $schedulable = self::_getSchedulable($uid);
797
798                if ($schedulable) { //Caso tarefa exista
799                    if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {
800                       
801                        $calendarToSchedulable = array();
802                        $calendarToSchedulable['calendar'] = $params['calendar'];
803                        $calendarToSchedulable['schedulable'] = $schedulable['id'];
804                        $interation['calendarToSchedulable://' . mt_rand() . '(Formatter)'] = $calendarToSchedulable;
805       
806                        if (isset($params['status'])) {
807                            if($params['owner'] != Config::me("uidNumber")){                       
808                                $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );                       
809                                $pID = self::_getParticipantByMail($user[0]['mail'], $schedulable['participants']);
810                            }else
811                                $pID = self::_getParticipantByMail(Config::me('mail'), $schedulable['participants']);
812                                $interation['participant://' . $pID]['status'] = $params['status'];
813                        }
814                        Config::regSet('noAlarm', FALSE); //reativa notificação
815                    } else {
816
817                        if (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence']) //Organizador esta requisitando que você atualize o evento
818                            $interation = self::_makeVEVENT($schedulable, $component, $params);
819                        else if ($component->getProperty('sequence', false, false) === $schedulable['sequence']) {
820                        //Ler melhor rfc sobre isto 3.2.2.2
821                        //Aparentemente é para retornar um ical com o evento atualizado para o attende
822                        }
823
824                        if (isset($params['status'])) {
825                            if($params['owner'] != Config::me("uidNumber")){                       
826                               $user = Controller::Read(array('concept' => 'user'), false, array('filter' => array('=', 'id', $params['owner'])) );                     
827                                $pID = self::_getParticipantByMail($user[0]['mail'], $schedulable['participants']);
828                            }else{
829                                $pID = self::_getParticipantByMail(Config::me('mail'), $schedulable['participants']);
830                                //Verifica a importação de tarefas em que não participo
831                                if ($pID) {
832                                    $pID =  mt_rand() . '2(Formatter)';
833                                    $interation['participant://' . $pID]['status'] = $params['status'];
834                                    $interation['participant://' . $pID]['user'] = $params['owner'];
835                                    $interation['participant://' . $pID]['isOrganizer'] = '0';
836                                    $interation['participant://' . $pID]['schedulable'] = $schedulable['id'];
837                                }else
838                                    $interation['participant://' . $pID]['status'] = $params['status'];
839                            }
840                        }
841                    }
842                } else { // Importar tarefa
843                    $interation = self::_makeVTODO(array(), $component, $params);
844
845                    if (strpos($params['prodid'], 'kigkonsult.se') !== false) { //envia notificação para fora
846
847                        /* Data de Inicio */
848                        $startTime = $component->getProperty('dtstart', false, true);
849                        $tzid = isset($startTime['params']['TZID']) ? $startTime['params']['TZID'] : $params['X-WR-TIMEZONE'];
850
851                        /* Tiem zone do evento */   
852                        if ($tzid)
853                            $sc['timezone'] = self::nomalizeTZID($tzid);
854                        else
855                            $sc['timezone'] = isset($params['calendar_timezone']) ? $params['calendar_timezone'] : 'America/Sao_Paulo';
856
857                        $objTimezone = new DateTimeZone($sc['timezone']);
858
859                        if (isset($startTime['params']['VALUE']) && $startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone'])) {
860                            $sc['allDay'] = 1;
861                            $sc['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $sc['timezone']) . '000';
862                        } 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 */
863                            $sc['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $startTime['params']['TZID']) . '000';
864                        else {
865                            $sc['startTime'] = self::date2timestamp($startTime['value']) . '000';
866                            if (strpos($params['prodid'], 'Outlook') !== false) {
867                                //Se o ics veio em utc não aplicar horario de verão
868                                $sTime = new DateTime('@' . (int) ($sc['startTime'] / 1000), new DateTimeZone('UTC'));
869                                $sTime->setTimezone($objTimezone);
870                                if ($sTime->format('I')) //Se o ics veio em utc não aplicar horario de verão
871                                $sc['startTime'] = $sc['startTime'] - 3600000;
872                            }
873                        }
874
875                        /* Data de Termino */
876                        $endTime = $component->getProperty('dtend', false, true);
877
878                        if (isset($endTime['params']['VALUE']) && $endTime['params']['VALUE'] === 'DATE')
879                            $sc['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $sc['timezone']) . '000';
880                        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 */
881                            $sc['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $endTime['params']['TZID']) . '000';
882                        else {
883                            $sc['endTime'] = self::date2timestamp($endTime['value']) . '000';
884                            if (strpos($params['prodid'], 'Outlook') !== false) {
885                                //Se o ics veio em utc não aplicar horario de verão
886                                $eTime = new DateTime('@' . (int) ($sc['endTime'] / 1000), new DateTimeZone('UTC'));
887                                $eTime->setTimezone($objTimezone);
888                                if ($eTime->format('I'))
889                                    $sc['endTime'] = $sc['endTime'] - 3600000;
890                            }
891                        }
892
893
894                        if ($uid = $component->getProperty('uid', false, false))                   
895                            $sc['uid'] = $uid;
896
897                        $sc['summary'] = mb_convert_encoding($component->getProperty('summary', false, false), 'UTF-8', 'UTF-8,ISO-8859-1');
898
899                        /* Definindo Description */
900                        if ($desc = $component->getProperty('description', false, false))
901                            $sc['description'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $desc), 'UTF-8', 'UTF-8,ISO-8859-1');
902
903            if ($priority = $component->getProperty('priority', false, false))
904                $sc['priority'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $priority), 'UTF-8', 'UTF-8,ISO-8859-1');
905
906            if ($status = $component->getProperty('status', false, false))
907                $sc['status'] = $this->decodeStatusTodo(mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $status), 'UTF-8', 'UTF-8,ISO-8859-1'));
908
909            if ($percentage = $component->getProperty('percent-complete', false, false))
910                $sc['percentage'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $percentage), 'UTF-8', 'UTF-8,ISO-8859-1');
911
912                        /* Definindo location */
913                        if ($location = $component->getProperty('location', false, false))
914                            $sc['location'] = mb_convert_encoding($location, 'UTF-8', 'UTF-8,ISO-8859-1');
915
916
917
918                        if ($property = $component->getProperty('organizer', FALSE, TRUE)) {
919                            $participant = array();
920                            $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
921
922                            $participantID = mt_rand() . '2(Formatter)';
923
924                            $participant['isOrganizer'] = '1';
925
926                            $user = null;
927
928                            $participant['isExternal'] = 1;
929                            /* Gera um randon id para o contexto formater */
930                            $userID = mt_rand() . '4(Formatter)';
931
932                            $user['mail'] = $mailUser;
933                            $organizerMail = $mailUser;
934
935                            $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
936                            $user['isExternal'] = '1';
937                            $participant['user'] = $user;
938
939                            $sc['participants'][] = $participant;
940                        }
941
942
943                        $participant['status'] = isset($params['status']) ? $params['status'] : STATUS_ACCEPTED;
944                        $participant['isOrganizer'] = '0';
945                        $participant['isExternal'] = 0;
946                        $participant['user'] = array('mail' => Config::me('mail'), 'name' => Config::me('cn'));
947                        $sc['participants'][] = $participant;
948                        $sc['type'] = TODO_ID;
949
950
951                        $ical['source'] = Controller::format(array('service' => 'iCal'), array($sc), array('method' => 'REPLY'));
952                        $ical['type'] = 'application/ics';
953                        $ical['name'] = 'outlook.ics';
954
955                        $ical2['source'] = $ical['source'];
956                        $ical2['type'] = 'text/calendar; method=REPLY';
957                        $ical2['name'] = 'thunderbird.ics';
958
959                        $timezone = new DateTimeZone('UTC');
960                        $sTime = new DateTime('@' . (int) ($sc['startTime'] / 1000), $timezone);
961                        $eTime = new DateTime('@' . (int) ($sc['endTime'] / 1000), $timezone);
962
963                        if (isset($sc['timezone'])) {
964                            $sTime->setTimezone(new DateTimeZone($sc['timezone']));
965                            $eTime->setTimezone(new DateTimeZone($sc['timezone']));
966                        }
967
968                        $data = array('startDate' => date_format($sTime, 'd/m/Y'),
969                        'startTime' => (isset($sc['allDay']) && $sc['allDay'] ) ? '' : date_format($sTime, 'H:i'),
970                        'endDate' => date_format($eTime, 'd/m/Y'),
971                        'endTime' => isset($sc['allDay']) ? '' : date_format($eTime, 'H:i'),
972                        'eventTitle' => $sc['summary'],
973                        'eventLocation' => isset($sc['location']) ? $sc['location'] : '',
974                        'timezone' => ($sc['timezone']) ? $sc['timezone'] : 'UTC',
975                        'participant' => (isset($part['user']['name']) ? $part['user']['name'] : $part['user']['mail']));
976
977                        $subject['notificationType'] = 'Convite Aceito';
978                        $subject['eventTitle'] = mb_convert_encoding($sc['summary'], 'ISO-8859-1', 'ISO-8859-1,UTF-8');
979                        $subject['startDate'] = date_format($sTime, 'd/m/Y');
980                        $subject['startTime'] = ($sc['allDay']) ? '' : date_format($sTime, 'H:i');
981                        $subject['endDate'] = date_format($eTime, 'd/m/Y');
982                        $subject['endTime'] = ($sc['allDay']) ? '' : date_format($eTime, 'H:i');
983                        $subject['participant'] = Config::me('uid');
984
985                        $params['status'] = isset($params['status']) ? $params['status'] : STATUS_ACCEPTED;
986
987                        switch ($params['status']) {
988                            case STATUS_ACCEPTED:
989                                $tpl = 'notify_accept_body';
990                                $subject['notificationType'] = 'Convite Aceito';
991                                break;
992                            case STATUS_TENTATIVE:
993                                $tpl = 'notify_attempt_body';
994                                $subject['notificationType'] = 'Convite  aceito provisoriamente';
995                                break;
996                            case STATUS_CANCELLED:
997                                $tpl = 'notify_reject_body';
998                                $subject['notificationType'] = 'Convite rejeitado';
999                                break;
1000                        }
1001
1002                        require_once ROOTPATH . '/api/parseTPL.php';
1003
1004                        $mail = array();
1005                        $mail['attachments'][] = $ical;
1006                        $mail['attachments'][] = $ical2;
1007
1008                        $mail['isHtml'] = true;
1009                        $mail['body'] = parseTPL::load_tpl($data, ROOTPATH . '/modules/calendar/templates/' . $tpl . '.tpl');
1010                        $mail['subject'] = parseTPL::load_tpl($subject, ROOTPATH . '/modules/calendar/templates/notify_subject.tpl');
1011
1012                        $mail['from'] = '"' . Config::me('cn') . '" <' . Config::me('mail') . '>';
1013                        $mail['to'] = $organizerMail;
1014
1015
1016                        Controller::create(array('service' => 'SMTP'), $mail);
1017                    }
1018                }
1019                break;
1020
1021            case 'REFRESH':
1022                break;
1023
1024            case 'CANCEL':
1025                if ($schedulable = self::_getSchedulable($uid))
1026                    $interation['schedulable://' . $schedulable['id']] = false;
1027                break;
1028
1029            case 'ADD':
1030                break;
1031
1032            case 'REPLY':
1033                if ($schedulable = self::_getSchedulable($uid)) {
1034                    while ($property = $component->getProperty('attendee', FALSE, TRUE))
1035                        if ($pID = self::_getParticipantByMail(str_replace('MAILTO:', '', $property['value']), $schedulable['participants']))
1036                            $interation['participant://' . $pID] = array('id' => $pID, 'status' => constant('STATUS_' . strtoupper($property['params']['PARTSTAT'])));
1037
1038                    $interation['schedulable://' . $schedulable['id']]['sequence'] = $schedulable['sequence'] + 1;
1039                }
1040                break;
1041
1042            case 'COUNTER':
1043                if ($params['acceptedSuggestion'] !== 'false') {
1044
1045                    $schedulable = self::_getSchedulable($uid);
1046                    $params['calendar'] = self::_existInMyCalendars($schedulable['id'], $params['owner']);
1047
1048                    $interation = self::_makeCOUNTER($schedulable, $component, $params);
1049                    Config::regSet('noAlarm', FALSE);
1050                } else {
1051                    $response = array();
1052                    $response['from'] = $params['from'];
1053                    $response['type'] = 'suggestionResponse';
1054                    $response['status'] = 'DECLINECOUNTER';
1055                    $response['schedulable'] = self::_getSchedulable($uid);
1056
1057                    Controller::create(array('concept' => 'notification'), $response);
1058                }
1059                break;
1060
1061            case 'DECLINECOUNTER':
1062                break;
1063
1064            default:
1065
1066                $schedulable = self::_getSchedulable($uid);
1067
1068                if ($schedulable && ( self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence'])) { //Caso o evento exista
1069                    $interation = self::_makeVEVENT($schedulable, $component, $params);
1070
1071                    if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {
1072                        $calendarToSchedulable = array();
1073                        $calendarToSchedulable['calendar'] = $params['calendar'];
1074                        $calendarToSchedulable['schedulable'] = $schedulable['id'];
1075                        $interation['calendarToSchedulable://' . mt_rand() . '(Formatter)'] = $calendarToSchedulable;
1076                    }
1077                }
1078                else // Importar evento
1079                $interation = self::_makeVEVENT(array(), $component, $params);
1080
1081                break;
1082            }
1083   
1084            $return[] = $interation;
1085        break;
1086        /***********************************************************************TODO*******************************************************************************/
1087        case 'VTIMEZONE':
1088                break;
1089            }
1090        }
1091        return $return;
1092    }
1093
1094    public function analize($data, $params = false) {
1095        $vcalendar = new icalCreator( );
1096        $vcalendar->parse(trim($data));
1097        $vcalendar->sort();
1098
1099        $return = array();
1100        $method = $vcalendar->getProperty('method', FALSE, FALSE);
1101
1102        while ($component = $vcalendar->getComponent()) {
1103            $interation = array();
1104            $uid = $component->getProperty('uid', false, false); //Resgata o uid do componente
1105            switch (strtoupper($component->objName)) {
1106                case 'VEVENT':
1107
1108                    switch ($method) {
1109                        case 'PUBLISH':
1110                            $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'calendarIds');
1111                            break;
1112
1113                        case 'REQUEST':
1114                            $schedulable = self::_getSchedulable($uid);
1115                            if ($schedulable) { //Caso o evento exista
1116                                $isOrganizer = false;
1117                                $isParticipant = false;
1118
1119                                foreach ($schedulable['participants'] as $value)
1120                                    if ($value['user']['id'] == $params['owner']) {
1121                                        $isParticipant = true;
1122                                        if ($value['isOrganizer'])
1123                                            $isOrganizer = true;
1124
1125                                        if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {
1126                                            $interation = ICAL_ACTION_UPDATE;
1127                                            $interation = ( strrpos($value['acl'], ATTENDEE_ACL_PARTICIPATION_REQUIRED) ) ? ICAL_ACTION_IMPORT_REQUIRED : array('action' => ICAL_ACTION_IMPORT, 'type' => 'calendarIds');
1128                                            break;
1129                                        }
1130                                    } else {
1131                                        if (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence']) //Organizador esta requisitando que você atualize o evento
1132                                            $interation = ($isOrganizer) ? ICAL_ACTION_ORGANIZER_UPDATE : ICAL_ACTION_UPDATE;
1133                                        else
1134                                            $interation = ($isOrganizer) ? ICAL_ACTION_ORGANIZER_NONE : ICAL_ACTION_NONE;
1135                                    }
1136                                if (!$isParticipant){
1137                                      $interation = self::_checkParticipantByPermissions($schedulable);
1138                                    }
1139                            }else
1140                                    $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'calendarIds');
1141                           
1142                            if(($interation != ICAL_ACTION_NONE) && ($interation != ICAL_ACTION_ORGANIZER_NONE) && ($interation != ICAL_ACTION_ORGANIZER_UPDATE) && ($interation != ICAL_ACTION_NONE)) {
1143                                if($params['owner'] != Config::me("uidNumber")){
1144                                    $sig = Controller::find(array('concept' => 'calendarSignature'), array('calendar'), array('filter' => array('AND', array('=', 'user', $params['owner']), array('=', 'isOwner', '1'))));
1145                                    $calendars = array();
1146                                    foreach ($sig as $val)
1147                                        $calendars[] = $val['calendar'];
1148                               
1149                                    $calendarsPermission = Controller::find(array('concept' => 'calendarToPermission'), array('calendar'), array('filter' => array('AND', array('=', 'user', Config::me("uidNumber")), array('IN', 'calendar', $calendars))));   
1150                                   
1151                                    foreach ($calendarsPermission as $val)
1152                                        $ids[] = $val['calendar'];
1153
1154                                    $interation = array('action' => ICAL_ACTION_IMPORT_FROM_PERMISSION ,'calendar' => $ids);
1155                                }
1156                            }
1157
1158                            break;
1159
1160                        case 'REFRESH':
1161                            break;
1162
1163                        case 'CANCEL':
1164                            $interation = ICAL_ACTION_DELETE;
1165                            break;
1166
1167                        case 'ADD':
1168                            break;
1169
1170                        case 'REPLY':
1171                                if ($schedulable = self::_getSchedulable($uid)) {
1172                                        while ($property = $component->getProperty('attendee', FALSE, TRUE))
1173                                            if ($attendee = self::_getParticipantByMail(str_replace('MAILTO:', '', $property['value']), $schedulable['participants'], true))
1174                                                        $interation = (constant('STATUS_' . strtoupper($property['params']['PARTSTAT'])) == $attendee['status']) ?  ICAL_ACTION_NONE : ICAL_ACTION_REPLY;
1175                            }else
1176                                $interation = ICAL_NOT_FOUND;
1177                            break;
1178
1179                        case 'COUNTER':
1180                            $interation = ICAL_ACTION_SUGGESTION;
1181                            break;
1182
1183                        case 'DECLINECOUNTER':
1184                            $interation = ICAL_ACTION_NONE;
1185                            break;
1186
1187                        default:
1188                            $schedulable = self::_getSchedulable($uid);
1189
1190                            if ($schedulable && ( self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence'])) //Caso o evento exista
1191                                $interation = ICAL_ACTION_UPDATE;
1192                            else if ($schedulable)
1193                                $interation = ICAL_ACTION_NONE;
1194                            else // Importar evento
1195                                $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'calendarIds');
1196
1197                            break;
1198                    }
1199
1200                    $return[$uid] = $interation;
1201                    break;
1202                case 'VTODO':
1203                    switch ($method) {
1204                        case 'PUBLISH':
1205                            $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'groupIds');
1206                            break;
1207
1208                        case 'REQUEST':
1209                            $schedulable = self::_getSchedulable($uid);
1210                           
1211                            if ($schedulable) { //Caso o evento exista
1212                                $isOrganizer = false;
1213                                $isParticipant = false;
1214
1215                                foreach ($schedulable['participants'] as $value)
1216                                    if ($value['user']['id'] == $params['owner']) {
1217                                        $isParticipant = true;
1218                                        if ($value['isOrganizer'])
1219                                            $isOrganizer = true;
1220
1221                                        if (!self::_existInMyCalendars($schedulable['id'], $params['owner'])) {   
1222                                            $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'groupIds');
1223                                            break;
1224                                        }
1225                                    } else {
1226                                        if (self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence']) //Organizador esta requisitando que você atualize o evento
1227                                            $interation = ($isOrganizer) ? ICAL_ACTION_ORGANIZER_UPDATE : ICAL_ACTION_UPDATE;
1228                                        else
1229                                            $interation = ($isOrganizer) ? ICAL_ACTION_ORGANIZER_NONE : ICAL_ACTION_NONE;
1230                                    }
1231                                if (!$isParticipant){
1232                                      $interation = self::_checkParticipantByPermissions($schedulable);
1233                                    }
1234                            }else
1235                                $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'groupIds');
1236                            break;
1237
1238                        case 'REFRESH':
1239                            break;
1240
1241                        case 'CANCEL':
1242                            $interation = ICAL_ACTION_DELETE;
1243                            break;
1244
1245                        case 'ADD':
1246                            break;
1247
1248                        case 'REPLY':
1249                            $interation = ICAL_ACTION_REPLY;
1250                            break;
1251
1252                        case 'COUNTER':
1253                            $interation = ICAL_ACTION_SUGGESTION;
1254                            break;
1255
1256                        case 'DECLINECOUNTER':
1257                            $interation = ICAL_ACTION_NONE;
1258                            break;
1259
1260                        default:
1261                            $schedulable = self::_getSchedulable($uid);
1262
1263                            if ($schedulable && ( self::_getTime($component, 'dtstamp') > $schedulable['dtstamp'] || $component->getProperty('sequence', false, false) > $schedulable['sequence'])) //Caso o evento exista
1264                                $interation = ICAL_ACTION_UPDATE;
1265                            else if ($schedulable)
1266                                $interation = ICAL_ACTION_NONE;
1267                            else // Importar evento
1268                                $interation = array('action' => ICAL_ACTION_IMPORT, 'type' => 'groupIds');
1269
1270                            break;
1271                    }
1272
1273                    $return[$uid] = $interation;
1274                    break;
1275                case 'VTIMEZONE':
1276                break;
1277            }
1278        }
1279
1280        return $return;
1281    }
1282
1283    /* Helpers */
1284
1285    private static function _getTzOffset($rTz, $oTz = null, $time = 'now') {
1286        if ($oTz === null) {
1287            if (!is_string($oTz = date_default_timezone_get())) {
1288                return false; // A UTC timestamp was returned -- bail out!
1289            }
1290        }
1291        $origin_dtz = new DateTimeZone(self::nomalizeTZID($oTz));
1292        $remote_dtz = new DateTimeZone(self::nomalizeTZID($rTz));
1293        $origin_dt = new DateTime($time, $origin_dtz);
1294        $remote_dt = new DateTime("now", $remote_dtz);
1295
1296        $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
1297
1298
1299        return $offset;
1300    }
1301
1302    private function _getStatus($id) {
1303        $a = array(
1304            STATUS_CONFIRMED => 'ACCEPTED',
1305            STATUS_CANCELLED => 'CANCELLED',
1306            STATUS_TENTATIVE => 'TENTATIVE',
1307            STATUS_UNANSWERED => 'NEEDS-ACTION',
1308            STATUS_DELEGATED => 'DELEGATED'
1309        );
1310
1311        return isset($a[$id]) ? $a[$id] : 'NEEDS-ACTION';
1312    }
1313
1314    public function decodeStatusTodo( $action )
1315    {
1316     $a = array(
1317        'NEED_ACTION' => STATUS_TODO_NEED_ACTION,
1318        'IN_PROGRESS' => STATUS_TODO_IN_PROGRESS  ,
1319        'COMPLETED' =>  STATUS_TODO_COMPLETED ,
1320        'CANCELLED'  => STATUS_TODO_CANCELLED
1321    );
1322
1323    return isset($a[$action]) ? $a[$action] : 'STATUS_TODO_NEED_ACTION';
1324   
1325    }
1326
1327
1328    private function _getStatusTodo($id) {
1329        $a = array(
1330            STATUS_TODO_NEED_ACTION => 'NEED_ACTION',
1331            STATUS_TODO_IN_PROGRESS => 'IN_PROGRESS',
1332            STATUS_TODO_COMPLETED => 'COMPLETED',
1333            STATUS_TODO_CANCELLED => 'CANCELLED'
1334        );
1335
1336    return isset($a[$id]) ? $a[$id] : 'NEED_ACTION';
1337    }
1338
1339    private static function _checkParticipantByPermissions($schedulable) {
1340
1341        $calendarIds = Controller::find(array('concept' => 'calendarSignature'), array('calendar'), array('filter' => array('AND', array('=','isOwner','0'), array('=', 'user', Config::me("uidNumber")))));
1342
1343        if($calendarIds && isset($calendarIds[0])){
1344            $ids = array();
1345            foreach($calendarIds as $value)
1346                    array_push($ids, $value['calendar']);
1347
1348            $signaturesOfOwners = Controller::find(array('concept' => 'calendarSignature'), false, array('filter' => array('AND', array('IN', 'calendar', $ids) , array('=','isOwner','1')), 'deepness' => 2 ));
1349           
1350            foreach($signaturesOfOwners as $value){
1351                        if(self::_getParticipantByMail($value['user']['mail'], $schedulable['participants'])){
1352                            $eventoFromCalendar = Controller::read( array( 'concept' => 'calendarToSchedulable') , false, array('filter' => array('AND', array('=','schedulable',$schedulable['id']), array('=','calendar', $value['calendar']['id']))));
1353
1354                            return  ($eventoFromCalendar && isset($eventoFromCalendar[0])) ? ICAL_ACTION_NONE_FROM_PERMISSION : array('action' => ICAL_ACTION_IMPORT_FROM_PERMISSION, 'calendar' => array($value['calendar']['id']) );
1355                        }
1356            }   
1357        }
1358        return array('action' => ICAL_ACTION_IMPORT, 'type' => 'calendarIds');
1359    }
1360   
1361   
1362    private static function _getParticipantByMail($mail, &$participants, $isFull = false) {
1363        if ($participants && $participants != '')
1364            foreach ($participants as $i => $v)
1365                if ((is_array($v) && isset($v['user'])) && ($v['user']['mail'] == $mail || (isset($v['user']['mailAlternateAddress']) && in_array($mail, $v['user']['mailAlternateAddress']))))
1366                      return !!$isFull ? $v : $v['id'];
1367        return false;
1368    }
1369
1370    static private function nomalizeTZID($TZID) {
1371        if (isset(self::$timezonesMap[$TZID]))
1372            return self::$timezonesMap[$TZID];
1373        else if (in_array($TZID, self::$suportedTimzones))
1374            return $TZID;
1375        else
1376            return date_default_timezone_get();
1377    }
1378
1379    static private function date2timestamp($datetime, $tz = null) {
1380        if (!isset($datetime['hour']))
1381            $datetime['hour'] = '0';
1382        if (!isset($datetime['min']))
1383            $datetime['min'] = '0';
1384        if (!isset($datetime['sec']))
1385            $datetime['sec'] = '0';
1386
1387        foreach ($datetime as $dkey => $dvalue)
1388            if ('tz' != $dkey)
1389                $datetime[$dkey] = (integer) $dvalue;
1390
1391        if ($tz)
1392            $datetime['tz'] = $tz;
1393
1394        $offset = ( isset($datetime['tz']) && ( '' < trim($datetime['tz']))) ? iCalUtilityFunctions::_tz2offset($datetime['tz']) : 0;
1395
1396        return gmmktime($datetime['hour'], $datetime['min'], ($datetime['sec'] + $offset), $datetime['month'], $datetime['day'], $datetime['year']);
1397    }
1398
1399    static private function _makeCOUNTER($schedulable, $component, $params) {
1400        $interation = array();
1401        $eventID = isset($schedulable['id']) ? $schedulable['id'] : mt_rand() . '(Formatter)';
1402
1403        /* Data de Inicio */
1404        $startTime = $component->getProperty('dtstart', false, true);
1405
1406        /* Tiem zone do evento */
1407        if (isset($startTime['params']['TZID']))
1408            $schedulable['timezone'] = self::nomalizeTZID($startTime['params']['TZID']);
1409        else
1410            $schedulable['timezone'] = isset($params['calendar_timezone']) ? $params['calendar_timezone'] : 'America/Sao_Paulo';
1411
1412        $objTimezone = new DateTimeZone($schedulable['timezone']);
1413
1414        if ($startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone'])) {
1415            $schedulable['allDay'] = 1;
1416            $schedulable['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $schedulable['timezone'], '@' . self::date2timestamp($startTime['value'])) . '000';
1417        } 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 */
1418            $schedulable['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $startTime['params']['TZID'], '@' . self::date2timestamp($startTime['value'])) . '000';
1419            $schedulable['allDay'] = 0;
1420        } else {
1421            $schedulable['startTime'] = self::date2timestamp($startTime['value']) . '000';
1422            if (strpos($params['prodid'], 'Outlook') !== false) {
1423                //Se o ics veio em utc não aplicar horario de verão
1424                $sTime = new DateTime('@' . (int) ($schedulable['startTime'] / 1000), new DateTimeZone('UTC'));
1425                $sTime->setTimezone($objTimezone);
1426                if ($sTime->format('I')) //Se o ics veio em utc não aplicar horario de verão
1427                    $schedulable['startTime'] = $schedulable['startTime'] - 3600000;
1428            }
1429        }
1430
1431        /* Data de Termino */
1432        $endTime = $component->getProperty('dtend', false, true);
1433
1434        if ($endTime['params']['VALUE'] === 'DATE')
1435            $schedulable['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $schedulable['timezone'], '@' . self::date2timestamp($endTime['value'])) . '000';
1436        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 */
1437            $schedulable['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $endTime['params']['TZID'], '@' . self::date2timestamp($endTime['value'])) . '000';
1438        else {
1439            $schedulable['endTime'] = self::date2timestamp($endTime['value']) . '000';
1440            if (strpos($params['prodid'], 'Outlook') !== false) {
1441                //Se o ics veio em utc não aplicar horario de verão
1442                $eTime = new DateTime('@' . (int) ($schedulable['endTime'] / 1000), new DateTimeZone('UTC'));
1443                $eTime->setTimezone($objTimezone);
1444                if ($eTime->format('I'))
1445                    $schedulable['endTime'] = $schedulable['endTime'] - 3600000;
1446            }
1447        }
1448        unset($schedulable['participants']);
1449        $interation['schedulable://' . $eventID] = $schedulable;
1450
1451        return $interation;
1452    }
1453
1454    static private function _makeVEVENT($schedulable, $component, $params) {
1455        $interation = array();
1456        $eventID = isset($schedulable['id']) ? $schedulable['id'] : mt_rand() . '(Formatter)';
1457
1458        /* Data de Inicio */
1459        $startTime = $component->getProperty('dtstart', false, true);
1460
1461        $tzid = (isset($startTime['params']['TZID']) ? $startTime['params']['TZID'] : $params['X-WR-TIMEZONE']);
1462
1463        /* Tiem zone do evento */
1464        if ($tzid){
1465            $tzid = self::nomalizeTZID($tzid);
1466            $schedulable['timezone'] = $tzid;
1467        }else
1468            $schedulable['timezone'] = isset($params['calendar_timezone']) ? $params['calendar_timezone'] : 'America/Sao_Paulo';
1469
1470        $objTimezone = new DateTimeZone($schedulable['timezone']);
1471
1472        if (isset($startTime['params']['VALUE']) && $startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone'])) {
1473            $schedulable['allDay'] = 1;
1474            $schedulable['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $schedulable['timezone'], '@' . self::date2timestamp($startTime['value'])) . '000';
1475        } 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 */
1476            $schedulable['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $tzid, '@' . self::date2timestamp($startTime['value'])) . '000';
1477            $schedulable['allDay'] = 0;
1478        } else {
1479            $schedulable['startTime'] = self::date2timestamp($startTime['value']) . '000';
1480            if (strpos($params['prodid'], 'Outlook') !== false) {
1481                //Se o ics veio em utc não aplicar horario de verão
1482                $sTime = new DateTime('@' . (int) ($schedulable['startTime'] / 1000), new DateTimeZone('UTC'));
1483                $sTime->setTimezone($objTimezone);
1484                if ($sTime->format('I')) //Se o ics veio em utc não aplicar horario de verão
1485                    $schedulable['startTime'] = $schedulable['startTime'] - 3600000;
1486            }
1487        }
1488
1489        /* Data de Termino */
1490        $endTime = $component->getProperty('dtend', false, true);
1491
1492        $tzid = isset($endTime['params']['TZID']) ? $endTime['params']['TZID'] : $params['X-WR-TIMEZONE'];
1493       
1494        if($tzid)
1495            $tzid = self::nomalizeTZID($tzid);
1496
1497        if (isset($endTime['params']['VALUE']) && $endTime['params']['VALUE'] === 'DATE')
1498            $schedulable['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $schedulable['timezone'], '@' . self::date2timestamp($endTime['value'])) . '000';
1499        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 */
1500            $schedulable['endTime'] = self::date2timestamp($endTime['value']) - self::_getTzOffset('UTC', $tzid, '@' . self::date2timestamp($endTime['value'])) . '000';
1501        else {
1502            $schedulable['endTime'] = self::date2timestamp($endTime['value']) . '000';
1503            if (strpos($params['prodid'], 'Outlook') !== false) {
1504                //Se o ics veio em utc não aplicar horario de verão
1505                $eTime = new DateTime('@' . (int) ($schedulable['endTime'] / 1000), new DateTimeZone('UTC'));
1506                $eTime->setTimezone($objTimezone);
1507                if ($eTime->format('I'))
1508                    $schedulable['endTime'] = $schedulable['endTime'] - 3600000;
1509            }
1510        }
1511
1512
1513        $schedulable['summary'] = mb_convert_encoding($component->getProperty('summary', false, false), 'ISO-8859-1', 'UTF-8,ISO-8859-1');
1514
1515        /* Definindo Description */
1516        if ($desc = $component->getProperty('description', false, false))
1517            $schedulable['description'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $desc), 'ISO-8859-1', 'UTF-8,ISO-8859-1');
1518
1519        /* Definindo location */
1520        if ($location = $component->getProperty('location', false, false))
1521            $schedulable['location'] = mb_convert_encoding($location, 'ISO-8859-1', 'UTF-8,ISO-8859-1');
1522
1523
1524        /* Definindo Class */
1525        $class = $component->getProperty('class', false, false);
1526        if ($class && defined(constant(strtoupper('CLASS_' . $class))))
1527            $schedulable['class'] = constant(strtoupper('CLASS_' . $class));
1528        else if (!isset($schedulable['class']))
1529            $schedulable['class'] = CLASS_PRIVATE; // padrão classe private
1530
1531        /* Definindo RRULE */
1532        if ($rrule = $component->getProperty('rrule', false, false)) {
1533            /* Gera um randon id para o contexto formater */
1534            $repeatID = mt_rand() . '3(Formatter)';
1535
1536            $repeat = array();
1537            $repeat['schedulable'] = $eventID;
1538            foreach ($rrule as $i => $v) {
1539                if (strtolower($i) == 'freq')
1540                    $repeat['frequency'] = $v;
1541                else if (strtolower($i) == 'until')
1542                    $repeat['endTime'] = $v;
1543                else
1544                    $repeat[strtolower($i)] = $v;
1545            }
1546
1547            $interation['repeat://' . $repeatID] = $repeat;
1548        }
1549
1550
1551        $schedulable['calendar'] = $params['calendar'];
1552
1553        $participantsInEvent = array();
1554
1555        //TODO: Participants com delegated nao estao sendo levados em conta
1556        while ($property = $component->getProperty('attendee', FALSE, TRUE)) {
1557            $participant = array();
1558
1559            $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
1560
1561            $participantID = ($tpID = self::_getParticipantByMail($mailUser, $schedulable['participants'])) ? $tpID : mt_rand() . '2(Formatter)';
1562            $participant['schedulable'] = $eventID;
1563
1564            if (isset($params['status']) && $mailUser == Config::me('mail'))
1565                $participant['status'] = $params['status'];
1566            else
1567                $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_' . $property['params']['PARTSTAT']) !== null ) ? constant('STATUS_' . $property['params']['PARTSTAT']) : STATUS_UNANSWERED;
1568
1569
1570            $participant['isOrganizer'] = '0';
1571
1572            /* Verifica se este usuario é um usuario interno do ldap */
1573            $intUser = Controller::find(array('concept' => 'user'), array('id', 'isExternal'), array('filter' => array('OR', array('=', 'mail', $mailUser), array('=', 'mailAlternateAddress', $mailUser))));
1574
1575            $user = null;
1576            if ($intUser && count($intUser) > 0) {
1577                $participant['isExternal'] = isset($intUser[0]['isExternal']) ? $intUser[0]['isExternal'] : 0;
1578                $participant['user'] = $intUser[0]['id'];
1579            } else {
1580                $participant['isExternal'] = 1;
1581                /* Gera um randon id para o contexto formater */
1582                $userID = mt_rand() . '4(Formatter)';
1583
1584                $user['mail'] = $mailUser;
1585                $user['isExternal'] = '1';
1586                $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
1587                $user['participants'] = array($participantID);
1588                $participant['user'] = $userID;
1589                $interation['user://' . $userID] = $user;
1590            }
1591
1592            $interation['participant://' . $participantID] = $participant;
1593            $schedulable['participants'][] = $participantID;
1594        };
1595
1596        if ($property = $component->getProperty('organizer', FALSE, TRUE)) {
1597            $participant = array();
1598            $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
1599
1600            $participantID = mt_rand() . '2(Formatter)';
1601
1602            $participant['schedulable'] = $eventID;
1603            $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_' . $property['params']['PARTSTAT']) !== null ) ? constant('STATUS_' . $property['params']['PARTSTAT']) : STATUS_UNANSWERED;
1604            $participant['isOrganizer'] = '1';
1605            $participant['acl'] = 'rowi';
1606
1607            /* Verifica se este usuario é um usuario interno do ldap */
1608            $intUser = Controller::find(array('concept' => 'user'), array('id', 'isExternal'), array('filter' => array('OR', array('=', 'mail', $mailUser), array('=', 'mailAlternateAddress', $mailUser))));
1609
1610            $user = null;
1611            if ($intUser && count($intUser) > 0) {
1612                $participant['isExternal'] = isset($intUser[0]['isExternal']) ? $intUser[0]['isExternal'] : 0;
1613                $participant['user'] = $intUser[0]['id'];
1614            } else {
1615                $participant['isExternal'] = 1;
1616                /* Gera um randon id para o contexto formater */
1617                $userID = mt_rand() . '4(Formatter)';
1618
1619                $user['mail'] = $mailUser;
1620                $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
1621                $user['participants'] = array($participantID);
1622                $user['isExternal'] = '1';
1623                $participant['user'] = $userID;
1624                $interation['user://' . $userID] = $user;
1625            }
1626
1627            $interation['participant://' . $participantID] = $participant;
1628            $schedulable['participants'][] = $participantID;
1629        } else if (!isset($schedulable['participants']) || !is_array($schedulable['participants']) || count($schedulable['participants']) < 1) {//caso não tenha organizador o usuario se torna organizador
1630            $user = Controller::read(array('concept' => 'user', 'id' => $params['owner']), array('mail'));
1631
1632            if (!self::_getParticipantByMail($user['mail'], $schedulable['participants'])) {
1633                        $participantID = mt_rand() . '2(Formatter)';
1634
1635                        $participant['schedulable'] = $eventID;
1636                        $participant['status'] = STATUS_CONFIRMED;
1637                        $participant['isOrganizer'] = '1';
1638                        $participant['acl'] = 'rowi';
1639                        $participant['isExternal'] = 0;
1640                        $participant['user'] = $params['owner'];
1641                        $interation['participant://' . $participantID] = $participant;
1642                        $schedulable['participants'][] = $participantID;
1643            }
1644        }
1645       
1646        $alarms = array();
1647       
1648        /* Definindo ALARMES */
1649        while ($alarmComp = $component->getComponent('valarm'))
1650        {
1651                $alarm = array();
1652                $alarmID = mt_rand() . '6(Formatter)';
1653                $action =  $alarmComp->getProperty('action', false, true);
1654                $trygger = $alarmComp->getProperty('trigger', false, true);
1655                $alarm['type'] = self::decodeAlarmAction($action['value']);
1656
1657                 if(isset($trygger['value']['day']))
1658                {
1659                        $alarm['time'] = $trygger['value']['day'];
1660                        $alarm['unit'] = 'd';
1661                }
1662                else if(isset($trygger['value']['hour']))
1663                {
1664                        $alarm['time'] = $trygger['value']['hour'];
1665                        $alarm['unit'] = 'h';
1666                }
1667                else if(isset($trygger['value']['min']))
1668                {
1669                        $alarm['time'] = $trygger['value']['min'];
1670                        $alarm['unit'] = 'm';
1671                }
1672               
1673                foreach ($interation as $iint => &$vint)
1674                {
1675                        if(isset($vint['user']) && $vint['user'] == Config::me('uidNumber'))
1676                        {
1677                                $alarm['participant'] = str_replace('participant://', '', $iint);       
1678                                $vint['alarms'][] = $alarmID;
1679                        }
1680                }
1681                $alarm['schedulable'] = $eventID;
1682                               
1683                $interation['alarm://' . $alarmID ] = $alarm;
1684               
1685        }
1686       
1687       
1688        /* Definindo DTSTAMP */
1689        if ($dtstamp = self::_getTime($component, 'dtstamp'))
1690            $schedulable['dtstamp'] = $dtstamp;
1691
1692        /* Definindo TRANSP */
1693        if (($tranp = $component->getProperty('transp', false, true)) && $tranp && is_string($tranp) && strtoupper($tranp) == 'OPAQUE')
1694            $schedulable['transparent'] = 1;
1695
1696        /* Definindo last_update */
1697        if ($lastUpdate = self::_getTime($component, 'LAST-MODIFIED'))
1698            $schedulable['lastUpdate'] = $lastUpdate;
1699
1700
1701        if ($sequence = $component->getProperty('SEQUENCE', false, false))
1702            $schedulable['sequence'] = $sequence;
1703
1704        if ($uid = $component->getProperty('uid', false, false))
1705            ;
1706        $schedulable['uid'] = $uid;
1707
1708        while ($attach = $component->getProperty('ATTACH', FALSE, TRUE)) {
1709
1710            $attachCurrent = array('name' => $attach['params']['X-FILENAME'],
1711                'size' => strlen($attach['value']),
1712                'type' => self::_getContentType($attach['params']['X-FILENAME'])
1713            );
1714
1715            $ids = Controller::find(array('concept' => 'attachment'), array('id'), array('filter' => array('AND', array('=', 'name', $attachCurrent['name']), array('=', 'size', $attachCurrent['size']), array('=', 'type', $attachCurrent['type']))));
1716
1717            if (!is_array($ids)) {
1718                $attachCurrent['source'] = $attach['value'];
1719                //insere o anexo no banco e pega id para colcar no relacionamento                               
1720                $idAttachment = Controller::create(array('concept' => 'attachment'), $attachCurrent);
1721            }else
1722                $idAttachment = array('id' => $ids[0]['id']);
1723
1724            $calendarToAttachmentId = mt_rand() . '2(Formatter)';
1725            $calendarToAttachment['attachment'] = $idAttachment['id'];
1726            $calendarToAttachment['schedulable'] = $eventID;
1727            $interation['schedulableToAttachment://' . $calendarToAttachmentId] = $calendarToAttachment;
1728
1729            $schedulable['attachments'][] = $calendarToAttachmentId;
1730        }
1731
1732        $schedulable['type'] = '1';
1733
1734        $interation['schedulable://' . $eventID] = $schedulable;
1735
1736        return $interation;
1737    }
1738   
1739    static private function _makeVTODO($schedulable, $component, $params) {
1740        $interation = array();
1741        $todoID = isset($schedulable['id']) ? $schedulable['id'] : mt_rand() . '(Formatter)';
1742
1743        /* Data de Inicio */
1744        $startTime = $component->getProperty('dtstart', false, true);
1745
1746        $tzid = (isset($startTime['params']['TZID']) ? $startTime['params']['TZID'] : $params['X-WR-TIMEZONE']);
1747
1748        /* Tiem zone do evento */
1749        if ($tzid){
1750            $tzid = self::nomalizeTZID($tzid);
1751            $schedulable['timezone'] = $tzid;
1752        }else
1753            $schedulable['timezone'] = isset($params['calendar_timezone']) ? $params['calendar_timezone'] : 'America/Sao_Paulo';
1754
1755        $objTimezone = new DateTimeZone($schedulable['timezone']);
1756
1757        if (isset($startTime['params']['VALUE']) && $startTime['params']['VALUE'] === 'DATE' && isset($params['calendar_timezone'])) {
1758            $schedulable['allDay'] = 1;
1759            $schedulable['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $schedulable['timezone'], '@' . self::date2timestamp($startTime['value'])) . '000';
1760        } 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 */
1761            $schedulable['startTime'] = self::date2timestamp($startTime['value']) - self::_getTzOffset('UTC', $tzid, '@' . self::date2timestamp($startTime['value'])) . '000';
1762            $schedulable['allDay'] = 0;
1763        } else {
1764            $schedulable['startTime'] = self::date2timestamp($startTime['value']) . '000';
1765            if (strpos($params['prodid'], 'Outlook') !== false) {
1766                //Se o ics veio em utc não aplicar horario de verão
1767                $sTime = new DateTime('@' . (int) ($schedulable['startTime'] / 1000), new DateTimeZone('UTC'));
1768                $sTime->setTimezone($objTimezone);
1769                if ($sTime->format('I')) //Se o ics veio em utc não aplicar horario de verão
1770                    $schedulable['startTime'] = $schedulable['startTime'] - 3600000;
1771            }
1772        }
1773
1774        /* Data de Termino */
1775        if($due = $component->getProperty('due', false, true)){
1776
1777        $tzid = isset($due['params']['TZID']) ? $due['params']['TZID'] : $params['X-WR-TIMEZONE'];
1778
1779        if($tzid)
1780            $tzid = self::nomalizeTZID($tzid);
1781
1782        if (isset($due['params']['VALUE']) && $due['params']['VALUE'] === 'DATE')
1783            $schedulable['due'] = self::date2timestamp($due['value']) - self::_getTzOffset('UTC', $schedulable['timezone'], '@' . self::date2timestamp($due['value'])) . '000';
1784        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 */
1785            $schedulable['due'] = self::date2timestamp($due['value']) - self::_getTzOffset('UTC', $tzid, '@' . self::date2timestamp($due['value'])) . '000';
1786        else {
1787            $schedulable['due'] = self::date2timestamp($due['value']) . '000';
1788            if (strpos($params['prodid'], 'Outlook') !== false) {
1789            //Se o ics veio em utc não aplicar horario de verão
1790            $dueTime = new DateTime('@' . (int) ($schedulable['due'] / 1000), new DateTimeZone('UTC'));
1791            $dueTime->setTimezone($objTimezone);
1792
1793            if ($dueTime->format('I'))
1794                $schedulable['due'] = $schedulable['due'] - 3600000;
1795            }
1796        }
1797        $schedulable['endTime'] = $schedulable['due'];
1798    }else
1799        $schedulable['endTime'] = $schedulable['startTime'];
1800
1801        $schedulable['type'] = '2'; //type schedulable
1802        $schedulable['summary'] = mb_convert_encoding($component->getProperty('summary', false, false), 'ISO-8859-1', 'UTF-8,ISO-8859-1');
1803
1804        /* Definindo Description */
1805        if ($desc = $component->getProperty('description', false, false))
1806            $schedulable['description'] = mb_convert_encoding(str_ireplace(array('\n', '\t'), array("\n", "\t"), $desc), 'ISO-8859-1', 'UTF-8,ISO-8859-1');
1807
1808        /* Definindo Class */
1809        $class = $component->getProperty('class', false, false);
1810        if ($class && defined(constant(strtoupper('CLASS_' . $class))))
1811            $schedulable['class'] = constant(strtoupper('CLASS_' . $class));
1812        else if (!isset($schedulable['class']))
1813            $schedulable['class'] = CLASS_PRIVATE; // padrão classe private
1814
1815        $schedulable['calendar'] = $params['calendar'];
1816
1817        $participantsInTodo = array();
1818
1819        //TODO: Participants com delegated nao estao sendo levados em conta
1820        while ($property = $component->getProperty('attendee', FALSE, TRUE)) {
1821            $participant = array();
1822
1823            $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
1824
1825            $participantID = ($tpID = self::_getParticipantByMail($mailUser, $schedulable['participants'])) ? $tpID : mt_rand() . '2(Formatter)';
1826            $participant['schedulable'] = $todoID;
1827
1828            if (isset($params['status']) && $mailUser == Config::me('mail'))
1829                $participant['status'] = $params['status'];
1830            else
1831                $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_' . $property['params']['PARTSTAT']) !== null ) ? constant('STATUS_' . $property['params']['PARTSTAT']) : STATUS_UNANSWERED;
1832
1833
1834            $participant['isOrganizer'] = '0';
1835
1836            /* Verifica se este usuario é um usuario interno do ldap */
1837            $intUser = Controller::find(array('concept' => 'user'), array('id', 'isExternal'), array('filter' => array('OR', array('=', 'mail', $mailUser), array('=', 'mailAlternateAddress', $mailUser))));
1838
1839            $user = null;
1840            if ($intUser && count($intUser) > 0) {
1841                $participant['isExternal'] = isset($intUser[0]['isExternal']) ? $intUser[0]['isExternal'] : 0;
1842                $participant['user'] = $intUser[0]['id'];
1843            } else {
1844                $participant['isExternal'] = 1;
1845                /* Gera um randon id para o contexto formater */
1846                $userID = mt_rand() . '4(Formatter)';
1847
1848                $user['mail'] = $mailUser;
1849                $user['isExternal'] = '1';
1850                $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
1851                $user['participants'] = array($participantID);
1852                $participant['user'] = $userID;
1853                $interation['user://' . $userID] = $user;
1854            }
1855
1856            $interation['participant://' . $participantID] = $participant;
1857            $schedulable['participants'][] = $participantID;
1858        };
1859
1860        if ($property = $component->getProperty('organizer', FALSE, TRUE)) {
1861            $participant = array();
1862            $mailUser = trim(str_replace('MAILTO:', '', $property['value']));
1863
1864            $participantID = mt_rand() . '2(Formatter)';
1865
1866            $participant['schedulable'] = $todoID;
1867            $participant['status'] = (isset($property['params']['PARTSTAT']) && constant('STATUS_' . $property['params']['PARTSTAT']) !== null ) ? constant('STATUS_' . $property['params']['PARTSTAT']) : STATUS_UNANSWERED;
1868            $participant['isOrganizer'] = '1';
1869            $participant['acl'] = 'rowi';
1870
1871            /* Verifica se este usuario é um usuario interno do ldap */
1872            $intUser = Controller::find(array('concept' => 'user'), array('id', 'isExternal'), array('filter' => array('OR', array('=', 'mail', $mailUser), array('=', 'mailAlternateAddress', $mailUser))));
1873
1874            $user = null;
1875            if ($intUser && count($intUser) > 0) {
1876                $participant['isExternal'] = isset($intUser[0]['isExternal']) ? $intUser[0]['isExternal'] : 0;
1877                $participant['user'] = $intUser[0]['id'];
1878            } else {
1879                $participant['isExternal'] = 1;
1880                /* Gera um randon id para o contexto formater */
1881                $userID = mt_rand() . '4(Formatter)';
1882
1883                $user['mail'] = $mailUser;
1884                $user['name'] = ( isset($property['params']['CN']) ) ? $property['params']['CN'] : '';
1885                $user['participants'] = array($participantID);
1886                $user['isExternal'] = '1';
1887                $participant['user'] = $userID;
1888                $interation['user://' . $userID] = $user;
1889            }
1890
1891            $interation['participant://' . $participantID] = $participant;
1892            $schedulable['participants'][] = $participantID;
1893            } else if (!isset($schedulable['participants']) || !is_array($schedulable['participants']) || count($schedulable['participants']) < 1) {//caso não tenha organizador o usuario se torna organizador
1894            $user = Controller::read(array('concept' => 'user', 'id' => $params['owner']), array('mail'));
1895
1896            if (!self::_getParticipantByMail($user['mail'], $schedulable['participants'])) {
1897                $participantID = mt_rand() . '2(Formatter)';
1898
1899                $participant['schedulable'] = $todoID;
1900                $participant['status'] = STATUS_CONFIRMED;
1901                $participant['isOrganizer'] = '1';
1902                $participant['acl'] = 'rowi';
1903                $participant['isExternal'] = 0;
1904                $participant['user'] = $params['owner'];
1905                $interation['participant://' . $participantID] = $participant;
1906                $schedulable['participants'][] = $participantID;
1907            }
1908        }
1909       
1910        $alarms = array();
1911       
1912        /* Definindo ALARMES */
1913        while ($alarmComp = $component->getComponent('valarm'))
1914        {
1915                $alarm = array();
1916                $alarmID = mt_rand() . '6(Formatter)';
1917                $action =  $alarmComp->getProperty('action', false, true);
1918                $trygger = $alarmComp->getProperty('trigger', false, true);
1919                $alarm['type'] = self::decodeAlarmAction($action['value']);
1920
1921                 if(isset($trygger['value']['day']))
1922                {
1923                        $alarm['time'] = $trygger['value']['day'];
1924                        $alarm['unit'] = 'd';
1925                }
1926                else if(isset($trygger['value']['hour']))
1927                {
1928                        $alarm['time'] = $trygger['value']['hour'];
1929                        $alarm['unit'] = 'h';
1930                }
1931                else if(isset($trygger['value']['min']))
1932                {
1933                        $alarm['time'] = $trygger['value']['min'];
1934                        $alarm['unit'] = 'm';
1935                }
1936               
1937                foreach ($interation as $iint => &$vint)
1938                {
1939                        if(isset($vint['user']) && $vint['user'] == Config::me('uidNumber'))
1940                        {
1941                                $alarm['participant'] = str_replace('participant://', '', $iint);       
1942                                $vint['alarms'][] = $alarmID;
1943                        }
1944                }
1945                $alarm['schedulable'] = $eventID;
1946                               
1947                $interation['alarm://' . $alarmID ] = $alarm;
1948               
1949        }
1950       
1951       
1952        /* Definindo DTSTAMP */
1953        if ($dtstamp = self::_getTime($component, 'dtstamp'))
1954            $schedulable['dtstamp'] = $dtstamp;
1955
1956        /* Definindo TRANSP */
1957        if (($tranp = $component->getProperty('transp', false, true)) && $tranp && is_string($tranp) && strtoupper($tranp) == 'OPAQUE')
1958            $schedulable['transparent'] = 1;
1959
1960        /* Definindo last_update */
1961        if ($lastUpdate = self::_getTime($component, 'LAST-MODIFIED'))
1962            $schedulable['lastUpdate'] = $lastUpdate;
1963
1964
1965        if ($sequence = $component->getProperty('SEQUENCE', false, false))
1966            $schedulable['sequence'] = $sequence;
1967
1968        if ($uid = $component->getProperty('uid', false, false))
1969            ;
1970        $schedulable['uid'] = $uid;
1971
1972        while ($attach = $component->getProperty('ATTACH', FALSE, TRUE)) {
1973
1974            $attachCurrent = array('name' => $attach['params']['X-FILENAME'],
1975                'size' => strlen($attach['value']),
1976                'type' => self::_getContentType($attach['params']['X-FILENAME'])
1977            );
1978
1979            $ids = Controller::find(array('concept' => 'attachment'), array('id'), array('filter' => array('AND', array('=', 'name', $attachCurrent['name']), array('=', 'size', $attachCurrent['size']), array('=', 'type', $attachCurrent['type']))));
1980
1981            if (!is_array($ids)) {
1982                $attachCurrent['source'] = $attach['value'];
1983                //insere o anexo no banco e pega id para colcar no relacionamento                               
1984                $idAttachment = Controller::create(array('concept' => 'attachment'), $attachCurrent);
1985            }else
1986                $idAttachment = array('id' => $ids[0]['id']);
1987
1988            $calendarToAttachmentId = mt_rand() . '2(Formatter)';
1989            $calendarToAttachment['attachment'] = $idAttachment['id'];
1990            $calendarToAttachment['schedulable'] = $eventID;
1991            $interation['schedulableToAttachment://' . $calendarToAttachmentId] = $calendarToAttachment;
1992
1993            $schedulable['attachments'][] = $calendarToAttachmentId;
1994        }
1995
1996        $interation['schedulable://' . $todoID] = $schedulable;
1997
1998    return $interation;
1999    }
2000
2001    static private function _getSchedulable($uid) {
2002        $schedulable = Controller::find(array('concept' => 'schedulable'), false, array('filter' => array('=', 'uid', $uid), 'deepness' => 2));
2003        return (isset($schedulable[0])) ? $schedulable[0] : false;
2004    }
2005
2006    static private function _existInMyCalendars($id, $owner) {
2007        $sig = Controller::find(array('concept' => 'calendarSignature'), array('user', 'calendar', 'isOwner'), array('filter' =>  array('=', 'user', $owner)));
2008        $calendars = array();
2009        foreach ($sig as $val)
2010            $calendars[] = $val['calendar'];
2011
2012        $return = Controller::find(array('concept' => 'calendarToSchedulable'), null, array('filter' => array('AND', array('IN', 'calendar', $calendars), array('=', 'schedulable', $id))));
2013
2014        return (isset($return[0])) ? $return[0]['calendar'] : false;
2015    }
2016
2017    static private function _getTime(&$component, $property) {
2018        if ($date = $component->getProperty($property, false, true))
2019            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';
2020
2021        return false;
2022    }
2023
2024    static private function _getContentType($fileName) {
2025        $strFileType = strtolower(substr($fileName, strrpos($fileName, '.')));
2026
2027        switch ($strFileType) {
2028            case ".asf": return "video/x-ms-asf";
2029            case ".avi": return "video/avi";
2030            case ".doc": return "application/msword";
2031            case ".zip": return "application/zip";
2032            case ".xls": return "application/vnd.ms-excel";
2033            case ".gif": return "image/gif";
2034            case ".bmp": return "image/bmp";
2035            case ".jpeg":
2036            case ".jpg": return "image/jpeg";
2037            case ".wav": return "audio/wav";
2038            case ".mp3": return "audio/mpeg3";
2039            case ".mpeg":
2040            case ".mpg": return "video/mpeg";
2041            case ".rtf": return "application/rtf";
2042            case ".html":
2043            case ".htm": return "text/html";
2044            case ".xml": return "text/xml";
2045            case ".xsl": return "text/xsl";
2046            case ".css": return "text/css";
2047            case ".php": return "text/php";
2048            case ".asp": return "text/asp";
2049            case ".pdf": return "application/pdf";
2050            case ".png": return "image/png";
2051            case ".txt": return "text/plain";
2052            case ".log": return "text/plain";
2053            case ".wmv": return "video/x-ms-wmv";
2054            case ".sxc": return "application/vnd.sun.xml.calc";
2055            case ".odt": return "application/vnd.oasis.opendocument.text";
2056            case ".stc": return "application/vnd.sun.xml.calc.template";
2057            case ".sxd": return "application/vnd.sun.xml.draw";
2058            case ".std": return "application/vnd.sun.xml.draw.template";
2059            case ".sxi": return "application/vnd.sun.xml.impress";
2060            case ".sti": return "application/vnd.sun.xml.impress.template";
2061            case ".sxm": return "application/vnd.sun.xml.math";
2062            case ".sxw": return "application/vnd.sun.xml.writer";
2063            case ".sxq": return "application/vnd.sun.xml.writer.global";
2064            case ".stw": return "application/vnd.sun.xml.writer.template";
2065            case ".pps": return "application/vnd.ms-powerpoint";
2066            case ".odt": return "application/vnd.oasis.opendocument.text";
2067            case ".ott": return "application/vnd.oasis.opendocument.text-template";
2068            case ".oth": return "application/vnd.oasis.opendocument.text-web";
2069            case ".odm": return "application/vnd.oasis.opendocument.text-master";
2070            case ".odg": return "application/vnd.oasis.opendocument.graphics";
2071            case ".otg": return "application/vnd.oasis.opendocument.graphics-template";
2072            case ".odp": return "application/vnd.oasis.opendocument.presentation";
2073            case ".otp": return "application/vnd.oasis.opendocument.presentation-template";
2074            case ".ods": return "application/vnd.oasis.opendocument.spreadsheet";
2075            case ".ots": return "application/vnd.oasis.opendocument.spreadsheet-template";
2076            case ".odc": return "application/vnd.oasis.opendocument.chart";
2077            case ".odf": return "application/vnd.oasis.opendocument.formula";
2078            case ".odi": return "application/vnd.oasis.opendocument.image";
2079            case ".ndl": return "application/vnd.lotus-notes";
2080            case ".eml": return "text/plain";
2081            case ".ps" : return "application/postscript";
2082            default : return "application/octet-stream";
2083        }
2084    }
2085   
2086    public function codeAlarmAction( $action )
2087    {
2088        switch ($action)
2089        {
2090                case ALARM_MAIL :
2091                                return  'EMAIL';
2092                                break;
2093                case ALARM_ALERT :
2094                                return  'DISPLAY';
2095                                break;
2096                case 'mail' :
2097                                        return  'EMAIL';
2098                                        break;
2099                case 'alert'  :
2100                                        return  'DISPLAY';
2101                                        break;
2102        }
2103       
2104    }
2105   
2106    public function decodeAlarmAction( $action )
2107    {
2108        switch ( $action )
2109        {
2110                case 'EMAIL'  :
2111                        return  'mail';
2112                        break;
2113                case 'DISPLAY' :
2114                        return  'alert';
2115                        break;
2116
2117        }
2118   
2119    }
2120
2121}
2122
2123?>
Note: See TracBrowser for help on using the repository browser.