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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. $IPcc = Tools::geoip($_SERVER['REMOTE_ADDR']);
  95. $DB->query("
  96. INSERT INTO users_main
  97. (Username, Email, PassHash, torrent_pass, IP, PermissionID, Enabled, Invites, FLTokens, Uploaded, ipcc)
  98. VALUES
  99. ('".db_string(trim($_POST['username']))."',
  100. '".Crypto::encrypt($_POST['email'])."',
  101. '".db_string(Users::make_sec_hash($_POST['password']))."',
  102. '".db_string($torrent_pass)."',
  103. '".Crypto::encrypt($_SERVER['REMOTE_ADDR'])."',
  104. '$Class',
  105. '$Enabled',
  106. '".$ENV->STARTING_INVITES."',
  107. '".$ENV->STARTING_TOKENS."',
  108. '".$ENV->STARTING_UPLOAD."',
  109. '$IPcc')
  110. ");
  111. $UserID = $DB->inserted_id();
  112. // 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
  113. $DB->query("
  114. DELETE FROM invites
  115. WHERE InviteKey = '".db_string($_REQUEST['invite'])."'");
  116. // Award invite badge to inviter if they don't have it
  117. /*
  118. if (Badges::award_badge($InviterID, 136)) {
  119. 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.");
  120. $Cache->delete_value('user_badges_'.$InviterID);
  121. }
  122. */
  123. $DB->query("
  124. SELECT ID
  125. FROM stylesheets
  126. WHERE `Default` = '1'");
  127. list($StyleID) = $DB->next_record();
  128. $AuthKey = Users::make_secret();
  129. if ($InviteReason !== '') {
  130. $InviteReason = db_string(sqltime()." - $InviteReason");
  131. }
  132. $DB->query("
  133. INSERT INTO users_info
  134. (UserID, StyleID, AuthKey, Inviter, JoinDate, AdminComment)
  135. VALUES
  136. ('$UserID', '$StyleID', '".db_string($AuthKey)."', '$InviterID', NOW(), '$InviteReason')");
  137. $DB->query("
  138. INSERT INTO users_history_ips
  139. (UserID, IP, StartTime)
  140. VALUES
  141. ('$UserID', '".Crypto::encrypt($_SERVER['REMOTE_ADDR'])."', NOW())");
  142. $DB->query("
  143. INSERT INTO users_notifications_settings
  144. (UserID)
  145. VALUES
  146. ('$UserID')");
  147. $DB->query("
  148. INSERT INTO users_history_emails
  149. (UserID, Email, Time, IP)
  150. VALUES
  151. ('$UserID', '".Crypto::encrypt($_REQUEST['email'])."', NULL, '".Crypto::encrypt($_SERVER['REMOTE_ADDR'])."')");
  152. if ($_REQUEST['email'] != $InviteEmail) {
  153. $DB->query("
  154. INSERT INTO users_history_emails
  155. (UserID, Email, Time, IP)
  156. VALUES
  157. ('$UserID', '".Crypto::encrypt($InviteEmail)."', NOW(), '".Crypto::encrypt($_SERVER['REMOTE_ADDR'])."')");
  158. }
  159. // Manage invite trees, delete invite
  160. if ($InviterID !== null && $InviterID !== 0) {
  161. $DB->query("
  162. SELECT TreePosition, TreeID, TreeLevel
  163. FROM invite_tree
  164. WHERE UserID = '$InviterID'");
  165. list($InviterTreePosition, $TreeID, $TreeLevel) = $DB->next_record();
  166. // If the inviter doesn't have an invite tree
  167. // Note: This should never happen unless you've transferred from another database, like What.CD did
  168. if (!$DB->has_results()) {
  169. $DB->query("
  170. SELECT MAX(TreeID) + 1
  171. FROM invite_tree");
  172. list($TreeID) = $DB->next_record();
  173. $DB->query("
  174. INSERT INTO invite_tree
  175. (UserID, InviterID, TreePosition, TreeID, TreeLevel)
  176. VALUES ('$InviterID', '0', '1', '$TreeID', '1')");
  177. $TreePosition = 2;
  178. $TreeLevel = 2;
  179. } else {
  180. $DB->query("
  181. SELECT TreePosition
  182. FROM invite_tree
  183. WHERE TreePosition > '$InviterTreePosition'
  184. AND TreeLevel <= '$TreeLevel'
  185. AND TreeID = '$TreeID'
  186. ORDER BY TreePosition
  187. LIMIT 1");
  188. list($TreePosition) = $DB->next_record();
  189. if ($TreePosition) {
  190. $DB->query("
  191. UPDATE invite_tree
  192. SET TreePosition = TreePosition + 1
  193. WHERE TreeID = '$TreeID'
  194. AND TreePosition >= '$TreePosition'");
  195. } else {
  196. $DB->query("
  197. SELECT TreePosition + 1
  198. FROM invite_tree
  199. WHERE TreeID = '$TreeID'
  200. ORDER BY TreePosition DESC
  201. LIMIT 1");
  202. list($TreePosition) = $DB->next_record();
  203. }
  204. $TreeLevel++;
  205. // Create invite tree record
  206. $DB->query("
  207. INSERT INTO invite_tree
  208. (UserID, InviterID, TreePosition, TreeID, TreeLevel)
  209. VALUES
  210. ('$UserID', '$InviterID', '$TreePosition', '$TreeID', '$TreeLevel')");
  211. }
  212. } else { // No inviter (open registration)
  213. $DB->query("
  214. SELECT MAX(TreeID)
  215. FROM invite_tree");
  216. list($TreeID) = $DB->next_record();
  217. $TreeID++;
  218. $InviterID = 0;
  219. $TreePosition = 1;
  220. $TreeLevel = 1;
  221. }
  222. include(SERVER_ROOT.'/classes/templates.class.php');
  223. $TPL = new TEMPLATE;
  224. $TPL->open(SERVER_ROOT.'/templates/new_registration.tpl');
  225. $TPL->set('Username', $_REQUEST['username']);
  226. $TPL->set('TorrentKey', $torrent_pass);
  227. $TPL->set('SITE_NAME', $ENV->SITE_NAME);
  228. $TPL->set('SITE_DOMAIN', SITE_DOMAIN);
  229. Misc::send_email($_REQUEST['email'], "New account confirmation at $ENV->SITE_NAME", $TPL->get(), 'noreply');
  230. Tracker::update_tracker('add_user', array('id' => $UserID, 'passkey' => $torrent_pass));
  231. $Sent = 1;
  232. }
  233. } elseif ($_GET['invite']) {
  234. // If they haven't submitted the form, check to see if their invite is good
  235. $DB->query("
  236. SELECT InviteKey
  237. FROM invites
  238. WHERE InviteKey = '".db_string($_GET['invite'])."'");
  239. if (!$DB->has_results()) {
  240. error('Invite not found!');
  241. }
  242. }
  243. include('step1.php');
  244. } elseif (!$ENV->OPEN_REGISTRATION) {
  245. if (isset($_GET['welcome'])) {
  246. include('code.php');
  247. } else {
  248. include('closed.php');
  249. }
  250. }