databases.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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) 2020
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //define the databases class
  22. if (!class_exists('databases')) {
  23. class databases {
  24. /**
  25. * declare private variables
  26. */
  27. private $app_name;
  28. private $app_uuid;
  29. private $permission_prefix;
  30. private $list_page;
  31. private $table;
  32. private $uuid_prefix;
  33. /**
  34. * called when the object is created
  35. */
  36. public function __construct() {
  37. //assign private variables
  38. $this->app_name = 'databases';
  39. $this->app_uuid = '8d229b6d-1383-fcec-74c6-4ce1682479e2';
  40. $this->permission_prefix = 'database_';
  41. $this->list_page = 'databases.php';
  42. $this->table = 'databases';
  43. $this->uuid_prefix = 'database_';
  44. }
  45. /**
  46. * called when there are no references to a particular object
  47. * unset the variables used in the class
  48. */
  49. public function __destruct() {
  50. foreach ($this as $key => $value) {
  51. unset($this->$key);
  52. }
  53. }
  54. /**
  55. * delete records
  56. */
  57. public function delete($records) {
  58. if (permission_exists($this->permission_prefix.'delete')) {
  59. //add multi-lingual support
  60. $language = new text;
  61. $text = $language->get();
  62. //validate the token
  63. $token = new token;
  64. if (!$token->validate($_SERVER['PHP_SELF'])) {
  65. message::add($text['message-invalid_token'],'negative');
  66. header('Location: '.$this->list_page);
  67. exit;
  68. }
  69. //delete multiple records
  70. if (is_array($records) && @sizeof($records) != 0) {
  71. //build the delete array
  72. foreach ($records as $x => $record) {
  73. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  74. $array[$this->table][$x][$this->uuid_prefix.'uuid'] = $record['uuid'];
  75. }
  76. }
  77. //delete the checked rows
  78. if (is_array($array) && @sizeof($array) != 0) {
  79. //execute delete
  80. $database = new database;
  81. $database->app_name = $this->app_name;
  82. $database->app_uuid = $this->app_uuid;
  83. $database->delete($array);
  84. unset($array);
  85. //set message
  86. message::add($text['message-delete']);
  87. }
  88. unset($records);
  89. }
  90. }
  91. }
  92. /**
  93. * copy records
  94. */
  95. public function copy($records) {
  96. if (permission_exists($this->permission_prefix.'add')) {
  97. //add multi-lingual support
  98. $language = new text;
  99. $text = $language->get();
  100. //validate the token
  101. $token = new token;
  102. if (!$token->validate($_SERVER['PHP_SELF'])) {
  103. message::add($text['message-invalid_token'],'negative');
  104. header('Location: '.$this->list_page);
  105. exit;
  106. }
  107. //copy the checked records
  108. if (is_array($records) && @sizeof($records) != 0) {
  109. //get checked records
  110. foreach ($records as $x => $record) {
  111. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  112. $uuids[] = "'".$record['uuid']."'";
  113. }
  114. }
  115. //create insert array from existing data
  116. if (is_array($uuids) && @sizeof($uuids) != 0) {
  117. $sql = "select * from v_".$this->table." ";
  118. $sql .= "where ".$this->uuid_prefix."uuid in (".implode(', ', $uuids).") ";
  119. $database = new database;
  120. $rows = $database->select($sql, $parameters, 'all');
  121. if (is_array($rows) && @sizeof($rows) != 0) {
  122. foreach ($rows as $x => $row) {
  123. //copy data
  124. $array[$this->table][$x] = $row;
  125. //overwrite
  126. $array[$this->table][$x][$this->uuid_prefix.'uuid'] = uuid();
  127. $array[$this->table][$x]['database_description'] = trim($row['database_description'].' ('.$text['label-copy'].')');
  128. }
  129. }
  130. unset($sql, $parameters, $rows, $row);
  131. }
  132. //save the changes and set the message
  133. if (is_array($array) && @sizeof($array) != 0) {
  134. //save the array
  135. $database = new database;
  136. $database->app_name = $this->app_name;
  137. $database->app_uuid = $this->app_uuid;
  138. $database->save($array);
  139. unset($array);
  140. //set message
  141. message::add($text['message-copy']);
  142. }
  143. unset($records);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. ?>