import.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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('/' . $_POST['file']);
  29. } else {
  30. $file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']);
  31. }
  32. if(!$file) {
  33. OCP\JSON::error(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(), $_POST['addressbookname']);
  38. if(!$id) {
  39. OCP\JSON::error(array('message' => 'Error creating address book.'));
  40. exit();
  41. }
  42. OC_Contacts_Addressbook::setActive($id, 1);
  43. }else{
  44. $id = $_POST['id'];
  45. if(!$id) {
  46. OCP\JSON::error(array('message' => 'Error getting the ID of the address book.'));
  47. exit();
  48. }
  49. OC_Contacts_App::getAddressbook($id); // is owner access check
  50. }
  51. //analyse the contacts file
  52. writeProgress('40');
  53. $lines = explode($nl, $file);
  54. $inelement = false;
  55. $parts = array();
  56. $card = array();
  57. foreach($lines as $line){
  58. if(strtoupper(trim($line)) == 'BEGIN:VCARD'){
  59. $inelement = true;
  60. } elseif (strtoupper(trim($line)) == 'END:VCARD') {
  61. $card[] = $line;
  62. $parts[] = implode($nl, $card);
  63. $card = array();
  64. $inelement = false;
  65. }
  66. if ($inelement === true && trim($line) != '') {
  67. $card[] = $line;
  68. }
  69. }
  70. //import the contacts
  71. writeProgress('70');
  72. $imported = 0;
  73. $failed = 0;
  74. if(!count($parts) > 0) {
  75. OCP\JSON::error(array('message' => 'No contacts to import in .'.$_POST['file'].' Please check if the file is corrupted.'));
  76. exit();
  77. }
  78. foreach($parts as $part){
  79. $card = OC_VObject::parse($part);
  80. if (!$card) {
  81. $failed += 1;
  82. OCP\Util::writeLog('contacts','Import: skipping card. Error parsing VCard: '.$part, OCP\Util::ERROR);
  83. continue; // Ditch cards that can't be parsed by Sabre.
  84. }
  85. try {
  86. OC_Contacts_VCard::add($id, $card);
  87. $imported += 1;
  88. } catch (Exception $e) {
  89. OCP\Util::writeLog('contacts', 'Error importing vcard: '.$e->getMessage().$nl.$card, OCP\Util::ERROR);
  90. $failed += 1;
  91. }
  92. }
  93. //done the import
  94. writeProgress('100');
  95. sleep(3);
  96. OC_Cache::remove($progresskey);
  97. if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
  98. if(!$view->unlink('/' . $_POST['file'])) {
  99. OCP\Util::writeLog('contacts','Import: Error unlinking OC_FilesystemView ' . '/' . $_POST['file'], OCP\Util::ERROR);
  100. }
  101. }
  102. OCP\JSON::success(array('data' => array('imported'=>$imported, 'failed'=>$failed)));