BxDolPermalinks.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolDb');
  10. /**
  11. * @page objects
  12. * @section permalinks Permalinks
  13. * @ref BxDolPermalinks
  14. */
  15. /**
  16. * Permalinks for any content.
  17. *
  18. * An object of the class allows to check whether permalink is enabled
  19. * and get it for specified standard URI.
  20. *
  21. *
  22. * All permalinks must match the whole URL, to be automatially determined.
  23. * There are only 2 permlinks which are replaced by prefix:
  24. * - modules/?r= - for module URLs
  25. * - page.php?i= - for pages URLs
  26. *
  27. *
  28. * @section example Example of usage:
  29. *
  30. * 1. Register permalink in database by adding necessary info in the `sys_permalinks` table.
  31. * 2. Create an object and process the URI
  32. * @code
  33. * $oPermalinks = new BxDolPermalinks();
  34. * $oPermalinks->permalink('modules/?r=news/home');
  35. * @endcode
  36. *
  37. *
  38. * @section acl Memberships/ACL:
  39. * Doesn't depend on user's membership.
  40. *
  41. *
  42. * @section alerts Alerts:
  43. *
  44. * The following alert can be used when permalink call is performed:
  45. * - $sUnit - system
  46. * - $sAction - permalink | unpermalink
  47. * - $iObjectId - 0 (not used)
  48. * - $iSenderId - 0 (not used)
  49. * - $aExtra['link'] - the link to check permalink for
  50. * - $aExtra['return_data'] - it is possible to override default behavior, by setting 'return_data' to non NULL value.
  51. *
  52. */
  53. class BxDolPermalinks extends BxDolDb implements iBxDolSingleton {
  54. protected $sCacheFile;
  55. protected $aLinksStandard;
  56. protected $aLinksPermalink;
  57. protected $aPrefixesStandard;
  58. protected $aPrefixesPermalink;
  59. function __construct() {
  60. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  61. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  62. parent::__construct();
  63. if (getParam('sys_db_cache_enable')) {
  64. $oCache = $this->getDbCacheObject();
  65. $sCacheKey = $this->genDbCacheKey('sys_permalinks');
  66. $aPermalinksData = $oCache->getData($sCacheKey);
  67. if (null === $aPermalinksData) {
  68. $aPermalinksData = $this->getPermalinksData();
  69. $oCache->setData ($sCacheKey, $aPermalinksData);
  70. }
  71. } else {
  72. $aPermalinksData = $this->getPermalinksData();
  73. }
  74. $this->aLinksStandard = $aPermalinksData['standard'];
  75. $this->aLinksPermalink = $aPermalinksData['permalink'];
  76. $this->aPrefixesStandard = $aPermalinksData['prefixes_standard'];
  77. $this->aPrefixesPermalink = $aPermalinksData['prefixes_permalink'];
  78. }
  79. public function cacheInvalidate() {
  80. return BxDolDb::getInstance()->cleanCache ('sys_permalinks');
  81. }
  82. /**
  83. * Prevent cloning the instance
  84. */
  85. public function __clone() {
  86. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  87. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  88. }
  89. /**
  90. * Get singleton instance of the class
  91. */
  92. public static function getInstance() {
  93. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  94. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolPermalinks();
  95. return $GLOBALS['bxDolClasses'][__CLASS__];
  96. }
  97. function getPermalinksData() {
  98. $aLinksStandard = $this->getAll("SELECT * FROM `sys_permalinks`");
  99. $aResult = array(
  100. 'standard' => array(),
  101. 'permalink' => array(),
  102. 'prefixes_standard' => array(),
  103. 'prefixes_permalink' => array(),
  104. );
  105. foreach ($aLinksStandard as $aLink) {
  106. $a = array(
  107. 'permalink' => $aLink['permalink'],
  108. 'standard' => $aLink['standard'],
  109. 'check' => $aLink['check'],
  110. 'enabled' => getParam($aLink['check']) == 'on',
  111. );
  112. $aResult['standard'][$aLink['standard']] = $a;
  113. $aResult['permalink'][$aLink['permalink']] = $a;
  114. if ($aLink['compare_by_prefix']) {
  115. $aResult['prefixes_standard'][$aLink['standard']] = strlen($aLink['standard']);
  116. $aResult['prefixes_permalink'][$aLink['permalink']] = strlen($aLink['permalink']);
  117. }
  118. }
  119. return $aResult;
  120. }
  121. /**
  122. * Get page name (changeable page of URL) from the URL.
  123. * @param $sLink - relative UNpermalinked, it is better to use unpermalink method to make sure that correct url is passed here
  124. * @param $iLength - position of the page name, or false to detect automatically
  125. * @returm page name (changeable part of URL) or empty string if page name is not detectable.
  126. */
  127. function getPageNameFromLink($sLink, $iLength = false) {
  128. if (false == $iLength) {
  129. $sLink = $this->_fixUrl($sLink);
  130. foreach ($this->aPrefixesStandard as $sKey => $iLen) {
  131. if (strncmp($sLink, $sKey, $iLen) === 0) {
  132. $iLength = $iLen;
  133. break;
  134. }
  135. }
  136. }
  137. return false === $iLength ? '' : substr($sLink, $iLength);
  138. }
  139. /**
  140. * Make permalink from link.
  141. * @param $sLink - relative URL.
  142. * @param $aParams - params to add to the url.
  143. * @return - relative permalinked URL if it was detected and permalinks are ON or unchanged URL otherwise.
  144. */
  145. function permalink($sLink, $aParams = array()) {
  146. $sRet = null;
  147. bx_alert('system', 'permalink', 0, 0, array('link' => $sLink, 'params' => &$aParams, 'return_data' => &$sRet));
  148. if (null !== $sRet)
  149. return $sRet;
  150. $sLink = $this->_fixUrl($sLink);
  151. foreach ($this->aPrefixesStandard as $sKey => $iLength) {
  152. if (strncmp($sLink, $sKey, $iLength) !== 0)
  153. continue;
  154. $sPage = $this->getPageNameFromLink($sLink, $iLength);
  155. if (!$this->_isEnabled($sKey))
  156. return $sLink;
  157. return bx_append_url_params($this->aLinksStandard[$sKey]['permalink'] . $sPage, $aParams);
  158. }
  159. return bx_append_url_params($this->_isEnabled($sLink) ? $this->aLinksStandard[$sLink]['permalink'] : $sLink, $aParams);
  160. }
  161. /**
  162. * Remove permalink from link.
  163. * @param $sLink - relative or absoulte URL.
  164. * @param $isStripBaseUrl - strip site prefix (absolute URL) automatically (enabled by default)
  165. * @return - relative UNpermalinked URL if it was detected or relative URL if URL withing the site or unchanged URL otherwise.
  166. */
  167. function unpermalink($sLink, $isStripBaseUrl = true) {
  168. if ($isStripBaseUrl && 0 == strncmp($sLink, BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)))
  169. $sLink = substr($sLink, strlen(BX_DOL_URL_ROOT));
  170. $sRet = null;
  171. bx_alert('system', 'unpermalink', 0, 0, array('link' => $sLink, 'return_data' => &$sRet));
  172. if (null !== $sRet)
  173. return $sRet;
  174. foreach ($this->aPrefixesPermalink as $sKey => $iLength) {
  175. if (strncmp($sLink, $sKey, $iLength) !== 0)
  176. continue;
  177. $sPage = substr($sLink, $iLength);
  178. return $this->aLinksPermalink[$sKey]['standard'] . $sPage;
  179. }
  180. return isset($this->aLinksPermalink[$sLink]) ? $this->aLinksPermalink[$sLink]['standard'] : $sLink;
  181. }
  182. function _isEnabled($sLink) {
  183. return array_key_exists($sLink, $this->aLinksStandard) && $this->aLinksStandard[$sLink]['enabled'];
  184. }
  185. /**
  186. * redirect to the correct url after switching skin or language
  187. * only correct modules urls are supported
  188. */
  189. function redirectIfNecessary ($aSkip = array()) {
  190. $sCurrentUrl = $_SERVER['PHP_SELF'] . '?' . bx_encode_url_params($_GET, $aSkip);
  191. if (!preg_match('/modules\/index.php\?r=(\w+)(.*)/', $sCurrentUrl, $m))
  192. return false;
  193. $sStandardLink = 'modules/?r=' . $m[1] . '/';
  194. $sPermalink = $this->permalink ($sStandardLink);
  195. if (false !== strpos($sCurrentUrl, $sPermalink))
  196. return false;
  197. header("HTTP/1.1 301 Moved Permanently");
  198. header ('Location:' . BX_DOL_URL_ROOT . $sPermalink . rtrim(trim(urldecode($m[2]), '/'), '&'));
  199. return true;
  200. }
  201. protected function _fixUrl($sLink) {
  202. if (strncmp($sLink, 'modules/index.php?r=', 20) === 0)
  203. $sLink = str_replace('modules/index.php?r=', 'modules/?r=', $sLink);
  204. return $sLink;
  205. }
  206. }
  207. /** @} */