BxDolPermalinks.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. {
  55. protected $sCacheFile;
  56. protected $aLinksStandard;
  57. protected $aLinksPermalink;
  58. protected $aPrefixesStandard;
  59. protected $aPrefixesPermalink;
  60. function __construct()
  61. {
  62. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  63. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  64. parent::__construct();
  65. if (getParam('sys_db_cache_enable')) {
  66. $oCache = $this->getDbCacheObject();
  67. $sCacheKey = $this->genDbCacheKey('sys_permalinks');
  68. $aPermalinksData = $oCache->getData($sCacheKey);
  69. if (null === $aPermalinksData) {
  70. $aPermalinksData = $this->getPermalinksData();
  71. $oCache->setData ($sCacheKey, $aPermalinksData);
  72. }
  73. } else {
  74. $aPermalinksData = $this->getPermalinksData();
  75. }
  76. $this->aLinksStandard = $aPermalinksData['standard'];
  77. $this->aLinksPermalink = $aPermalinksData['permalink'];
  78. $this->aPrefixesStandard = $aPermalinksData['prefixes_standard'];
  79. $this->aPrefixesPermalink = $aPermalinksData['prefixes_permalink'];
  80. }
  81. public function cacheInvalidate()
  82. {
  83. return BxDolDb::getInstance()->cleanCache ('sys_permalinks');
  84. }
  85. /**
  86. * Prevent cloning the instance
  87. */
  88. public function __clone()
  89. {
  90. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  91. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  92. }
  93. /**
  94. * Get singleton instance of the class
  95. */
  96. public static function getInstance()
  97. {
  98. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  99. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolPermalinks();
  100. return $GLOBALS['bxDolClasses'][__CLASS__];
  101. }
  102. function getPermalinksData()
  103. {
  104. $aLinksStandard = $this->getAll("SELECT * FROM `sys_permalinks`");
  105. $aResult = array(
  106. 'standard' => array(),
  107. 'permalink' => array(),
  108. 'prefixes_standard' => array(),
  109. 'prefixes_permalink' => array(),
  110. );
  111. foreach ($aLinksStandard as $aLink) {
  112. $a = array(
  113. 'permalink' => $aLink['permalink'],
  114. 'standard' => $aLink['standard'],
  115. 'check' => $aLink['check'],
  116. 'enabled' => getParam($aLink['check']) == 'on',
  117. );
  118. $aResult['standard'][$aLink['standard']] = $a;
  119. $aResult['permalink'][$aLink['permalink']] = $a;
  120. if ($aLink['compare_by_prefix']) {
  121. $aResult['prefixes_standard'][$aLink['standard']] = strlen($aLink['standard']);
  122. $aResult['prefixes_permalink'][$aLink['permalink']] = strlen($aLink['permalink']);
  123. }
  124. }
  125. return $aResult;
  126. }
  127. /**
  128. * Get page name (changeable page of URL) from the URL.
  129. * @param $sLink - relative UNpermalinked, it is better to use unpermalink method to make sure that correct url is passed here
  130. * @param $iLength - position of the page name, or false to detect automatically
  131. * @returm page name (changeable part of URL) or empty string if page name is not detectable.
  132. */
  133. function getPageNameFromLink($sLink, $iLength = false)
  134. {
  135. if (false == $iLength) {
  136. $sLink = $this->_fixUrl($sLink);
  137. foreach ($this->aPrefixesStandard as $sKey => $iLen) {
  138. if (strncmp($sLink, $sKey, $iLen) === 0) {
  139. $iLength = $iLen;
  140. break;
  141. }
  142. }
  143. }
  144. return false === $iLength ? '' : substr($sLink, $iLength);
  145. }
  146. /**
  147. * Make permalink from link.
  148. * @param $sLink - relative URL.
  149. * @param $aParams - params to add to the url.
  150. * @return - relative permalinked URL if it was detected and permalinks are ON or unchanged URL otherwise.
  151. */
  152. function permalink($sLink, $aParams = array())
  153. {
  154. $sRet = null;
  155. bx_alert('system', 'permalink', 0, 0, array('link' => $sLink, 'params' => &$aParams, 'return_data' => &$sRet));
  156. if (null !== $sRet)
  157. return $sRet;
  158. $sLink = $this->_fixUrl($sLink);
  159. foreach ($this->aPrefixesStandard as $sKey => $iLength) {
  160. if (strncmp($sLink, $sKey, $iLength) !== 0)
  161. continue;
  162. $sPage = $this->getPageNameFromLink($sLink, $iLength);
  163. if (!$this->_isEnabled($sKey))
  164. return $sLink;
  165. return bx_append_url_params($this->aLinksStandard[$sKey]['permalink'] . $sPage, $aParams);
  166. }
  167. return bx_append_url_params($this->_isEnabled($sLink) ? $this->aLinksStandard[$sLink]['permalink'] : $sLink, $aParams);
  168. }
  169. /**
  170. * Remove permalink from link.
  171. * @param $sLink - relative or absoulte URL.
  172. * @param $isStripBaseUrl - strip site prefix (absolute URL) automatically (enabled by default)
  173. * @return - relative UNpermalinked URL if it was detected or relative URL if URL withing the site or unchanged URL otherwise.
  174. */
  175. function unpermalink($sLink, $isStripBaseUrl = true)
  176. {
  177. if ($isStripBaseUrl && 0 == strncmp($sLink, BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)))
  178. $sLink = substr($sLink, strlen(BX_DOL_URL_ROOT));
  179. $sRet = null;
  180. bx_alert('system', 'unpermalink', 0, 0, array('link' => $sLink, 'return_data' => &$sRet));
  181. if (null !== $sRet)
  182. return $sRet;
  183. foreach ($this->aPrefixesPermalink as $sKey => $iLength) {
  184. if (strncmp($sLink, $sKey, $iLength) !== 0)
  185. continue;
  186. $sPage = substr($sLink, $iLength);
  187. return $this->aLinksPermalink[$sKey]['standard'] . $sPage;
  188. }
  189. return isset($this->aLinksPermalink[$sLink]) ? $this->aLinksPermalink[$sLink]['standard'] : $sLink;
  190. }
  191. function _isEnabled($sLink)
  192. {
  193. return array_key_exists($sLink, $this->aLinksStandard) && $this->aLinksStandard[$sLink]['enabled'];
  194. }
  195. /**
  196. * redirect to the correct url after switching skin or language
  197. * only correct modules urls are supported
  198. */
  199. function redirectIfNecessary ($aSkip = array())
  200. {
  201. $sCurrentUrl = $_SERVER['PHP_SELF'] . '?' . bx_encode_url_params($_GET, $aSkip);
  202. if (!preg_match('/modules\/index.php\?r=(\w+)(.*)/', $sCurrentUrl, $m))
  203. return false;
  204. $sStandardLink = 'modules/?r=' . $m[1] . '/';
  205. $sPermalink = $this->permalink ($sStandardLink);
  206. if (false !== strpos($sCurrentUrl, $sPermalink))
  207. return false;
  208. header("HTTP/1.1 301 Moved Permanently");
  209. header ('Location:' . BX_DOL_URL_ROOT . $sPermalink . rtrim(trim(urldecode($m[2]), '/'), '&'));
  210. return true;
  211. }
  212. protected function _fixUrl($sLink)
  213. {
  214. if (strncmp($sLink, 'modules/index.php?r=', 20) === 0)
  215. $sLink = str_replace('modules/index.php?r=', 'modules/?r=', $sLink);
  216. return $sLink;
  217. }
  218. }
  219. /** @} */