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.

42 lines
966 B

  1. <?php
  2. error_reporting(E_ALL ^ E_DEPRECATED);
  3. function json_response(&$app, $response, $code=200) {
  4. $app->response()->status($code);
  5. $app->response()['Content-Type'] = 'application/json';
  6. $app->response()->body(json_encode($response));
  7. }
  8. function k($a, $k, $default=null) {
  9. if(is_array($k)) {
  10. $result = true;
  11. foreach($k as $key) {
  12. $result = $result && array_key_exists($key, $a);
  13. }
  14. return $result;
  15. } else {
  16. if(is_array($a) && array_key_exists($k, $a) && $a[$k])
  17. return $a[$k];
  18. elseif(is_object($a) && property_exists($a, $k) && $a->$k)
  19. return $a->$k;
  20. else
  21. return $default;
  22. }
  23. }
  24. function is_authenticated($params) {
  25. if(!isset($params['token']))
  26. return false;
  27. $token_file = __DIR__.'/../data/apikeys.txt';
  28. if(!file_exists($token_file))
  29. return false;
  30. $valid_tokens = array_filter(file($token_file));
  31. if(in_array($params['token'], $valid_tokens))
  32. return true;
  33. return false;
  34. }