/** * @return array */ private function getICalPeriods() { $response = []; $iCalLink = $this->checkForICalHref(); if ($this->checkAccessToFile($iCalLink)) { $startArray = array(); $endArray = array(); require_once 'iCalcreator.php'; $v = new \vcalendar(); // initiate new CALENDAR $config = array("unique_id" => "ukrapts.com", "url" => "{$iCalLink}"); $v->setConfig($config); $v->parse(); $v->sort(); $i = 0; while ($comp = $v->getComponent("VEVENT")) { $dtstart_array = $comp->getProperty("DTSTART", 1, TRUE); $dtstart = $dtstart_array["value"]; $startDate = "{$dtstart["year"]}-{$dtstart["month"]}-{$dtstart["day"]}"; $dtend_array = $comp->getProperty("dtend", 1, TRUE); $dtend = $dtend_array["value"]; $endDate = "{$dtend["year"]}-{$dtend["month"]}-{$dtend["day"]}"; if ($endDate >= $this->DBQueries()->currentDate()) { $startArray[] = $startDate; $endArray[] = $endDate; } $i++; } sort($startArray); sort($endArray); $response['start'] = $startArray; $response['end'] = $endArray; } return $response; }
/** * The file contents is looked at and events are created here. * @param string $fileContents * @return integer The number of events saved. */ public function setEventsFromICal($fullDir, $fullFileName, $personId) { //start parse of local file $v = new vcalendar(); // set directory $v->setConfig('directory', $fullDir); // set file name $v->setConfig('filename', $fullFileName); $v->parse(); $count = 0; foreach ($v->components as $component => $info) { # get first vevent $comp = $v->getComponent("VEVENT"); $summary_array = $comp->getProperty("summary", 1, TRUE); //echo "Event: ", $summary_array["value"], "\n"; $dtstart_array = $comp->getProperty("dtstart", 1, TRUE); $dtstart = $dtstart_array["value"]; $startDate = "{$dtstart["year"]}-{$dtstart["month"]}-{$dtstart["day"]}"; $startTime = "{$dtstart["hour"]}:{$dtstart["min"]}:{$dtstart["sec"]}"; $dtend_array = $comp->getProperty("dtend", 1, TRUE); $dtend = $dtend_array["value"]; $endDate = "{$dtend["year"]}-{$dtend["month"]}-{$dtend["day"]}"; $endTime = "{$dtend["hour"]}:{$dtend["min"]}:{$dtend["sec"]}"; //echo "start: ", $startDate,"T",$startTime, "\n"; //echo "end: ", $endDate,"T",$endTime, "\n"; $location_array = $comp->getProperty("location", 1, TRUE); //echo "Location: ", $location_array["value"], "\n <br>"; //TODO: Check that this event does not already exist. $event = new Event(); $event->setPersonId($personId); $event->setName($summary_array["value"]); $event->setStartTime($startDate . "T" . $startTime); $event->setEndTime($endDate . "T" . $endTime); $event->setLocation($location_array["value"]); $event->save(); $count++; } return $count; }
public function get_json() { $event_json = array(); $filters = $this->in->exists('filters', 'int') ? $this->in->getArray('filters', 'int') : false; // parse the feeds $feeds = $this->pdh->get('calendars', 'idlist', array('feed', $filters)); if (is_array($feeds) && count($feeds) > 0) { foreach ($feeds as $feed) { $feedurl = $this->pdh->get('calendars', 'feed', array($feed)); if (isValidURL($feedurl)) { require_once $this->root_path . 'libraries/icalcreator/iCalcreator.class.php'; $vcalendar = new vcalendar(array('url' => $feedurl)); if (TRUE === $vcalendar->parse()) { $vcalendar->sort(); while ($comp = $vcalendar->getComponent('vevent')) { $startdate = $comp->getProperty('dtstart', 1); $enddate = $comp->getProperty('dtend', 1); $startdate_out = $startdate['year'] . '-' . $startdate['month'] . '-' . $startdate['day'] . ' ' . (isset($startdate['hour']) ? $startdate['hour'] . ':' . $startdate['min'] : '00:00'); $enddate_out = $enddate['year'] . '-' . $enddate['month'] . '-' . $enddate['day'] . ' ' . (isset($enddate['hour']) ? $enddate['hour'] . ':' . $enddate['min'] : '00:00'); $allday = isset($enddate['hour']) && isset($startdate['hour']) ? false : true; $eventcolor = $this->pdh->get('calendars', 'color', $feed); $eventcolor_txt = get_brightness($eventcolor) > 130 ? 'black' : 'white'; $event_json[] = array('eventid' => $calid, 'title' => $comp->getProperty('summary', 1), 'start' => $startdate_out, 'end' => $enddate_out, 'allDay' => $allday, 'note' => $comp->getProperty('description', 1), 'color' => '#' . $eventcolor, 'textColor' => $eventcolor_txt); } } } } } // add the calendar events to the json feed $calendars = $this->pdh->get('calendars', 'idlist', array('nofeed', $filters)); $caleventids = $this->pdh->get('calendar_events', 'id_list', array(false, $this->in->get('start', 0), $this->in->get('end', 0))); if (is_array($caleventids) && count($caleventids) > 0) { foreach ($caleventids as $calid) { $eventextension = $this->pdh->get('calendar_events', 'extension', array($calid)); $raidmode = $eventextension['calendarmode']; $eventcolor = $this->pdh->get('calendars', 'color', $this->pdh->get('calendar_events', 'calendar_id', array($calid))); $eventcolor_txt = get_brightness($eventcolor) > 130 ? 'black' : 'white'; if (in_array($this->pdh->get('calendar_events', 'calendar_id', array($calid)), $calendars)) { if ($raidmode == 'raid') { // fetch the attendees $attendees_raw = $this->pdh->get('calendar_raids_attendees', 'attendees', array($calid)); $attendees = array(); if (is_array($attendees_raw)) { foreach ($attendees_raw as $attendeeid => $attendeerow) { $attendees[$attendeerow['signup_status']][$attendeeid] = $attendeerow; } } // Build the guest array $guests = array(); if (registry::register('config')->get('calendar_raid_guests') == 1) { $guestarray = registry::register('plus_datahandler')->get('calendar_raids_guests', 'members', array($calid)); if (is_array($guestarray)) { foreach ($guestarray as $guest_row) { $guests[] = $guest_row['name']; } } } // fetch per raid data $raidcal_status = unserialize($this->config->get('calendar_raid_status')); $rstatusdata = ''; if (is_array($raidcal_status)) { foreach ($raidcal_status as $raidcalstat_id) { if ($raidcalstat_id != 4) { $actcount = isset($attendees[$raidcalstat_id]) ? count($attendees[$raidcalstat_id]) : 0; if ($raidcalstat_id == 0) { $actcount += is_array($guests) ? count($guests) : 0; } $rstatusdata .= '<div class="raid_status' . $raidcalstat_id . '">' . $this->user->lang(array('raidevent_raid_status', $raidcalstat_id)) . ': ' . $actcount . '</div>'; } } } $rstatusdata .= '<div class="raid_status_total">' . $this->user->lang('raidevent_raid_required') . ': ' . (isset($eventextension) ? $eventextension['attendee_count'] : 0) . '</div>'; $deadlinedate = $this->pdh->get('calendar_events', 'time_start', array($calid)) - $eventextension['deadlinedate'] * 3600; $deadline = $deadlinedate > $this->time->time || $this->config->get('calendar_raid_allowstatuschange') == '1' && $this->pdh->get('calendar_raids_attendees', 'status', array($calid, $this->user->id)) > 0 && $this->pdh->get('calendar_raids_attendees', 'status', array($calid, $this->user->id)) != 4 && $this->pdh->get('calendar_events', 'time_end', array($calid)) > $this->time->time ? false : true; $deadlineflag = $deadline ? '<img src="' . $this->root_path . 'images/calendar/clock_s.png" alt="Deadline" title="' . $this->user->lang('raidevent_raid_deadl_reach') . '" />' : ''; // Build the JSON $event_json[] = array('title' => $this->in->decode_entity($this->pdh->get('calendar_events', 'name', array($calid))), 'start' => $this->time->date('Y-m-d H:i', $this->pdh->get('calendar_events', 'time_start', array($calid))), 'end' => $this->time->date('Y-m-d H:i', $this->pdh->get('calendar_events', 'time_end', array($calid))), 'closed' => $this->pdh->get('calendar_events', 'raidstatus', array($calid)) == 1 ? true : false, 'editable' => true, 'eventid' => $calid, 'flag' => $deadlineflag . $this->pdh->get('calendar_raids_attendees', 'html_status', array($calid, $this->user->data['user_id'])), 'url' => 'calendar/viewcalraid.php' . $this->SID . '&eventid=' . $calid, 'icon' => $eventextension['raid_eventid'] ? $this->pdh->get('event', 'icon', array($eventextension['raid_eventid'], true, true)) : '', 'note' => $this->pdh->get('calendar_events', 'notes', array($calid)), 'raidleader' => $eventextension['raidleader'] > 0 ? implode(', ', $this->pdh->aget('member', 'name', 0, array($eventextension['raidleader']))) : '', 'rstatusdata' => $rstatusdata, 'color' => '#' . $eventcolor, 'textColor' => $eventcolor_txt); } else { $event_json[] = array('eventid' => $calid, 'title' => $this->pdh->get('calendar_events', 'name', array($calid)), 'start' => $this->time->date('Y-m-d H:i', $this->pdh->get('calendar_events', 'time_start', array($calid))), 'end' => $this->time->date('Y-m-d H:i', $this->pdh->get('calendar_events', 'time_end', array($calid))), 'allDay' => $this->pdh->get('calendar_events', 'allday', array($calid)) > 0 ? true : false, 'note' => $this->pdh->get('calendar_events', 'notes', array($calid)), 'color' => '#' . $eventcolor, 'textColor' => $eventcolor_txt); } } } } // Output the array as JSON echo json_encode($event_json); exit; }
$self_age = filemtime($_SERVER['SCRIPT_FILENAME']); $self_age_fm = strftime("%Y%M%DT%H%M%SZ", $self_age); $fname = "facebook-$uid-$key.ics"; $age = filemtime($fname); if (!$age || time()-$age > 60*60) { #http://www.fbcalendar.com/cal/707610112/9b857cca655089425da6d912-707610112/events/ics/attending,unsure,not_replied/ $data = file_get_contents("http://www.fbcalendar.com/cal/$uid/$key-$uid/events/ics/attending,unsure,not_replied/"); if ($data == "") die("Can't read from Facebook!\n"); file_put_contents($fname, $data); } $v->parse($fname); $tz = genTimezone ($timezone); $tzid = $tz->getProperty("TZID"); $out->addComponent($tz); while( $vevent = $v->getComponent( 'vevent' )) { $url = $vevent->getProperty('URL'); preg_match("/eid%3D(\d+)/", $url, $matches); if (count($matches) != 2) { print_r($matches); die("Wrong number of matches!"); } $url = "http://www.facebook.com/event.php?eid=".$matches[1];
* modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program; if not, see <http://www.gnu.org/licenses/>. */ require_once( 'iCalcreator.class.php' ); $v = new vcalendar(); // create a new calendar instance $v->parse($argv[1]); echo "<?\n\$_tz = array("; foreach ($v->components as $tz) { if (!is_a($tz, "vtimezone")) continue; $location = $tz->getProperty("X-LIC-LOCATION"); $name = trim($location[1]); if ($name == "") { $location = $tz->getProperty("TZID"); $name = trim($location); } $junk = array(); echo "\"$name\" => \"".urlencode($tz->createComponent($junk))."\",\n";
/** * function iCal2xls * * Convert iCal file to xls format and send file to browser (default) or save xls file to disk * Definition iCal : rcf2445, http://kigkonsult.se/downloads/index.php#rfc * Using iCalcreator: http://kigkonsult.se/downloads/index.php#iCalcreator * Based on PEAR Spreadsheet_Excel_Writer-0.9.1 (and OLE-1.0.0RC1) * to be installed as * pear install channel://pear.php.net/OLE-1.0.0RC1 * pear install channel://pear.php.net/Spreadsheet_Excel_Writer-0.9.1 * * @author Kjell-Inge Gustafsson <*****@*****.**> * @since 3.0 - 2011-12-21 * @param object $calendar opt. iCalcreator calendar instance * @return bool returns FALSE when error */ public function iCal2xls($calendar = FALSE) { $timeexec = array('start' => microtime(TRUE)); if ($this->log) { $this->log->log(' ********** START **********', PEAR_LOG_NOTICE); } /** check input/output directory and filename */ $inputdirFile = $outputdirFile = ''; $inputFileParts = $outputFileParts = array(); $remoteInput = $remoteOutput = FALSE; if ($calendar) { $inputdirFile = $calendar->getConfig('DIRFILE'); $inputFileParts = pathinfo($inputdirFile); $inputFileParts['dirname'] = realpath($inputFileParts['dirname']); if ($this->log) { $this->log->log('fileParts:' . var_export($inputFileParts, TRUE), PEAR_LOG_DEBUG); } } elseif (FALSE === $this->_fixIO('input', 'ics', $inputdirFile, $inputFileParts, $remoteInput)) { if ($this->log) { $this->log->log(number_format(microtime(TRUE) - $timeexec['start'], 5) . ' sec', PEAR_LOG_ERR); $this->log->log("ERROR 2, invalid input ({$inputdirFile})", PEAR_LOG_ERR); $this->log->flush(); } return FALSE; } if (FALSE === $this->_fixIO('output', FALSE, $outputdirFile, $outputFileParts, $remoteOutput)) { if (FALSE === $this->setConfig('outputfilename', $inputFileParts['filename'] . '.xls')) { if ($this->log) { $this->log->log(number_format(microtime(TRUE) - $timeexec['start'], 5) . ' sec', PEAR_LOG_ERR); $this->log->log('ERROR 3, invalid output (' . $inputFileParts['filename'] . '.csv)', PEAR_LOG_ERR); $this->log->flush(); } return FALSE; } $outputdirFile = $this->getConfig('outputdirectory') . DIRECTORY_SEPARATOR . $inputFileParts['filename'] . '.xls'; $outputFileParts = pathinfo($outputdirFile); if ($this->log) { $this->log->log("output set to '{$outputdirFile}'", PEAR_LOG_INFO); } } if ($this->log) { $this->log->log("INPUT..FILE:{$inputdirFile}", PEAR_LOG_NOTICE); $this->log->log("OUTPUT.FILE:{$outputdirFile}", PEAR_LOG_NOTICE); } $save = $this->getConfig('save'); if ($calendar) { $calnl = $calendar->getConfig('nl'); } else { /** iCalcreator set config, read and parse input iCal file */ $calendar = new vcalendar(); if (FALSE !== ($unique_id = $this->getConfig('unique_id'))) { $calendar->setConfig('unique_id', $unique_id); } $calnl = $calendar->getConfig('nl'); if ($remoteInput) { if (FALSE === $calendar->setConfig('url', $inputdirFile)) { if ($this->log) { $this->log->log("ERROR 3 INPUT FILE:'{$inputdirFile}' iCalcreator: invalid url", 3); } return FALSE; } } else { if (FALSE === $calendar->setConfig('directory', $inputFileParts['dirname'])) { if ($this->log) { $this->log->log("ERROR 4 INPUT FILE:'{$inputdirFile}' iCalcreator: invalid directory: '" . $inputFileParts['dirname'] . "'", 3); $this->log->flush(); } return FALSE; } if (FALSE === $calendar->setConfig('filename', $inputFileParts['basename'])) { if ($this->log) { $this->log->log("ERROR 5 INPUT FILE:'{$inputdirFile}' iCalcreator: invalid filename: '" . $inputFileParts['basename'] . "'", 3); $this->log->flush(); } return FALSE; } } if (FALSE === $calendar->parse()) { if ($this->log) { $this->log->log("ERROR 6 INPUT FILE:'{$inputdirFile}' iCalcreator parse error", 3); $this->log->flush(); } return FALSE; } } // end if( !$calendar ) $timeexec['fileOk'] = microtime(TRUE); if (!function_exists('iCaldate2timestamp')) { function iCaldate2timestamp($d) { if (6 > count($d)) { return mktime(0, 0, 0, $d['month'], $d['day'], $d['year']); } else { return mktime($d['hour'], $d['min'], $d['sec'], $d['month'], $d['day'], $d['year']); } } } if (!function_exists('fixiCalString')) { function fixiCalString($s) { global $calnl; $s = str_replace('\\,', ',', $s); $s = str_replace('\\;', ';', $s); $s = str_replace('\\n ', chr(10), $s); $s = str_replace('\\\\', '\\', $s); $s = str_replace("{$calnl}", chr(10), $s); return utf8_decode($s); } } /** Creating a workbook */ require_once 'Spreadsheet/Excel/Writer.php'; if ($save) { $workbook = new Spreadsheet_Excel_Writer($outputdirFile); } else { $workbook = new Spreadsheet_Excel_Writer(); } $workbook->setVersion(8); // Use Excel97/2000 Format /** opt. sending HTTP headers */ if (!$save) { $workbook->send($outputFileParts['basename']); } /** Creating a worksheet */ $worksheet =& $workbook->addWorksheet($inputFileParts['filename']); /** fix formats */ $format_bold =& $workbook->addFormat(); $format_bold->setBold(); $timeexec['wrkbkOk'] = microtime(TRUE); /** info rows */ $row = -1; $worksheet->writeString(++$row, 0, 'kigkonsult.se', $format_bold); $worksheet->writeString($row, 1, ICALCREATOR_VERSION, $format_bold); $worksheet->writeString($row, 2, ICALCNVVERSION . ' iCal2xls', $format_bold); $worksheet->writeString($row, 3, date('Y-m-d H:i:s')); $filename = $remoteInput ? $inputdirFile : $inputFileParts['basename']; $worksheet->writeString(++$row, 0, 'iCal input', $format_bold); $worksheet->writeString($row, 1, $filename); $worksheet->writeString($row, 2, 'xls output', $format_bold); $worksheet->writeString($row, 3, $outputFileParts['basename']); if (FALSE !== ($prop = $calendar->getProperty('CALSCALE'))) { $worksheet->writeString(++$row, 0, 'CALSCALE', $format_bold); $worksheet->writeString($row, 1, $prop); } if (FALSE !== ($prop = $calendar->getProperty('METHOD'))) { $worksheet->writeString(++$row, 0, 'METHOD', $format_bold); $worksheet->writeString($row, 1, $prop); } while (FALSE !== ($xprop = $calendar->getProperty())) { $worksheet->writeString(++$row, 0, $xprop[0], $format_bold); $worksheet->writeString($row, 1, $xprop[1]); } $timeexec['infoOk'] = microtime(TRUE); if (FALSE === ($propsToSkip = $this->getConfig('skip'))) { $propsToSkip = array(); } /** fix property order list */ $proporderOrg = array(); for ($key = 2; $key < 99; $key++) { if (FALSE !== ($value = $this->getConfig($key))) { $proporderOrg[$value] = $key; if ($this->log) { $this->log->log("{$value} in column {$key}", 7); } } } /** fix vtimezone property order list */ $proporder = $proporderOrg; $proporder['TYPE'] = 0; $proporder['ORDER'] = 1; $props = array('TZID', 'LAST-MODIFIED', 'TZURL', 'DTSTART', 'TZOFFSETTO', 'TZOFFSETFROM', 'COMMENT', 'RRULE', 'RDATE', 'TZNAME'); $pix = 2; foreach ($props as $prop) { if (isset($proporder[$prop])) { continue; } if (in_array($prop, $propsToSkip)) { if ($this->log) { $this->log->log("'{$prop}' removed from output", 7); } continue; } while (in_array($pix, $proporder)) { $pix++; } $proporder[$prop] = $pix++; } /** remove unused properties from and add x-props to property order list */ $maxpropix = 11; if ($maxpropix != count($proporder) - 1) { $maxpropix = count($proporder) - 1; } $compsinfo = $calendar->getConfig('compsinfo'); $potmp = array(); $potmp[0] = 'TYPE'; $potmp[1] = 'ORDER'; foreach ($compsinfo as $cix => $compinfo) { if ('vtimezone' != $compinfo['type']) { continue; } $comp = $calendar->getComponent($compinfo['ordno']); foreach ($compinfo['props'] as $propName => $propcnt) { if (!in_array($propName, $potmp) && isset($proporder[$propName])) { $potmp[$proporder[$propName]] = $propName; } elseif ('X-PROP' == $propName) { while ($xprop = $comp->getProperty()) { if (!in_array($xprop[0], $potmp)) { $maxpropix += 1; $potmp[$maxpropix] = $xprop[0]; } // end if } // end while xprop } // end X-PROP } // end $compinfo['props'] if (isset($compinfo['sub'])) { foreach ($compinfo['sub'] as $compinfo2) { foreach ($compinfo2['props'] as $propName => $propcnt) { if (!in_array($propName, $potmp) && isset($proporder[$propName])) { $potmp[$proporder[$propName]] = $propName; } elseif ('X-PROP' == $propName) { $scomp = $comp->getComponent($compinfo2['ordno']); while ($xprop = $scomp->getProperty()) { if (!in_array($xprop[0], $potmp)) { $maxpropix += 1; $potmp[$maxpropix] = $xprop[0]; } // end if } // end while xprop } // end X-PROP } // end $compinfo['sub']['props'] } // end foreach( $compinfo['sub'] } // end if( isset( $compinfo['sub'] } // end foreach compinfo - vtimezone ksort($potmp, SORT_NUMERIC); $proporder = array_flip(array_values($potmp)); if ($this->log) { $this->log->log("timezone proporder=" . implode(',', array_flip($proporder)), 7); } /** create vtimezone info */ if (2 < count($proporder)) { $row += 1; /** create vtimezone header row */ foreach ($proporder as $propName => $col) { if (isset($this->config[$propName])) { $worksheet->writeString($row, $col, $this->config[$propName], $format_bold); // check map of userfriendly name to iCal property name if ($this->log) { $this->log->log("header row, col={$col}: {$propName}, replaced by " . $this->config[$propName], 7); } } else { $worksheet->writeString($row, $col, $propName, $format_bold); } } $allowedProps = array('VTIMEZONE' => array('TZID', 'LAST-MODIFIED', 'TZURL'), 'STANDARD' => array('DTSTART', 'TZOFFSETTO', 'TZOFFSETFROM', 'COMMENT', 'RDATE', 'RRULE', 'TZNAME'), 'DAYLIGHT' => array('DTSTART', 'TZOFFSETTO', 'TZOFFSETFROM', 'COMMENT', 'RDATE', 'RRULE', 'TZNAME')); /** create vtimezone data rows */ foreach ($compsinfo as $cix => $compinfo) { if ('vtimezone' != $compinfo['type']) { continue; } $row += 1; $worksheet->writeString($row, $proporder['TYPE'], $compinfo['type']); $worksheet->writeString($row, $proporder['ORDER'], $compinfo['ordno']); $comp = $calendar->getComponent($compinfo['ordno']); foreach ($proporder as $propName => $col) { if ('TYPE' == $propName || 'ORDER' == $propName) { continue; } if ('X-' == substr($propName, 0, 2)) { continue; } if (!in_array($propName, $allowedProps['VTIMEZONE'])) { // check if component allows property if ($this->log) { $this->log->log("ERROR 7, INPUT FILE:'{$inputdirFile}' iCalcreator: unvalid property for component '" . $compinfo['type'] . "': '{$propName}'", PEAR_LOG_INFO); } continue; } if (isset($compinfo['props'][$propName])) { if ('LAST-MODIFIED' == $propName) { $fcn = 'createLastModified'; } else { $fcn = 'create' . strtoupper(substr($propName, 0, 1)) . strtolower(substr($propName, 1)); } if (!method_exists($comp, $fcn)) { if ($this->log) { $this->log->log('ERROR 8 INPUT FILE:"' . $filename . '" iCalcreator: unknown property: "' . $propName . '" (' . $fcn . ')', PEAR_LOG_INFO); } continue; } $output = str_replace("{$calnl} ", '', rtrim($comp->{$fcn}())); $output = str_replace($propName . ';', '', $output); $output = str_replace($propName . ':', '', $output); $worksheet->writeString($row, $proporder[$propName], fixiCalString($output)); } } // end foreach( $proporder if (isset($compinfo['props']['X-PROP'])) { while ($xprop = $comp->getProperty()) { $output = str_replace("{$calnl} ", '', rtrim($xprop[1])); $worksheet->writeString($row, $proporder[$xprop[0]], fixiCalString($output)); } } if (isset($compinfo['sub'])) { foreach ($compinfo['sub'] as $compinfo2) { $row += 1; $worksheet->writeString($row, $proporder['TYPE'], $compinfo2['type']); $worksheet->writeString($row, $proporder['ORDER'], $compinfo['ordno'] . ':' . $compinfo2['ordno']); $scomp = $comp->getComponent($compinfo2['ordno']); foreach ($proporder as $propName => $col) { if ('TYPE' == $propName || 'ORDER' == $propName) { continue; } if ('X-' == substr($propName, 0, 2)) { continue; } if (!in_array($propName, $allowedProps[strtoupper($compinfo2['type'])])) { // check if component allows property if ($this->log) { $this->log->log("ERROR 9, INPUT FILE:'{$inputdirFile}' iCalcreator: unvalid property for component '" . $compinfo2['type'] . "': '{$propName}'", PEAR_LOG_INFO); } continue; } if (isset($compinfo2['props'][$propName])) { $fcn = 'create' . strtoupper(substr($propName, 0, 1)) . strtolower(substr($propName, 1)); if (!method_exists($scomp, $fcn)) { if ($this->log) { $this->log->log('ERROR 10 INPUT FILE:"' . $filename . '" iCalcreator: unknown property: "' . $propName . '" (' . $fcn . ')', PEAR_LOG_INFO); } continue; } $output = str_replace("{$calnl} ", '', rtrim($scomp->{$fcn}())); $output = str_replace($propName . ';', '', $output); $output = str_replace($propName . ':', '', $output); $worksheet->writeString($row, $proporder[$propName], fixiCalString($output)); } } // end foreach( $proporder if (isset($compinfo2['props']['X-PROP'])) { while ($xprop = $scomp->getProperty()) { $output = str_replace("{$calnl} ", '', rtrim($xprop[1])); $worksheet->writeString($row, $proporder[$xprop[0]], fixiCalString($output)); } } } // end foreach( $compinfo['sub'] } // end if( isset( $compinfo['sub']['props'] )) } // end foreach } // end vtimezone $timeexec['zoneOk'] = microtime(TRUE); $maxColCount = count($proporder); /** fix property order list */ $proporder = $proporderOrg; $proporder['TYPE'] = 0; $proporder['ORDER'] = 1; $props = array('UID', 'DTSTAMP', 'SUMMARY', 'DTSTART', 'DURATION', 'DTEND', 'DUE', 'RRULE', 'RDATE', 'EXRULE', 'EXDATE', 'DESCRIPTION', 'CATEGORIES', 'ORGANIZER', 'LOCATION', 'RESOURCES', 'CONTACT', 'URL', 'COMMENT', 'PRIORITY', 'ATTENDEE', 'CLASS', 'TRANSP', 'SEQUENCE', 'STATUS', 'COMPLETED', 'CREATED', 'LAST-MODIFIED', 'ACTION', 'TRIGGER', 'REPEAT', 'ATTACH', 'FREEBUSY', 'RELATED-TO', 'REQUEST-STATUS', 'GEO', 'PERCENT-COMPLETE', 'RECURRENCE-ID'); $pix = 2; foreach ($props as $prop) { if (isset($proporder[$prop])) { continue; } if (in_array($prop, $propsToSkip)) { if ($this->log) { $this->log->log("'{$prop}' removed from output", 7); } continue; } while (in_array($pix, $proporder)) { $pix++; } $proporder[$prop] = $pix++; } /** remove unused properties from and add x-props to property order list */ if ($maxpropix < count($proporder) - 1) { $maxpropix = count($proporder) - 1; } $potmp = array(); $potmp[0] = 'TYPE'; $potmp[1] = 'ORDER'; // $potmp[2] = 'UID'; foreach ($compsinfo as $cix => $compinfo) { if ('vtimezone' == $compinfo['type']) { continue; } foreach ($compinfo['props'] as $propName => $propcnt) { if (!in_array($propName, $potmp) && isset($proporder[$propName])) { $potmp[$proporder[$propName]] = $propName; } elseif ('X-PROP' == $propName) { $comp = $calendar->getComponent($compinfo['ordno']); while ($xprop = $comp->getProperty()) { if (!in_array($xprop[0], $potmp)) { $maxpropix += 1; $potmp[$maxpropix] = $xprop[0]; } // end if } // while( $xprop } // end elseif( 'X-PROP' } // end foreach( $compinfo['props'] if (isset($compinfo['sub'])) { foreach ($compinfo['sub'] as $compinfo2) { foreach ($compinfo2['props'] as $propName => $propcnt) { if (!in_array($propName, $potmp) && isset($proporder[$propName])) { $potmp[$proporder[$propName]] = $propName; } elseif ('X-PROP' == $propName) { $scomp = $comp->getComponent($compinfo2['ordno']); while ($xprop = $scomp->getProperty()) { if (!in_array($xprop[0], $potmp)) { $maxpropix += 1; $potmp[$maxpropix] = $xprop[0]; } // end if } // end while xprop } // end X-PROP } // end $compinfo['sub']['props'] } // end foreach( $compinfo['sub'] } // end if( isset( $compinfo['sub'] } ksort($potmp, SORT_NUMERIC); $proporder = array_flip(array_values($potmp)); if ($this->log) { $this->log->log("comp proporder=" . implode(',', array_flip($proporder)), 7); } if ($maxColCount < count($proporder)) { $maxColCount = count($proporder); } /** create header row */ $row += 1; foreach ($proporder as $propName => $col) { if (isset($this->config[$propName])) { $worksheet->writeString($row, $col, $this->config[$propName], $format_bold); // check map of userfriendly name to iCal property name if ($this->log) { $this->log->log("header row, col={$col}: {$propName}, replaced by " . $this->config[$propName], 7); } } else { $worksheet->writeString($row, $col, $propName, $format_bold); } } $allowedProps = array('VEVENT' => array('ATTACH', 'ATTENDEE', 'CATEGORIES', 'CLASS', 'COMMENT', 'CONTACT', 'CREATED', 'DESCRIPTION', 'DTEND', 'DTSTAMP', 'DTSTART', 'DURATION', 'EXDATE', 'RXRULE', 'GEO', 'LAST-MODIFIED', 'LOCATION', 'ORGANIZER', 'PRIORITY', 'RDATE', 'RECURRENCE-ID', 'RELATED-TO', 'RESOURCES', 'RRULE', 'REQUEST-STATUS', 'SEQUENCE', 'STATUS', 'SUMMARY', 'TRANSP', 'UID', 'URL'), 'VTODO' => array('ATTACH', 'ATTENDEE', 'CATEGORIES', 'CLASS', 'COMMENT', 'COMPLETED', 'CONTACT', 'CREATED', 'DESCRIPTION', 'DTSTAMP', 'DTSTART', 'DUE', 'DURATION', 'EXDATE', 'EXRULE', 'GEO', 'LAST-MODIFIED', 'LOCATION', 'ORGANIZER', 'PERCENT', 'PRIORITY', 'RDATE', 'RECURRENCE-ID', 'RELATED-TO', 'RESOURCES', 'RRULE', 'REQUEST-STATUS', 'SEQUENCE', 'STATUS', 'SUMMARY', 'UID', 'URL'), 'VJOURNAL' => array('ATTACH', 'ATTENDEE', 'CATEGORIES', 'CLASS', 'COMMENT', 'CONTACT', 'CREATED', 'DESCRIPTION', 'DTSTAMP', 'DTSTART', 'EXDATE', 'EXRULE', 'LAST-MODIFIED', 'ORGANIZER', 'RDATE', 'RECURRENCE-ID', 'RELATED-TO', 'RRULE', 'REQUEST-STATUS', 'SEQUENCE', 'STATUS', 'SUMMARY', 'UID', 'URL'), 'VFREEBUSY' => array('ATTENDEE', 'COMMENT', 'CONTACT', 'DTEND', 'DTSTAMP', 'DTSTART', 'DURATION', 'FREEBUSY', 'ORGANIZER', 'UID', 'URL'), 'VALARM' => array('ACTION', 'ATTACH', 'ATTENDEE', 'DESCRIPTION', 'DURATION', 'REPEAT', 'SUMMARY', 'TRIGGER')); /** create data rows */ foreach ($compsinfo as $cix => $compinfo) { if ('vtimezone' == $compinfo['type']) { continue; } $row += 1; $worksheet->writeString($row, $proporder['TYPE'], $compinfo['type']); $worksheet->writeString($row, $proporder['ORDER'], $compinfo['ordno']); // $worksheet->write( $row, $proporder['UID'], $compinfo['uid'] ); $comp = $calendar->getComponent($compinfo['ordno']); foreach ($proporder as $propName => $col) { if ('TYPE' == $propName || 'ORDER' == $propName) { continue; } if ('X-' == substr($propName, 0, 2)) { continue; } if (!in_array($propName, $allowedProps[strtoupper($compinfo['type'])])) { // check if component allows property if ($this->log) { $this->log->log("ERROR 11, INPUT FILE:'{$inputdirFile}' iCalcreator: unvalid property for component '" . $compinfo['type'] . "': '{$propName}'", PEAR_LOG_INFO); } continue; } if (isset($compinfo['props'][$propName])) { switch ($propName) { case 'LAST-MODIFIED': $fcn = 'createLastModified'; break; case 'RECURRENCE-ID': $fcn = 'createRecurrenceid'; break; case 'RELATED-TO': $fcn = 'createRelatedTo'; break; case 'REQUEST-STATUS': $fcn = 'createRequestStatus'; break; case 'PERCENT-COMPLETE': $fcn = 'createPercentComplete'; break; default: $fcn = 'create' . strtoupper(substr($propName, 0, 1)) . strtolower(substr($propName, 1)); } if (!method_exists($comp, $fcn)) { if ($this->log) { $this->log->log("ERROR 12 INPUT FILE:'{$filename}' iCalcreator: unknown property: '{$propName}' ({$fcn})", PEAR_LOG_INFO); } continue; } $output = str_replace("{$calnl} ", '', rtrim($comp->{$fcn}())); $output = str_replace($propName . ';', '', $output); $output = str_replace($propName . ':', '', $output); $worksheet->writeString($row, $proporder[$propName], fixiCalString($output)); } } // end foreach( $proporder if (isset($compinfo['props']['X-PROP'])) { while ($xprop = $comp->getProperty()) { $output = str_replace("{$calnl} ", '', rtrim($xprop[1])); $worksheet->writeString($row, $proporder[$xprop[0]], fixiCalString($output)); } } if (isset($compinfo['sub'])) { foreach ($compinfo['sub'] as $compinfo2) { $row += 1; $worksheet->writeString($row, $proporder['TYPE'], $compinfo2['type']); $worksheet->writeString($row, $proporder['ORDER'], $compinfo['ordno'] . ':' . $compinfo2['ordno']); $scomp = $comp->getComponent($compinfo2['ordno']); foreach ($proporder as $propName => $col) { if ('TYPE' == $propName || 'ORDER' == $propName) { continue; } if ('X-' == substr($propName, 0, 2)) { continue; } if (!in_array($propName, $allowedProps[strtoupper($compinfo2['type'])])) { // check if component allows property if ($this->log) { $this->log->log("ERROR 13, INPUT FILE:'{$inputdirFile}' iCalcreator: unvalid property for component '" . $compinfo2['type'] . "': '{$propName}'", PEAR_LOG_INFO); } continue; } if (isset($compinfo2['props'][$propName])) { $fcn = 'create' . strtoupper(substr($propName, 0, 1)) . strtolower(substr($propName, 1)); if (!method_exists($scomp, $fcn)) { if ($this->log) { $this->log->log("ERROR 14 INPUT FILE:'{$filename}' iCalcreator: unknown property: '{$propName}' ({$fcn})", PEAR_LOG_INFO); } continue; } $output = str_replace("{$calnl} ", '', rtrim($scomp->{$fcn}())); $output = str_replace($propName . ';', '', $output); $output = str_replace($propName . ':', '', $output); $worksheet->writeString($row, $proporder[$propName], fixiCalString($output)); } // end if( isset( $compinfo2['props'][$propName] } // end foreach( $proporder if (isset($compinfo2['props']['X-PROP'])) { while ($xprop = $scomp->getProperty()) { $output = str_replace("{$calnl} ", '', rtrim($xprop[1])); $output = str_replace('\\n ', chr(10), $output); $worksheet->writeString($row, $proporder[$xprop[0]], fixiCalString($output)); } } // end if( isset( $compinfo2['props']['X-PROP'] } // end foreach( $compinfo['sub'] } // end if( isset( $compinfo['sub'] } // foreach( $compsinfo as if ($this->log) { $timeexec['exit'] = microtime(TRUE); $msg = "'{$filename}'"; $msg .= ' fileOk:' . number_format($timeexec['fileOk'] - $timeexec['start'], 5); $msg .= ' wrkbkOk:' . number_format($timeexec['wrkbkOk'] - $timeexec['fileOk'], 5); $msg .= ' infoOk:' . number_format($timeexec['infoOk'] - $timeexec['wrkbkOk'], 5); $msg .= ' zoneOk:' . number_format($timeexec['zoneOk'] - $timeexec['infoOk'], 5); $msg .= ' compOk:' . number_format($timeexec['exit'] - $timeexec['zoneOk'], 5); $msg .= ' total:' . number_format($timeexec['exit'] - $timeexec['start'], 5) . 'sec'; $msg .= ', ' . ($row + 1) . " rows, {$maxColCount} cols"; $this->log->log($msg, PEAR_LOG_DEBUG); $msg = "'{$filename}' (" . count($compsinfo) . ' components) start:' . date('H:i:s', $timeexec['start']); $msg .= ' total:' . number_format($timeexec['exit'] - $timeexec['start'], 5) . 'sec'; if ($save) { $msg .= " saved as '{$outputdirFile}'"; } else { $msg .= " redirected as '" . $outputFileParts['basename'] . "'"; } $this->log->log($msg, PEAR_LOG_NOTICE); } /** Close and, opt., send the file */ if ($this->log) { $this->log->flush(); } $workbook->close(); return TRUE; }
public function import_ics_data($calendar_id) { // ------------------------------------- // Get some basic info to use later // ------------------------------------- $cbasics = $this->data->calendar_basics(); $cbasics = $cbasics[$calendar_id]; $urls = $cbasics['ics_url']; if ($urls == '') { return FALSE; } $tz_offset = $cbasics['tz_offset'] != '' ? $cbasics['tz_offset'] : '0000'; /* This shouldn't be happening because DST is only something that would need to be applied when generating the users current local time. If an event were at 7pm EST or EDT, it would still be at 7pm either way. I hate DST. if ($tz_offset != '0000' AND ee()->config->item('daylight_savings') == 'y') { $tz_offset += 100; } */ $channel_id = $this->data->channel_is_events_channel(); $author_id = $cbasics['author_id']; // ------------------------------------- // Prepare the URLs // ------------------------------------- if (!is_array($urls)) { $urls = explode("\n", $urls); } foreach ($urls as $k => $url) { $urls[$k] = trim($url); } // ------------------------------------- // Load iCalCreator // ------------------------------------- if (!class_exists('vcalendar')) { require_once CALENDAR_PATH_ASSETS . 'icalcreator/iCalcreator.class.php'; } // ------------------------------------- // Load Calendar_datetime // ------------------------------------- if (!class_exists('Calendar_datetime')) { require_once CALENDAR_PATH . 'calendar.datetime' . EXT; } $CDT = new Calendar_datetime(); $CDT_end = new Calendar_datetime(); // ------------------------------------- // Load Publish // ------------------------------------- if (APP_VER < 2.0) { //need to set DSP if not present if (!isset($GLOBALS['DSP']) or !is_object($GLOBALS['DSP'])) { if (!class_exists('Display')) { require_once PATH_CP . 'cp.display' . EXT; } $GLOBALS['DSP'] = new Display(); } if (!class_exists('Publish')) { require_once PATH_CP . 'cp.publish' . EXT; } $PB = new Publish(); $PB->assign_cat_parent = ee()->config->item('auto_assign_cat_parents') == 'n' ? FALSE : TRUE; } else { ee()->load->library('api'); ee()->api->instantiate(array('channel_entries', 'channel_categories', 'channel_fields')); ee()->api_channel_entries->assign_cat_parent = ee()->config->item('auto_assign_cat_parents') == 'n' ? FALSE : TRUE; } // ------------------------------------- // Tell our extensions that we're running the icalendar import // ------------------------------------- $this->cache['ical'] = TRUE; // ------------------------------------- // Get already-imported events // ------------------------------------- $imported = $this->data->get_imported_events($calendar_id); // ------------------------------------- // Don't let EXT drop us early // ------------------------------------- ee()->extensions->in_progress = ''; // ------------------------------------- // Cycle through the URLs // ------------------------------------- foreach ($urls as $url) { $ICAL = new vcalendar(); $ICAL->parse($this->fetch_url($url)); // ------------------------------------- // Iterate among the events // ------------------------------------- while ($event = $ICAL->getComponent('vevent')) { // ------------------------------------- // Times // ------------------------------------- $hour = isset($event->dtstart['value']['hour']) ? $event->dtstart['value']['hour'] : 00; $minute = isset($event->dtstart['value']['min']) ? $event->dtstart['value']['min'] : 00; $end_hour = isset($event->dtend['value']['hour']) ? $event->dtend['value']['hour'] : $hour; $end_minute = isset($event->dtend['value']['min']) ? $event->dtend['value']['min'] : $minute; // ------------------------------------- // Last-modified date // ------------------------------------- if (isset($event->lastmodified['value'])) { $lm_date = $event->lastmodified['value']['year'] . $event->lastmodified['value']['month'] . $event->lastmodified['value']['day'] . $event->lastmodified['value']['hour'] . $event->lastmodified['value']['min']; } elseif (isset($event->dtstamp['value'])) { $lm_date = $event->dtstamp['value']['year'] . $event->dtstamp['value']['month'] . $event->dtstamp['value']['day'] . $event->dtstamp['value']['hour'] . $event->dtstamp['value']['min']; } else { $lm_date = $event->created['value']['year'] . $event->created['value']['month'] . $event->created['value']['day'] . $event->created['value']['hour'] . $event->created['value']['min']; } // ------------------------------------- // Does this event already exist? Do we need to update? // ------------------------------------- if (isset($imported[$event->uid['value']])) { // ------------------------------------- // Has the event been updated? No reason // to do any work if it's the same old stuff. // ------------------------------------- if ($lm_date == $imported[$event->uid['value']]['last_mod']) { continue; } elseif ($lm_date == $imported[$event->uid['value']]['last_mod']) { continue; } $entry_id = $imported[$event->uid['value']]['entry_id']; } else { $entry_id = ''; } // ------------------------------------- // Adjust CDT // ------------------------------------- $CDT->change_datetime($event->dtstart['value']['year'], $event->dtstart['value']['month'], $event->dtstart['value']['day'], $hour, $minute); if (isset($event->dtend['value'])) { $CDT_end->change_datetime($event->dtend['value']['year'], $event->dtend['value']['month'], $event->dtend['value']['day'], $end_hour, $end_minute); } else { $CDT_end->change_ymd($CDT->ymd); $CDT_end->change_time($end_hour, $end_minute); } // ------------------------------------- // Adjust to the correct timezone for thie calendar // ------------------------------------- if (!isset($event->dtstart['params']['TZID']) or $event->dtstart['params']['TZID'] == '') { if (isset($event->dtstart['value']['hour'])) { $CDT->add_time($tz_offset); $CDT_end->add_time($tz_offset); } else { $CDT_end->add_day(-1); } } // ------------------------------------- // Variableification // ------------------------------------- $title = isset($event->summary['value']) ? $event->summary['value'] : lang('no_title'); $summary = (isset($event->description) and is_array($event->description) and isset($event->description[0]['value'])) ? $event->description[0]['value'] : ''; $location = isset($event->location['value']) ? $event->location['value'] : ''; $rules = $this->ical_rule_to_calendar_rule($event->rrule); $exceptions = array('date' => array()); if (mb_strlen($title) > 100) { $title = substr($title, 0, 100); } if (is_array($event->exdate) and !empty($event->exdate)) { $exceptions = $this->ical_exdate_to_calendar_exception($event->exdate); } $recurs = (is_array($event->rrule) and !empty($event->rrule)) ? 'y' : 'n'; // ------------------------------------- // Fix some linebreak problems // ------------------------------------- $summary = str_replace(array("\r", "\n"), '', $summary); $summary = str_replace('\\n', "\n", $summary); // ------------------------------------- // Set up $_POST // ------------------------------------- $_POST = $post_data = array('site_id' => $this->data->get_site_id(), 'author_id' => $author_id, 'entry_id' => $entry_id, 'weblog_id' => $channel_id, 'channel_id' => $channel_id, 'status' => 'open', 'entry_date' => date('Y-m-d H:i a', ee()->localize->now - 3600 * 24 * 2), 'title' => $title, 'calendar_id' => $calendar_id, 'field_id_' . $this->data->get_field_id(CALENDAR_EVENTS_FIELD_PREFIX . 'summary') => $summary, 'field_id_' . $this->data->get_field_id(CALENDAR_EVENTS_FIELD_PREFIX . 'location') => $location, 'rule_id' => array(), 'start_date' => array($CDT->ymd), 'start_time' => array($CDT->hour . $CDT->minute), 'end_date' => array($CDT_end->ymd), 'end_time' => array($CDT_end->hour . $CDT_end->minute), 'all_day' => !isset($event->dtstart['value']['hour']) ? 'y' : 'n', 'rule_type' => $rules['rule_type'], 'repeat_years' => $rules['repeat_years'], 'repeat_months' => $rules['repeat_months'], 'repeat_weeks' => $rules['repeat_weeks'], 'repeat_days' => $rules['repeat_days'], 'days_of_week' => $rules['days_of_week'], 'relative_dow' => $rules['relative_dow'], 'days_of_month' => $rules['days_of_month'], 'months_of_year' => $rules['months_of_year'], 'end_by' => $rules['end_by'], 'end_after' => $rules['end_after'], 'occurrences' => $exceptions, 'expiration_date' => '', 'comment_expiration_date' => '', 'allow_comments' => 'n'); // ------------------------------------- // Let Publish do its things // ------------------------------------- if (APP_VER < 2.0) { $PB->submit_new_entry(FALSE); //<- LOOK HOW EASY IT USED TO BE >:| } else { //EE 1.x doesn't have this field $opt_field = 'field_id_' . $this->data->get_field_id(CALENDAR_EVENTS_FIELD_PREFIX . 'dates_and_options'); $_POST[$opt_field] = $calendar_id; $post_data[$opt_field] = $calendar_id; //this worked pre EE 2.1.3, then stopped working? *sigh* //now we have to do all of this mess manually for field //settings before inserting new entries via the API //ee()->api_channel_fields->fetch_custom_channel_fields(); //-------------------------------------------- // Check for custom field group //-------------------------------------------- $fg_query = ee()->db->query("SELECT field_group\n\t\t\t\t\t\t FROM\texp_channels\n\t\t\t\t\t\t WHERE\tchannel_id = '" . ee()->db->escape_str($channel_id) . "'"); if ($fg_query->num_rows() > 0) { $field_group = $fg_query->row('field_group'); ee()->lang->loadfile('channel'); ee()->lang->loadfile('content'); ee()->load->model('field_model'); ee()->load->model('channel_model'); // Rudimentary handling of custom fields $field_query = ee()->channel_model->get_channel_fields($field_group); $dst_enabled = ee()->session->userdata('daylight_savings'); foreach ($field_query->result_array() as $row) { $field_data = ''; $field_dt = ''; $field_fmt = $row['field_fmt']; // Settings that need to be prepped $settings = array('field_instructions' => trim($row['field_instructions']), 'field_text_direction' => $row['field_text_direction'] == 'rtl' ? 'rtl' : 'ltr', 'field_fmt' => $field_fmt, 'field_dt' => $field_dt, 'field_data' => $field_data, 'field_name' => 'field_id_' . $row['field_id'], 'dst_enabled' => $dst_enabled); $ft_settings = array(); if (isset($row['field_settings']) and strlen($row['field_settings'])) { $ft_settings = unserialize(base64_decode($row['field_settings'])); } $settings = array_merge($row, $settings, $ft_settings); ee()->api_channel_fields->set_settings($row['field_id'], $settings); } } //now we can do the new entry ee()->api_channel_entries->submit_new_entry($channel_id, $post_data); } // ------------------------------------- // Update the imports table // ------------------------------------- $data = array('calendar_id' => $calendar_id, 'event_id' => $this->cache['ical_event_id'], 'entry_id' => $this->cache['ical_entry_id'], 'uid' => $event->uid['value'], 'last_mod' => $lm_date); if ($entry_id != '') { $data['import_id'] = $imported[$event->uid['value']]['import_id']; $this->data->update_imported_event($data); } else { //$data['import_id'] = '0'; $this->data->add_imported_event($data); } } } $this->data->update_ics_updated($calendar_id); ee()->extensions->end_script = FALSE; ee()->extensions->in_progress = APP_VER < 2.0 ? 'submit_new_entry_end' : 'entry_submission_end'; return TRUE; }
/** * Gets and parses an iCalendar feed into an array of `Ai1ec_Event' objects * * @param object $feed Row from the ai1ec_event_feeds table * * @return int Number of events imported */ public function parse_ics_feed(&$feed, $content = false) { global $ai1ec_events_helper; $count = 0; $comment_status = 'open'; if (isset($feed->comments_enabled) && $feed->comments_enabled < 1) { $comment_status = 'closed'; } $do_show_map = 0; if (isset($feed->map_display_enabled) && $feed->map_display_enabled > 0) { $do_show_map = 1; } // set unique id, required if any component UID is missing $config = array('unique_id' => 'ai1ec'); // create new instance $v = new vcalendar(array('unique_id' => $feed->feed_url, 'url' => $feed->feed_url)); // actual parse of the feed if ($v->parse($content)) { $count = $this->add_vcalendar_events_to_db($v, $feed, $comment_status, $do_show_map); } return $count; }
/** * @param string $uri * @param array $start * @param array $end * @param string $subject * @param bool $allday * @param string $description * @param string $location * @param null $color * @param string $timezone * @param bool $notification * @param null $notification_type * @param null $notification_value * @throws Sabre_DAV_Exception_NotFound * @throws Sabre_DAV_Exception_Conflict */ public function updateItem($uri, $start, $end, $subject = "", $allday = false, $description = "", $location = "", $color = null, $timezone = "", $notification = true, $notification_type = null, $notification_value = null) { $a = get_app(); $usr_id = IntVal($this->calendarDb->uid); $old = q("SELECT * FROM %s%sjqcalendar WHERE `uid` = %d AND `namespace` = %d AND `namespace_id` = %d AND `ical_uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $usr_id, $this->getNamespace(), $this->namespace_id, dbesc($uri)); if (count($old) == 0) { throw new Sabre_DAV_Exception_NotFound("Not Found 1"); } $old_obj = new DBClass_friendica_jqcalendar($old[0]); $calendarBackend = new Sabre_CalDAV_Backend_Std(); $obj = $calendarBackend->getCalendarObject($this->getNamespace() . "-" . $this->namespace_id, $old_obj->ical_uri); if (!$obj) { throw new Sabre_DAV_Exception_NotFound("Not Found 2"); } $v = new vcalendar(); $v->setConfig('unique_id', $a->get_hostname()); $v->setMethod('PUBLISH'); $v->setProperty("x-wr-calname", "AnimexxCal"); $v->setProperty("X-WR-CALDESC", "Animexx Calendar"); $v->setProperty("X-WR-TIMEZONE", $a->timezone); $obj["calendardata"] = icalendar_sanitize_string($obj["calendardata"]); $v->parse($obj["calendardata"]); /** @var $vevent vevent */ $vevent = $v->getComponent('vevent'); if (trim($vevent->getProperty('uid')) . ".ics" != $old_obj->ical_uri) { throw new Sabre_DAV_Exception_Conflict("URI != URI: " . $old_obj->ical_uri . " vs. " . trim($vevent->getProperty("uid"))); } if ($end["year"] < $start["year"] || $end["year"] == $start["year"] && $end["month"] < $start["month"] || $end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] < $start["day"] || $end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] == $start["day"] && $end["hour"] < $start["hour"] || $end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] == $start["day"] && $end["hour"] == $start["hour"] && $end["minute"] < $start["minute"] || $end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] == $start["day"] && $end["hour"] == $start["hour"] && $end["minute"] == $start["minute"] && $end["second"] < $start["second"]) { $end = $start; if ($end["hour"] < 23) { $end["hour"]++; } } // DTEND muss <= DTSTART if ($start["hour"] == 0 && $start["minute"] == 0 && $end["hour"] == 23 && $end["minute"] == 59) { $allday = true; } if ($allday) { $vevent->setDtstart($start["year"], $start["month"], $start["day"], FALSE, FALSE, FALSE, FALSE, array("VALUE" => "DATE")); $end = mktime(0, 0, 0, $end["month"], $end["day"], $end["year"]) + 3600 * 24; // If a DST change occurs on the current day $end += date("Z", $end - 3600 * 24) - date("Z", $end); $vevent->setDtend(date("Y", $end), date("m", $end), date("d", $end), FALSE, FALSE, FALSE, FALSE, array("VALUE" => "DATE")); } else { $vevent->setDtstart($start["year"], $start["month"], $start["day"], $start["hour"], $start["minute"], $start["second"], FALSE, array("VALUE" => "DATE-TIME")); $vevent->setDtend($end["year"], $end["month"], $end["day"], $end["hour"], $end["minute"], $end["second"], FALSE, array("VALUE" => "DATE-TIME")); } if ($subject != "") { $vevent->setProperty('LOCATION', $location); $vevent->setProperty('summary', $subject); $vevent->setProperty('description', $description); } if (!is_null($color) && $color >= 0) { $vevent->setProperty("X-ANIMEXX-COLOR", $color); } if (!$notification || $notification_type != null) { $vevent->deleteComponent("VALARM"); if ($notification) { $valarm = new valarm(); $valarm->setTrigger($notification_type == "year" ? $notification_value : 0, $notification_type == "month" ? $notification_value : 0, $notification_type == "day" ? $notification_value : 0, $notification_type == "week" ? $notification_value : 0, $notification_type == "hour" ? $notification_value : 0, $notification_type == "minute" ? $notification_value : 0, $notification_type == "minute" ? $notification_value : 0, true, $notification_value > 0); $valarm->setProperty("ACTION", "DISPLAY"); $valarm->setProperty("DESCRIPTION", $subject); $vevent->setComponent($valarm); } } $v->deleteComponent("vevent"); $v->setComponent($vevent, trim($vevent->getProperty("uid"))); $ical = $v->createCalendar(); $calendarBackend->updateCalendarObject($this->getNamespace() . "-" . $this->namespace_id, $old_obj->ical_uri, $ical); }
/** * Tries to import data treating it either as csv or as ics. * * @param string $data * @return int the number of imported objetcs */ private function import_from_string($data) { $ics = $this->_registry->get('import-export.ics'); $id = __('textarea_import', AI1EC_PLUGIN_NAME) . '-' . date('Y-m-d-H:i:s'); $feed = $this->create_feed_instance($id); $comments = isset($_POST['ai1ec_file_upload_comments_enabled']) ? 'open' : 'closed'; $show_map = isset($_POST['ai1ec_file_upload_map_display_enabled']) ? 1 : 0; $ical_cnv = new iCalcnv(); $ical_cnv->setConfig(array('outputobj' => true, 'string_to_parse' => $data)); $v = $ical_cnv->csv2iCal(); $count = $ics->add_vcalendar_events_to_db($v, array('feed' => $feed, 'comment_status' => $comments, 'do_show_map' => $show_map)); if (0 === $count['count']) { // create new instance $v = new vcalendar(); $v->parse($data); $count = $ics->add_vcalendar_events_to_db($v, array('feed' => $feed, 'comment_status' => $comments, 'do_show_map' => $show_map)); } return $count['count']; }
} } /* check if testfile is to be recreated (after 24h) */ $createFileStatus = FALSE; if (TRUE === ($createFileStatus = !is_file($dirFile))) { addLogEntry(3, " '{$dirFile}' file is missing"); } else { $calendar = new vcalendar(); $calendar->setConfig('unique_id', UNIQUE); if (!$calendar->setConfig('directory', CALDIR)) { addLogEntry(1, ' ERROR (1) when setting directory \'' . CALDIR . '\', check directory/file permissions!!'); } elseif (!$calendar->setConfig('filename', TESTFILE)) { addLogEntry(1, " ERROR (2) when setting directory/file '{$dirFile}', check directory/file permissions!!"); } else { $starttime2 = microtime(TRUE); $createFileStatus = !$calendar->parse(); $exec = array(', Parse exec time:', $starttime2, microtime(TRUE)); if (FALSE !== $createFileStatus) { addLogEntry(2, ' FALSE from calendar->parse, i.e. file missing or empty' . $exec[0], $exec[1], $exec[2]); } else { addLogEntry(3, ' TRUE from calendar->parse' . $exec[0], $exec[1], $exec[2]); $starttime2 = microtime(TRUE); $comp = $calendar->getComponent('vevent', 1); $exec = array(', exec time:', $starttime2, microtime(TRUE)); if (FALSE === $comp) { $createFileStatus = TRUE; addLogEntry(2, ' FALSE from calendar->getComponent, no vevent in file' . $exec[0], $exec[1], $exec[2]); } else { addLogEntry(3, ' TRUE from calendar->getComponent' . $exec[0], $exec[1], $exec[2]); $starttime2 = microtime(TRUE); $dtstart = $comp->getProperty('dtstart');
/** * function iCal2csv * * Convert iCal file to csv format and send file to browser (default) or save csv file to disk * Definition iCal : rcf2445, http://localhost/work/kigkonsult.se/downloads/index.php#rfc2445 * Definition csv : http://en.wikipedia.org/wiki/Comma-separated_values * Using iCalcreator: http://localhost/work/kigkonsult.se/downloads/index.php#iCalcreator * ical directory/file read/write error OR iCalcreator parse error will be directed to error_log/log * * @author Kjell-Inge Gustafsson <*****@*****.**> * @since 2.0 - 2009-03-27 * @param string $filename file to convert (incl. opt. directory) * @param array $conf opt, default FALSE(=array('del'=>'"','sep'=>',', 'nl'=>'\n'), delimiter, separator and newline characters * escape sequences will be expanded, '\n' will be used as "\n" etc. * also map iCal property names to user friendly names, ex. 'DTSTART' => 'startdate' * also order output columns, ex. 2 => 'DTSTART' (2=first order column, 3 next etc) * also properties to skip, ex. 'skip' => array( 'CREATED', 'PRIORITY' ); * @param bool $save opt, default FALSE, TRUE=save to disk * @param string $diskfilename opt, filename for file to save or else taken from $filename + 'csv' extension * @param object $log opt, default FALSE (error_log), writes log to file using PEAR LOG or eClog class * @return bool returns FALSE when error */ function iCal2csv($filename, $conf = FALSE, $save = FALSE, $diskfilename = FALSE, $log = FALSE) { if ($log) { $timeexec = array('start' => microtime(TRUE)); } $iCal2csv_VERSION = 'iCal2csv 2.0'; if (!function_exists('fileCheckRead')) { require_once 'fileCheck.php'; } if (!class_exists('vcalendar', FALSE)) { require_once 'iCalcreator.class.php'; } if ($log) { $log->log("{$iCal2csv_VERSION} input={$filename}, conf=" . var_export($conf, TRUE) . ", save={$save}, diskfilename={$diskfilename}", 7); } $remoteInput = 'http://' == strtolower(substr($filename, 0, 7)) || 'webcal://' == strtolower(substr($filename, 0, 9)) ? TRUE : FALSE; // field DELimiter && field SEParator && NewLine character(-s) etc. if (!$conf) { $conf = array(); } if (!isset($conf['del'])) { $conf['del'] = '"'; } if (!isset($conf['sep'])) { $conf['sep'] = ','; } if (!isset($conf['nl'])) { $conf['nl'] = "\n"; } foreach ($conf as $key => $value) { if ('skip' == $key) { foreach ($value as $six => $skipp) { $conf['skip'][$six] = strtoupper($skipp); } } elseif ('2' <= $key && '99' > $key) { $conf[$key] = strtoupper($value); if ($log) { $log->log("{$iCal2csv_VERSION} column {$key} contains " . strtoupper($value), 7); } } elseif (in_array($key, array('del', 'sep', 'nl'))) { $conf[$key] = "{$value}"; } else { $conf[strtoupper($key)] = $value; if ($log) { $log->log("{$iCal2csv_VERSION} " . strtoupper($key) . " mapped to {$value}", 7); } } } /* create path and filename */ if ($remoteInput) { $inputFileParts = parse_url($filename); $inputFileParts = array_merge($inputFileParts, pathinfo($inputFileParts['path'])); if (!$diskfilename) { $diskfilename = $inputFileParts['filename'] . '.csv'; } } else { if (FALSE === ($filename = fileCheckRead($filename, $log))) { if ($log) { $log->log("{$iCal2csv_VERSION} (" . number_format(microtime(TRUE) - $timeexec['start'], 5) . ')'); $log->flush(); } return FALSE; } $inputFileParts = pathinfo($filename); if (!$diskfilename) { $diskfilename = $inputFileParts['dirname'] . DIRECTORY_SEPARATOR . $inputFileParts['filename'] . '.csv'; } } $outputFileParts = pathinfo($diskfilename); if ($save) { if (FALSE === ($diskfilename = fileCheckWrite($outputFileParts['dirname'] . DIRECTORY_SEPARATOR . $outputFileParts['basename'], $log))) { if ($log) { $log->log("{$iCal2csv_VERSION} (" . number_format(microtime(TRUE) - $timeexec['start'], 5) . ')'); $log->flush(); } return FALSE; } } if ($log) { $msg = $iCal2csv_VERSION . ' INPUT FILE:"' . $inputFileParts['dirname'] . DIRECTORY_SEPARATOR . $inputFileParts['basename'] . '"'; if ($save) { $msg .= ' OUTPUT FILE: "' . $outputFileParts['dirname'] . DIRECTORY_SEPARATOR . $outputFileParts['basename'] . '"'; } $log->log($msg, 7); } /* iCalcreator check, read and parse input iCal file */ $calendar = new vcalendar(); $calnl = $calendar->getConfig('nl'); if ($remoteInput) { if (FALSE === $calendar->setConfig('url', $filename)) { $msg = $iCal2csv_VERSION . ' ERROR 3 INPUT FILE:"' . $filename . '" iCalcreator: invalid url'; if ($log) { $log->log($msg, 3); $log->flush(); } else { error_log($msg); } return FALSE; } } else { if (FALSE === $calendar->setConfig('directory', $inputFileParts['dirname'])) { $msg = $iCal2csv_VERSION . ' ERROR 4 INPUT FILE:"' . $filename . '" iCalcreator: invalid directory: "' . $inputFileParts['dirname'] . '"'; if ($log) { $log->log($msg, 3); $log->flush(); } else { error_log($msg); } return FALSE; } if (FALSE === $calendar->setConfig('filename', $inputFileParts['basename'])) { $msg = $iCal2csv_VERSION . ' ERROR 5 INPUT FILE:"' . $filename . '" iCalcreator: invalid filename: "' . $inputFileParts['basename'] . '"'; if ($log) { $log->log($msg, 3); $log->flush(); } else { error_log($msg); } return FALSE; } } if (FALSE === $calendar->parse()) { $msg = $iCal2csv_VERSION . ' ERROR 6 INPUT FILE:"' . $filename . '" iCalcreator parse error'; if ($log) { $log->log($msg, 3); $log->flush(); } else { error_log($msg); } return FALSE; } if ($log) { $timeexec['fileOk'] = microtime(TRUE); } if (!function_exists('iCaldate2timestamp')) { function iCaldate2timestamp($d) { if (6 > count($d)) { return mktime(0, 0, 0, $d['month'], $d['day'], $d['year']); } else { return mktime($d['hour'], $d['min'], $d['sec'], $d['month'], $d['day'], $d['year']); } } } if (!function_exists('fixiCalString')) { function fixiCalString($s) { $s = str_replace('\\,', ',', $s); $s = str_replace('\\;', ';', $s); $s = str_replace('\\n ', chr(10), $s); $s = str_replace('\\\\', '\\', $s); return $s; } } /* create output array */ $rows = array(); /* info rows */ $rows[] = array('kigkonsult.se', ICALCREATOR_VERSION, $iCal2csv_VERSION, date('Y-m-d H:i:s')); $filename = $remoteInput ? $filename : $inputFileParts['basename']; $rows[] = array('iCal input', $filename, 'csv output', $outputFileParts['basename']); if ($prop = $calendar->getProperty('CALSCALE')) { $rows[] = array('CALSCALE', $prop); } if ($prop = $calendar->getProperty('METHOD')) { $rows[] = array('METHOD', $prop); } while ($xprop = $calendar->getProperty()) { $rows[] = array($xprop[0], $xprop[1]); } if ($log) { $timeexec['infoOk'] = microtime(TRUE); } /* fix vtimezone property order list */ $proporder = array(); foreach ($conf as $key => $value) { if ('2' <= $key && '99' > $key) { $proporder[$value] = $key; if ($log) { $log->log("{$iCal2csv_VERSION} {$value} in column {$key}", 7); } } } $proporder['TYPE'] = 0; $proporder['ORDER'] = 1; $props = array('TZID', 'LAST-MODIFIED', 'TZURL', 'DTSTART', 'TZOFFSETTO', 'TZOFFSETFROM', 'TZOFFSETTFROM', 'COMMENT', 'RRULE', 'RDATE', 'TZNAME'); $pix = 2; foreach ($props as $prop) { if (isset($proporder[$prop])) { continue; } if (isset($conf['skip']) && in_array($prop, $conf['skip'])) { if ($log) { $log->log("{$iCal2csv_VERSION} {$prop} removed from output", 7); } continue; } while (in_array($pix, $proporder)) { $pix++; } $proporder[$prop] = $pix++; } /* remove unused properties from and add x-props to property order list */ $maxpropix = 11; if ($maxpropix != count($proporder) - 1) { $maxpropix = count($proporder) - 1; } $compsinfo = $calendar->getConfig('compsinfo'); $potmp = array(); $potmp[0] = 'TYPE'; $potmp[1] = 'ORDER'; foreach ($compsinfo as $cix => $compinfo) { if ('vtimezone' != $compinfo['type']) { continue; } $comp = $calendar->getComponent($compinfo['ordno']); foreach ($compinfo['props'] as $propName => $propcnt) { if (!in_array($propName, $potmp) && isset($proporder[$propName])) { $potmp[$proporder[$propName]] = $propName; } elseif ('X-PROP' == $propName) { while ($xprop = $comp->getProperty()) { if (!in_array($xprop[0], $potmp)) { $maxpropix += 1; $potmp[$maxpropix] = $xprop[0]; } // end if } // end while xprop } // end X-PROP } // end $compinfo['props'] if (isset($compinfo['sub'])) { foreach ($compinfo['sub'] as $compinfo2) { foreach ($compinfo2['props'] as $propName => $propcnt) { if (!in_array($propName, $potmp) && isset($proporder[$propName])) { $potmp[$proporder[$propName]] = $propName; } elseif ('X-PROP' == $propName) { $scomp = $comp->getComponent($compinfo2['ordno']); while ($xprop = $scomp->getProperty()) { if (!in_array($xprop[0], $potmp)) { $maxpropix += 1; $potmp[$maxpropix] = $xprop[0]; } // end if } // end while xprop } // end X-PROP } // end $compinfo['sub']['props'] } // end foreach( $compinfo['sub'] } // end if( isset( $compinfo['sub'] } // end foreach compinfo - vtimezone ksort($potmp, SORT_NUMERIC); if ('2.6' == substr(ICALCREATOR_VERSION, -3)) { foreach ($potmp as $k => $v) { // iCalcreator 2.6 bug fix if ('TZOFFSETTFROM' == $v) { $potmp[$k] = 'TZOFFSETFROM'; } } } $proporder = array_flip(array_values($potmp)); if ($log) { $log->log("{$iCal2csv_VERSION} zone proporder=" . implode(',', array_flip($proporder)), 7); } /* create vtimezone info */ $row = count($rows) - 1; if (2 < count($proporder)) { $row += 1; /* create vtimezone header row */ foreach ($proporder as $propName => $col) { if (isset($conf[$propName])) { $rows[$row][$col] = $conf[$propName]; // check map of userfriendly name to iCal property name if ($log) { $log->log("{$iCal2csv_VERSION} header row, col={$col}: {$propName}, replaced by " . $conf[$propName], 7); } } else { $rows[$row][$col] = $propName; } } $allowedProps = array('VTIMEZONE' => array('TZID', 'LAST-MODIFIED', 'TZURL'), 'STANDARD' => array('DTSTART', 'TZOFFSETTO', 'TZOFFSETFROM', 'COMMENT', 'RDATE', 'RRULE', 'TZNAME'), 'DAYLIGHT' => array('DTSTART', 'TZOFFSETTO', 'TZOFFSETFROM', 'COMMENT', 'RDATE', 'RRULE', 'TZNAME')); /* create vtimezone data rows */ foreach ($compsinfo as $cix => $compinfo) { if ('vtimezone' != $compinfo['type']) { continue; } $row += 1; foreach ($proporder as $propName => $col) { $rows[$row][] = ''; } // set all cells empty $rows[$row][$proporder['TYPE']] = $compinfo['type']; $rows[$row][$proporder['ORDER']] = $compinfo['ordno']; $comp = $calendar->getComponent($compinfo['ordno']); foreach ($proporder as $propName => $col) { if ('TYPE' == $propName || 'ORDER' == $propName) { continue; } if ('X-' == substr($propName, 0, 2)) { continue; } if (!in_array($propName, $allowedProps['VTIMEZONE'])) { // check if component allows property continue; } if (isset($compinfo['props'][$propName])) { if ('LAST-MODIFIED' == $propName) { $fcn = 'createLastModified'; } else { $fcn = 'create' . strtoupper(substr($propName, 0, 1)) . strtolower(substr($propName, 1)); } if (!method_exists($comp, $fcn)) { $msg = $iCal2csv_VERSION . ' ERROR 7 INPUT FILE:"' . $filename . '" iCalcreator: unknown property: "' . $propName . '" (' . $fcn . ')'; if ($log) { $log->log($msg, 3); } else { error_log($msg); } continue; } $output = str_replace("{$calnl} ", '', rtrim($comp->{$fcn}())); $output = str_replace($propName . ';', '', $output); $output = str_replace($propName . ':', '', $output); $rows[$row][$proporder[$propName]] = fixiCalString($output); } } // end foreach( $proporder if (isset($compinfo['props']['X-PROP'])) { while ($xprop = $comp->getProperty()) { $output = str_replace("{$calnl} ", '', rtrim($xprop[1])); $rows[$row][$proporder[$xprop[0]]] = fixiCalString($output); } } if (isset($compinfo['sub'])) { foreach ($compinfo['sub'] as $compinfo2) { $row += 1; foreach ($proporder as $propName => $col) { $rows[$row][] = ''; } // set all cells empty $rows[$row][$proporder['TYPE']] = $compinfo2['type']; $rows[$row][$proporder['ORDER']] = $compinfo['ordno'] . ':' . $compinfo2['ordno']; $scomp = $comp->getComponent($compinfo2['ordno']); foreach ($proporder as $propName => $col) { if ('TYPE' == $propName || 'ORDER' == $propName) { continue; } if ('X-' == substr($propName, 0, 2)) { continue; } if (!in_array($propName, $allowedProps[strtoupper($compinfo2['type'])])) { // check if component allows property continue; } if ('2.6' == substr(ICALCREATOR_VERSION, -3) && 'TZOFFSETFROM' == $propName) { $propName = 'TZOFFSETTFROM'; } // iCalcreator 2.6 bug fix if (isset($compinfo2['props'][$propName])) { if ('2.6' == substr(ICALCREATOR_VERSION, -3) && 'TZOFFSETTFROM' == $propName) { $propName = 'TZOFFSETFROM'; } // iCalcreator 2.6 bug fix $fcn = 'create' . strtoupper(substr($propName, 0, 1)) . strtolower(substr($propName, 1)); if (!method_exists($scomp, $fcn)) { $msg = $iCal2csv_VERSION . ' ERROR 8 INPUT FILE:"' . $filename . '" iCalcreator: unknown property: "' . $propName . '" (' . $fcn . ')'; if ($log) { $log->log($msg, 3); } else { error_log($msg); } continue; } $output = str_replace("{$calnl} ", '', rtrim($scomp->{$fcn}())); $output = str_replace($propName . ';', '', $output); $output = str_replace($propName . ':', '', $output); $rows[$row][$proporder[$propName]] = fixiCalString($output); } } // end foreach( $proporder if (isset($compinfo2['props']['X-PROP'])) { while ($xprop = $scomp->getProperty()) { $output = str_replace("{$calnl} ", '', rtrim($xprop[1])); $rows[$row][$proporder[$xprop[0]]] = fixiCalString($output); } } } // end foreach( $compinfo['sub'] } // end if( isset( $compinfo['sub']['props'] )) } // end foreach } // end vtimezone if ($log) { $timeexec['zoneOk'] = microtime(TRUE); } $maxColCount = count($proporder); /* fix property order list */ $proporder = array(); foreach ($conf as $key => $value) { if ('2' <= $key && '99' > $key) { $proporder[$value] = $key; if ($log) { $log->log("{$iCal2csv_VERSION} {$value} in column {$key}", 7); } } } $proporder['TYPE'] = 0; $proporder['ORDER'] = 1; $props = array('UID', 'DTSTAMP', 'SUMMARY', 'DTSTART', 'DURATION', 'DTEND', 'DUE', 'RRULE', 'RDATE', 'EXRULE', 'EXDATE', 'DESCRIPTION', 'CATEGORIES', 'ORGANIZER', 'LOCATION', 'RESOURCES', 'CONTACT', 'URL', 'COMMENT', 'PRIORITY', 'ATTENDEE', 'CLASS', 'TRANSP', 'SEQUENCE', 'STATUS', 'COMPLETED', 'CREATED', 'LAST-MODIFIED', 'ACTION', 'TRIGGER', 'REPEAT', 'ATTACH', 'FREEBUSY', 'RELATED-TO', 'REQUEST-STATUS', 'GEO', 'PERCENT-COMPLETE', 'RECURRENCE-ID'); $pix = 2; foreach ($props as $prop) { if (isset($proporder[$prop])) { continue; } if (isset($conf['skip']) && in_array($prop, $conf['skip'])) { if ($log) { $log->log("{$iCal2csv_VERSION} {$prop} removed from output", 7); } continue; } while (in_array($pix, $proporder)) { $pix++; } $proporder[$prop] = $pix++; } /* remove unused properties from and add x-props to property order list */ if ($maxpropix < count($proporder) - 1) { $maxpropix = count($proporder) - 1; } $potmp = array(); $potmp[0] = 'TYPE'; $potmp[1] = 'ORDER'; // $potmp[2] = 'UID'; foreach ($compsinfo as $cix => $compinfo) { if ('vtimezone' == $compinfo['type']) { continue; } foreach ($compinfo['props'] as $propName => $propcnt) { if (!in_array($propName, $potmp) && isset($proporder[$propName])) { $potmp[$proporder[$propName]] = $propName; } elseif ('X-PROP' == $propName) { $comp = $calendar->getComponent($compinfo['ordno']); while ($xprop = $comp->getProperty()) { if (!in_array($xprop[0], $potmp)) { $maxpropix += 1; $potmp[$maxpropix] = $xprop[0]; } // end if } // while( $xprop } // end elseif( 'X-PROP' } // end foreach( $compinfo['props'] if (isset($compinfo['sub'])) { foreach ($compinfo['sub'] as $compinfo2) { foreach ($compinfo2['props'] as $propName => $propcnt) { if (!in_array($propName, $potmp) && isset($proporder[$propName])) { $potmp[$proporder[$propName]] = $propName; } elseif ('X-PROP' == $propName) { $scomp = $comp->getComponent($compinfo2['ordno']); while ($xprop = $scomp->getProperty()) { if (!in_array($xprop[0], $potmp)) { $maxpropix += 1; $potmp[$maxpropix] = $xprop[0]; } // end if } // end while xprop } // end X-PROP } // end $compinfo['sub']['props'] } // end foreach( $compinfo['sub'] } // end if( isset( $compinfo['sub'] } ksort($potmp, SORT_NUMERIC); $proporder = array_flip(array_values($potmp)); if ($log) { $log->log("{$iCal2csv_VERSION} comp proporder=" . implode(',', array_flip($proporder)), 7); } if ($maxColCount < count($proporder)) { $maxColCount = count($proporder); } /* create header row */ $row += 1; foreach ($proporder as $propName => $col) { if (isset($conf[$propName])) { $rows[$row][$col] = $conf[$propName]; // check map of userfriendly name to iCal property name if ($log) { $log->log("{$iCal2csv_VERSION} header row, col={$col}: {$propName}, replaced by " . $conf[$propName], 7); } } else { $rows[$row][$col] = $propName; } } $allowedProps = array('VEVENT' => array('ATTACH', 'ATTENDEE', 'CATEGORIES', 'CLASS', 'COMMENT', 'CONTACT', 'CREATED', 'DESCRIPTION', 'DTEND', 'DTSTAMP', 'DTSTART', 'DURATION', 'EXDATE', 'RXRULE', 'GEO', 'LAST-MODIFIED', 'LOCATION', 'ORGANIZER', 'PRIORITY', 'RDATE', 'RECURRENCE-ID', 'RELATED-TO', 'RESOURCES', 'RRULE', 'REQUEST-STATUS', 'SEQUENCE', 'STATUS', 'SUMMARY', 'TRANSP', 'UID', 'URL'), 'VTODO' => array('ATTACH', 'ATTENDEE', 'CATEGORIES', 'CLASS', 'COMMENT', 'COMPLETED', 'CONTACT', 'CREATED', 'DESCRIPTION', 'DTSTAMP', 'DTSTART', 'DUE', 'DURATION', 'EXATE', 'EXRULE', 'GEO', 'LAST-MODIFIED', 'LOCATION', 'ORGANIZER', 'PERCENT', 'PRIORITY', 'RDATE', 'RECURRENCE-ID', 'RELATED-TO', 'RESOURCES', 'RRULE', 'REQUEST-STATUS', 'SEQUENCE', 'STATUS', 'SUMMARY', 'UID', 'URL'), 'VJOURNAL' => array('ATTACH', 'ATTENDEE', 'CATEGORIES', 'CLASS', 'COMMENT', 'CONTACT', 'CREATED', 'DESCRIPTION', 'DTSTAMP', 'DTSTART', 'EXDATE', 'EXRULE', 'LAST-MODIFIED', 'ORGANIZER', 'RDATE', 'RECURRENCE-ID', 'RELATED-TO', 'RRULE', 'REQUEST-STATUS', 'SEQUENCE', 'STATUS', 'SUMMARY', 'UID', 'URL'), 'VFREEBUSY' => array('ATTENDEE', 'COMMENT', 'CONTACT', 'DTEND', 'DTSTAMP', 'DTSTART', 'DURATION', 'FREEBUSY', 'ORGANIZER', 'UID', 'URL'), 'VALARM' => array('ACTION', 'ATTACH', 'ATTENDEE', 'DESCRIPTION', 'DURATION', 'REPEAT', 'TRANSP', 'TRIGGER')); /* create data rows */ foreach ($compsinfo as $cix => $compinfo) { if ('vtimezone' == $compinfo['type']) { continue; } $row += 1; foreach ($proporder as $propName => $col) { $rows[$row][] = ''; } // set all cells empty $rows[$row][$proporder['TYPE']] = $compinfo['type']; $rows[$row][$proporder['ORDER']] = $compinfo['ordno']; // $rows[$row][$proporder['UID']] = $compinfo['uid']; $comp = $calendar->getComponent($compinfo['ordno']); foreach ($proporder as $propName => $col) { if ('TYPE' == $propName || 'ORDER' == $propName) { continue; } if ('X-' == substr($propName, 0, 2)) { continue; } if (!in_array($propName, $allowedProps[strtoupper($compinfo['type'])])) { // check if component allows property continue; } if (isset($compinfo['props'][$propName])) { switch ($propName) { case 'LAST-MODIFIED': $fcn = 'createLastModified'; break; case 'RECURRENCE-ID': $fcn = 'createRecurrenceid'; break; case 'RELATED-TO': $fcn = 'createRelatedTo'; break; case 'REQUEST-STATUS': $fcn = 'createRequestStatus'; break; case 'PERCENT-COMPLETE': $fcn = 'createPercentComplete'; break; default: $fcn = 'create' . strtoupper(substr($propName, 0, 1)) . strtolower(substr($propName, 1)); } if (!method_exists($comp, $fcn)) { $msg = $iCal2csv_VERSION . ' ERROR 9 INPUT FILE:"' . $filename . '" iCalcreator: unknown property: "' . $propName . '" (' . $fcn . ')'; if ($log) { $log->log($msg, 3); } else { error_log($msg); } continue; } $output = str_replace("{$calnl} ", '', rtrim($comp->{$fcn}())); $output = str_replace($propName . ';', '', $output); $output = str_replace($propName . ':', '', $output); $rows[$row][$proporder[$propName]] = fixiCalString($output); } } // end foreach( $proporder if (isset($compinfo['props']['X-PROP'])) { while ($xprop = $comp->getProperty()) { $output = str_replace("{$calnl} ", '', rtrim($xprop[1])); $rows[$row][$proporder[$xprop[0]]] = fixiCalString($output); } } if (isset($compinfo['sub'])) { foreach ($compinfo['sub'] as $compinfo2) { $row += 1; foreach ($proporder as $propName => $col) { $rows[$row][] = ''; } // set all cells empty $rows[$row][$proporder['TYPE']] = $compinfo2['type']; $rows[$row][$proporder['ORDER']] = $compinfo['ordno'] . ':' . $compinfo2['ordno']; $scomp = $comp->getComponent($compinfo2['ordno']); foreach ($proporder as $propName => $col) { if ('TYPE' == $propName || 'ORDER' == $propName) { continue; } if ('X-' == substr($propName, 0, 2)) { continue; } if (!in_array($propName, $allowedProps[strtoupper($compinfo2['type'])])) { // check if component allows property continue; } if (isset($compinfo2['props'][$propName])) { $fcn = 'create' . strtoupper(substr($propName, 0, 1)) . strtolower(substr($propName, 1)); if (!method_exists($scomp, $fcn)) { $msg = $iCal2csv_VERSION . ' ERROR 10 INPUT FILE:"' . $filename . '" iCalcreator: unknown property: "' . $propName . '" (' . $fcn . ')'; if ($log) { $log->log($msg, 3); } else { error_log($msg); } continue; } $output = str_replace("{$calnl} ", '', rtrim($scomp->{$fcn}())); $output = str_replace($propName . ';', '', $output); $output = str_replace($propName . ':', '', $output); $rows[$row][$proporder[$propName]] = fixiCalString($output); } } // end foreach( $proporder if (isset($compinfo2['props']['X-PROP'])) { while ($xprop = $scomp->getProperty()) { $output = str_replace("{$calnl} ", '', rtrim($xprop[1])); $rows[$row][$proporder[$xprop[0]]] = fixiCalString($output); } } } // if( isset( $compinfo2['props']['X-PROP'] } // end if( isset( $compinfo['sub'] } // foreach( $compsinfo as if ($log) { $timeexec['compOk'] = microtime(TRUE); } /* fix csv format */ // fields that contain commas, double-quotes, or line-breaks must be quoted, // a quote within a field must be escaped with an additional quote immediately preceding the literal quote, // space before and after delimiter commas may be trimmed (which is prohibited by RFC 4180) // a line break within an element must be preserved. // Fields may ALWAYS be enclosed within double-quote characters, whether necessary or not. foreach ($rows as $row => $line) { for ($col = 0; $col < $maxColCount; $col++) { if (!isset($line[$col]) || empty($line[$col])) { $rows[$row][$col] = $conf['del'] . $conf['del']; continue; } if (ctype_digit($line[$col])) { continue; } $cell = str_replace($conf['del'], $conf['del'] . $conf['del'], $line[$col]); $rows[$row][$col] = $conf['del'] . $cell . $conf['del']; } $rows[$row] = implode($conf['sep'], $rows[$row]); } $output = implode($conf['nl'], $rows) . $conf['nl']; if ($log) { $timeexec['exit'] = microtime(TRUE); $msg = "{$iCal2csv_VERSION} '{$filename}'"; $msg .= ' fileOk:' . number_format($timeexec['fileOk'] - $timeexec['start'], 5); $msg .= ' infoOk:' . number_format($timeexec['infoOk'] - $timeexec['fileOk'], 5); $msg .= ' zoneOk:' . number_format($timeexec['zoneOk'] - $timeexec['infoOk'], 5); $msg .= ' compOk:' . number_format($timeexec['compOk'] - $timeexec['zoneOk'], 5); $msg .= ' csvOk:' . number_format($timeexec['exit'] - $timeexec['compOk'], 5); $msg .= ' total:' . number_format($timeexec['exit'] - $timeexec['start'], 5) . 'sec'; $log->log($msg, 7); $msg = "{$iCal2csv_VERSION} '{$filename}' (" . count($compsinfo) . ' components) start:' . date('H:i:s', $timeexec['start']); $msg .= ' total:' . number_format($timeexec['exit'] - $timeexec['start'], 5) . 'sec'; if ($save) { $msg .= " -> '{$diskfilename}'"; } $msg .= ', size=' . strlen($output); $msg .= ', ' . count($rows) . " rows, {$maxColCount} cols"; $log->log($msg, 6); } /* save or send the file */ if ($save) { if (FALSE !== file_put_contents($diskfilename, $output)) { if ($log) { $log->flush(); } return TRUE; } else { $msg = $iCal2csv_VERSION . ' ERROR 11 INPUT FILE:"' . $filename . '" Invalid write to output file : "' . $diskfilename . '"'; if ($log) { $log->log($msg, 3); $log->flush(); } else { error_log($msg); } return FALSE; } } else { if ($log) { $log->flush(); } /** return data, auto gzip */ $filezise = strlen($output); if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { $output = gzencode($output, 9); $filezise = strlen($output); header('Content-Encoding: gzip'); header('Vary: *'); } header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename="' . $outputFileParts['basename'] . '"'); header('Cache-Control: max-age=10'); header('Content-Length: ' . $filezise); echo $output; } exit; }
/** * Import an iCal file into the database * @param array Course info * @return boolean True on success, false otherwise * @deprecated */ function agenda_import_ical($course_info, $file) { require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php'; $charset = api_get_system_encoding(); $filepath = api_get_path(SYS_ARCHIVE_PATH) . $file['name']; if (!@move_uploaded_file($file['tmp_name'], $filepath)) { error_log('Problem moving uploaded file: ' . $file['error'] . ' in ' . __FILE__ . ' line ' . __LINE__); return false; } require_once api_get_path(LIBRARY_PATH) . 'icalcreator/iCalcreator.class.php'; $ical = new vcalendar(); $ical->setConfig('directory', dirname($filepath)); $ical->setConfig('filename', basename($filepath)); $return = $ical->parse(); //we need to recover: summary, description, dtstart, dtend, organizer, attendee, location (=course name), /* $ve = $ical->getComponent(VEVENT); $ttitle = $ve->getProperty('summary'); $title = api_convert_encoding($ttitle,$charset,'UTF-8'); $tdesc = $ve->getProperty('description'); $desc = api_convert_encoding($tdesc,$charset,'UTF-8'); $start_date = $ve->getProperty('dtstart'); $start_date_string = $start_date['year'].'-'.$start_date['month'].'-'.$start_date['day'].' '.$start_date['hour'].':'.$start_date['min'].':'.$start_date['sec']; $ts = $ve->getProperty('dtend'); if ($ts) { $end_date_string = $ts['year'].'-'.$ts['month'].'-'.$ts['day'].' '.$ts['hour'].':'.$ts['min'].':'.$ts['sec']; } else { //Check duration if dtend does not exist $duration = $ve->getProperty('duration'); if ($duration) { $duration = $ve->getProperty('duration'); $duration_string = $duration['year'].'-'.$duration['month'].'-'.$duration['day'].' '.$duration['hour'].':'.$duration['min'].':'.$duration['sec']; $start_date_tms = mktime(intval($start_date['hour']), intval($start_date['min']), intval($start_date['sec']), intval($start_date['month']), intval($start_date['day']), intval($start_date['year'])); //$start_date_tms = mktime(($start_date['hour']), ($start_date['min']), ($start_date['sec']), ($start_date['month']), ($start_date['day']), ($start_date['year'])); //echo date('d-m-Y - h:i:s', $start_date_tms); $end_date_string = mktime(intval($start_date['hour']) +$duration['hour'], intval($start_date['min']) + $duration['min'], intval($start_date['sec']) + $duration['sec'], intval($start_date['month']) + $duration['month'], intval($start_date['day'])+$duration['day'], intval($start_date['year']) + $duration['year']); $end_date_string = date('Y-m-d H:i:s', $end_date_string); //echo date('d-m-Y - h:i:s', $end_date_string); } } //echo $start_date.' - '.$end_date; $organizer = $ve->getProperty('organizer'); $attendee = $ve->getProperty('attendee'); $course_name = $ve->getProperty('location'); //insert the event in our database $id = agenda_add_item($course_info,$title,$desc,$start_date_string,$end_date_string,$_POST['selectedform']); $repeat = $ve->getProperty('rrule'); if(is_array($repeat) && !empty($repeat['FREQ'])) { $trans = array('DAILY'=>'daily','WEEKLY'=>'weekly','MONTHLY'=>'monthlyByDate','YEARLY'=>'yearly'); $freq = $trans[$repeat['FREQ']]; $interval = $repeat['INTERVAL']; if(isset($repeat['UNTIL']) && is_array($repeat['UNTIL'])) { $until = mktime(23,59,59,$repeat['UNTIL']['month'],$repeat['UNTIL']['day'],$repeat['UNTIL']['year']); $res = agenda_add_repeat_item($course_info,$id,$freq,$until,$_POST['selectedform']); } */ $eventcount = 0; $message = array(); $agenda_obj = new Agenda(); while (true) { //we need to recover: summary, description, dtstart, dtend, organizer, attendee, location (=course name) $ve = $ical->getComponent('VEVENT', $eventcount); if (!$ve) { break; } $ttitle = $ve->getProperty('summary'); $title = api_convert_encoding($ttitle, $charset, 'UTF-8'); $tdesc = $ve->getProperty('description'); $desc = api_convert_encoding($tdesc, $charset, 'UTF-8'); $start_date = $ve->getProperty('dtstart', false, true); if (isset($start_date['params']['VALUE'])) { $start_date_value = $start_date['value']; if ($start_date['params']['VALUE'] == 'DATE') { $start_date_string = $start_date_value['year'] . '-' . $start_date_value['month'] . '-' . $start_date_value['day'] . ''; } else { $start_date_string = $start_date_value['year'] . '-' . $start_date_value['month'] . '-' . $start_date_value['day'] . ' ' . $start_date_value['hour'] . ':' . $start_date_value['min'] . ':' . $start_date_value['sec']; } } else { continue; } $ts = $ve->getProperty('dtend'); if ($ts) { $end_date = $ve->getProperty('dtend', false, true); if (isset($end_date['params']['VALUE'])) { $end_date_value = $end_date['value']; if ($end_date['params']['VALUE'] == 'DATE') { $end_date_string = $end_date_value['year'] . '-' . $end_date_value['month'] . '-' . $end_date_value['day'] . ''; } else { $end_date_string = $end_date_value['year'] . '-' . $end_date_value['month'] . '-' . $end_date_value['day'] . ' ' . $end_date_value['hour'] . ':' . $end_date_value['min'] . ':' . $end_date_value['sec']; } } else { //Default behaviour $end_date_string = $ts['year'] . '-' . $ts['month'] . '-' . $ts['day'] . ' ' . $ts['hour'] . ':' . $ts['min'] . ':' . $ts['sec']; } } else { //Check duration if dtend does not exist $duration = $ve->getProperty('duration'); if ($duration) { $duration = $ve->getProperty('duration'); $duration_string = $duration['year'] . '-' . $duration['month'] . '-' . $duration['day'] . ' ' . $duration['hour'] . ':' . $duration['min'] . ':' . $duration['sec']; $start_date_tms = mktime(intval($start_date['hour']), intval($start_date['min']), intval($start_date['sec']), intval($start_date['month']), intval($start_date['day']), intval($start_date['year'])); //$start_date_tms = mktime(($start_date['hour']), ($start_date['min']), ($start_date['sec']), ($start_date['month']), ($start_date['day']), ($start_date['year'])); //echo date('d-m-Y - h:i:s', $start_date_tms); $end_date_string = mktime(intval($start_date['hour']) + $duration['hour'], intval($start_date['min']) + $duration['min'], intval($start_date['sec']) + $duration['sec'], intval($start_date['month']) + $duration['month'], intval($start_date['day']) + $duration['day'], intval($start_date['year']) + $duration['year']); $end_date_string = api_get_utc_datetime($end_date_string); //echo date('d-m-Y - h:i:s', $end_date_string); } } //echo $start_date.' - '.$end_date; $organizer = $ve->getProperty('organizer'); $attendee = $ve->getProperty('attendee'); $course_name = $ve->getProperty('location'); //insert the event in our database $agenda_obj->type = 'course'; $all_day = 'false'; if ($start_date_string == $end_date_string) { $all_day = 'true'; } $date = new DateTime($start_date_string); $date->add(new DateInterval('P1D')); if ($start_date_string == $date->format('Y-m-d h:i:s')) { $all_day = 'true'; } $id = $agenda_obj->add_event($start_date_string, $end_date_string, $all_day, $title, $desc, array('everyone')); $message[] = " {$title} - " . $start_date_string . " - " . $end_date_string; $repeat = $ve->getProperty('rrule'); if (is_array($repeat) && !empty($repeat['FREQ'])) { $trans = array('DAILY' => 'daily', 'WEEKLY' => 'weekly', 'MONTHLY' => 'monthlyByDate', 'YEARLY' => 'yearly'); $freq = $trans[$repeat['FREQ']]; $interval = $repeat['INTERVAL']; if (isset($repeat['UNTIL']) && is_array($repeat['UNTIL'])) { $until = mktime(23, 59, 59, $repeat['UNTIL']['month'], $repeat['UNTIL']['day'], $repeat['UNTIL']['year']); $res = agenda_add_repeat_item($course_info, $id, $freq, $until, $attendee); } //TODO: deal with count if (!empty($repeat['COUNT'])) { $count = $repeat['COUNT']; $res = agenda_add_repeat_item($course_info, $id, $freq, $count, $attendee); } } $eventcount++; } if (!empty($message)) { $message = implode('<br /> ', $message); } return $message; }
#!/usr/bin/php <?php require 'vendor/autoload.php'; require_once 'configs.php'; $scopes = array('https://www.googleapis.com/auth/calendar'); // iCal データ取得 $client = new GuzzleHttp\Client(); $clientRes = $client->get($aipoIcalUrl, ['auth' => [$aipoUser, $aipoPasswd]]); if ($clientRes->getStatusCode() != 200) { echo 'Authorization Required' . "\n"; exit(1); } $icalContents = $clientRes->getBody(); $ical = new vcalendar(); $icalRes = $ical->parse($icalContents); $aipoEvents = array(); if ($icalRes == true) { foreach ($ical->components as $key => $event) { $summary = $event->getProperty('summary'); if (!$summary) { continue; } $dtstamp = $event->getProperty('dtstamp'); $dtstart = $event->getProperty('dtstart', 0, true); $dtend = $event->getProperty('dtend', 0, true); $uid = $event->getProperty('uid'); preg_match('/^([0-9A-Za-z]+)-([0-9]+)\\@(.+?)$/', $uid, $matches); $aipoId = $matches[2]; $start = new Google_Service_Calendar_EventDateTime(); if (!empty($dtstart['params']) && $dtstart['params']['VALUE'] == 'DATE') { $start->setDate($dtstart['value']['year'] . '-' . sprintf('%02d', $dtstart['value']['month']) . '-' . sprintf('%02d', $dtstart['value']['day']));
/** * Parses an iCalendar resource */ function parse_icalendar($data) { $vcalendar = new vcalendar($this->config); $vcalendar->parse($data); return $vcalendar; }
/** * Tries to import data from an ics or csv file * * @return int */ private function import_from_file() { global $ai1ec_importer_helper; $v = false; $file_extension = strtolower(substr($_FILES[self::NAME_OF_FILE_INPUT]['name'], -3)); if ($file_extension === 'csv') { $iCalcnv = new iCalcnv(); $iCalcnv->setConfig(array('inputfilename' => basename($_FILES[self::NAME_OF_FILE_INPUT]['tmp_name']), 'inputdirectory' => dirname($_FILES[self::NAME_OF_FILE_INPUT]['tmp_name']), 'outputobj' => TRUE, 'extension_check' => FALSE)); $v = $iCalcnv->csv2iCal(); } else { if ($file_extension === 'ics') { // create new instance $v = new vcalendar(); $v->parse(file_get_contents($_FILES[self::NAME_OF_FILE_INPUT]['tmp_name'])); } } $id = $_FILES[self::NAME_OF_FILE_INPUT]['name'] . '-' . date('Y-m-d-H:i:s'); $feed = $this->create_feed_instance($id); $comments = isset($_POST['ai1ec_file_upload_comments_enabled']) ? 'open' : 'closed'; $show_map = isset($_POST['ai1ec_file_upload_map_display_enabled']) ? 1 : 0; $count = $ai1ec_importer_helper->add_vcalendar_events_to_db($v, $feed, $comments, $show_map); return $count; }
<!-- Date: 2010-05-08 --> </head> <body> <?php ini_set('display_errors', 1); require_once 'iCalcreator.class.php'; $v = new vcalendar(); /* start parse of local file */ $v->setConfig('directory', 'uploads'); // set directory $v->setConfig('filename', 'basic.ics'); // set file name $v->parse(); foreach ($v->components as $component => $info) { # get first vevent $comp = $v->getComponent("VEVENT"); //print_r($comp); $summary_array = $comp->getProperty("summary", 1, TRUE); //$summary_array = $vevent->getProperty("summary", 1, TRUE); echo "Event: ", $summary_array["value"], "\n"; $dtstart_array = $comp->getProperty("dtstart", 1, TRUE); //$dtstart_array = $vevent->getProperty("dtstart", 1, TRUE); $dtstart = $dtstart_array["value"]; $startDate = "{$dtstart["year"]}-{$dtstart["month"]}-{$dtstart["day"]}"; $startTime = "{$dtstart["hour"]}:{$dtstart["min"]}:{$dtstart["sec"]}"; $dtend_array = $comp->getProperty("dtend", 1, TRUE); //$dtend_array = $vevent->getProperty("dtend", 1, TRUE); $dtend = $dtend_array["value"];
public function get_json() { $event_json = array(); $filters = $this->in->exists('filters', 'int') ? $this->in->getArray('filters', 'int') : false; $range_start = $this->time->fromformat($this->in->get('start', ''), 'Y-m-d'); $range_end = $this->time->fromformat($this->in->get('end', ''), 'Y-m-d'); $filterby = $this->in->get('filterby', 'all'); // parse the feeds $feeds = $this->pdh->get('calendars', 'idlist', array('feed', $filters)); if (is_array($feeds) && count($feeds) > 0) { foreach ($feeds as $feed) { $feedurl = $this->pdh->get('calendars', 'feed', array($feed)); if (isValidURL($feedurl)) { require_once $this->root_path . 'libraries/icalcreator/iCalcreator.class.php'; $vcalendar = new vcalendar(array('url' => $feedurl)); if (TRUE === $vcalendar->parse()) { $vcalendar->sort(); while ($comp = $vcalendar->getComponent('vevent')) { $startdate = $comp->getProperty('dtstart', 1); $enddate = $comp->getProperty('dtend', 1); // set the date for the events $allday = isset($enddate['hour']) && isset($startdate['hour']) ? false : true; if ($allday) { $startdate_out = sprintf("%04d", $startdate['year']) . '-' . sprintf("%02d", $startdate['month']) . '-' . sprintf("%02d", $startdate['day']) . ' 00:00'; $enddate_out = sprintf("%04d", $enddate['year']) . '-' . sprintf("%02d", $enddate['month']) . '-' . sprintf("%02d", $enddate['day'] - 1) . ' 00:00'; } else { $startdate_out = sprintf("%04d", $startdate['year']) . '-' . sprintf("%02d", $startdate['month']) . '-' . sprintf("%02d", $startdate['day']) . ' ' . (isset($startdate['hour']) ? sprintf("%02d", $startdate['hour']) . ':' . sprintf("%02d", $startdate['min']) : '00:00'); $enddate_out = sprintf("%04d", $enddate['year']) . '-' . $enddate['month'] . '-' . $enddate['day'] . ' ' . (isset($enddate['hour']) ? $enddate['hour'] . ':' . $enddate['min'] : '00:00'); } // build the event colours $eventcolor = $this->pdh->get('calendars', 'color', $feed); $eventcolor_txt = get_brightness($eventcolor) > 130 ? 'black' : 'white'; $event_json[] = array('eventid' => $calid, 'title' => $comp->getProperty('summary', 1), 'start' => $startdate_out, 'end' => $enddate_out, 'allDay' => $allday, 'note' => $comp->getProperty('description', 1), 'color' => $eventcolor . ' !important', 'textColor' => $eventcolor_txt . ' !important'); } } } } } // add the calendar events to the json feed $calendars = $this->pdh->get('calendars', 'idlist', array('nofeed', $filters)); $caleventids = $this->pdh->get('calendar_events', 'id_list', array(false, $range_start, $range_end, false, $filterby)); if (is_array($caleventids) && count($caleventids) > 0) { foreach ($caleventids as $calid) { $eventextension = $this->pdh->get('calendar_events', 'extension', array($calid)); $raidmode = $eventextension['calendarmode']; $eventcolor = $this->pdh->get('calendars', 'color', $this->pdh->get('calendar_events', 'calendar_id', array($calid))); $eventcolor_txt = get_brightness($eventcolor) > 130 ? 'black' : 'white'; if (in_array($this->pdh->get('calendar_events', 'calendar_id', array($calid)), $calendars)) { if ($raidmode == 'raid') { // fetch the attendees $attendees_raw = $this->pdh->get('calendar_raids_attendees', 'attendees', array($calid)); $attendees = array(); if (is_array($attendees_raw)) { foreach ($attendees_raw as $attendeeid => $attendeerow) { $attendees[$attendeerow['signup_status']][$attendeeid] = $attendeerow; } } // Build the guest array $guests = array(); if (registry::register('config')->get('calendar_raid_guests') == 1) { $guestarray = registry::register('plus_datahandler')->get('calendar_raids_guests', 'members', array($calid)); if (is_array($guestarray)) { foreach ($guestarray as $guest_row) { $guests[] = $guest_row['name']; } } } // fetch per raid data $raidcal_status = $this->config->get('calendar_raid_status'); $rstatusdata = ''; if (is_array($raidcal_status)) { foreach ($raidcal_status as $raidcalstat_id) { if ($raidcalstat_id != 4) { $actcount = isset($attendees[$raidcalstat_id]) ? count($attendees[$raidcalstat_id]) : 0; if ($raidcalstat_id == 0) { $actcount += is_array($guests) ? count($guests) : 0; } $rstatusdata .= '<div class="raid_status' . $raidcalstat_id . '">' . $this->user->lang(array('raidevent_raid_status', $raidcalstat_id)) . ': ' . $actcount . '</div>'; } } } $rstatusdata .= '<div class="raid_status_total">' . $this->user->lang('raidevent_raid_required') . ': ' . (isset($eventextension) ? $eventextension['attendee_count'] : 0) . '</div>'; $deadlinedate = $this->pdh->get('calendar_events', 'time_start', array($calid)) - $eventextension['deadlinedate'] * 3600; $deadline = $deadlinedate > $this->time->time || $this->config->get('calendar_raid_allowstatuschange') == '1' && $this->pdh->get('calendar_raids_attendees', 'status', array($calid, $this->user->id)) > 0 && $this->pdh->get('calendar_raids_attendees', 'status', array($calid, $this->user->id)) != 4 && $this->pdh->get('calendar_events', 'time_end', array($calid)) > $this->time->time ? false : true; $deadlineflag = $deadline ? '<i class="fa fa-lock fa-lg" title="' . $this->user->lang('raidevent_raid_deadl_reach') . '"></i>' : ''; // Build the JSON $event_json[] = array('type' => 'raid', 'eventid' => $calid, 'editable' => $this->user->check_auth('a_cal_revent_conf', false) || $this->check_permission($calid) ? true : false, 'title' => $this->in->decode_entity($this->pdh->get('calendar_events', 'name', array($calid))), 'url' => $this->routing->build('calendarevent', $this->pdh->get('calendar_events', 'name', array($calid)), $calid), 'start' => $this->time->date('Y-m-d H:i', $this->pdh->get('calendar_events', 'time_start', array($calid))), 'end' => $this->time->date('Y-m-d H:i', $this->pdh->get('calendar_events', 'time_end', array($calid))), 'closed' => $this->pdh->get('calendar_events', 'raidstatus', array($calid)) == 1 ? true : false, 'flag' => $deadlineflag . $this->pdh->get('calendar_raids_attendees', 'html_status', array($calid, $this->user->data['user_id'])), 'icon' => $eventextension['raid_eventid'] ? $this->pdh->get('event', 'icon', array($eventextension['raid_eventid'], true)) : '', 'note' => $this->pdh->get('calendar_events', 'notes', array($calid)), 'raidleader' => $eventextension['raidleader'] > 0 ? implode(', ', $this->pdh->aget('member', 'name', 0, array($eventextension['raidleader']))) : '', 'rstatusdata' => $rstatusdata, 'color' => $eventcolor . ' !important', 'textColor' => $eventcolor_txt . ' !important'); } else { // check if the event is private if (!$this->pdh->get('calendar_events', 'private_userperm', array($calid))) { continue; } $alldayevents = $this->pdh->get('calendar_events', 'allday', array($calid)) > 0 ? true : false; $event_json[] = array('type' => 'event', 'eventid' => $calid, 'editable' => $this->user->check_auth('a_cal_revent_conf', false) || $this->check_permission($calid) ? true : false, 'url' => $this->routing->build('calendarevent', $this->pdh->get('calendar_events', 'name', array($calid)), $calid) . 'eventdetails', 'title' => $this->pdh->get('calendar_events', 'name', array($calid)), 'start' => $this->time->date('Y-m-d H:i', $this->pdh->get('calendar_events', 'time_start', array($calid))), 'end' => $this->time->date('Y-m-d H:i', $this->pdh->get('calendar_events', 'time_end', array($calid, $alldayevents))), 'allDay' => $alldayevents, 'note' => $this->pdh->get('calendar_events', 'notes', array($calid)), 'color' => $eventcolor, 'textColor' => $eventcolor_txt, 'isowner' => $this->pdh->get('calendar_events', 'is_owner', array($calid)), 'isinvited' => $this->pdh->get('calendar_events', 'is_invited', array($calid)), 'joinedevent' => $this->pdh->get('calendar_events', 'joined_invitation', array($calid)), 'author' => $this->pdh->get('calendar_events', 'creator', array($calid)), 'attendees' => $this->pdh->get('calendar_events', 'sharedevent_attendees', array($calid))); } } } } // birthday calendar if ($this->config->get('calendar_show_birthday') && $this->user->check_auth('u_userlist', false)) { $birthday_y = $this->time->date('Y', $range_end); $birthdays = $this->pdh->get('user', 'birthday_list'); if (is_array($birthdays)) { foreach ($birthdays as $birthday_uid => $birthday_ts) { $birthday_month = $this->time->date('m', $birthday_ts); if ($birthday_month >= $this->time->date('m', $range_start) && $birthday_month <= $this->time->date('m', $range_end)) { $event_json[] = array('type' => 'birthday', 'className' => 'cal_birthday', 'title' => $this->pdh->get('user', 'name', array($birthday_uid)), 'start' => $birthday_y . '-' . $this->time->date('m-d', $birthday_ts), 'end' => $birthday_y . '-' . $this->time->date('m-d', $birthday_ts), 'allDay' => true, 'textColor' => '#000000', 'backgroundColor' => '#E8E8E8', 'borderColor' => '#7F7F7F'); } } } } // hooks if ($this->hooks->isRegistered('calendar')) { $arrHooksData = $this->hooks->process('calendar', array('start' => $range_start, 'end' => $range_end), false); if (count($arrHooksData) > 0) { $event_json = array_merge($arrHooksData, $event_json); } } // Output the array as JSON echo json_encode($event_json); exit; }
public static function toKalenderEvent($iCal, $ownerClass = "iCal", $ownerClassID = "-1") { $VC = new vcalendar(); $VC->parse($iCal); $event = $VC->getComponent("vevent"); $dayStart = $event->getProperty("DTSTART"); $dayEnd = $event->getProperty("DTEND"); $dayStartTS = strtotime(implode("", $dayStart)); $dayEndTS = strtotime(implode("", $dayEnd)); $KE = new KalenderEvent($ownerClass, $ownerClassID, Kalender::formatDay($dayStartTS), Kalender::formatTime($dayStartTS), $event->getProperty("SUMMARY")); $KE->UID($event->getProperty("UID")); $organizer = $event->getProperty("ORGANIZER", 0, true); $organizer["value"] = str_replace("MAILTO:", "", $organizer["value"]); $ON = $organizer["value"]; if (isset($organizer["params"]["CN"])) { $ON = $organizer["params"]["CN"]; } $OE = $organizer["value"]; $KE->organizer($ON, $OE); if ($dayStart["hour"] . $dayStart["min"] == "" and $dayEnd["hour"] . $dayEnd["min"] == "") { $KE->allDay(true); } else { $KE->endDay(Kalender::formatDay($dayEndTS)); $KE->endTime(Kalender::formatTime($dayEndTS)); } return $KE; }
$calendar .= "selected=\"selected\""; } $calendar .= ">{$i}</option>"; } $calendar .= " </select> \n\n <a href=\"index.php?page=calender&view=list&month={$nextMonth}&year={$year}&menuid={$menuid}\"><img src=\"{$templateinfo['imagedir']}next.png\" title=\"Next Month\" alt=\"Next Month\" border=\"0\"/></a> \n <a href=\"index.php?page=calender&view=list&month={$monthnums[$month]}&year={$nextYear}&menuid={$menuid}\"><img src=\"{$templateinfo['imagedir']}last.png\" title=\"Next Year\" alt=\"Next Year\" border=\"0\"/></a>\n </div>\n </form>"; $days = number_days($monthnum); $monthStart = strtotime("{$year}-{$monthnum}-01 00:00:00"); $monthEnd = strtotime("{$year}-{$monthnum}-{$days} 23:59:59"); $numcalendar = 0; $calitems = $data->select_fetch_all_rows($numcalendar, "calendar_items", "WHERE ((((startdate >= {$monthStart} OR enddate >= {$monthEnd}) AND startdate <= {$monthEnd}) OR (startdate <= {$monthStart} AND enddate >= {$monthStart})) AND allowed=1 AND trash=0) ORDER BY startdate ASC"); $icalsql = $data->select_query("ical_items"); $temp2 = $data->select_fetch_one_row("calendar_items", "ORDER BY id DESC", "id"); $startid = $temp2['id'] + 1; while ($temp = $data->fetch_array($icalsql)) { $vcalendar = new vcalendar(); $vcalendar->parse('http://' . $temp['link']); while ($vevent = $vcalendar->getComponent()) { $startdate = $vevent->getProperty('dtstart'); $enddate = $vevent->getProperty('dtend'); if ($startdate['year'] <= $year && $enddate['year'] >= $year && $startdate['month'] <= $monthnum && $enddate['month'] >= $monthnum) { $item = array(); $item['id'] = $startid++; $item['summary'] = trim($vevent->getProperty('summary')) . " [{$temp['name']}]"; $item['detail'] = trim($vevent->getProperty('description')); $item['startdate'] = mktime($startdate['hour'], $startdate['min'], 0, $startdate['month'], $startdate['day'], $startdate['year']); $item['enddate'] = mktime($enddate['hour'], $enddate['min'], 0, $enddate['month'], $enddate['day'], $enddate['year']); $item['groups'] = $temp['groups']; $item['colour'] = $temp['colour']; $item['ical'] = true; $calitems[] = $item; $numcalendar++;
/** * @param DBClass_friendica_calendars $calendar * @param DBClass_friendica_calendarobjects $calendarobject */ function renderCalDavEntry_data(&$calendar, &$calendarobject) { $a = get_app(); $v = new vcalendar(); $v->setConfig('unique_id', $a->get_hostname()); $v->parse($calendarobject->calendardata); $v->sort(); $eventArray = $v->selectComponents(2009, 1, 1, date("Y") + 2, 12, 30); $start_min = $end_max = ""; $allday = $summary = $vevent = $rrule = $color = $start = $end = null; $location = $description = ""; foreach ($eventArray as $yearArray) { foreach ($yearArray as $monthArray) { foreach ($monthArray as $day => $dailyEventsArray) { foreach ($dailyEventsArray as $vevent) { /** @var $vevent vevent */ $start = ""; $rrule = "NULL"; $allday = 0; $dtstart = $vevent->getProperty('X-CURRENT-DTSTART'); if (is_array($dtstart)) { $start = "'" . $dtstart[1] . "'"; if (strpos($dtstart[1], ":") === false) { $allday = 1; } } else { $dtstart = $vevent->getProperty('dtstart'); if (isset($dtstart["day"]) && $dtstart["day"] == $day) { // Mehrtägige Events nur einmal rein if (isset($dtstart["hour"])) { $start = "'" . $dtstart["year"] . "-" . $dtstart["month"] . "-" . $dtstart["day"] . " " . $dtstart["hour"] . ":" . $dtstart["minute"] . ":" . $dtstart["secont"] . "'"; } else { $start = "'" . $dtstart["year"] . "-" . $dtstart["month"] . "-" . $dtstart["day"] . " 00:00:00'"; $allday = 1; } } } $dtend = $vevent->getProperty('X-CURRENT-DTEND'); if (is_array($dtend)) { $end = "'" . $dtend[1] . "'"; if (strpos($dtend[1], ":") === false) { $allday = 1; } } else { $dtend = $vevent->getProperty('dtend'); if (isset($dtend["hour"])) { $end = "'" . $dtend["year"] . "-" . $dtend["month"] . "-" . $dtend["day"] . " " . $dtend["hour"] . ":" . $dtend["minute"] . ":" . $dtend["second"] . "'"; } else { $end = "'" . $dtend["year"] . "-" . $dtend["month"] . "-" . $dtend["day"] . " 00:00:00' - INTERVAL 1 SECOND"; $allday = 1; } } $summary = $vevent->getProperty('summary'); $description = $vevent->getProperty('description'); $location = $vevent->getProperty('location'); $rrule_prob = $vevent->getProperty('rrule'); if ($rrule_prob != null) { $rrule = $vevent->createRrule(); $rrule = "'" . dbesc($rrule) . "'"; } $color_ = $vevent->getProperty("X-ANIMEXX-COLOR"); $color = is_array($color_) ? $color_[1] : "NULL"; if ($start_min == "" || preg_replace("/[^0-9]/", "", $start) < preg_replace("/[^0-9]/", "", $start_min)) { $start_min = $start; } if ($end_max == "" || preg_replace("/[^0-9]/", "", $end) > preg_replace("/[^0-9]/", "", $start_min)) { $end_max = $end; } } } } } if ($start_min != "") { if ($allday && mb_strlen($end_max) == 12) { $x = explode("-", str_replace("'", "", $end_max)); $time = mktime(0, 0, 0, IntVal($x[1]), IntVal($x[2]), IntVal($x[0])); $end_max = date("'Y-m-d H:i:s'", $time - 1); } q("INSERT INTO %s%sjqcalendar (`uid`, `namespace`, `namespace_id`, `ical_uri`, `Subject`, `Location`, `Description`, `StartTime`, `EndTime`, `IsAllDayEvent`, `RecurringRule`, `Color`)\n\t\t\tVALUES (%d, %d, %d, '%s', '%s', '%s', '%s', %s, %s, %d, '%s', '%s')", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendar->uid), IntVal($calendarobject->namespace), IntVal($calendarobject->namespace_id), dbesc($calendarobject->uri), dbesc($summary), dbesc($location), dbesc(str_replace("\\n", "\n", $description)), $start_min, $end_max, IntVal($allday), dbesc($rrule), dbesc($color)); foreach ($vevent->components as $comp) { /** @var $comp calendarComponent */ $trigger = $comp->getProperty("TRIGGER"); $sql_field = $trigger["relatedStart"] ? $start : $end; $sql_op = $trigger["before"] ? "DATE_SUB" : "DATE_ADD"; $num = ""; $rel_type = ""; $rel_value = 0; if (isset($trigger["second"])) { $num = IntVal($trigger["second"]) . " SECOND"; $rel_type = "second"; $rel_value = IntVal($trigger["second"]); } if (isset($trigger["minute"])) { $num = IntVal($trigger["minute"]) . " MINUTE"; $rel_type = "minute"; $rel_value = IntVal($trigger["minute"]); } if (isset($trigger["hour"])) { $num = IntVal($trigger["hour"]) . " HOUR"; $rel_type = "hour"; $rel_value = IntVal($trigger["hour"]); } if (isset($trigger["day"])) { $num = IntVal($trigger["day"]) . " DAY"; $rel_type = "day"; $rel_value = IntVal($trigger["day"]); } if (isset($trigger["week"])) { $num = IntVal($trigger["week"]) . " WEEK"; $rel_type = "week"; $rel_value = IntVal($trigger["week"]); } if (isset($trigger["month"])) { $num = IntVal($trigger["month"]) . " MONTH"; $rel_type = "month"; $rel_value = IntVal($trigger["month"]); } if (isset($trigger["year"])) { $num = IntVal($trigger["year"]) . " YEAR"; $rel_type = "year"; $rel_value = IntVal($trigger["year"]); } if ($trigger["before"]) { $rel_value *= -1; } if ($rel_type != "") { $not_date = "{$sql_op}({$sql_field}, INTERVAL {$num})"; q("INSERT INTO %s%snotifications (`uid`, `ical_uri`, `rel_type`, `rel_value`, `alert_date`, `notified`) VALUES ('%s', '%s', '%s', '%s', %s, IF(%s < NOW(), 1, 0))", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendar->uid), dbesc($calendarobject->uri), dbesc($rel_type), IntVal($rel_value), $not_date, $not_date); } } } }
public function processFeeds() { require_once dirname(__FILE__) . '/mxcalendars.ics.class.php'; $f = $this->modx->newQuery('mxCalendarFeed'); $f->where(array('active:=' => 1, 'nextrunon:<=' => time())); $f->prepare(); $mxcfeeds = $this->modx->getCollection('mxCalendarFeed', $f); if ($this->loggingEnabled) { $this->logEvent('feed', 'feeds processor called\\n\\nSQL:\\n' . $f->toSql()); } //$this->modx->setLogLevel(modX::LOG_LEVEL_INFO); foreach ($mxcfeeds as $feed) { $hadmodifications = 0; if ($feed->get('type') == 'ical') { $activeUrl = $feed->get('feed'); $myics = file_get_contents($activeUrl); // Cache the response for giggles //$this->modx->cacheManager->set('mxcfeed-'.$feed->get('id'),$myics,3600); $config = array("unique_id" => 'mxcfeed-' . $feed->get('id') . '-' . time(), "url" => $activeUrl); $vcalendar = new vcalendar($config); $vcalendar->parse(); //$this->modx->setLogLevel(modX::LOG_LEVEL_INFO); //$this->modx->log(modX::LOG_LEVEL_INFO,'Parsing feed #'.$feed->get('id').' events. ['.$feed->get('feed').']\n\nResponse:\n'.$myics); if ($this->loggingEnabled) { $this->logEvent('feed parse', 'Parsing feed #' . $feed->get('id') . ' events. [' . $feed->get('feed') . ']\\n\\nResponse:\\n' . $myics); } while ($vevent = $vcalendar->getComponent("vevent")) { if ($vevent->dtstart['value']) { $start = mktime($vevent->dtstart['value']['hour'], $vevent->dtstart['value']['min'], $vevent->dtstart['value']['sec'], $vevent->dtstart['value']['month'], $vevent->dtstart['value']['day'], $vevent->dtstart['value']['year']); // one occurrence } else { $start = ''; } if ($vevent->dtend['value']) { $end = mktime($vevent->dtend['value']['hour'], $vevent->dtend['value']['min'], $vevent->dtend['value']['sec'], $vevent->dtend['value']['month'], $vevent->dtend['value']['day'], $vevent->dtend['value']['year']); } else { $end = ''; } if ($vevent->lastmodified['value']) { $lastchange = mktime($vevent->lastmodified['value']['hour'], $vevent->lastmodified['value']['min'], $vevent->lastmodified['value']['sec'], $vevent->lastmodified['value']['month'], $vevent->lastmodified['value']['day'], $vevent->lastmodified['value']['year']); } else { $lastchange = ''; } if ($vevent->created['value']) { $createdDate = mktime($vevent->created['value']['hour'], $vevent->created['value']['min'], $vevent->created['value']['sec'], $vevent->created['value']['month'], $vevent->created['value']['day'], $vevent->created['value']['year']); } else { $createdDate = ''; } $description = $vevent->getProperty("description"); // one occurrence $location = $vevent->getProperty("location"); $title = $vevent->getProperty("summary"); $feedEventUID = $vevent->getProperty("uid"); //-- Multiple Occurances //while( $comment = $vevent->getProperty( "comment" )) { // MAY occur more than once // echo json_encode($comment).'<br /><hr /><br />'; //} // Output for testing $event = array('title' => $title, 'description' => !empty($description) ? $description : '', 'location_name' => $location, 'startdate' => $start, 'enddate' => $end, 'source' => 'feed', 'lastedit' => $lastchange, 'feeds_id' => $feed->get('id'), 'feeds_uid' => $feedEventUID, 'context' => '', 'categoryid' => $feed->get('defaultcategoryid'), 'createdon' => $createDate, 'repeattype' => 0, 'repeaton' => '', 'repeatfrequency' => 0); //echo 'Title: '.$title.'<br />'.json_encode($event).'<br /><hr><br /><br />'; //-- Save the new event if (!empty($feedEventUID)) { $existingEvent = $this->modx->getObject('mxCalendarEvents', array('feeds_uid' => $feedEventUID)); if (!is_object($existingEvent)) { $existingEvent = $this->modx->getObject('mxCalendarEvents', array('title' => $title)); } } else { $existingEvent = $this->modx->getObject('mxCalendarEvents', array('title' => $title)); } if (is_object($existingEvent)) { // Check and modify existing event if modified since last update if ($existingEvent->get('lastedit') <= $lastchange) { // Event has been updated so lets just update all properties $existingEvent->fromArray($event); $existingEvent->save(); if ($this->loggingEnabled) { $this->logEvent('feed', 'Update Event (' . $existingEvent->get('id') . ') for feed #' . $feed->get('id') . '\\n\\nEvent JSON:\\n' . json_encode($event)); } $hadmodifications++; } } else { // Create the newly found event from the feed $feedEvent = $this->modx->newObject('mxCalendarEvents'); $feedEvent->fromArray($event); $feedEvent->save(); if ($this->loggingEnabled) { $this->logEvent('feed', 'New Event (' . $feedEvent->get('id') . ') for feed #' . $feed->get('id') . '\\n\\nEvent JSON:\\n' . json_encode($event)); } $hadmodifications++; } } //-- Update the feed next run time $nextTime = strtotime('+' . $feed->get('timerint') . ' ' . $feed->get('timermeasurement')); $feed->set('lastrunon', time()); $feed->set('nextrunon', $nextTime); $feed->save(); if ($hadmodifications) { $this->logEvent('feed', 'Parsing feed #' . $feed->get('id') . ' had <strong>' . $hadmodifications . '</strong> event' . ($hadmodifications > 1 ? 's' : '') . ' added/updated [' . $feed->get('feed') . ']'); } else { $this->logEvent('feed', 'Parsing feed #' . $feed->get('id') . ' had no changes. [' . $feed->get('feed') . ']'); } } else { //-- ==================== --// //-- Process the XML feed --// //-- ==================== --// $activeUrl = $feed->get('feed'); $xmlEvents = file_get_contents($activeUrl); $events = new SimpleXMLElement($xmlEvents); $idx = 0; foreach ($events->event as $event) { if (strtolower($event->timebegin) !== 'all day') { $startDateTime = strtotime($event->date . ' ' . $event->timebegin); $endDateTime = strtotime($event->date . ' ' . str_replace('- ', '', $event->timeend)); } else { $startDateTime = strtotime($event->date . ' 00:00:00'); $endDateTime = strtotime($event->date . ' 23:59:59'); } $lastchange = !empty($event->lastedit) ? $event->lastedit : time(); // Output for testing $eventdata = array('title' => $event->title, 'description' => !empty($event->description) ? $event->description : '', 'location_name' => !empty($event->location) ? $event->location : '', 'startdate' => $startDateTime, 'enddate' => $endDateTime, 'source' => 'feed', 'lastedit' => $lastchange, 'feeds_id' => $feed->get('id'), 'feeds_uid' => !empty($event->eventid) ? $event->eventid : '', 'context' => '', 'categoryid' => $feed->get('defaultcategoryid'), 'createdon' => !empty($event->createDate) ? $event->createDate : time(), 'repeattype' => 0, 'repeaton' => '', 'repeatfrequency' => 0); //-- Save the new event if (!empty($event->eventid) && isset($event->eventid)) { $q = $this->modx->newQuery('mxCalendarEvents'); $title = (string) $event->title; $feeduid = (string) $event->eventid; $q->where(array('mxCalendarEvents.title' => $title, 'mxCalendarEvents.feeds_id' => $feed->get('id'), 'mxCalendarEvents.feeds_uid' => $feeduid)); $q->prepare(); //echo 'SQL ['.$event->title.' '.$event->eventid.']: <br />'.$q->toSQL().'<br /><br />'; $existingEvent = $this->modx->getObject('mxCalendarEvents', $q); //$existingEvent = $this->modx->getObject('mxCalendarEvents',array()); } else { $existingEvent = false; } if (is_object($existingEvent)) { // Check and modify existing event if modified since last update if ($existingEvent->get('lastedit') <= $lastchange) { // Event has been updated so lets just update all properties $existingEvent->fromArray($eventdata); $existingEvent->save(); if ($this->loggingEnabled) { $this->logEvent('feed', 'Update Event (' . $existingEvent->get('id') . ')[' . $event->eventid . '] for feed #' . $feed->get('id') . '\\n\\nEvent JSON:\\n' . json_encode($event)); } $hadmodifications++; } } else { // Create the newly found event from the feed $feedEvent = $this->modx->newObject('mxCalendarEvents'); $feedEvent->fromArray($eventdata); $feedEvent->save(); if ($this->loggingEnabled) { $this->logEvent('feed', 'New Event (' . $feedEvent->get('id') . ') for feed #' . $feed->get('id') . '\\n\\nEvent JSON:\\n' . json_encode($event)); } $hadmodifications++; } //unset($event); $idx++; } //-- Update the feed next run time $nextTime = strtotime('+' . $feed->get('timerint') . ' ' . $feed->get('timermeasurement')); $feed->set('lastrunon', time()); $feed->set('nextrunon', $nextTime); $feed->save(); if ($hadmodifications) { $this->logEvent('feed', 'Parsing feed #' . $feed->get('id') . ' had <strong>' . $hadmodifications . '</strong> event' . ($hadmodifications > 1 ? 's' : '') . ' added/updated [' . $feed->get('feed') . ']'); } else { $this->logEvent('feed', 'Parsing feed #' . $feed->get('id') . ' had no changes. [' . $feed->get('feed') . ']'); } } } }
public function import_ics_data($calendar_id, $run_deletes = false) { // ------------------------------------- // this has the potential to take a while // ------------------------------------- set_time_limit(0); // ------------------------------------- // Get some basic info to use later // ------------------------------------- $tmpl_exists = isset(ee()->TMPL) && is_object(ee()->TMPL); $cbasics = $this->data->calendar_basics(); $cbasics = $cbasics[$calendar_id]; $urls = $cbasics['ics_url']; if ($urls == '') { return FALSE; } $tz_offset = $cbasics['tz_offset'] != '' ? $cbasics['tz_offset'] : '0000'; // ------------------------------------- // delete missing items from update? // ------------------------------------- $delete = ($this->check_yes($this->preference('ics_update_delete_default')) or $tmpl_exists && $this->check_yes(ee()->TMPL->fetch_param('delete_missing'))); /* This shouldn't be happening because DST is only something that would need to be applied when generating the users current local time. If an event were at 7pm EST or EDT, it would still be at 7pm either way. I hate DST. if ($tz_offset != '0000' AND ee()->config->item('daylight_savings') == 'y') { $tz_offset += 100; } */ $channel_id = $this->data->channel_is_events_channel(); $author_id = $cbasics['author_id']; // ------------------------------------- // Prepare the URLs // ------------------------------------- if (!is_array($urls)) { $urls = explode("\n", $urls); } foreach ($urls as $k => $url) { $urls[$k] = trim($url); } // ------------------------------------- // Load iCalCreator // ------------------------------------- if (!class_exists('vcalendar')) { require_once 'libraries/icalcreator/iCalcreator.class.php'; } // ------------------------------------- // Load Calendar_datetime // ------------------------------------- if (!class_exists('Calendar_datetime')) { require_once CALENDAR_PATH . 'calendar.datetime' . EXT; } $CDT = new Calendar_datetime(); $CDT_end = new Calendar_datetime(); // ------------------------------------- // Load Publish // ------------------------------------- ee()->load->library('api'); ee()->api->instantiate(array('channel_entries', 'channel_categories', 'channel_fields')); ee()->load->helper('url'); ee()->api_channel_entries->assign_cat_parent = ee()->config->item('auto_assign_cat_parents') == 'n' ? FALSE : TRUE; // ------------------------------------- // Tell our extensions that we're running the icalendar import // ------------------------------------- $this->cache['ical'] = TRUE; // ------------------------------------- // Get already-imported events // ------------------------------------- $imported = $this->data->get_imported_events($calendar_id); $imported_not_found = !empty($imported) ? array_combine(array_keys($imported), array_keys($imported)) : array(); // ------------------------------------- // Don't let EXT drop us early // ------------------------------------- ee()->extensions->in_progress = ''; // ------------------------------------- // Cycle through the URLs // ------------------------------------- $errors = array(); foreach ($urls as $url) { $ICAL = new vcalendar(); $output = $this->fetch_url($url); //lets not do horrible things if (empty($output)) { continue; } $ICAL->parse($output); // ------------------------------------- // Iterate among the events // ------------------------------------- while ($event = $ICAL->getComponent('vevent')) { // ------------------------------------- // Times // ------------------------------------- $hour = isset($event->dtstart['value']['hour']) ? $event->dtstart['value']['hour'] : 00; $minute = isset($event->dtstart['value']['min']) ? $event->dtstart['value']['min'] : 00; $end_hour = isset($event->dtend['value']['hour']) ? $event->dtend['value']['hour'] : $hour; $end_minute = isset($event->dtend['value']['min']) ? $event->dtend['value']['min'] : $minute; // ------------------------------------- // Last-modified date // ------------------------------------- if (isset($event->lastmodified['value'])) { $lm_date = $event->lastmodified['value']['year'] . $event->lastmodified['value']['month'] . $event->lastmodified['value']['day'] . $event->lastmodified['value']['hour'] . $event->lastmodified['value']['min']; } elseif (isset($event->dtstamp['value'])) { $lm_date = $event->dtstamp['value']['year'] . $event->dtstamp['value']['month'] . $event->dtstamp['value']['day'] . $event->dtstamp['value']['hour'] . $event->dtstamp['value']['min']; } else { $lm_date = $event->created['value']['year'] . $event->created['value']['month'] . $event->created['value']['day'] . $event->created['value']['hour'] . $event->created['value']['min']; } // ------------------------------------- // Does this event already exist? Do we need to update? // ------------------------------------- if (isset($imported[$event->uid['value']])) { //for what needs to be deleted unset($imported_not_found[$event->uid['value']]); // ------------------------------------- // Has the event been updated? No reason // to do any work if it's the same old stuff. // ------------------------------------- if ($lm_date == $imported[$event->uid['value']]['last_mod']) { continue; } $entry_id = $imported[$event->uid['value']]['entry_id']; } else { $entry_id = ''; } // ------------------------------------- // Adjust CDT // ------------------------------------- $CDT->change_datetime($event->dtstart['value']['year'], $event->dtstart['value']['month'], $event->dtstart['value']['day'], $hour, $minute); if (isset($event->dtend['value'])) { $CDT_end->change_datetime($event->dtend['value']['year'], $event->dtend['value']['month'], $event->dtend['value']['day'], $end_hour, $end_minute); } else { $CDT_end->change_ymd($CDT->ymd); $CDT_end->change_time($end_hour, $end_minute); } // ------------------------------------- // Adjust to the correct timezone for thie calendar // ------------------------------------- if (!isset($event->dtstart['params']['TZID']) or $event->dtstart['params']['TZID'] == '') { if (isset($event->dtstart['value']['hour'])) { $CDT->add_time($tz_offset); $CDT_end->add_time($tz_offset); } else { $CDT_end->add_day(-1); } } // ------------------------------------- // Variableification // ------------------------------------- $title = isset($event->summary['value']) ? $event->summary['value'] : lang('no_title'); $summary = (isset($event->description) and is_array($event->description) and isset($event->description[0]['value'])) ? $event->description[0]['value'] : ''; $location = isset($event->location['value']) ? $event->location['value'] : ''; $rules = $this->ical_rule_to_calendar_rule($event->rrule); $exceptions = array('date' => array()); if (mb_strlen($title) > 100) { $title = substr($title, 0, 100); } if (is_array($event->exdate) and !empty($event->exdate)) { $exceptions = $this->ical_exdate_to_calendar_exception($event->exdate); } $recurs = (is_array($event->rrule) and !empty($event->rrule)) ? 'y' : 'n'; // ------------------------------------- // Fix some linebreak problems // ------------------------------------- $summary = str_replace(array("\r", "\n"), '', $summary); $summary = str_replace('\\n', "\n", $summary); // ------------------------------------- // Set up $_POST // ------------------------------------- $prefix = CALENDAR_EVENTS_FIELD_PREFIX; $sum_fid = $this->data->get_field_id($prefix . 'summary'); $loc_fid = $this->data->get_field_id($prefix . 'location'); $dao_fid = $this->data->get_field_id($prefix . 'dates_and_options'); //lets not be buttwipes //will restore old post later $old_POST = $_POST; //must use $_POST here because many fieldtypoes use their own //processing via POST and ignore incoming data $_POST = $post_data = array('site_id' => $this->data->get_site_id(), 'author_id' => $author_id, 'entry_id' => $entry_id, 'channel_id' => $channel_id, 'status' => 'open', 'entry_date' => ee()->localize->now - 3600 * 24 * 2, 'title' => $title, 'url_title' => url_title($title), 'calendar_id' => $calendar_id, 'field_id_' . $sum_fid => $summary, 'field_id_' . $loc_fid => $location, 'field_id_' . $dao_fid => $calendar_id, 'rule_id' => array(), 'start_date' => array($CDT->ymd), 'start_time' => array($CDT->hour . $CDT->minute), 'end_date' => array($CDT_end->ymd), 'end_time' => array($CDT_end->hour . $CDT_end->minute), 'all_day' => !isset($event->dtstart['value']['hour']) ? 'y' : 'n', 'rule_type' => $rules['rule_type'], 'repeat_years' => $rules['repeat_years'], 'repeat_months' => $rules['repeat_months'], 'repeat_weeks' => $rules['repeat_weeks'], 'repeat_days' => $rules['repeat_days'], 'days_of_week' => $rules['days_of_week'], 'relative_dow' => $rules['relative_dow'], 'days_of_month' => $rules['days_of_month'], 'months_of_year' => $rules['months_of_year'], 'end_by' => $rules['end_by'], 'end_after' => $rules['end_after'], 'occurrences' => $exceptions, 'expiration_date' => '', 'comment_expiration_date' => '', 'allow_comments' => 'n'); //-------------------------------------------- // Check for custom field group //-------------------------------------------- ee()->api_channel_fields->setup_entry_settings($channel_id, $post_data); if ($entry_id) { // ------------------------------------- // get old data and merge // ------------------------------------- ee()->load->model('channel_entries_model'); //if you don't give it the channel_id it //only selects entry, channel, and author id. lol wtf? $old_data_q = ee()->channel_entries_model->get_entry($entry_id, $channel_id); if ($old_data_q->num_rows() > 0) { $old_data = $old_data_q->row_array(); ee()->load->library('calendar_ics_field_update'); //prep special cases $old_data = ee()->calendar_ics_field_update->run_prep_fields($old_data); //need to let our new data override old $_POST = $post_data = array_merge($old_data, $post_data); } //fire update and cross fingers $result = ee()->api_channel_entries->update_entry($entry_id, $post_data); } else { //now we can do the new entry $result = ee()->api_channel_entries->submit_new_entry($channel_id, $post_data); } if ($result === FALSE) { //restore old post $_POST = $old_POST; $errors[] = ee()->api_channel_entries->errors; continue; } // ------------------------------------- // Update the imports table // ------------------------------------- /*if (isset($this->cache['ical_event_id'])) {*/ $data = array('calendar_id' => $calendar_id, 'event_id' => $this->cache['ical_event_id'], 'entry_id' => $this->cache['ical_entry_id'], 'uid' => $event->uid['value'], 'last_mod' => $lm_date); if ($entry_id != '') { $data['import_id'] = $imported[$event->uid['value']]['import_id']; $this->data->update_imported_event($data); } else { //$data['import_id'] = '0'; $this->data->add_imported_event($data); } /*}*/ //restore old post $_POST = $old_POST; } //END while ($event = $ICAL->getComponent('vevent')) } //END foreach ($urls as $url) // ------------------------------------- // items to delete // ------------------------------------- if ($delete) { $deleted_ids = array(); foreach ($imported_not_found as $uid => $value) { $deleted_ids[] = $imported[$uid]['entry_id']; } if (!empty($deleted_ids)) { ee()->api_channel_entries->delete_entry($deleted_ids); } } $this->data->update_ics_updated($calendar_id); ee()->extensions->end_script = FALSE; ee()->extensions->in_progress = 'entry_submission_end'; return TRUE; }
function _get_events_url(&$events, $url, $date) { $v = new vcalendar(); $v->setConfig('unique_id', 'barchat'); $v->setProperty('method', 'PUBLISH'); $v->setProperty("x-wr-calname", "Calendar Sample"); $v->setProperty("X-WR-CALDESC", "Calendar Description"); $v->setProperty("X-WR-TIMEZONE", "America/New_York"); $v->setConfig('url', $url); try { $v->parse(); } catch (exception $e) { } $v->sort(); $m = $date->format('n'); $d = $date->format('j'); $y = $date->format('Y'); $eventArray = $v->selectComponents($y, $m, 1, $y, $m, 31); foreach ((array) $eventArray as $yearkey => $yeararray) { foreach ((array) $yeararray as $monthkey => $montharray) { foreach ((array) $montharray as $daykey => $dayarray) { foreach ((array) $dayarray as $eventnumber => $event) { //echo "{$y}-{$m}-{$daykey} [{$eventnumber}]: "; $time = $event->dtstart['value']; $tz = $event->dtstart['params']['TZID'] == '' ? 'America/New_York' : $event->dtstart['params']['TZID']; if ($time['tz'] == 'Z') { $tz = 'GMT'; } if (isset($event->dtstart['params']['VALUE']) && $event->dtstart['params']['VALUE'] == 'DATE') { $allday = new DateTime("{$time['year']}-{$time['month']}-{$time['day']}", new DateTimeZone($tz)); $allday->setTimezone(new DateTimeZone('America/New_York')); $d = sprintf('%04d-%02d-%02d', $y, $m, $daykey); if (!is_array($events[$d])) { $events[$d] = array(); } $alldayint = intval($allday->format('U')); while (isset($events[$d][$alldayint])) { $alldayint++; } $events[$d][$alldayint] = '<span class="calendartime">All Day</span> ' . trim($event->summary['value']); //var_dump(date('r', $allday) . ' = ' . $allday); //var_dump($event->summary['value']); } else { if (isset($event->xprop['X-CURRENT-DTSTART'])) { $dt = new DateTime($event->xprop['X-CURRENT-DTSTART']['value'], new DateTimeZone($tz)); } else { $dt = new DateTime("{$time['year']}-{$time['month']}-{$time['day']} {$time['hour']}:{$time['min']}:{$time['sec']}", new DateTimeZone($tz)); } $dt->setTimezone(new DateTimeZone('America/New_York')); if (isset($event->xprop['X-CURRENT-DTEND'])) { $dte = new DateTime($event->xprop['X-CURRENT-DTEND']['value'], new DateTimeZone($tz)); } else { $timee = $event->dtstart['value']; $dte = new DateTime("{$timee['year']}-{$timee['month']}-{$timee['day']} {$timee['hour']}:{$timee['min']}:{$timee['sec']}", new DateTimeZone($tz)); } $dte->setTimezone(new DateTimeZone('America/New_York')); if (!is_array($events[$d])) { $events[$d] = array(); } $d = sprintf('%04d-%02d-%02d', $y, $m, $daykey); $daytime = $dt->format('U'); while (isset($events[$d][$daytime])) { $daytime++; } if ($dt->format('g:ia') != $dte->format('g:ia')) { $events[$d][$daytime] = '<span class="calendartime">' . $dt->format('g:ia') . ' - ' . $dte->format('g:ia') . '</span> ' . trim($event->summary['value']); } else { $events[$d][$daytime] = '<span class="calendartime">' . $dt->format('g:ia') . '</span> ' . trim($event->summary['value']); } //var_dump($event->dtstart); //var_dump($event->summary['value']); //var_dump($dt->format('r')); //var_dump($event); } } } } } }
/** * Import an iCal file into the database * @param array Course info * @return boolean True on success, false otherwise */ function agenda_import_ical($course_info, $file) { require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php'; $charset = api_get_system_encoding(); $filepath = api_get_path(SYS_ARCHIVE_PATH) . $file['name']; if (!@move_uploaded_file($file['tmp_name'], $filepath)) { error_log('Problem moving uploaded file: ' . $file['error'] . ' in ' . __FILE__ . ' line ' . __LINE__); return false; } require_once api_get_path(LIBRARY_PATH) . 'icalcreator/iCalcreator.class.php'; $ical = new vcalendar(); $ical->setConfig('directory', dirname($filepath)); $ical->setConfig('filename', basename($filepath)); $ical->parse(); //we need to recover: summary, description, dtstart, dtend, organizer, attendee, location (=course name), // rrule $ve = $ical->getComponent(VEVENT); //print_r($ve); $ttitle = $ve->getProperty('summary'); //print_r($ttitle); $title = api_convert_encoding($ttitle, $charset, 'UTF-8'); $tdesc = $ve->getProperty('description'); $desc = api_convert_encoding($tdesc, $charset, 'UTF-8'); $ts = $ve->getProperty('dtstart'); $start_date = $ts['year'] . '-' . $ts['month'] . '-' . $ts['day'] . ' ' . $ts['hour'] . ':' . $ts['min'] . ':' . $ts['sec']; $ts = $ve->getProperty('dtend'); $end_date = $ts['year'] . '-' . $ts['month'] . '-' . $ts['day'] . ' ' . $ts['hour'] . ':' . $ts['min'] . ':' . $ts['sec']; //echo $start_date.' - '.$end_date; $organizer = $ve->getProperty('organizer'); $attendee = $ve->getProperty('attendee'); $course_name = $ve->getProperty('location'); //insert the event in our database $id = agenda_add_item($course_info, $title, $desc, $start_date, $end_date, $_POST['selectedform']); $repeat = $ve->getProperty('rrule'); if (is_array($repeat) && !empty($repeat['FREQ'])) { $trans = array('DAILY' => 'daily', 'WEEKLY' => 'weekly', 'MONTHLY' => 'monthlyByDate', 'YEARLY' => 'yearly'); $freq = $trans[$repeat['FREQ']]; $interval = $repeat['INTERVAL']; if (isset($repeat['UNTIL']) && is_array($repeat['UNTIL'])) { $until = mktime(23, 59, 59, $repeat['UNTIL']['month'], $repeat['UNTIL']['day'], $repeat['UNTIL']['year']); $res = agenda_add_repeat_item($course_info, $id, $freq, $until, $_POST['selectedform']); } //TODO: deal with count if (!empty($repeat['COUNT'])) { $count = $repeat['COUNT']; $res = agenda_add_repeat_item($course_info, $id, $freq, $count, $_POST['selectedform']); } } return true; }