Esempio n. 1
0
</script>

<script id="contactsImportTemplate" class="hidden" type="text/template">
<div id="contacts-import-div" class="contacts-import-class">
<p id="contacts-import-into-p">
	<label for="contacts-import-into"><?php p($l->t('Address book')); ?></label>
	<select id="contacts-import-into">
		<option value="-1"><?php p($l->t('Import into...')); ?></option>
	</select>
</p>
<p id="contacts-import-format-p">
	<label for="contacts-import-into"><?php p($l->t('Format')); ?></label>
	<select id="contacts-import-format">
		<option value="automatic"><?php p($l->t('Automatic format')); ?></option>
		<?php
		$importManager = new ImportManager();
		$types = $importManager->getTypes();
		foreach ($types as $id => $label) {
			echo "<option value=\"$id\">$label</option>";
		}
		?>
	</select>
</p>
<p id="contacts-import-upload" class="hidden">
	<input id="contacts-import-upload-start" class="tooltipped" title="<?php p($l->t('Select file...')); ?>" type="file" name="file" disabled />
</p>
<p id="contacts-import-status">
	<div class="import-status">
		<label id="import-status-text"></label>
		<div id="import-status-progress"></div>
	</div>
 /**
  * @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;
 }