user_edit.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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) 2008-2024
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. Luis Daniel Lucio Quiroz <[email protected]>
  21. */
  22. //includes files
  23. require_once dirname(__DIR__, 2) . "/resources/require.php";
  24. require_once "resources/check_auth.php";
  25. //add multi-lingual support
  26. $language = new text;
  27. $text = $language->get();
  28. //create a single database object
  29. $database = new database;
  30. $database->app_name = 'users';
  31. $database->app_uuid = '112124b3-95c2-5352-7e9d-d14c0b88f207';
  32. //get user uuid
  33. if (!empty($_REQUEST["id"]) && ((is_uuid($_REQUEST["id"]) && permission_exists('user_edit')) || (is_uuid($_REQUEST["id"]) && $_REQUEST["id"] == $_SESSION['user_uuid']))) {
  34. $user_uuid = $_REQUEST["id"];
  35. $action = 'edit';
  36. }
  37. elseif (permission_exists('user_add') && !isset($_REQUEST["id"])) {
  38. $user_uuid = uuid();
  39. $action = 'add';
  40. }
  41. else {
  42. // load users own account
  43. header("Location: user_edit.php?id=".urlencode($_SESSION['user_uuid']));
  44. exit;
  45. }
  46. //get total user count from the database, check limit, if defined
  47. if (permission_exists('user_add') && $action == 'add' && !empty($_SESSION['limit']['users']['numeric'])) {
  48. $sql = "select count(*) ";
  49. $sql .= "from v_users ";
  50. $sql .= "where domain_uuid = :domain_uuid ";
  51. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  52. $num_rows = $database->select($sql, $parameters, 'column');
  53. unset($sql, $parameters);
  54. if ($num_rows >= $_SESSION['limit']['users']['numeric']) {
  55. message::add($text['message-maximum_users'].' '.$_SESSION['limit']['users']['numeric'], 'negative');
  56. header('Location: users.php');
  57. exit;
  58. }
  59. }
  60. //required to be a superadmin to update an account that is a member of the superadmin group
  61. if (permission_exists('user_edit') && $action == 'edit') {
  62. $superadmins = superadmin_list();
  63. if (if_superadmin($superadmins, $user_uuid)) {
  64. if (!if_group("superadmin")) {
  65. echo "access denied";
  66. exit;
  67. }
  68. }
  69. }
  70. //delete the group from the user
  71. if (!empty($_GET["a"]) && $_GET["a"] == "delete" && is_uuid($_GET["group_uuid"]) && is_uuid($user_uuid) && permission_exists("user_delete")) {
  72. //set the variables
  73. $group_uuid = $_GET["group_uuid"];
  74. //delete the group from the users
  75. $array['user_groups'][0]['group_uuid'] = $group_uuid;
  76. $array['user_groups'][0]['user_uuid'] = $user_uuid;
  77. $p = permissions::new();
  78. $p->add('user_group_delete', 'temp');
  79. $database->delete($array);
  80. unset($array);
  81. $p->delete('user_group_delete', 'temp');
  82. //redirect the user
  83. message::add($text['message-update']);
  84. header("Location: user_edit.php?id=".urlencode($user_uuid));
  85. exit;
  86. }
  87. //retrieve password requirements
  88. if (permission_exists('user_password')) {
  89. $required['length'] = $_SESSION['users']['password_length']['numeric'];
  90. $required['number'] = ($_SESSION['users']['password_number']['boolean'] == 'true') ? true : false;
  91. $required['lowercase'] = ($_SESSION['users']['password_lowercase']['boolean'] == 'true') ? true : false;
  92. $required['uppercase'] = ($_SESSION['users']['password_uppercase']['boolean'] == 'true') ? true : false;
  93. $required['special'] = ($_SESSION['users']['password_special']['boolean'] == 'true') ? true : false;
  94. }
  95. //prepare the data
  96. if (!empty($_POST)) {
  97. //get the HTTP values and set as variables
  98. if (permission_exists('user_edit') && $action == 'edit') {
  99. $user_uuid = $_REQUEST["id"];
  100. $username_old = $_POST["username_old"];
  101. }
  102. $domain_uuid = $_POST["domain_uuid"];
  103. $username = $_POST["username"];
  104. $password = $_POST["password"];
  105. $password_confirm = $_POST["password_confirm"];
  106. $user_email = $_POST["user_email"];
  107. $user_status = $_POST["user_status"] ?? '';
  108. $user_language = $_POST["user_language"];
  109. $user_time_zone = $_POST["user_time_zone"];
  110. if (permission_exists('contact_edit') && $action == 'edit') {
  111. $contact_uuid = $_POST["contact_uuid"];
  112. }
  113. else if (permission_exists('contact_add') && $action == 'add') {
  114. $contact_organization = $_POST["contact_organization"];
  115. $contact_name_given = $_POST["contact_name_given"];
  116. $contact_name_family = $_POST["contact_name_family"];
  117. }
  118. $group_uuid_name = $_POST["group_uuid_name"];
  119. $user_type = $_POST["user_type"];
  120. $user_enabled = $_POST["user_enabled"] ?? 'false';
  121. if (permission_exists('api_key')) {
  122. $api_key = $_POST["api_key"];
  123. }
  124. if (permission_exists('message_key')) {
  125. $message_key = $_POST["message_key"];
  126. }
  127. if (!empty($_SESSION['authentication']['methods']) && in_array('totp', $_SESSION['authentication']['methods'])) {
  128. $user_totp_secret = strtoupper($_POST["user_totp_secret"]);
  129. }
  130. //validate the token
  131. $token = new token;
  132. if (!$token->validate($_SERVER['PHP_SELF'])) {
  133. message::add($text['message-invalid_token'],'negative');
  134. header('Location: users.php');
  135. exit;
  136. }
  137. //validate the user status
  138. switch ($user_status) {
  139. case "Available" :
  140. break;
  141. case "Available (On Demand)" :
  142. break;
  143. case "On Break" :
  144. break;
  145. case "Do Not Disturb" :
  146. break;
  147. case "Logged Out" :
  148. break;
  149. default :
  150. $user_status = '';
  151. }
  152. //check required values
  153. if (empty($username)) {
  154. $invalid[] = $text['label-username'];
  155. }
  156. //require a username format: any, email, no_email
  157. if (!empty($_SESSION['users']['username_format']['text']) && $_SESSION['users']['username_format']['text'] != 'any') {
  158. if (
  159. ($_SESSION['users']['username_format']['text'] == 'email' && !valid_email($username)) ||
  160. ($_SESSION['users']['username_format']['text'] == 'no_email' && valid_email($username))
  161. ) {
  162. message::add($text['message-username_format_invalid'], 'negative', 7500);
  163. }
  164. }
  165. //require unique globally or per domain
  166. if ((permission_exists('user_edit') && $action == 'edit' && $username != $username_old && !empty($username)) ||
  167. (permission_exists('user_add') && $action == 'add' && !empty($username))) {
  168. $sql = "select count(*) from v_users ";
  169. if (isset($_SESSION["users"]["unique"]["text"]) && $_SESSION["users"]["unique"]["text"] == "global") {
  170. $sql .= "where username = :username ";
  171. }
  172. else {
  173. $sql .= "where username = :username ";
  174. $sql .= "and domain_uuid = :domain_uuid ";
  175. $parameters['domain_uuid'] = $domain_uuid;
  176. }
  177. $parameters['username'] = $username;
  178. $num_rows = $database->select($sql, $parameters, 'column');
  179. if ($num_rows > 0) {
  180. message::add($text['message-username_exists'], 'negative', 7500);
  181. }
  182. unset($sql, $parameters);
  183. }
  184. //require the passwords to match
  185. if (!empty($password) && $password != $password_confirm) {
  186. message::add($text['message-password_mismatch'], 'negative', 7500);
  187. }
  188. //require passwords not allowed to be empty
  189. if (permission_exists('user_password') && permission_exists('user_add') && $action == 'add') {
  190. if (empty($password)) {
  191. message::add($text['message-password_blank'], 'negative', 7500);
  192. }
  193. if (empty($group_uuid_name)) {
  194. $invalid[] = $text['label-group'];
  195. }
  196. }
  197. //require a value a valid email address format
  198. if (!valid_email($user_email)) {
  199. $invalid[] = $text['label-email'];
  200. }
  201. //require passwords with the defined required attributes: length, number, lower case, upper case, and special characters
  202. if (permission_exists('user_password') && !empty($password)) {
  203. if (!empty($required['length']) && is_numeric($required['length']) && $required['length'] != 0) {
  204. if (strlen($password) < $required['length']) {
  205. $invalid[] = $text['label-characters'];
  206. }
  207. }
  208. if ($required['number']) {
  209. if (!preg_match('/(?=.*[\d])/', $password)) {
  210. $invalid[] = $text['label-numbers'];
  211. }
  212. }
  213. if ($required['lowercase']) {
  214. if (!preg_match('/(?=.*[a-z])/', $password)) {
  215. $invalid[] = $text['label-lowercase_letters'];
  216. }
  217. }
  218. if ($required['uppercase']) {
  219. if (!preg_match('/(?=.*[A-Z])/', $password)) {
  220. $invalid[] = $text['label-uppercase_letters'];
  221. }
  222. }
  223. if ($required['special']) {
  224. if (!preg_match('/(?=.*[\W])/', $password)) {
  225. $invalid[] = $text['label-special_characters'];
  226. }
  227. }
  228. }
  229. //return if error
  230. if (message::count() != 0 || !empty($invalid)) {
  231. if ($invalid) { message::add($text['message-required'].implode(', ', $invalid), 'negative', 7500); }
  232. persistent_form_values('store', $_POST);
  233. header("Location: user_edit.php".(permission_exists('user_edit') && $action != 'add' ? "?id=".urlencode($user_uuid) : null));
  234. exit;
  235. }
  236. else {
  237. persistent_form_values('clear');
  238. }
  239. //save the data
  240. $i = $n = $x = $c = 0; //set initial array indexes
  241. //check to see if user language is set
  242. $sql = "select user_setting_uuid, user_setting_value from v_user_settings ";
  243. $sql .= "where user_setting_category = 'domain' ";
  244. $sql .= "and user_setting_subcategory = 'language' ";
  245. $sql .= "and user_uuid = :user_uuid ";
  246. $parameters['user_uuid'] = $user_uuid;
  247. $row = $database->select($sql, $parameters, 'row');
  248. if (!empty($user_language) && (empty($row) || (!empty($row['user_setting_uuid']) && !is_uuid($row['user_setting_uuid'])))) {
  249. //add user setting to array for insert
  250. $array['user_settings'][$i]['user_setting_uuid'] = uuid();
  251. $array['user_settings'][$i]['user_uuid'] = $user_uuid;
  252. $array['user_settings'][$i]['domain_uuid'] = $domain_uuid;
  253. $array['user_settings'][$i]['user_setting_category'] = 'domain';
  254. $array['user_settings'][$i]['user_setting_subcategory'] = 'language';
  255. $array['user_settings'][$i]['user_setting_name'] = 'code';
  256. $array['user_settings'][$i]['user_setting_value'] = $user_language;
  257. $array['user_settings'][$i]['user_setting_enabled'] = 'true';
  258. $i++;
  259. }
  260. else {
  261. if (empty($row['user_setting_value']) || empty($user_language)) {
  262. $array_delete['user_settings'][0]['user_setting_category'] = 'domain';
  263. $array_delete['user_settings'][0]['user_setting_subcategory'] = 'language';
  264. $array_delete['user_settings'][0]['user_uuid'] = $user_uuid;
  265. $p = permissions::new();
  266. $p->add('user_setting_delete', 'temp');
  267. $database->delete($array_delete);
  268. unset($array_delete);
  269. $p->delete('user_setting_delete', 'temp');
  270. }
  271. if (!empty($user_language)) {
  272. //add user setting to array for update
  273. $array['user_settings'][$i]['user_setting_uuid'] = $row['user_setting_uuid'];
  274. $array['user_settings'][$i]['user_uuid'] = $user_uuid;
  275. $array['user_settings'][$i]['domain_uuid'] = $domain_uuid;
  276. $array['user_settings'][$i]['user_setting_category'] = 'domain';
  277. $array['user_settings'][$i]['user_setting_subcategory'] = 'language';
  278. $array['user_settings'][$i]['user_setting_name'] = 'code';
  279. $array['user_settings'][$i]['user_setting_value'] = $user_language;
  280. $array['user_settings'][$i]['user_setting_enabled'] = 'true';
  281. $i++;
  282. }
  283. }
  284. unset($sql, $parameters, $row);
  285. //check to see if user time zone is set
  286. $sql = "select user_setting_uuid, user_setting_value from v_user_settings ";
  287. $sql .= "where user_setting_category = 'domain' ";
  288. $sql .= "and user_setting_subcategory = 'time_zone' ";
  289. $sql .= "and user_uuid = :user_uuid ";
  290. $parameters['user_uuid'] = $user_uuid;
  291. $row = $database->select($sql, $parameters, 'row');
  292. if (!empty($user_time_zone) && (empty($row) || (!empty($row['user_setting_uuid']) && !is_uuid($row['user_setting_uuid'])))) {
  293. //add user setting to array for insert
  294. $array['user_settings'][$i]['user_setting_uuid'] = uuid();
  295. $array['user_settings'][$i]['user_uuid'] = $user_uuid;
  296. $array['user_settings'][$i]['domain_uuid'] = $domain_uuid;
  297. $array['user_settings'][$i]['user_setting_category'] = 'domain';
  298. $array['user_settings'][$i]['user_setting_subcategory'] = 'time_zone';
  299. $array['user_settings'][$i]['user_setting_name'] = 'name';
  300. $array['user_settings'][$i]['user_setting_value'] = $user_time_zone;
  301. $array['user_settings'][$i]['user_setting_enabled'] = 'true';
  302. $i++;
  303. }
  304. else {
  305. if (empty($row['user_setting_value']) || empty($user_time_zone)) {
  306. $array_delete['user_settings'][0]['user_setting_category'] = 'domain';
  307. $array_delete['user_settings'][0]['user_setting_subcategory'] = 'time_zone';
  308. $array_delete['user_settings'][0]['user_uuid'] = $user_uuid;
  309. $p = permissions::new();
  310. $p->add('user_setting_delete', 'temp');
  311. $database->delete($array_delete);
  312. unset($array_delete);
  313. $p->delete('user_setting_delete', 'temp');
  314. }
  315. if (!empty($user_time_zone)) {
  316. //add user setting to array for update
  317. $array['user_settings'][$i]['user_setting_uuid'] = $row['user_setting_uuid'];
  318. $array['user_settings'][$i]['user_uuid'] = $user_uuid;
  319. $array['user_settings'][$i]['domain_uuid'] = $domain_uuid;
  320. $array['user_settings'][$i]['user_setting_category'] = 'domain';
  321. $array['user_settings'][$i]['user_setting_subcategory'] = 'time_zone';
  322. $array['user_settings'][$i]['user_setting_name'] = 'name';
  323. $array['user_settings'][$i]['user_setting_value'] = $user_time_zone;
  324. $array['user_settings'][$i]['user_setting_enabled'] = 'true';
  325. $i++;
  326. }
  327. }
  328. unset($sql, $parameters, $row);
  329. //check to see if message key is set
  330. if (permission_exists('message_key')) {
  331. $sql = "select user_setting_uuid, user_setting_value from v_user_settings ";
  332. $sql .= "where user_setting_category = 'message' ";
  333. $sql .= "and user_setting_subcategory = 'key' ";
  334. $sql .= "and user_uuid = :user_uuid ";
  335. $parameters['user_uuid'] = $user_uuid;
  336. $row = $database->select($sql, $parameters, 'row');
  337. if (!empty($message_key) && (empty($row) || (!empty($row['user_setting_uuid']) && !is_uuid($row['user_setting_uuid'])))) {
  338. //add user setting to array for insert
  339. $array['user_settings'][$i]['user_setting_uuid'] = uuid();
  340. $array['user_settings'][$i]['user_uuid'] = $user_uuid;
  341. $array['user_settings'][$i]['domain_uuid'] = $domain_uuid;
  342. $array['user_settings'][$i]['user_setting_category'] = 'message';
  343. $array['user_settings'][$i]['user_setting_subcategory'] = 'key';
  344. $array['user_settings'][$i]['user_setting_name'] = 'text';
  345. $array['user_settings'][$i]['user_setting_value'] = $message_key;
  346. $array['user_settings'][$i]['user_setting_enabled'] = 'true';
  347. $i++;
  348. }
  349. else {
  350. if (empty($row['user_setting_value']) || empty($message_key)) {
  351. $array_delete['user_settings'][0]['user_setting_category'] = 'message';
  352. $array_delete['user_settings'][0]['user_setting_subcategory'] = 'key';
  353. $array_delete['user_settings'][0]['user_uuid'] = $user_uuid;
  354. $p = permissions::new();
  355. $p->add('user_setting_delete', 'temp');
  356. $database->delete($array_delete);
  357. unset($array_delete);
  358. $p->delete('user_setting_delete', 'temp');
  359. }
  360. if (!empty($message_key)) {
  361. //add user setting to array for update
  362. $array['user_settings'][$i]['user_setting_uuid'] = $row['user_setting_uuid'];
  363. $array['user_settings'][$i]['user_uuid'] = $user_uuid;
  364. $array['user_settings'][$i]['domain_uuid'] = $domain_uuid;
  365. $array['user_settings'][$i]['user_setting_category'] = 'message';
  366. $array['user_settings'][$i]['user_setting_subcategory'] = 'key';
  367. $array['user_settings'][$i]['user_setting_name'] = 'text';
  368. $array['user_settings'][$i]['user_setting_value'] = $message_key;
  369. $array['user_settings'][$i]['user_setting_enabled'] = 'true';
  370. $i++;
  371. }
  372. }
  373. }
  374. unset($sql, $parameters, $row);
  375. //assign the user to the group
  376. if ((permission_exists('user_add') || permission_exists('user_edit')) && !empty($_REQUEST["group_uuid_name"])) {
  377. $group_data = explode('|', $group_uuid_name);
  378. $group_uuid = $group_data[0];
  379. $group_name = $group_data[1];
  380. //compare the group level to only add groups at the same level or lower than the user
  381. $sql = "select * from v_groups ";
  382. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  383. $sql .= "and group_uuid = :group_uuid ";
  384. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  385. $parameters['group_uuid'] = $group_uuid;
  386. $row = $database->select($sql, $parameters, 'row');
  387. if ($row['group_level'] <= $_SESSION['user']['group_level']) {
  388. $array['user_groups'][$n]['user_group_uuid'] = uuid();
  389. $array['user_groups'][$n]['domain_uuid'] = $domain_uuid;
  390. $array['user_groups'][$n]['group_name'] = $group_name;
  391. $array['user_groups'][$n]['group_uuid'] = $group_uuid;
  392. $array['user_groups'][$n]['user_uuid'] = $user_uuid;
  393. $n++;
  394. }
  395. unset($parameters);
  396. }
  397. //update domain, if changed
  398. if ((permission_exists('user_add') || permission_exists('user_edit')) && permission_exists('user_domain')) {
  399. //adjust group user records
  400. $sql = "select user_group_uuid from v_user_groups ";
  401. $sql .= "where user_uuid = :user_uuid ";
  402. $parameters['user_uuid'] = $user_uuid;
  403. $result = $database->select($sql, $parameters, 'all');
  404. if (is_array($result)) {
  405. foreach ($result as $row) {
  406. //add group user to array for update
  407. $array['user_groups'][$n]['user_group_uuid'] = $row['user_group_uuid'];
  408. $array['user_groups'][$n]['domain_uuid'] = $domain_uuid;
  409. $n++;
  410. }
  411. }
  412. unset($sql, $parameters);
  413. //adjust user setting records
  414. $sql = "select user_setting_uuid from v_user_settings ";
  415. $sql .= "where user_uuid = :user_uuid ";
  416. $parameters['user_uuid'] = $user_uuid;
  417. $result = $database->select($sql, $parameters);
  418. if (is_array($result)) {
  419. foreach ($result as $row) {
  420. //add user setting to array for update
  421. $array['user_settings'][$i]['user_setting_uuid'] = $row['user_setting_uuid'];
  422. $array['user_settings'][$i]['domain_uuid'] = $domain_uuid;
  423. $i++;
  424. }
  425. }
  426. unset($sql, $parameters);
  427. //unassign any foreign domain groups
  428. $sql = "delete from v_user_groups ";
  429. $sql .= "where domain_uuid = :domain_uuid ";
  430. $sql .= "and user_uuid = :user_uuid ";
  431. $sql .= "and group_uuid not in (";
  432. $sql .= " select group_uuid from v_groups where domain_uuid = :domain_uuid or domain_uuid is null ";
  433. $sql .= ") ";
  434. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  435. $parameters['user_uuid'] = $user_uuid;
  436. $database->execute($sql, $parameters);
  437. unset($sql, $parameters);
  438. }
  439. //add contact to array for insert
  440. if ($action == 'add' && permission_exists('user_add') && permission_exists('contact_add')) {
  441. $contact_uuid = uuid();
  442. $array['contacts'][$c]['domain_uuid'] = $domain_uuid;
  443. $array['contacts'][$c]['contact_uuid'] = $contact_uuid;
  444. $array['contacts'][$c]['contact_type'] = 'user';
  445. $array['contacts'][$c]['contact_organization'] = $contact_organization;
  446. $array['contacts'][$c]['contact_name_given'] = $contact_name_given;
  447. $array['contacts'][$c]['contact_name_family'] = $contact_name_family;
  448. $array['contacts'][$c]['contact_nickname'] = $username;
  449. $c++;
  450. if (permission_exists('contact_email_add')) {
  451. $contact_email_uuid = uuid();
  452. $array['contact_emails'][$c]['contact_email_uuid'] = $contact_email_uuid;
  453. $array['contact_emails'][$c]['domain_uuid'] = $domain_uuid;
  454. $array['contact_emails'][$c]['contact_uuid'] = $contact_uuid;
  455. $array['contact_emails'][$c]['email_address'] = $user_email;
  456. $array['contact_emails'][$c]['email_primary'] = '1';
  457. $c++;
  458. }
  459. }
  460. //set the password hash cost
  461. $options = array('cost' => 10);
  462. //add user setting to array for update
  463. $array['users'][$x]['user_uuid'] = $user_uuid;
  464. $array['users'][$x]['domain_uuid'] = $domain_uuid;
  465. if (!empty($username) && (empty($username_old) || $username != $username_old)) {
  466. $array['users'][$x]['username'] = $username;
  467. }
  468. if (permission_exists('user_password') && !empty($password) && $password == $password_confirm) {
  469. //remove the session id files
  470. $sql = "select session_id from v_user_logs ";
  471. $sql .= "where user_uuid = :user_uuid ";
  472. $sql .= "and timestamp > NOW() - INTERVAL '4 hours' ";
  473. $parameters['user_uuid'] = $user_uuid;
  474. $user_logs = $database->select($sql, $parameters, 'all');
  475. foreach ($user_logs as $row) {
  476. if (preg_match('/^[a-zA-Z0-9,-]+$/', $row['session_id']) && file_exists(session_save_path() . "/sess_" . $row['session_id'])) {
  477. unlink(session_save_path() . "/sess_" . $row['session_id']);
  478. }
  479. }
  480. //create a one way hash for the user password
  481. $array['users'][$x]['password'] = password_hash($password, PASSWORD_DEFAULT, $options);
  482. $array['users'][$x]['salt'] = null;
  483. }
  484. $array['users'][$x]['user_email'] = $user_email;
  485. $array['users'][$x]['user_status'] = $user_status;
  486. if (permission_exists('user_add') || permission_exists('user_edit')) {
  487. if (permission_exists('api_key')) {
  488. $array['users'][$x]['api_key'] = (!empty($api_key)) ? $api_key : null;
  489. }
  490. if (!empty($_SESSION['authentication']['methods']) && in_array('totp', $_SESSION['authentication']['methods'])) {
  491. $array['users'][$x]['user_totp_secret'] = $user_totp_secret;
  492. }
  493. $array['users'][$x]['user_type'] = $user_type;
  494. $array['users'][$x]['user_enabled'] = $user_enabled;
  495. if (permission_exists('contact_add')) {
  496. $array['users'][$x]['contact_uuid'] = (!empty($contact_uuid)) ? $contact_uuid : null;
  497. }
  498. if ($action == 'add') {
  499. $array['users'][$x]['add_user'] = $_SESSION["user"]["username"];
  500. $array['users'][$x]['add_date'] = date("Y-m-d H:i:s.uO");
  501. }
  502. }
  503. $x++;
  504. //add the user_edit permission
  505. $p = permissions::new();
  506. $p->add("user_setting_add", "temp");
  507. $p->add("user_setting_edit", "temp");
  508. $p->add("user_edit", "temp");
  509. $p->add('user_group_add', 'temp');
  510. //save the data
  511. $database->save($array);
  512. //$message = $database->message;
  513. //remove the temporary permission
  514. $p->delete("user_setting_add", "temp");
  515. $p->delete("user_setting_edit", "temp");
  516. $p->delete("user_edit", "temp");
  517. $p->delete('user_group_add', 'temp');
  518. //if call center installed
  519. if ($action == 'edit' && permission_exists('user_edit') && file_exists($_SERVER["PROJECT_ROOT"]."/app/call_centers/app_config.php")) {
  520. //get the call center agent uuid
  521. $sql = "select call_center_agent_uuid from v_call_center_agents ";
  522. $sql .= "where domain_uuid = :domain_uuid ";
  523. $sql .= "and user_uuid = :user_uuid ";
  524. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  525. $parameters['user_uuid'] = $user_uuid;
  526. $call_center_agent_uuid = $database->select($sql, $parameters, 'column');
  527. unset($sql, $parameters);
  528. //update the user_status
  529. if (isset($call_center_agent_uuid) && is_uuid($call_center_agent_uuid) && !empty($user_status)) {
  530. $esl = event_socket::create();
  531. $switch_cmd = "callcenter_config agent set status ".$call_center_agent_uuid." '".$user_status."'";
  532. $switch_result = event_socket::api($switch_cmd);
  533. }
  534. //update the user state
  535. if (isset($call_center_agent_uuid) && is_uuid($call_center_agent_uuid)) {
  536. $esl = event_socket::create();
  537. $cmd = "callcenter_config agent set state ".$call_center_agent_uuid." Waiting";
  538. $response = event_socket::api($cmd);
  539. }
  540. }
  541. //response message
  542. if ($action == 'edit') {
  543. message::add($text['message-update'],'positive');
  544. }
  545. else {
  546. message::add($text['message-add'],'positive');
  547. }
  548. header("Location: user_edit.php?id=".urlencode($user_uuid));
  549. exit;
  550. }
  551. //populate form
  552. if (persistent_form_values('exists')) {
  553. //populate the form with values from session variable
  554. persistent_form_values('load');
  555. //clear, set $unsaved flag
  556. persistent_form_values('clear');
  557. }
  558. else {
  559. //populate the form with values from db
  560. if ($action == 'edit') {
  561. $sql = "select domain_uuid, user_uuid, username, user_email, api_key, user_totp_secret, ";
  562. $sql .= "user_type, user_enabled, contact_uuid, cast(user_enabled as text), user_status ";
  563. $sql .= "from v_users ";
  564. $sql .= "where user_uuid = :user_uuid ";
  565. if (!permission_exists('user_all')) {
  566. $sql .= "and domain_uuid = :domain_uuid ";
  567. $parameters['domain_uuid'] = $domain_uuid;
  568. }
  569. $parameters['user_uuid'] = $user_uuid;
  570. $row = $database->select($sql, $parameters, 'row');
  571. if (is_array($row) && sizeof($row) > 0) {
  572. $domain_uuid = $row["domain_uuid"];
  573. $user_uuid = $row["user_uuid"];
  574. $username = $row["username"];
  575. $user_email = $row["user_email"];
  576. $api_key = $row["api_key"];
  577. $user_totp_secret = $row["user_totp_secret"];
  578. $user_type = $row["user_type"];
  579. $user_enabled = $row["user_enabled"];
  580. if (permission_exists('contact_view')) {
  581. $contact_uuid = $row["contact_uuid"];
  582. }
  583. $user_status = $row["user_status"];
  584. }
  585. else {
  586. message::add($text['message-invalid_user'], 'negative', 7500);
  587. header("Location: user_edit.php?id=".$_SESSION['user_uuid']);
  588. exit;
  589. }
  590. unset($sql, $parameters, $row);
  591. //get user settings
  592. $sql = "select * from v_user_settings ";
  593. $sql .= "where user_uuid = :user_uuid ";
  594. $sql .= "and user_setting_enabled = 'true' ";
  595. $parameters['user_uuid'] = $user_uuid;
  596. $result = $database->select($sql, $parameters, 'all');
  597. if (is_array($result)) {
  598. foreach($result as $row) {
  599. $name = $row['user_setting_name'];
  600. $category = $row['user_setting_category'];
  601. $subcategory = $row['user_setting_subcategory'];
  602. if (empty($subcategory)) {
  603. //$$category[$name] = $row['domain_setting_value'];
  604. $user_settings[$category][$name] = $row['user_setting_value'];
  605. }
  606. else {
  607. $user_settings[$category][$subcategory][$name] = $row['user_setting_value'];
  608. }
  609. }
  610. }
  611. unset($sql, $parameters, $result, $row);
  612. }
  613. }
  614. //set the defaults
  615. if (empty($user_enabled)) { $user_enabled = "true"; }
  616. if (empty($user_totp_secret)) { $user_totp_secret = ""; }
  617. //create token
  618. $object = new token;
  619. $token = $object->create($_SERVER['PHP_SELF']);
  620. //include the header
  621. require_once "resources/header.php";
  622. $document['title'] = $text['title-user_edit'];
  623. //show the content
  624. if (permission_exists('user_password')) {
  625. echo "<script>\n";
  626. echo " function compare_passwords() {\n";
  627. echo " if (document.getElementById('password') === document.activeElement || document.getElementById('password_confirm') === document.activeElement) {\n";
  628. echo " if ($('#password').val() != '' || $('#password_confirm').val() != '') {\n";
  629. echo " if ($('#password').val() != $('#password_confirm').val()) {\n";
  630. echo " $('#password').removeClass('formfld_highlight_good');\n";
  631. echo " $('#password_confirm').removeClass('formfld_highlight_good');\n";
  632. echo " $('#password').addClass('formfld_highlight_bad');\n";
  633. echo " $('#password_confirm').addClass('formfld_highlight_bad');\n";
  634. echo " }\n";
  635. echo " else {\n";
  636. echo " $('#password').removeClass('formfld_highlight_bad');\n";
  637. echo " $('#password_confirm').removeClass('formfld_highlight_bad');\n";
  638. echo " $('#password').addClass('formfld_highlight_good');\n";
  639. echo " $('#password_confirm').addClass('formfld_highlight_good');\n";
  640. echo " }\n";
  641. echo " }\n";
  642. echo " }\n";
  643. echo " else {\n";
  644. echo " $('#password').removeClass('formfld_highlight_bad');\n";
  645. echo " $('#password_confirm').removeClass('formfld_highlight_bad');\n";
  646. echo " $('#password').removeClass('formfld_highlight_good');\n";
  647. echo " $('#password_confirm').removeClass('formfld_highlight_good');\n";
  648. echo " }\n";
  649. echo " }\n";
  650. echo " function show_strength_meter() {\n";
  651. echo " $('#pwstrength_progress').slideDown();\n";
  652. echo " }\n";
  653. echo "</script>\n";
  654. }
  655. echo "<form name='frm' id='frm' method='post'>\n";
  656. echo "<div class='action_bar' id='action_bar'>\n";
  657. echo " <div class='heading'><b>".$text['header-user_edit']."</b></div>\n";
  658. echo " <div class='actions'>\n";
  659. if (!empty($unsaved)) {
  660. echo "<div class='unsaved'>".$text['message-unsaved_changes']." <i class='fas fa-exclamation-triangle'></i></div>";
  661. }
  662. if (permission_exists('user_add') || permission_exists('user_edit')) {
  663. echo button::create(['type'=>'button','label'=>$text['button-back'],'icon'=>$_SESSION['theme']['button_icon_back'],'id'=>'btn_back','link'=>'users.php']);
  664. }
  665. $button_margin = 'margin-left: 15px;';
  666. if (permission_exists('ticket_add') || permission_exists('ticket_edit')) {
  667. echo button::create(['type'=>'button','label'=>$text['button-tickets'],'icon'=>'tags','style'=>$button_margin,'link'=>PROJECT_PATH.'/app/tickets/tickets.php?user_uuid='.urlencode($user_uuid)]);
  668. unset($button_margin);
  669. }
  670. if (permission_exists('user_permissions') && file_exists('../../app/user_permissions/user_permissions.php')) {
  671. echo button::create(['type'=>'button','label'=>$text['button-permissions'],'icon'=>'key','style'=>$button_margin,'link'=>PROJECT_PATH.'/app/user_permissions/user_permissions.php?id='.urlencode($user_uuid)]);
  672. unset($button_margin);
  673. }
  674. if (permission_exists('user_setting_view')) {
  675. echo button::create(['type'=>'button','label'=>$text['button-settings'],'icon'=>$_SESSION['theme']['button_icon_settings'],'id'=>'btn_settings','style'=>'','link'=>PROJECT_PATH.'/core/user_settings/user_settings.php?id='.urlencode($user_uuid)]);
  676. }
  677. echo button::create(['type'=>'button','label'=>$text['button-save'],'icon'=>$_SESSION['theme']['button_icon_save'],'id'=>'btn_save','style'=>'margin-left: 15px;','onclick'=>'submit_form();']);
  678. echo " </div>\n";
  679. echo " <div style='clear: both;'></div>\n";
  680. echo "</div>\n";
  681. echo $text['description-user_edit']."\n";
  682. echo "<br /><br />\n";
  683. echo "<div class='card'>\n";
  684. echo "<table cellpadding='0' cellspacing='0' border='0' width='100%'>";
  685. echo " <tr>";
  686. echo " <td width='30%' class='vncellreq' valign='top'>".$text['label-username']."</td>";
  687. echo " <td width='70%' class='vtable'>";
  688. if (permission_exists("user_edit")) {
  689. echo " <input type='text' class='formfld' name='username' id='username' autocomplete='new-password' value='".escape($username ?? '')."' required='required'>\n";
  690. echo " <input type='text' style='display: none;' disabled='disabled'>\n"; //help defeat browser auto-fill
  691. }
  692. else {
  693. echo " ".escape($username)."\n";
  694. echo " <input type='hidden' name='username' id='username' autocomplete='new-password' value='".escape($username ?? '')."'>\n";
  695. }
  696. echo " </td>";
  697. echo " </tr>";
  698. if (permission_exists('user_password')) {
  699. echo " <tr>";
  700. echo " <td class='vncell".(($action == 'add') ? 'req' : null)."' valign='top'>".$text['label-password']."</td>";
  701. echo " <td class='vtable'>";
  702. echo " <input type='password' style='display: none;' disabled='disabled'>"; //help defeat browser auto-fill
  703. echo " <input type='password' autocomplete='new-password' class='formfld' name='password' id='password' value=\"".escape($password ?? null)."\" ".($action == 'add' ? "required='required'" : null)." onkeypress='show_strength_meter();' onfocus='compare_passwords();' onkeyup='compare_passwords();' onblur='compare_passwords();'>";
  704. echo " <div id='pwstrength_progress' class='pwstrength_progress'></div><br />\n";
  705. if ((!empty($required['length']) && is_numeric($required['length']) && $required['length'] != 0) || $required['number'] || $required['lowercase'] || $required['uppercase'] || $required['special']) {
  706. echo $text['label-required'].': ';
  707. if (is_numeric($required['length']) && $required['length'] != 0) {
  708. echo $required['length']." ".$text['label-characters'];
  709. if ($required['number'] || $required['lowercase'] || $required['uppercase'] || $required['special']) {
  710. echo " (";
  711. }
  712. }
  713. if ($required['number']) {
  714. $required_temp[] = $text['label-number'];
  715. }
  716. if ($required['lowercase']) {
  717. $required_temp[] = $text['label-lowercase'];
  718. }
  719. if ($required['uppercase']) {
  720. $required_temp[] = $text['label-uppercase'];
  721. }
  722. if ($required['special']) {
  723. $required_temp[] = $text['label-special'];
  724. }
  725. if (!empty($required_temp)) {
  726. echo implode(', ',$required_temp);
  727. if (is_numeric($required['length']) && $required['length'] != 0) {
  728. echo ")";
  729. }
  730. }
  731. unset($required_temp);
  732. }
  733. echo " </td>";
  734. echo " </tr>";
  735. echo " <tr>";
  736. echo " <td class='vncell".(($action == 'add') ? 'req' : null)."' valign='top'>".$text['label-confirm_password']."</td>";
  737. echo " <td class='vtable'>";
  738. echo " <input type='password' autocomplete='new-password' class='formfld' name='password_confirm' id='password_confirm' value=\"".escape($password_confirm ?? null)."\" ".($action == 'add' ? "required='required'" : null)." onfocus='compare_passwords();' onkeyup='compare_passwords();' onblur='compare_passwords();'><br />\n";
  739. echo " ".$text['message-green_border_passwords_match']."\n";
  740. echo " </td>";
  741. echo " </tr>";
  742. }
  743. echo " <tr>";
  744. echo " <td class='vncellreq'>".$text['label-email']."</td>";
  745. echo " <td class='vtable'><input type='text' class='formfld' name='user_email' value='".escape($user_email ?? '')."' required='required'></td>";
  746. echo " </tr>";
  747. echo " <tr>\n";
  748. echo " <td width='20%' class=\"vncell\" valign='top'>\n";
  749. echo " ".$text['label-user_language']."\n";
  750. echo " </td>\n";
  751. echo " <td class=\"vtable\" align='left'>\n";
  752. echo " <select id='user_language' name='user_language' class='formfld' style=''>\n";
  753. echo " <option value=''></option>\n";
  754. //get all language codes from database
  755. $sql = "select * from v_languages order by language asc ";
  756. $languages = $database->select($sql, null, 'all');
  757. if (!empty($languages) && is_array($languages) && sizeof($languages) != 0) {
  758. foreach ($languages as $row) {
  759. $language_codes[$row["code"]] = $row["language"];
  760. }
  761. }
  762. unset($sql, $languages, $row);
  763. if (is_array($_SESSION['app']['languages']) && sizeof($_SESSION['app']['languages']) != 0) {
  764. foreach ($_SESSION['app']['languages'] as $code) {
  765. $selected = (isset($user_language) && $code == $user_language) || (isset($user_settings['domain']['language']['code']) && $code == $user_settings['domain']['language']['code']) ? "selected='selected'" : null;
  766. echo " <option value='".$code."' ".$selected.">".escape($language_codes[$code] ?? null)." [".escape($code ?? null)."]</option>\n";
  767. }
  768. }
  769. echo " </select>\n";
  770. echo " <br />\n";
  771. echo " ".$text['description-user_language']."<br />\n";
  772. echo " </td>\n";
  773. echo " </tr>\n";
  774. echo " <tr>\n";
  775. echo " <td width='20%' class=\"vncell\" valign='top'>\n";
  776. echo " ".$text['label-time_zone']."\n";
  777. echo " </td>\n";
  778. echo " <td class=\"vtable\" align='left'>\n";
  779. echo " <select id='user_time_zone' name='user_time_zone' class='formfld' style=''>\n";
  780. echo " <option value=''></option>\n";
  781. //$list = DateTimeZone::listAbbreviations();
  782. $time_zone_identifiers = DateTimeZone::listIdentifiers();
  783. $previous_category = '';
  784. $x = 0;
  785. foreach ($time_zone_identifiers as $key => $row) {
  786. $time_zone = explode("/", $row);
  787. $category = $time_zone[0];
  788. if ($category != $previous_category) {
  789. if ($x > 0) {
  790. echo " </optgroup>\n";
  791. }
  792. echo " <optgroup label='".$category."'>\n";
  793. }
  794. $selected = (isset($user_time_zone) && $row == $user_time_zone) || (!empty($user_settings['domain']['time_zone']) && $row == $user_settings['domain']['time_zone']['name']) ? "selected='selected'" : null;
  795. echo " <option value='".escape($row)."' ".$selected.">".escape($row)."</option>\n";
  796. $previous_category = $category;
  797. $x++;
  798. }
  799. echo " </select>\n";
  800. echo " <br />\n";
  801. echo " ".$text['description-time_zone']."<br />\n";
  802. echo " </td>\n";
  803. echo " </tr>\n";
  804. if (permission_exists("user_status")) {
  805. echo " <tr>\n";
  806. echo " <td width='20%' class=\"vncell\" valign='top'>\n";
  807. echo " ".$text['label-status']."\n";
  808. echo " </td>\n";
  809. echo " <td class=\"vtable\">\n";
  810. echo " <select id='user_status' name='user_status' class='formfld' style=''>\n";
  811. echo " <option value=''></option>\n";
  812. echo " <option value='Available' ".(($user_status == "Available") ? "selected='selected'" : null).">".$text['option-available']."</option>\n";
  813. echo " <option value='Available (On Demand)' ".(($user_status == "Available (On Demand)") ? "selected='selected'" : null).">".$text['option-available_on_demand']."</option>\n";
  814. echo " <option value='Logged Out' ".(($user_status == "Logged Out") ? "selected='selected'" : null).">".$text['option-logged_out']."</option>\n";
  815. echo " <option value='On Break' ".(($user_status == "On Break") ? "selected='selected'" : null).">".$text['option-on_break']."</option>\n";
  816. echo " <option value='Do Not Disturb' ".(($user_status == "Do Not Disturb") ? "selected='selected'" : null).">".$text['option-do_not_disturb']."</option>\n";
  817. echo " </select>\n";
  818. echo " <br />\n";
  819. echo " ".$text['description-status']."<br />\n";
  820. echo " </td>\n";
  821. echo " </tr>\n";
  822. }
  823. if ($action == 'edit' && permission_exists("user_edit") && permission_exists('contact_edit')) {
  824. echo " <tr>";
  825. echo " <td class='vncell' valign='top'>".$text['label-contact']."</td>";
  826. echo " <td class='vtable'>\n";
  827. $sql = "select ";
  828. $sql .= "c.contact_uuid, ";
  829. $sql .= "c.contact_organization, ";
  830. $sql .= "c.contact_name_given, ";
  831. $sql .= "c.contact_name_family, ";
  832. $sql .= "c.contact_nickname ";
  833. $sql .= "from ";
  834. $sql .= "v_contacts as c ";
  835. $sql .= "where ";
  836. $sql .= "c.domain_uuid = :domain_uuid ";
  837. $sql .= "and not exists ( ";
  838. $sql .= " select ";
  839. $sql .= " contact_uuid ";
  840. $sql .= " from ";
  841. $sql .= " v_users as u ";
  842. $sql .= " where ";
  843. $sql .= " u.domain_uuid = :domain_uuid ";
  844. if (is_uuid($contact_uuid)) { //don't exclude currently assigned contact
  845. $sql .= "and u.contact_uuid <> :contact_uuid ";
  846. $parameters['contact_uuid'] = $contact_uuid;
  847. }
  848. $sql .= " and u.contact_uuid = c.contact_uuid ";
  849. $sql .= ") ";
  850. $sql .= "order by ";
  851. $sql .= "lower(c.contact_organization) asc, ";
  852. $sql .= "lower(c.contact_name_family) asc, ";
  853. $sql .= "lower(c.contact_name_given) asc, ";
  854. $sql .= "lower(c.contact_nickname) asc ";
  855. $parameters['domain_uuid'] = $_SESSION['domain_uuid'];
  856. $contacts = $database->select($sql, $parameters, 'all');
  857. unset($parameters);
  858. echo "<select name=\"contact_uuid\" id=\"contact_uuid\" class=\"formfld\">\n";
  859. echo "<option value=\"\"></option>\n";
  860. foreach($contacts as $row) {
  861. $contact_name = array();
  862. if (!empty($row['contact_organization'])) { $contact_name[] = $row['contact_organization']; }
  863. if (!empty($row['contact_name_family'])) { $contact_name[] = $row['contact_name_family']; }
  864. if (!empty($row['contact_name_given'])) { $contact_name[] = $row['contact_name_given']; }
  865. if (!empty($row['contact_name_family']) && empty($row['contact_name_family']) && !empty($row['contact_nickname'])) { $contact_name[] = $row['contact_nickname']; }
  866. echo "<option value='".escape($row['contact_uuid'])."' ".(($row['contact_uuid'] == $contact_uuid) ? "selected='selected'" : null).">".escape(implode(', ', $contact_name))."</option>\n";
  867. }
  868. unset($sql, $row_count);
  869. echo "</select>\n";
  870. echo "<br />\n";
  871. echo $text['description-contact']."\n";
  872. if (!empty($contact_uuid)) {
  873. echo " <a href=\"".PROJECT_PATH."/core/contacts/contact_edit.php?id=".urlencode($contact_uuid)."\">".$text['description-contact_view']."</a>\n";
  874. }
  875. echo " </td>";
  876. echo " </tr>";
  877. }
  878. elseif ($action == 'add' && permission_exists("user_add") && permission_exists('contact_add')) {
  879. echo " <tr>";
  880. echo " <td class='vncell'>".$text['label-first_name']."</td>";
  881. echo " <td class='vtable'><input type='text' class='formfld' name='contact_name_given' value='".escape($contact_name_given ?? '')."'></td>";
  882. echo " </tr>";
  883. echo " <tr>";
  884. echo " <td class='vncell'>".$text['label-last_name']."</td>";
  885. echo " <td class='vtable'><input type='text' class='formfld' name='contact_name_family' value='".escape($contact_name_family ?? '')."'></td>";
  886. echo " </tr>";
  887. echo " <tr>";
  888. echo " <td class='vncell'>".$text['label-organization']."</td>";
  889. echo " <td class='vtable'><input type='text' class='formfld' name='contact_organization' value='".escape($contact_organization ?? '')."'></td>";
  890. echo " </tr>";
  891. }
  892. if (permission_exists("user_groups")) {
  893. echo " <tr>";
  894. echo " <td class='vncellreq' valign='top'>".$text['label-groups']."</td>";
  895. echo " <td class='vtable'>";
  896. $sql = "select ";
  897. $sql .= " ug.*, g.domain_uuid as group_domain_uuid ";
  898. $sql .= "from ";
  899. $sql .= " v_user_groups as ug, ";
  900. $sql .= " v_groups as g ";
  901. $sql .= "where ";
  902. $sql .= " ug.group_uuid = g.group_uuid ";
  903. $sql .= " and (";
  904. $sql .= " g.domain_uuid = :domain_uuid ";
  905. $sql .= " or g.domain_uuid is null ";
  906. $sql .= " ) ";
  907. $sql .= " and ug.domain_uuid = :domain_uuid ";
  908. $sql .= " and ug.user_uuid = :user_uuid ";
  909. $sql .= "order by ";
  910. $sql .= " g.domain_uuid desc, ";
  911. $sql .= " g.group_name asc ";
  912. $parameters['domain_uuid'] = $domain_uuid;
  913. $parameters['user_uuid'] = $user_uuid;
  914. $user_groups = $database->select($sql, $parameters, 'all');
  915. if (is_array($user_groups)) {
  916. echo "<table cellpadding='0' cellspacing='0' border='0'>\n";
  917. foreach($user_groups as $field) {
  918. if (!empty($field['group_name'])) {
  919. echo "<tr>\n";
  920. echo " <td class='vtable' style='white-space: nowrap; padding-right: 30px;' nowrap='nowrap'>";
  921. echo escape($field['group_name']).((!empty($field['group_domain_uuid'])) ? "@".$_SESSION['domains'][$field['group_domain_uuid']]['domain_name'] : null);
  922. echo " </td>\n";
  923. if (permission_exists('user_group_delete') || if_group("superadmin")) {
  924. echo " <td class='list_control_icons' style='width: 25px;'>\n";
  925. echo " <a href='user_edit.php?id=".urlencode($user_uuid)."&domain_uuid=".urlencode($domain_uuid)."&group_uuid=".urlencode($field['group_uuid'])."&a=delete' alt='".$text['button-delete']."' onclick=\"return confirm('".$text['confirm-delete']."')\">".$v_link_label_delete."</a>\n";
  926. echo " </td>\n";
  927. }
  928. echo "</tr>\n";
  929. if (is_uuid($field['group_uuid'])) {
  930. $assigned_groups[] = $field['group_uuid'];
  931. }
  932. }
  933. }
  934. echo "</table>\n";
  935. }
  936. unset($sql, $parameters, $user_groups, $field);
  937. $sql = "select * from v_groups ";
  938. $sql .= "where (domain_uuid = :domain_uuid or domain_uuid is null) ";
  939. if (!empty($assigned_groups) && is_array($assigned_groups) && sizeof($assigned_groups) > 0) {
  940. $sql .= "and group_uuid not in ('".implode("','",$assigned_groups)."') ";
  941. }
  942. $sql .= "order by domain_uuid desc, group_name asc ";
  943. $parameters['domain_uuid'] = $domain_uuid;
  944. $groups = $database->select($sql, $parameters, 'all');
  945. if (is_array($groups)) {
  946. if (isset($assigned_groups)) { echo "<br />\n"; }
  947. echo "<select name='group_uuid_name' class='formfld' style='width: auto; margin-right: 3px;' ".($action == 'add' ? "required='required'" : null).">\n";
  948. echo " <option value=''></option>\n";
  949. foreach($groups as $field) {
  950. if ($field['group_level'] <= $_SESSION['user']['group_level']) {
  951. if (!isset($assigned_groups) || (isset($assigned_groups) && !in_array($field["group_uuid"], $assigned_groups))) {
  952. if (isset($group_uuid_name) && $group_uuid_name == $field['group_uuid']."|".$field['group_name']) { $selected = "selected='selected'"; } else { $selected = ''; }
  953. echo " <option value='".$field['group_uuid']."|".$field['group_name']."' $selected>".$field['group_name'].((!empty($field['domain_uuid'])) ? "@".$_SESSION['domains'][$field['domain_uuid']]['domain_name'] : null)."</option>\n";
  954. }
  955. }
  956. }
  957. echo "</select>";
  958. if ($action == 'edit') {
  959. echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'onclick'=>'submit_form();']);
  960. }
  961. }
  962. unset($sql, $parameters, $groups, $field);
  963. echo " </td>";
  964. echo " </tr>";
  965. }
  966. if (permission_exists('user_type')) {
  967. echo "<tr>\n";
  968. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  969. echo " ".$text['label-user_type']."\n";
  970. echo "</td>\n";
  971. echo "<td class='vtable' align='left'>\n";
  972. echo " <select class='formfld' name='user_type' id='user_type'>\n";
  973. echo " <option value='default' ".(($user_type == "default") ? "selected='selected'" : null).">".$text['label-default']."</option>\n";
  974. echo " <option value='virtual' ".(($user_type == "virtual") ? "selected='selected'" : null).">".$text['label-virtual']."</option>\n";
  975. echo " </select>\n";
  976. echo "<br />\n";
  977. echo $text['description-user_type']."\n";
  978. echo "</td>\n";
  979. echo "</tr>\n";
  980. }
  981. if (permission_exists('user_domain')) {
  982. echo "<tr>\n";
  983. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  984. echo " ".$text['label-domain']."\n";
  985. echo "</td>\n";
  986. echo "<td class='vtable' align='left'>\n";
  987. echo " <select class='formfld' name='domain_uuid'>\n";
  988. foreach ($_SESSION['domains'] as $row) {
  989. echo " <option value='".escape($row['domain_uuid'])."' ".(($row['domain_uuid'] == $domain_uuid) ? "selected='selected'" : null).">".escape($row['domain_name'])."</option>\n";
  990. }
  991. echo " </select>\n";
  992. echo "<br />\n";
  993. echo $text['description-domain_name']."\n";
  994. echo "</td>\n";
  995. echo "</tr>\n";
  996. }
  997. else {
  998. echo "<input type='hidden' name='domain_uuid' value='".escape($domain_uuid)."'>";
  999. }
  1000. if (permission_exists('api_key')) {
  1001. echo " <tr>";
  1002. echo " <td class='vncell' valign='top'>".$text['label-api_key']."</td>";
  1003. echo " <td class='vtable'>\n";
  1004. echo " <input type='text' class='formfld' style='width: 250px; display: none;' name='api_key' id='api_key' value=\"".escape($api_key ?? '')."\" >";
  1005. if (empty($api_key)) {
  1006. //generate api key
  1007. echo button::create(['type'=>'button',
  1008. 'label'=>$text['button-generate'],
  1009. 'icon'=>'key',
  1010. 'style'=>'margin-top: 1px; margin-bottom: 1px;',
  1011. 'onclick'=>"document.getElementById('api_key').value = '".generate_password(32,3)."';
  1012. document.getElementById('frm').submit();"]);
  1013. }
  1014. else {
  1015. //view the api key
  1016. echo button::create(['type'=>'button',
  1017. 'label'=>$text['button-view'],
  1018. 'id'=>'button-api_key_view',
  1019. 'icon'=>'key',
  1020. 'style'=>'margin-top: 1px; margin-bottom: 1px;',
  1021. 'onclick'=>"document.getElementById ('button-api_key_view').style.display = 'none';
  1022. document.getElementById('api_key').style.display = 'inline';
  1023. document.getElementById('button-api_key_hide').style.display = 'inline';
  1024. document.getElementById('button-api_key_view').style.display = 'none';"]);
  1025. echo button::create(['type'=>'button',
  1026. 'label'=>$text['button-hide'],
  1027. 'id'=>'button-api_key_hide',
  1028. 'icon'=>'key',
  1029. 'style'=>'display: none;',
  1030. 'onclick'=>"document.getElementById('api_key').style.display = 'none';
  1031. document.getElementById('button-api_key_hide').style.display = 'none';
  1032. document.getElementById('button-api_key_view').style.display = 'inline';"]);
  1033. }
  1034. if (!empty($text['description-api_key'])) {
  1035. echo " <br />".$text['description-api_key']."<br />\n";
  1036. }
  1037. echo " </td>";
  1038. echo " </tr>";
  1039. }
  1040. //user time based one time password secret
  1041. if (!empty($_SESSION['authentication']['methods']) && in_array('totp', $_SESSION['authentication']['methods'])) {
  1042. if (!empty($user_totp_secret) && !empty($username)) {
  1043. $otpauth = "otpauth://totp/".$username."?secret=".$user_totp_secret."&issuer=".$_SESSION['domain_name'];
  1044. require_once 'resources/qr_code/QRErrorCorrectLevel.php';
  1045. require_once 'resources/qr_code/QRCode.php';
  1046. require_once 'resources/qr_code/QRCodeImage.php';
  1047. try {
  1048. $code = new QRCode (- 1, QRErrorCorrectLevel::H);
  1049. $code->addData($otpauth);
  1050. $code->make();
  1051. $img = new QRCodeImage ($code, $width=210, $height=210, $quality=50);
  1052. $img->draw();
  1053. $image = $img->getImage();
  1054. $img->finish();
  1055. }
  1056. catch (Exception $error) {
  1057. echo $error;
  1058. }
  1059. }
  1060. echo "<tr>\n";
  1061. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  1062. echo " ".$text['label-user_totp_secret']."\n";
  1063. echo "</td>\n";
  1064. echo "<td class='vtable' align='left' valign='top'>\n";
  1065. echo " <input type='hidden' class='formfld' style='width: 250px;' name='user_totp_secret' id='user_totp_secret' value=\"".escape($user_totp_secret)."\" >";
  1066. if (empty($user_totp_secret)) {
  1067. $base32 = new base2n(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', FALSE, TRUE, TRUE);
  1068. $user_totp_secret = $base32->encode(generate_password(20,3));
  1069. echo button::create(['type'=>'button',
  1070. 'label'=>$text['button-setup'],
  1071. 'icon'=>'key',
  1072. 'onclick'=>"document.getElementById('user_totp_secret').value = '".$user_totp_secret."';
  1073. document.getElementById('frm').submit();"]);
  1074. }
  1075. else {
  1076. echo " <div id='totp_qr' style='display:none;'>\n";
  1077. echo " ".$user_totp_secret."<br />\n";
  1078. echo " <img src=\"data:image/jpeg;base64,".base64_encode($image)."\" style='margin-top: 0px; padding: 5px; background: white; max-width: 100%;'><br />\n";
  1079. echo " ".$text['description-user_totp_qr_code']."<br /><br />\n";
  1080. echo " </div>\n";
  1081. echo button::create(['type'=>'button',
  1082. 'label'=>$text['button-view'],
  1083. 'id'=>'button-totp_view',
  1084. 'icon'=>'key',
  1085. 'onclick'=>"document.getElementById('totp_qr').style.display = 'inline';
  1086. document.getElementById('button-totp_hide').style.display = 'inline';
  1087. document.getElementById('button-totp_disable').style.display = 'inline';
  1088. document.getElementById('button-totp_view').style.display = 'none';"]);
  1089. echo button::create(['type'=>'button',
  1090. 'label'=>$text['button-hide'],
  1091. 'id'=>'button-totp_hide',
  1092. 'icon'=>'key',
  1093. 'style'=>'display: none;',
  1094. 'onclick'=>"document.getElementById('totp_qr').style.display = 'none';
  1095. document.getElementById('button-totp_hide').style.display = 'none';
  1096. document.getElementById('button-totp_disable').style.display = 'none';
  1097. document.getElementById('button-totp_view').style.display = 'inline';"]);
  1098. echo button::create(['type'=>'button',
  1099. 'label'=>$text['button-disable'],
  1100. 'id'=>'button-totp_disable',
  1101. 'icon'=>'trash',
  1102. 'style'=>'display: none;',
  1103. 'onclick'=>"document.getElementById('user_totp_secret').value = '';
  1104. document.getElementById('frm').submit();"]);
  1105. }
  1106. if (empty($user_totp_secret)) {
  1107. echo " <br />".$text['description-user_totp_secret']."<br />\n";
  1108. }
  1109. else {
  1110. echo " <br />".$text['description-user_totp_view']."<br />\n";
  1111. }
  1112. echo "</td>\n";
  1113. echo "</tr>\n";
  1114. }
  1115. echo "<tr ".($user_uuid == $_SESSION['user_uuid'] ? "style='display: none;'" : null).">\n";
  1116. echo "<td class='vncell' valign='top' align='left' nowrap='nowrap'>\n";
  1117. echo " ".$text['label-enabled']."\n";
  1118. echo "</td>\n";
  1119. echo "<td class='vtable' align='left'>\n";
  1120. if (substr($_SESSION['theme']['input_toggle_style']['text'], 0, 6) == 'switch') {
  1121. echo " <label class='switch'>\n";
  1122. echo " <input type='checkbox' id='user_enabled' name='user_enabled' value='true' ".($user_enabled == 'true' ? "checked='checked'" : null).">\n";
  1123. echo " <span class='slider'></span>\n";
  1124. echo " </label>\n";
  1125. }
  1126. else {
  1127. echo " <select class='formfld' id='user_enabled' name='user_enabled'>\n";
  1128. echo " <option value='true' ".($user_enabled == 'true' ? "selected='selected'" : null).">".$text['option-true']."</option>\n";
  1129. echo " <option value='false' ".($user_enabled == 'false' ? "selected='selected'" : null).">".$text['option-false']."</option>\n";
  1130. echo " </select>\n";
  1131. }
  1132. echo "<br />\n";
  1133. echo $text['description-enabled']."\n";
  1134. echo "</td>\n";
  1135. echo "</tr>\n";
  1136. echo "</table>";
  1137. echo "</div>\n";
  1138. echo "<br /><br />";
  1139. if ($action == 'edit') {
  1140. echo "<input type='hidden' name='id' value=\"".escape($user_uuid)."\">";
  1141. if (permission_exists("user_edit")) {
  1142. echo "<input type='hidden' name='username_old' value=\"".escape($username)."\">";
  1143. }
  1144. }
  1145. echo "<input type='hidden' name='".$token['name']."' value='".$token['hash']."'>\n";
  1146. echo "</form>";
  1147. //hide password fields before submit
  1148. echo "<script>\n";
  1149. echo " function submit_form() {\n";
  1150. echo " hide_password_fields();\n";
  1151. echo " $('form#frm').submit();\n";
  1152. echo " }\n";
  1153. echo "</script>\n";
  1154. //include the footer
  1155. require_once "resources/footer.php";
  1156. ?>