import.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Georg Ehrke <ownclouddev at georgswebsite dot de>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. //check for addressbooks rights or create new one
  9. ob_start();
  10. OCP\JSON::checkLoggedIn();
  11. OCP\App::checkAppEnabled('contacts');
  12. session_write_close();
  13. $nl = "\n";
  14. global $progresskey;
  15. $progresskey = 'contacts.import-' . (isset($_GET['progresskey'])?$_GET['progresskey']:'');
  16. if (isset($_GET['progress']) && $_GET['progress']) {
  17. echo OC_Cache::get($progresskey);
  18. die;
  19. }
  20. function writeProgress($pct) {
  21. global $progresskey;
  22. OC_Cache::set($progresskey, $pct, 300);
  23. }
  24. writeProgress('10');
  25. $view = $file = null;
  26. if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
  27. $view = OCP\Files::getStorage('contacts');
  28. $file = $view->file_get_contents('/imports/' . $_POST['file']);
  29. } else {
  30. $file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']);
  31. }
  32. if(!$file) {
  33. OCP\JSON::error(array('data' => array('message' => 'Import file was empty.')));
  34. exit();
  35. }
  36. if(isset($_POST['method']) && $_POST['method'] == 'new') {
  37. $id = OC_Contacts_Addressbook::add(OCP\USER::getUser(),
  38. $_POST['addressbookname']);
  39. if(!$id) {
  40. OCP\JSON::error(
  41. array(
  42. 'data' => array('message' => 'Error creating address book.')
  43. )
  44. );
  45. exit();
  46. }
  47. OC_Contacts_Addressbook::setActive($id, 1);
  48. }else{
  49. $id = $_POST['id'];
  50. if(!$id) {
  51. OCP\JSON::error(
  52. array(
  53. 'data' => array(
  54. 'message' => 'Error getting the ID of the address book.',
  55. 'file'=>$_POST['file']
  56. )
  57. )
  58. );
  59. exit();
  60. }
  61. OC_Contacts_App::getAddressbook($id); // is owner access check
  62. }
  63. //analyse the contacts file
  64. writeProgress('40');
  65. $file = str_replace(array("\r","\n\n"), array("\n","\n"), $file);
  66. $lines = explode($nl, $file);
  67. $inelement = false;
  68. $parts = array();
  69. $card = array();
  70. foreach($lines as $line){
  71. if(strtoupper(trim($line)) == 'BEGIN:VCARD') {
  72. $inelement = true;
  73. } elseif (strtoupper(trim($line)) == 'END:VCARD') {
  74. $card[] = $line;
  75. $parts[] = implode($nl, $card);
  76. $card = array();
  77. $inelement = false;
  78. }
  79. if ($inelement === true && trim($line) != '') {
  80. $card[] = $line;
  81. }
  82. }
  83. //import the contacts
  84. writeProgress('70');
  85. $imported = 0;
  86. $failed = 0;
  87. if(!count($parts) > 0) {
  88. OCP\JSON::error(
  89. array(
  90. 'data' => array(
  91. 'message' => 'No contacts to import in '
  92. . $_POST['file'].'. Please check if the file is corrupted.',
  93. 'file'=>$_POST['file']
  94. )
  95. )
  96. );
  97. if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
  98. if(!$view->unlink('/imports/' . $_POST['file'])) {
  99. OCP\Util::writeLog('contacts',
  100. 'Import: Error unlinking OC_FilesystemView ' . '/' . $_POST['file'],
  101. OCP\Util::ERROR);
  102. }
  103. }
  104. exit();
  105. }
  106. foreach($parts as $part){
  107. $card = OC_VObject::parse($part);
  108. if (!$card) {
  109. $failed += 1;
  110. OCP\Util::writeLog('contacts',
  111. 'Import: skipping card. Error parsing VCard: ' . $part,
  112. OCP\Util::ERROR);
  113. continue; // Ditch cards that can't be parsed by Sabre.
  114. }
  115. try {
  116. OC_Contacts_VCard::add($id, $card);
  117. $imported += 1;
  118. } catch (Exception $e) {
  119. OCP\Util::writeLog('contacts',
  120. 'Error importing vcard: ' . $e->getMessage() . $nl . $card,
  121. OCP\Util::ERROR);
  122. $failed += 1;
  123. }
  124. }
  125. //done the import
  126. writeProgress('100');
  127. sleep(3);
  128. OC_Cache::remove($progresskey);
  129. if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
  130. if(!$view->unlink('/imports/' . $_POST['file'])) {
  131. OCP\Util::writeLog('contacts',
  132. 'Import: Error unlinking OC_FilesystemView ' . '/' . $_POST['file'],
  133. OCP\Util::ERROR);
  134. }
  135. }
  136. OCP\JSON::success(
  137. array(
  138. 'data' => array(
  139. 'imported'=>$imported,
  140. 'failed'=>$failed,
  141. 'file'=>$_POST['file'],
  142. )
  143. )
  144. );