2012-01-15 14:40:32 +04: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 14:47:35 +04:00
|
|
|
//check for addressbooks rights or create new one
|
2012-01-15 14:40:32 +04:00
|
|
|
ob_start();
|
2012-04-17 21:31:29 +04:00
|
|
|
|
2012-05-03 14:23:29 +04:00
|
|
|
OCP\JSON::checkLoggedIn();
|
2012-05-02 21:08:37 +04:00
|
|
|
OCP\App::checkAppEnabled('contacts');
|
2012-01-15 14:40:32 +04:00
|
|
|
$nl = "\n";
|
|
|
|
$progressfile = 'import_tmp/' . md5(session_id()) . '.txt';
|
2012-05-14 01:24:04 +04:00
|
|
|
|
|
|
|
function writeProgress($pct) {
|
|
|
|
if(is_writable('import_tmp/')){
|
|
|
|
$progressfopen = fopen($progressfile, 'w');
|
|
|
|
fwrite($progressfopen, $pct);
|
|
|
|
fclose($progressfopen);
|
|
|
|
}
|
2012-01-15 14:40:32 +04:00
|
|
|
}
|
2012-05-14 01:24:04 +04:00
|
|
|
writeProgress('10');
|
2012-04-24 03:43:21 +04:00
|
|
|
$view = $file = null;
|
|
|
|
if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
|
2012-05-19 12:44:08 +04:00
|
|
|
$view = OCP\Files::getStorage('contacts');
|
2012-04-24 03:43:21 +04:00
|
|
|
$file = $view->file_get_contents('/' . $_POST['file']);
|
|
|
|
} else {
|
|
|
|
$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']);
|
|
|
|
}
|
2012-04-26 05:20:28 +04:00
|
|
|
if(!$file) {
|
2012-05-03 14:23:29 +04:00
|
|
|
OCP\JSON::error(array('message' => 'Import file was empty.'));
|
2012-04-26 05:20:28 +04:00
|
|
|
exit();
|
|
|
|
}
|
2012-04-24 03:43:21 +04:00
|
|
|
if(isset($_POST['method']) && $_POST['method'] == 'new'){
|
2012-05-01 20:50:31 +04:00
|
|
|
$id = OC_Contacts_Addressbook::add(OCP\USER::getUser(), $_POST['addressbookname']);
|
2012-05-14 01:24:04 +04:00
|
|
|
if(!$id) {
|
|
|
|
OCP\JSON::error(array('message' => 'Error creating address book.'));
|
|
|
|
exit();
|
|
|
|
}
|
2012-01-15 14:40:32 +04:00
|
|
|
OC_Contacts_Addressbook::setActive($id, 1);
|
|
|
|
}else{
|
|
|
|
$id = $_POST['id'];
|
2012-05-14 01:24:04 +04:00
|
|
|
if(!$id) {
|
|
|
|
OCP\JSON::error(array('message' => 'Error getting the ID of the address book.'));
|
|
|
|
exit();
|
|
|
|
}
|
2012-02-12 00:48:45 +04:00
|
|
|
OC_Contacts_App::getAddressbook($id); // is owner access check
|
2012-01-15 14:40:32 +04:00
|
|
|
}
|
2012-01-15 14:47:35 +04:00
|
|
|
//analyse the contacts file
|
2012-05-14 01:24:04 +04:00
|
|
|
writeProgress('20');
|
2012-01-15 14:40:32 +04:00
|
|
|
$searchfor = array('VCARD');
|
|
|
|
$parts = $searchfor;
|
|
|
|
$filearr = explode($nl, $file);
|
|
|
|
$inelement = false;
|
|
|
|
$parts = array();
|
|
|
|
$i = 0;
|
|
|
|
foreach($filearr as $line){
|
|
|
|
foreach($searchfor as $search){
|
|
|
|
if(substr_count($line, $search) == 1){
|
|
|
|
list($attr, $val) = explode(':', $line);
|
|
|
|
if($attr == 'BEGIN'){
|
|
|
|
$parts[]['begin'] = $i;
|
|
|
|
$inelement = true;
|
|
|
|
}
|
|
|
|
if($attr == 'END'){
|
|
|
|
$parts[count($parts) - 1]['end'] = $i;
|
|
|
|
$inelement = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
}
|
2012-01-15 14:47:35 +04:00
|
|
|
//import the contacts
|
2012-05-14 01:24:04 +04:00
|
|
|
writeProgress('40');
|
2012-01-15 14:40:32 +04:00
|
|
|
$start = '';
|
|
|
|
for ($i = 0; $i < $parts[0]['begin']; $i++) {
|
|
|
|
if($i == 0){
|
|
|
|
$start = $filearr[0];
|
|
|
|
}else{
|
|
|
|
$start .= $nl . $filearr[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$end = '';
|
|
|
|
for($i = $parts[count($parts) - 1]['end'] + 1;$i <= count($filearr) - 1; $i++){
|
|
|
|
if($i == $parts[count($parts) - 1]['end'] + 1){
|
|
|
|
$end = $filearr[$parts[count($parts) - 1]['end'] + 1];
|
|
|
|
}else{
|
|
|
|
$end .= $nl . $filearr[$i];
|
|
|
|
}
|
|
|
|
}
|
2012-05-14 01:24:04 +04:00
|
|
|
writeProgress('50');
|
2012-01-15 14:40:32 +04:00
|
|
|
$importready = array();
|
|
|
|
foreach($parts as $part){
|
|
|
|
for($i = $part['begin']; $i <= $part['end'];$i++){
|
|
|
|
if($i == $part['begin']){
|
|
|
|
$content = $filearr[$i];
|
|
|
|
}else{
|
|
|
|
$content .= $nl . $filearr[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$importready[] = $start . $nl . $content . $nl . $end;
|
|
|
|
}
|
2012-05-14 01:24:04 +04:00
|
|
|
writeProgress('70');
|
2012-01-15 14:40:32 +04:00
|
|
|
if(count($parts) == 1){
|
2012-03-08 00:27:03 +04:00
|
|
|
$importready = array($file);
|
|
|
|
}
|
2012-04-23 23:14:10 +04:00
|
|
|
$imported = 0;
|
|
|
|
$failed = 0;
|
2012-05-14 01:24:04 +04:00
|
|
|
if(!count($importready) > 0) {
|
|
|
|
OCP\JSON::error(array('message' => 'No contacts to import in .'.$_POST['file'].' Please check if the file is corrupted.'));
|
|
|
|
exit();
|
|
|
|
}
|
2012-03-08 00:27:03 +04:00
|
|
|
foreach($importready as $import){
|
|
|
|
$card = OC_VObject::parse($import);
|
|
|
|
if (!$card) {
|
2012-04-23 23:14:10 +04:00
|
|
|
$failed += 1;
|
2012-05-01 19:38:27 +04:00
|
|
|
OCP\Util::writeLog('contacts','Import: skipping card. Error parsing VCard: '.$import, OCP\Util::ERROR);
|
2012-03-08 00:27:03 +04:00
|
|
|
continue; // Ditch cards that can't be parsed by Sabre.
|
2012-01-15 14:40:32 +04:00
|
|
|
}
|
2012-04-23 23:14:10 +04:00
|
|
|
$imported += 1;
|
2012-03-08 00:27:03 +04:00
|
|
|
OC_Contacts_VCard::add($id, $card);
|
2012-01-15 14:40:32 +04:00
|
|
|
}
|
|
|
|
//done the import
|
2012-05-14 01:24:04 +04:00
|
|
|
writeProgress('100');
|
2012-01-15 14:40:32 +04:00
|
|
|
sleep(3);
|
|
|
|
if(is_writable('import_tmp/')){
|
|
|
|
unlink($progressfile);
|
|
|
|
}
|
2012-04-24 03:43:21 +04:00
|
|
|
if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
|
|
|
|
if(!$view->unlink('/' . $_POST['file'])) {
|
2012-05-01 19:38:27 +04:00
|
|
|
OCP\Util::writeLog('contacts','Import: Error unlinking OC_FilesystemView ' . '/' . $_POST['file'], OCP\Util::ERROR);
|
2012-04-23 23:14:10 +04:00
|
|
|
}
|
|
|
|
}
|
2012-05-03 14:23:29 +04:00
|
|
|
OCP\JSON::success(array('data' => array('imported'=>$imported, 'failed'=>$failed)));
|