Reminder.php 12 KB

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