BxBasePage.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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('BxDolPage');
  10. /**
  11. * Page representation.
  12. * @see BxDolPage
  13. */
  14. class BxBasePage extends BxDolPage {
  15. protected $_oTemplate;
  16. protected $_oPageCacheObject = null;
  17. public function __construct ($aObject, $oTemplate) {
  18. parent::__construct ($aObject);
  19. if ($oTemplate)
  20. $this->_oTemplate = $oTemplate;
  21. else
  22. $this->_oTemplate = BxDolTemplate::getInstance();
  23. }
  24. /**
  25. * Get page code with automatic caching, adding necessary css/js files and system template vars.
  26. * @return string.
  27. */
  28. public function getCode () {
  29. if (bx_get('dynamic') && ($iBlockId = (int)bx_get('pageBlock'))) {
  30. if (!$this->_isVisiblePage($this->_aObject)) {
  31. header('HTTP/1.0 403 Forbidden');
  32. exit;
  33. }
  34. header( 'Content-type:text/html;charset=utf-8' );
  35. echo $this->_getBlockOnlyCode($iBlockId);
  36. exit;
  37. }
  38. if (!$this->_isVisiblePage($this->_aObject))
  39. return $this->_getPageAccessDeniedMsg ();
  40. $this->_addJsCss();
  41. $this->_addSysTemplateVars();
  42. $this->_selectMenu();
  43. // TODO: insert profiler call here
  44. if (!getParam('sys_page_cache_enable') || !$this->_aObject['cache_lifetime'])
  45. return $this->_getPageCode();
  46. $oCache = $this->_getPageCacheObject();
  47. $sKey = $this->_getPageCacheKey();
  48. $mixedRet = $oCache->getData($sKey, $this->_aObject['cache_lifetime']);
  49. if ($mixedRet !== null) {
  50. return $mixedRet;
  51. } else {
  52. $sPageCode = $this->_getPageCode();
  53. $oCache->setData($sKey, $sPageCode, $this->_aObject['cache_lifetime']);
  54. }
  55. return $sPageCode;
  56. }
  57. /**
  58. * Get block title.
  59. * @return string
  60. */
  61. public function getBlockTitle ($aBlock) {
  62. return $this->_replaceMarkers(_t($aBlock['title']));
  63. }
  64. /**
  65. * Get page code only.
  66. * @return string
  67. */
  68. protected function _getPageCode () {
  69. $aVars = array ();
  70. $aBlocks = $this->_oQuery->getPageBlocks();
  71. foreach ($aBlocks as $sKey => $aCell) {
  72. $sCell = '';
  73. foreach ($aCell as $aBlock) {
  74. $sContentWithBox = $this->_getBlockCode($aBlock);
  75. if ($sContentWithBox)
  76. $sCell .= '<div class="bx-page-block-container bx-def-padding-topbottom" id="bx-page-block-' . $aBlock['id'] . '">' . $sContentWithBox . '</div>';
  77. }
  78. $aVars[$sKey] = $sCell;
  79. }
  80. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  81. }
  82. /**
  83. * Get one block code only.
  84. * @return string
  85. */
  86. protected function _getBlockOnlyCode ($iBlockId) {
  87. $aBlock = $this->_oQuery->getPageBlock((int)$iBlockId);
  88. return $this->_getBlockCode($aBlock);
  89. }
  90. /**
  91. * Get block code.
  92. * @return string
  93. */
  94. protected function _getBlockCode($aBlock) {
  95. $sContentWithBox = '';
  96. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  97. if ($this->_isVisibleBlock($aBlock)) {
  98. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  99. $mixedContent = $this->$sFunc($aBlock);
  100. $sTitle = $this->getBlockTitle($aBlock);
  101. if (is_array($mixedContent)) {
  102. if (isset($mixedContent['content']))
  103. $sContentWithBox = DesignBoxContent(
  104. isset($mixedContent['title']) ? $mixedContent['title'] : $sTitle,
  105. $mixedContent['content'],
  106. isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'],
  107. isset($mixedContent['menu']) ? $mixedContent['menu'] : false
  108. );
  109. } elseif (is_string($mixedContent)) {
  110. $sContentWithBox = DesignBoxContent($sTitle, $mixedContent, $aBlock['designbox_id']);
  111. } else {
  112. $sContentWithBox = false;
  113. }
  114. }
  115. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  116. return $sContentWithBox;
  117. }
  118. /**
  119. * Add necessary js and css files.
  120. */
  121. protected function _addJsCss() {
  122. $this->_oTemplate->addCss('page_layouts.css');
  123. }
  124. /**
  125. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  126. */
  127. protected function _addSysTemplateVars () {
  128. $sPageTitle = $this->_getPageTitle();
  129. if ($sPageTitle)
  130. $this->_oTemplate->setPageHeader ($sPageTitle);
  131. $sMetaDesc = $this->_getPageMetaDesc();
  132. if ($sMetaDesc)
  133. $this->_oTemplate->setPageDescription ($sMetaDesc);
  134. $sMetaKeywords = $this->_getPageMetaKeywords();
  135. if ($sMetaKeywords)
  136. $this->_oTemplate->addPageKeywords ($sMetaKeywords);
  137. $sMetaRobots = $this->_getPageMetaRobots();
  138. if ($sMetaRobots)
  139. $this->_oTemplate->setPageMetaRobots ($sMetaRobots);
  140. }
  141. /**
  142. * Select menu from page properties.
  143. */
  144. protected function _selectMenu () {
  145. bx_import('BxDolMenu');
  146. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  147. }
  148. /**
  149. * Get content for 'raw' block type.
  150. * @return string
  151. */
  152. protected function _getBlockRaw ($aBlock) {
  153. return $this->_replaceMarkers($aBlock['content']);
  154. }
  155. /**
  156. * Get content for 'html' block type.
  157. * @return string
  158. */
  159. protected function _getBlockHtml ($aBlock) {
  160. return $this->_getBlockRaw ($aBlock);
  161. }
  162. /**
  163. * Get content for 'lang' block type.
  164. * @return string
  165. */
  166. protected function _getBlockLang ($aBlock) {
  167. return $this->_replaceMarkers(_t(trim($aBlock['content'])));
  168. }
  169. /**
  170. * Get content for 'image' block type.
  171. * @return string
  172. */
  173. protected function _getBlockImage ($aBlock) {
  174. if (empty($aBlock['content']))
  175. return false;
  176. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  177. $iFileId = (int)$iFileId;
  178. if (!$iFileId)
  179. return false;
  180. bx_import('BxDolStorage');
  181. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  182. if (!$oStorage)
  183. return false;
  184. $sUrl = $oStorage->getFileUrlById($iFileId);
  185. if (!$sUrl)
  186. return false;
  187. $sStyleAdd = '';
  188. if ('center' == $sAlign || 'left' == $sAlign || 'right' == $sAlign)
  189. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  190. return '<div class="bx-page-image-container" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  191. }
  192. /**
  193. * Get content for 'rss' block type.
  194. * @return string
  195. */
  196. protected function _getBlockRss ($aBlock) {
  197. if (empty($aBlock['content']))
  198. return false;
  199. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  200. $iNum = (int)$iNum;
  201. bx_import('BxTemplFunctions');
  202. return BxTemplFunctions::getInstance()->getRssHolder ($aBlock['id'], $iNum);
  203. }
  204. /**
  205. * Get content for 'menu' block type.
  206. * @return string
  207. */
  208. protected function _getBlockMenu ($aBlock) {
  209. bx_import('BxTemplMenu');
  210. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  211. return $oMenu ? $oMenu->getCode () : '';
  212. }
  213. /**
  214. * Get content for 'service' block type.
  215. * @return string
  216. */
  217. protected function _getBlockService ($aBlock) {
  218. $a = @unserialize($aBlock['content']);
  219. if (false === $a || !is_array($a))
  220. return '';
  221. return BxDolService::call($a['module'], $a['method'], isset($a['params']) ? $this->_replaceMarkers($a['params']) : array(), isset($a['class']) ? $a['class'] : 'Module');
  222. }
  223. /**
  224. * Get page title.
  225. * @return string
  226. */
  227. protected function _getPageTitle() {
  228. return $this->_replaceMarkers(_t($this->_aObject['title']));
  229. }
  230. /**
  231. * Get page meta description.
  232. * @return string
  233. */
  234. protected function _getPageMetaDesc() {
  235. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  236. }
  237. /**
  238. * Get page meta keywords.
  239. * @return string
  240. */
  241. protected function _getPageMetaKeywords() {
  242. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  243. }
  244. /**
  245. * Get page meta robots.
  246. * @return string
  247. */
  248. protected function _getPageMetaRobots() {
  249. return _t($this->_aObject['meta_robots']);
  250. }
  251. /**
  252. * Get access denied message HTML.
  253. * @return string
  254. */
  255. protected function _getPageAccessDeniedMsg () {
  256. return MsgBox(_t('_Access denied'));
  257. }
  258. /**
  259. * Get page cache object.
  260. * @return cache object instance
  261. */
  262. protected function _getPageCacheObject () {
  263. if ($this->_oPageCacheObject != null) {
  264. return $this->_oPageCacheObject;
  265. } else {
  266. $sEngine = getParam('sys_page_cache_engine');
  267. $this->_oPageCacheObject = bx_instance ('BxDolCache' . $sEngine);
  268. if (!$this->_oPageCacheObject->isAvailable())
  269. $this->_oPageCacheObject = bx_instance ('BxDolCacheFile');
  270. return $this->_oPageCacheObject;
  271. }
  272. }
  273. /**
  274. * Get page cache key.
  275. * @param $isPrefixOnly return key prefix only.
  276. * @return string
  277. */
  278. protected function _getPageCacheKey ($isPrefixOnly = false) {
  279. $s = 'page_' . $this->_aObject['object'] . '_';
  280. if ($isPrefixOnly)
  281. return $s;
  282. $s .= $this->_getPageCacheParams ();
  283. $s .= md5(BX_DOL_VERSION . BX_DOL_BUILD . BX_DOL_URL_ROOT) . '.php';
  284. return $s;
  285. }
  286. /**
  287. * Additional cache key. In the case of dynamic page.
  288. * For example - profile page, where each profile has own page.
  289. * @return string
  290. */
  291. protected function _getPageCacheParams () {
  292. return '';
  293. }
  294. /**
  295. * Clean page cache.
  296. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  297. * @return string
  298. */
  299. protected function cleanCache ($isDelAllWithPagePrefix = false) {
  300. $oCache = $this->_getPageCacheObject ();
  301. $sKey = $this->_getPageCacheKey($isDelAllWithPagePrefix);
  302. if ($isDelAllWithPagePrefix)
  303. return $oCache->removeAllByPrefix($sKey);
  304. else
  305. return $oCache->delData($sKey);
  306. }
  307. }
  308. /** @} */