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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 didn't enter a valid username.", array('regex' => USERNAME_REGEX));
  33. $Val->SetFields('email', true, 'email', "You didn't enter a valid email address.");
  34. $Val->SetFields('password', true, 'regex', "Your password was too short.", array('regex'=>'/(?=^.{6,}$).*$/'));
  35. $Val->SetFields('confirm_password', true, 'compare', "Your passwords don't match.", array('comparefield' => 'password'));
  36. $Val->SetFields('readrules', true, 'checkbox', "You didn't agree to read the rules and wiki.");
  37. $Val->SetFields('readwiki', true, 'checkbox', "You didn't provide consent to the privacy policy.");
  38. $Val->SetFields('agereq', true, 'checkbox', "You didn't confirm that you're of legal age.");
  39. if (!apcu_exists('DBKEY')) {
  40. $Err = "Registration temporarily disabled due to degraded database access (security measure).";
  41. }
  42. if (!empty($_POST['submit'])) {
  43. // User has submitted registration form
  44. $Err = $Val->ValidateForm($_REQUEST);
  45. if (!$Err) {
  46. // Don't allow a username of "0" or "1" due to PHP's type juggling
  47. if (trim($_POST['username']) === '0' || trim($_POST['username']) === '1') {
  48. $Err = "You can't have a username of 0 or 1.";
  49. }
  50. $DB->query("
  51. SELECT COUNT(ID)
  52. FROM users_main
  53. WHERE Username LIKE '".db_string(trim($_POST['username']))."'");
  54. list($UserCount) = $DB->next_record();
  55. if ($UserCount) {
  56. $Err = "There's already someone registered with that username.";
  57. $_REQUEST['username'] = '';
  58. }
  59. if ($_REQUEST['invite']) {
  60. $DB->query("
  61. SELECT InviterID, Email, Reason
  62. FROM invites
  63. WHERE InviteKey = '".db_string($_REQUEST['invite'])."'");
  64. if (!$DB->has_results()) {
  65. $Err = "The invite code is invalid.";
  66. $InviterID = 0;
  67. } else {
  68. list($InviterID, $InviteEmail, $InviteReason) = $DB->next_record(MYSQLI_NUM, false);
  69. $InviteEmail = Crypto::decrypt($InviteEmail);
  70. }
  71. } else {
  72. $InviterID = 0;
  73. $InviteEmail = $_REQUEST['email'];
  74. $InviteReason = '';
  75. }
  76. }
  77. if (!$Err) {
  78. $torrent_pass = Users::make_secret();
  79. // Previously SELECT COUNT(ID) FROM users_main, which is a lot slower
  80. $DB->query("
  81. SELECT ID
  82. FROM users_main
  83. LIMIT 1");
  84. $UserCount = $DB->record_count();
  85. if ($UserCount === 0) {
  86. $NewInstall = true;
  87. $Class = SYSOP;
  88. $Enabled = '1';
  89. } else {
  90. $NewInstall = false;
  91. $Class = USER;
  92. $Enabled = '0';
  93. }
  94. $DB->query("
  95. INSERT INTO users_main
  96. (Username, Email, PassHash, torrent_pass, IP, PermissionID, Enabled, Invites, FLTokens, Uploaded)
  97. VALUES
  98. ('".db_string(trim($_POST['username']))."',
  99. '".Crypto::encrypt($_POST['email'])."',
  100. '".db_string(Users::make_sec_hash($_POST['password']))."',
  101. '".db_string($torrent_pass)."',
  102. '".Crypto::encrypt($_SERVER['REMOTE_ADDR'])."',
  103. '$Class',
  104. '$Enabled',
  105. '".$ENV->STARTING_INVITES."',
  106. '".$ENV->STARTING_TOKENS."',
  107. '".$ENV->STARTING_UPLOAD."')
  108. ");
  109. $UserID = $DB->inserted_id();
  110. // 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
  111. $DB->query("
  112. DELETE FROM invites
  113. WHERE InviteKey = '".db_string($_REQUEST['invite'])."'");
  114. // Award invite badge to inviter if they don't have it
  115. /*
  116. if (Badges::award_badge($InviterID, 136)) {
  117. 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.");
  118. $Cache->delete_value('user_badges_'.$InviterID);
  119. }
  120. */
  121. $DB->query("
  122. SELECT ID
  123. FROM stylesheets
  124. WHERE `Default` = '1'");
  125. list($StyleID) = $DB->next_record();
  126. $AuthKey = Users::make_secret();
  127. if ($InviteReason !== '') {
  128. $InviteReason = db_string(sqltime()." - $InviteReason");
  129. }
  130. $DB->query("
  131. INSERT INTO users_info
  132. (UserID, StyleID, AuthKey, Inviter, JoinDate, AdminComment)
  133. VALUES
  134. ('$UserID', '$StyleID', '".db_string($AuthKey)."', '$InviterID', NOW(), '$InviteReason')");
  135. $DB->query("
  136. INSERT INTO users_history_ips
  137. (UserID, IP, StartTime)
  138. VALUES
  139. ('$UserID', '".Crypto::encrypt($_SERVER['REMOTE_ADDR'])."', NOW())");
  140. $DB->query("
  141. INSERT INTO users_notifications_settings
  142. (UserID)
  143. VALUES
  144. ('$UserID')");
  145. $DB->query("
  146. INSERT INTO users_history_emails
  147. (UserID, Email, Time, IP)
  148. VALUES
  149. ('$UserID', '".Crypto::encrypt($_REQUEST['email'])."', NULL, '".Crypto::encrypt($_SERVER['REMOTE_ADDR'])."')");
  150. if ($_REQUEST['email'] != $InviteEmail) {
  151. $DB->query("
  152. INSERT INTO users_history_emails
  153. (UserID, Email, Time, IP)
  154. VALUES
  155. ('$UserID', '".Crypto::encrypt($InviteEmail)."', NOW(), '".Crypto::encrypt($_SERVER['REMOTE_ADDR'])."')");
  156. }
  157. // Manage invite trees, delete invite
  158. if ($InviterID !== null && $InviterID !== 0) {
  159. $DB->query("
  160. SELECT TreePosition, TreeID, TreeLevel
  161. FROM invite_tree
  162. WHERE UserID = '$InviterID'");
  163. list($InviterTreePosition, $TreeID, $TreeLevel) = $DB->next_record();
  164. // If the inviter doesn't have an invite tree
  165. // Note: This should never happen unless you've transferred from another database, like What.CD did
  166. if (!$DB->has_results()) {
  167. $DB->query("
  168. SELECT MAX(TreeID) + 1
  169. FROM invite_tree");
  170. list($TreeID) = $DB->next_record();
  171. $DB->query("
  172. INSERT INTO invite_tree
  173. (UserID, InviterID, TreePosition, TreeID, TreeLevel)
  174. VALUES ('$InviterID', '0', '1', '$TreeID', '1')");
  175. $TreePosition = 2;
  176. $TreeLevel = 2;
  177. } else {
  178. $DB->query("
  179. SELECT TreePosition
  180. FROM invite_tree
  181. WHERE TreePosition > '$InviterTreePosition'
  182. AND TreeLevel <= '$TreeLevel'
  183. AND TreeID = '$TreeID'
  184. ORDER BY TreePosition
  185. LIMIT 1");
  186. list($TreePosition) = $DB->next_record();
  187. if ($TreePosition) {
  188. $DB->query("
  189. UPDATE invite_tree
  190. SET TreePosition = TreePosition + 1
  191. WHERE TreeID = '$TreeID'
  192. AND TreePosition >= '$TreePosition'");
  193. } else {
  194. $DB->query("
  195. SELECT TreePosition + 1
  196. FROM invite_tree
  197. WHERE TreeID = '$TreeID'
  198. ORDER BY TreePosition DESC
  199. LIMIT 1");
  200. list($TreePosition) = $DB->next_record();
  201. }
  202. $TreeLevel++;
  203. // Create invite tree record
  204. $DB->query("
  205. INSERT INTO invite_tree
  206. (UserID, InviterID, TreePosition, TreeID, TreeLevel)
  207. VALUES
  208. ('$UserID', '$InviterID', '$TreePosition', '$TreeID', '$TreeLevel')");
  209. }
  210. } else { // No inviter (open registration)
  211. $DB->query("
  212. SELECT MAX(TreeID)
  213. FROM invite_tree");
  214. list($TreeID) = $DB->next_record();
  215. $TreeID++;
  216. $InviterID = 0;
  217. $TreePosition = 1;
  218. $TreeLevel = 1;
  219. }
  220. include(SERVER_ROOT.'/classes/templates.class.php');
  221. $TPL = new TEMPLATE;
  222. $TPL->open(SERVER_ROOT.'/templates/new_registration.tpl');
  223. $TPL->set('Username', $_REQUEST['username']);
  224. $TPL->set('TorrentKey', $torrent_pass);
  225. $TPL->set('SITE_NAME', $ENV->SITE_NAME);
  226. $TPL->set('SITE_DOMAIN', SITE_DOMAIN);
  227. Misc::send_email($_REQUEST['email'], "New account confirmation at $ENV->SITE_NAME", $TPL->get(), 'noreply');
  228. Tracker::update_tracker('add_user', array('id' => $UserID, 'passkey' => $torrent_pass));
  229. $Sent = 1;
  230. }
  231. } elseif ($_GET['invite']) {
  232. // If they haven't submitted the form, check to see if their invite is good
  233. $DB->query("
  234. SELECT InviteKey
  235. FROM invites
  236. WHERE InviteKey = '".db_string($_GET['invite'])."'");
  237. if (!$DB->has_results()) {
  238. error('Invite not found!');
  239. }
  240. }
  241. include('step1.php');
  242. } elseif (!$ENV->OPEN_REGISTRATION) {
  243. if (isset($_GET['welcome'])) {
  244. include('code.php');
  245. } else {
  246. include('closed.php');
  247. }
  248. }