import.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. require_once ('../../lib/base.php');
  11. OC_JSON::checkLoggedIn();
  12. OC_Util::checkAppEnabled('calendar');
  13. $nl = "\n";
  14. $progressfile = 'import_tmp/' . md5(session_id()) . '.txt';
  15. if(is_writable('import_tmp/')){
  16. $progressfopen = fopen($progressfile, 'w');
  17. fwrite($progressfopen, '10');
  18. fclose($progressfopen);
  19. }
  20. $file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']);
  21. if($_POST['method'] == 'new'){
  22. $id = OC_Contacts_Addressbook::add(OC_User::getUser(), $_POST['addressbookname']);
  23. OC_Contacts_Addressbook::setActive($id, 1);
  24. }else{
  25. $id = $_POST['id'];
  26. OC_Contacts_App::getAddressbook($id); // is owner access check
  27. }
  28. //analyse the contacts file
  29. if(is_writable('import_tmp/')){
  30. $progressfopen = fopen($progressfile, 'w');
  31. fwrite($progressfopen, '20');
  32. fclose($progressfopen);
  33. }
  34. $searchfor = array('VCARD');
  35. $parts = $searchfor;
  36. $filearr = explode($nl, $file);
  37. $inelement = false;
  38. $parts = array();
  39. $i = 0;
  40. foreach($filearr as $line){
  41. foreach($searchfor as $search){
  42. if(substr_count($line, $search) == 1){
  43. list($attr, $val) = explode(':', $line);
  44. if($attr == 'BEGIN'){
  45. $parts[]['begin'] = $i;
  46. $inelement = true;
  47. }
  48. if($attr == 'END'){
  49. $parts[count($parts) - 1]['end'] = $i;
  50. $inelement = false;
  51. }
  52. }
  53. }
  54. $i++;
  55. }
  56. //import the contacts
  57. if(is_writable('import_tmp/')){
  58. $progressfopen = fopen($progressfile, 'w');
  59. fwrite($progressfopen, '40');
  60. fclose($progressfopen);
  61. }
  62. $start = '';
  63. for ($i = 0; $i < $parts[0]['begin']; $i++) {
  64. if($i == 0){
  65. $start = $filearr[0];
  66. }else{
  67. $start .= $nl . $filearr[$i];
  68. }
  69. }
  70. $end = '';
  71. for($i = $parts[count($parts) - 1]['end'] + 1;$i <= count($filearr) - 1; $i++){
  72. if($i == $parts[count($parts) - 1]['end'] + 1){
  73. $end = $filearr[$parts[count($parts) - 1]['end'] + 1];
  74. }else{
  75. $end .= $nl . $filearr[$i];
  76. }
  77. }
  78. if(is_writable('import_tmp/')){
  79. $progressfopen = fopen($progressfile, 'w');
  80. fwrite($progressfopen, '50');
  81. fclose($progressfopen);
  82. }
  83. $importready = array();
  84. foreach($parts as $part){
  85. for($i = $part['begin']; $i <= $part['end'];$i++){
  86. if($i == $part['begin']){
  87. $content = $filearr[$i];
  88. }else{
  89. $content .= $nl . $filearr[$i];
  90. }
  91. }
  92. $importready[] = $start . $nl . $content . $nl . $end;
  93. }
  94. if(is_writable('import_tmp/')){
  95. $progressfopen = fopen($progressfile, 'w');
  96. fwrite($progressfopen, '70');
  97. fclose($progressfopen);
  98. }
  99. if(count($parts) == 1){
  100. OC_Contacts_VCard::add($id, $file);
  101. }else{
  102. foreach($importready as $import){
  103. OC_Contacts_VCard::add($id, $import);
  104. }
  105. }
  106. //done the import
  107. if(is_writable('import_tmp/')){
  108. $progressfopen = fopen($progressfile, 'w');
  109. fwrite($progressfopen, '100');
  110. fclose($progressfopen);
  111. }
  112. sleep(3);
  113. if(is_writable('import_tmp/')){
  114. unlink($progressfile);
  115. }
  116. OC_JSON::success();