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.

take_edit.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?
  2. authorize();
  3. $UserID = $_REQUEST['userid'];
  4. if (!is_number($UserID)) {
  5. error(404);
  6. }
  7. //For this entire page, we should generally be using $UserID not $LoggedUser['ID'] and $U[] not $LoggedUser[]
  8. $U = Users::user_info($UserID);
  9. if (!$U) {
  10. error(404);
  11. }
  12. $Permissions = Permissions::get_permissions($U['PermissionID']);
  13. if ($UserID != $LoggedUser['ID'] && !check_perms('users_edit_profiles', $Permissions['Class'])) {
  14. send_irc('PRIVMSG '.ADMIN_CHAN.' :User '.$LoggedUser['Username'].' ('.site_url().'user.php?id='.$LoggedUser['ID'].') just tried to edit the profile of '.site_url().'user.php?id='.$_REQUEST['userid']);
  15. error(403);
  16. }
  17. $Val->SetFields('stylesheet', 1, "number", "You forgot to select a stylesheet.");
  18. $Val->SetFields('styleurl', 0, "regex", "You did not enter a valid stylesheet URL.", ['regex' => '/^'.CSS_REGEX.'$/i']);
  19. // The next two are commented out because the drop-down menus were replaced with a check box and radio buttons
  20. //$Val->SetFields('disablegrouping', 0, "number", "You forgot to select your torrent grouping option.");
  21. //$Val->SetFields('torrentgrouping', 0, "number", "You forgot to select your torrent grouping option.");
  22. $Val->SetFields('postsperpage', 1, "number", "You forgot to select your posts per page option.", ['inarray' => [25, 50, 100]]);
  23. //$Val->SetFields('hidecollage', 1, "number", "You forgot to select your collage option.", ['minlength' => 0, 'maxlength' => 1]);
  24. $Val->SetFields('collagecovers', 1, "number", "You forgot to select your collage option.");
  25. $Val->SetFields('avatar', 0, "regex", "You did not enter a valid avatar URL.", ['regex' => "/^".IMAGE_REGEX."$/i"]);
  26. $Val->SetFields('email', 1, "email", "You did not enter a valid email address.");
  27. $Val->SetFields('twofa', 0, "regex", "You did not enter a valid 2FA verification code.", ['regex' => '/^[0-9]{6}$/']);
  28. $Val->SetFields('irckey', 0, "string", "You did not enter a valid IRC key. An IRC key must be between 6 and 32 characters long.", ['minlength' => 6, 'maxlength' => 32]);
  29. $Val->SetFields('new_pass_1', 0, "regex", "You did not enter a valid password. A valid password is 6 characters or longer.", ['regex' => '/(?=^.{6,}$).*$/']);
  30. $Val->SetFields('new_pass_2', 1, "compare", "Your passwords do not match.", ['comparefield' => 'new_pass_1']);
  31. if (check_perms('site_advanced_search')) {
  32. $Val->SetFields('searchtype', 1, "number", "You forgot to select your default search preference.", ['minlength' => 0, 'maxlength' => 1]);
  33. }
  34. $ValErr = $Val->ValidateForm($_POST);
  35. if ($ValErr) {
  36. error($ValErr);
  37. }
  38. if (!apcu_exists('DBKEY')) {
  39. error("Cannot edit profile until database fully decrypted");
  40. }
  41. // Begin building $Paranoia
  42. // Reduce the user's input paranoia until it becomes consistent
  43. if (isset($_POST['p_uniquegroups_l'])) {
  44. $_POST['p_uploads_l'] = 'on';
  45. $_POST['p_uploads_c'] = 'on';
  46. }
  47. if (isset($_POST['p_uploads_l'])) {
  48. $_POST['p_uniquegroups_l'] = 'on';
  49. $_POST['p_uniquegroups_c'] = 'on';
  50. $_POST['p_perfectflacs_l'] = 'on';
  51. $_POST['p_perfectflacs_c'] = 'on';
  52. $_POST['p_artistsadded'] = 'on';
  53. }
  54. if (isset($_POST['p_collagecontribs_l'])) {
  55. $_POST['p_collages_l'] = 'on';
  56. $_POST['p_collages_c'] = 'on';
  57. }
  58. if (isset($_POST['p_snatched_c']) && isset($_POST['p_seeding_c']) && isset($_POST['p_downloaded'])) {
  59. $_POST['p_requiredratio'] = 'on';
  60. }
  61. // if showing exactly 2 of stats, show all 3 of stats
  62. $StatsShown = 0;
  63. $Stats = ['downloaded', 'uploaded', 'ratio'];
  64. foreach ($Stats as $S) {
  65. if (isset($_POST["p_$S"])) {
  66. $StatsShown++;
  67. }
  68. }
  69. if ($StatsShown == 2) {
  70. foreach ($Stats as $S) {
  71. $_POST["p_$S"] = 'on';
  72. }
  73. }
  74. $Paranoia = [];
  75. $Checkboxes = ['downloaded', 'uploaded', 'ratio', 'lastseen', 'requiredratio', 'invitedcount', 'artistsadded', 'notifications'];
  76. foreach ($Checkboxes as $C) {
  77. if (!isset($_POST["p_$C"])) {
  78. $Paranoia[] = $C;
  79. }
  80. }
  81. $SimpleSelects = ['torrentcomments', 'collages', 'collagecontribs', 'uploads', 'uniquegroups', 'perfectflacs', 'seeding', 'leeching', 'snatched'];
  82. foreach ($SimpleSelects as $S) {
  83. if (!isset($_POST["p_$S".'_c']) && !isset($_POST["p_$S".'_l'])) {
  84. // Very paranoid - don't show count or list
  85. $Paranoia[] = "$S+";
  86. } elseif (!isset($_POST["p_$S".'_l'])) {
  87. // A little paranoid - show count, don't show list
  88. $Paranoia[] = $S;
  89. }
  90. }
  91. $Bounties = ['requestsfilled', 'requestsvoted'];
  92. foreach ($Bounties as $B) {
  93. if (isset($_POST["p_$B".'_list'])) {
  94. $_POST["p_$B".'_count'] = 'on';
  95. $_POST["p_$B".'_bounty'] = 'on';
  96. }
  97. if (!isset($_POST["p_$B".'_list'])) {
  98. $Paranoia[] = $B.'_list';
  99. }
  100. if (!isset($_POST["p_$B".'_count'])) {
  101. $Paranoia[] = $B.'_count';
  102. }
  103. if (!isset($_POST["p_$B".'_bounty'])) {
  104. $Paranoia[] = $B.'_bounty';
  105. }
  106. }
  107. if (!isset($_POST['p_donor_heart'])) {
  108. $Paranoia[] = 'hide_donor_heart';
  109. }
  110. if (isset($_POST['p_donor_stats'])) {
  111. Donations::show_stats($UserID);
  112. } else {
  113. Donations::hide_stats($UserID);
  114. }
  115. // End building $Paranoia
  116. $DB->query("
  117. SELECT Email, PassHash, TwoFactor, PublicKey, IRCKey
  118. FROM users_main
  119. WHERE ID = $UserID");
  120. list($CurEmail, $CurPassHash, $CurTwoFA, $CurPublicKey, $CurIRCKey) = $DB->next_record();
  121. function require_password($Setting = false) {
  122. global $CurPassHash;
  123. if (empty($_POST['cur_pass'])) {
  124. error('A setting you changed requires you to enter your current password'.($Setting ? ' (Setting: '.$Setting.')' : ''));
  125. }
  126. if (!Users::check_password($_POST['cur_pass'], $CurPassHash)) {
  127. error('The password you entered was incorrect'.($Setting ? ' (Required by setting: '.$Setting.')' : ''));
  128. }
  129. }
  130. // Email change
  131. $CurEmail = DBCrypt::decrypt($CurEmail);
  132. if ($CurEmail != $_POST['email']) {
  133. // Non-admins have to authenticate to change email
  134. if (!check_perms('users_edit_profiles')) {
  135. require_password("Change Email");
  136. }
  137. $NewEmail = db_string($_POST['email']);
  138. // Update the time of their last email change to the current time *not* the current change.
  139. $ChangerIP = db_string($LoggedUser['IP']);
  140. $DB->query("
  141. UPDATE users_history_emails
  142. SET Time = '".sqltime()."'
  143. WHERE UserID = '$UserID'
  144. AND Time IS NULL");
  145. $DB->query("
  146. INSERT INTO users_history_emails
  147. (UserID, Email, Time, IP)
  148. VALUES
  149. ('$UserID', '".DBCrypt::encrypt($NewEmail)."', NULL, '".DBCrypt::encrypt($_SERVER['REMOTE_ADDR'])."')");
  150. }
  151. // PGP Key
  152. if ($CurPublicKey != $_POST['publickey']) {
  153. require_password("Change Public Key");
  154. $DB->query("
  155. UPDATE users_main
  156. SET PublicKey = '".db_string($_POST['publickey'])."'
  157. WHERE ID = $UserID");
  158. }
  159. // 2FA activation
  160. if (!empty($_POST['twofa']) && empty($CurTwoFA)) {
  161. require_password("Enable 2-Factor");
  162. require_once SERVER_ROOT.'/classes/twofa.class.php';
  163. $TwoFA = new TwoFactorAuth(SITE_NAME);
  164. if ($TwoFA->verifyCode($_POST['twofasecret'], $_POST['twofa'])) {
  165. $DB->query("
  166. UPDATE users_main
  167. SET TwoFactor='".db_string($_POST['twofasecret'])."'
  168. WHERE ID = $UserID");
  169. } else {
  170. error('Invalid 2FA verification code.');
  171. }
  172. }
  173. // 2FA deactivation
  174. if (isset($_POST['disable2fa'])) {
  175. require_password("Disable 2-Factor");
  176. $DB->query("
  177. UPDATE users_main
  178. SET TwoFactor = NULL
  179. WHERE ID = $UserID");
  180. }
  181. if (!empty($_POST['new_pass_1']) && !empty($_POST['new_pass_2'])) {
  182. require_password("Change Password");
  183. $ResetPassword = true;
  184. }
  185. if ($CurIRCKey != $_POST['irckey']) {
  186. require_password("Change IRC Key");
  187. }
  188. if (isset($_POST['resetpasskey'])) {
  189. require_password("Reset Passkey");
  190. }
  191. if ($LoggedUser['DisableAvatar'] && $_POST['avatar'] != $U['Avatar']) {
  192. error('Your avatar privileges have been revoked.');
  193. }
  194. if (!empty($LoggedUser['DefaultSearch'])) {
  195. $Options['DefaultSearch'] = $LoggedUser['DefaultSearch'];
  196. }
  197. $Options['DisableGrouping2'] = (!empty($_POST['disablegrouping']) ? 0 : 1);
  198. $Options['TorrentGrouping'] = (!empty($_POST['torrentgrouping']) ? 1 : 0);
  199. $Options['PostsPerPage'] = (int)$_POST['postsperpage'];
  200. $Options['CollageCovers'] = (empty($_POST['collagecovers']) ? 0 : $_POST['collagecovers']);
  201. $Options['ShowTorFilter'] = (empty($_POST['showtfilter']) ? 0 : 1);
  202. $Options['ShowTags'] = (!empty($_POST['showtags']) ? 1 : 0);
  203. $Options['AutoSubscribe'] = (!empty($_POST['autosubscribe']) ? 1 : 0);
  204. $Options['DisableSmileys'] = (!empty($_POST['disablesmileys']) ? 1 : 0);
  205. $Options['AutoloadCommStats'] = (check_perms('users_mod') && !empty($_POST['autoload_comm_stats']) ? 1 : 0);
  206. $Options['DisableAvatars'] = db_string($_POST['disableavatars']);
  207. $Options['Identicons'] = (!empty($_POST['identicons']) ? (int)$_POST['identicons'] : 0);
  208. $Options['DisablePMAvatars'] = (!empty($_POST['disablepmavatars']) ? 1 : 0);
  209. $Options['NotifyOnQuote'] = (!empty($_POST['notifications_Quotes_popup']) ? 1 : 0);
  210. $Options['ListUnreadPMsFirst'] = (!empty($_POST['list_unread_pms_first']) ? 1 : 0);
  211. $Options['ShowSnatched'] = (!empty($_POST['showsnatched']) ? 1 : 0);
  212. $Options['ShowMagnets'] = (!empty($_POST['showmagnets']) ? 1 : 0);
  213. $Options['DisableAutoSave'] = (!empty($_POST['disableautosave']) ? 1 : 0);
  214. $Options['CoverArt'] = (int)!empty($_POST['coverart']);
  215. $Options['ShowExtraCovers'] = (int)!empty($_POST['show_extra_covers']);
  216. $Options['HideLolicon'] = (int)!empty($_POST['hide_lolicon']);
  217. $Options['HideScat'] = (int)!empty($_POST['hide_scat']);
  218. $Options['HideSnuff'] = (int)!empty($_POST['hide_snuff']);
  219. $Options['AutoComplete'] = (int)$_POST['autocomplete'];
  220. $Options['StyleAdditions'] = $_POST['style_additions'] ?? [];
  221. if (isset($LoggedUser['DisableFreeTorrentTop10'])) {
  222. $Options['DisableFreeTorrentTop10'] = $LoggedUser['DisableFreeTorrentTop10'];
  223. }
  224. if (!empty($_POST['sorthide'])) {
  225. $JSON = json_decode($_POST['sorthide']);
  226. foreach ($JSON as $J) {
  227. $E = explode('_', $J);
  228. $Options['SortHide'][$E[0]] = $E[1];
  229. }
  230. } else {
  231. $Options['SortHide'] = [];
  232. }
  233. if (check_perms('site_advanced_search')) {
  234. $Options['SearchType'] = $_POST['searchtype'];
  235. } else {
  236. unset($Options['SearchType']);
  237. }
  238. //TODO: Remove the following after a significant amount of time
  239. unset($Options['ArtistNoRedirect']);
  240. unset($Options['ShowQueryList']);
  241. unset($Options['ShowCacheList']);
  242. $UnseededAlerts = isset($_POST['unseededalerts']) ? 1 : 0;
  243. Donations::update_rewards($UserID);
  244. NotificationsManager::save_settings($UserID);
  245. // Begin Badge settings
  246. if (!empty($_POST['badges'])) {
  247. $BadgeIDs = array_slice($_POST['badges'], 0, 5);
  248. } else {
  249. $BadgeIDs = [];
  250. }
  251. $BadgesChanged = false;
  252. $NewBadges = [];
  253. if ($Cache->get_value('user_badges_'.$UserID)) {
  254. $Badges = $Cache->get_value('user_badges_'.$UserID);
  255. foreach ($Badges as $Badge) {
  256. if (in_array($Badge['BadgeID'], $BadgeIDs)) { // Is the current badge in the list of badges the user wants to display?
  257. $Displayed = true;
  258. $DisplayedBadgeIDs[] = $Badge['BadgeID'];
  259. if ($Badge['Displayed'] == 0) { // The user wants to display a badge that wasn't displayed before
  260. $BadgesChanged = true;
  261. }
  262. } else { // The user no longer wants to display a badge that was displayed before
  263. $Displayed = false;
  264. $BadgesChanged = true;
  265. }
  266. $NewBadges[] = ['BadgeID' => $Badge['BadgeID'], 'Displayed' => $Displayed?'1':'0'];
  267. }
  268. } else {
  269. $BadgesChanged = true;
  270. }
  271. if ($BadgesChanged) {
  272. $Cache->cache_value('user_badges_'.$UserID, $NewBadges);
  273. }
  274. // End Badge settings
  275. $Cache->begin_transaction("user_info_$UserID");
  276. $Cache->update_row(false, [
  277. 'Avatar' => display_str($_POST['avatar']),
  278. 'Paranoia' => $Paranoia
  279. ]);
  280. $Cache->commit_transaction(0);
  281. $Cache->begin_transaction("user_info_heavy_$UserID");
  282. $Cache->update_row(false, [
  283. 'StyleID' => $_POST['stylesheet'],
  284. 'StyleURL' => display_str($_POST['styleurl'])
  285. ]);
  286. $Cache->update_row(false, $Options);
  287. $Cache->commit_transaction(0);
  288. $SQL = "
  289. UPDATE users_main AS m
  290. JOIN users_info AS i ON m.ID = i.UserID
  291. SET
  292. i.StyleID = '".db_string($_POST['stylesheet'])."',
  293. i.StyleURL = '".db_string($_POST['styleurl'])."',
  294. i.Avatar = '".db_string($_POST['avatar'])."',
  295. i.SiteOptions = '".db_string(json_encode($Options))."',
  296. i.NotifyOnQuote = '".db_string($Options['NotifyOnQuote'])."',
  297. i.Info = '".db_string($_POST['info'])."',
  298. i.InfoTitle = '".db_string($_POST['profile_title'])."',
  299. i.UnseededAlerts = '$UnseededAlerts',
  300. m.Email = '".DBCrypt::encrypt($_POST['email'])."',
  301. m.IRCKey = '".db_string($_POST['irckey'])."',
  302. m.Paranoia = '".db_string(json_encode($Paranoia))."'";
  303. if ($ResetPassword) {
  304. $ChangerIP = db_string(DBCrypt::encrypt($LoggedUser['IP']));
  305. $PassHash = Users::make_sec_hash($_POST['new_pass_1']);
  306. $SQL.= ",m.PassHash = '".db_string($PassHash)."'";
  307. $DB->query("
  308. INSERT INTO users_history_passwords
  309. (UserID, ChangerIP, ChangeTime)
  310. VALUES
  311. ('$UserID', '$ChangerIP', '".sqltime()."')");
  312. }
  313. if (isset($_POST['resetpasskey'])) {
  314. $UserInfo = Users::user_heavy_info($UserID);
  315. $OldPassKey = db_string($UserInfo['torrent_pass']);
  316. $NewPassKey = db_string(Users::make_secret());
  317. $ChangerIP = db_string(DBCrypt::encrypt($LoggedUser['IP']));
  318. $SQL .= ",m.torrent_pass = '$NewPassKey'";
  319. $DB->query("
  320. INSERT INTO users_history_passkeys
  321. (UserID, OldPassKey, NewPassKey, ChangerIP, ChangeTime)
  322. VALUES
  323. ('$UserID', '$OldPassKey', '$NewPassKey', '$ChangerIP', '".sqltime()."')");
  324. $Cache->begin_transaction("user_info_heavy_$UserID");
  325. $Cache->update_row(false, ['torrent_pass' => $NewPassKey]);
  326. $Cache->commit_transaction(0);
  327. $Cache->delete_value("user_$OldPassKey");
  328. Tracker::update_tracker('change_passkey', ['oldpasskey' => $OldPassKey, 'newpasskey' => $NewPassKey]);
  329. }
  330. $SQL .= "WHERE m.ID = '".db_string($UserID)."'";
  331. $DB->query($SQL);
  332. if ($BadgesChanged) {
  333. $DB->query("
  334. UPDATE users_badges
  335. SET Displayed = 0
  336. WHERE UserID = $UserID");
  337. if (!empty($BadgeIDs)) {
  338. $DB->query("
  339. UPDATE users_badges
  340. SET Displayed = 1
  341. WHERE UserID = $UserID
  342. AND BadgeID IN (".db_string(implode(',', $BadgeIDs)).")");
  343. }
  344. }
  345. if ($ResetPassword) {
  346. logout_all_sessions();
  347. }
  348. header("Location: user.php?action=edit&userid=$UserID");
  349. ?>