PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/register.php

https://gitlab.com/LibreTitan/Panther
PHP | 332 lines | 247 code | 60 blank | 25 comment | 57 complexity | 48d9689ed70280fa23ae74e00a817fc6 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (C) 2015 Panther (https://www.pantherforum.org)
  4. * based on code by FluxBB copyright (C) 2008-2012 FluxBB
  5. * License: http://www.gnu.org/licenses/gpl.html GPL version 3 or higher
  6. */
  7. if (!defined('PANTHER'))
  8. {
  9. define('PANTHER_ROOT', __DIR__.'/');
  10. require PANTHER_ROOT.'include/common.php';
  11. }
  12. if ($panther_user['is_bot'])
  13. message($lang_common['No permission']);
  14. // If we are logged in, we shouldn't be here
  15. if (!$panther_user['is_guest'])
  16. {
  17. header('Location: '.panther_link($panther_url['index']));
  18. exit;
  19. }
  20. // Load the register.php language file
  21. require PANTHER_ROOT.'lang/'.$panther_user['language'].'/register.php';
  22. // Load the register.php/profile.php language file
  23. require PANTHER_ROOT.'lang/'.$panther_user['language'].'/prof_reg.php';
  24. if (file_exists(FORUM_CACHE_DIR.'cache_robots.php'))
  25. include FORUM_CACHE_DIR.'cache_robots.php';
  26. if (!defined('PANTHER_ROBOTS_LOADED'))
  27. {
  28. if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
  29. require PANTHER_ROOT.'include/cache.php';
  30. generate_robots_cache();
  31. require FORUM_CACHE_DIR.'cache_robots.php';
  32. }
  33. if ($panther_config['o_regs_allow'] == '0')
  34. message($lang_register['No new regs']);
  35. // User pressed the cancel button
  36. if (isset($_GET['cancel']))
  37. redirect(panther_link($panther_url['index']), $lang_register['Reg cancel redirect']);
  38. else if ($panther_config['o_rules'] == '1' && !isset($_GET['agree']) && !isset($_POST['form_sent']))
  39. {
  40. $page_title = array($panther_config['o_board_title'], $lang_register['Register'], $lang_register['Forum rules']);
  41. define('PANTHER_ACTIVE_PAGE', 'register');
  42. require PANTHER_ROOT.'header.php';
  43. $tpl = load_template('register_rules.tpl');
  44. echo $tpl->render(
  45. array(
  46. 'lang_register' => $lang_register,
  47. 'panther_config' => $panther_config,
  48. 'form_action' => panther_link($panther_url['register']),
  49. )
  50. );
  51. require PANTHER_ROOT.'footer.php';
  52. }
  53. // Start with a clean slate
  54. $errors = array();
  55. if (isset($_POST['form_sent']))
  56. {
  57. ($hook = get_extensions('register_before_validation')) ? eval($hook) : null;
  58. confirm_referrer('register.php');
  59. // Check that someone from this IP didn't register a user within the last two hours (DoS prevention)
  60. $data = array(
  61. ':remote_address' => get_remote_address(),
  62. ':registered' => (time() - 7200),
  63. );
  64. $ps = $db->select('users', 1, $data, 'registration_ip=:remote_address AND registered>:registered');
  65. if ($ps->rowCount())
  66. $errors[] = $lang_register['Registration flood'];
  67. if (!empty($panther_robots))
  68. {
  69. $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
  70. $answer = isset($_POST['answer']) ? panther_trim($_POST['answer']) : '';
  71. if (!isset($panther_robots[$id]) || $answer != $panther_robots[$id]['answer'])
  72. $errors[] = $lang_common['Robot test fail'];
  73. }
  74. $username = isset($_POST['req_user']) ? panther_trim($_POST['req_user']) : '';
  75. $email1 = isset($_POST['req_email1']) ? strtolower(panther_trim($_POST['req_email1'])) : '';
  76. $password_salt = random_pass(16);
  77. if ($panther_config['o_regs_verify'] == '1')
  78. {
  79. $email2 = isset($_POST['req_email2']) ? strtolower(panther_trim($_POST['req_email2'])) : '';
  80. $password1 = random_pass(12);
  81. $password2 = $password1;
  82. }
  83. else
  84. {
  85. $password1 = isset($_POST['req_password1']) ? panther_trim($_POST['req_password1']) : '';
  86. $password2 = isset($_POST['req_password2']) ? panther_trim($_POST['req_password2']) : '';
  87. }
  88. // Validate username and passwords
  89. check_username($username);
  90. if (panther_strlen($password1) < 6)
  91. $errors[] = $lang_prof_reg['Pass too short'];
  92. else if ($password1 != $password2)
  93. $errors[] = $lang_prof_reg['Pass not match'];
  94. // Validate email
  95. require PANTHER_ROOT.'include/email.php';
  96. if (!$mailer->is_valid_email($email1))
  97. $errors[] = $lang_common['Invalid email'];
  98. else if ($panther_config['o_regs_verify'] == '1' && $email1 != $email2)
  99. $errors[] = $lang_register['Email not match'];
  100. // Check if it's a banned email address
  101. if ($mailer->is_banned_email($email1))
  102. {
  103. if ($panther_config['p_allow_banned_email'] == '0')
  104. $errors[] = $lang_prof_reg['Banned email'];
  105. $banned_email = true; // Used later when we send an alert email
  106. }
  107. else
  108. $banned_email = false;
  109. // Check if someone else already has registered with that email address
  110. $dupe_list = array();
  111. $data = array(
  112. ':email' => $email1
  113. );
  114. $ps = $db->select('users', 'username', $data, 'email=:email');
  115. if ($ps->rowCount())
  116. {
  117. if ($panther_config['p_allow_dupe_email'] == '0')
  118. $errors[] = $lang_prof_reg['Dupe email'];
  119. $ps->setFetchMode(PDO::FETCH_COLUMN, 0);
  120. foreach ($ps as $cur_dupe)
  121. $dupe_list[] = $cur_dupe;
  122. }
  123. // Make sure we have a valid language string
  124. if (isset($_POST['language']))
  125. {
  126. $language = preg_replace('%[\.\\\/]%', '', $_POST['language']);
  127. if (!file_exists(PANTHER_ROOT.'lang/'.$language.'/common.php'))
  128. message($lang_common['Bad request'], false, '404 Not Found');
  129. }
  130. else
  131. $language = $panther_config['o_default_lang'];
  132. $timezone = isset($_POST['timezone']) ? round($_POST['timezone'], 1) : '';
  133. $dst = isset($_POST['dst']) ? 1 : 0;
  134. $email_setting = isset($_POST['email_setting']) && ($_POST['email_setting'] > 0 && $_POST['email_setting'] < 2) ? intval($_POST['email_setting']) : $panther_config['o_default_email_setting'];
  135. ($hook = get_extensions('register_after_validation')) ? eval($hook) : null;
  136. $url_username = url_friendly($username);
  137. // Did everything go according to plan?
  138. if (empty($errors))
  139. {
  140. // Insert the new user into the database. We do this now to get the last inserted ID for later use
  141. $now = time();
  142. $initial_group_id = ($panther_config['o_regs_verify'] == '0') ? $panther_config['o_default_user_group'] : PANTHER_UNVERIFIED;
  143. $password_hash = panther_hash($password1.$password_salt);
  144. // Add the user
  145. $insert = array(
  146. 'username' => $username,
  147. 'group_id' => $initial_group_id,
  148. 'password' => $password_hash,
  149. 'salt' => $password_salt,
  150. 'email' => $email1,
  151. 'email_setting' => $email_setting,
  152. 'timezone' => $timezone,
  153. 'dst' => $dst,
  154. 'language' => $language,
  155. 'style' => $panther_config['o_default_style'],
  156. 'registered' => $now,
  157. 'registration_ip' => get_remote_address(),
  158. 'last_visit' => $now,
  159. );
  160. $db->insert('users', $insert);
  161. $new_uid = $db->lastInsertId($db->prefix.'users');
  162. $login_key = generate_login_key($new_uid);
  163. if ($panther_config['o_regs_verify'] == '0')
  164. {
  165. // Regenerate the users info cache
  166. if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
  167. require PANTHER_ROOT.'include/cache.php';
  168. generate_users_info_cache();
  169. }
  170. // If the mailing list isn't empty, we may need to send out some alerts
  171. if ($panther_config['o_mailing_list'] != '')
  172. {
  173. // If we previously found out that the email was banned
  174. if ($banned_email)
  175. {
  176. $info = array(
  177. 'message' => array(
  178. '<username>' => $username,
  179. '<email>' => $email1,
  180. '<profile_url>' => panther_link($panther_url['profile'], array($new_uid, $url_username)),
  181. )
  182. );
  183. $mail_tpl = $mailer->parse(PANTHER_ROOT.'lang/'.$panther_user['language'].'/mail_templates/banned_email_register.tpl', $info);
  184. $mailer->send($panther_config['o_mailing_list'], $mail_tpl['subject'], $mail_tpl['message']);
  185. }
  186. // If we previously found out that the email was a dupe
  187. if (!empty($dupe_list))
  188. {
  189. $info = array(
  190. 'message' => array(
  191. '<username>' => $username,
  192. '<dupe_list>' => implode(', ', $dupe_list),
  193. '<profile_url>' => panther_link($panther_url['profile'], array($new_uid, $url_username)),
  194. ),
  195. );
  196. $mail_tpl = $mailer->parse(PANTHER_ROOT.'lang/'.$panther_user['language'].'/mail_templates/dupe_email_register.tpl', $info);
  197. $mailer->send($panther_config['o_mailing_list'], $mail_tpl['subject'], $mail_tpl['message']);
  198. }
  199. // Should we alert people on the admin mailing list that a new user has registered?
  200. if ($panther_config['o_regs_report'] == '1')
  201. {
  202. $info = array(
  203. 'message' => array(
  204. '<username>' => $username,
  205. '<base_url>' => get_base_url(),
  206. '<profile_url>' => panther_link($panther_url['profile'], array($new_uid, $url_username)),
  207. '<admin_url>' => panther_link($panther_url['profile_admin'], array($new_uid)),
  208. ),
  209. );
  210. $mail_tpl = $mailer->parse(PANTHER_ROOT.'lang/'.$panther_user['language'].'/mail_templates/new_user.tpl', $info);
  211. $mailer->send($panther_config['o_mailing_list'], $mail_tpl['subject'], $mail_tpl['message']);
  212. }
  213. }
  214. // Must the user verify the registration or do we log him/her in right now?
  215. if ($panther_config['o_regs_verify'] == '1')
  216. {
  217. $info = array(
  218. 'subject' => array(
  219. '<board_title>' => $panther_config['o_board_title'],
  220. ),
  221. 'message' => array(
  222. '<base_url>' => get_base_url(),
  223. '<username>' => $username,
  224. '<password>' => $password1,
  225. '<login_url>' => panther_link($panther_url['login']),
  226. )
  227. );
  228. $mail_tpl = $mailer->parse(PANTHER_ROOT.'lang/'.$panther_user['language'].'/mail_templates/welcome.tpl', $info);
  229. $mailer->send($email1, $mail_tpl['subject'], $mail_tpl['message']);
  230. message(sprintf($lang_register['Reg email'], $panther_config['o_admin_email']), true);
  231. }
  232. panther_setcookie($new_uid, $login_key, time() + $panther_config['o_timeout_visit']);
  233. redirect(panther_link($panther_url['index']), $lang_register['Reg complete']);
  234. }
  235. }
  236. $page_title = array($panther_config['o_board_title'], $lang_register['Register']);
  237. $required_fields = array('req_user' => $lang_common['Username'], 'req_password1' => $lang_common['Password'], 'req_password2' => $lang_prof_reg['Confirm pass'], 'req_email1' => $lang_common['Email'], 'req_email2' => $lang_common['Email'].' 2');
  238. $focus_element = array('register', 'req_user');
  239. if (!empty($panther_robots))
  240. $required_fields['answer'] = $lang_common['Robot title'];
  241. ($hook = get_extensions('register_before_header')) ? eval($hook) : null;
  242. define('PANTHER_ACTIVE_PAGE', 'register');
  243. require PANTHER_ROOT.'header.php';
  244. $timezone = isset($timezone) ? $timezone : $panther_config['o_default_timezone'];
  245. $dst = isset($dst) ? $dst : $panther_config['o_default_dst'];
  246. $email_setting = isset($email_setting) ? $email_setting : $panther_config['o_default_email_setting'];
  247. ($hook = get_extensions('register_before_submit')) ? eval($hook) : null;
  248. $render = array(
  249. 'lang_register' => $lang_register,
  250. 'errors' => $errors,
  251. 'form_action' => panther_link($panther_url['register_register']),
  252. 'csrf_token' => generate_csrf_token(),
  253. 'lang_common' => $lang_common,
  254. 'lang_prof_reg' => $lang_prof_reg,
  255. 'POST' => $_POST,
  256. 'panther_config' => $panther_config,
  257. 'dst' => $dst,
  258. 'timezone' => $timezone,
  259. 'email_setting' => $email_setting,
  260. 'languages' => forum_list_langs(),
  261. );
  262. if (!empty($panther_robots))
  263. {
  264. $id = array_rand($panther_robots);
  265. $test = $panther_robots[$id];
  266. $render['robot_id'] = $id;
  267. $render['robot_test'] = $test;
  268. }
  269. $tpl = load_template('register.tpl');
  270. echo $tpl->render($render);
  271. ($hook = get_extensions('register_after_output')) ? eval($hook) : null;
  272. require PANTHER_ROOT.'footer.php';