v_cdr_import.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2012
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. include "root.php";
  22. require "resources/require.php";
  23. if(php_sapi_name() == 'cli') {
  24. //allow access for command line interface
  25. }
  26. else {
  27. //require authentication
  28. require_once "resources/check_auth.php";
  29. if (permission_exists('cdr_csv_view')) {
  30. //access granted
  31. }
  32. else {
  33. echo "access denied";
  34. exit;
  35. }
  36. }
  37. require "lib_cdr.php";
  38. //---- begin import cdr records -----------------------------------------------------------------------------------
  39. $v_file = $_SESSION['switch']['log']['dir']."/cdr-csv/Master.csv";
  40. //echo filesize($v_file);
  41. //Open file (DON'T USE a+ pointer will be wrong!)
  42. $fh = fopen($v_file, 'r');
  43. $read = 524288;
  44. //$read = 524288;
  45. //$read = 1048576;
  46. //$read = 16777216; //Read 16meg chunks
  47. $x = 0;
  48. $part = 0;
  49. $strcount=0;
  50. while(!feof($fh)) {
  51. $rbuf = fread($fh, $read);
  52. for($i=$read;$i > 0 || $n == chr(10);$i--) {
  53. $n=substr($rbuf, $i, 1);
  54. if($n == chr(10))break;
  55. //If we are at the end of the file, just grab the rest and stop loop
  56. elseif(feof($fh)) {
  57. $i = $read;
  58. $buf = substr($rbuf, 0, $i+1);
  59. break;
  60. }
  61. }
  62. $count = $db->exec("BEGIN;"); //returns affected rows
  63. //This is the buffer we want to do stuff with, maybe thow to a function?
  64. $buf = substr($rbuf, 0, $i+1);
  65. $buf = str_replace("{domain_uuid}", $domain_uuid, $buf);
  66. $totalsize = strlen($buf)+$totalsize;
  67. $lnarray = explode ("\n", $buf);
  68. //print_r($lnarray);
  69. $columnvaluecount=0;
  70. foreach($lnarray as $sql) {
  71. //--- Begin SQLite -------------------------------------
  72. if (strlen($sql) > 0) {
  73. //echo $sql."<br /><br />\n";
  74. $count = $db->exec(check_sql($sql)); //returns affected rows
  75. $x++;
  76. if ($x > 10000) {
  77. $count = $db->exec("COMMIT;"); //returns affected rows
  78. $count = $db->exec("BEGIN;"); //returns affected rows
  79. }
  80. }
  81. unset($sql);
  82. //---EndSQLite-------------------------------------
  83. //if ($columnvaluecount > 10) { break; }
  84. $columnvaluecount++;
  85. }
  86. //Point marker back to last \n point
  87. $part = ftell($fh)-($read-($i+1));
  88. fseek($fh, $part);
  89. if ($strcount >= 5000) { break; } //handle up to a gig file
  90. $strcount++;
  91. }
  92. $count = $db->exec("COMMIT;"); //returns affected rows
  93. fclose($fh);
  94. //truncate the file now that it has been processed
  95. $fh = fopen($v_file, 'w');
  96. fclose($fh);
  97. //---- begin import cdr records -----------------------------------------------------------------------------------
  98. ?>