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

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