1
0

api.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. require_once('./inc/header.inc.php');
  3. require_once(BX_DIRECTORY_PATH_INC . "profiles.inc.php");
  4. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  5. define('BX_API', true);
  6. header('Content-Type: application/json');
  7. bx_api_check_access();
  8. $aRequest = isset($_GET['r']) ? explode('/', $_GET['r']) : [];
  9. $a = ['sModule', 'sMethod', 'sClass'];
  10. foreach ($aRequest as $i => $v)
  11. if (isset($a[$i]) && preg_match('/^[A-Za-z0-9_-]+$/', $v))
  12. ${$a[$i]} = $v;
  13. if (!isset($sClass))
  14. $sClass = 'Module';
  15. foreach ($a as $v) {
  16. if (!isset($$v)) {
  17. header('HTTP/1.0 404 Not Found');
  18. header('Status: 404 Not Found');
  19. echo json_encode(['status' => 404, 'error' => _t("_sys_request_page_not_found_cpt")]);
  20. exit;
  21. }
  22. }
  23. if (!BxDolRequest::serviceExists($sModule, $sMethod, $sClass)) {
  24. header('HTTP/1.0 404 Not Found');
  25. header('Status: 404 Not Found');
  26. echo json_encode(['status' => 404, 'error' => _t("_sys_request_page_not_found_cpt")]);
  27. exit;
  28. }
  29. if (!($aParams = bx_get('params')))
  30. $aParams = array();
  31. elseif (is_string($aParams) && preg_match('/^\[.*\]$/', $aParams))
  32. $aParams = @json_decode($aParams);
  33. if (!is_array($aParams))
  34. $aParams = array($aParams);
  35. $mixedRet = BxDolService::call($sModule, $sMethod, $aParams, $sClass);
  36. if (is_array($mixedRet) && isset($mixedRet['error'])) {
  37. header('HTTP/1.0 500 Internal Server Error');
  38. header('Status: 500 Internal Server Error');
  39. $a = [
  40. 'status' => isset($mixedRet['code']) ? $mixedRet['code'] : 500,
  41. 'error' => isset($mixedRet['desc']) ? $mixedRet['desc'] : $mixedRet['error'],
  42. 'all' => $mixedRet,
  43. ];
  44. if (isset($mixedRet['code']))
  45. $a['code'] = $mixedRet['code'];
  46. echo json_encode($a);
  47. exit;
  48. }
  49. echo json_encode([
  50. 'status' => 200,
  51. 'module' => $sModule,
  52. 'method' => $sMethod,
  53. 'params' => $aParams,
  54. 'data' => $mixedRet,
  55. ]);