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

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