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

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