Ejemplo n.º 1
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();
 }
Ejemplo n.º 2
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;
 }
 /**
  * @param $properties
  * @return mixed
  */
 public function createOrUpdate($properties)
 {
     $id = null;
     $vcard = null;
     if (array_key_exists('id', $properties)) {
         // TODO: test if $id belongs to this addressbook
         $id = $properties['id'];
         // TODO: Test $vcard
         $vcard = App::getContactVCard($properties['id']);
         foreach (array_keys($properties) as $name) {
             if (isset($vcard->{$name})) {
                 unset($vcard->{$name});
             }
         }
     } else {
         $vcard = \Sabre\VObject\Component::create('VCARD');
         $uid = substr(md5(rand() . time()), 0, 10);
         $vcard->add('UID', $uid);
         try {
             $id = VCard::add($this->id, $vcard, null, true);
         } catch (Exception $e) {
             \OCP\Util::writeLog('contacts', __METHOD__ . ' ' . $e->getMessage(), \OCP\Util::ERROR);
             return false;
         }
     }
     foreach ($properties as $name => $value) {
         switch ($name) {
             case 'ADR':
             case 'N':
                 if (is_array($value)) {
                     $property = \Sabre\VObject\Property::create($name);
                     $property->setParts($value);
                     $vcard->add($property);
                 } else {
                     $vcard->{$name} = $value;
                 }
                 break;
             case 'BDAY':
                 // TODO: try/catch
                 $date = new \DateTime($value);
                 $vcard->BDAY = $date->format('Y-m-d');
                 $vcard->BDAY->VALUE = 'DATE';
                 break;
             case 'EMAIL':
             case 'TEL':
             case 'IMPP':
                 // NOTE: We don't know if it's GTalk, Jabber etc. only the protocol
             // NOTE: We don't know if it's GTalk, Jabber etc. only the protocol
             case 'URL':
                 if (is_array($value)) {
                     foreach ($value as $val) {
                         $vcard->add($name, strip_tags($val));
                     }
                 } else {
                     $vcard->add($name, strip_tags($value));
                 }
             default:
                 $vcard->{$name} = $value;
                 break;
         }
     }
     try {
         VCard::edit($id, $vcard);
     } catch (Exception $e) {
         \OCP\Util::writeLog('contacts', __METHOD__ . ' ' . $e->getMessage(), \OCP\Util::ERROR);
         return false;
     }
     $asarray = VCard::structureContact($vcard);
     $asarray['id'] = $id;
     return $asarray;
 }