PageRenderTime 97ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/xenforo/library/bdApi/Data/Helper/Core.php

https://gitlab.com/billyprice1/bdApi
PHP | 181 lines | 112 code | 26 blank | 43 comment | 23 complexity | 653177700acebd5a2321f3c6a2434385 MD5 | raw file
  1. <?php
  2. class bdApi_Data_Helper_Core
  3. {
  4. /**
  5. * Adds system information into response data (both XML and JSON)
  6. *
  7. * @param array $data
  8. */
  9. public static function addDefaultResponse(array &$data)
  10. {
  11. if (XenForo_Application::debugMode()) {
  12. $data['debug'] = XenForo_Debug::getDebugTemplateParams();
  13. if (!empty($data['debug']['debug_url'])) {
  14. $data['debug']['debug_url'] = self::safeConvertApiUriToAbsoluteUri($data['debug']['debug_url'], true);
  15. }
  16. $session = self::safeGetSession();
  17. if (!empty($session)) {
  18. $clientId = $session->getOAuthClientId();
  19. if (!empty($clientId)) {
  20. $data['debug']['client_id'] = $clientId;
  21. $oauthToken = $session->getOAuthTokenText();;
  22. if (!empty($oauthToken)) {
  23. $data['debug']['oauth_token'] = $oauthToken;
  24. }
  25. }
  26. $languageId = $session->get('languageId');
  27. if (!empty($languageId)) {
  28. $data['debug']['language_id'] = $languageId;
  29. }
  30. }
  31. }
  32. if (XenForo_Visitor::getUserId() > 0) {
  33. $data['system_info']['visitor_id'] = XenForo_Visitor::getUserId();
  34. $data['system_info']['time'] = XenForo_Application::$time;
  35. }
  36. }
  37. /**
  38. * Builds and adds the navigation for api data
  39. *
  40. * @param XenForo_Input $input
  41. * @param array $data
  42. * @param int $perPage
  43. * @param int $totalItems
  44. * @param int $page
  45. * @param string $linkType
  46. * @param mixed $linkData
  47. * @param array $linkParams
  48. * @param array $options
  49. */
  50. public static function addPageLinks(XenForo_Input $input, array &$data, $perPage, $totalItems, $page, $linkType, $linkData = null, array $linkParams = array(), array $options = array())
  51. {
  52. if (empty($perPage)) {
  53. return;
  54. }
  55. $pageNav = array();
  56. $inputData = $input->filter(array(
  57. 'fields_include' => XenForo_Input::STRING,
  58. 'fields_exclude' => XenForo_Input::STRING,
  59. ));
  60. if (!empty($inputData['fields_include'])) {
  61. $linkParams['fields_include'] = $inputData['fields_include'];
  62. } elseif (!empty($inputData['fields_exclude'])) {
  63. $linkParams['fields_exclude'] = $inputData['fields_exclude'];
  64. }
  65. if (empty($page)) {
  66. $page = 1;
  67. }
  68. $pageNav['pages'] = ceil($totalItems / $perPage);
  69. if ($pageNav['pages'] <= 1) {
  70. // do not do anything if there is only 1 page (or no pages)
  71. return;
  72. }
  73. $pageNav['page'] = $page;
  74. if ($page > 1) {
  75. // a previous link should only be added if we are not at page 1
  76. $pageNav['prev'] = bdApi_Data_Helper_Core::safeBuildApiLink($linkType, $linkData, array_merge($linkParams, array('page' => $page - 1)));
  77. }
  78. if ($page < $pageNav['pages']) {
  79. // a next link should only be added if we are not at the last page
  80. $pageNav['next'] = bdApi_Data_Helper_Core::safeBuildApiLink($linkType, $linkData, array_merge($linkParams, array('page' => $page + 1)));
  81. }
  82. // add the page navigation into `links`
  83. // the data may have existing links or not
  84. // we simply don't care
  85. if (empty($data['links'])) {
  86. $data['links'] = array();
  87. }
  88. $data['links'] = array_merge($data['links'], $pageNav);
  89. }
  90. /**
  91. * Filters data into another array with value from specified keys only
  92. *
  93. * @param array $data
  94. * @param array $publicKeys
  95. * @return array
  96. */
  97. public static function filter(array $data, array $publicKeys)
  98. {
  99. $filteredData = array();
  100. foreach ($publicKeys as $publicKey => $mappedKey) {
  101. if (is_int($publicKey)) {
  102. // backward compatible with previous versions
  103. // where $publicKeys is just an array of keys
  104. // (with no key value pair)
  105. $publicKey = $mappedKey;
  106. }
  107. if (isset($data[$publicKey])) {
  108. $filteredData[$mappedKey] = $data[$publicKey];
  109. }
  110. }
  111. return $filteredData;
  112. }
  113. /**
  114. * @return bdApi_Session|null
  115. */
  116. public static function safeGetSession()
  117. {
  118. if (XenForo_Application::isRegistered('_bdApi_session')) {
  119. return XenForo_Application::get('_bdApi_session');
  120. }
  121. if (XenForo_Application::isRegistered('session')) {
  122. $session = XenForo_Application::getSession();
  123. if ($session instanceof bdApi_Session) {
  124. return $session;
  125. }
  126. }
  127. return null;
  128. }
  129. /**
  130. * @return string
  131. */
  132. public static function safeBuildApiLink()
  133. {
  134. $args = func_get_args();
  135. $func = array('XenForo_Link', 'buildApiLink');
  136. if (is_callable($func)) {
  137. return call_user_func_array($func, $args);
  138. } else {
  139. return '';
  140. }
  141. }
  142. /**
  143. * @return string
  144. */
  145. public static function safeConvertApiUriToAbsoluteUri()
  146. {
  147. $args = func_get_args();
  148. $func = array('XenForo_Link', 'convertApiUriToAbsoluteUri');
  149. if (is_callable($func)) {
  150. return call_user_func_array($func, $args);
  151. } else {
  152. return '';
  153. }
  154. }
  155. }