Register.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. <?php
  2. /**
  3. * This file has two main jobs, but they really are one. It registers new
  4. * members, and it helps the administrator moderate member registrations.
  5. * Similarly, it handles account activation as well.
  6. *
  7. * Simple Machines Forum (SMF)
  8. *
  9. * @package SMF
  10. * @author Simple Machines http://www.simplemachines.org
  11. * @copyright 2014 Simple Machines and individual contributors
  12. * @license http://www.simplemachines.org/about/smf/license.php BSD
  13. *
  14. * @version 2.1 Alpha 1
  15. */
  16. if (!defined('SMF'))
  17. die('No direct access...');
  18. /**
  19. * Begin the registration process.
  20. *
  21. * @param array $reg_errors = array()
  22. */
  23. function Register($reg_errors = array())
  24. {
  25. global $txt, $boarddir, $context, $modSettings, $user_info;
  26. global $language, $scripturl, $smcFunc, $sourcedir, $smcFunc, $cur_profile;
  27. // Is this an incoming AJAX check?
  28. if (isset($_GET['sa']) && $_GET['sa'] == 'usernamecheck')
  29. return RegisterCheckUsername();
  30. // Check if the administrator has it disabled.
  31. if (!empty($modSettings['registration_method']) && $modSettings['registration_method'] == '3')
  32. fatal_lang_error('registration_disabled', false);
  33. // If this user is an admin - redirect them to the admin registration page.
  34. if (allowedTo('moderate_forum') && !$user_info['is_guest'])
  35. redirectexit('action=admin;area=regcenter;sa=register');
  36. // You are not a guest, so you are a member - and members don't get to register twice!
  37. elseif (empty($user_info['is_guest']))
  38. redirectexit();
  39. loadLanguage('Login');
  40. loadTemplate('Register');
  41. // Do we need them to agree to the registration agreement, first?
  42. $context['require_agreement'] = !empty($modSettings['requireAgreement']);
  43. $context['registration_passed_agreement'] = !empty($_SESSION['registration_agreed']);
  44. $context['show_coppa'] = !empty($modSettings['coppaAge']);
  45. // Under age restrictions?
  46. if ($context['show_coppa'])
  47. {
  48. $context['skip_coppa'] = false;
  49. $context['coppa_agree_above'] = sprintf($txt[($context['require_agreement'] ? 'agreement_' : '') . 'agree_coppa_above'], $modSettings['coppaAge']);
  50. $context['coppa_agree_below'] = sprintf($txt[($context['require_agreement'] ? 'agreement_' : '') . 'agree_coppa_below'], $modSettings['coppaAge']);
  51. }
  52. // What step are we at?
  53. $current_step = isset($_REQUEST['step']) ? (int) $_REQUEST['step'] : ($context['require_agreement'] ? 1 : 2);
  54. // Does this user agree to the registation agreement?
  55. if ($current_step == 1 && (isset($_POST['accept_agreement']) || isset($_POST['accept_agreement_coppa'])))
  56. {
  57. $context['registration_passed_agreement'] = $_SESSION['registration_agreed'] = true;
  58. $current_step = 2;
  59. // Skip the coppa procedure if the user says he's old enough.
  60. if ($context['show_coppa'])
  61. {
  62. $_SESSION['skip_coppa'] = !empty($_POST['accept_agreement']);
  63. // Are they saying they're under age, while under age registration is disabled?
  64. if (empty($modSettings['coppaType']) && empty($_SESSION['skip_coppa']))
  65. {
  66. loadLanguage('Login');
  67. fatal_lang_error('under_age_registration_prohibited', false, array($modSettings['coppaAge']));
  68. }
  69. }
  70. }
  71. // Make sure they don't squeeze through without agreeing.
  72. elseif ($current_step > 1 && $context['require_agreement'] && !$context['registration_passed_agreement'])
  73. $current_step = 1;
  74. // Show the user the right form.
  75. $context['sub_template'] = $current_step == 1 ? 'registration_agreement' : 'registration_form';
  76. $context['page_title'] = $current_step == 1 ? $txt['registration_agreement'] : $txt['registration_form'];
  77. // Add the register chain to the link tree.
  78. $context['linktree'][] = array(
  79. 'url' => $scripturl . '?action=register',
  80. 'name' => $txt['register'],
  81. );
  82. // Prepare the time gate! Do it like so, in case later steps want to reset the limit for any reason, but make sure the time is the current one.
  83. if (!isset($_SESSION['register']))
  84. $_SESSION['register'] = array(
  85. 'timenow' => time(),
  86. 'limit' => 10, // minimum number of seconds required on this page for registration
  87. );
  88. else
  89. $_SESSION['register']['timenow'] = time();
  90. // If you have to agree to the agreement, it needs to be fetched from the file.
  91. if ($context['require_agreement'])
  92. {
  93. // Have we got a localized one?
  94. if (file_exists($boarddir . '/agreement.' . $user_info['language'] . '.txt'))
  95. $context['agreement'] = parse_bbc(file_get_contents($boarddir . '/agreement.' . $user_info['language'] . '.txt'), true, 'agreement_' . $user_info['language']);
  96. elseif (file_exists($boarddir . '/agreement.txt'))
  97. $context['agreement'] = parse_bbc(file_get_contents($boarddir . '/agreement.txt'), true, 'agreement');
  98. else
  99. $context['agreement'] = '';
  100. // Nothing to show, lets disable registration and inform the admin of this error
  101. if (empty($context['agreement']))
  102. {
  103. // No file found or a blank file, log the error so the admin knows there is a problem!
  104. log_error($txt['registration_agreement_missing'], 'critical');
  105. fatal_lang_error('registration_disabled', false);
  106. }
  107. }
  108. if (!empty($modSettings['userLanguage']))
  109. {
  110. $selectedLanguage = empty($_SESSION['language']) ? $language : $_SESSION['language'];
  111. // Do we have any languages?
  112. if (empty($context['languages']))
  113. getLanguages();
  114. // Try to find our selected language.
  115. foreach ($context['languages'] as $key => $lang)
  116. {
  117. $context['languages'][$key]['name'] = strtr($lang['name'], array('-utf8' => ''));
  118. // Found it!
  119. if ($selectedLanguage == $lang['filename'])
  120. $context['languages'][$key]['selected'] = true;
  121. }
  122. }
  123. // Any custom fields we want filled in?
  124. require_once($sourcedir . '/Profile.php');
  125. loadCustomFields(0, 'register');
  126. // Or any standard ones?
  127. if (!empty($modSettings['registration_fields']))
  128. {
  129. require_once($sourcedir . '/Profile-Modify.php');
  130. // Setup some important context.
  131. loadLanguage('Profile');
  132. loadTemplate('Profile');
  133. $context['user']['is_owner'] = true;
  134. // Here, and here only, emulate the permissions the user would have to do this.
  135. $user_info['permissions'] = array_merge($user_info['permissions'], array('profile_account_own', 'profile_extra_own', 'profile_other_own', 'profile_password_own'));
  136. $reg_fields = explode(',', $modSettings['registration_fields']);
  137. // We might have had some submissions on this front - go check.
  138. foreach ($reg_fields as $field)
  139. if (isset($_POST[$field]))
  140. $cur_profile[$field] = $smcFunc['htmlspecialchars']($_POST[$field]);
  141. // Load all the fields in question.
  142. setupProfileContext($reg_fields);
  143. }
  144. // Generate a visual verification code to make sure the user is no bot.
  145. if (!empty($modSettings['reg_verification']))
  146. {
  147. require_once($sourcedir . '/Subs-Editor.php');
  148. $verificationOptions = array(
  149. 'id' => 'register',
  150. );
  151. $context['visual_verification'] = create_control_verification($verificationOptions);
  152. $context['visual_verification_id'] = $verificationOptions['id'];
  153. }
  154. // Otherwise we have nothing to show.
  155. else
  156. $context['visual_verification'] = false;
  157. // Are they coming from an OpenID login attempt?
  158. if (!empty($_SESSION['openid']['verified']) && !empty($_SESSION['openid']['openid_uri']))
  159. {
  160. $context['openid'] = $_SESSION['openid']['openid_uri'];
  161. $context['username'] = $smcFunc['htmlspecialchars'](!empty($_POST['user']) ? $_POST['user'] : $_SESSION['openid']['nickname']);
  162. $context['email'] = $smcFunc['htmlspecialchars'](!empty($_POST['email']) ? $_POST['email'] : $_SESSION['openid']['email']);
  163. }
  164. // See whether we have some prefiled values.
  165. else
  166. {
  167. $context += array(
  168. 'openid' => isset($_POST['openid_identifier']) ? $_POST['openid_identifier'] : '',
  169. 'username' => isset($_POST['user']) ? $smcFunc['htmlspecialchars']($_POST['user']) : '',
  170. 'email' => isset($_POST['email']) ? $smcFunc['htmlspecialchars']($_POST['email']) : '',
  171. );
  172. }
  173. // @todo Why isn't this a simple set operation?
  174. // Were there any errors?
  175. $context['registration_errors'] = array();
  176. if (!empty($reg_errors))
  177. foreach ($reg_errors as $error)
  178. $context['registration_errors'][] = $error;
  179. createToken('register');
  180. }
  181. /**
  182. * Actually register the member.
  183. *
  184. * @param bool $verifiedOpenID = false
  185. */
  186. function Register2($verifiedOpenID = false)
  187. {
  188. global $scripturl, $txt, $modSettings, $context, $sourcedir;
  189. global $user_info, $options, $smcFunc;
  190. checkSession();
  191. validateToken('register');
  192. // Start collecting together any errors.
  193. $reg_errors = array();
  194. // Did we save some open ID fields?
  195. if ($verifiedOpenID && !empty($context['openid_save_fields']))
  196. {
  197. foreach ($context['openid_save_fields'] as $id => $value)
  198. $_POST[$id] = $value;
  199. }
  200. // You can't register if it's disabled.
  201. if (!empty($modSettings['registration_method']) && $modSettings['registration_method'] == 3)
  202. fatal_lang_error('registration_disabled', false);
  203. // Things we don't do for people who have already confirmed their OpenID allegances via register.
  204. if (!$verifiedOpenID)
  205. {
  206. // Well, if you don't agree, you can't register.
  207. if (!empty($modSettings['requireAgreement']) && empty($_SESSION['registration_agreed']))
  208. redirectexit();
  209. // Make sure they came from *somewhere*, have a session.
  210. if (!isset($_SESSION['old_url']))
  211. redirectexit('action=register');
  212. // If we don't require an agreement, we need a extra check for coppa.
  213. if (empty($modSettings['requireAgreement']) && !empty($modSettings['coppaAge']))
  214. $_SESSION['skip_coppa'] = !empty($_POST['accept_agreement']);
  215. // Are they under age, and under age users are banned?
  216. if (!empty($modSettings['coppaAge']) && empty($modSettings['coppaType']) && empty($_SESSION['skip_coppa']))
  217. {
  218. // @todo This should be put in Errors, imho.
  219. loadLanguage('Login');
  220. fatal_lang_error('under_age_registration_prohibited', false, array($modSettings['coppaAge']));
  221. }
  222. // Check the time gate for miscreants. First make sure they came from somewhere that actually set it up.
  223. if (empty($_SESSION['register']['timenow']) || empty($_SESSION['register']['limit']))
  224. redirectexit('action=register');
  225. // Failing that, check the time on it.
  226. if (time() - $_SESSION['register']['timenow'] < $_SESSION['register']['limit'])
  227. {
  228. // @todo This too should be put in Errors, imho.
  229. loadLanguage('Login');
  230. $reg_errors[] = $txt['error_too_quickly'];
  231. }
  232. // Check whether the visual verification code was entered correctly.
  233. if (!empty($modSettings['reg_verification']))
  234. {
  235. require_once($sourcedir . '/Subs-Editor.php');
  236. $verificationOptions = array(
  237. 'id' => 'register',
  238. );
  239. $context['visual_verification'] = create_control_verification($verificationOptions, true);
  240. if (is_array($context['visual_verification']))
  241. {
  242. loadLanguage('Errors');
  243. foreach ($context['visual_verification'] as $error)
  244. $reg_errors[] = $txt['error_' . $error];
  245. }
  246. }
  247. }
  248. foreach ($_POST as $key => $value)
  249. {
  250. if (!is_array($_POST[$key]))
  251. $_POST[$key] = htmltrim__recursive(str_replace(array("\n", "\r"), '', $_POST[$key]));
  252. }
  253. // Collect all extra registration fields someone might have filled in.
  254. $possible_strings = array(
  255. 'birthdate',
  256. 'time_format',
  257. 'buddy_list',
  258. 'pm_ignore_list',
  259. 'smiley_set',
  260. 'personal_text', 'avatar',
  261. 'lngfile',
  262. 'secret_question', 'secret_answer',
  263. );
  264. $possible_ints = array(
  265. 'pm_email_notify',
  266. 'notify_types',
  267. 'id_theme',
  268. );
  269. $possible_floats = array(
  270. 'time_offset',
  271. );
  272. $possible_bools = array(
  273. 'notify_announcements', 'notify_regularity', 'notify_send_body',
  274. 'hide_email', 'show_online',
  275. );
  276. // We may want to add certain things to these if selected in the admin panel.
  277. if (!empty($modSettings['registration_fields']))
  278. {
  279. $reg_fields = explode(',', $modSettings['registration_fields']);
  280. // Easy string fields first.
  281. foreach (array('skype', 'aim', 'yim', 'location') as $field)
  282. if (in_array($field, $reg_fields))
  283. $possible_strings[] = $field;
  284. // Then the easy numeric fields.
  285. foreach (array('icq', 'gender') as $field)
  286. if (in_array($field, $reg_fields))
  287. $possible_ints[] = $field;
  288. // Website is a little different
  289. if (in_array('website', $reg_fields))
  290. $possible_strings += array('website_url', 'website_title');
  291. }
  292. if (isset($_POST['secret_answer']) && $_POST['secret_answer'] != '')
  293. $_POST['secret_answer'] = md5($_POST['secret_answer']);
  294. // Needed for isReservedName() and registerMember().
  295. require_once($sourcedir . '/Subs-Members.php');
  296. // Validation... even if we're not a mall.
  297. if (isset($_POST['real_name']) && (allowedTo('profile_displayed_name') || allowedTo('moderate_forum')))
  298. {
  299. $_POST['real_name'] = trim(preg_replace('~[\t\n\r \x0B\0' . ($context['utf8'] ? '\x{A0}\x{AD}\x{2000}-\x{200F}\x{201F}\x{202F}\x{3000}\x{FEFF}' : '\x00-\x08\x0B\x0C\x0E-\x19\xA0') . ']+~' . ($context['utf8'] ? 'u' : ''), ' ', $_POST['real_name']));
  300. if (trim($_POST['real_name']) != '' && !isReservedName($_POST['real_name']) && $smcFunc['strlen']($_POST['real_name']) < 60)
  301. $possible_strings[] = 'real_name';
  302. }
  303. // Handle a string as a birthdate...
  304. if (isset($_POST['birthdate']) && $_POST['birthdate'] != '')
  305. $_POST['birthdate'] = strftime('%Y-%m-%d', strtotime($_POST['birthdate']));
  306. // Or birthdate parts...
  307. elseif (!empty($_POST['bday1']) && !empty($_POST['bday2']))
  308. $_POST['birthdate'] = sprintf('%04d-%02d-%02d', empty($_POST['bday3']) ? 0 : (int) $_POST['bday3'], (int) $_POST['bday1'], (int) $_POST['bday2']);
  309. // By default assume email is hidden, only show it if we tell it to.
  310. $_POST['hide_email'] = !empty($_POST['allow_email']) ? 0 : 1;
  311. // Validate the passed language file.
  312. if (isset($_POST['lngfile']) && !empty($modSettings['userLanguage']))
  313. {
  314. // Do we have any languages?
  315. if (empty($context['languages']))
  316. getLanguages();
  317. // Did we find it?
  318. if (isset($context['languages'][$_POST['lngfile']]))
  319. $_SESSION['language'] = $_POST['lngfile'];
  320. else
  321. unset($_POST['lngfile']);
  322. }
  323. else
  324. unset($_POST['lngfile']);
  325. // Set the options needed for registration.
  326. $regOptions = array(
  327. 'interface' => 'guest',
  328. 'username' => !empty($_POST['user']) ? $_POST['user'] : '',
  329. 'email' => !empty($_POST['email']) ? $_POST['email'] : '',
  330. 'password' => !empty($_POST['passwrd1']) ? $_POST['passwrd1'] : '',
  331. 'password_check' => !empty($_POST['passwrd2']) ? $_POST['passwrd2'] : '',
  332. 'openid' => !empty($_POST['openid_identifier']) ? $_POST['openid_identifier'] : '',
  333. 'auth_method' => !empty($_POST['authenticate']) ? $_POST['authenticate'] : '',
  334. 'check_reserved_name' => true,
  335. 'check_password_strength' => true,
  336. 'check_email_ban' => true,
  337. 'send_welcome_email' => !empty($modSettings['send_welcomeEmail']),
  338. 'require' => !empty($modSettings['coppaAge']) && !$verifiedOpenID && empty($_SESSION['skip_coppa']) ? 'coppa' : (empty($modSettings['registration_method']) ? 'nothing' : ($modSettings['registration_method'] == 1 ? 'activation' : 'approval')),
  339. 'extra_register_vars' => array(),
  340. 'theme_vars' => array(),
  341. );
  342. // Include the additional options that might have been filled in.
  343. foreach ($possible_strings as $var)
  344. if (isset($_POST[$var]))
  345. $regOptions['extra_register_vars'][$var] = $smcFunc['htmlspecialchars']($_POST[$var], ENT_QUOTES);
  346. foreach ($possible_ints as $var)
  347. if (isset($_POST[$var]))
  348. $regOptions['extra_register_vars'][$var] = (int) $_POST[$var];
  349. foreach ($possible_floats as $var)
  350. if (isset($_POST[$var]))
  351. $regOptions['extra_register_vars'][$var] = (float) $_POST[$var];
  352. foreach ($possible_bools as $var)
  353. if (isset($_POST[$var]))
  354. $regOptions['extra_register_vars'][$var] = empty($_POST[$var]) ? 0 : 1;
  355. // Registration options are always default options...
  356. if (isset($_POST['default_options']))
  357. $_POST['options'] = isset($_POST['options']) ? $_POST['options'] + $_POST['default_options'] : $_POST['default_options'];
  358. $regOptions['theme_vars'] = isset($_POST['options']) && is_array($_POST['options']) ? $_POST['options'] : array();
  359. // Make sure they are clean, dammit!
  360. $regOptions['theme_vars'] = htmlspecialchars__recursive($regOptions['theme_vars']);
  361. // Check whether we have fields that simply MUST be displayed?
  362. $request = $smcFunc['db_query']('', '
  363. SELECT col_name, field_name, field_type, field_length, mask, show_reg
  364. FROM {db_prefix}custom_fields
  365. WHERE active = {int:is_active}',
  366. array(
  367. 'is_active' => 1,
  368. )
  369. );
  370. $custom_field_errors = array();
  371. while ($row = $smcFunc['db_fetch_assoc']($request))
  372. {
  373. // Don't allow overriding of the theme variables.
  374. if (isset($regOptions['theme_vars'][$row['col_name']]))
  375. unset($regOptions['theme_vars'][$row['col_name']]);
  376. // Not actually showing it then?
  377. if (!$row['show_reg'])
  378. continue;
  379. // Prepare the value!
  380. $value = isset($_POST['customfield'][$row['col_name']]) ? trim($_POST['customfield'][$row['col_name']]) : '';
  381. // We only care for text fields as the others are valid to be empty.
  382. if (!in_array($row['field_type'], array('check', 'select', 'radio')))
  383. {
  384. // Is it too long?
  385. if ($row['field_length'] && $row['field_length'] < $smcFunc['strlen']($value))
  386. $custom_field_errors[] = array('custom_field_too_long', array($row['field_name'], $row['field_length']));
  387. // Any masks to apply?
  388. if ($row['field_type'] == 'text' && !empty($row['mask']) && $row['mask'] != 'none')
  389. {
  390. // @todo We never error on this - just ignore it at the moment...
  391. if ($row['mask'] == 'email' && (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $value) === 0 || strlen($value) > 255))
  392. $custom_field_errors[] = array('custom_field_invalid_email', array($row['field_name']));
  393. elseif ($row['mask'] == 'number' && preg_match('~[^\d]~', $value))
  394. $custom_field_errors[] = array('custom_field_not_number', array($row['field_name']));
  395. elseif (substr($row['mask'], 0, 5) == 'regex' && trim($value) != '' && preg_match(substr($row['mask'], 5), $value) === 0)
  396. $custom_field_errors[] = array('custom_field_inproper_format', array($row['field_name']));
  397. }
  398. }
  399. // Is this required but not there?
  400. if (trim($value) == '' && $row['show_reg'] > 1)
  401. $custom_field_errors[] = array('custom_field_empty', array($row['field_name']));
  402. }
  403. $smcFunc['db_free_result']($request);
  404. // Process any errors.
  405. if (!empty($custom_field_errors))
  406. {
  407. loadLanguage('Errors');
  408. foreach ($custom_field_errors as $error)
  409. $reg_errors[] = vsprintf($txt['error_' . $error[0]], $error[1]);
  410. }
  411. // Lets check for other errors before trying to register the member.
  412. if (!empty($reg_errors))
  413. {
  414. $_REQUEST['step'] = 2;
  415. $_SESSION['register']['limit'] = 5; // If they've filled in some details, they won't need the full 10 seconds of the limit.
  416. return Register($reg_errors);
  417. }
  418. // If they're wanting to use OpenID we need to validate them first.
  419. if (empty($_SESSION['openid']['verified']) && !empty($_POST['authenticate']) && $_POST['authenticate'] == 'openid')
  420. {
  421. // What do we need to save?
  422. $save_variables = array();
  423. foreach ($_POST as $k => $v)
  424. if (!in_array($k, array('sc', 'sesc', $context['session_var'], 'passwrd1', 'passwrd2', 'regSubmit')))
  425. $save_variables[$k] = $v;
  426. require_once($sourcedir . '/Subs-OpenID.php');
  427. smf_openID_validate($_POST['openid_identifier'], false, $save_variables);
  428. }
  429. // If we've come from OpenID set up some default stuff.
  430. elseif ($verifiedOpenID || (!empty($_POST['openid_identifier']) && $_POST['authenticate'] == 'openid'))
  431. {
  432. $regOptions['username'] = !empty($_POST['user']) && trim($_POST['user']) != '' ? $_POST['user'] : $_SESSION['openid']['nickname'];
  433. $regOptions['email'] = !empty($_POST['email']) && trim($_POST['email']) != '' ? $_POST['email'] : $_SESSION['openid']['email'];
  434. $regOptions['auth_method'] = 'openid';
  435. $regOptions['openid'] = !empty($_POST['openid_identifier']) ? $_POST['openid_identifier'] : $_SESSION['openid']['openid_uri'];
  436. }
  437. $memberID = registerMember($regOptions, true);
  438. // What there actually an error of some kind dear boy?
  439. if (is_array($memberID))
  440. {
  441. $reg_errors = array_merge($reg_errors, $memberID);
  442. $_REQUEST['step'] = 2;
  443. return Register($reg_errors);
  444. }
  445. // Do our spam protection now.
  446. spamProtection('register');
  447. // We'll do custom fields after as then we get to use the helper function!
  448. if (!empty($_POST['customfield']))
  449. {
  450. require_once($sourcedir . '/Profile.php');
  451. require_once($sourcedir . '/Profile-Modify.php');
  452. makeCustomFieldChanges($memberID, 'register');
  453. }
  454. // If COPPA has been selected then things get complicated, setup the template.
  455. if (!empty($modSettings['coppaAge']) && empty($_SESSION['skip_coppa']))
  456. redirectexit('action=coppa;member=' . $memberID);
  457. // Basic template variable setup.
  458. elseif (!empty($modSettings['registration_method']))
  459. {
  460. loadTemplate('Register');
  461. $context += array(
  462. 'page_title' => $txt['register'],
  463. 'title' => $txt['registration_successful'],
  464. 'sub_template' => 'after',
  465. 'description' => $modSettings['registration_method'] == 2 ? $txt['approval_after_registration'] : $txt['activate_after_registration']
  466. );
  467. }
  468. else
  469. {
  470. call_integration_hook('integrate_activate', array($regOptions['username']));
  471. setLoginCookie(60 * $modSettings['cookieTime'], $memberID, sha1(sha1(strtolower($regOptions['username']) . $regOptions['password']) . $regOptions['register_vars']['password_salt']));
  472. redirectexit('action=login2;sa=check;member=' . $memberID, $context['server']['needs_login_fix']);
  473. }
  474. }
  475. /**
  476. * @todo needs description
  477. */
  478. function Activate()
  479. {
  480. global $context, $txt, $modSettings, $scripturl, $sourcedir, $smcFunc, $language, $user_info;
  481. // Logged in users should not bother to activate their accounts
  482. if (!empty($user_info['id']))
  483. redirectexit();
  484. loadLanguage('Login');
  485. loadTemplate('Login');
  486. if (empty($_REQUEST['u']) && empty($_POST['user']))
  487. {
  488. if (empty($modSettings['registration_method']) || $modSettings['registration_method'] == '3')
  489. fatal_lang_error('no_access', false);
  490. $context['member_id'] = 0;
  491. $context['sub_template'] = 'resend';
  492. $context['page_title'] = $txt['invalid_activation_resend'];
  493. $context['can_activate'] = empty($modSettings['registration_method']) || $modSettings['registration_method'] == '1';
  494. $context['default_username'] = isset($_GET['user']) ? $_GET['user'] : '';
  495. return;
  496. }
  497. // Get the code from the database...
  498. $request = $smcFunc['db_query']('', '
  499. SELECT id_member, validation_code, member_name, real_name, email_address, is_activated, passwd, lngfile
  500. FROM {db_prefix}members' . (empty($_REQUEST['u']) ? '
  501. WHERE member_name = {string:email_address} OR email_address = {string:email_address}' : '
  502. WHERE id_member = {int:id_member}') . '
  503. LIMIT 1',
  504. array(
  505. 'id_member' => isset($_REQUEST['u']) ? (int) $_REQUEST['u'] : 0,
  506. 'email_address' => isset($_POST['user']) ? $_POST['user'] : '',
  507. )
  508. );
  509. // Does this user exist at all?
  510. if ($smcFunc['db_num_rows']($request) == 0)
  511. {
  512. $context['sub_template'] = 'retry_activate';
  513. $context['page_title'] = $txt['invalid_userid'];
  514. $context['member_id'] = 0;
  515. return;
  516. }
  517. $row = $smcFunc['db_fetch_assoc']($request);
  518. $smcFunc['db_free_result']($request);
  519. // Change their email address? (they probably tried a fake one first :P.)
  520. if (isset($_POST['new_email'], $_REQUEST['passwd']) && sha1(strtolower($row['member_name']) . $_REQUEST['passwd']) == $row['passwd'] && ($row['is_activated'] == 0 || $row['is_activated'] == 2))
  521. {
  522. if (empty($modSettings['registration_method']) || $modSettings['registration_method'] == 3)
  523. fatal_lang_error('no_access', false);
  524. // @todo Separate the sprintf?
  525. if (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $_POST['new_email']) == 0)
  526. fatal_error(sprintf($txt['valid_email_needed'], $smcFunc['htmlspecialchars']($_POST['new_email'])), false);
  527. // Make sure their email isn't banned.
  528. isBannedEmail($_POST['new_email'], 'cannot_register', $txt['ban_register_prohibited']);
  529. // Ummm... don't even dare try to take someone else's email!!
  530. $request = $smcFunc['db_query']('', '
  531. SELECT id_member
  532. FROM {db_prefix}members
  533. WHERE email_address = {string:email_address}
  534. LIMIT 1',
  535. array(
  536. 'email_address' => $_POST['new_email'],
  537. )
  538. );
  539. // @todo Separate the sprintf?
  540. if ($smcFunc['db_num_rows']($request) != 0)
  541. fatal_lang_error('email_in_use', false, array($smcFunc['htmlspecialchars']($_POST['new_email'])));
  542. $smcFunc['db_free_result']($request);
  543. updateMemberData($row['id_member'], array('email_address' => $_POST['new_email']));
  544. $row['email_address'] = $_POST['new_email'];
  545. $email_change = true;
  546. }
  547. // Resend the password, but only if the account wasn't activated yet.
  548. if (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'resend' && ($row['is_activated'] == 0 || $row['is_activated'] == 2) && (!isset($_REQUEST['code']) || $_REQUEST['code'] == ''))
  549. {
  550. require_once($sourcedir . '/Subs-Post.php');
  551. $replacements = array(
  552. 'REALNAME' => $row['real_name'],
  553. 'USERNAME' => $row['member_name'],
  554. 'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $row['id_member'] . ';code=' . $row['validation_code'],
  555. 'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $row['id_member'],
  556. 'ACTIVATIONCODE' => $row['validation_code'],
  557. 'FORGOTPASSWORDLINK' => $scripturl . '?action=reminder',
  558. );
  559. $emaildata = loadEmailTemplate('resend_activate_message', $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
  560. sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 0);
  561. $context['page_title'] = $txt['invalid_activation_resend'];
  562. // This will ensure we don't actually get an error message if it works!
  563. $context['error_title'] = '';
  564. fatal_lang_error(!empty($email_change) ? 'change_email_success' : 'resend_email_success', false);
  565. }
  566. // Quit if this code is not right.
  567. if (empty($_REQUEST['code']) || $row['validation_code'] != $_REQUEST['code'])
  568. {
  569. if (!empty($row['is_activated']))
  570. fatal_lang_error('already_activated', false);
  571. elseif ($row['validation_code'] == '')
  572. {
  573. loadLanguage('Profile');
  574. fatal_error(sprintf($txt['registration_not_approved'], $scripturl . '?action=activate;user=' . $row['member_name']), false);
  575. }
  576. $context['sub_template'] = 'retry_activate';
  577. $context['page_title'] = $txt['invalid_activation_code'];
  578. $context['member_id'] = $row['id_member'];
  579. return;
  580. }
  581. // Let the integration know that they've been activated!
  582. call_integration_hook('integrate_activate', array($row['member_name']));
  583. // Validation complete - update the database!
  584. updateMemberData($row['id_member'], array('is_activated' => 1, 'validation_code' => ''));
  585. // Also do a proper member stat re-evaluation.
  586. updateStats('member', false);
  587. if (!isset($_POST['new_email']))
  588. {
  589. require_once($sourcedir . '/Subs-Post.php');
  590. adminNotify('activation', $row['id_member'], $row['member_name']);
  591. }
  592. $context += array(
  593. 'page_title' => $txt['registration_successful'],
  594. 'sub_template' => 'login',
  595. 'default_username' => $row['member_name'],
  596. 'default_password' => '',
  597. 'never_expire' => false,
  598. 'description' => $txt['activate_success']
  599. );
  600. }
  601. /**
  602. * This function will display the contact information for the forum, as well a form to fill in.
  603. */
  604. function CoppaForm()
  605. {
  606. global $context, $modSettings, $txt, $smcFunc;
  607. loadLanguage('Login');
  608. loadTemplate('Register');
  609. // No User ID??
  610. if (!isset($_GET['member']))
  611. fatal_lang_error('no_access', false);
  612. // Get the user details...
  613. $request = $smcFunc['db_query']('', '
  614. SELECT member_name
  615. FROM {db_prefix}members
  616. WHERE id_member = {int:id_member}
  617. AND is_activated = {int:is_coppa}',
  618. array(
  619. 'id_member' => (int) $_GET['member'],
  620. 'is_coppa' => 5,
  621. )
  622. );
  623. if ($smcFunc['db_num_rows']($request) == 0)
  624. fatal_lang_error('no_access', false);
  625. list ($username) = $smcFunc['db_fetch_row']($request);
  626. $smcFunc['db_free_result']($request);
  627. if (isset($_GET['form']))
  628. {
  629. // Some simple contact stuff for the forum.
  630. $context['forum_contacts'] = (!empty($modSettings['coppaPost']) ? $modSettings['coppaPost'] . '<br /><br />' : '') . (!empty($modSettings['coppaFax']) ? $modSettings['coppaFax'] . '<br />' : '');
  631. $context['forum_contacts'] = !empty($context['forum_contacts']) ? $context['forum_name_html_safe'] . '<br />' . $context['forum_contacts'] : '';
  632. // Showing template?
  633. if (!isset($_GET['dl']))
  634. {
  635. // Shortcut for producing underlines.
  636. $context['ul'] = '<u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</u>';
  637. $context['template_layers'] = array();
  638. $context['sub_template'] = 'coppa_form';
  639. $context['page_title'] = $txt['coppa_form_title'];
  640. $context['coppa_body'] = str_replace(array('{PARENT_NAME}', '{CHILD_NAME}', '{USER_NAME}'), array($context['ul'], $context['ul'], $username), $txt['coppa_form_body']);
  641. }
  642. // Downloading.
  643. else
  644. {
  645. // The data.
  646. $ul = ' ';
  647. $crlf = "\r\n";
  648. $data = $context['forum_contacts'] . $crlf . $txt['coppa_form_address'] . ':' . $crlf . $txt['coppa_form_date'] . ':' . $crlf . $crlf . $crlf . $txt['coppa_form_body'];
  649. $data = str_replace(array('{PARENT_NAME}', '{CHILD_NAME}', '{USER_NAME}', '<br>', '<br />'), array($ul, $ul, $username, $crlf, $crlf), $data);
  650. // Send the headers.
  651. header('Connection: close');
  652. header('Content-Disposition: attachment; filename="approval.txt"');
  653. header('Content-Type: ' . (isBrowser('ie') || isBrowser('opera') ? 'application/octetstream' : 'application/octet-stream'));
  654. header('Content-Length: ' . count($data));
  655. echo $data;
  656. obExit(false);
  657. }
  658. }
  659. else
  660. {
  661. $context += array(
  662. 'page_title' => $txt['coppa_title'],
  663. 'sub_template' => 'coppa',
  664. );
  665. $context['coppa'] = array(
  666. 'body' => str_replace('{MINIMUM_AGE}', $modSettings['coppaAge'], $txt['coppa_after_registration']),
  667. 'many_options' => !empty($modSettings['coppaPost']) && !empty($modSettings['coppaFax']),
  668. 'post' => empty($modSettings['coppaPost']) ? '' : $modSettings['coppaPost'],
  669. 'fax' => empty($modSettings['coppaFax']) ? '' : $modSettings['coppaFax'],
  670. 'phone' => empty($modSettings['coppaPhone']) ? '' : str_replace('{PHONE_NUMBER}', $modSettings['coppaPhone'], $txt['coppa_send_by_phone']),
  671. 'id' => $_GET['member'],
  672. );
  673. }
  674. }
  675. /**
  676. * Show the verification code or let it hear.
  677. */
  678. function VerificationCode()
  679. {
  680. global $sourcedir, $modSettings, $context, $scripturl;
  681. $verification_id = isset($_GET['vid']) ? $_GET['vid'] : '';
  682. $code = $verification_id && isset($_SESSION[$verification_id . '_vv']) ? $_SESSION[$verification_id . '_vv']['code'] : (isset($_SESSION['visual_verification_code']) ? $_SESSION['visual_verification_code'] : '');
  683. // Somehow no code was generated or the session was lost.
  684. if (empty($code))
  685. {
  686. header('Content-Type: image/gif');
  687. die("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B");
  688. }
  689. // Show a window that will play the verification code.
  690. elseif (isset($_REQUEST['sound']))
  691. {
  692. loadLanguage('Login');
  693. loadTemplate('Register');
  694. $context['verification_sound_href'] = $scripturl . '?action=verificationcode;rand=' . md5(mt_rand()) . ($verification_id ? ';vid=' . $verification_id : '') . ';format=.wav';
  695. $context['sub_template'] = 'verification_sound';
  696. $context['template_layers'] = array();
  697. obExit();
  698. }
  699. // If we have GD, try the nice code.
  700. elseif (empty($_REQUEST['format']))
  701. {
  702. require_once($sourcedir . '/Subs-Graphics.php');
  703. if (in_array('gd', get_loaded_extensions()) && !showCodeImage($code))
  704. header('HTTP/1.1 400 Bad Request');
  705. // Otherwise just show a pre-defined letter.
  706. elseif (isset($_REQUEST['letter']))
  707. {
  708. $_REQUEST['letter'] = (int) $_REQUEST['letter'];
  709. if ($_REQUEST['letter'] > 0 && $_REQUEST['letter'] <= strlen($code) && !showLetterImage(strtolower($code{$_REQUEST['letter'] - 1})))
  710. {
  711. header('Content-Type: image/gif');
  712. die("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B");
  713. }
  714. }
  715. // You must be up to no good.
  716. else
  717. {
  718. header('Content-Type: image/gif');
  719. die("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B");
  720. }
  721. }
  722. elseif ($_REQUEST['format'] === '.wav')
  723. {
  724. require_once($sourcedir . '/Subs-Sound.php');
  725. if (!createWaveFile($code))
  726. header('HTTP/1.1 400 Bad Request');
  727. }
  728. // We all die one day...
  729. die();
  730. }
  731. /**
  732. * See if a username already exists.
  733. */
  734. function RegisterCheckUsername()
  735. {
  736. global $sourcedir, $smcFunc, $context, $txt;
  737. // This is XML!
  738. loadTemplate('Xml');
  739. $context['sub_template'] = 'check_username';
  740. $context['checked_username'] = isset($_GET['username']) ? un_htmlspecialchars($_GET['username']) : '';
  741. $context['valid_username'] = true;
  742. // Clean it up like mother would.
  743. $context['checked_username'] = preg_replace('~[\t\n\r \x0B\0' . ($context['utf8'] ? '\x{A0}\x{AD}\x{2000}-\x{200F}\x{201F}\x{202F}\x{3000}\x{FEFF}' : '\x00-\x08\x0B\x0C\x0E-\x19\xA0') . ']+~' . ($context['utf8'] ? 'u' : ''), ' ', $context['checked_username']);
  744. require_once($sourcedir . '/Subs-Auth.php');
  745. $errors = validateUsername(0, $context['checked_username'], true);
  746. $context['valid_username'] = empty($errors);
  747. }
  748. ?>