PageRenderTime 25ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/processEditUser.php

http://find-it.googlecode.com/
PHP | 132 lines | 117 code | 9 blank | 6 comment | 9 complexity | 576964c7b939a8ce9a2034da54f8071f MD5 | raw file
Possible License(s): CC-BY-3.0
  1. <?php
  2. $errors = array();
  3. if ($_POST) {
  4. // run the validation script
  5. require_once('library.php');
  6. try {
  7. // Validate Last name not empty
  8. $val = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::STRING);
  9. if(!$val->isValid($_POST['lastName'])){
  10. $errors['lastName']='*** Required field -> ';
  11. }
  12. // Validate First name not empty
  13. if(!$val->isValid($_POST['firstName'])){
  14. $errors['firstName']='*** Required field -> ';
  15. }
  16. // Validate email
  17. $val = new Zend_Validate_EmailAddress();
  18. if (!$val->isValid($_POST['email'])){
  19. $errors['email'] = '*** Use a valid email address -> ';
  20. }
  21. // Validate confirmation email identical
  22. $val = new Zend_Validate_Identical($_POST['email']);
  23. if (!$val->isValid($_POST['confirmEmail'])){
  24. $errors['confirmEmail'] = "*** Emails don't mach -> ";
  25. }
  26. /*
  27. // Check that accountName is 6-50 alphanumeric and check if it exists in DB.
  28. // Also get the accountId
  29. $val = new Zend_Validate();
  30. $lenght = new Zend_Validate_StringLength(6,50);
  31. $val->addValidator($lenght);
  32. $val->addValidator(new Zend_Validate_Alnum());
  33. if (!$val->isValid($_POST['accountName'])){
  34. $errors['accountName'] = '*** Use 6-50 letters or numbers only -> ';
  35. }else{
  36. $sql = $dbRead->quoteInto('SELECT accountName,id from accounts WHERE accountName = ?',$_POST['accountName']);
  37. $result = $dbRead->fetchAll($sql);
  38. if (!$result){
  39. $errors['accountName'] = '*** '.$_POST['accountName'] . ' does not exist -> ';
  40. } else {
  41. $accountId = $result[0]['id'];
  42. }
  43. }
  44. */
  45. // check that username is 6-50 alphanumeric and not already taken in DB
  46. $val = new Zend_Validate();
  47. $lenght = new Zend_Validate_StringLength(6,50);
  48. $val->addValidator($lenght);
  49. $val->addValidator(new Zend_Validate_Alnum());
  50. $userNameChanged = FALSE;
  51. if (!$val->isValid($_POST['userName'])){
  52. $errors['userName'] = '*** Use 6-50 letters or numbers only -> ';
  53. }else{
  54. $sql = 'SELECT userName FROM `users` WHERE `userId`= ? ORDER BY userName';
  55. $result = $dbRead->fetchAll($sql,$_GET['userId']);
  56. if($result[0]['userName'] === $_POST['userName']){
  57. $userNameChanged = FALSE;
  58. } else{
  59. $userNameChanged = TRUE;
  60. $sql = $dbRead->quoteInto('SELECT userName from users WHERE userName = ?',$_POST['userName']);
  61. $result = $dbRead->fetchAll($sql);
  62. if ($result){
  63. $errors['userName'] = '*** '.$_POST['userName'] . ' is already in use -> ';
  64. }
  65. }
  66. }
  67. echo $userNameChanged;
  68. $hasPassword = 0;
  69. //Check if anything was entered in password fields
  70. if(!empty($_POST['password']) || !empty($_POST['confirmPassword'])){
  71. $hasPassword = 1;
  72. }
  73. if($hasPassword === 1){
  74. echo ('password = '.$_POST['password']);
  75. // Validate password.
  76. $lenght->setMin(8);
  77. $val = new Zend_Validate();
  78. $val->addValidator($lenght);
  79. //$val->addValidator(new Zend_Validate_Regex('(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$'));
  80. //$val->addValidator(new Zend_Validate_Regex('^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*$'));
  81. $val->addValidator(new Zend_Validate_Alnum());
  82. if (!$val->isValid($_POST['password'])){
  83. $errors['password'] = '*** Use at least 8 alpha-numeric characters -> ';
  84. }
  85. // Validate that confirmation password is identical
  86. $val = new Zend_Validate_Identical($_POST['password']);
  87. if (!$val->isValid($_POST['confirmPassword'])) {
  88. $errors['confirmPassword'] = "*** Passwords don't mach -> ";
  89. }
  90. }
  91. //If there are no errors
  92. if(!$errors){
  93. if(!$userNameChanged){
  94. $data = array('lastName' => $_POST['lastName'],
  95. 'firstName' => $_POST['firstName'],
  96. 'email' => $_POST['email'],
  97. 'password' => sha1($_POST['password']),
  98. 'accessLevel' => $_POST
  99. ['accessLevel'],
  100. 'accountId' => $_SESSION
  101. ['accountId']);
  102. $dbWrite->update('users', $data, 'userId = '.$_GET['userId']);
  103. header('Location: UserList.php');
  104. } else if($userNameChanged) {
  105. $data = array('lastName' => $_POST['lastName'],
  106. 'firstName' => $_POST['firstName'],
  107. 'email' => $_POST['email'],
  108. 'userName' => $_POST['userName'],
  109. 'password' => sha1($_POST['password']),
  110. 'accessLevel' => $_POST
  111. ['accessLevel'],
  112. 'accountId' => $_SESSION
  113. ['accountId']);
  114. $dbWrite->update('users', $data, 'userId = '.$_GET['userId']);
  115. header('Location: UserList.php');
  116. }
  117. }
  118. } catch (Exception $e){
  119. echo $e->getMessage();
  120. }
  121. }