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;
 }
 function insert_new_entry()
 {
     global $IN, $FNS, $OUT, $LANG, $SESS, $LOC, $EXT, $PREFS;
     $LANG->fetch_language_file('weblog');
     $LANG->fetch_language_file('publish');
     // Ya gotta be logged-in billy bob...
     if ($SESS->userdata('member_id') == 0) {
         return $OUT->show_user_error('general', $LANG->line('weblog_must_be_logged_in'));
     }
     /** ----------------------------------------
     		/**  Prep data for insertion
     		/** ----------------------------------------*/
     if (!$IN->GBL('preview', 'POST')) {
         unset($_POST['hidden_pings']);
         unset($_POST['status_id']);
         unset($_POST['allow_cmts']);
         unset($_POST['allow_tbks']);
         unset($_POST['sticky_entry']);
         if (!$IN->GBL('entry_date', 'POST')) {
             $_POST['entry_date'] = $LOC->set_human_time($LOC->now);
         }
         if (!class_exists('Display')) {
             require PATH_CP . 'cp.display' . EXT;
         }
         global $DSP;
         $DSP = new Display();
         // -------------------------------------------
         // 'weblog_standalone_insert_entry' hook.
         //  - Modify any of the POST data for a stand alone entry insert
         //
         $edata = $EXT->call_extension('weblog_standalone_insert_entry');
         if ($EXT->end_script === TRUE) {
             return;
         }
         //
         // -------------------------------------------
         if (!class_exists('Publish')) {
             require PATH_CP . 'cp.publish' . EXT;
         }
         $PB = new Publish();
         $PB->assign_cat_parent = $PREFS->ini('auto_assign_cat_parents') == 'n' ? FALSE : TRUE;
         return $PB->submit_new_entry(FALSE);
     }
     // END Insert
     /** ----------------------------------------
     		/**  Preview Entry
     		/** ----------------------------------------*/
     if ($IN->GBL('PRV', 'POST') == '') {
         $LANG->fetch_language_file('weblog');
         return $OUT->show_user_error('general', $LANG->line('weblog_no_preview_template'));
     }
     $FNS->clear_caching('all', $_POST['PRV']);
     // -------------------------------------------
     // 'weblog_standalone_preview_entry' hook.
     //  - Modify any of the POST data for a stand alone entry preview
     //
     $edata = $EXT->call_extension('weblog_standalone_preview_entry');
     if ($EXT->end_script === TRUE) {
         return;
     }
     //
     // -------------------------------------------
     require PATH_CORE . 'core.template' . EXT;
     global $TMPL;
     $TMPL = new Template();
     $preview = !$IN->GBL('PRV', 'POST') ? '' : $IN->GBL('PRV');
     if (strpos($preview, '/') === FALSE) {
         return FALSE;
     }
     $ex = explode("/", $preview);
     if (count($ex) != 2) {
         return FALSE;
     }
     $TMPL->run_template_engine($ex['0'], $ex['1']);
 }