/** * @NoAdminRequired */ public function patch() { $params = $this->request->urlParams; $patch = $this->request->patch; $response = new JSONResponse(); $name = $patch['name']; $value = $patch['value']; $checksum = isset($patch['checksum']) ? $patch['checksum'] : null; $parameters = isset($patch['parameters']) ? $patch['parameters'] : null; $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); $contact = $addressBook->getChild($params['contactId']); if (!$contact) { return $response->setStatus(Http::STATUS_NOT_FOUND)->bailOut(App::$l10n->t('Couldn\'t find contact.')); } if (!$name) { return $response->setStatus(Http::STATUS_PRECONDITION_FAILED)->bailOut(App::$l10n->t('Property name is not set.')); } if (!$checksum && in_array($name, Properties::$multi_properties)) { return $response->setStatus(Http::STATUS_PRECONDITION_FAILED)->bailOut(App::$l10n->t('Property checksum is not set.')); } if (is_array($value)) { // NOTE: Important, otherwise the compound value will be // set in the order the fields appear in the form! ksort($value); } $result = array('contactId' => $params['contactId']); if ($checksum && in_array($name, Properties::$multi_properties)) { try { if (is_null($value)) { $contact->unsetPropertyByChecksum($checksum); } else { $checksum = $contact->setPropertyByChecksum($checksum, $name, $value, $parameters); $result['checksum'] = $checksum; } } catch (Exception $e) { return $response->setStatus(Http::STATUS_PRECONDITION_FAILED)->bailOut(App::$l10n->t('Information about vCard is incorrect. Please reload the page.')); } } elseif (!in_array($name, Properties::$multi_properties)) { if (is_null($value)) { unset($contact->{$name}); } else { if (!$contact->setPropertyByName($name, $value, $parameters)) { return $response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR)->bailOut(App::$l10n->t('Error updating contact')); } } } if (!$contact->save()) { return $response->bailOut(App::$l10n->t('Error saving contact to backend')); } $result['lastmodified'] = $contact->lastModified(); return $response->setData($result); }
/** * If an Exception is being caught, return a JSON error response with * a suitable status code * @param Controller $controller the controller that is being called * @param string $methodName the name of the method that will be called on * the controller * @param \Exception $exception the thrown exception * @return Response a Response object */ public function afterException($controller, $methodName, \Exception $exception) { // If there's no proper status code associated, set it to 500. $response = new JSONResponse(); if ($exception->getCode() < 100) { $response->setStatus(500); } else { $response->setStatus($exception->getCode()); } $response->setErrorMessage($exception->getMessage()); //$this->api->log(get_class($controller) . '->' . $methodName . ': ' . $exception->getMessage()); return $response; }
/** * If an Exception is being caught, return a JSON error response with * a suitable status code * @param Controller $controller the controller that is being called * @param string $methodName the name of the method that will be called on * the controller * @param \Exception $exception the thrown exception * @return Response a Response object */ public function afterException($controller, $methodName, \Exception $exception) { \OCP\Util::writeLog('contacts', __METHOD__ . ' method: ' . $methodName, \OCP\Util::DEBUG); // If there's no proper status code associated, set it to 500. $response = new JSONResponse(); if ($exception->getCode() < 100) { $response->setStatus(HttpStatus::STATUS_INTERNAL_SERVER_ERROR); } else { $response->setStatus($exception->getCode()); } $response->setErrorMessage($exception->getMessage()); \OCP\Util::logException('contacts', $exception); return $response; }
/** * @NoAdminRequired */ public function start() { $request = $this->request; $response = new JSONResponse(); $params = $this->request->urlParams; $app = new App($this->api->getUserId()); $addressBook = $app->getAddressBook($params['backend'], $params['addressBookId']); if (!$addressBook->hasPermission(\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'); $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; $file = $view->file_get_contents('/imports/' . $filename); \OC_FileProxy::$enabled = $proxyStatus; $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 $imported = 0; $failed = 0; $partially = 0; $processed = 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 { $vcard->validate(MyVCard::REPAIR | MyVCard::UPGRADE); } catch (\Exception $e) { \OCP\Util::writeLog('contacts', __METHOD__ . ' ' . 'Error validating vcard: ' . $e->getMessage(), \OCP\Util::ERROR); $failed += 1; } /** * TODO * - Check if a contact with identical UID exists. * - If so, fetch that contact and call $contact->mergeFromVCard($vcard); * - Increment $updated var (not present yet.) * - continue */ try { if ($addressBook->addChild($vcard)) { $imported += 1; } else { $failed += 1; } } catch (\Exception $e) { $response->debug('Error importing vcard: ' . $e->getMessage() . $nl . $vcard->serialize()); $failed += 1; } $processed += 1; $writeProgress($processed); } //done the import sleep(3); // Give client side a chance to read the progress. $response->setParams(array('backend' => $params['backend'], 'addressBookId' => $params['addressBookId'], 'imported' => $imported, 'partially' => $partially, 'failed' => $failed)); return $response; }
/** * @NoAdminRequired */ public function start() { $request = $this->request; $response = new JSONResponse(); $params = $this->request->urlParams; $app = new App(\OCP\User::getUser()); $addressBookId = $params['addressBookId']; $format = $params['importType']; $addressBook = $app->getAddressBook($params['backend'], $addressBookId); if (!$addressBook->hasPermission(\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'); $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; $file = $view->file_get_contents('/imports/' . $filename); \OC_FileProxy::$enabled = $proxyStatus; $importManager = new ImportManager(); $formatList = $importManager->getTypes(); $found = false; $parts = array(); foreach ($formatList as $formatName => $formatDisplayName) { if ($formatName == $format) { $parts = $importManager->importFile($view->getLocalFile('/imports/' . $filename), $formatName); $found = true; } } if (!$found) { // detect file type $mostLikelyName = ""; $mostLikelyValue = 0; $probability = $importManager->detectFileType($view->getLocalFile('/imports/' . $filename)); foreach ($probability as $probName => $probValue) { if ($probValue > $mostLikelyValue) { $mostLikelyName = $probName; $mostLikelyValue = $probValue; } } if ($mostLikelyValue > 0) { // found one (most likely...) $parts = $importManager->importFile($view->getLocalFile('/imports/' . $filename), $mostLikelyName); } } if ($parts) { //import the contacts $imported = 0; $failed = 0; $processed = 0; $total = count($parts); foreach ($parts as $part) { /** * TODO * - Check if a contact with identical UID exists. * - If so, fetch that contact and call $contact->mergeFromVCard($part); * - Increment $updated var (not present yet.) * - continue */ try { $id = $addressBook->addChild($part); if ($id) { $imported++; $favourites = $part->select('X-FAVOURITES'); foreach ($favourites as $favourite) { if ($favourite->getValue() == 'yes') { $this->tagMgr->addToFavorites($id); } } } else { $failed++; } } catch (\Exception $e) { $response->debug('Error importing vcard: ' . $e->getMessage() . $nl . $part->serialize()); $failed++; } $processed++; $this->writeProcess($processed, $total, $progresskey); } } else { $imported = 0; $failed = 0; $processed = 0; $total = 0; } $this->cleanup($view, $filename, $progresskey, $response); //done the import sleep(3); // Give client side a chance to read the progress. $response->setParams(array('backend' => $params['backend'], 'addressBookId' => $params['addressBookId'], 'importType' => $params['importType'], 'imported' => $imported, 'count' => $processed, 'total' => $total, 'failed' => $failed)); return $response; }
/** * @NoAdminRequired */ public function deleteChild() { $params = $this->request->urlParams; $response = new JSONResponse(); $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); $result = $addressBook->deleteChild($params['contactId']); if ($result === false) { throw new \Exception(App::$l10n->t('Error deleting contact'), 500); } return $response->setStatus('204'); }
/** * @NoAdminRequired */ public function deleteChild() { $params = $this->request->urlParams; $response = new JSONResponse(); $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); try { $result = $addressBook->deleteChild($params['contactId']); } catch (Exception $e) { return $response->bailOut($e->getMessage()); } if ($result === false) { return $response->bailOut(App::$l10n->t('Error deleting contact.')); } return $response->setStatus('204'); }