PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/snippets/weblogin/webchangepwd.inc.php

https://github.com/modxcms/evolution
PHP | 180 lines | 159 code | 10 blank | 11 comment | 20 complexity | 75f3beb571d875fcbc2481ac65d7018b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MIT, BSD-2-Clause, Apache-2.0, BSD-3-Clause
  1. <?php
  2. # WebChangePwd 1.0
  3. # Created By Raymond Irving April, 2005
  4. #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  5. defined('IN_PARSER_MODE') or die();
  6. # load tpl
  7. if(is_numeric($tpl)) $tpl = ($doc=$modx->getDocuments($tpl)) ? $doc['content']:"Document '$tpl' not found.";
  8. else if($tpl) $tpl = ($chunk=$modx->getChunk($tpl)) ? $chunk:"Chunk '$tpl' not found.";
  9. if(!$tpl) $tpl = getWebChangePwdtpl();
  10. // extract declarations
  11. $declare = webLoginExtractDeclarations($tpl);
  12. $tpls = explode((isset($declare["separator"]) ? $declare["separator"]:"<!--tpl_separator-->"),$tpl);
  13. if(!$isPostBack && isset($_SESSION['webValidated'])){
  14. // display password screen
  15. $tpl = $tpls[0];
  16. $tpl = str_replace("[+action+]",$modx->makeUrl($modx->documentIdentifier),$tpl);
  17. $tpl.="<script type='text/javascript'>
  18. if (document.changepwdfrm) document.changepwdfrm.oldpassword.focus();
  19. </script>";
  20. $output .= $tpl;
  21. }
  22. else if ($isPostBack && isset($_SESSION['webValidated'])){
  23. $oldpassword = $_POST['oldpassword'];
  24. $genpassword = $_POST['newpassword'];
  25. $passwordgenmethod = $_POST['passwordgenmethod'];
  26. $passwordnotifymethod = $_POST['passwordnotifymethod'];
  27. $specifiedpassword = $_POST['specifiedpassword'];
  28. $uid = $modx->getLoginUserID();
  29. $type = $modx->getLoginUserType();
  30. // load template
  31. $tpl = $tpls[0];
  32. $tpl = str_replace("[+action+]",$modx->makeUrl($modx->documentIdentifier),$tpl);
  33. $tpl.="<script type='text/javascript'>if (document.changepwdfrm) document.changepwdfrm.oldpassword.focus();</script>";
  34. // get user record
  35. if($type=='manager') $ds = $modx->getUserInfo($uid);
  36. else $ds = $modx->getWebUserInfo($uid);
  37. // verify password
  38. if($ds['password']==md5($oldpassword)) {
  39. // verify password
  40. if ($passwordgenmethod=="spec" && $_POST['specifiedpassword']!=$_POST['confirmpassword']) {
  41. $output = webLoginAlert("Password typed is mismatched",1).$tpl;
  42. return;
  43. }
  44. // generate a new password for this user
  45. if($specifiedpassword!="" && $passwordgenmethod=="spec") {
  46. if(strlen($specifiedpassword) < 6 ) {
  47. $output = webLoginAlert("Password is too short!").$tpl;
  48. return;
  49. } else {
  50. $newpassword = $specifiedpassword;
  51. }
  52. } elseif($specifiedpassword=="" && $passwordgenmethod=="spec") {
  53. $output = webLoginAlert("You didn't specify a password for this user!").$tpl;
  54. return;
  55. } elseif($passwordgenmethod=='g') {
  56. $newpassword = webLoginGeneratePassword(8);
  57. } else {
  58. $output = webLoginAlert("No password generation method specified!").$tpl;
  59. return;
  60. }
  61. // handle notification
  62. if($passwordnotifymethod=='e') {
  63. $rt = webLoginSendNewPassword($ds["email"],$ds["username"],$newpassword,$ds["fullname"]);
  64. if($rt!==true) { // an error occured
  65. $output = $rt.$tpl;
  66. return;
  67. }
  68. else {
  69. $newpassmsg = "A copy of the new password was sent to your email address.";
  70. }
  71. }
  72. else {
  73. $newpassmsg = "The new password is <b>" . htmlspecialchars($newpassword, ENT_QUOTES) . "</b>.";
  74. }
  75. // save new password to database
  76. $rt = $modx->changeWebUserPassword($oldpassword,md5($newpassword));
  77. if($rt!==true) {
  78. $output = webLoginAlert("An error occured while saving new password: $rt");
  79. return;
  80. }
  81. // display change notification
  82. $tpl = $tpls[1];
  83. $tpl = str_replace("[+newpassmsg+]",$newpassmsg,$tpl);
  84. $output .= $tpl;
  85. }
  86. else {
  87. $output = webLoginAlert("Incorrect password. Please try again.").$tpl;
  88. return;
  89. }
  90. }
  91. // Returns Default WebChangePwd tpl
  92. function getWebChangePwdtpl(){
  93. ob_start();
  94. ?>
  95. <!-- #declare:separator <hr> -->
  96. <!-- login form section-->
  97. <form method="post" name="changepwdfrm" action="[+action+]" style="margin: 0px; padding: 0px;">
  98. <table border="0" cellpadding="1" width="300">
  99. <tr>
  100. <td><fieldset style="width:300px">
  101. <legend><b>Enter your current password</b></legend>
  102. <table border="0" cellpadding="0" style="margin-left:20px;">
  103. <tr>
  104. <td style="padding:0px 0px 0px 0px;">
  105. <label for="oldpassword" style="width:120px">Current password:</label>
  106. </td>
  107. <td style="padding:0px 0px 0px 0px;">
  108. <input type="password" name="oldpassword" size="20" /><br />
  109. </td>
  110. </tr>
  111. </table>
  112. </fieldset> <fieldset style="width:300px">
  113. <legend><b>New password method</b></legend>
  114. <input type="radio" name="passwordgenmethod" value="g" checked />Let this website
  115. generate a password.<br />
  116. <input type="radio" name="passwordgenmethod" value="spec" />Let me specify
  117. the password:<br />
  118. <div style="padding-left:20px">
  119. <table border="0" cellpadding="0">
  120. <tr>
  121. <td style="padding:0px 0px 0px 0px;">
  122. <label for="specifiedpassword" style="width:120px">New password:</label>
  123. </td>
  124. <td style="padding:0px 0px 0px 0px;">
  125. <input type="password" name="specifiedpassword" onchange="documentdirty=true;" onkeypress="document.changepwdfrm.passwordgenmethod[1].checked=true;" size="20" /><br />
  126. </td>
  127. </tr>
  128. <tr>
  129. <td style="padding:0px 0px 0px 0px;">
  130. <label for="confirmpassword" style="width:120px">Confirm password:</label>
  131. </td>
  132. <td style="padding:0px 0px 0px 0px;">
  133. <input type="password" name="confirmpassword" onchange="documentdirty=true;" onkeypress="document.changepwdfrm.passwordgenmethod[1].checked=true;" size="20" /><br />
  134. </td>
  135. </tr>
  136. </table>
  137. <small><span class="warning" style="font-weight:normal">The password you
  138. specify needs to be at least 6 characters long.</span></small>
  139. </div>
  140. </fieldset><br />
  141. <fieldset style="width:300px">
  142. <legend><b>Password notification method</b></legend>
  143. <input type="radio" name="passwordnotifymethod" value="e" />Send the new password
  144. by e-mail.<br />
  145. <input type="radio" name="passwordnotifymethod" value="s" checked />Show the new password
  146. on screen.
  147. </fieldset></td>
  148. </tr>
  149. <tr>
  150. <td align="right"><input type="submit" value="Submit" name="cmdwebchngpwd" />
  151. <input type="reset" value="Reset" name="cmdreset" />
  152. </td>
  153. </tr>
  154. </table>
  155. </form>
  156. <hr>
  157. <!-- notification section -->
  158. Your password was successfully changed.<br /><br />
  159. [+newpassmsg+]
  160. <?php
  161. $t = ob_get_contents();
  162. ob_end_clean();
  163. return $t;
  164. }
  165. ?>