users.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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) 2019-2020
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. /**
  22. * users class
  23. *
  24. * @method null delete
  25. * @method null toggle
  26. * @method null copy
  27. */
  28. if (!class_exists('users')) {
  29. class users {
  30. /**
  31. * declare the variables
  32. */
  33. private $app_name;
  34. private $app_uuid;
  35. private $name;
  36. private $table;
  37. private $toggle_field;
  38. private $toggle_values;
  39. private $location;
  40. /**
  41. * called when the object is created
  42. */
  43. public function __construct() {
  44. //assign the variables
  45. $this->app_name = 'users';
  46. $this->app_uuid = '112124b3-95c2-5352-7e9d-d14c0b88f207';
  47. $this->name = 'user';
  48. $this->table = 'users';
  49. $this->toggle_field = 'user_enabled';
  50. $this->toggle_values = ['true','false'];
  51. $this->location = 'users.php';
  52. }
  53. /**
  54. * delete rows from the database
  55. */
  56. public function delete($records) {
  57. if (permission_exists($this->name.'_delete')) {
  58. //add multi-lingual support
  59. $language = new text;
  60. $text = $language->get();
  61. //validate the token
  62. $token = new token;
  63. if (!$token->validate($_SERVER['PHP_SELF'])) {
  64. message::add($text['message-invalid_token'],'negative');
  65. header('Location: '.$this->location);
  66. exit;
  67. }
  68. //delete multiple records
  69. if (is_array($records) && @sizeof($records) != 0) {
  70. //build the delete array
  71. $x = 0;
  72. foreach ($records as $record) {
  73. //add to the array
  74. if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
  75. //get the user_uuid
  76. $user_uuid = $record['uuid'];
  77. //get the user's domain from v_users
  78. if (permission_exists('user_domain')) {
  79. $sql = "select domain_uuid from v_users ";
  80. $sql .= "where user_uuid = :user_uuid ";
  81. $parameters['user_uuid'] = $user_uuid;
  82. $database = new database;
  83. $domain_uuid = $database->select($sql, $parameters, 'column');
  84. unset($sql, $parameters);
  85. }
  86. else {
  87. $domain_uuid = $_SESSION['domain_uuid'];
  88. }
  89. //required to be a superadmin to delete a member of the superadmin group
  90. $superadmin_list = superadmin_list();
  91. if (if_superadmin($superadmin_list, $user_uuid)) {
  92. if (!if_group("superadmin")) {
  93. //access denied - do not delete the user
  94. header("Location: index.php");
  95. return;
  96. }
  97. }
  98. //delete the user settings
  99. $array['user_settings'][$x]['user_uuid'] = $user_uuid;
  100. $array['user_settings'][$x]['domain_uuid'] = $domain_uuid;
  101. //delete the groups the user is assigned to
  102. $array['user_groups'][$x]['user_uuid'] = $user_uuid;
  103. $array['user_groups'][$x]['domain_uuid'] = $domain_uuid;
  104. //delete the user
  105. $array['users'][$x]['user_uuid'] = $user_uuid;
  106. $array['users'][$x]['domain_uuid'] = $domain_uuid;
  107. //increment the id
  108. $x++;
  109. }
  110. }
  111. //delete the checked rows
  112. if (is_array($array) && @sizeof($array) != 0) {
  113. //execute
  114. $p = permissions::new();
  115. $p->add('user_setting_delete', 'temp');
  116. $p->add('user_group_delete', 'temp');
  117. //execute delete
  118. $database = new database;
  119. $database->app_name = $this->app_name;
  120. $database->app_uuid = $this->app_uuid;
  121. $database->delete($array);
  122. unset($array);
  123. $p->delete('user_setting_delete', 'temp');
  124. $p->delete('user_group_delete', 'temp');
  125. //set message
  126. message::add($text['message-delete']);
  127. }
  128. unset($records);
  129. }
  130. }
  131. }
  132. /**
  133. * toggle a field between two values
  134. */
  135. public function toggle($records) {
  136. if (permission_exists($this->name.'_edit')) {
  137. //add multi-lingual support
  138. $language = new text;
  139. $text = $language->get();
  140. //validate the token
  141. $token = new token;
  142. if (!$token->validate($_SERVER['PHP_SELF'])) {
  143. message::add($text['message-invalid_token'],'negative');
  144. header('Location: '.$this->location);
  145. exit;
  146. }
  147. //toggle the checked records
  148. if (is_array($records) && @sizeof($records) != 0) {
  149. //get current toggle state
  150. foreach($records as $record) {
  151. if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
  152. $uuids[] = "'".$record['uuid']."'";
  153. }
  154. }
  155. if (is_array($uuids) && @sizeof($uuids) != 0) {
  156. $sql = "select ".$this->name."_uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
  157. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  158. $database = new database;
  159. $rows = $database->select($sql, $parameters ?? null, 'all');
  160. if (is_array($rows) && @sizeof($rows) != 0) {
  161. foreach ($rows as $row) {
  162. $states[$row['uuid']] = $row['toggle'];
  163. }
  164. }
  165. unset($sql, $parameters, $rows, $row);
  166. }
  167. //build update array
  168. $x = 0;
  169. foreach($states as $uuid => $state) {
  170. //create the array
  171. $array[$this->table][$x][$this->name.'_uuid'] = $uuid;
  172. $array[$this->table][$x][$this->toggle_field] = $state == $this->toggle_values[0] ? $this->toggle_values[1] : $this->toggle_values[0];
  173. //increment the id
  174. $x++;
  175. }
  176. //save the changes
  177. if (is_array($array) && @sizeof($array) != 0) {
  178. //save the array
  179. $database = new database;
  180. $database->app_name = $this->app_name;
  181. $database->app_uuid = $this->app_uuid;
  182. $database->save($array);
  183. unset($array);
  184. //set message
  185. message::add($text['message-toggle']);
  186. }
  187. unset($records, $states);
  188. }
  189. }
  190. }
  191. /**
  192. * copy rows from the database
  193. */
  194. public function copy($records) {
  195. if (permission_exists($this->name.'_add')) {
  196. //add multi-lingual support
  197. $language = new text;
  198. $text = $language->get();
  199. //validate the token
  200. $token = new token;
  201. if (!$token->validate($_SERVER['PHP_SELF'])) {
  202. message::add($text['message-invalid_token'],'negative');
  203. header('Location: '.$this->location);
  204. exit;
  205. }
  206. //copy the checked records
  207. if (!empty($records) && is_array($records) && @sizeof($records) != 0) {
  208. //get checked records
  209. foreach($records as $record) {
  210. if (!empty($record['checked']) && $record['checked'] == 'true' && is_uuid($record['uuid'])) {
  211. $uuids[] = "'".$record['uuid']."'";
  212. }
  213. }
  214. //create the array from existing data
  215. if (!empty($uuids) && is_array($uuids) && @sizeof($uuids) != 0) {
  216. $sql = "select * from v_".$this->table." ";
  217. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  218. $database = new database;
  219. $rows = $database->select($sql, $parameters ?? null, 'all');
  220. if (is_array($rows) && @sizeof($rows) != 0) {
  221. $x = 0;
  222. foreach ($rows as $row) {
  223. //copy data
  224. $array[$this->table][$x] = $row;
  225. //add copy to the description
  226. $array[$this->table][$x][$this->name.'_uuid'] = uuid();
  227. $array[$this->table][$x]['username'] = $row['username'].'-'.$text['label-copy'];
  228. //increment the id
  229. $x++;
  230. }
  231. }
  232. unset($sql, $parameters, $rows, $row);
  233. }
  234. //save the changes and set the message
  235. if (!empty($array) && is_array($array) && @sizeof($array) != 0) {
  236. //save the array
  237. $database = new database;
  238. $database->app_name = $this->app_name;
  239. $database->app_uuid = $this->app_uuid;
  240. $database->save($array);
  241. unset($array);
  242. //set message
  243. message::add($text['message-copy']);
  244. }
  245. unset($records);
  246. }
  247. }
  248. }
  249. /**
  250. * Remove old user log entries. Called the maintenance service application.
  251. * @param settings $settings
  252. * @return void
  253. */
  254. public static function database_maintenance(settings $settings): void {
  255. $database = $settings->database();
  256. $domains = maintenance_service::get_domains($database);
  257. foreach ($domains as $domain_uuid => $domain_name) {
  258. $domain_settings = new settings(['database' => $database, 'domain_uuid' => $domain_uuid]);
  259. $retention_days = $domain_settings->get('users', 'database_retention_days', '');
  260. if (!empty($retention_days) && is_numeric($retention_days)) {
  261. $sql = "delete from v_user_logs where timestamp < NOW() - INTERVAL '$retention_days days'";
  262. $sql.= " and domain_uuid = '$domain_uuid'";
  263. $database->execute($sql);
  264. $code = $database->message['code'] ?? 0;
  265. if ($code == 200) {
  266. maintenance_service::log_write(self::class, "Successfully removed entries older than $retention_days", $domain_uuid);
  267. } else {
  268. $message = $database->message['message'] ?? "An unknown error has occurred";
  269. maintenance_service::log_write(self::class, "Unable to remove old database records. Error message: $message ($code)", $domain_uuid, maintenance_service::LOG_ERROR);
  270. }
  271. } else {
  272. maintenance_service::log_write(self::class, "Database retention days not set or not numeric", $domain_uuid);
  273. }
  274. }
  275. }
  276. }
  277. }
  278. ?>