PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/admin/user.php

https://bitbucket.org/d3bugg3r/shiftsystem
PHP | 135 lines | 130 code | 5 blank | 0 comment | 40 complexity | 771b46550b8c1d2d4736cc5c60c0cc74 MD5 | raw file
  1. <h3>User Management</h3>
  2. <?if ($_GET['u'] == 1) { //the new user form has been submitted
  3. $user = escape($_POST['user']);
  4. $email = escape($_POST['email']);
  5. $admin = escape($_POST['admin']);
  6. $colour = escape($_POST['colour']);
  7. $id = escape($_POST['id']);
  8. if (escape($_POST['submit']) == "Edit User"){ //Checks to see if this is an edit or a create request
  9. $edit = true;
  10. $prev = $_POST['prev_user'];
  11. if ($user != $prev) { //username has been changed
  12. $user_val = mysql_query("SELECT * FROM users WHERE username='$user'");
  13. if (mysql_num_rows($user_val) != 0) { //username is not already in use
  14. echo "Username is already in use. Please hit back and select another";
  15. die();
  16. }
  17. }
  18. $password = $_POST['password'];
  19. if ($password != '') $pass = md5($password);
  20. if ($admin == "yes") $admin = "1"; else $admin = "0";
  21. if (mysql_query("UPDATE users SET username='$user', password='$pass', email='$email', admin='$admin', colour='$colour' WHERE ID='$id'")) {
  22. echo "User edited successfully";
  23. die();
  24. }
  25. else {
  26. echo "There was an error, please try again: ";
  27. echo mysql_error();
  28. die();
  29. }
  30. }
  31. elseif (escape($_POST['submit']) == "Delete User") { //this is a delete request
  32. echo "here";
  33. if (!isset($_POST['del_conf'])) { //deletion has not yet been confirmed
  34. echo "<p>Are you sure you wish to permanently delete this user? This cannot be undone</p>";
  35. echo "<form method='post' action='?p=admin&a=user&u=1'>";
  36. $id = $_POST['id'];
  37. echo "<input type='hidden' name='id' value='$id' />";
  38. echo "<input type='hidden' name='del_conf' value='true' />";
  39. echo "<input type='submit' name='submit' value='Delete User' />";
  40. echo "<input type='submit' value='Cancel' />";
  41. echo "</form>";
  42. die();
  43. }
  44. else { //deletion has been confirmed
  45. $id = $_POST['id'];
  46. if (mysql_query("DELETE FROM users WHERE ID='$id'")) {
  47. echo "<p>User deleted Successfully</p>";
  48. }
  49. else echo "There was an error! ".mysql_error();
  50. }
  51. }
  52. elseif (escape($_POST['submit']) == "Cancel") {
  53. }
  54. else { //This is a create request
  55. $user_val = mysql_query("SELECT * FROM users WHERE username='$user'");
  56. if (mysql_num_rows($user_val) == 0) { //username is not already in use
  57. if ($admin=="yes") $admin = 1; else $admin=0;
  58. $password = generatePassword();
  59. $pass = md5($password);
  60. if (mysql_query("INSERT INTO users VALUES('', '$user', '$pass', '$admin', '$email', '$colour')")) {
  61. mail($email, 'User Account Created', "DO NOT REPLY TO THIS MESSAGE!
  62. A User account has been created for you in the Shift Management System with the following details.
  63. Username: $user
  64. Password: $password
  65. Please change your password when you first log in", 'From: Shift Management System');
  66. echo "User account created. The user has been emailed their details";
  67. die();
  68. }
  69. else {
  70. echo "There was an error creating the account. Please try again";
  71. die();
  72. }
  73. }
  74. else {
  75. echo "Username is already in use. Please hit back and select another";
  76. die();
  77. }
  78. }
  79. }
  80. $users = mysql_query("SELECT * FROM users WHERE 1=1");
  81. echo "<form method='post' action='?p=admin&a=user&u=2'>";
  82. echo "Select a User to edit: <select name='user'>";
  83. while ($user_row = mysql_fetch_row($users)) {
  84. $u = $user_row[1];
  85. $u_id = $user_row[0];
  86. echo "<option value='$u_id'>$u</option>";
  87. }
  88. echo "</select><input type='submit' value='Edit' /></form><br />";
  89. if ($_GET['u'] == 2) { //The edit user form has been submitted
  90. $id = escape($_POST['user']);
  91. $result = mysql_query("SELECT * FROM users WHERE id='$id'");
  92. $details = mysql_fetch_array($result);
  93. $edit = true;
  94. echo "<h4>Edit User Details</h4>";
  95. }
  96. else echo "<h4>Create New User</h4>";
  97. echo "<form method='post' action='?p=admin&a=user&u=1'>
  98. <input type='hidden' name='id' value='$id' />
  99. <table class='invisible'>
  100. <tr>
  101. <td>Username:</td><td> <input name='user' value='$details[1]' type='text' /></td>
  102. </tr>
  103. <tr>
  104. <td>Email Address:</td><td> <input name='email' value='$details[4]' type='text' /></td>
  105. </tr>";
  106. if ($edit) {
  107. echo "<tr>
  108. <td>Password:</td><td> <input type='password' name='password' /> (Leave blank to remain the same)<input type='hidden' name='prev_user' value='$details[1]' /></td>
  109. </tr>";
  110. }
  111. else {
  112. echo "<tr>
  113. <td>Password:</td><td> Will be emailed to user</td>
  114. </tr>";
  115. }
  116. echo "<tr>
  117. <td>Colour Code:</td><td><input type='text' name='colour' value='$details[5]' /> <a class='tooltip'>Scroll over for More Info<span>Colour Code: Enter the colour that will represent the user.<br />Either Red, Black etc or a Hex Code (e.g. #000000)</span></a>" ;
  118. echo "<tr>
  119. <td>Set as Admin:</td><td> <input type='checkbox' name='admin' value='yes'"; if (($edit) && ($details[3]==1)) echo "checked='yes'"; echo " /></td>
  120. </tr>
  121. </table>
  122. Please doublecheck the details before submitting<br />";
  123. if ($edit) {
  124. echo "<input type='submit' name='submit' value='Edit User' />";
  125. echo "<input type='submit' name='submit' value='Delete User' />";
  126. }
  127. else {
  128. echo "<input type='submit' name='submit' value='Create User' />";
  129. }
  130. echo "</form>";