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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. return "Expected an integer > 1, got $ID in Security::checkInt.";
  23. #error("Expected an integer > 1, got $ID in Security::checkInt.");
  24. }
  25. }
  26. return;
  27. }
  28. /**
  29. * Setup pitfalls
  30. *
  31. * A series of quick sanity checks during app init.
  32. * Previously in classes/script_start.php.
  33. */
  34. public function setupPitfalls()
  35. {
  36. $ENV = ENV::go();
  37. # Bad PHP version
  38. if (version_compare(PHP_VERSION, $ENV->PHP_MIN, '<')) {
  39. error("Gazelle requires PHP > $ENV->PHP_MIN.");
  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 php-apcu not loaded.');
  48. }
  49. # mbstring
  50. if (!extension_loaded('mbstring')) {
  51. error('Multibyte string extension php-mbstring not loaded.');
  52. }
  53. # memcache
  54. if (!extension_loaded('memcache')) {
  55. error('memcached extension php-memcache not loaded.');
  56. }
  57. # mysqli
  58. if (!extension_loaded('mysqli')) {
  59. error('mysqli extension php-mysql not loaded.');
  60. }
  61. # blake3
  62. if ($ENV->FEATURE_BIOPHP && !extension_loaded('blake3')) {
  63. error('Please install and enable the php-blake3 extension.');
  64. }
  65. # Deal with dumbasses
  66. if (isset($_REQUEST['info_hash']) || isset($_REQUEST['peer_id'])) {
  67. error(
  68. 'd14:failure reason40:Invalid .torrent, try downloading again.e',
  69. $NoHTML = true,
  70. $Debug = false
  71. );
  72. }
  73. return;
  74. }
  75. /**
  76. * UserID checks
  77. *
  78. * @param array $Permissions Permission string
  79. * @param int $UserID Defaults to $_GET['userid'] if none supplied.
  80. * @return int $UserID The working $UserID.
  81. */
  82. public function checkUser($Permissions = [], $UserID = null)
  83. {
  84. /*
  85. if (!$UserID) {
  86. error('$UserID is required.');
  87. }
  88. */
  89. # No Gazelle args passed
  90. if ($_GET['userid'] && empty($UserID)) {
  91. $UserID = $_GET['userid'];
  92. } else {
  93. $UserID = G::$LoggedUser['ID'];
  94. }
  95. # NaN
  96. if (!is_int($UserID) && not_null($UserID)) {
  97. error('$UserID must be an integer.');
  98. }
  99. # $Permissions: string fallback as in View::show_header()
  100. if (is_string($Permissions) && !empty($Permissions)) {
  101. $Permissions = explode(',', $Permissions);
  102. }
  103. # Check each permission and error out if necessary
  104. foreach ($Permissions as $Permission) {
  105. if (!check_perms($Permissions)) {
  106. error(403);
  107. break;
  108. }
  109. }
  110. # If all tests pass
  111. return (int) $UserID;
  112. }
  113. }