device_logs.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /*
  3. Copyright (c) 2019-2022 Mark J Crane <[email protected]>
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
  13. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  16. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  17. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  19. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  20. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  22. SUCH DAMAGE.
  23. */
  24. /**
  25. * device_logs class
  26. *
  27. * @method null delete
  28. * @method null toggle
  29. * @method null copy
  30. */
  31. class device_logs {
  32. /**
  33. * declare the variables
  34. */
  35. private $app_name;
  36. private $app_uuid;
  37. private $name;
  38. private $table;
  39. private $toggle_field;
  40. private $toggle_values;
  41. private $location;
  42. /**
  43. * called when the object is created
  44. */
  45. public function __construct() {
  46. //assign the variables
  47. $this->app_name = 'device_logs';
  48. $this->app_uuid = '78b1e5c7-5028-43e7-a05b-a36b44f87087';
  49. $this->name = 'device_log';
  50. $this->table = 'device_logs';
  51. $this->toggle_field = '';
  52. $this->toggle_values = ['true','false'];
  53. $this->location = 'device_logs.php';
  54. }
  55. /**
  56. * called when there are no references to a particular object
  57. * unset the variables used in the class
  58. */
  59. public function __destruct() {
  60. foreach ($this as $key => $value) {
  61. unset($this->$key);
  62. }
  63. }
  64. /**
  65. * delete rows from the database
  66. */
  67. public function delete($records) {
  68. if (permission_exists($this->name.'_delete')) {
  69. //add multi-lingual support
  70. $language = new text;
  71. $text = $language->get();
  72. //validate the token
  73. $token = new token;
  74. if (!$token->validate($_SERVER['PHP_SELF'])) {
  75. message::add($text['message-invalid_token'],'negative');
  76. header('Location: '.$this->location);
  77. exit;
  78. }
  79. //delete multiple records
  80. if (is_array($records) && @sizeof($records) != 0) {
  81. //build the delete array
  82. $x = 0;
  83. foreach ($records as $record) {
  84. //add to the array
  85. if (isset($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
  86. $array[$this->table][$x][$this->name.'_uuid'] = $record['uuid'];
  87. $array[$this->table][$x]['domain_uuid'] = $_SESSION['domain_uuid'];
  88. }
  89. //increment the id
  90. $x++;
  91. }
  92. //delete the checked rows
  93. if (is_array($array) && @sizeof($array) != 0) {
  94. //execute delete
  95. $database = new database;
  96. $database->app_name = $this->app_name;
  97. $database->app_uuid = $this->app_uuid;
  98. $database->delete($array);
  99. unset($array);
  100. //set message
  101. message::add($text['message-delete']);
  102. }
  103. unset($records);
  104. }
  105. }
  106. }
  107. /**
  108. * toggle a field between two values
  109. */
  110. public function toggle($records) {
  111. if (permission_exists($this->name.'_edit')) {
  112. //add multi-lingual support
  113. $language = new text;
  114. $text = $language->get();
  115. //validate the token
  116. $token = new token;
  117. if (!$token->validate($_SERVER['PHP_SELF'])) {
  118. message::add($text['message-invalid_token'],'negative');
  119. header('Location: '.$this->location);
  120. exit;
  121. }
  122. //toggle the checked records
  123. if (is_array($records) && @sizeof($records) != 0) {
  124. //get current toggle state
  125. foreach($records as $record) {
  126. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  127. $uuids[] = "'".$record['uuid']."'";
  128. }
  129. }
  130. if (is_array($uuids) && @sizeof($uuids) != 0) {
  131. $sql = "select ".$this->name."_uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
  132. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  133. $sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) ";
  134. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  135. $database = new database;
  136. $rows = $database->select($sql, $parameters, 'all');
  137. if (is_array($rows) && @sizeof($rows) != 0) {
  138. foreach ($rows as $row) {
  139. $states[$row['uuid']] = $row['toggle'];
  140. }
  141. }
  142. unset($sql, $parameters, $rows, $row);
  143. }
  144. //build update array
  145. $x = 0;
  146. foreach($states as $uuid => $state) {
  147. //create the array
  148. $array[$this->table][$x][$this->name.'_uuid'] = $uuid;
  149. $array[$this->table][$x][$this->toggle_field] = $state == $this->toggle_values[0] ? $this->toggle_values[1] : $this->toggle_values[0];
  150. //increment the id
  151. $x++;
  152. }
  153. //save the changes
  154. if (is_array($array) && @sizeof($array) != 0) {
  155. //save the array
  156. $database = new database;
  157. $database->app_name = $this->app_name;
  158. $database->app_uuid = $this->app_uuid;
  159. $database->save($array);
  160. unset($array);
  161. //set message
  162. message::add($text['message-toggle']);
  163. }
  164. unset($records, $states);
  165. }
  166. }
  167. }
  168. /**
  169. * copy rows from the database
  170. */
  171. public function copy($records) {
  172. if (permission_exists($this->name.'_add')) {
  173. //add multi-lingual support
  174. $language = new text;
  175. $text = $language->get();
  176. //validate the token
  177. $token = new token;
  178. if (!$token->validate($_SERVER['PHP_SELF'])) {
  179. message::add($text['message-invalid_token'],'negative');
  180. header('Location: '.$this->location);
  181. exit;
  182. }
  183. //copy the checked records
  184. if (is_array($records) && @sizeof($records) != 0) {
  185. //get checked records
  186. foreach($records as $record) {
  187. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  188. $uuids[] = "'".$record['uuid']."'";
  189. }
  190. }
  191. //create the array from existing data
  192. if (is_array($uuids) && @sizeof($uuids) != 0) {
  193. $sql = "select * from v_".$this->table." ";
  194. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  195. $sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) ";
  196. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  197. $database = new database;
  198. $rows = $database->select($sql, $parameters, 'all');
  199. if (is_array($rows) && @sizeof($rows) != 0) {
  200. $x = 0;
  201. foreach ($rows as $row) {
  202. //copy data
  203. $array[$this->table][$x] = $row;
  204. //add copy to the description
  205. $array[$this->table][$x][$this->name.'_uuid'] = uuid();
  206. //increment the id
  207. $x++;
  208. }
  209. }
  210. unset($sql, $parameters, $rows, $row);
  211. }
  212. //save the changes and set the message
  213. if (is_array($array) && @sizeof($array) != 0) {
  214. //save the array
  215. $database = new database;
  216. $database->app_name = $this->app_name;
  217. $database->app_uuid = $this->app_uuid;
  218. $database->save($array);
  219. unset($array);
  220. //set message
  221. message::add($text['message-copy']);
  222. }
  223. unset($records);
  224. }
  225. }
  226. }
  227. }
  228. ?>