/**
  * Returns the list of addressbooks for a specific user.
  *
  * @param string $principalUri
  * @return array
  */
 public function getAddressBooksForUser($principalUri)
 {
     $uid = dav_compat_principal2uid($principalUri);
     $addressBooks = array();
     $books = q("SELECT id, ctag FROM %s%saddressbooks WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CARDDAV_NAMESPACE_PRIVATE, IntVal($uid), dbesc(CARDDAV_FRIENDICA_CONTACT));
     $ctag = $books[0]["ctag"];
     $addressBooks[] = array('id' => $books[0]["id"], 'uri' => "friendica", 'principaluri' => $principalUri, '{DAV:}displayname' => t("Friendica-Contacts"), '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => t("Your Friendica-Contacts"), '{http://calendarserver.org/ns/}getctag' => $ctag, '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data' => new Sabre_CardDAV_Property_SupportedAddressData());
     return $addressBooks;
 }
 /**
  * Returns the list of addressbooks for a specific user.
  *
  * @param string $principalUri
  * @return array
  */
 public function getAddressBooksForUser($principalUri)
 {
     $uid = dav_compat_principal2uid($principalUri);
     $addressBooks = array();
     $books = q("SELECT id, uri, displayname, principaluri, description, ctag FROM %s%saddressbooks_phone WHERE principaluri = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($principalUri));
     if (count($books) == 0) {
         q("INSERT INTO %s%saddressbooks_phone (uid, principaluri, displayname, uri, description, ctag) VALUES (%d, '%s', '%s', '%s', '%s', 1)", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $uid, dbesc($principalUri), 'Other', 'phone', 'Manually added contacts');
         $books = q("SELECT id, uri, displayname, principaluri, description, ctag FROM %s%saddressbooks_phone WHERE principaluri = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($principalUri));
     }
     foreach ($books as $row) {
         $addressBooks[] = array('id' => CARDDAV_NAMESPACE_PHONECONTACTS . "-" . $row['id'], 'uri' => $row['uri'], 'principaluri' => $row['principaluri'], '{DAV:}displayname' => $row['displayname'], '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], '{http://calendarserver.org/ns/}getctag' => $row['ctag'], '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data' => new Sabre_CardDAV_Property_SupportedAddressData());
     }
     return $addressBooks;
 }
 /**
  * Returns the list of addressbooks for a specific user.
  *
  * @param string $principalUri
  * @return array
  */
 public function getAddressBooksForUser($principalUri)
 {
     $uid = dav_compat_principal2uid($principalUri);
     $addressBooks = array();
     $books = q("SELECT ctag FROM %s%saddressbooks_community WHERE uid = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($uid));
     if (count($books) == 0) {
         q("INSERT INTO %s%saddressbooks_community (uid, ctag) VALUES (%d, 1)", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($uid));
         $ctag = 1;
     } else {
         $ctag = $books[0]["ctag"];
     }
     $addressBooks[] = array('id' => CARDDAV_NAMESPACE_COMMUNITYCONTACTS . "-" . $uid, 'uri' => "friendica", 'principaluri' => $principalUri, '{DAV:}displayname' => t("Friendica-Contacts"), '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => t("Your Friendica-Contacts"), '{http://calendarserver.org/ns/}getctag' => $ctag, '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}supported-address-data' => new Sabre_CardDAV_Property_SupportedAddressData());
     return $addressBooks;
 }
 /**
  * Creates a new address book
  *
  * @param string $principalUri
  * @param string $url Just the 'basename' of the url.
  * @param array $properties
  * @throws Sabre_DAV_Exception_BadRequest
  * @return void
  */
 public function createAddressBook($principalUri, $url, array $properties)
 {
     $uid = dav_compat_principal2uid($principalUri);
     $values = array('displayname' => null, 'description' => null, 'principaluri' => $principalUri, 'uri' => $url);
     foreach ($properties as $property => $newValue) {
         switch ($property) {
             case '{DAV:}displayname':
                 $values['displayname'] = $newValue;
                 break;
             case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description':
                 $values['description'] = $newValue;
                 break;
             default:
                 throw new Sabre_DAV_Exception_BadRequest('Unknown property: ' . $property);
         }
     }
     q("INSERT INTO %s%saddressbooks (`uri`, `displayname`, `description`, `namespace`, `namespace_id`, `ctag`) VALUES ('%s', '%s', '%s', %d, %d, 1)", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($values["uri"]), dbesc($values["displayname"]), dbesc($values["description"]), CARDDAV_NAMESPACE_PRIVATE, IntVal($uid));
 }
 /**
  * Creates a new calendar for a principal.
  *
  * If the creation was a success, an id must be returned that can be used to reference
  * this calendar in other methods, such as updateCalendar.
  *
  * @param string $principalUri
  * @param string $calendarUri
  * @param array $properties
  * @throws Sabre_DAV_Exception|Sabre_DAV_Exception_Conflict
  * @return string|void
  */
 public function createCalendar($principalUri, $calendarUri, array $properties)
 {
     $uid = dav_compat_principal2uid($principalUri);
     $r = q("SELECT * FROM %s%scalendars WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CALDAV_NAMESPACE_PRIVATE, $uid, dbesc($calendarUri));
     if (count($r) > 0) {
         throw new Sabre_DAV_Exception_Conflict("A calendar with this URI already exists");
     }
     $keys = array("`namespace`", "`namespace_id`", "`ctag`", "`uri`");
     $vals = array(CALDAV_NAMESPACE_PRIVATE, IntVal($uid), 1, "'" . dbesc($calendarUri) . "'");
     // Default value
     $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set';
     $has_vevent = $has_vtodo = 1;
     if (isset($properties[$sccs])) {
         if (!$properties[$sccs] instanceof Sabre_CalDAV_Property_SupportedCalendarComponentSet) {
             throw new Sabre_DAV_Exception('The ' . $sccs . ' property must be of type: Sabre_CalDAV_Property_SupportedCalendarComponentSet');
         }
         $v = $properties[$sccs]->getValue();
         $has_vevent = $has_vtodo = 0;
         foreach ($v as $w) {
             if (mb_strtolower($w) == "vevent") {
                 $has_vevent = 1;
             }
             if (mb_strtolower($w) == "vtodo") {
                 $has_vtodo = 1;
             }
         }
     }
     $keys[] = "`has_vevent`";
     $keys[] = "`has_vtodo`";
     $vals[] = $has_vevent;
     $vals[] = $has_vtodo;
     foreach ($this->propertyMap as $xmlName => $dbName) {
         if (isset($properties[$xmlName])) {
             $keys[] = "`{$dbName}`";
             $vals[] = "'" . dbesc($properties[$xmlName]) . "'";
         }
     }
     $sql = sprintf("INSERT INTO %s%scalendars (" . implode(', ', $keys) . ") VALUES (" . implode(', ', $vals) . ")", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
     q($sql);
     $x = q("SELECT id FROM %s%scalendars WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CALDAV_NAMESPACE_PRIVATE, $uid, $calendarUri);
     return $x[0]["id"];
 }