Welcome to mirror list, hosted at ThFree Co, Russian Federation.

import.php « contacts « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0ee35f9fd81f094e54fa94950f98cba648e7deee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?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.
 */
//check for addressbooks rights or create new one
ob_start();
 
OCP\JSON::checkLoggedIn();
OCP\App::checkAppEnabled('contacts');
session_write_close();

$nl = "\n";

global $progresskey;
$progresskey = 'contacts.import-' . $_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);
}
writeProgress('10');
$view = $file = null;
if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
	$view = OCP\Files::getStorage('contacts');
	$file = $view->file_get_contents('/' . $_POST['file']);
} else {
	$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']);
}
if(!$file) {
	OCP\JSON::error(array('message' => 'Import file was empty.'));
	exit();
}
if(isset($_POST['method']) && $_POST['method'] == 'new'){
	$id = OC_Contacts_Addressbook::add(OCP\USER::getUser(), $_POST['addressbookname']);
	if(!$id) {
		OCP\JSON::error(array('message' => 'Error creating address book.'));
		exit();
	}
	OC_Contacts_Addressbook::setActive($id, 1);
}else{
	$id = $_POST['id'];
	if(!$id) {
		OCP\JSON::error(array('message' => 'Error getting the ID of the address book.'));
		exit();
	}
	OC_Contacts_App::getAddressbook($id); // is owner access check
}
//analyse the contacts file
writeProgress('40');
$lines = explode($nl, $file);
$inelement = false;
$parts = array();
$card = array();
foreach($lines as $line){
	if(strtoupper(trim($line)) == 'BEGIN:VCARD'){
		$inelement = true;
	} elseif (strtoupper(trim($line)) == 'END:VCARD') {
		$card[] = $line;
		$parts[] = implode($nl, $card);
		$card = array();
		$inelement = false;
	}
	if ($inelement === true && trim($line) != '') {
		$card[] = $line;
	}
}
//import the contacts
writeProgress('70');
$imported = 0;
$failed = 0;
if(!count($parts) > 0) {
	OCP\JSON::error(array('message' => 'No contacts to import in .'.$_POST['file'].' Please check if the file is corrupted.'));
	exit();
}
foreach($parts as $part){
	$card = OC_VObject::parse($part);
	if (!$card) {
		$failed += 1;
		OCP\Util::writeLog('contacts','Import: skipping card. Error parsing VCard: '.$part, OCP\Util::ERROR);
		continue; // Ditch cards that can't be parsed by Sabre.
	}
	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;
	}
}
//done the import
writeProgress('100');
sleep(3);
OC_Cache::remove($progresskey);
if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
	if(!$view->unlink('/' . $_POST['file'])) {
		OCP\Util::writeLog('contacts','Import: Error unlinking OC_FilesystemView ' . '/' . $_POST['file'], OCP\Util::ERROR);
	}
}
OCP\JSON::success(array('data' => array('imported'=>$imported, 'failed'=>$failed)));