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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * - "Quick SQL injection check"
  9. */
  10. class Security
  11. {
  12. /**
  13. * Check integer
  14. *
  15. * Makes sure a number ID is valid,
  16. * e.g., a page ID requested by GET.
  17. */
  18. public function checkInt(...$IDs)
  19. {
  20. foreach ($IDs as $ID) {
  21. if (!ID || !is_int($ID) || $ID < 1) {
  22. error("Expected an integer > 1, got $ID in Security::checkInt.");
  23. }
  24. }
  25. return;
  26. }
  27. /**
  28. * Setup pitfalls
  29. *
  30. * A series of quick sanity checks during app init.
  31. * Previously in classes/script_start.php.
  32. */
  33. public function setupPitfalls()
  34. {
  35. $ENV = ENV::go();
  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. # Bad PHP version
  45. if (version_compare($ENV->PHP_MIN, '7.4.0', '<')) {
  46. error("Gazelle requires PHP > $ENV->PHP_MIN.");
  47. }
  48. # Deal with dumbasses
  49. if (isset($_REQUEST['info_hash']) && isset($_REQUEST['peer_id'])) {
  50. error(
  51. 'd14:failure reason40:Invalid .torrent, try downloading again.e',
  52. $NoHTML = true,
  53. $Debug = false
  54. );
  55. }
  56. return;
  57. }
  58. /**
  59. * UserID checks
  60. *
  61. * @param array $Permissions Permission string
  62. * @param int $UserID Defaults to $_GET['userid'] if none supplied.
  63. * @return int $UserID The working $UserID.
  64. */
  65. public function checkUser($Permissions = [], $UserID = null)
  66. {
  67. /*
  68. if (!$UserID) {
  69. error('$UserID is required.');
  70. }
  71. */
  72. # No Gazelle args passed
  73. if ($_GET['userid'] && empty($UserID)) {
  74. $UserID = $_GET['userid'];
  75. } else {
  76. $UserID = G::$LoggedUser['ID'];
  77. }
  78. # NaN
  79. if (!is_int($UserID) && not_null($UserID)) {
  80. error('$UserID must be an integer.');
  81. }
  82. # $Permissions: string fallback as in View::show_header()
  83. if (is_string($Permissions) && !empty($Permissions)) {
  84. $Permissions = explode(',', $Permissions);
  85. }
  86. # Check each permission and error out if necessary
  87. foreach ($Permissions as $Permission) {
  88. if (!check_perms($Permissions)) {
  89. error(403);
  90. break;
  91. }
  92. }
  93. # If all tests pass
  94. return (int) $UserID;
  95. }
  96. }