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-04-17 19:31:29 +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');
|
2012-06-04 17:42:59 +02:00
|
|
|
session_write_close();
|
|
|
|
|
2012-01-15 11:40:32 +01:00
|
|
|
$nl = "\n";
|
2012-05-29 23:40:26 +02:00
|
|
|
|
2012-06-04 17:42:59 +02:00
|
|
|
global $progresskey;
|
2012-06-11 22:12:06 +02:00
|
|
|
$progresskey = 'contacts.import-' . (isset($_GET['progresskey'])?$_GET['progresskey']:'');
|
2012-06-04 17:42:59 +02:00
|
|
|
|
|
|
|
if (isset($_GET['progress']) && $_GET['progress']) {
|
|
|
|
echo OC_Cache::get($progresskey);
|
|
|
|
die;
|
|
|
|
}
|
2012-05-13 23:24:04 +02:00
|
|
|
|
|
|
|
function writeProgress($pct) {
|
2012-06-04 17:42:59 +02:00
|
|
|
global $progresskey;
|
|
|
|
OC_Cache::set($progresskey, $pct, 300);
|
2012-01-15 11:40:32 +01:00
|
|
|
}
|
2012-05-13 23:24:04 +02:00
|
|
|
writeProgress('10');
|
2012-04-24 01:43:21 +02:00
|
|
|
$view = $file = null;
|
|
|
|
if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
|
2012-05-19 10:44:08 +02:00
|
|
|
$view = OCP\Files::getStorage('contacts');
|
2012-04-24 01:43:21 +02:00
|
|
|
$file = $view->file_get_contents('/' . $_POST['file']);
|
|
|
|
} else {
|
|
|
|
$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']);
|
|
|
|
}
|
2012-04-26 03:20:28 +02:00
|
|
|
if(!$file) {
|
2012-05-03 12:23:29 +02:00
|
|
|
OCP\JSON::error(array('message' => 'Import file was empty.'));
|
2012-04-26 03:20:28 +02:00
|
|
|
exit();
|
|
|
|
}
|
2012-04-24 01:43:21 +02:00
|
|
|
if(isset($_POST['method']) && $_POST['method'] == 'new'){
|
2012-05-01 18:50:31 +02:00
|
|
|
$id = OC_Contacts_Addressbook::add(OCP\USER::getUser(), $_POST['addressbookname']);
|
2012-05-13 23:24:04 +02:00
|
|
|
if(!$id) {
|
|
|
|
OCP\JSON::error(array('message' => 'Error creating address book.'));
|
|
|
|
exit();
|
|
|
|
}
|
2012-01-15 11:40:32 +01:00
|
|
|
OC_Contacts_Addressbook::setActive($id, 1);
|
|
|
|
}else{
|
|
|
|
$id = $_POST['id'];
|
2012-05-13 23:24:04 +02:00
|
|
|
if(!$id) {
|
|
|
|
OCP\JSON::error(array('message' => 'Error getting the ID of the address book.'));
|
|
|
|
exit();
|
|
|
|
}
|
2012-02-11 21:48:45 +01:00
|
|
|
OC_Contacts_App::getAddressbook($id); // is owner access check
|
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');
|
|
|
|
$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();
|
|
|
|
foreach($lines as $line){
|
|
|
|
if(strtoupper(trim($line)) == 'BEGIN:VCARD'){
|
|
|
|
$inelement = true;
|
|
|
|
} elseif (strtoupper(trim($line)) == 'END:VCARD') {
|
2012-06-11 22:05:15 +02:00
|
|
|
$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) != '') {
|
2012-06-11 22:05:15 +02:00
|
|
|
$card[] = $line;
|
2012-01-15 11:40:32 +01:00
|
|
|
}
|
|
|
|
}
|
2012-05-29 23:40:26 +02:00
|
|
|
//import the contacts
|
2012-05-13 23:24:04 +02:00
|
|
|
writeProgress('70');
|
2012-04-23 21:14:10 +02:00
|
|
|
$imported = 0;
|
|
|
|
$failed = 0;
|
2012-05-29 23:40:26 +02:00
|
|
|
if(!count($parts) > 0) {
|
2012-05-13 23:24:04 +02:00
|
|
|
OCP\JSON::error(array('message' => 'No contacts to import in .'.$_POST['file'].' Please check if the file is corrupted.'));
|
|
|
|
exit();
|
|
|
|
}
|
2012-05-29 23:40:26 +02:00
|
|
|
foreach($parts as $part){
|
|
|
|
$card = OC_VObject::parse($part);
|
2012-03-07 21:27:03 +01:00
|
|
|
if (!$card) {
|
2012-04-23 21:14:10 +02:00
|
|
|
$failed += 1;
|
2012-05-29 23:40:26 +02:00
|
|
|
OCP\Util::writeLog('contacts','Import: skipping card. Error parsing VCard: '.$part, OCP\Util::ERROR);
|
2012-03-07 21:27:03 +01:00
|
|
|
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 {
|
|
|
|
OC_Contacts_VCard::add($id, $card);
|
|
|
|
$imported += 1;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
OCP\Util::writeLog('contacts', 'Error importing vcard: '.$e->getMessage().$nl.$card, OCP\Util::ERROR);
|
|
|
|
$failed += 1;
|
|
|
|
}
|
2012-01-15 11:40:32 +01:00
|
|
|
}
|
|
|
|
//done the import
|
2012-05-13 23:24:04 +02:00
|
|
|
writeProgress('100');
|
2012-01-15 11:40:32 +01:00
|
|
|
sleep(3);
|
2012-06-04 17:42:59 +02:00
|
|
|
OC_Cache::remove($progresskey);
|
2012-04-24 01:43:21 +02:00
|
|
|
if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
|
|
|
|
if(!$view->unlink('/' . $_POST['file'])) {
|
2012-05-01 17:38:27 +02:00
|
|
|
OCP\Util::writeLog('contacts','Import: Error unlinking OC_FilesystemView ' . '/' . $_POST['file'], OCP\Util::ERROR);
|
2012-04-23 21:14:10 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-03 12:23:29 +02:00
|
|
|
OCP\JSON::success(array('data' => array('imported'=>$imported, 'failed'=>$failed)));
|