Reminder.php 12 KB

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