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

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($ID)
  19. #public function checkInt(int|array $ID) # Union types need PHP 8 - unbelievable!
  20. {
  21. # Cast single ID to array
  22. if (!is_array($ID)) {
  23. $ID = [$ID];
  24. }
  25. # Check each ID supplied
  26. foreach ($ID as $ID) {
  27. if (!ID || !is_int($ID) || $ID < 1) {
  28. error(400);
  29. }
  30. }
  31. return;
  32. }
  33. /**
  34. * Setup pitfalls
  35. *
  36. * A series of quick sanity checks during app init.
  37. * Previously in classes/script_start.php.
  38. */
  39. public function setupPitfalls()
  40. {
  41. # short_open_tag
  42. if (!ini_get('short_open_tag')) {
  43. error('short_open_tag != On in php.ini');
  44. }
  45. # apcu
  46. if (!extension_loaded('apcu')) {
  47. error('APCu extension not loaded');
  48. }
  49. # Deal with dumbasses
  50. if (isset($_REQUEST['info_hash']) && isset($_REQUEST['peer_id'])) {
  51. error(
  52. 'd14:failure reason40:Invalid .torrent, try downloading again.e',
  53. $NoHTML = true,
  54. $Debug = false
  55. );
  56. }
  57. return;
  58. }
  59. /**
  60. * UserID checks
  61. *
  62. * @param array $Permissions Permission string
  63. * @param int $UserID Defaults to $_GET['userid'] if none supplied.
  64. * @return int $UserID The working $UserID.
  65. */
  66. public function checkUser($Permissions = [], $UserID = null)
  67. {
  68. /*
  69. if (!$UserID) {
  70. error('$UserID is required.');
  71. }
  72. */
  73. # No Gazelle args passed
  74. if ($_GET['userid'] && empty($UserID)) {
  75. $UserID = $_GET['userid'];
  76. } else {
  77. $UserID = G::$LoggedUser['ID'];
  78. }
  79. # NaN
  80. if (!is_int($UserID) && not_null($UserID)) {
  81. error('$UserID must be an integer.');
  82. }
  83. # $Permissions: string fallback as in View::show_header()
  84. if (is_string($Permissions) && !empty($Permissions)) {
  85. $Permissions = explode(',', $Permissions);
  86. }
  87. # Check each permission and error out if necessary
  88. foreach ($Permissions as $Permission) {
  89. if (!check_perms($Permissions)) {
  90. error(403);
  91. break;
  92. }
  93. }
  94. # If all tests pass
  95. return (int) $UserID;
  96. }
  97. }