lostpwd.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. function makeRandomPassword() {
  25. $salt = "abchefghjkmnpqrstuvwxyz0123456789";
  26. srand((double)microtime()*1000000);
  27. $i = 0;
  28. $pass = "";
  29. while ($i <= 7) {
  30. $num = rand() % 33;
  31. $tmp = substr($salt, $num, 1);
  32. $pass = $pass . $tmp;
  33. $i++;
  34. }
  35. return $pass;
  36. }
  37. function exec_ogp_module() {
  38. global $db,$view,$settings;
  39. $view->setCharset(get_lang('lang_charset'));
  40. $errorCount = 0;
  41. if(isset($errors)){
  42. unset($errors);
  43. }
  44. $moduleLink = "index.php?m=lostpwd";
  45. $lang_switch = (isset($_GET['lang']) and $_GET['lang'] != "") ? '&lang='.$_GET['lang'] : "";
  46. echo '<h2>'. get_lang("recover") . '</h2>';
  47. // We either need to show the form or process the email address input
  48. if(!isset( $_GET['user_id'] ) AND !isset( $_GET['ch_pass_uid'] )){
  49. if(isset($_POST['email_address'])){
  50. /* Start of Process User Input */
  51. $email_address = trim($_POST['email_address']);
  52. if ( empty($email_address) )
  53. {
  54. $errorCount++;
  55. $errors[] = get_lang('incomplete');
  56. }
  57. if (!stristr($email_address,"@") OR !stristr($email_address,"."))
  58. {
  59. $errorCount++;
  60. $errors[] = get_lang('errormail');
  61. }
  62. if($errorCount == 0){
  63. // Check to see if email address is in the database
  64. $user_info = $db->getUserByEmail($email_address);
  65. if ( empty($user_info) )
  66. {
  67. $errorCount++;
  68. $errors[] = get_lang('errormail');
  69. }
  70. // Still no errors?
  71. if($errorCount == 0){
  72. $user_id = $user_info['user_id'];
  73. $ch_pass_uid = $user_info['users_passwd'];
  74. $subject = get_lang('confirm_change_subject');
  75. $s = ( isset($_SERVER['HTTPS']) and get_true_boolean($_SERVER['HTTPS']) ) ? "s" : "";
  76. $serverName = $_SERVER["SERVER_NAME"];
  77. if(empty($serverName) || $serverName == "_"){
  78. $serverName = $_SERVER['HTTP_HOST'];
  79. }
  80. $recover_link = '<a href="http'.$s.'://'.$serverName.$_SERVER['REQUEST_URI']."&user_id=".$user_id.'&ch_pass_uid='.$ch_pass_uid.
  81. '" >http'.$s.'://'.$serverName.$_SERVER['REQUEST_URI']."&user_id=".$user_id.'&ch_pass_uid='.$ch_pass_uid.'</a>';
  82. $message = get_lang_f('confirm_change_password_message',$recover_link);
  83. if (mymail($email_address, $subject, $message, $settings) == TRUE)
  84. {
  85. echo "<p>".get_lang('confirm_send')."</p>";
  86. }
  87. else
  88. {
  89. echo "<p>".get_lang('mail_failed')."</p>";
  90. }
  91. unset($_POST['email_address']);
  92. }
  93. }
  94. }else{
  95. // Show form
  96. $showForm = 1;
  97. }
  98. // Any errors? If so, show the form
  99. if($errorCount > 0){
  100. $showForm = 1;
  101. }
  102. if(isset($showForm) and $showForm == 1){
  103. echo '<table style="width:200px" align="center" >
  104. <tr>
  105. <td colspan=2 >';
  106. // Print errors if there are any
  107. if(isset($errors) && is_array($errors)){
  108. foreach($errors as $error){
  109. echo '<p style="color: red;">' . $error . '</p>';
  110. }
  111. }
  112. echo '<form method="post" action="?m=lostpwd'.$lang_switch.'">
  113. <label for="email_address">' . get_lang("email") . '</label>
  114. </td>
  115. </tr>
  116. <tr>
  117. <td>
  118. <input type="text" title="' . get_lang("enter_email") . '" name="email_address" size="30" value="';
  119. if(isset($email_address)){
  120. echo $email_address;
  121. }
  122. echo '"/>
  123. </td>
  124. </tr>
  125. <tr>
  126. <td style="text-align:right;">
  127. <input type="submit" value="' . get_lang("submit") . '" class="submit-button"/>
  128. </form>
  129. </td>
  130. </tr>
  131. <tr>
  132. <td style="text-align:left;">
  133. <form method="post" action="index.php' . str_replace("&","?",$lang_switch) . '" style="margin-top:-28px;">
  134. <input type="submit" value="<<&nbsp;' . get_lang("back") .'" class="submit-button"/>
  135. </form>
  136. </td>
  137. </tr></table>';
  138. }
  139. }else if(isset( $_GET['user_id'] ) AND isset( $_GET['ch_pass_uid'] )){
  140. $user_id = trim($_GET['user_id']);
  141. $ch_pass_uid = trim($_GET['ch_pass_uid']);
  142. $user_info = $db->getUserById($user_id);
  143. if ( empty($user_info) )
  144. {
  145. print_failure(get_lang('errormail'));
  146. echo "<p><a href='" . $moduleLink . "'>&lt;&lt; ".get_lang('back')."</a></p>";
  147. return;
  148. }
  149. $email_address = $user_info['users_email'];
  150. $random_password = makeRandomPassword();
  151. $db_password = md5($random_password);
  152. $old_pass_md5_hash = $user_info['users_passwd'];
  153. if ( $old_pass_md5_hash != $ch_pass_uid )
  154. {
  155. print_failure("Failed to update password for user.");
  156. echo "<p><a href='" . $moduleLink . "'>&lt;&lt; ".get_lang('back')."</a></p>";
  157. return;
  158. }
  159. $random_password = makeRandomPassword();
  160. $db_password = md5($random_password);
  161. if ( $db->updateUsersPassword($user_id,$db_password) === FALSE )
  162. {
  163. print_failure("Failed to update password for user.");
  164. echo "<p><a href='" . $moduleLink . "'>&lt;&lt; ".get_lang('back')."</a></p>";
  165. return;
  166. }
  167. $subject = get_lang('subject');
  168. $message = get_lang_f('password_message',$random_password);
  169. if (mymail($email_address, $subject, $message, $settings) == TRUE)
  170. {
  171. echo "<p>".get_lang('send')."</p>";
  172. }
  173. else
  174. {
  175. echo "<p>".get_lang('mail_failed')."</p>";
  176. }
  177. echo "<p>".get_lang('click')." <a href='index.php'>".get_lang('here')."</a> ".get_lang('to_login')."</p>";
  178. }else{
  179. print_failure("Security alert.");
  180. }
  181. }
  182. ?>