BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. declare(strict_types=1);
  3. # Unsure if require_once is needed here
  4. require_once 'classes/env.class.php';
  5. $ENV = ENV::go();
  6. /*
  7. if (isset($LoggedUser)) {
  8. // Silly user, what are you doing here!
  9. header('Location: index.php');
  10. error();
  11. }
  12. */
  13. include SERVER_ROOT.'/classes/validate.class.php';
  14. $Val = new Validate;
  15. if (!empty($_REQUEST['confirm'])) {
  16. // Confirm registration
  17. $DB->query("
  18. SELECT ID
  19. FROM users_main
  20. WHERE torrent_pass = '".db_string($_REQUEST['confirm'])."'
  21. AND Enabled = '0'");
  22. list($UserID) = $DB->next_record();
  23. if ($UserID) {
  24. $DB->query("
  25. UPDATE users_main
  26. SET Enabled = '1'
  27. WHERE ID = '$UserID'");
  28. $Cache->increment('stats_user_count');
  29. include('step2.php');
  30. }
  31. } elseif ($ENV->OPEN_REGISTRATION || !empty($_REQUEST['invite'])) {
  32. $Val->SetFields('username', true, 'regex', 'You did not enter a valid username.', array('regex' => USERNAME_REGEX));
  33. $Val->SetFields('email', true, 'email', 'You did not enter a valid email address.');
  34. $Val->SetFields('password', true, 'regex', 'Your password must be at least 6 characters long.', array('regex'=>'/(?=^.{6,}$).*$/'));
  35. $Val->SetFields('confirm_password', true, 'compare', 'Your passwords do not match.', array('comparefield' => 'password'));
  36. $Val->SetFields('readrules', true, 'checkbox', 'You did not select the box that says you will read the rules.');
  37. $Val->SetFields('readwiki', true, 'checkbox', 'You did not select the box that says you will read the wiki.');
  38. $Val->SetFields('agereq', true, 'checkbox', 'You did not select the box that says you are 18 years of age or older.');
  39. //$Val->SetFields('captcha', true, 'string', 'You did not enter a captcha code.', array('minlength' => 6, 'maxlength' => 6));
  40. if (!apcu_exists('DBKEY')) {
  41. $Err = "Registration temporarily disabled due to degraded database access (security measure).";
  42. }
  43. if (!empty($_POST['submit'])) {
  44. // User has submitted registration form
  45. $Err = $Val->ValidateForm($_REQUEST);
  46. /*
  47. if (!$Err && strtolower($_SESSION['captcha']) !== strtolower($_REQUEST['captcha'])) {
  48. $Err = 'You did not enter the correct captcha code.';
  49. }
  50. */
  51. if (!$Err) {
  52. // Don't allow a username of "0" or "1" due to PHP's type juggling
  53. if (trim($_POST['username']) === '0' || trim($_POST['username']) === '1') {
  54. $Err = 'You cannot have a username of "0" or "1."';
  55. }
  56. $DB->query("
  57. SELECT COUNT(ID)
  58. FROM users_main
  59. WHERE Username LIKE '".db_string(trim($_POST['username']))."'");
  60. list($UserCount) = $DB->next_record();
  61. if ($UserCount) {
  62. $Err = 'There is already someone registered with that username.';
  63. $_REQUEST['username'] = '';
  64. }
  65. if ($_REQUEST['invite']) {
  66. $DB->query("
  67. SELECT InviterID, Email, Reason
  68. FROM invites
  69. WHERE InviteKey = '".db_string($_REQUEST['invite'])."'");
  70. if (!$DB->has_results()) {
  71. $Err = 'Invite does not exist.';
  72. $InviterID = 0;
  73. } else {
  74. list($InviterID, $InviteEmail, $InviteReason) = $DB->next_record(MYSQLI_NUM, false);
  75. $InviteEmail = Crypto::decrypt($InviteEmail);
  76. }
  77. } else {
  78. $InviterID = 0;
  79. $InviteEmail = $_REQUEST['email'];
  80. $InviteReason = '';
  81. }
  82. }
  83. if (!$Err) {
  84. $torrent_pass = Users::make_secret();
  85. // Previously SELECT COUNT(ID) FROM users_main, which is a lot slower
  86. $DB->query("
  87. SELECT ID
  88. FROM users_main
  89. LIMIT 1");
  90. $UserCount = $DB->record_count();
  91. if ($UserCount === 0) {
  92. $NewInstall = true;
  93. $Class = SYSOP;
  94. $Enabled = '1';
  95. } else {
  96. $NewInstall = false;
  97. $Class = USER;
  98. $Enabled = '0';
  99. }
  100. $IPcc = Tools::geoip($_SERVER['REMOTE_ADDR']);
  101. $DB->query("
  102. INSERT INTO users_main
  103. (Username, Email, PassHash, torrent_pass, IP, PermissionID, Enabled, Invites, FLTokens, Uploaded, ipcc)
  104. VALUES
  105. ('".db_string(trim($_POST['username']))."',
  106. '".Crypto::encrypt($_POST['email'])."',
  107. '".db_string(Users::make_sec_hash($_POST['password']))."',
  108. '".db_string($torrent_pass)."',
  109. '".Crypto::encrypt($_SERVER['REMOTE_ADDR'])."',
  110. '$Class',
  111. '$Enabled',
  112. '".$ENV->STARTING_INVITES."',
  113. '".$ENV->STARTING_TOKENS."',
  114. '".$ENV->STARTING_UPLOAD."',
  115. '$IPcc')
  116. ");
  117. $UserID = $DB->inserted_id();
  118. // User created, delete invite. If things break after this point, then it's better to have a broken account to fix than a 'free' invite floating around that can be reused
  119. $DB->query("
  120. DELETE FROM invites
  121. WHERE InviteKey = '".db_string($_REQUEST['invite'])."'");
  122. // Award invite badge to inviter if they don't have it
  123. /*
  124. if (Badges::award_badge($InviterID, 136)) {
  125. Misc::send_pm($InviterID, 0, 'You have received a badge!', "You have received a badge for inviting a user to the site.\n\nIt can be enabled from your user settings.");
  126. $Cache->delete_value('user_badges_'.$InviterID);
  127. }
  128. */
  129. $DB->query("
  130. SELECT ID
  131. FROM stylesheets
  132. WHERE `Default` = '1'");
  133. list($StyleID) = $DB->next_record();
  134. $AuthKey = Users::make_secret();
  135. if ($InviteReason !== '') {
  136. $InviteReason = db_string(sqltime()." - $InviteReason");
  137. }
  138. $DB->query("
  139. INSERT INTO users_info
  140. (UserID, StyleID, AuthKey, Inviter, JoinDate, AdminComment)
  141. VALUES
  142. ('$UserID', '$StyleID', '".db_string($AuthKey)."', '$InviterID', NOW(), '$InviteReason')");
  143. $DB->query("
  144. INSERT INTO users_history_ips
  145. (UserID, IP, StartTime)
  146. VALUES
  147. ('$UserID', '".Crypto::encrypt($_SERVER['REMOTE_ADDR'])."', NOW())");
  148. $DB->query("
  149. INSERT INTO users_notifications_settings
  150. (UserID)
  151. VALUES
  152. ('$UserID')");
  153. $DB->query("
  154. INSERT INTO users_history_emails
  155. (UserID, Email, Time, IP)
  156. VALUES
  157. ('$UserID', '".Crypto::encrypt($_REQUEST['email'])."', NULL, '".Crypto::encrypt($_SERVER['REMOTE_ADDR'])."')");
  158. if ($_REQUEST['email'] != $InviteEmail) {
  159. $DB->query("
  160. INSERT INTO users_history_emails
  161. (UserID, Email, Time, IP)
  162. VALUES
  163. ('$UserID', '".Crypto::encrypt($InviteEmail)."', NOW(), '".Crypto::encrypt($_SERVER['REMOTE_ADDR'])."')");
  164. }
  165. // Manage invite trees, delete invite
  166. if ($InviterID !== null && $InviterID !== 0) {
  167. $DB->query("
  168. SELECT TreePosition, TreeID, TreeLevel
  169. FROM invite_tree
  170. WHERE UserID = '$InviterID'");
  171. list($InviterTreePosition, $TreeID, $TreeLevel) = $DB->next_record();
  172. // If the inviter doesn't have an invite tree
  173. // Note: This should never happen unless you've transferred from another database, like What.CD did
  174. if (!$DB->has_results()) {
  175. $DB->query("
  176. SELECT MAX(TreeID) + 1
  177. FROM invite_tree");
  178. list($TreeID) = $DB->next_record();
  179. $DB->query("
  180. INSERT INTO invite_tree
  181. (UserID, InviterID, TreePosition, TreeID, TreeLevel)
  182. VALUES ('$InviterID', '0', '1', '$TreeID', '1')");
  183. $TreePosition = 2;
  184. $TreeLevel = 2;
  185. } else {
  186. $DB->query("
  187. SELECT TreePosition
  188. FROM invite_tree
  189. WHERE TreePosition > '$InviterTreePosition'
  190. AND TreeLevel <= '$TreeLevel'
  191. AND TreeID = '$TreeID'
  192. ORDER BY TreePosition
  193. LIMIT 1");
  194. list($TreePosition) = $DB->next_record();
  195. if ($TreePosition) {
  196. $DB->query("
  197. UPDATE invite_tree
  198. SET TreePosition = TreePosition + 1
  199. WHERE TreeID = '$TreeID'
  200. AND TreePosition >= '$TreePosition'");
  201. } else {
  202. $DB->query("
  203. SELECT TreePosition + 1
  204. FROM invite_tree
  205. WHERE TreeID = '$TreeID'
  206. ORDER BY TreePosition DESC
  207. LIMIT 1");
  208. list($TreePosition) = $DB->next_record();
  209. }
  210. $TreeLevel++;
  211. // Create invite tree record
  212. $DB->query("
  213. INSERT INTO invite_tree
  214. (UserID, InviterID, TreePosition, TreeID, TreeLevel)
  215. VALUES
  216. ('$UserID', '$InviterID', '$TreePosition', '$TreeID', '$TreeLevel')");
  217. }
  218. } else { // No inviter (open registration)
  219. $DB->query("
  220. SELECT MAX(TreeID)
  221. FROM invite_tree");
  222. list($TreeID) = $DB->next_record();
  223. $TreeID++;
  224. $InviterID = 0;
  225. $TreePosition = 1;
  226. $TreeLevel = 1;
  227. }
  228. include(SERVER_ROOT.'/classes/templates.class.php');
  229. $TPL = new TEMPLATE;
  230. $TPL->open(SERVER_ROOT.'/templates/new_registration.tpl');
  231. $TPL->set('Username', $_REQUEST['username']);
  232. $TPL->set('TorrentKey', $torrent_pass);
  233. $TPL->set('SITE_NAME', $ENV->SITE_NAME);
  234. $TPL->set('SITE_DOMAIN', SITE_DOMAIN);
  235. Misc::send_email($_REQUEST['email'], "New account confirmation at $ENV->SITE_NAME", $TPL->get(), 'noreply');
  236. Tracker::update_tracker('add_user', array('id' => $UserID, 'passkey' => $torrent_pass));
  237. $Sent = 1;
  238. }
  239. } elseif ($_GET['invite']) {
  240. // If they haven't submitted the form, check to see if their invite is good
  241. $DB->query("
  242. SELECT InviteKey
  243. FROM invites
  244. WHERE InviteKey = '".db_string($_GET['invite'])."'");
  245. if (!$DB->has_results()) {
  246. error('Invite not found!');
  247. }
  248. }
  249. include('step1.php');
  250. } elseif (!$ENV->OPEN_REGISTRATION) {
  251. if (isset($_GET['welcome'])) {
  252. include('code.php');
  253. } else {
  254. include('closed.php');
  255. }
  256. }