1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-01 13:24:10 +01:00
OwncloudContactsOfficial/import.php

179 lines
4.5 KiB
PHP
Raw Normal View History

2012-01-15 11:40:32 +01:00
<?php
/**
* Copyright (c) 2012 Georg Ehrke <ownclouddev at georgswebsite dot de>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
2012-01-15 11:47:35 +01:00
//check for addressbooks rights or create new one
2012-01-15 11:40:32 +01:00
ob_start();
2012-09-07 14:32:45 +02:00
2012-05-03 12:23:29 +02:00
OCP\JSON::checkLoggedIn();
2012-05-02 19:08:37 +02:00
OCP\App::checkAppEnabled('contacts');
OCP\JSON::callCheck();
session_write_close();
2012-01-15 11:40:32 +01:00
$nl = "\n";
2012-05-29 23:40:26 +02:00
global $progresskey;
$progresskey = 'contacts.import-' . (isset($_GET['progresskey'])?$_GET['progresskey']:'');
if (isset($_GET['progress']) && $_GET['progress']) {
echo OC_Cache::get($progresskey);
die;
}
function writeProgress($pct) {
global $progresskey;
OC_Cache::set($progresskey, $pct, 300);
2012-01-15 11:40:32 +01:00
}
writeProgress('10');
$view = null;
$inputfile = strtr($_POST['file'], array('/' => '', "\\" => ''));
if(OC\Files\Filesystem::isFileBlacklisted($inputfile)) {
OCP\JSON::error(array('data' => array('message' => 'Upload of blacklisted file: ' . $inputfile)));
exit();
}
if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
$view = OCP\Files::getStorage('contacts');
$file = $view->file_get_contents('/imports/' . $inputfile);
} else {
$file = \OC\Files\Filesystem::file_get_contents($_POST['path'] . '/' . $inputfile);
}
if(!$file) {
OCP\JSON::error(array('data' => array('message' => 'Import file was empty.')));
exit();
}
2012-07-20 17:09:03 +02:00
if(isset($_POST['method']) && $_POST['method'] == 'new') {
2012-10-25 03:34:12 +02:00
$id = OCA\Contacts\Addressbook::add(OCP\USER::getUser(),
2012-07-20 17:09:03 +02:00
$_POST['addressbookname']);
if(!$id) {
2012-07-20 17:09:03 +02:00
OCP\JSON::error(
array(
'data' => array('message' => 'Error creating address book.')
)
);
exit();
}
2012-10-25 03:34:12 +02:00
OCA\Contacts\Addressbook::setActive($id, 1);
2012-01-15 11:40:32 +01:00
}else{
$id = $_POST['id'];
if(!$id) {
2012-07-20 17:09:03 +02:00
OCP\JSON::error(
array(
'data' => array(
2012-09-07 14:32:45 +02:00
'message' => 'Error getting the ID of the address book.',
'file'=>OCP\Util::sanitizeHTML($inputfile)
2012-07-20 17:09:03 +02:00
)
)
);
exit();
}
2012-10-05 05:05:49 +02:00
try {
2012-10-25 03:34:12 +02:00
OCA\Contacts\Addressbook::find($id); // is owner access check
2012-10-05 05:05:49 +02:00
} catch(Exception $e) {
OCP\JSON::error(
array(
'data' => array(
'message' => $e->getMessage(),
'file'=>OCP\Util::sanitizeHTML($inputfile)
2012-10-05 05:05:49 +02:00
)
)
);
exit();
}
2012-01-15 11:40:32 +01:00
}
2012-01-15 11:47:35 +01:00
//analyse the contacts file
2012-05-29 23:40:26 +02:00
writeProgress('40');
2012-07-15 04:15:57 +02:00
$file = str_replace(array("\r","\n\n"), array("\n","\n"), $file);
2012-05-29 23:40:26 +02:00
$lines = explode($nl, $file);
2012-01-15 11:40:32 +01:00
$inelement = false;
$parts = array();
2012-05-29 23:40:26 +02:00
$card = array();
2012-09-07 15:21:03 +02:00
foreach($lines as $line) {
2012-07-20 17:09:03 +02:00
if(strtoupper(trim($line)) == 'BEGIN:VCARD') {
2012-05-29 23:40:26 +02:00
$inelement = true;
} elseif (strtoupper(trim($line)) == 'END:VCARD') {
$card[] = $line;
2012-05-29 23:40:26 +02:00
$parts[] = implode($nl, $card);
$card = array();
$inelement = false;
2012-01-15 11:40:32 +01:00
}
2012-05-29 23:40:26 +02:00
if ($inelement === true && trim($line) != '') {
$card[] = $line;
2012-01-15 11:40:32 +01:00
}
}
2012-05-29 23:40:26 +02:00
//import the contacts
writeProgress('70');
$imported = 0;
$failed = 0;
$partial = 0;
2012-05-29 23:40:26 +02:00
if(!count($parts) > 0) {
2012-07-20 17:09:03 +02:00
OCP\JSON::error(
array(
'data' => array(
'message' => 'No contacts to import in '
. OCP\Util::sanitizeHTML($inputfile).'. Please check if the file is corrupted.',
'file'=>OCP\Util::sanitizeHTML($inputfile)
2012-07-20 17:09:03 +02:00
)
)
);
2012-07-15 17:34:30 +02:00
if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
if(!$view->unlink('/imports/' . $inputfile)) {
2012-09-07 14:32:45 +02:00
OCP\Util::writeLog('contacts',
'Import: Error unlinking OC_FilesystemView ' . '/' . OCP\Util::sanitizeHTML($inputfile),
2012-07-20 17:09:03 +02:00
OCP\Util::ERROR);
2012-07-15 17:34:30 +02:00
}
}
exit();
}
2012-09-07 15:21:03 +02:00
foreach($parts as $part) {
2012-11-22 00:14:03 +01:00
try {
2012-11-23 01:18:36 +01:00
$vcard = Sabre\VObject\Reader::read($part);
} catch (Sabre\VObject\ParseException $e) {
try {
$vcard = Sabre\VObject\Reader::read($part, Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES);
$partial += 1;
OCP\Util::writeLog('contacts',
'Import: Retrying reading card. Error parsing VCard: ' . $e->getMessage(),
OCP\Util::ERROR);
} catch (Exception $e) {
$failed += 1;
OCP\Util::writeLog('contacts',
'Import: skipping card. Error parsing VCard: ' . $e->getMessage(),
OCP\Util::ERROR);
continue; // Ditch cards that can't be parsed by Sabre.
}
2012-01-15 11:40:32 +01:00
}
2012-05-29 23:40:26 +02:00
try {
2012-11-22 00:14:03 +01:00
OCA\Contacts\VCard::add($id, $vcard);
2012-05-29 23:40:26 +02:00
$imported += 1;
} catch (Exception $e) {
2012-09-07 14:32:45 +02:00
OCP\Util::writeLog('contacts',
2012-11-22 00:14:03 +01:00
'Error importing vcard: ' . $e->getMessage() . $nl . $vcard,
2012-07-20 17:09:03 +02:00
OCP\Util::ERROR);
2012-05-29 23:40:26 +02:00
$failed += 1;
}
2012-01-15 11:40:32 +01:00
}
//done the import
writeProgress('100');
2012-01-15 11:40:32 +01:00
sleep(3);
OC_Cache::remove($progresskey);
if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
if(!$view->unlink('/imports/' . $inputfile)) {
2012-09-07 14:32:45 +02:00
OCP\Util::writeLog('contacts',
'Import: Error unlinking OC_FilesystemView ' . '/' . $inputfile,
2012-07-20 17:09:03 +02:00
OCP\Util::ERROR);
}
}
2012-07-20 17:09:03 +02:00
OCP\JSON::success(
array(
'data' => array(
2012-09-07 14:32:45 +02:00
'imported'=>$imported,
'failed'=>$failed,
'file'=>OCP\Util::sanitizeHTML($inputfile),
2012-07-20 17:09:03 +02:00
)
)
);