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

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