device_logs.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. if (!class_exists('device_logs')) {
  32. class device_logs {
  33. /**
  34. * declare the variables
  35. */
  36. private $app_name;
  37. private $app_uuid;
  38. private $name;
  39. private $table;
  40. private $toggle_field;
  41. private $toggle_values;
  42. private $location;
  43. /**
  44. * called when the object is created
  45. */
  46. public function __construct() {
  47. //assign the variables
  48. $this->app_name = 'device_logs';
  49. $this->app_uuid = '78b1e5c7-5028-43e7-a05b-a36b44f87087';
  50. $this->name = 'device_log';
  51. $this->table = 'device_logs';
  52. $this->toggle_field = '';
  53. $this->toggle_values = ['true','false'];
  54. $this->location = 'device_logs.php';
  55. }
  56. /**
  57. * called when there are no references to a particular object
  58. * unset the variables used in the class
  59. */
  60. public function __destruct() {
  61. foreach ($this as $key => $value) {
  62. unset($this->$key);
  63. }
  64. }
  65. /**
  66. * delete rows from the database
  67. */
  68. public function delete($records) {
  69. if (permission_exists($this->name.'_delete')) {
  70. //add multi-lingual support
  71. $language = new text;
  72. $text = $language->get();
  73. //validate the token
  74. $token = new token;
  75. if (!$token->validate($_SERVER['PHP_SELF'])) {
  76. message::add($text['message-invalid_token'],'negative');
  77. header('Location: '.$this->location);
  78. exit;
  79. }
  80. //delete multiple records
  81. if (is_array($records) && @sizeof($records) != 0) {
  82. //build the delete array
  83. $x = 0;
  84. foreach ($records as $record) {
  85. //add to the array
  86. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  87. $array[$this->table][$x][$this->name.'_uuid'] = $record['uuid'];
  88. $array[$this->table][$x]['domain_uuid'] = $_SESSION['domain_uuid'];
  89. }
  90. //increment the id
  91. $x++;
  92. }
  93. //delete the checked rows
  94. if (is_array($array) && @sizeof($array) != 0) {
  95. //execute delete
  96. $database = new database;
  97. $database->app_name = $this->app_name;
  98. $database->app_uuid = $this->app_uuid;
  99. $database->delete($array);
  100. unset($array);
  101. //set message
  102. message::add($text['message-delete']);
  103. }
  104. unset($records);
  105. }
  106. }
  107. }
  108. /**
  109. * toggle a field between two values
  110. */
  111. public function toggle($records) {
  112. if (permission_exists($this->name.'_edit')) {
  113. //add multi-lingual support
  114. $language = new text;
  115. $text = $language->get();
  116. //validate the token
  117. $token = new token;
  118. if (!$token->validate($_SERVER['PHP_SELF'])) {
  119. message::add($text['message-invalid_token'],'negative');
  120. header('Location: '.$this->location);
  121. exit;
  122. }
  123. //toggle the checked records
  124. if (is_array($records) && @sizeof($records) != 0) {
  125. //get current toggle state
  126. foreach($records as $record) {
  127. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  128. $uuids[] = "'".$record['uuid']."'";
  129. }
  130. }
  131. if (is_array($uuids) && @sizeof($uuids) != 0) {
  132. $sql = "select ".$this->name."_uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
  133. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  134. $sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) ";
  135. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  136. $database = new database;
  137. $rows = $database->select($sql, $parameters, 'all');
  138. if (is_array($rows) && @sizeof($rows) != 0) {
  139. foreach ($rows as $row) {
  140. $states[$row['uuid']] = $row['toggle'];
  141. }
  142. }
  143. unset($sql, $parameters, $rows, $row);
  144. }
  145. //build update array
  146. $x = 0;
  147. foreach($states as $uuid => $state) {
  148. //create the array
  149. $array[$this->table][$x][$this->name.'_uuid'] = $uuid;
  150. $array[$this->table][$x][$this->toggle_field] = $state == $this->toggle_values[0] ? $this->toggle_values[1] : $this->toggle_values[0];
  151. //increment the id
  152. $x++;
  153. }
  154. //save the changes
  155. if (is_array($array) && @sizeof($array) != 0) {
  156. //save the array
  157. $database = new database;
  158. $database->app_name = $this->app_name;
  159. $database->app_uuid = $this->app_uuid;
  160. $database->save($array);
  161. unset($array);
  162. //set message
  163. message::add($text['message-toggle']);
  164. }
  165. unset($records, $states);
  166. }
  167. }
  168. }
  169. /**
  170. * copy rows from the database
  171. */
  172. public function copy($records) {
  173. if (permission_exists($this->name.'_add')) {
  174. //add multi-lingual support
  175. $language = new text;
  176. $text = $language->get();
  177. //validate the token
  178. $token = new token;
  179. if (!$token->validate($_SERVER['PHP_SELF'])) {
  180. message::add($text['message-invalid_token'],'negative');
  181. header('Location: '.$this->location);
  182. exit;
  183. }
  184. //copy the checked records
  185. if (is_array($records) && @sizeof($records) != 0) {
  186. //get checked records
  187. foreach($records as $record) {
  188. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  189. $uuids[] = "'".$record['uuid']."'";
  190. }
  191. }
  192. //create the array from existing data
  193. if (is_array($uuids) && @sizeof($uuids) != 0) {
  194. $sql = "select * from v_".$this->table." ";
  195. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  196. $sql .= "and (domain_uuid = :domain_uuid or domain_uuid is null) ";
  197. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  198. $database = new database;
  199. $rows = $database->select($sql, $parameters, 'all');
  200. if (is_array($rows) && @sizeof($rows) != 0) {
  201. $x = 0;
  202. foreach ($rows as $row) {
  203. //copy data
  204. $array[$this->table][$x] = $row;
  205. //add copy to the description
  206. $array[$this->table][$x][$this->name.'_uuid'] = uuid();
  207. //increment the id
  208. $x++;
  209. }
  210. }
  211. unset($sql, $parameters, $rows, $row);
  212. }
  213. //save the changes and set the message
  214. if (is_array($array) && @sizeof($array) != 0) {
  215. //save the array
  216. $database = new database;
  217. $database->app_name = $this->app_name;
  218. $database->app_uuid = $this->app_uuid;
  219. $database->save($array);
  220. unset($array);
  221. //set message
  222. message::add($text['message-copy']);
  223. }
  224. unset($records);
  225. }
  226. }
  227. }
  228. }
  229. }
  230. ?>