Reminder.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /* This file deals with sending out reminders, and checking the secret answer
  15. and question. It uses just a few functions to do this, which are:
  16. void RemindMe()
  17. - this is just the controlling delegator.
  18. - uses the Profile language files and Reminder template.
  19. void RemindMail()
  20. // !!!
  21. void setPassword()
  22. // !!!
  23. void setPassword2()
  24. // !!!
  25. void SecretAnswerInput()
  26. // !!!
  27. void SecretAnswer2()
  28. // !!!
  29. */
  30. // Forgot 'yer password?
  31. function RemindMe()
  32. {
  33. global $txt, $context;
  34. loadLanguage('Profile');
  35. loadTemplate('Reminder');
  36. $context['page_title'] = $txt['authentication_reminder'];
  37. $context['robot_no_index'] = true;
  38. // Delegation can be useful sometimes.
  39. $subActions = array(
  40. 'picktype' => 'RemindPick',
  41. 'secret2' => 'SecretAnswer2',
  42. 'setpassword' =>'setPassword',
  43. 'setpassword2' =>'setPassword2'
  44. );
  45. // Any subaction? If none, fall through to the main template, which will ask for one.
  46. if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
  47. $subActions[$_REQUEST['sa']]();
  48. }
  49. // Pick a reminder type.
  50. function RemindPick()
  51. {
  52. global $context, $txt, $scripturl, $sourcedir, $user_info, $webmaster_email, $smcFunc, $language, $modSettings;
  53. checkSession();
  54. // Coming with a known ID?
  55. if (!empty($_REQUEST['uid']))
  56. {
  57. $where = 'id_member = {int:id_member}';
  58. $where_params['id_member'] = (int) $_REQUEST['uid'];
  59. }
  60. elseif (isset($_POST['user']) && $_POST['user'] != '')
  61. {
  62. $where = 'member_name = {string:member_name}';
  63. $where_params['member_name'] = $_POST['user'];
  64. $where_params['email_address'] = $_POST['user'];
  65. }
  66. // You must enter a username/email address.
  67. if (empty($where))
  68. fatal_lang_error('username_no_exist', false);
  69. // Find the user!
  70. $request = $smcFunc['db_query']('', '
  71. SELECT id_member, real_name, member_name, email_address, is_activated, validation_code, lngfile, openid_uri, secret_question
  72. FROM {db_prefix}members
  73. WHERE ' . $where . '
  74. LIMIT 1',
  75. array_merge($where_params, array(
  76. ))
  77. );
  78. // Maybe email?
  79. if ($smcFunc['db_num_rows']($request) == 0 && empty($_REQUEST['uid']))
  80. {
  81. $smcFunc['db_free_result']($request);
  82. $request = $smcFunc['db_query']('', '
  83. SELECT id_member, real_name, member_name, email_address, is_activated, validation_code, lngfile, openid_uri, secret_question
  84. FROM {db_prefix}members
  85. WHERE email_address = {string:email_address}
  86. LIMIT 1',
  87. array_merge($where_params, array(
  88. ))
  89. );
  90. if ($smcFunc['db_num_rows']($request) == 0)
  91. fatal_lang_error('no_user_with_email', false);
  92. }
  93. $row = $smcFunc['db_fetch_assoc']($request);
  94. $smcFunc['db_free_result']($request);
  95. $context['account_type'] = !empty($row['openid_uri']) ? 'openid' : 'password';
  96. // If the user isn't activated/approved, give them some feedback on what to do next.
  97. if ($row['is_activated'] != 1)
  98. {
  99. // Awaiting approval...
  100. if (trim($row['validation_code']) == '')
  101. fatal_error($txt['registration_not_approved'] . ' <a href="' . $scripturl . '?action=activate;user=' . $_POST['user'] . '">' . $txt['here'] . '</a>.', false);
  102. else
  103. fatal_error($txt['registration_not_activated'] . ' <a href="' . $scripturl . '?action=activate;user=' . $_POST['user'] . '">' . $txt['here'] . '</a>.', false);
  104. }
  105. // You can't get emailed if you have no email address.
  106. $row['email_address'] = trim($row['email_address']);
  107. if ($row['email_address'] == '')
  108. fatal_error($txt['no_reminder_email'] . '<br />' . $txt['send_email'] . ' <a href="mailto:' . $webmaster_email . '">webmaster</a> ' . $txt['to_ask_password'] . '.');
  109. // If they have no secret question then they can only get emailed the item, or they are requesting the email, send them an email.
  110. if (empty($row['secret_question']) || (isset($_POST['reminder_type']) && $_POST['reminder_type'] == 'email'))
  111. {
  112. // Randomly generate a new password, with only alpha numeric characters that is a max length of 10 chars.
  113. require_once($sourcedir . '/Subs-Members.php');
  114. $password = generateValidationCode();
  115. require_once($sourcedir . '/Subs-Post.php');
  116. $replacements = array(
  117. 'REALNAME' => $row['real_name'],
  118. 'REMINDLINK' => $scripturl . '?action=reminder;sa=setpassword;u=' . $row['id_member'] . ';code=' . $password,
  119. 'IP' => $user_info['ip'],
  120. 'MEMBERNAME' => $row['member_name'],
  121. 'OPENID' => $row['openid_uri'],
  122. );
  123. $emaildata = loadEmailTemplate('forgot_' . $context['account_type'], $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
  124. $context['description'] = $txt['reminder_' . (!empty($row['openid_uri']) ? 'openid_' : '') . 'sent'];
  125. // If they were using OpenID simply email them their OpenID identity.
  126. sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  127. if (empty($row['openid_uri']))
  128. // Set the password in the database.
  129. updateMemberData($row['id_member'], array('validation_code' => substr(md5($password), 0, 10)));
  130. // Set up the template.
  131. $context['sub_template'] = 'sent';
  132. // Dont really.
  133. return;
  134. }
  135. // Otherwise are ready to answer the question?
  136. elseif (isset($_POST['reminder_type']) && $_POST['reminder_type'] == 'secret')
  137. {
  138. return SecretAnswerInput();
  139. }
  140. // No we're here setup the context for template number 2!
  141. $context['sub_template'] = 'reminder_pick';
  142. $context['current_member'] = array(
  143. 'id' => $row['id_member'],
  144. 'name' => $row['member_name'],
  145. );
  146. }
  147. // Set your new password
  148. function setPassword()
  149. {
  150. global $txt, $context;
  151. loadLanguage('Login');
  152. // You need a code!
  153. if (!isset($_REQUEST['code']))
  154. fatal_lang_error('no_access', false);
  155. // Fill the context array.
  156. $context += array(
  157. 'page_title' => $txt['reminder_set_password'],
  158. 'sub_template' => 'set_password',
  159. 'code' => $_REQUEST['code'],
  160. 'memID' => (int) $_REQUEST['u']
  161. );
  162. }
  163. function setPassword2()
  164. {
  165. global $context, $txt, $modSettings, $smcFunc, $sourcedir;
  166. checkSession();
  167. if (empty($_POST['u']) || !isset($_POST['passwrd1']) || !isset($_POST['passwrd2']))
  168. fatal_lang_error('no_access', false);
  169. $_POST['u'] = (int) $_POST['u'];
  170. if ($_POST['passwrd1'] != $_POST['passwrd2'])
  171. fatal_lang_error('passwords_dont_match', false);
  172. if ($_POST['passwrd1'] == '')
  173. fatal_lang_error('no_password', false);
  174. loadLanguage('Login');
  175. // Get the code as it should be from the database.
  176. $request = $smcFunc['db_query']('', '
  177. SELECT validation_code, member_name, email_address, passwd_flood
  178. FROM {db_prefix}members
  179. WHERE id_member = {int:id_member}
  180. AND is_activated = {int:is_activated}
  181. AND validation_code != {string:blank_string}
  182. LIMIT 1',
  183. array(
  184. 'id_member' => $_POST['u'],
  185. 'is_activated' => 1,
  186. 'blank_string' => '',
  187. )
  188. );
  189. // Does this user exist at all?
  190. if ($smcFunc['db_num_rows']($request) == 0)
  191. fatal_lang_error('invalid_userid', false);
  192. list ($realCode, $username, $email, $flood_value) = $smcFunc['db_fetch_row']($request);
  193. $smcFunc['db_free_result']($request);
  194. // Is the password actually valid?
  195. require_once($sourcedir . '/Subs-Auth.php');
  196. $passwordError = validatePassword($_POST['passwrd1'], $username, array($email));
  197. // What - it's not?
  198. if ($passwordError != null)
  199. fatal_lang_error('profile_error_password_' . $passwordError, false);
  200. require_once($sourcedir . '/LogInOut.php');
  201. // Quit if this code is not right.
  202. if (empty($_POST['code']) || substr($realCode, 0, 10) != substr(md5($_POST['code']), 0, 10))
  203. {
  204. // Stop brute force attacks like this.
  205. validatePasswordFlood($_POST['u'], $flood_value, false);
  206. fatal_error($txt['invalid_activation_code'], false);
  207. }
  208. // Just in case, flood control.
  209. validatePasswordFlood($_POST['u'], $flood_value, true);
  210. // User validated. Update the database!
  211. updateMemberData($_POST['u'], array('validation_code' => '', 'passwd' => sha1(strtolower($username) . $_POST['passwrd1'])));
  212. call_integration_hook('integrate_reset_pass', array($username, $username, $_POST['passwrd1']));
  213. loadTemplate('Login');
  214. $context += array(
  215. 'page_title' => $txt['reminder_password_set'],
  216. 'sub_template' => 'login',
  217. 'default_username' => $username,
  218. 'default_password' => $_POST['passwrd1'],
  219. 'never_expire' => false,
  220. 'description' => $txt['reminder_password_set']
  221. );
  222. }
  223. // Get the secret answer.
  224. function SecretAnswerInput()
  225. {
  226. global $txt, $context, $smcFunc;
  227. checkSession();
  228. // Strings for the register auto javascript clever stuffy wuffy.
  229. loadLanguage('Login');
  230. // Check they entered something...
  231. if (empty($_REQUEST['uid']))
  232. fatal_lang_error('username_no_exist', false);
  233. // Get the stuff....
  234. $request = $smcFunc['db_query']('', '
  235. SELECT id_member, real_name, member_name, secret_question, openid_uri
  236. FROM {db_prefix}members
  237. WHERE id_member = {int:id_member}
  238. LIMIT 1',
  239. array(
  240. 'id_member' => (int) $_REQUEST['uid'],
  241. )
  242. );
  243. if ($smcFunc['db_num_rows']($request) == 0)
  244. fatal_lang_error('username_no_exist', false);
  245. $row = $smcFunc['db_fetch_assoc']($request);
  246. $smcFunc['db_free_result']($request);
  247. $context['account_type'] = !empty($row['openid_uri']) ? 'openid' : 'password';
  248. // If there is NO secret question - then throw an error.
  249. if (trim($row['secret_question']) == '')
  250. fatal_lang_error('registration_no_secret_question', false);
  251. // Ask for the answer...
  252. $context['remind_user'] = $row['id_member'];
  253. $context['remind_type'] = '';
  254. $context['secret_question'] = $row['secret_question'];
  255. $context['sub_template'] = 'ask';
  256. }
  257. function SecretAnswer2()
  258. {
  259. global $txt, $context, $modSettings, $smcFunc, $sourcedir;
  260. checkSession();
  261. // Hacker? How did you get this far without an email or username?
  262. if (empty($_REQUEST['uid']))
  263. fatal_lang_error('username_no_exist', false);
  264. loadLanguage('Login');
  265. // Get the information from the database.
  266. $request = $smcFunc['db_query']('', '
  267. SELECT id_member, real_name, member_name, secret_answer, secret_question, openid_uri, email_address
  268. FROM {db_prefix}members
  269. WHERE id_member = {int:id_member}
  270. LIMIT 1',
  271. array(
  272. 'id_member' => $_REQUEST['uid'],
  273. )
  274. );
  275. if ($smcFunc['db_num_rows']($request) == 0)
  276. fatal_lang_error('username_no_exist', false);
  277. $row = $smcFunc['db_fetch_assoc']($request);
  278. $smcFunc['db_free_result']($request);
  279. // Check if the secret answer is correct.
  280. if ($row['secret_question'] == '' || $row['secret_answer'] == '' || md5($_POST['secret_answer']) != $row['secret_answer'])
  281. {
  282. log_error(sprintf($txt['reminder_error'], $row['member_name']), 'user');
  283. fatal_lang_error('incorrect_answer', false);
  284. }
  285. // If it's OpenID this is where the music ends.
  286. if (!empty($row['openid_uri']))
  287. {
  288. $context['sub_template'] = 'sent';
  289. $context['description'] = sprintf($txt['reminder_openid_is'], $row['openid_uri']);
  290. return;
  291. }
  292. // You can't use a blank one!
  293. if (strlen(trim($_POST['passwrd1'])) === 0)
  294. fatal_lang_error('no_password', false);
  295. // They have to be the same too.
  296. if ($_POST['passwrd1'] != $_POST['passwrd2'])
  297. fatal_lang_error('passwords_dont_match', false);
  298. // Make sure they have a strong enough password.
  299. require_once($sourcedir . '/Subs-Auth.php');
  300. $passwordError = validatePassword($_POST['passwrd1'], $row['member_name'], array($row['email_address']));
  301. // Invalid?
  302. if ($passwordError != null)
  303. fatal_lang_error('profile_error_password_' . $passwordError, false);
  304. // Alright, so long as 'yer sure.
  305. updateMemberData($row['id_member'], array('passwd' => sha1(strtolower($row['member_name']) . $_POST['passwrd1'])));
  306. call_integration_hook('integrate_reset_pass', array($row['member_name'], $row['member_name'], $_POST['passwrd1']));
  307. // Tell them it went fine.
  308. loadTemplate('Login');
  309. $context += array(
  310. 'page_title' => $txt['reminder_password_set'],
  311. 'sub_template' => 'login',
  312. 'default_username' => $row['member_name'],
  313. 'default_password' => $_POST['passwrd1'],
  314. 'never_expire' => false,
  315. 'description' => $txt['reminder_password_set']
  316. );
  317. }
  318. ?>