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. * - "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(400);
  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. # short_open_tag
  36. if (!ini_get('short_open_tag')) {
  37. error('short_open_tag != On in php.ini');
  38. }
  39. # apcu
  40. if (!extension_loaded('apcu')) {
  41. error('APCu extension not loaded');
  42. }
  43. # Deal with dumbasses
  44. if (isset($_REQUEST['info_hash']) && isset($_REQUEST['peer_id'])) {
  45. error(
  46. 'd14:failure reason40:Invalid .torrent, try downloading again.e',
  47. $NoHTML = true,
  48. $Debug = false
  49. );
  50. }
  51. return;
  52. }
  53. /**
  54. * UserID checks
  55. *
  56. * @param array $Permissions Permission string
  57. * @param int $UserID Defaults to $_GET['userid'] if none supplied.
  58. * @return int $UserID The working $UserID.
  59. */
  60. public function checkUser($Permissions = [], $UserID = null)
  61. {
  62. /*
  63. if (!$UserID) {
  64. error('$UserID is required.');
  65. }
  66. */
  67. # No Gazelle args passed
  68. if ($_GET['userid'] && empty($UserID)) {
  69. $UserID = $_GET['userid'];
  70. } else {
  71. $UserID = G::$LoggedUser['ID'];
  72. }
  73. # NaN
  74. if (!is_int($UserID) && not_null($UserID)) {
  75. error('$UserID must be an integer.');
  76. }
  77. # $Permissions: string fallback as in View::show_header()
  78. if (is_string($Permissions) && !empty($Permissions)) {
  79. $Permissions = explode(',', $Permissions);
  80. }
  81. # Check each permission and error out if necessary
  82. foreach ($Permissions as $Permission) {
  83. if (!check_perms($Permissions)) {
  84. error(403);
  85. break;
  86. }
  87. }
  88. # If all tests pass
  89. return (int) $UserID;
  90. }
  91. }