1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-04 15:24:35 +01:00
OwncloudContactsOfficial/lib/controller/importcontroller.php

309 lines
9.2 KiB
PHP
Raw Normal View History

<?php
/**
* @author Thomas Tanghus
2014-01-26 00:40:22 +01:00
* @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
*
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Contacts\Controller;
2013-09-24 13:39:45 +02:00
use OCA\Contacts\App,
2013-09-17 18:46:59 +02:00
OCA\Contacts\JSONResponse,
OCA\Contacts\Controller,
2013-09-20 16:24:29 +02:00
Sabre\VObject,
2014-01-22 17:46:58 +01:00
OCA\Contacts\VObject\VCard as MyVCard,
2013-09-20 16:24:29 +02:00
OCA\Contacts\ImportManager;
/**
* Controller importing contacts
*/
2013-09-17 18:46:59 +02:00
class ImportController extends Controller {
/**
2013-09-24 13:39:45 +02:00
* @NoAdminRequired
*/
public function upload() {
$request = $this->request;
$params = $this->request->urlParams;
$addressBookId = $params['addressBookId'];
$format = $params['importType'];
$response = new JSONResponse();
$view = \OCP\Files::getStorage('contacts');
if(!$view->file_exists('imports')) {
$view->mkdir('imports');
}
if (!isset($request->files['file'])) {
$response->bailOut(App::$l10n->t('No file was uploaded. Unknown error'));
return $response;
}
$file=$request->files['file'];
if($file['error'] !== UPLOAD_ERR_OK) {
2013-12-13 09:13:41 +01:00
$error = $file['error'];
$errors = array(
UPLOAD_ERR_OK => App::$l10n->t("There is no error, the file uploaded with success"),
UPLOAD_ERR_INI_SIZE => App::$l10n->t("The uploaded file exceeds the upload_max_filesize directive in php.ini")
.ini_get('upload_max_filesize'),
UPLOAD_ERR_FORM_SIZE => App::$l10n->t("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"),
UPLOAD_ERR_PARTIAL => App::$l10n->t("The uploaded file was only partially uploaded"),
UPLOAD_ERR_NO_FILE => App::$l10n->t("No file was uploaded"),
UPLOAD_ERR_NO_TMP_DIR => App::$l10n->t('Missing a temporary folder'),
UPLOAD_ERR_CANT_WRITE => App::$l10n->t('Failed to write to disk'),
);
$response->bailOut($errors[$error]);
return $response;
}
$maxUploadFilesize = \OCP\Util::maxUploadFilesize('/');
$maxHumanFilesize = \OCP\Util::humanFileSize($maxUploadFilesize);
$totalSize = $file['size'];
if ($maxUploadFilesize >= 0 and $totalSize > $maxUploadFilesize) {
2013-09-20 16:24:29 +02:00
$response->bailOut(App::$l10n->t('Not enough storage available'));
return $response;
}
$tmpname = $file['tmp_name'];
$filename = strtr($file['name'], array('/' => '', "\\" => ''));
if(is_uploaded_file($tmpname)) {
if(\OC\Files\Filesystem::isFileBlacklisted($filename)) {
$response->bailOut(App::$l10n->t('Attempt to upload blacklisted file:') . $filename);
2013-09-20 16:24:29 +02:00
return $response;
}
$content = file_get_contents($tmpname);
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
if($view->file_put_contents('/imports/'.$filename, $content)) {
\OC_FileProxy::$enabled = $proxyStatus;
$progresskey = 'contacts-import-' . rand();
$response->setParams(
array(
'filename'=>$filename,
'progresskey' => $progresskey,
'backend' => $params['backend'],
'addressBookId' => $params['addressBookId'],
'importType' => $format
)
);
} else {
\OC_FileProxy::$enabled = $proxyStatus;
$response->bailOut(App::$l10n->t('Error uploading contacts to storage.'));
return $response;
}
} else {
$response->bailOut('Temporary file: \''.$tmpname.'\' has gone AWOL?');
return $response;
}
return $response;
}
/**
2013-09-24 13:39:45 +02:00
* @NoAdminRequired
*/
public function prepare() {
$request = $this->request;
$params = $this->request->urlParams;
2013-09-20 16:24:29 +02:00
$addressBookId = $params['addressBookId'];
$format = $params['importType'];
$response = new JSONResponse();
$filename = $request->post['filename'];
$path = $request->post['path'];
$view = \OCP\Files::getStorage('contacts');
if(!$view->file_exists('imports')) {
$view->mkdir('imports');
}
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
2013-11-28 12:18:05 +01:00
$content = \OC_Filesystem::file_get_contents($path . '/' . $filename);
//$content = file_get_contents('oc://' . $path . '/' . $filename);
if($view->file_put_contents('/imports/' . $filename, $content)) {
\OC_FileProxy::$enabled = $proxyStatus;
$progresskey = 'contacts-import-' . rand();
$response->setParams(
array(
'filename'=>$filename,
'progresskey' => $progresskey,
'backend' => $params['backend'],
2013-09-20 16:24:29 +02:00
'addressBookId' => $params['addressBookId'],
'importType' => $params['importType']
)
);
} else {
\OC_FileProxy::$enabled = $proxyStatus;
$response->bailOut(App::$l10n->t('Error moving file to imports folder.'));
}
return $response;
}
/**
2013-09-24 13:39:45 +02:00
* @NoAdminRequired
*/
public function start() {
$request = $this->request;
$response = new JSONResponse();
$params = $this->request->urlParams;
$app = new App($this->api->getUserId());
2013-09-20 16:24:29 +02:00
$addressBookId = $params['addressBookId'];
$format = $params['importType'];
2013-09-20 16:24:29 +02:00
$addressBook = $app->getAddressBook($params['backend'], $addressBookId);
if(!$addressBook->hasPermission(\OCP\PERMISSION_CREATE)) {
2013-05-18 00:07:58 +02:00
$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, $total) use ($progresskey) {
\OC_Cache::set($progresskey, $pct, 300);
\OC_Cache::set($progresskey.'_total', $total, 300);
};
2014-03-18 15:15:08 +01:00
$cleanup = function() use ($view, $filename, $progresskey, $response) {
if(!$view->unlink('/imports/' . $filename)) {
$response->debug('Unable to unlink /imports/' . $filename);
}
\OC_Cache::remove($progresskey);
\OC_Cache::remove($progresskey.'_total');
};
2013-09-20 16:24:29 +02:00
$importManager = new ImportManager();
2013-09-20 16:24:29 +02:00
$formatList = $importManager->getTypes();
$found = false;
$parts = array();
2013-09-20 16:24:29 +02:00
foreach ($formatList as $formatName => $formatDisplayName) {
if ($formatName == $format) {
$parts = $importManager->importFile($view->getLocalFile('/imports/' . $filename), $formatName);
$found = true;
}
}
2013-09-20 16:24:29 +02:00
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);
}
}
2013-09-20 16:24:29 +02:00
if ($parts) {
//import the contacts
$imported = 0;
$failed = 0;
$processed = 0;
$total = count($parts);
2014-03-09 15:03:21 +01:00
2013-09-20 16:24:29 +02:00
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 {
2013-09-20 16:24:29 +02:00
$id = $addressBook->addChild($part);
if($id) {
$imported++;
$favourites = $part->select('X-FAVOURITES');
foreach ($favourites as $favourite) {
if ($favourite->getValue() == 'yes') {
$tagMgr = $this->server->getTagManager()->load('contact');
$tagMgr->addToFavorites($id);
}
}
} else {
$failed++;
}
} catch (\Exception $e) {
2013-09-20 16:24:29 +02:00
$response->debug('Error importing vcard: ' . $e->getMessage() . $nl . $part->serialize());
$failed++;
}
2013-09-20 16:24:29 +02:00
$processed++;
$writeProgress($processed, $total);
}
2013-09-20 16:24:29 +02:00
} else {
$imported = 0;
$failed = 0;
$processed = 0;
$total = 0;
}
2013-09-20 16:24:29 +02:00
$cleanup();
//done the import
sleep(3); // Give client side a chance to read the progress.
$response->setParams(
array(
'backend' => $params['backend'],
'addressBookId' => $params['addressBookId'],
2013-09-20 16:24:29 +02:00
'importType' => $params['importType'],
'imported' => $imported,
'count' => $processed,
'total' => $total,
'failed' => $failed,
)
);
return $response;
}
/**
2013-09-24 13:39:45 +02:00
* @NoAdminRequired
*/
public function status() {
$request = $this->request;
$response = new JSONResponse();
2013-06-25 21:44:23 +02:00
$progresskey = isset($request->get['progresskey']) ? $request->get['progresskey'] : null;
if(is_null($progresskey)) {
$response->bailOut(App::$l10n->t('Progress key missing from request.'));
return $response;
}
error_log("progresskey: ".\OC_Cache::get($progresskey)." total: ".\OC_Cache::get($progresskey.'_total') );
$response->setParams(array('progress' => \OC_Cache::get($progresskey), 'total' => \OC_Cache::get($progresskey.'_total') ));
return $response;
}
2013-09-20 16:24:29 +02:00
}