Exemplo n.º 1
0
 public static function getBirthdayEvents($parameters)
 {
     $name = $parameters['calendar_id'];
     if (strpos($name, 'birthday_') != 0) {
         return;
     }
     $info = explode('_', $name);
     $aid = $info[1];
     Addressbook::find($aid);
     foreach (VCard::all($aid) as $contact) {
         try {
             $vcard = VObject\Reader::read($contact['carddata']);
         } catch (Exception $e) {
             continue;
         }
         $birthday = $vcard->BDAY;
         if ($birthday) {
             $date = new \DateTime($birthday);
             $vevent = VObject\Component::create('VEVENT');
             //$vevent->setDateTime('LAST-MODIFIED', new DateTime($vcard->REV));
             $vevent->add('DTSTART');
             $vevent->DTSTART->setDateTime($date, VObject\Property\DateTime::DATE);
             $vevent->add('DURATION', 'P1D');
             $vevent->{'UID'} = substr(md5(rand() . time()), 0, 10);
             // DESCRIPTION?
             $vevent->{'RRULE'} = 'FREQ=YEARLY';
             $title = str_replace('{name}', $vcard->FN, 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");
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Store feeds into the Contacts
  * @param array $feed 
  */
 public static function parseFeed($feed)
 {
     $userid = \OCP\User::getUser();
     foreach ($feed as $source) {
         $entry = Adapter::translateContact($source);
         if (isset($entry[self::CONTACT_GID]) && !empty($entry[self::CONTACT_GID])) {
             $oldContactId = self::findByGid($userid, $entry[self::CONTACT_GID]);
             if ($oldContactId) {
                 //If exists and should not be updated - skip
                 if (self::needUpdate($oldContactId)) {
                     $vcard = self::toVcard($entry);
                     \OCA\Contacts\VCard::edit($oldContactId, $vcard);
                 }
                 continue;
             }
         }
         $vcard = self::toVcard($entry);
         $bookid = self::getBookId($userid);
         \OCA\Contacts\VCard::add($bookid, $vcard);
     }
     \OCA\Contacts\App::getCategories();
 }
Exemplo n.º 3
0
            }
            break;
        case 'EMAIL':
        case 'TEL':
        case 'IMPP':
        case 'URL':
            debug('Setting element: (EMAIL/TEL/ADR)' . $element);
            $property->setValue($value);
            break;
        default:
            $vcard->{$name} = $value;
            break;
    }
    setParameters($property, $parameters, true);
    // Do checksum and be happy
    if (in_array($name, $multi_properties)) {
        $checksum = substr(md5($property->serialize()), 0, 8);
    }
}
//debug('New checksum: '.$checksum);
//$vcard->children[$line] = $property; ???
try {
    VCard::edit($id, $vcard);
} catch (Exception $e) {
    bailOut($e->getMessage());
}
if (in_array($name, $multi_properties)) {
    \OCP\JSON::success(array('data' => array('line' => $line, 'checksum' => $checksum, 'oldchecksum' => $_POST['checksum'], 'lastmodified' => App::lastModified($vcard)->format('U'))));
} else {
    \OCP\JSON::success(array('data' => array('lastmodified' => App::lastModified($vcard)->format('U'))));
}
Exemplo n.º 4
0
 public function start()
 {
     $request = $this->request;
     $response = new JSONResponse();
     $params = $this->request->urlParams;
     $app = new App($this->api->getUserId());
     $addressBook = Addressbook::find($params['addressbookid']);
     if (!$addressBook['permissions'] & \OCP\PERMISSION_CREATE) {
         $response->setStatus('403');
         $response->bailOut(App::$l10n->t('You do not have permissions to import into this address book.'));
         return $response;
     }
     $filename = isset($request->post['filename']) ? $request->post['filename'] : null;
     $progresskey = isset($request->post['progresskey']) ? $request->post['progresskey'] : null;
     if (is_null($filename)) {
         $response->bailOut(App::$l10n->t('File name missing from request.'));
         return $response;
     }
     if (is_null($progresskey)) {
         $response->bailOut(App::$l10n->t('Progress key missing from request.'));
         return $response;
     }
     $filename = strtr($filename, array('/' => '', "\\" => ''));
     if (\OC\Files\Filesystem::isFileBlacklisted($filename)) {
         $response->bailOut(App::$l10n->t('Attempt to access blacklisted file:') . $filename);
         return $response;
     }
     $view = \OCP\Files::getStorage('contacts');
     $file = $view->file_get_contents('/imports/' . $filename);
     $writeProgress = function ($pct) use($progresskey) {
         \OC_Cache::set($progresskey, $pct, 300);
     };
     $cleanup = function () use($view, $filename, $progresskey) {
         if (!$view->unlink('/imports/' . $filename)) {
             $response->debug('Unable to unlink /imports/' . $filename);
         }
         \OC_Cache::remove($progresskey);
     };
     $writeProgress('20');
     $nl = "\n";
     $file = str_replace(array("\r", "\n\n"), array("\n", "\n"), $file);
     $lines = explode($nl, $file);
     $inelement = false;
     $parts = array();
     $card = array();
     foreach ($lines as $line) {
         if (strtoupper(trim($line)) == 'BEGIN:VCARD') {
             $inelement = true;
         } elseif (strtoupper(trim($line)) == 'END:VCARD') {
             $card[] = $line;
             $parts[] = implode($nl, $card);
             $card = array();
             $inelement = false;
         }
         if ($inelement === true && trim($line) != '') {
             $card[] = $line;
         }
     }
     if (count($parts) === 0) {
         $response->bailOut(App::$l10n->t('No contacts found in: ') . $filename);
         $cleanup();
         return $response;
     }
     //import the contacts
     $writeProgress('40');
     $imported = 0;
     $failed = 0;
     $partially = 0;
     // TODO: Add a new group: "Imported at {date}"
     foreach ($parts as $part) {
         try {
             $vcard = VObject\Reader::read($part);
         } catch (VObject\ParseException $e) {
             try {
                 $vcard = VObject\Reader::read($part, VObject\Reader::OPTION_IGNORE_INVALID_LINES);
                 $partially += 1;
                 $response->debug('Import: Retrying reading card. Error parsing VCard: ' . $e->getMessage());
             } catch (\Exception $e) {
                 $failed += 1;
                 $response->debug('Import: skipping card. Error parsing VCard: ' . $e->getMessage());
                 continue;
                 // Ditch cards that can't be parsed by Sabre.
             }
         }
         try {
             if (VCard::add($params['addressbookid'], $vcard)) {
                 $imported += 1;
                 $writeProgress($imported);
             } else {
                 $failed += 1;
             }
         } catch (\Exception $e) {
             $response->debug('Error importing vcard: ' . $e->getMessage() . $nl . $vcard->serialize());
             $failed += 1;
         }
     }
     //done the import
     sleep(3);
     // Give client side a chance to read the progress.
     $response->setParams(array('addressbookid' => $params['addressbookid'], 'imported' => $imported, 'partially' => $partially, 'failed' => $failed));
     return $response;
 }
Exemplo n.º 5
0
 /**
  * @brief Get the last modification time.
  * @param OC_VObject|Sabre\VObject\Component|integer|null $contact
  * @returns DateTime | null
  */
 public static function lastModified($contact = null)
 {
     if (is_null($contact)) {
         // FIXME: This doesn't take shared address books into account.
         $sql = 'SELECT MAX(`lastmodified`) FROM `oc_contacts_cards`, `oc_contacts_addressbooks` ' . 'WHERE  `oc_contacts_cards`.`addressbookid` = `oc_contacts_addressbooks`.`id` AND ' . '`oc_contacts_addressbooks`.`userid` = ?';
         $stmt = \OCP\DB::prepare($sql);
         $result = $stmt->execute(array(\OCP\USER::getUser()));
         if (\OC_DB::isError($result)) {
             \OC_Log::write('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result), \OC_Log::ERROR);
             return null;
         }
         $lastModified = $result->fetchOne();
         if (!is_null($lastModified)) {
             return new \DateTime('@' . $lastModified);
         }
     } else {
         if (is_numeric($contact)) {
             $card = VCard::find($contact, array('lastmodified'));
             return $card ? new \DateTime('@' . $card['lastmodified']) : null;
         } elseif ($contact instanceof \OC_VObject || $contact instanceof VObject\Component) {
             return isset($contact->REV) ? \DateTime::createFromFormat(\DateTime::W3C, $contact->REV) : null;
         }
     }
 }
Exemplo n.º 6
0
 /**
  * @brief Get the last modification time.
  * @param OC_VObject|Sabre\VObject\Component|integer|null $contact
  * @returns DateTime | null
  */
 public static function lastModified($contact = null)
 {
     if (is_null($contact)) {
         $addressBooks = Addressbook::all(\OCP\User::getUser());
         $lastModified = 0;
         foreach ($addressBooks as $addressBook) {
             if (isset($addressBook['ctag']) and (int) $addressBook['ctag'] > $lastModified) {
                 $lastModified = $addressBook['ctag'];
             }
         }
         return new \DateTime('@' . $lastModified);
     } else {
         if (is_numeric($contact)) {
             $card = VCard::find($contact, array('lastmodified'));
             return $card ? new \DateTime('@' . $card['lastmodified']) : null;
         } elseif ($contact instanceof \OC_VObject || $contact instanceof VObject\Component) {
             return isset($contact->REV) ? \DateTime::createFromFormat(\DateTime::W3C, $contact->REV) : null;
         }
     }
 }
Exemplo n.º 7
0
 /**
  * @brief Get the last modification time.
  * @param OC_VObject|Sabre\VObject\Component|integer $contact
  * @returns DateTime | null
  */
 public static function lastModified($contact)
 {
     if (is_numeric($contact)) {
         $card = VCard::find($contact);
         return $card ? new \DateTime('@' . $card['lastmodified']) : null;
     } elseif ($contact instanceof \OC_VObject || $contact instanceof VObject\Component) {
         return isset($contact->REV) ? \DateTime::createFromFormat(\DateTime::W3C, $contact->REV) : null;
     }
 }
 /**
  * @param $id
  * @return mixed
  */
 public function delete($id)
 {
     try {
         $query = 'SELECT * FROM `*PREFIX*contacts_cards` WHERE `id` = ? AND `addressbookid` = ?';
         $stmt = \OCP\DB::prepare($query);
         $result = $stmt->execute(array($id, $this->id));
         if (\OCP\DB::isError($result)) {
             \OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
             return false;
         }
         if ($result->numRows() === 0) {
             \OCP\Util::writeLog('contacts', __METHOD__ . 'Contact with id ' . $id . 'doesn\'t belong to addressbook with id ' . $this->id, \OCP\Util::ERROR);
             return false;
         }
     } catch (\Exception $e) {
         \OCP\Util::writeLog('contacts', __METHOD__ . ', exception: ' . $e->getMessage(), \OCP\Util::ERROR);
         return false;
     }
     return VCard::delete($id);
 }