Guest User

test

a guest
Oct 21st, 2021
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. <?php
  2.  
  3. // INIT
  4.  
  5. require('./../cfg/general.inc.php');
  6. require('./../includes/core/functions.php');
  7.  
  8. class_autoload(true);
  9.  
  10. DB::connect();
  11. HTML::$compile_dir = '../' . HTML::$compile_dir;
  12.  
  13. // URL
  14.  
  15. // vars
  16.  
  17. $result = [];
  18. $query = [];
  19. $path = '';
  20. $method = $_SERVER['REQUEST_METHOD'];
  21.  
  22. // headers
  23.  
  24. $headers = getallheaders();
  25. $project = $headers['project'] ?? '';
  26. $token = $headers['token'] ?? '';
  27. $v = $headers['v'] ?? 0;
  28.  
  29. header('Content-Type: application/json');
  30. header('X-Content-Type-Options: nosniff');
  31.  
  32. // path
  33.  
  34. $url = $_SERVER['REQUEST_URI'];
  35. $url = preg_replace('~^/api/~i', '', $url);
  36. $url = explode('?', $url);
  37. $path = isset($url[0]) && $url[0] ? flt_input($url[0]) : '';
  38.  
  39. // query
  40.  
  41. if ($method == 'GET') isset($url[1]) ? parse_str($url[1], $query_raw) : $query_raw = [];
  42. else $query_raw = json_decode(file_get_contents('php://input'), true);
  43. if (!$query_raw && $_POST) $query_raw = $_POST;
  44. if (is_array($query_raw)) foreach ($query_raw as $key => $value) $query[flt_input($key)] = flt_input($value);
  45.  
  46. // ROUTES
  47.  
  48. error_log($method);
  49. error_log($path);
  50. error_log($token);
  51. error_log(json_encode($query, JSON_UNESCAPED_UNICODE));
  52.  
  53. // validate
  54.  
  55. if (!$v) response(error_response(1002, 'Invalid request: v (version API) is required'));
  56. else if ($v != 1) response(error_response(1002, 'Invalid request: v (version API) is incorrect'));
  57.  
  58. if (!$project) response(error_response(1002, 'Invalid request: project is required'));
  59. else if (!in_array($project, ['copybro', 'mafin'])) response(error_response(1002, 'Invalid request: project is incorrect'));
  60.  
  61. // routes
  62.  
  63. if ($path == 'auth.sendCode') call('POST', $method, $query, 'Session::phone_code');
  64. else if ($path == 'auth.confirmCode') call('POST', $method, $query, 'Session::phone_confirm');
  65. else {
  66. // validate
  67. if (!$token) response(error_response(1001, 'User authorization failed: no access token passed.'));
  68. // session
  69. $response = Session::init(2, ['token' => $token]);
  70. if (isset($response['error_code'])) response($response);
  71. // routes (auth)
  72. if ($path == 'auth.logout') call('POST', $method, NULL, 'Session::logout');
  73. // routes (users)
  74. // your methods here ...
  75. // routes (not found)
  76. response(error_response(1002, 'Application authorization failed: method is unavailable with service token.'));
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment