/** * @brief Gets the VCard as an OC_VObject * @returns The card or null if the card could not be parsed. */ public static function getContactVCard($id) { $card = self::getContactObject($id); $vcard = OC_VObject::parse($card['carddata']); // Try to fix cards with missing 'N' field from pre ownCloud 4. Hot damn, this is ugly... if (!is_null($vcard) && !$vcard->__isset('N')) { $version = OCP\App::getAppVersion('contacts'); if ($version >= 5) { OCP\Util::writeLog('contacts', 'OC_Contacts_App::getContactVCard. Deprecated check for missing N field', OCP\Util::DEBUG); } OCP\Util::writeLog('contacts', 'getContactVCard, Missing N field', OCP\Util::DEBUG); if ($vcard->__isset('FN')) { OCP\Util::writeLog('contacts', 'getContactVCard, found FN field: ' . $vcard->__get('FN'), OCP\Util::DEBUG); $n = implode(';', array_reverse(array_slice(explode(' ', $vcard->__get('FN')), 0, 2))) . ';;;'; $vcard->setString('N', $n); OC_Contacts_VCard::edit($id, $vcard); } else { // Else just add an empty 'N' field :-P $vcard->setString('N', 'Unknown;Name;;;'); } } if (!is_null($vcard) && !isset($vcard->REV)) { $rev = new DateTime('@' . $card['lastmodified']); $vcard->setString('REV', $rev->format(DateTime::W3C)); } return $vcard; }
/** * @brief exports an event and convert all times to UTC * @param integer $id id of the event * @return string */ private static function event($id) { $event = OC_Calendar_Object::find($id); $return = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:ownCloud Calendar " . OCP\App::getAppVersion('calendar') . "\nX-WR-CALNAME:" . $event['summary'] . "\n"; $return .= self::generateEvent($event); $return .= "END:VCALENDAR"; return $return; }
/** * @brief Adds a card * @param integer $aid Addressbook id * @param OC_VObject $card vCard file * @param string $uri the uri of the card, default based on the UID * @return insertid on success or null if no card. */ public static function add($aid, OC_VObject $card, $uri = null, $isnew = false) { if (is_null($card)) { OCP\Util::writeLog('contacts', 'OC_Contacts_VCard::add. No vCard supplied', OCP\Util::ERROR); return null; } if (!$isnew) { OC_Contacts_App::loadCategoriesFromVCard($card); self::updateValuesFromAdd($aid, $card); } $card->setString('VERSION', '3.0'); // Add product ID is missing. $prodid = trim($card->getAsString('PRODID')); if (!$prodid) { $appinfo = OCP\App::getAppInfo('contacts'); $appversion = OCP\App::getAppVersion('contacts'); $prodid = '-//ownCloud//NONSGML ' . $appinfo['name'] . ' ' . $appversion . '//EN'; $card->setString('PRODID', $prodid); } $fn = $card->getAsString('FN'); if (empty($fn)) { $fn = ''; } if (!$uri) { $uid = $card->getAsString('UID'); $uri = $uid . '.vcf'; } $data = $card->serialize(); $stmt = OCP\DB::prepare('INSERT INTO *PREFIX*contacts_cards (addressbookid,fullname,carddata,uri,lastmodified) VALUES(?,?,?,?,?)'); $result = $stmt->execute(array($aid, $fn, $data, $uri, time())); $newid = OCP\DB::insertid('*PREFIX*contacts_cards'); OC_Contacts_Addressbook::touch($aid); return $newid; }
private function createVCalendar($vobject) { if (is_object($vobject)) { $vobject = @$vobject->serialize(); } $vcalendar = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:ownCloud Calendar " . OCP\App::getAppVersion('calendar') . "\n"; $vcalendar .= $vobject; $vcalendar .= "END:VCALENDAR"; return $vcalendar; }
/** * @brief Adds a card * @param $aid integer Addressbook id * @param $card OC_VObject vCard file * @param $uri string the uri of the card, default based on the UID * @param $isChecked boolean If the vCard should be checked for validity and version. * @return insertid on success or false. */ public static function add($aid, OC_VObject $card, $uri = null, $isChecked = false) { if (is_null($card)) { OCP\Util::writeLog('contacts', 'OC_Contacts_VCard::add. No vCard supplied', OCP\Util::ERROR); return null; } $addressbook = OC_Contacts_Addressbook::find($aid); if ($addressbook['userid'] != OCP\User::getUser()) { $sharedAddressbook = OCP\Share::getItemSharedWithBySource('addressbook', $aid); if (!$sharedAddressbook || !($sharedAddressbook['permissions'] & OCP\Share::PERMISSION_CREATE)) { throw new Exception(OC_Contacts_App::$l10n->t('You do not have the permissions to add contacts to this addressbook.')); } } if (!$isChecked) { OC_Contacts_App::loadCategoriesFromVCard($card); self::updateValuesFromAdd($aid, $card); } $card->setString('VERSION', '3.0'); // Add product ID is missing. $prodid = trim($card->getAsString('PRODID')); if (!$prodid) { $appinfo = OCP\App::getAppInfo('contacts'); $appversion = OCP\App::getAppVersion('contacts'); $prodid = '-//ownCloud//NONSGML ' . $appinfo['name'] . ' ' . $appversion . '//EN'; $card->setString('PRODID', $prodid); } $fn = $card->getAsString('FN'); if (empty($fn)) { $fn = ''; } if (!$uri) { $uid = $card->getAsString('UID'); $uri = $uid . '.vcf'; } $data = $card->serialize(); $stmt = OCP\DB::prepare('INSERT INTO `*PREFIX*contacts_cards` (`addressbookid`,`fullname`,`carddata`,`uri`,`lastmodified`) VALUES(?,?,?,?,?)'); try { $result = $stmt->execute(array($aid, $fn, $data, $uri, time())); } catch (Exception $e) { OCP\Util::writeLog('contacts', __METHOD__ . ', exception: ' . $e->getMessage(), OCP\Util::ERROR); OCP\Util::writeLog('contacts', __METHOD__ . ', aid: ' . $aid . ' uri' . $uri, OCP\Util::DEBUG); return false; } $newid = OCP\DB::insertid('*PREFIX*contacts_cards'); OC_Contacts_Addressbook::touch($aid); OC_Hook::emit('OC_Contacts_VCard', 'post_createVCard', $newid); return $newid; }
public static function getBirthdayEvents($parameters) { $name = $parameters['calendar_id']; if (strpos($name, 'birthday_') != 0) { return; } $info = explode('_', $name); $aid = $info[1]; OC_Contacts_App::getAddressbook($aid); foreach (OC_Contacts_VCard::all($aid) as $card) { $vcard = OC_VObject::parse($card['carddata']); if (!$vcard) { continue; } $birthday = $vcard->BDAY; if ($birthday) { $date = new DateTime($birthday); $vevent = new OC_VObject('VEVENT'); //$vevent->setDateTime('LAST-MODIFIED', new DateTime($vcard->REV)); $vevent->setDateTime('DTSTART', $date, Sabre_VObject_Element_DateTime::DATE); $vevent->setString('DURATION', 'P1D'); $vevent->setString('UID', substr(md5(rand() . time()), 0, 10)); // DESCRIPTION? $vevent->setString('RRULE', 'FREQ=YEARLY'); $title = str_replace('{name}', $vcard->getAsString('FN'), OC_Contacts_App::$l10n->t('{name}\'s Birthday')); $parameters['events'][] = array('id' => 0, 'vevent' => $vevent, 'repeating' => true, 'summary' => $title, 'calendardata' => "BEGIN:VCALENDAR\nVERSION:2.0\n" . "PRODID:ownCloud Contacts " . OCP\App::getAppVersion('contacts') . "\n" . $vevent->serialize() . "END:VCALENDAR"); } } }