Oppaitime'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

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