source: sandbox/2.4.2-expresso1/prototype/services/iCal.php @ 6951

Revision 6951, 95.5 KB checked in by acoutinho, 12 years ago (diff)

Ticket #2966 - Suporte a importacao e exportacao de tarefas / correcoes de bugs

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