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.

autoenable.class.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. # todo: Check strict equality gently
  3. class AutoEnable
  4. {
  5. // Constants for database values
  6. const APPROVED = 1;
  7. const DENIED = 2;
  8. const DISCARDED = 3;
  9. // Cache key to store the number of enable requests
  10. const CACHE_KEY_NAME = 'num_enable_requests';
  11. // The default request rejected message
  12. const REJECTED_MESSAGE = "Your request to re-enable your account has been rejected.<br /><br />This may be because a request is already pending for your username, or because a recent request was denied.<br /><br />You are encouraged to discuss this with staff by visiting %s on %s";
  13. // The default request received message
  14. const RECEIVED_MESSAGE = "Your request to re-enable your account has been received. Most requests are responded to within minutes. Remember to check your spam.<br /><br />If you do not receive an email after 48 hours have passed, please visit us on IRC for assistance.";
  15. /**
  16. * Handle a new enable request
  17. *
  18. * @param string $Username The user's username
  19. * @param string $Email The user's email address
  20. * @return string The output
  21. */
  22. public static function new_request($Username, $Email)
  23. {
  24. if (empty($Username)) {
  25. header("Location: login.php");
  26. die();
  27. }
  28. // Get the user's ID
  29. G::$DB->query("
  30. SELECT um.ID, ui.BanReason
  31. FROM users_main AS um
  32. JOIN users_info ui ON ui.UserID = um.ID
  33. WHERE um.Username = '$Username'
  34. AND um.Enabled = '2'");
  35. if (G::$DB->has_results()) {
  36. // Make sure the user can make another request
  37. list($UserID, $BanReason) = G::$DB->next_record();
  38. G::$DB->query("
  39. SELECT 1 FROM users_enable_requests
  40. WHERE UserID = '$UserID'
  41. AND (
  42. (
  43. Timestamp > NOW() - INTERVAL 1 WEEK
  44. AND HandledTimestamp IS NULL
  45. )
  46. OR
  47. (
  48. Timestamp > NOW() - INTERVAL 2 MONTH
  49. AND
  50. Outcome = '".self::DENIED."'
  51. )
  52. )");
  53. }
  54. $IP = $_SERVER['REMOTE_ADDR'];
  55. if (G::$DB->has_results() || !isset($UserID)) {
  56. // User already has/had a pending activation request or username is invalid
  57. $Output = sprintf(self::REJECTED_MESSAGE, BOT_DISABLED_CHAN, BOT_SERVER);
  58. if (isset($UserID)) {
  59. Tools::update_user_notes($UserID, sqltime() . " - Enable request rejected from $IP\n\n");
  60. }
  61. } else {
  62. // New disable activation request
  63. $UserAgent = db_string($_SERVER['HTTP_USER_AGENT']);
  64. G::$DB->query(
  65. "
  66. INSERT INTO users_enable_requests
  67. (UserID, Email, IP, UserAgent, Timestamp)
  68. VALUES (?, ?, ?, ?, NOW())",
  69. $UserID,
  70. Crypto::encrypt($Email),
  71. Crypto::encrypt($IP),
  72. $UserAgent
  73. );
  74. $RequestID = G::$DB->inserted_id();
  75. // Cache the number of requests for the modbar
  76. G::$Cache->increment_value(self::CACHE_KEY_NAME);
  77. setcookie('username', '', time() - 60 * 60, '/', '', false);
  78. $Output = self::RECEIVED_MESSAGE;
  79. Tools::update_user_notes($UserID, sqltime() . " - Enable request " . G::$DB->inserted_id() . " received from $IP\n\n");
  80. if ($BanReason == 3) {
  81. //self::handle_requests([$RequestID], self::APPROVED, "Automatically approved (inactivity)");
  82. }
  83. }
  84. return $Output;
  85. }
  86. /*
  87. * Handle requests
  88. *
  89. * @param int|int[] $IDs An array of IDs, or a single ID
  90. * @param int $Status The status to mark the requests as
  91. * @param string $Comment The staff member comment
  92. */
  93. public static function handle_requests($IDs, $Status, $Comment)
  94. {
  95. if ($Status != self::APPROVED && $Status != self::DENIED && $Status != self::DISCARDED) {
  96. error(404);
  97. }
  98. $UserInfo = [];
  99. $IDs = (!is_array($IDs)) ? [$IDs] : $IDs;
  100. if (count($IDs) == 0) {
  101. error(404);
  102. }
  103. foreach ($IDs as $ID) {
  104. if (!is_number($ID)) {
  105. error(404);
  106. }
  107. }
  108. G::$DB->query("SELECT Email, ID, UserID
  109. FROM users_enable_requests
  110. WHERE ID IN (".implode(',', $IDs).")
  111. AND Outcome IS NULL");
  112. $Results = G::$DB->to_array(false, MYSQLI_NUM);
  113. if ($Status != self::DISCARDED) {
  114. // Prepare email
  115. require_once(SERVER_ROOT . '/classes/templates.class.php');
  116. $TPL = new TEMPLATE;
  117. if ($Status == self::APPROVED) {
  118. $TPL->open(SERVER_ROOT . '/templates/enable_request_accepted.tpl');
  119. $TPL->set('SITE_DOMAIN', SITE_DOMAIN);
  120. } else {
  121. $TPL->open(SERVER_ROOT . '/templates/enable_request_denied.tpl');
  122. }
  123. $TPL->set('SITE_NAME', SITE_NAME);
  124. foreach ($Results as $Result) {
  125. list($Email, $ID, $UserID) = $Result;
  126. $Email = Crypto::decrypt($Email);
  127. $UserInfo[] = array($ID, $UserID);
  128. if ($Status == self::APPROVED) {
  129. // Generate token
  130. $Token = db_string(Users::make_secret());
  131. G::$DB->query("
  132. UPDATE users_enable_requests
  133. SET Token = ?
  134. WHERE ID = ?", $Token, $ID);
  135. $TPL->set('TOKEN', $Token);
  136. }
  137. // Send email
  138. $Subject = "Your enable request for " . SITE_NAME . " has been ";
  139. $Subject .= ($Status == self::APPROVED) ? 'approved' : 'denied';
  140. Misc::send_email($Email, $Subject, $TPL->get(), 'noreply');
  141. }
  142. } else {
  143. foreach ($Results as $Result) {
  144. list(, $ID, $UserID) = $Result;
  145. $UserInfo[] = array($ID, $UserID);
  146. }
  147. }
  148. // User notes stuff
  149. $StaffID = G::$LoggedUser['ID'] ?? 0;
  150. G::$DB->query("
  151. SELECT Username
  152. FROM users_main
  153. WHERE ID = ?", $StaffID);
  154. if (G::$DB->has_results()) {
  155. list($StaffUser) = G::$DB->next_record();
  156. } else {
  157. $StaffUser = "System";
  158. $StaffID = 0;
  159. }
  160. foreach ($UserInfo as $User) {
  161. list($ID, $UserID) = $User;
  162. $BaseComment = sqltime() . " - Enable request $ID " . strtolower(self::get_outcome_string($Status)) . ' by [user]'.$StaffUser.'[/user]';
  163. $BaseComment .= (!empty($Comment)) ? "\nReason: $Comment\n\n" : "\n\n";
  164. Tools::update_user_notes($UserID, $BaseComment);
  165. }
  166. // Update database values and decrement cache
  167. G::$DB->query("
  168. UPDATE users_enable_requests
  169. SET HandledTimestamp = NOW(),
  170. CheckedBy = ?,
  171. Outcome = ?
  172. WHERE ID IN (".implode(',', $IDs).")", $StaffID, $Status);
  173. G::$Cache->decrement_value(self::CACHE_KEY_NAME, count($IDs));
  174. }
  175. /**
  176. * Unresolve a discarded request
  177. *
  178. * @param int $ID The request ID
  179. */
  180. public static function unresolve_request($ID)
  181. {
  182. $ID = (int) $ID;
  183. if (empty($ID)) {
  184. error(404);
  185. }
  186. G::$DB->query("
  187. SELECT UserID
  188. FROM users_enable_requests
  189. WHERE Outcome = '" . self::DISCARDED . "'
  190. AND ID = '$ID'");
  191. if (!G::$DB->has_results()) {
  192. error(404);
  193. } else {
  194. list($UserID) = G::$DB->next_record();
  195. }
  196. G::$DB->query("
  197. SELECT Username
  198. FROM users_main
  199. WHERE ID = '" . G::$LoggedUser['ID'] . "'");
  200. list($StaffUser) = G::$DB->next_record();
  201. Tools::update_user_notes($UserID, sqltime() . " - Enable request $ID unresolved by [user]" . $StaffUser . '[/user]' . "\n\n");
  202. G::$DB->query("
  203. UPDATE users_enable_requests
  204. SET Outcome = NULL, HandledTimestamp = NULL, CheckedBy = NULL
  205. WHERE ID = '$ID'");
  206. G::$Cache->increment_value(self::CACHE_KEY_NAME);
  207. }
  208. /**
  209. * Get the corresponding outcome string for a numerical value
  210. *
  211. * @param int $Outcome The outcome integer
  212. * @return string The formatted output string
  213. */
  214. public static function get_outcome_string($Outcome)
  215. {
  216. if ($Outcome == self::APPROVED) {
  217. $String = "Approved";
  218. } elseif ($Outcome == self::DENIED) {
  219. $String = "Rejected";
  220. } elseif ($Outcome == self::DISCARDED) {
  221. $String = "Discarded";
  222. } else {
  223. $String = "---";
  224. }
  225. return $String;
  226. }
  227. /**
  228. * Handle a user's request to enable an account
  229. *
  230. * @param string $Token The token
  231. * @return string The error output, or an empty string
  232. */
  233. public static function handle_token($Token)
  234. {
  235. $Token = db_string($Token);
  236. G::$DB->query("
  237. SELECT uer.UserID, uer.HandledTimestamp, um.torrent_pass, um.Visible, um.IP
  238. FROM users_enable_requests AS uer
  239. LEFT JOIN users_main AS um ON uer.UserID = um.ID
  240. WHERE Token = '$Token'");
  241. if (G::$DB->has_results()) {
  242. list($UserID, $Timestamp, $TorrentPass, $Visible, $IP) = G::$DB->next_record();
  243. G::$DB->query("UPDATE users_enable_requests SET Token = NULL WHERE Token = '$Token'");
  244. if ($Timestamp < time_minus(3600 * 48)) {
  245. // Old request
  246. Tools::update_user_notes($UserID, sqltime() . " - Tried to use an expired enable token from ".$_SERVER['REMOTE_ADDR']."\n\n");
  247. $Err = "Token has expired. Please visit ".BOT_DISABLED_CHAN." on ".BOT_SERVER." to discuss this with staff.";
  248. } else {
  249. // Good request, decrement cache value and enable account
  250. G::$Cache->decrement_value(AutoEnable::CACHE_KEY_NAME);
  251. $VisibleTrIP = ($Visible && Crypto::decrypt($IP) != '127.0.0.1') ? '1' : '0';
  252. Tracker::update_tracker('add_user', array('id' => $UserID, 'passkey' => $TorrentPass, 'visible' => $VisibleTrIP));
  253. G::$DB->query("UPDATE users_main SET Enabled = '1', can_leech = '1' WHERE ID = '$UserID'");
  254. G::$DB->query("UPDATE users_info SET BanReason = '0' WHERE UserID = '$UserID'");
  255. G::$Cache->delete_value('user_info_'.$UserID);
  256. $Err = "Your account has been enabled. You may now log in.";
  257. }
  258. } else {
  259. $Err = "Invalid token.";
  260. }
  261. return $Err;
  262. }
  263. /**
  264. * Build the search query, from the searchbox inputs
  265. *
  266. * @param int $UserID The user ID
  267. * @param string $IP The IP
  268. * @param string $SubmittedTimestamp The timestamp representing when the request was submitted
  269. * @param int $HandledUserID The ID of the user that handled the request
  270. * @param string $HandledTimestamp The timestamp representing when the request was handled
  271. * @param int $OutcomeSearch The outcome of the request
  272. * @param boolean $Checked Should checked requests be included?
  273. * @return array The WHERE conditions for the query
  274. */
  275. public static function build_search_query($Username, $IP, $SubmittedBetween, $SubmittedTimestamp1, $SubmittedTimestamp2, $HandledUsername, $HandledBetween, $HandledTimestamp1, $HandledTimestamp2, $OutcomeSearch, $Checked)
  276. {
  277. $Where = [];
  278. if (!empty($Username)) {
  279. $Where[] = "um1.Username = '$Username'";
  280. }
  281. if (!empty($IP)) {
  282. // todo: Make this work with encrypted IPs
  283. $Where[] = "uer.IP = '$IP'";
  284. }
  285. if (!empty($SubmittedTimestamp1)) {
  286. switch ($SubmittedBetween) {
  287. case 'on':
  288. $Where[] = "DATE(uer.Timestamp) = DATE('$SubmittedTimestamp1')";
  289. break;
  290. case 'before':
  291. $Where[] = "DATE(uer.Timestamp) < DATE('$SubmittedTimestamp1')";
  292. break;
  293. case 'after':
  294. $Where[] = "DATE(uer.Timestamp) > DATE('$SubmittedTimestamp1')";
  295. break;
  296. case 'between':
  297. if (!empty($SubmittedTimestamp2)) {
  298. $Where[] = "DATE(uer.Timestamp) BETWEEN DATE('$SubmittedTimestamp1') AND DATE('$SubmittedTimestamp2')";
  299. }
  300. break;
  301. default:
  302. break;
  303. }
  304. }
  305. if (!empty($HandledTimestamp1)) {
  306. switch ($HandledBetween) {
  307. case 'on':
  308. $Where[] = "DATE(uer.HandledTimestamp) = DATE('$HandledTimestamp1')";
  309. break;
  310. case 'before':
  311. $Where[] = "DATE(uer.HandledTimestamp) < DATE('$HandledTimestamp1')";
  312. break;
  313. case 'after':
  314. $Where[] = "DATE(uer.HandledTimestamp) > DATE('$HandledTimestamp1')";
  315. break;
  316. case 'between':
  317. if (!empty($HandledTimestamp2)) {
  318. $Where[] = "DATE(uer.HandledTimestamp) BETWEEN DATE('$HandledTimestamp1') AND DATE('$HandledTimestamp2')";
  319. }
  320. break;
  321. default:
  322. break;
  323. }
  324. }
  325. if (!empty($HandledUsername)) {
  326. $Where[] = "um2.Username = '$HandledUsername'";
  327. }
  328. if (!empty($OutcomeSearch)) {
  329. $Where[] = "uer.Outcome = '$OutcomeSearch'";
  330. }
  331. if ($Checked) {
  332. // This is to skip the if statement in enable_requests.php
  333. $Where[] = "(uer.Outcome IS NULL OR uer.Outcome IS NOT NULL)";
  334. }
  335. return $Where;
  336. }
  337. }