api.pbxcdr.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Universal PBX CDR abstraction class
  4. */
  5. class PBXCdr {
  6. /**
  7. * Remote database MySQL host
  8. *
  9. * @var string
  10. */
  11. protected $host = '';
  12. /**
  13. * Remote database MySQL user login
  14. *
  15. * @var string
  16. */
  17. protected $login = '';
  18. /**
  19. * Remote database MySQL user password
  20. *
  21. * @var string
  22. */
  23. protected $password = '';
  24. /**
  25. * Remote database name
  26. *
  27. * @var string
  28. */
  29. protected $db = '';
  30. /**
  31. * Remote database CDR table name
  32. *
  33. * @var string
  34. */
  35. protected $table = '';
  36. /**
  37. * Remote database connection placeholder
  38. *
  39. * @var object
  40. */
  41. protected $database = '';
  42. /**
  43. * Current instance connection state
  44. *
  45. * @var bool
  46. */
  47. protected $connected = false;
  48. /**
  49. * Creates new remote database CDR abstraction instance
  50. *
  51. * @param string $host
  52. * @param string $login
  53. * @param string $password
  54. * @param string $db
  55. * @param string $table
  56. *
  57. * @return void
  58. */
  59. public function __construct($host, $login, $password, $db, $table) {
  60. $this->host = $host;
  61. $this->login = $login;
  62. $this->password = $password;
  63. $this->db = $db;
  64. $this->table = $table;
  65. $this->connectDatabase();
  66. }
  67. /**
  68. * Connects to remote database
  69. *
  70. * @return bool
  71. */
  72. protected function connectDatabase() {
  73. @$this->database = new mysqli($this->host, $this->login, $this->password, $this->db);
  74. if (!$this->database->connect_error) {
  75. $this->connected = true;
  76. } else {
  77. $this->connected = false;
  78. }
  79. return($this->connected);
  80. }
  81. /**
  82. * Another database query execution
  83. *
  84. * @param string $query - query to execute
  85. *
  86. * @return array/bool
  87. */
  88. protected function query($query) {
  89. $result = array();
  90. if ($this->connected) {
  91. $result = array();
  92. $result_query = $this->database->query($query, MYSQLI_USE_RESULT);
  93. while ($row = $result_query->fetch_assoc()) {
  94. $result[] = $row;
  95. }
  96. mysqli_free_result($result_query);
  97. } else {
  98. $result = $this->connected;
  99. }
  100. return ($result);
  101. }
  102. /**
  103. * Returns Call Detail Record for current day or selected range of time
  104. *
  105. * @param string $dateFrom
  106. * @param string $dateTo
  107. *
  108. * @return array/bool
  109. */
  110. public function getCDR($dateFrom = '', $dateTo = '') {
  111. $result = array();
  112. $where = '';
  113. if ($dateFrom AND $dateTo) {
  114. $where .= " WHERE `calldate` BETWEEN '" . $dateFrom . "' AND '" . $dateTo . "'";
  115. } else {
  116. $where .= " WHERE `calldate` LIKE '" . curdate() . "%'";
  117. }
  118. $query = "SELECT * from `" . $this->table . "` " . $where . " ORDER BY `calldate` ASC";
  119. $result = $this->query($query);
  120. return($result);
  121. }
  122. }