PublicApi.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace app\api\controller;
  3. trait PublicApi
  4. {
  5. public function check_config()
  6. {
  7. if ($GLOBALS['config']['api']['publicapi']['status'] != 1) {
  8. echo 'closed';
  9. die;
  10. }
  11. if ($GLOBALS['config']['api']['publicapi']['charge'] == 1) {
  12. $h = $_SERVER['REMOTE_ADDR'];
  13. if (!$h) {
  14. echo lang('api/auth_err');
  15. exit;
  16. } else {
  17. $auth = $GLOBALS['config']['api']['publicapi']['auth'];
  18. $this->checkDomainAuth($auth);
  19. }
  20. }
  21. }
  22. private function checkDomainAuth($auth)
  23. {
  24. $ip = mac_get_client_ip();
  25. $auth_list = ['127.0.0.1'];
  26. if (!empty($auth)) {
  27. foreach (explode('#', $auth) as $domain) {
  28. $domain = trim($domain);
  29. $auth_list[] = $domain;
  30. if (!mac_string_is_ip($domain)) {
  31. $auth_list[] = gethostbyname($domain);
  32. }
  33. }
  34. $auth_list = array_unique($auth_list);
  35. $auth_list = array_filter($auth_list);
  36. }
  37. if (!in_array($ip, $auth_list)) {
  38. echo lang('api/auth_err');
  39. exit;
  40. }
  41. }
  42. protected function format_sql_string($str)
  43. {
  44. $str = preg_replace('/\b(SELECT|INSERT|UPDATE|DELETE|DROP|UNION|WHERE|FROM|JOIN|INTO|VALUES|SET|AND|OR|NOT|EXISTS|HAVING|GROUP BY|ORDER BY|LIMIT|OFFSET)\b/i', '', $str);
  45. $str = preg_replace('/[^\w\s\-\.]/', '', $str);
  46. $str = trim(preg_replace('/\s+/', ' ', $str));
  47. return $str;
  48. }
  49. }