summaryrefslogtreecommitdiffstats
path: root/apps/contacts/import.php
diff options
context:
space:
mode:
authorGeorg Ehrke <dev@georgswebsite.de>2012-01-15 11:40:32 +0100
committerGeorg Ehrke <dev@georgswebsite.de>2012-02-05 12:13:53 +0100
commit8337eaa1ec3962b185a2fe2e03ccf24fed7020c4 (patch)
treebbe3c95fa8ced7412b487ec665d3f460201f46d7 /apps/contacts/import.php
parent90c4666b12e0fb2da8f0e15a018d8edd736f4f4f (diff)
downloadnextcloud-server-8337eaa1ec3962b185a2fe2e03ccf24fed7020c4.tar.gz
nextcloud-server-8337eaa1ec3962b185a2fe2e03ccf24fed7020c4.zip
add import function for contacts
Diffstat (limited to 'apps/contacts/import.php')
-rw-r--r--apps/contacts/import.php120
1 files changed, 120 insertions, 0 deletions
diff --git a/apps/contacts/import.php b/apps/contacts/import.php
new file mode 100644
index 00000000000..f145c9a8822
--- /dev/null
+++ b/apps/contacts/import.php
@@ -0,0 +1,120 @@
+<?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 calendar rights or create new one
+ob_start();
+require_once ('../../lib/base.php');
+OC_JSON::checkLoggedIn();
+OC_Util::checkAppEnabled('calendar');
+$nl = "\n";
+$progressfile = 'import_tmp/' . md5(session_id()) . '.txt';
+if(is_writable('import_tmp/')){
+ $progressfopen = fopen($progressfile, 'w');
+ fwrite($progressfopen, '10');
+ fclose($progressfopen);
+}
+$file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']);
+if($_POST['method'] == 'new'){
+ $id = OC_Contacts_Addressbook::add(OC_User::getUser(), $_POST['addressbookname']);
+ OC_Contacts_Addressbook::setActive($id, 1);
+}else{
+ $contacts = OC_Contacts_Addressbook::find($_POST['id']);
+ if($contacts['userid'] != OC_USER::getUser()){
+ OC_JSON::error();
+ exit();
+ }
+ $id = $_POST['id'];
+}
+//analyse the calendar file
+if(is_writable('import_tmp/')){
+ $progressfopen = fopen($progressfile, 'w');
+ fwrite($progressfopen, '20');
+ fclose($progressfopen);
+}
+$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++;
+}
+//import the calendar
+if(is_writable('import_tmp/')){
+ $progressfopen = fopen($progressfile, 'w');
+ fwrite($progressfopen, '40');
+ fclose($progressfopen);
+}
+$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];
+ }
+}
+if(is_writable('import_tmp/')){
+ $progressfopen = fopen($progressfile, 'w');
+ fwrite($progressfopen, '50');
+ fclose($progressfopen);
+}
+$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;
+}
+if(is_writable('import_tmp/')){
+ $progressfopen = fopen($progressfile, 'w');
+ fwrite($progressfopen, '70');
+ fclose($progressfopen);
+}
+if(count($parts) == 1){
+ OC_Contacts_VCard::add($id, $file);
+}else{
+ foreach($importready as $import){
+ OC_Contacts_VCard::add($id, $import);
+ }
+}
+//done the import
+if(is_writable('import_tmp/')){
+ $progressfopen = fopen($progressfile, 'w');
+ fwrite($progressfopen, '100');
+ fclose($progressfopen);
+}
+sleep(3);
+if(is_writable('import_tmp/')){
+ unlink($progressfile);
+}
+OC_JSON::success(); \ No newline at end of file