PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/xenforo/library/bdApi/Link.php

https://gitlab.com/billyprice1/bdApi
PHP | 183 lines | 128 code | 33 blank | 22 comment | 29 complexity | e8d8eb16f8f143ef3b00c3f365d6dfbe MD5 | raw file
  1. <?php
  2. /* @var $app XenForo_Application */
  3. $app = XenForo_Application::getInstance();
  4. $xenforoLinkPath = $app->getRootDir() . '/library/XenForo/Link.php';
  5. $xenforoLinkContents = file_get_contents($xenforoLinkPath);
  6. // remove <?php
  7. $xenforoLinkContents = substr($xenforoLinkContents, 5);
  8. // rename class
  9. $xenforoLinkContents = str_replace('class XenForo_Link', 'class _XenForo_Link', $xenforoLinkContents);
  10. // change self reference to XenForo, required to patch https issue
  11. $xenforoLinkContents = str_replace('self::', 'XenForo_Link::', $xenforoLinkContents);
  12. eval($xenforoLinkContents);
  13. class bdApi_Link extends _XenForo_Link
  14. {
  15. public function __construct($linkString, $canPrependFull = true)
  16. {
  17. if ($canPrependFull) {
  18. // we have to verify this because caller may not know that all relative urls are forced to absolute by us
  19. $canPrependFull = !Zend_Uri::check($linkString);
  20. }
  21. parent::__construct($linkString, $canPrependFull);
  22. }
  23. public static function buildPublicLink($type, $data = null, array $extraParams = array(), $skipPrepend = false)
  24. {
  25. // the type MUST BE canonical:$type
  26. // NOTE: this is the opposite with api links
  27. if (strpos($type, 'full:') === 0) {
  28. // replace full: with canonical:
  29. $type = str_replace('full:', 'canonical:', $type);
  30. } elseif (strpos($type, 'canonical:') === false) {
  31. // enforce canonical:
  32. $type = 'canonical:' . $type;
  33. }
  34. $session = bdApi_Data_Helper_Core::safeGetSession();
  35. if (!empty($session)) {
  36. // auto appends locale param from session
  37. if (!isset($extraParams['locale'])) {
  38. $locale = $session->get('requestLocale');
  39. if (!empty($locale)) {
  40. $timestamp = time() + 86400;
  41. $extraParams['_apiLanguageId'] = sprintf('%s %s', $timestamp,
  42. bdApi_Crypt::encryptTypeOne($session->get('languageId'), $timestamp)
  43. );
  44. }
  45. }
  46. }
  47. return parent::buildPublicLink($type, $data, $extraParams, $skipPrepend);
  48. }
  49. public static function buildApiLink($type, $data = null, array $extraParams = array(), $skipPrepend = false)
  50. {
  51. // the type MUST BE full:type
  52. // NOTE: this is the opposite with public links
  53. if (strpos($type, 'canonical:') === 0) {
  54. // replace canonical: with full:
  55. $type = str_replace('canonical:', 'full:', $type);
  56. } elseif (strpos($type, 'full:') === false) {
  57. // enforce full:
  58. $type = 'full:' . $type;
  59. }
  60. $session = bdApi_Data_Helper_Core::safeGetSession();
  61. if (!empty($session)) {
  62. // auto appends oauth_token param from the session
  63. if (!isset($extraParams['oauth_token'])) {
  64. $oauthToken = $session->getOAuthTokenText();
  65. if (!empty($oauthToken)
  66. && !empty($_REQUEST['oauth_token'])
  67. && $_REQUEST['oauth_token'] === $oauthToken
  68. ) {
  69. // only append token to built link if the current request has token in query too
  70. // this will prevent token in links if it's requested with OTT, token in Auth header
  71. // or token in body (PUT/POST requests)
  72. $extraParams['oauth_token'] = $oauthToken;
  73. }
  74. }
  75. // auto appends locale param from session
  76. if (!isset($extraParams['locale'])) {
  77. $locale = $session->get('requestLocale');
  78. if (!empty($locale)) {
  79. $extraParams['locale'] = $locale;
  80. }
  81. }
  82. }
  83. $type = XenForo_Link::_checkForFullLink($type, $fullLink, $fullLinkPrefix);
  84. $link = XenForo_Link::_buildLink('api', $type, $data, $extraParams);
  85. $queryString = XenForo_Link::buildQueryString($extraParams);
  86. if ($link instanceof XenForo_Link) {
  87. $canPrependFull = $link->canPrependFull();
  88. } else {
  89. $canPrependFull = true;
  90. if (strpos($link, '#') !== false) {
  91. list($link, $hash) = explode('#', $link);
  92. }
  93. }
  94. if ($queryString !== '' && $link !== '') {
  95. $append = "?$link&$queryString";
  96. } else {
  97. // 1 or neither of these has content
  98. $append = $link . $queryString;
  99. if ($append !== '') {
  100. $append = "?$append";
  101. }
  102. }
  103. if ($skipPrepend) {
  104. $outputLink = $append;
  105. } else {
  106. $outputLink = 'index.php' . $append;
  107. }
  108. if ($fullLink && $canPrependFull) {
  109. $outputLink = $fullLinkPrefix . $outputLink;
  110. }
  111. // deal with a hash in the $type {xen:link prefix#hash..}
  112. if (($hashPos = strpos($type, '#')) !== false) {
  113. $hash = substr($type, $hashPos + 1);
  114. }
  115. if ($outputLink === '') {
  116. $outputLink = '.';
  117. }
  118. return $outputLink . (empty($hash) ? '' : '#' . $hash);
  119. }
  120. public static function convertUriToAbsoluteUri($uri, $includeHost = false, array $paths = null)
  121. {
  122. if (!$paths) {
  123. $paths = XenForo_Application::get('requestPaths');
  124. }
  125. $boardUrl = rtrim(XenForo_Application::getOptions()->get('boardUrl'), '/') . '/';
  126. $boardUrlParsed = parse_url($boardUrl);
  127. $paths['protocol'] = $boardUrlParsed['scheme'];
  128. $paths['host'] = $boardUrlParsed['host'] . (isset($boardUrlParsed['port']) ? (':' . $boardUrlParsed) : '');
  129. $paths['fullBasePath'] = $boardUrl;
  130. $paths['basePath'] = $boardUrlParsed['path'];
  131. return parent::convertUriToAbsoluteUri($uri, true, $paths);
  132. }
  133. public static function convertApiUriToAbsoluteUri($uri, $includeHost = false, array $paths = null)
  134. {
  135. return parent::convertUriToAbsoluteUri($uri, true, $paths);
  136. }
  137. protected static function _checkForFullLink($type, &$fullLink, &$fullLinkPrefix)
  138. {
  139. $type = parent::_checkForFullLink($type, $fullLink, $fullLinkPrefix);
  140. if (!empty($fullLinkPrefix)) {
  141. // fix issue with HTTPS requests
  142. $paths = XenForo_Application::get('requestPaths');
  143. if ($paths['protocol'] === 'https' AND parse_url($fullLinkPrefix, PHP_URL_SCHEME) === 'http') {
  144. $fullLinkPrefix = str_replace('http://', 'https://', $fullLinkPrefix);
  145. }
  146. }
  147. return $type;
  148. }
  149. }
  150. eval('class XenForo_Link extends bdApi_Link {}');