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.

users.class.php 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. <?
  2. class Users {
  3. /**
  4. * Get $Classes (list of classes keyed by ID) and $ClassLevels
  5. * (list of classes keyed by level)
  6. * @return array ($Classes, $ClassLevels)
  7. */
  8. public static function get_classes() {
  9. global $Debug;
  10. // Get permissions
  11. list($Classes, $ClassLevels) = G::$Cache->get_value('classes');
  12. if (!$Classes || !$ClassLevels) {
  13. $QueryID = G::$DB->get_query_id();
  14. G::$DB->query('
  15. SELECT ID, Name, Abbreviation, Level, Secondary
  16. FROM permissions
  17. ORDER BY Level');
  18. $Classes = G::$DB->to_array('ID');
  19. $ClassLevels = G::$DB->to_array('Level');
  20. G::$DB->set_query_id($QueryID);
  21. G::$Cache->cache_value('classes', [$Classes, $ClassLevels], 0);
  22. }
  23. $Debug->set_flag('Loaded permissions');
  24. return [$Classes, $ClassLevels];
  25. }
  26. /**
  27. * Get user info, is used for the current user and usernames all over the site.
  28. *
  29. * @param $UserID int The UserID to get info for
  30. * @return array with the following keys:
  31. * int ID
  32. * string Username
  33. * int PermissionID
  34. * array Paranoia - $Paranoia array sent to paranoia.class
  35. * boolean Artist
  36. * boolean Donor
  37. * string Warned - When their warning expires in international time format
  38. * string Avatar - URL
  39. * boolean Enabled
  40. * string Title
  41. * string CatchupTime - When they last caught up on forums
  42. * boolean Visible - If false, they don't show up on peer lists
  43. * array ExtraClasses - Secondary classes.
  44. * int EffectiveClass - the highest level of their main and secondary classes
  45. */
  46. public static function user_info($UserID) {
  47. global $Classes;
  48. $UserInfo = G::$Cache->get_value("user_info_$UserID");
  49. // the !isset($UserInfo['Paranoia']) can be removed after a transition period
  50. if (empty($UserInfo) || empty($UserInfo['ID']) || !isset($UserInfo['Paranoia']) || empty($UserInfo['Class'])) {
  51. $OldQueryID = G::$DB->get_query_id();
  52. G::$DB->query("
  53. SELECT
  54. m.ID,
  55. m.Username,
  56. m.PermissionID,
  57. m.Paranoia,
  58. i.Artist,
  59. i.Donor,
  60. i.Warned,
  61. i.Avatar,
  62. m.Enabled,
  63. m.Title,
  64. i.CatchupTime,
  65. m.Visible,
  66. la.Type AS LockedAccount,
  67. GROUP_CONCAT(ul.PermissionID SEPARATOR ',') AS Levels
  68. FROM users_main AS m
  69. INNER JOIN users_info AS i ON i.UserID = m.ID
  70. LEFT JOIN locked_accounts AS la ON la.UserID = m.ID
  71. LEFT JOIN users_levels AS ul ON ul.UserID = m.ID
  72. WHERE m.ID = '$UserID'
  73. GROUP BY m.ID");
  74. if (!G::$DB->has_results()) { // Deleted user, maybe?
  75. $UserInfo = [
  76. 'ID' => $UserID,
  77. 'Username' => '',
  78. 'PermissionID' => 0,
  79. 'Paranoia' => [],
  80. 'Artist' => false,
  81. 'Donor' => false,
  82. 'Warned' => NULL,
  83. 'Avatar' => '',
  84. 'Enabled' => 0,
  85. 'Title' => '',
  86. 'CatchupTime' => 0,
  87. 'Visible' => '1',
  88. 'Levels' => '',
  89. 'Class' => 0
  90. ];
  91. } else {
  92. $UserInfo = G::$DB->next_record(MYSQLI_ASSOC, ['Paranoia', 'Title']);
  93. $UserInfo['CatchupTime'] = strtotime($UserInfo['CatchupTime']);
  94. if (!is_array($UserInfo['Paranoia'])) {
  95. $UserInfo['Paranoia'] = json_decode($UserInfo['Paranoia'], true);
  96. }
  97. if (!$UserInfo['Paranoia']) {
  98. $UserInfo['Paranoia'] = [];
  99. }
  100. $UserInfo['Class'] = $Classes[$UserInfo['PermissionID']]['Level'];
  101. }
  102. if (isset($UserInfo['LockedAccount']) && $UserInfo['LockedAccount'] == "") {
  103. unset($UserInfo['LockedAccount']);
  104. }
  105. if (!empty($UserInfo['Levels'])) {
  106. $UserInfo['ExtraClasses'] = array_fill_keys(explode(',', $UserInfo['Levels']), 1);
  107. } else {
  108. $UserInfo['ExtraClasses'] = [];
  109. }
  110. unset($UserInfo['Levels']);
  111. $EffectiveClass = $UserInfo['Class'];
  112. foreach ($UserInfo['ExtraClasses'] as $Class => $Val) {
  113. $EffectiveClass = max($EffectiveClass, $Classes[$Class]['Level']);
  114. }
  115. $UserInfo['EffectiveClass'] = $EffectiveClass;
  116. G::$Cache->cache_value("user_info_$UserID", $UserInfo, 2592000);
  117. G::$DB->set_query_id($OldQueryID);
  118. }
  119. if (strtotime($UserInfo['Warned']) < time()) {
  120. $UserInfo['Warned'] = NULL;
  121. G::$Cache->cache_value("user_info_$UserID", $UserInfo, 2592000);
  122. }
  123. return $UserInfo;
  124. }
  125. /**
  126. * Gets the heavy user info
  127. * Only used for current user
  128. *
  129. * @param $UserID The userid to get the information for
  130. * @return fetched heavy info.
  131. * Just read the goddamn code, I don't have time to comment this shit.
  132. */
  133. public static function user_heavy_info($UserID) {
  134. $HeavyInfo = G::$Cache->get_value("user_info_heavy_$UserID");
  135. if (empty($HeavyInfo)) {
  136. $QueryID = G::$DB->get_query_id();
  137. G::$DB->query("
  138. SELECT
  139. m.Invites,
  140. m.torrent_pass,
  141. m.IP,
  142. m.CustomPermissions,
  143. m.can_leech AS CanLeech,
  144. i.AuthKey,
  145. i.RatioWatchEnds,
  146. i.RatioWatchDownload,
  147. i.StyleID,
  148. i.StyleURL,
  149. i.DisableInvites,
  150. i.DisablePosting,
  151. i.DisableUpload,
  152. i.DisableWiki,
  153. i.DisableAvatar,
  154. i.DisablePM,
  155. i.DisablePoints,
  156. i.DisablePromotion,
  157. i.DisableRequests,
  158. i.DisableForums,
  159. i.DisableTagging,
  160. i.SiteOptions,
  161. i.LastReadNews,
  162. i.LastReadBlog,
  163. i.RestrictedForums,
  164. i.PermittedForums,
  165. m.FLTokens,
  166. m.BonusPoints,
  167. m.HnR,
  168. m.PermissionID
  169. FROM users_main AS m
  170. INNER JOIN users_info AS i ON i.UserID = m.ID
  171. WHERE m.ID = '$UserID'");
  172. $HeavyInfo = G::$DB->next_record(MYSQLI_ASSOC, ['CustomPermissions', 'SiteOptions']);
  173. $HeavyInfo['CustomPermissions'] = [];
  174. if (!empty($HeavyInfo['CustomPermissions'])) {
  175. $HeavyInfo['CustomPermissions'] = json_decode($HeavyInfo['CustomPermissions'], true);
  176. }
  177. $RestrictedForums = [];
  178. if (!empty($HeavyInfo['RestrictedForums'])) {
  179. $RestrictedForums = array_map('trim', explode(',', $HeavyInfo['RestrictedForums']));
  180. }
  181. unset($HeavyInfo['RestrictedForums']);
  182. $PermittedForums = [];
  183. if (!empty($HeavyInfo['PermittedForums'])) {
  184. $PermittedForums = array_map('trim', explode(',', $HeavyInfo['PermittedForums']));
  185. }
  186. unset($HeavyInfo['PermittedForums']);
  187. G::$DB->query("
  188. SELECT PermissionID
  189. FROM users_levels
  190. WHERE UserID = $UserID");
  191. $PermIDs = G::$DB->collect('PermissionID');
  192. foreach ($PermIDs AS $PermID) {
  193. $Perms = Permissions::get_permissions($PermID);
  194. if (!empty($Perms['PermittedForums'])) {
  195. $PermittedForums = array_merge($PermittedForums, array_map('trim', explode(',', $Perms['PermittedForums'])));
  196. }
  197. }
  198. $Perms = Permissions::get_permissions($HeavyInfo['PermissionID']);
  199. unset($HeavyInfo['PermissionID']);
  200. if (!empty($Perms['PermittedForums'])) {
  201. $PermittedForums = array_merge($PermittedForums, array_map('trim', explode(',', $Perms['PermittedForums'])));
  202. }
  203. $HeavyInfo['CustomForums'] = null;
  204. if (!empty($PermittedForums) || !empty($RestrictedForums)) {
  205. $HeavyInfo['CustomForums'] = [];
  206. foreach ($RestrictedForums as $ForumID) {
  207. $HeavyInfo['CustomForums'][$ForumID] = 0;
  208. }
  209. foreach ($PermittedForums as $ForumID) {
  210. $HeavyInfo['CustomForums'][$ForumID] = 1;
  211. }
  212. }
  213. if (isset($HeavyInfo['CustomForums'][''])) {
  214. unset($HeavyInfo['CustomForums']['']);
  215. }
  216. $HeavyInfo['SiteOptions'] = json_decode($HeavyInfo['SiteOptions'], true);
  217. if (!empty($HeavyInfo['SiteOptions'])) {
  218. $HeavyInfo = array_merge($HeavyInfo, $HeavyInfo['SiteOptions']);
  219. }
  220. unset($HeavyInfo['SiteOptions']);
  221. G::$DB->set_query_id($QueryID);
  222. G::$Cache->cache_value("user_info_heavy_$UserID", $HeavyInfo, 0);
  223. }
  224. return $HeavyInfo;
  225. }
  226. /**
  227. * Updates the site options in the database
  228. *
  229. * @param int $UserID the UserID to set the options for
  230. * @param array $NewOptions the new options to set
  231. * @return false if $NewOptions is empty, true otherwise
  232. */
  233. public static function update_site_options($UserID, $NewOptions) {
  234. if (!is_number($UserID)) {
  235. error(0);
  236. }
  237. if (empty($NewOptions)) {
  238. return false;
  239. }
  240. $QueryID = G::$DB->get_query_id();
  241. // Get SiteOptions
  242. G::$DB->query("
  243. SELECT SiteOptions
  244. FROM users_info
  245. WHERE UserID = $UserID");
  246. list($SiteOptions) = G::$DB->next_record(MYSQLI_NUM, false);
  247. $SiteOptions = json_decode($SiteOptions, true);
  248. // Get HeavyInfo
  249. $HeavyInfo = Users::user_heavy_info($UserID);
  250. // Insert new/replace old options
  251. $SiteOptions = array_merge($SiteOptions, $NewOptions);
  252. $HeavyInfo = array_merge($HeavyInfo, $NewOptions);
  253. // Update DB
  254. G::$DB->query("
  255. UPDATE users_info
  256. SET SiteOptions = '".db_string(json_encode($SiteOptions, true))."'
  257. WHERE UserID = $UserID");
  258. G::$DB->set_query_id($QueryID);
  259. // Update cache
  260. G::$Cache->cache_value("user_info_heavy_$UserID", $HeavyInfo, 0);
  261. // Update G::$LoggedUser if the options are changed for the current
  262. if (G::$LoggedUser['ID'] == $UserID) {
  263. G::$LoggedUser = array_merge(G::$LoggedUser, $NewOptions);
  264. G::$LoggedUser['ID'] = $UserID; // We don't want to allow userid switching
  265. }
  266. return true;
  267. }
  268. /**
  269. * Generate a random string
  270. *
  271. * @param Length
  272. * @return random alphanumeric string
  273. */
  274. public static function make_secret($Length = 32) {
  275. $Secret = '';
  276. $Chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  277. for ($i = 0; $i < $Length; $i++) {
  278. $Secret .= $Chars[random_int(0, strlen($Chars)-1)];
  279. }
  280. return str_shuffle($Secret);
  281. }
  282. /**
  283. * Verify a password against a password hash
  284. *
  285. * @param $Password password
  286. * @param $Hash password hash
  287. * @return true on correct password
  288. */
  289. public static function check_password($Password, $Hash) {
  290. if (!$Password || !$Hash) { return false; }
  291. return password_verify(str_replace("\0","",hash("sha512", $Password, true)), $Hash);
  292. }
  293. /**
  294. * Create salted hash for a given string
  295. *
  296. * @param $Str string to hash
  297. * @return salted hash
  298. */
  299. public static function make_sec_hash($Str) {
  300. return password_hash(str_replace("\0","",hash("sha512", $Str, true)), PASSWORD_DEFAULT);
  301. }
  302. /**
  303. * Returns a username string for display
  304. *
  305. * @param int $UserID
  306. * @param boolean $Badges whether or not badges (donor, warned, enabled) should be shown
  307. * @param boolean $IsWarned
  308. * @param boolean $IsEnabled
  309. * @param boolean $Class whether or not to show the class
  310. * @param boolean $Title whether or not to show the title
  311. * @param boolean $IsDonorForum for displaying donor forum honorific prefixes and suffixes
  312. * @return HTML formatted username
  313. */
  314. public static function format_username($UserID, $Badges = false, $IsWarned = true, $IsEnabled = true, $Class = false, $Title = false, $IsDonorForum = false) {
  315. global $Classes;
  316. if ($UserID == 0) {
  317. return 'System';
  318. }
  319. $UserInfo = self::user_info($UserID);
  320. if ($UserInfo['Username'] == '') {
  321. return "Unknown [$UserID]";
  322. }
  323. $Str = '';
  324. $Username = $UserInfo['Username'];
  325. $Paranoia = $UserInfo['Paranoia'];
  326. if ($UserInfo['Class'] < $Classes[MOD]['Level']) {
  327. $OverrideParanoia = check_perms('users_override_paranoia', $UserInfo['Class']);
  328. } else {
  329. // Don't override paranoia for mods who don't want to show their donor heart
  330. $OverrideParanoia = false;
  331. }
  332. $ShowDonorIcon = (!in_array('hide_donor_heart', $Paranoia) || $OverrideParanoia);
  333. if ($IsDonorForum) {
  334. list($Prefix, $Suffix, $HasComma) = Donations::get_titles($UserID);
  335. $Username = "$Prefix $Username" . ($HasComma ? ', ' : ' ') . "$Suffix ";
  336. }
  337. if ($Title) {
  338. $Str .= "<strong><a href=\"user.php?id=$UserID\">$Username</a></strong>";
  339. } else {
  340. $Str .= "<a href=\"user.php?id=$UserID\">$Username</a>";
  341. }
  342. if ($Badges) {
  343. $DonorRank = Donations::get_rank($UserID);
  344. if ($DonorRank == 0 && $UserInfo['Donor'] == 1) {
  345. $DonorRank = 1;
  346. }
  347. if ($ShowDonorIcon && $DonorRank > 0) {
  348. $IconLink = 'donate.php';
  349. $IconImage = 'donor.png';
  350. $IconText = 'Donor';
  351. $DonorHeart = $DonorRank;
  352. $SpecialRank = Donations::get_special_rank($UserID);
  353. $EnabledRewards = Donations::get_enabled_rewards($UserID);
  354. $DonorRewards = Donations::get_rewards($UserID);
  355. if ($EnabledRewards['HasDonorIconMouseOverText'] && !empty($DonorRewards['IconMouseOverText'])) {
  356. $IconText = display_str($DonorRewards['IconMouseOverText']);
  357. }
  358. if ($EnabledRewards['HasDonorIconLink'] && !empty($DonorRewards['CustomIconLink'])) {
  359. $IconLink = display_str($DonorRewards['CustomIconLink']);
  360. }
  361. if ($EnabledRewards['HasCustomDonorIcon'] && !empty($DonorRewards['CustomIcon'])) {
  362. $IconImage = ImageTools::process($DonorRewards['CustomIcon'], false, 'donoricon', $UserID);
  363. } else {
  364. if ($SpecialRank === MAX_SPECIAL_RANK) {
  365. $DonorHeart = 6;
  366. } elseif ($DonorRank === 5) {
  367. $DonorHeart = 4; // Two points between rank 4 and 5
  368. } elseif ($DonorRank >= MAX_RANK) {
  369. $DonorHeart = 5;
  370. }
  371. if ($DonorHeart === 1) {
  372. $IconImage = STATIC_SERVER . 'common/symbols/donor.png';
  373. } else {
  374. $IconImage = STATIC_SERVER . "common/symbols/donor_{$DonorHeart}.png";
  375. }
  376. }
  377. $Str .= "<a target=\"_blank\" href=\"$IconLink\"><img class=\"donor_icon tooltip\" src=\"$IconImage\" alt=\"$IconText\" title=\"$IconText\" /></a>";
  378. }
  379. $Str .= Badges::display_badges(Badges::get_displayed_badges($UserID), true);
  380. }
  381. $Str .= ($IsWarned && $UserInfo['Warned']) ? '<a href="wiki.php?action=article&amp;id=218"'
  382. . '><img src="'.STATIC_SERVER.'common/symbols/warned.png" alt="Warned" title="Warned'
  383. . (G::$LoggedUser['ID'] === $UserID ? ' - Expires ' . date('Y-m-d H:i', strtotime($UserInfo['Warned'])) : '')
  384. . '" class="tooltip" /></a>' : '';
  385. $Str .= ($IsEnabled && $UserInfo['Enabled'] == 2) ? '<a href="rules.php"><img src="'.STATIC_SERVER.'common/symbols/disabled.png" alt="Banned" title="Disabled" class="tooltip" /></a>' : '';
  386. if ($Class) {
  387. foreach (array_keys($UserInfo['ExtraClasses']) as $ExtraClass) {
  388. $Str .= ' ['.Users::make_class_abbrev_string($ExtraClass).']';
  389. }
  390. if ($Title) {
  391. $Str .= ' <strong>('.Users::make_class_string($UserInfo['PermissionID']).')</strong>';
  392. } else {
  393. $Str .= ' ('.Users::make_class_string($UserInfo['PermissionID']).')';
  394. }
  395. }
  396. if ($Title) {
  397. // Image proxy CTs
  398. if (check_perms('site_proxy_images') && !empty($UserInfo['Title'])) {
  399. $UserInfo['Title'] = preg_replace_callback('~src=("?)(http.+?)(["\s>])~',
  400. function($Matches) {
  401. return 'src=' . $Matches[1] . ImageTools::process($Matches[2]) . $Matches[3];
  402. },
  403. $UserInfo['Title']);
  404. }
  405. if ($UserInfo['Title']) {
  406. $Str .= ' <span class="user_title">('.$UserInfo['Title'].')</span>';
  407. }
  408. }
  409. return $Str;
  410. }
  411. /**
  412. * Given a class ID, return its name.
  413. *
  414. * @param int $ClassID
  415. * @return string name
  416. */
  417. public static function make_class_string($ClassID) {
  418. global $Classes;
  419. return $Classes[$ClassID]['Name'];
  420. }
  421. public static function make_class_abbrev_string($ClassID) {
  422. global $Classes;
  423. return '<abbr title="'.$Classes[$ClassID]['Name'].'">'.$Classes[$ClassID]['Abbreviation'].'</abbr>';
  424. }
  425. /**
  426. * Returns an array with User Bookmark data: group IDs, collage data, torrent data
  427. * @param string|int $UserID
  428. * @return array Group IDs, Bookmark Data, Torrent List
  429. */
  430. public static function get_bookmarks($UserID) {
  431. $UserID = (int)$UserID;
  432. if (($Data = G::$Cache->get_value("bookmarks_group_ids_$UserID"))) {
  433. list($GroupIDs, $BookmarkData) = $Data;
  434. } else {
  435. $QueryID = G::$DB->get_query_id();
  436. G::$DB->query("
  437. SELECT GroupID, Sort, `Time`
  438. FROM bookmarks_torrents
  439. WHERE UserID = $UserID
  440. ORDER BY Sort, `Time` ASC");
  441. $GroupIDs = G::$DB->collect('GroupID');
  442. $BookmarkData = G::$DB->to_array('GroupID', MYSQLI_ASSOC);
  443. G::$DB->set_query_id($QueryID);
  444. G::$Cache->cache_value("bookmarks_group_ids_$UserID", [$GroupIDs, $BookmarkData], 3600);
  445. }
  446. $TorrentList = Torrents::get_groups($GroupIDs);
  447. return [$GroupIDs, $BookmarkData, $TorrentList];
  448. }
  449. /**
  450. * Generate HTML for a user's avatar or just return the avatar URL
  451. * @param unknown $Avatar
  452. * @param unknown $UserID
  453. * @param unknown $Username
  454. * @param unknown $Setting
  455. * @param number $Size
  456. * @param string $ReturnHTML
  457. * @return string
  458. */
  459. public static function show_avatar($Avatar, $UserID, $Username, $Setting, $Size = 150, $ReturnHTML = true) {
  460. $Avatar = ImageTools::process($Avatar, false, 'avatar', $UserID);
  461. $Style = 'style="max-height: 400px;"';
  462. $AvatarMouseOverText = '';
  463. $SecondAvatar = '';
  464. $Class = 'class="double_avatar"';
  465. $EnabledRewards = Donations::get_enabled_rewards($UserID);
  466. if ($EnabledRewards['HasAvatarMouseOverText']) {
  467. $Rewards = Donations::get_rewards($UserID);
  468. $AvatarMouseOverText = $Rewards['AvatarMouseOverText'];
  469. }
  470. if (!empty($AvatarMouseOverText)) {
  471. $AvatarMouseOverText = "title=\"$AvatarMouseOverText\" alt=\"$AvatarMouseOverText\"";
  472. } else {
  473. $AvatarMouseOverText = "alt=\"$Username's avatar\"";
  474. }
  475. if ($EnabledRewards['HasSecondAvatar'] && !empty($Rewards['SecondAvatar'])) {
  476. $SecondAvatar = ' data-gazelle-second-avatar="' . ImageTools::process($Rewards['SecondAvatar'], false, 'avatar2', $UserID) . '"';
  477. }
  478. // case 1 is avatars disabled
  479. switch ($Setting) {
  480. case 0:
  481. if (!empty($Avatar)) {
  482. $ToReturn = ($ReturnHTML ? "<a href=\"user.php?id=$UserID\"><img src=\"$Avatar\" ".($Size?"width=\"$Size\" ":"")."$Style $AvatarMouseOverText$SecondAvatar $Class /></a>" : $Avatar);
  483. } else {
  484. $URL = STATIC_SERVER.'common/avatars/default.png';
  485. $ToReturn = ($ReturnHTML ? "<img src=\"$URL\" width=\"$Size\" $Style $AvatarMouseOverText$SecondAvatar />" : $URL);
  486. }
  487. break;
  488. case 2:
  489. $ShowAvatar = true;
  490. case 3:
  491. switch (G::$LoggedUser['Identicons']) {
  492. case 0:
  493. $Type = 'identicon';
  494. break;
  495. case 1:
  496. $Type = 'monsterid';
  497. break;
  498. case 2:
  499. $Type = 'wavatar';
  500. break;
  501. case 3:
  502. $Type = 'retro';
  503. break;
  504. case 4:
  505. $Type = '1';
  506. $Robot = true;
  507. break;
  508. case 5:
  509. $Type = '2';
  510. $Robot = true;
  511. break;
  512. case 6:
  513. $Type = '3';
  514. $Robot = true;
  515. break;
  516. default:
  517. $Type = 'identicon';
  518. }
  519. $Rating = 'pg';
  520. if (!isset($Robot) || !$Robot) {
  521. $URL = 'https://secure.gravatar.com/avatar/'.md5(strtolower(trim($Username)))."?s=$Size&amp;d=$Type&amp;r=$Rating";
  522. } else {
  523. $URL = 'https://robohash.org/'.md5($Username)."?set=set$Type&amp;size={$Size}x$Size";
  524. }
  525. if ($ShowAvatar == true && !empty($Avatar)) {
  526. $ToReturn = ($ReturnHTML ? "<img src=\"$Avatar\" width=\"$Size\" $Style $AvatarMouseOverText$SecondAvatar $Class />" : $Avatar);
  527. } else {
  528. $ToReturn = ($ReturnHTML ? "<img src=\"$URL\" width=\"$Size\" $Style $AvatarMouseOverText $Class />" : $URL);
  529. }
  530. break;
  531. default:
  532. $URL = STATIC_SERVER.'common/avatars/default.png';
  533. $ToReturn = ($ReturnHTML ? "<img src=\"$URL\" width=\"$Size\" $Style $AvatarMouseOverText$SecondAvatar $Class/>" : $URL);
  534. }
  535. return $ToReturn;
  536. }
  537. public static function has_avatars_enabled() {
  538. global $HeavyInfo;
  539. return isset($HeavyInfo['DisableAvatars']) && ($HeavyInfo['DisableAvatars'] != 1);
  540. }
  541. /**
  542. * Checks whether user has autocomplete enabled
  543. *
  544. * 0 - Enabled everywhere (default), 1 - Disabled, 2 - Searches only
  545. *
  546. * @param string $Type the type of the input.
  547. * @param boolean $Output echo out HTML
  548. * @return boolean
  549. */
  550. public static function has_autocomplete_enabled($Type, $Output = true) {
  551. $Enabled = false;
  552. if (empty(G::$LoggedUser['AutoComplete'])) {
  553. $Enabled = true;
  554. } elseif (G::$LoggedUser['AutoComplete'] !== 1) {
  555. switch ($Type) {
  556. case 'search':
  557. if (G::$LoggedUser['AutoComplete'] == 2) {
  558. $Enabled = true;
  559. }
  560. break;
  561. case 'other':
  562. if (G::$LoggedUser['AutoComplete'] != 2) {
  563. $Enabled = true;
  564. }
  565. break;
  566. }
  567. }
  568. if ($Enabled && $Output) {
  569. echo ' data-gazelle-autocomplete="true"';
  570. }
  571. if (!$Output) {
  572. // don't return a boolean if you're echoing HTML
  573. return $Enabled;
  574. }
  575. }
  576. /*
  577. * Initiate a password reset
  578. *
  579. * @param int $UserID The user ID
  580. * @param string $Username The username
  581. * @param string $Email The email address
  582. */
  583. public static function reset_password($UserID, $Username, $Email) {
  584. $ResetKey = Users::make_secret();
  585. G::$DB->query("
  586. UPDATE users_info
  587. SET
  588. ResetKey = '" . db_string($ResetKey) . "',
  589. ResetExpires = '" . time_plus(60 * 60) . "'
  590. WHERE UserID = '$UserID'");
  591. require(SERVER_ROOT . '/classes/templates.class.php');
  592. $TPL = NEW TEMPLATE;
  593. $TPL->open(SERVER_ROOT . '/templates/password_reset.tpl'); // Password reset template
  594. $TPL->set('Username', $Username);
  595. $TPL->set('ResetKey', $ResetKey);
  596. $TPL->set('IP', $_SERVER['REMOTE_ADDR']);
  597. $TPL->set('SITE_NAME', SITE_NAME);
  598. $TPL->set('SITE_DOMAIN', SITE_DOMAIN);
  599. Misc::send_email($Email, 'Password reset information for ' . SITE_NAME, $TPL->get(), 'noreply');
  600. }
  601. /*
  602. * Authorize a new location
  603. *
  604. * @param int $UserID The user ID
  605. * @param string $Username The username
  606. * @param int $ASN The ASN
  607. * @param string $Email The email address
  608. */
  609. public static function auth_location($UserID, $Username, $ASN, $Email) {
  610. $AuthKey = Users::make_secret();
  611. G::$Cache->cache_value('new_location_'.$AuthKey, ['UserID'=>$UserID, 'ASN'=>$ASN], 3600*2);
  612. require(SERVER_ROOT . '/classes/templates.class.php');
  613. $TPL = NEW TEMPLATE;
  614. $TPL->open(SERVER_ROOT . '/templates/new_location.tpl');
  615. $TPL->set('Username', $Username);
  616. $TPL->set('AuthKey', $AuthKey);
  617. $TPL->set('IP', $_SERVER['REMOTE_ADDR']);
  618. $TPL->set('SITE_NAME', SITE_NAME);
  619. $TPL->set('SITE_DOMAIN', SITE_DOMAIN);
  620. Misc::send_email($Email, 'Login from new location for '.SITE_NAME, $TPL->get(), 'noreply');
  621. }
  622. /*
  623. * @return array of strings that can be added to next source flag ( [current, old] )
  624. */
  625. public static function get_upload_sources() {
  626. if (!($SourceKey = G::$Cache->get_value('source_key_new'))) {
  627. G::$Cache->cache_value('source_key_new', $SourceKey = [Users::make_secret(), time()]);
  628. }
  629. $SourceKeyOld = G::$Cache->get_value('source_key_old');
  630. if ($SourceKey[1]-time() > 3600) {
  631. G::$Cache->cache_value('source_key_old', $SourceKeyOld = $SourceKey);
  632. G::$Cache->cache_value('source_key_new', $SourceKey = [Users::make_secret(), time()]);
  633. }
  634. G::$DB->query("
  635. SELECT
  636. COUNT(ID)
  637. FROM torrents
  638. WHERE UserID = ".G::$LoggedUser['ID']);
  639. list($Uploads) = G::$DB->next_record();
  640. $Source[0] = SITE_NAME.'-'.substr(hash('sha256', $SourceKey[0].G::$LoggedUser['ID'].$Uploads),0,10);
  641. $Source[1] = $SourceKeyOld ? SITE_NAME.'-'.substr(hash('sha256', $SourceKeyOld[0].G::$LoggedUser['ID'].$Uploads),0,10) : $Source[0];
  642. return $Source;
  643. }
  644. }