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

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