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.

security.class.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * Security
  5. *
  6. * Designed to hold common authentication functions from various sources:
  7. * - classes/script_start.php
  8. */
  9. class Security
  10. {
  11. /**
  12. * Check ID
  13. *
  14. * Makes sure a number ID is valid,
  15. * e.g., a page ID requested by GET.
  16. */
  17. public function CheckID($ID)
  18. {
  19. # Temporary failsafe
  20. # (int) 'dingus' = 0
  21. # (int) 3.14 = 3
  22. $ID = (int) $ID;
  23. if (!is_int($ID) || $ID < 1) {
  24. error(400);
  25. }
  26. return;
  27. }
  28. /**
  29. * Setup pitfalls
  30. *
  31. * A series of quick sanity checks during app init.
  32. * Previously in classes/script_start.php.
  33. */
  34. public function SetupPitfalls()
  35. {
  36. # short_open_tag
  37. if (!ini_get('short_open_tag')) {
  38. error('short_open_tag != On in php.ini');
  39. }
  40. # apcu
  41. if (!extension_loaded('apcu')) {
  42. error('APCu extension not loaded');
  43. }
  44. # Deal with dumbasses
  45. if (isset($_REQUEST['info_hash']) && isset($_REQUEST['peer_id'])) {
  46. error(
  47. 'd14:failure reason40:Invalid .torrent, try downloading again.e',
  48. $NoHTML = true,
  49. $Debug = false
  50. );
  51. }
  52. return;
  53. }
  54. /**
  55. * UserID checks
  56. *
  57. * @param array $Permissions Permission string
  58. * @param int $UserID Defaults to $_GET['userid'] if none supplied.
  59. * @return int $UserID The working $UserID.
  60. */
  61. public function checkUser($Permissions = [], $UserID = null)
  62. {
  63. /*
  64. if (!$UserID) {
  65. error('$UserID is required.');
  66. }
  67. */
  68. # No Gazelle args passed
  69. if ($_GET['userid'] && empty($UserID)) {
  70. $UserID = $_GET['userid'];
  71. } else {
  72. $UserID = G::$LoggedUser['ID'];
  73. }
  74. # NaN
  75. if (!is_int($UserID) && not_null($UserID)) {
  76. error('$UserID must be an integer.');
  77. }
  78. # $Permissions: string fallback as in View::show_header()
  79. if (is_string($Permissions) && !empty($Permissions)) {
  80. $Permissions = explode(',', $Permissions);
  81. }
  82. # Check each permission and error out if necessary
  83. foreach ($Permissions as $Permission) {
  84. if (!check_perms($Permissions)) {
  85. error(403);
  86. break;
  87. }
  88. }
  89. # If all tests pass
  90. return (int) $UserID;
  91. }
  92. }