Simple PHP api for SnipeIT app. Not maintained anymore; check out BinStack instead: https://source.netsyms.com/Business/BinStack and https://netsyms.biz/binstack
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.

required.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. ob_start(); // No worries about sending headers before/after content
  3. session_start();
  4. require 'vendor/autoload.php'; // Load database stuff from Composer
  5. require 'database.php'; // Load database settings
  6. define('JSON', true); // Don't touch this or Something Bad might happen.
  7. header('Content-Type: application/json'); // Don't touch this either.
  8. // Completely disable CORS stuff, everything is allowed. You could change this
  9. // if you know exactly what domain traffic is coming from.
  10. header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
  11. header('Access-Control-Allow-Credentials: true');
  12. /**
  13. * Check if a user exists in the database by username.
  14. * @param String $username
  15. */
  16. function username_exists($username) {
  17. global $database;
  18. return $database->has('users', ['username' => $username]);
  19. }
  20. /**
  21. * Check if a user exists in the database by email.
  22. * @param String $username
  23. */
  24. function email_exists($email) {
  25. global $database;
  26. return $database->has('users', ['email' => $email]);
  27. }
  28. /**
  29. * Checks the given credentials against the database.
  30. * Can use either email or username for identity.
  31. * @param string $username
  32. * @param string $password
  33. * @return boolean True if OK, else false
  34. */
  35. function authenticate_user($username, $password) {
  36. global $database;
  37. $qf = 'username';
  38. if (!username_exists($username)) {
  39. return false;
  40. }
  41. $hash = $database->select('users', ['password'], [$qf => $username])[0]['password'];
  42. return (password_verify($password, $hash));
  43. }
  44. /**
  45. * Checks if a string or whatever is empty.
  46. * @param $str The thingy to check
  47. * @return boolean True if it's empty or whatever.
  48. */
  49. function is_empty($str) {
  50. return (!isset($str) || $str == '' || $str == null);
  51. }
  52. /**
  53. * Send a generic OK message.
  54. * @param string $message Optional message text.
  55. * @param boolean $die End execution after sending message (default true).
  56. */
  57. function sendOK($message = "", $die = true) {
  58. if (!is_empty($message) && JSON) {
  59. echo '{ "status": "OK", "message": "' . $message . '" }';
  60. } elseif (is_empty($message) && JSON) {
  61. echo '{ "status": "OK" }';
  62. } elseif (!is_empty($message) && !JSON) {
  63. echo "OK:$message";
  64. } else {
  65. echo "OK";
  66. }
  67. if ($die) {
  68. die();
  69. }
  70. }
  71. /**
  72. * Send an error message.
  73. * @param string $error Error text.
  74. * @param boolean $die End execution after sending error (default true).
  75. */
  76. function sendError($error, $die = true) {
  77. if (JSON) {
  78. echo '{ "status": "ERROR", "message": "' . $error . '" }';
  79. } else {
  80. echo "Error: $error";
  81. }
  82. if ($die) {
  83. die();
  84. }
  85. }