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.

take_edit.php 13KB

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