Helpers.php 3.5 KB

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