1
0

api.php 4.8 KB

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