/**
  * Extracts ical properties and creates calendar.
  *
  * @see database_driver::create_calendar()
  */
 public function create_calendar($prop)
 {
     $props['user'] = $prop['ical_user'];
     $props['pass'] = $prop['ical_pass'];
     $props['url'] = str_ireplace('webcal://', 'http://', self::_encode_url($prop['ical_url']));
     $props['sync'] = $prop['sync'];
     // Check connection and handle redirects
     $props['url'] = $prop['url'] = $this->_check_connection($props);
     if (!$props['url']) {
         return false;
     }
     $prop['readonly'] = $this->readonly ? 1 : 0;
     if (!isset($props['color'])) {
         $props['color'] = 'cc0000';
     }
     $result = false;
     if (($obj_id = parent::create_calendar($prop)) !== false) {
         $result = $this->_set_ical_props($obj_id, self::OBJ_TYPE_ICAL, $props);
     }
     // Re-read calendars to internal buffer.
     $this->_read_calendars();
     // Initial sync of newly created calendars.
     $this->_init_sync_clients(array($obj_id));
     $this->_sync_calendar($obj_id);
     return $result;
 }
 /**
  * Extracts caldav properties and creates calendar.
  *
  * @see database_driver::create_calendar()
  */
 public function create_calendar($prop)
 {
     $result = false;
     $props = $prop;
     $props['url'] = self::_encode_url($prop['caldav_url']);
     if (!isset($props['color'])) {
         $props['color'] = 'cc0000';
     }
     $props['user'] = $prop['caldav_user'];
     $props['pass'] = $prop['caldav_pass'];
     $pwd_expanded_props = $props;
     $this->_expand_pass($pwd_expanded_props);
     if ($redirect = $this->_check_redirection($pwd_expanded_props)) {
         $pwd_expanded_props['url'] = $props['url'] = $prop['url'] = $redirect;
     }
     if (!$props['url']) {
         return false;
     }
     if (!$this->_check_connection($pwd_expanded_props) && $pwd_expanded_props['pass'] != '***TOKEN***') {
         return false;
     }
     if ($prop['autodiscover']) {
         $calendars = $this->autodiscover_calendars($pwd_expanded_props);
     } else {
         $calendars = array(0 => array('name' => $props['name'], 'href' => $props['url'], 'color' => $props['color']));
     }
     foreach ($calendars as $idx => $calendar) {
         $removed = $this->rc->config->get('calendar_caldavs_removed', array());
         $props['url'] = self::_encode_url($props['url']);
         if (isset($removed[slashify($props['url'])])) {
             unset($calendars[$idx]);
             if (!empty($calendars) || $this->rc->action == '') {
                 continue;
             } else {
                 $calendars[0] = $pwd_expanded_props;
                 $calendars[0]['href'] = $props['url'];
             }
         }
         $result = $this->rc->db->query("SELECT * FROM " . $this->_get_table($this->db_calendars_caldav_props) . " WHERE url LIKE ?", $calendar['href']);
         $result = $this->rc->db->fetch_assoc($result);
         if (is_array($result)) {
             $result = $this->rc->db->query("SELECT calendar_id FROM " . $this->_get_table($this->db_calendars) . " WHERE user_id = ? and calendar_id = ?", $this->rc->user->ID, $result['obj_id']);
             $result = $this->rc->db->fetch_assoc($result);
             if (is_array($result)) {
                 unset($calendars[$idx]);
             }
         }
     }
     $cal_ids = array();
     if (sizeof($calendars) > 0) {
         $result = true;
         foreach ($calendars as $calendar) {
             $props['url'] = self::_encode_url($calendar['href']);
             $props['name'] = $calendar['name'];
             $props['color'] = $calendar['color'];
             $props['sync'] = $calendar['sync'] ? $calendar['sync'] : $props['sync'];
             $props['authtype'] = $calendar['authtype'] ? $calendar['authtype'] : $props['authtype'];
             if (($obj_id = parent::create_calendar($props)) !== false) {
                 $result = $result && $this->_set_caldav_props($obj_id, self::OBJ_TYPE_VCAL, $props, 'create_calendar');
                 array_push($cal_ids, $obj_id);
             }
         }
     } else {
         // Fallback: Assume given URL as resource to a calendar.
         if (!isset($removed[slashify($props['url'])])) {
             if (($obj_id = parent::create_calendar($props)) !== false) {
                 $result = $this->_set_caldav_props($obj_id, self::OBJ_TYPE_VCAL, $props, 'create_calendar');
                 array_push($cal_ids, $obj_id);
             }
         } else {
             return true;
         }
     }
     // Re-read calendars to internal buffer.
     $this->_read_calendars();
     // Initial sync of newly created calendars.
     if (!empty($cal_ids)) {
         $this->_init_sync_clients($cal_ids);
         foreach ($cal_ids as $cal_id) {
             $this->_sync_calendar($cal_id, true);
         }
     }
     return $result ? $obj_id : $result;
 }