edituser.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. // If mailbox_limit is supported in the MySQL database
  3. $mailbox_limit_default = 0;
  4. if(defined('DBC_USERS_MAILBOXLIMIT')){
  5. // Get mailbox_limit default value from DB
  6. $sql = "SELECT DEFAULT(".DBC_USERS_MAILBOXLIMIT.") AS `".DBC_USERS_MAILBOXLIMIT."` FROM `".DBT_USERS."` LIMIT 1;";
  7. if(!$result = $db->query($sql)){
  8. die('There was an error running the query [' . $db->error . ']');
  9. }
  10. else{
  11. while($row = $result->fetch_assoc()){
  12. $mailbox_limit_default = $row[DBC_USERS_MAILBOXLIMIT];
  13. }
  14. }
  15. }
  16. if(isset($_POST['savemode'])){
  17. $savemode = $_POST['savemode'];
  18. if($savemode === "edit"){
  19. // Edit mode entered
  20. $id = $db->escape_string($_POST['id']);
  21. if(defined('DBC_USERS_MAILBOXLIMIT')){
  22. if($mailbox_limit == ""){
  23. $mailbox_limit = $mailbox_limit_default;
  24. }
  25. $mailbox_limit = $db->escape_string($_POST['mailbox_limit']);
  26. $sql = "UPDATE `".DBT_USERS."` SET `".DBC_USERS_MAILBOXLIMIT."` = '$mailbox_limit' WHERE `".DBC_USERS_ID."` = '$id';";
  27. if(!$result = $db->query($sql)){
  28. die('There was an error running the query [' . $db->error . ']');
  29. }
  30. }
  31. // Is there a changed password?
  32. if($_POST['password'] !== ""){
  33. $pass_ok = check_new_pass($_POST['password'], $_POST['password_rep']);
  34. if($pass_ok === true){
  35. // Password is okay and can be set
  36. $pass_hash = gen_pass_hash($_POST['password']);
  37. write_pass_hash_to_db($pass_hash, $id);
  38. // $editsuccessful = true;
  39. add_message("success", "User edited successfully.");
  40. }
  41. else{
  42. // Password is not okay
  43. // $editsuccessful = 2;
  44. add_message("fail", $PASS_ERR_MSG);
  45. }
  46. }
  47. else{
  48. // Redirect user to user list
  49. header("Location: ".FRONTEND_BASE_PATH."admin/listusers/?edited=1");
  50. }
  51. }
  52. else if($savemode === "create"){
  53. // Create mode entered
  54. $username = $db->escape_string($_POST['username']);
  55. $username = strtolower($username);
  56. $domain = $db->escape_string($_POST['domain']);
  57. $domain = strtolower($domain);
  58. if(defined('DBC_USERS_MAILBOXLIMIT')){
  59. $mailbox_limit = $db->escape_string($_POST['mailbox_limit']);
  60. }
  61. else{
  62. // make mailbox_limit dummy for "if"
  63. $mailbox_limit = 0;
  64. }
  65. $pass = $_POST['password'];
  66. $pass_rep = $_POST['password_rep'];
  67. if($username !== "" && $domain !== "" && $mailbox_limit !== ""){
  68. // Check if user already exists
  69. $user_exists = $db->query("SELECT `".DBC_USERS_USERNAME."`, `".DBC_USERS_DOMAIN."` FROM `".DBT_USERS."` WHERE `".DBC_USERS_USERNAME."` = '$username' AND `".DBC_USERS_DOMAIN."` = '$domain';");
  70. if($user_exists->num_rows == 0){
  71. // All fields filled with content
  72. // Check passwords
  73. $pass_ok = check_new_pass($pass, $pass_rep);
  74. if($pass_ok === true){
  75. // Password is okay ... continue
  76. $pass_hash = gen_pass_hash($pass);
  77. // Differ between version with mailbox_limit and version without
  78. if(defined('DBC_USERS_MAILBOXLIMIT')){
  79. $sql = "INSERT INTO `".DBT_USERS."` (`".DBC_USERS_USERNAME."`, `".DBC_USERS_DOMAIN."`, `".DBC_USERS_PASSWORD."`, `".DBC_USERS_MAILBOXLIMIT."`) VALUES ('$username', '$domain', '$pass_hash', '$mailbox_limit')";
  80. }
  81. else{
  82. $sql = "INSERT INTO `".DBT_USERS."` (`".DBC_USERS_USERNAME."`, `".DBC_USERS_DOMAIN."`, `".DBC_USERS_PASSWORD."`) VALUES ('$username', '$domain', '$pass_hash')";
  83. }
  84. if(!$result = $db->query($sql)){
  85. die('There was an error running the query [' . $db->error . ']');
  86. }
  87. // Redirect user to user list
  88. header("Location: ".FRONTEND_BASE_PATH."admin/listusers/?created=1");
  89. }
  90. else{
  91. // Password not okay
  92. add_message("fail", $PASS_ERR_MSG);
  93. }
  94. }
  95. else{
  96. add_message("fail", "User already exists in database.");
  97. }
  98. }
  99. else{
  100. // Fields missing
  101. add_message("fail", "Not all fields were filled out.");
  102. }
  103. }
  104. }
  105. // Select mode
  106. $mode = "create";
  107. if(isset($_GET['id'])){
  108. $mode = "edit";
  109. $id = $db->escape_string($_GET['id']);
  110. }
  111. if($mode === "edit"){
  112. //Load user data from DB
  113. $sql = "SELECT * from `".DBT_USERS."` WHERE `".DBC_USERS_ID."` = '$id' LIMIT 1;";
  114. if(!$result = $db->query($sql)){
  115. die('There was an error running the query [' . $db->error . ']');
  116. }
  117. while($row = $result->fetch_assoc()){
  118. $username = $row[DBC_USERS_USERNAME];
  119. $domain = $row[DBC_USERS_DOMAIN];
  120. if(defined('DBC_USERS_MAILBOXLIMIT')){
  121. $mailbox_limit = $row[DBC_USERS_MAILBOXLIMIT];
  122. }
  123. }
  124. }
  125. ?>
  126. <h1><?php if($mode === "create") { ?> Create <?php } else {?>Edit <?php } ?>User</h1>
  127. <?php output_messages(); ?>
  128. <p>
  129. <a class="button button-small" href="<?php echo FRONTEND_BASE_PATH; ?>admin/listusers/">&#10092; Back to user list</a>
  130. </p>
  131. <p>
  132. <?php
  133. if($mode === "edit"){
  134. echo "Username and domain cannot be edited.";
  135. }
  136. ?>
  137. </p>
  138. <form action="" method="post">
  139. <table>
  140. <tr> <th>Username</th> <th>Domain</th> <th>Password</th> <?php if(defined('DBC_USERS_MAILBOXLIMIT')){ ?><th>Mailbox limit (in MB)</th> <?php } ?> </tr>
  141. <tr>
  142. <td>
  143. <input name="username" class="textinput" type="text" autofocus <?php echo ($mode === "edit") ? ' disabled' : '';?> value="<?php if(isset($username)){echo strtolower(strip_tags($username));} ?>" placeholder="Username" required="required"/>
  144. </td>
  145. <td>
  146. @
  147. <select name="domain" <?php echo ($mode === "edit") ? ' disabled' : '';?> >
  148. <?php
  149. //Load user data from DB
  150. $sql = "SELECT `".DBC_DOMAINS_DOMAIN."` FROM `".DBT_DOMAINS."`;";
  151. if(!$result = $db->query($sql)){
  152. die('There was an error running the query [' . $db->error . ']');
  153. }
  154. while($row = $result->fetch_assoc()){
  155. $selected = "";
  156. if(isset($domain) && $row[DBC_DOMAINS_DOMAIN] === $domain){$selected = "selected=\"selected\"";}
  157. echo "<option value=\"".strip_tags($row[DBC_DOMAINS_DOMAIN])."\" ".$selected." >".strip_tags($row[DBC_DOMAINS_DOMAIN])."</option>";
  158. }
  159. ?>
  160. </select>
  161. </td>
  162. <td>
  163. <input name="password" class="textinput" type="password" placeholder="New password"/></br>
  164. <input name="password_rep" class="textinput" type="password" placeholder="New password (repeat)"/>
  165. <p>
  166. <input type="button" class="button button-small" name="Text 1" value="Generate password"
  167. onclick="pass=generatePassword();this.form.password.value=pass;this.form.password_rep.value=pass;this.form.password.type='text';this.form.password_rep.type='text'">
  168. </p>
  169. </td>
  170. <?php if(defined('DBC_USERS_MAILBOXLIMIT')){ ?>
  171. <td>
  172. <input name="mailbox_limit" class="textinput" type="number" value="<?php if(isset($mailbox_limit)){echo strip_tags($mailbox_limit);} else{echo strip_tags($mailbox_limit_default);} ?>" placeholder="Mailbox size (MB)" required="required"/>
  173. </td>
  174. <?php } ?>
  175. </tr>
  176. </table>
  177. <input name="savemode" type="hidden" value="<?php if(isset($mode)){echo $mode;} ?>"/>
  178. <input name="id" class="sendbutton" type="hidden" value="<?php if(isset($id)){echo $id;} ?>"/>
  179. <p>
  180. <input type="submit" class="button button-small" value="Save settings">
  181. </p>
  182. </form>