v_cdr_csv.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_once "resources/require.php";
  23. require_once "resources/check_auth.php";
  24. if (permission_exists('cdr_csv_view')) {
  25. //access granted
  26. }
  27. else {
  28. echo "access denied";
  29. exit;
  30. }
  31. require_once "lib_cdr.php";
  32. if (count($_REQUEST)>0) {
  33. $cdr_id = $_REQUEST["cdr_id"];
  34. $caller_id_name = $_REQUEST["caller_id_name"];
  35. $caller_id_number = $_REQUEST["caller_id_number"];
  36. $destination_number = $_REQUEST["destination_number"];
  37. $context = $_REQUEST["context"];
  38. $start_stamp = $_REQUEST["start_stamp"];
  39. $answer_stamp = $_REQUEST["answer_stamp"];
  40. $end_stamp = $_REQUEST["end_stamp"];
  41. $duration = $_REQUEST["duration"];
  42. $billsec = $_REQUEST["billsec"];
  43. $hangup_cause = $_REQUEST["hangup_cause"];
  44. $uuid = $_REQUEST["uuid"];
  45. $bleg_uuid = $_REQUEST["bleg_uuid"];
  46. $accountcode = $_REQUEST["accountcode"];
  47. $read_codec = $_REQUEST["read_codec"];
  48. $write_codec = $_REQUEST["write_codec"];
  49. $remote_media_ip = $_REQUEST["remote_media_ip"];
  50. $network_addr = $_REQUEST["network_addr"];
  51. }
  52. //get a list of assigned extensions for this user
  53. $sql = "";
  54. $sql .= " select * from v_extensions ";
  55. $sql .= "where domain_uuid = '$domain_uuid' ";
  56. $sql .= "and user_list like '%|".$_SESSION["username"]."|%' ";
  57. $prep_statement = $db->prepare(check_sql($sql));
  58. $prep_statement->execute();
  59. //$v_mailboxes = '';
  60. $x = 0;
  61. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  62. foreach ($result as &$row) {
  63. //$v_mailboxes = $v_mailboxes.$row["mailbox"].'|';
  64. //$extension_uuid = $row["extension_uuid"];
  65. //$mailbox = $row["mailbox"];
  66. $extension_array[$x]['extension_uuid'] = $row["extension_uuid"];
  67. $extension_array[$x]['extension'] = $row["extension"];
  68. $x++;
  69. }
  70. unset ($prep_statement, $x);
  71. if (if_group("admin") || if_group("superadmin")) {
  72. $sql_where = "where ";
  73. }
  74. if (strlen($domain_uuid) > 0) { $sql_where .= "and domain_uuid like '$domain_uuid' "; }
  75. if (strlen($cdr_id) > 0) { $sql_where .= "and cdr_id like '%$cdr_id%' "; }
  76. if (strlen($caller_id_name) > 0) { $sql_where .= "and caller_id_name like '%$caller_id_name%' "; }
  77. if (strlen($caller_id_number) > 0) { $sql_where .= "and caller_id_number like '%$caller_id_number%' "; }
  78. if (strlen($destination_number) > 0) { $sql_where .= "and destination_number like '%$destination_number%' "; }
  79. if (strlen($context) > 0) { $sql_where .= "and context like '%$context%' "; }
  80. if (strlen($start_stamp) > 0) { $sql_where .= "and start_stamp like '%$start_stamp%' "; }
  81. if (strlen($answer_stamp) > 0) { $sql_where .= "and answer_stamp like '%$answer_stamp%' "; }
  82. if (strlen($end_stamp) > 0) { $sql_where .= "and end_stamp like '%$end_stamp%' "; }
  83. if (strlen($duration) > 0) { $sql_where .= "and duration like '%$duration%' "; }
  84. if (strlen($billsec) > 0) { $sql_where .= "and billsec like '%$billsec%' "; }
  85. if (strlen($hangup_cause) > 0) { $sql_where .= "and hangup_cause like '%$hangup_cause%' "; }
  86. if (strlen($uuid) > 0) { $sql_where .= "and uuid like '%$uuid%' "; }
  87. if (strlen($bleg_uuid) > 0) { $sql_where .= "and bleg_uuid like '%$bleg_uuid%' "; }
  88. if (strlen($accountcode) > 0) { $sql_where .= "and accountcode like '%$accountcode%' "; }
  89. if (strlen($read_codec) > 0) { $sql_where .= "and read_codec like '%$read_codec%' "; }
  90. if (strlen($write_codec) > 0) { $sql_where .= "and write_codec like '%$write_codec%' "; }
  91. if (strlen($remote_media_ip) > 0) { $sql_where .= "and remote_media_ip like '%$remote_media_ip%' "; }
  92. if (strlen($network_addr) > 0) { $sql_where .= "and network_addr like '%$network_addr%' "; }
  93. if (!if_group("admin") && !if_group("superadmin")) {
  94. if (trim($sql_where) == "where") { $sql_where = ""; }
  95. //disable member search
  96. //$sql_where_orig = $sql_where;
  97. $sql_where = "where ";
  98. if (count($_SESSION['user']['extension']) > 0) {
  99. foreach ($_SESSION['user']['extension'] as &$row) {
  100. if ($row['user'] > 0) { $sql_where .= "or caller_id_number = '".$row['user']."' ". $sql_where_orig; } //source
  101. if ($row['user'] > 0) { $sql_where .= "or destination_number = '".$row['user']."' ".$sql_where_orig; } //destination
  102. if ($row['user'] > 0) { $sql_where .= "or destination_number = '*99".$row['user']."' ".$sql_where_orig; } //destination
  103. }
  104. }
  105. }
  106. $sql_where = str_replace ("where or", "where", $sql_where);
  107. $sql_where = str_replace ("where and", "where", $sql_where);
  108. $sql = "";
  109. $sql .= "select * from v_cdr ";
  110. $sql .= $sql_where;
  111. if (strlen($order_by)> 0) { $sql .= "order by $order_by $order "; }
  112. $prep_statement = $db->prepare(check_sql($sql));
  113. $prep_statement->execute();
  114. $result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
  115. $result_count = count($result);
  116. header('Content-type: application/octet-binary');
  117. header('Content-Disposition: attachment; filename=cdr.csv');
  118. $z = 0;
  119. foreach($result[0] as $key => $val) {
  120. if ($z == 0) {
  121. echo '"'.$key.'"';
  122. }
  123. else {
  124. echo ',"'.$key.'"';
  125. }
  126. $z++;
  127. }
  128. echo "\n";
  129. $x=0;
  130. while(true) {
  131. $z = 0;
  132. foreach($result[0] as $key => $val) {
  133. if ($z == 0) {
  134. echo '"'.$result[$x][$key].'"';
  135. }
  136. else {
  137. echo ',"'.$result[$x][$key].'"';
  138. }
  139. $z++;
  140. }
  141. echo "\n";
  142. ++$x;
  143. if ($x > ($result_count-1)) {
  144. break;
  145. }
  146. //$row++;
  147. }
  148. unset ($result_count);
  149. unset ($resulttype);
  150. unset ($result);
  151. unset ($key);
  152. unset ($val);
  153. unset ($msg);
  154. unset ($errormsg);
  155. unset ($sql);
  156. unset ($x);
  157. unset ($z);
  158. ?>