Advertisement
Guest User

Untitled

a guest
Apr 15th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. <?php
  2. /****************************************************************************************************
  3. Modified by Nicholas Mingo
  4. *****************************************************************************************************
  5. R. V. Sampangi. 2017. Solution for Server Side Scripting Assignment 5. In INFX2670: Introduction to
  6. Server Side Scripting, Faculty of Computer Science, Dalhousie University, NS, Canada.
  7. *****************************************************************************************************/
  8.  
  9. if(isset($_POST['create_user'])) {
  10. /*
  11. * Retrieve all the form values using the $_POST superglobal.
  12. */
  13. if($_POST['password']){
  14. die("passwords too short!");
  15. }
  16. if($_POST['password']){
  17. die("The password requires one uppercase letter a lower case letter and one number!");
  18. }
  19. if($_POST['password'] != $_POST['passwordagain']){
  20. die("passwords do not match");
  21. }
  22. $user_firstname = test_form_input($_POST['user_firstname']);
  23. $user_lastname = test_form_input($_POST['user_lastname']);
  24. $user_role = test_form_input($_POST['user_role']);
  25. $user_email = test_form_input($_POST['user_email']);
  26. $username = test_form_input($_POST['username']);
  27. $password = test_form_input($_POST['password']);
  28.  
  29. $user_image = $_FILES['user_image']['name'];
  30. $user_image_temp = $_FILES['user_image']['tmp_name'];
  31. $user_image_filesize = $_FILES['user_image']['size'];
  32.  
  33. if($user_image != "") {
  34. /*
  35. * This section of the code manages image uploads. As discussed in class,
  36. * we check if the file is of a specified type, and within the allowed file-size.
  37. */
  38. $target_file = "../images/" . $user_image;
  39. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  40. $mime = finfo_file($finfo, $user_image_temp);
  41.  
  42. /*
  43. * A list of MIME types are available here:
  44. * http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
  45. */
  46.  
  47. switch ($mime) {
  48. case 'image/jpeg':
  49. case 'image/png':
  50. if ($user_image_filesize < TWO_MEGA_BYTES) {
  51. //Upload the image.
  52. move_uploaded_file($user_image_temp, "$target_file");
  53. }
  54. break;
  55.  
  56. default:
  57. die("<br>Unknown file type. Your image cannot be uploaded.<br>");
  58. }
  59. }
  60. else {
  61. //Otherwise, the user has not set any user image.
  62. $user_image = "";
  63. }
  64.  
  65. $sql = "INSERT INTO users(user_firstname, user_lastname, user_email, user_role, user_image) ";
  66. $sql .= "VALUES('$user_firstname','$user_lastname','$user_email',$user_role,'$user_image')";
  67.  
  68. $submit_user_result = $conn->query($sql);
  69.  
  70. if (!$submit_user_result) {
  71. die ("Error creating user.<br>" . $conn->error . "<br>");
  72. }
  73.  
  74.  
  75. $sql = "SELECT user_id FROM users WHERE user_firstname = '{$user_firstname}' AND user_email = '{$user_email}'";
  76. $check_user = $conn->query($sql);
  77.  
  78. if (!$check_user) {
  79. die ("<p>Sorry. Your request could not be completed. You can see the detailed error report below:</p>" . $conn->error);
  80. }
  81.  
  82. while ($row = $check_user->fetch_assoc()) {
  83. $user_id = $row['user_id'];
  84. }
  85.  
  86.  
  87. $random_salt = rand();
  88.  
  89. $sql = "INSERT INTO login(user_id, username, password, random_salt) ";
  90. $sql .= "VALUES('$user_id','$username','$password',$random_salt)";
  91.  
  92. $submit_userlogin_result = $conn->query($sql);
  93.  
  94. if (!$submit_userlogin_result) {
  95. die ("Error creating user.<br>" . $conn->error . "<br>");
  96. }
  97.  
  98. header("Location: view_users.php");
  99.  
  100. }
  101.  
  102.  
  103. //CODE TO UPDATE THE POST AFTER USER SUBMITS THE FORM.
  104. if(isset($_GET['u_id'])) {
  105. $update_this_user_id = $_GET['u_id'];
  106. }
  107. if(isset($_POST['update_user'])) {
  108. /*
  109. * Retrieve all the form values using the $_POST superglobal.
  110. */
  111. $user_firstname = test_form_input($_POST['user_firstname']);
  112. $user_lastname = test_form_input($_POST['user_lastname']);
  113. $user_email = test_form_input($_POST['user_email']);
  114.  
  115. $user_image = $_FILES['user_image']['name'];
  116. $user_image_temp = $_FILES['user_image']['tmp_name'];
  117. $user_image_filesize = $_FILES['user_image']['size'];
  118.  
  119.  
  120. $sql = "SELECT user_image FROM users WHERE user_id = {$update_this_user_id}";
  121. $check_if_image_exists = $conn->query($sql);
  122.  
  123. if (!$check_if_image_exists) {
  124. die ("<p>Sorry. Your request could not be completed. You can see the detailed error report below:</p>" . $conn->error);
  125. }
  126.  
  127. while ($row = $check_if_image_exists->fetch_assoc()) {
  128. $image_name_check = $row['user_image'];
  129. }
  130.  
  131. if (($image_name_check == "" && $user_image != "") || ($image_name_check != "" && $user_image != "" && $image_name_check != $user_image)) {
  132. /*
  133. * This section of the code manages image uploads. As discussed in class,
  134. * we check if the file is of a specified type, and within the allowed file-size.
  135. */
  136. $target_file = "../images/" . $user_image;
  137. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  138. $mime = finfo_file($finfo, $user_image_temp);
  139.  
  140. /*
  141. * A list of MIME types are available here:
  142. * http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
  143. */
  144.  
  145. switch ($mime) {
  146. case 'image/jpeg':
  147. case 'image/png':
  148. if ($user_image_filesize < TWO_MEGA_BYTES) {
  149. //Upload the image.
  150. move_uploaded_file($user_image_temp, "$target_file");
  151. }
  152. break;
  153.  
  154. default:
  155. die("<br>Unknown file type. Your image cannot be uploaded.<br>");
  156. }
  157. }
  158. else {
  159. $user_image = $image_name_check;
  160. }
  161.  
  162.  
  163. $sql = "UPDATE users SET ";
  164. $sql .= "user_firstname = '{$user_firstname}', ";
  165. $sql .= "user_lastname = '{$user_lastname}', ";
  166. $sql .= "user_email = '{$user_email}', ";
  167. $sql .= "user_image = '{$user_image}' ";
  168. $sql .= "WHERE user_id = '{$update_this_user_id}'";
  169.  
  170. $update_post_result = $conn->query($sql);
  171.  
  172. if (!$update_post_result) {
  173. die ("Error updating user.<br>" . $conn->error . "<br>");
  174. }
  175.  
  176. header("Location: view_users.php");
  177.  
  178. }
  179.  
  180. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement