Helpers.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. use app\Hajeebtok;
  3. use Pecee\SimpleRouter\SimpleRouter as Router;
  4. use Pecee\Http\Url;
  5. use Pecee\Http\Response;
  6. use Pecee\Http\Request;
  7. /**
  8. * Get url for a route by using either name/alias, class or method name.
  9. *
  10. * The name parameter supports the following values:
  11. * - Route name
  12. * - Controller/resource name (with or without method)
  13. * - Controller class name
  14. *
  15. * When searching for controller/resource by name, you can use this syntax "route.name@method".
  16. * You can also use the same syntax when searching for a specific controller-class "MyController@home".
  17. * If no arguments is specified, it will return the url for the current loaded route.
  18. *
  19. * @param string|null $name
  20. * @param string|array|null $parameters
  21. * @param array|null $getParams
  22. * @return \Pecee\Http\Url
  23. * @throws \InvalidArgumentException
  24. */
  25. function url(?string $name = null, $parameters = null, ?array $getParams = null): Url
  26. {
  27. return Router::getUrl($name, $parameters, $getParams);
  28. }
  29. /**
  30. * @return \Pecee\Http\Response
  31. */
  32. function response(): Response
  33. {
  34. return Router::response();
  35. }
  36. /**
  37. * @return \Pecee\Http\Request
  38. */
  39. function request(): Request
  40. {
  41. return Router::request();
  42. }
  43. /**
  44. * Get input class
  45. * @param string|null $index Parameter index name
  46. * @param string|mixed|null $defaultValue Default return value
  47. * @param array ...$methods Default methods
  48. * @return \Pecee\Http\Input\InputHandler|array|string|null
  49. */
  50. function input($index = null, $defaultValue = null, ...$methods)
  51. {
  52. if ($index !== null) {
  53. return request()->getInputHandler()->value($index, $defaultValue, ...$methods);
  54. }
  55. return request()->getInputHandler();
  56. }
  57. /**
  58. * @param string $url
  59. * @param int|null $code
  60. */
  61. function redirect(string $url, ?int $code = null): void
  62. {
  63. if ($code !== null) {
  64. response()->httpCode($code);
  65. }
  66. response()->redirect($url);
  67. }
  68. /**
  69. * Get current csrf-token
  70. * @return string|null
  71. */
  72. function csrf_token(): ?string
  73. {
  74. $baseVerifier = Router::router()->getCsrfVerifier();
  75. if ($baseVerifier !== null) {
  76. return $baseVerifier->getTokenProvider()->getToken();
  77. }
  78. return null;
  79. }
  80. /**
  81. * @param array $data
  82. * @return string
  83. */
  84. function api_json(array $data): string
  85. {
  86. header("Content-Type: application/json");
  87. return json_encode($data);
  88. }
  89. function signed_in(Request $request): bool
  90. {
  91. $authorizationHeader = $request->getHeader("Authorization");
  92. $token = explode(" ", $authorizationHeader)[1];
  93. $tokenData = Hajeebtok::$Database->Row("SELECT * FROM sessions WHERE token = :token", ["token" => $token]);
  94. return !empty($tokenData);
  95. }
  96. function get_token_id(Request $request): int
  97. {
  98. $authorizationHeader = $request->getHeader("Authorization");
  99. $token = explode(" ", $authorizationHeader)[1];
  100. $tokenData = Hajeebtok::$Database->Row("SELECT * FROM sessions WHERE token = :token", ["token" => $token]);
  101. if(!empty($tokenData)) return $tokenData["account_id"];
  102. return 0;
  103. }