예제 #1
0
 /**
  * @brief edits a card
  * @param integer $id id of card
  * @param Sabre\VObject\Component $card  vCard file
  * @return boolean true on success, otherwise an exception will be thrown
  */
 public static function edit($id, VObject\Component $card)
 {
     $oldcard = self::find($id);
     if (!$oldcard) {
         \OCP\Util::writeLog('contacts', __METHOD__ . ', id: ' . $id . ' not found.', \OCP\Util::DEBUG);
         throw new \Exception(App::$l10n->t('Could not find the vCard with ID.' . $id));
     }
     if (is_null($card)) {
         return false;
     }
     // NOTE: Owner checks are being made in the ajax files, which should be done
     // inside the lib files to prevent any redundancies with sharing checks
     $addressbook = Addressbook::find($oldcard['addressbookid']);
     if ($addressbook['userid'] != \OCP\User::getUser()) {
         $sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $oldcard['addressbookid'], \OCP\Share::FORMAT_NONE, null, true);
         $sharedContact = \OCP\Share::getItemSharedWithBySource('contact', $id, \OCP\Share::FORMAT_NONE, null, true);
         $addressbook_permissions = 0;
         $contact_permissions = 0;
         if ($sharedAddressbook) {
             $addressbook_permissions = $sharedAddressbook['permissions'];
         }
         if ($sharedContact) {
             $contact_permissions = $sharedEvent['permissions'];
         }
         $permissions = max($addressbook_permissions, $contact_permissions);
         if (!($permissions & \OCP\PERMISSION_UPDATE)) {
             throw new \Exception(App::$l10n->t('You do not have the permissions to edit this contact.'));
         }
     }
     App::loadCategoriesFromVCard($id, $card);
     $fn = isset($card->FN) ? $card->FN : '';
     $now = new \DateTime();
     $card->{'REV'} = $now->format(\DateTime::W3C);
     $data = $card->serialize();
     $stmt = \OCP\DB::prepare('UPDATE `*PREFIX*contacts_cards` SET `fullname` = ?,`carddata` = ?, `lastmodified` = ? WHERE `id` = ?');
     try {
         $result = $stmt->execute(array($fn, $data, time(), $id));
         if (\OC_DB::isError($result)) {
             \OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
             return false;
         }
     } catch (\Exception $e) {
         \OCP\Util::writeLog('contacts', __METHOD__ . ', exception: ' . $e->getMessage(), \OCP\Util::ERROR);
         \OCP\Util::writeLog('contacts', __METHOD__ . ', id' . $id, \OCP\Util::DEBUG);
         return false;
     }
     App::cacheThumbnail($oldcard['id']);
     App::updateDBProperties($id, $card);
     Addressbook::touch($oldcard['addressbookid']);
     \OC_Hook::emit('\\OCA\\Contacts\\VCard', 'post_updateVCard', $id);
     return true;
 }