Reminder.php 12 KB

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