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

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