api.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. define('BX_API', true);
  3. require_once('./inc/header.inc.php');
  4. require_once(BX_DIRECTORY_PATH_INC . "profiles.inc.php");
  5. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  6. header('Content-Type: application/json');
  7. bx_api_check_access();
  8. if (bx_get('cnf') ){
  9. $sCnf = getParam('sys_api_config');
  10. echo json_encode(['data' => $sCnf, 'hash' => md5($sCnf)]);
  11. exit();
  12. }
  13. if(($sGooType = bx_get('goo')) !== false && ($oLf = BxDolLocationField::getObjectInstance('sys_google')) !== false) {
  14. $aResponse = false;
  15. switch($sGooType) {
  16. case 'list':
  17. if(($sValue = bx_get('input')) === false)
  18. break;
  19. $aResponse = $oLf->getAutocomplete($sValue);
  20. break;
  21. case 'place':
  22. if(($sPlaceId = bx_get('place_id')) === false)
  23. break;
  24. $aResponse = $oLf->getPlace($sPlaceId);
  25. if(empty($aResponse) || !is_array($aResponse))
  26. break;
  27. $aLocation = $aResponse['geometry']['location'];
  28. $aAddress = $aResponse['address_components'];
  29. $oFind = function($sType) use ($aAddress) {
  30. foreach($aAddress as $aItem) {
  31. if(in_array($sType, $aItem['types']))
  32. return trim($aItem['short_name']);
  33. }
  34. return '';
  35. };
  36. $aResponse = [
  37. 'lat' => $aLocation['lat'],
  38. 'lng' => $aLocation['lng'],
  39. 'location_string'=> $aResponse['formatted_address'],
  40. 'country' => $oFind('country'),
  41. 'state' => $oFind('administrative_area_level_1'),
  42. 'city' => $oFind('locality')?: $oFind('administrative_area_level_2'),
  43. 'street' => $oFind('route'),
  44. 'street_number' => $oFind('street_number'),
  45. 'zip' => $oFind('postal_code'),
  46. ];
  47. break;
  48. }
  49. if(!$aResponse)
  50. $aResponse = ['error' => _t('_sys_location_undefined')];
  51. echo json_encode($aResponse);
  52. exit;
  53. }
  54. $aRequest = isset($_GET['r']) ? explode('/', $_GET['r']) : [];
  55. $a = ['sModule', 'sMethod', 'sClass'];
  56. foreach ($aRequest as $i => $v)
  57. if (isset($a[$i]) && preg_match('/^[A-Za-z0-9_-]+$/', $v))
  58. ${$a[$i]} = $v;
  59. if (!isset($sClass))
  60. $sClass = 'Module';
  61. foreach ($a as $v) {
  62. if (!isset($$v)) {
  63. header('HTTP/1.0 404 Not Found');
  64. header('Status: 404 Not Found');
  65. BxDolLanguages::getInstance();
  66. echo json_encode(['status' => 404, 'error' => _t("_sys_request_page_not_found_cpt")]);
  67. exit;
  68. }
  69. }
  70. // check if service exists
  71. if (!BxDolRequest::serviceExists($sModule, $sMethod, $sClass)) {
  72. header('HTTP/1.0 404 Not Found');
  73. header('Status: 404 Not Found');
  74. BxDolLanguages::getInstance();
  75. echo json_encode(['status' => 404, 'error' => _t("_sys_request_page_not_found_cpt")]);
  76. exit;
  77. }
  78. // check if service is safe
  79. if (!getParam('sys_api_access_unsafe_services')) {
  80. $b = BxDolRequest::serviceExists($sModule, 'is_safe_service', 'system' == $sModule ? 'TemplServices' : 'Module');
  81. $bSafe = $b ? BxDolService::call($sModule, 'is_safe_service', array($sMethod), 'system' == $sModule ? 'TemplServices' : 'Module') : false;
  82. $bPublic = false;
  83. if (!$bSafe) {
  84. $b = BxDolRequest::serviceExists($sModule, 'is_public_service', 'system' == $sModule ? 'TemplServices' : 'Module');
  85. $bPublic = $b ? BxDolService::call($sModule, 'is_public_service', array($sMethod), 'system' == $sModule ? 'TemplServices' : 'Module') : false;
  86. }
  87. if (!$bPublic && !$bSafe) {
  88. header('HTTP/1.0 403 Forbidden');
  89. header('Status: 403 Forbidden');
  90. echo json_encode(['status' => 403, 'error' => _t("_Access denied")]);
  91. exit;
  92. }
  93. }
  94. // call service
  95. if (!($aParams = bx_get('params')))
  96. $aParams = array();
  97. elseif (is_string($aParams) && preg_match('/^\[.*\]$/', $aParams))
  98. $aParams = @json_decode($aParams, true);
  99. if (!is_array($aParams))
  100. $aParams = array($aParams);
  101. $mixedRet = BxDolService::call($sModule, $sMethod, $aParams, $sClass);
  102. // check output and return JSON
  103. if (is_array($mixedRet) && isset($mixedRet['error'])) {
  104. header('HTTP/1.0 500 Internal Server Error');
  105. header('Status: 500 Internal Server Error');
  106. $a = [
  107. 'status' => isset($mixedRet['code']) ? $mixedRet['code'] : 500,
  108. 'error' => isset($mixedRet['desc']) ? $mixedRet['desc'] : $mixedRet['error'],
  109. 'data' => isset($mixedRet['data']) ? $mixedRet['data'] : '',
  110. ];
  111. if (isset($mixedRet['code']))
  112. $a['code'] = $mixedRet['code'];
  113. echo json_encode($a);
  114. exit;
  115. }
  116. $aRv = [
  117. 'status' => 200,
  118. 'module' => $sModule,
  119. 'method' => $sMethod,
  120. 'params' => $aParams,
  121. 'data' => $mixedRet,
  122. 'hash' => md5(getParam('sys_api_config')),
  123. ];
  124. echo json_encode($aRv);