BxDolPage.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Pages.
  11. *
  12. * It allows to display pages which are built from studio.
  13. *
  14. * The new system has the following page features:
  15. * - Layouts: page can have any structure, not just columns.
  16. * - SEO: page can have own SEO options, like meta tags and meta keywords, as well as instructions for search bots.
  17. * - Cache: page can be cached on the server.
  18. * - Access control: page access can be controlled using member levels.
  19. *
  20. *
  21. *
  22. * @section page_create Creating the Page object:
  23. *
  24. * 1. add record to 'sys_objects_page' table:
  25. *
  26. * - object: name of the page object, in the format: vendor prefix, underscore, module prefix, underscore, internal identifier or nothing; for example: bx_profiles_view - profile view page.
  27. * - title: name to display as page title.
  28. * - module: the module this page belongs to.
  29. * - layout_id: page layout to use, this is id of the record from 'sys_pages_layouts' table.
  30. * - visible_for_levels: bit field with set of member level ids. To use member level id in bit field - the level id minus 1 as power of 2 is used, for example:
  31. * - user level id = 1 -> 2^(1-1) = 1
  32. * - user level id = 2 -> 2^(2-1) = 2
  33. * - user level id = 3 -> 2^(3-1) = 4
  34. * - user level id = 4 -> 2^(4-1) = 8
  35. * - visible_for_levels_editable: it determines if 'visible_for_levels' field is editable from page builder, visibility options can be overriden by custom class and shouldn't be editable in this case.
  36. * - url: the page url, if it is static page.
  37. * - meta_description: meta description of the page.
  38. * - meta_keywords: meta keywords of the page.
  39. * - meta_robots: instructions for search bots.
  40. * - cache_lifetime: number of seconds to store cache for.
  41. * - cache_editable: it determines if cache can be edited from page builder.
  42. * - deletable: it determines if page can be deleted from page builder.
  43. * - override_class_name: user defined class name which is derived from BxTemplPage.
  44. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  45. *
  46. * Page can select appropriate menu automatically if 'module' and 'object' fields in 'sys_objects_page' table are matched with 'module' and 'name' fields in 'sys_menu_items' table.
  47. *
  48. *
  49. * Page layout are stored in 'sys_pages_layouts' table:
  50. * - name: inner unique layout name.
  51. * - icon: layout icon to display in page builder, it should represent basic view of the layout to help studio operator determine the layout structure.
  52. * - title: layout name to display in page builder.
  53. * - template: template name to use to display page with certain layout.
  54. * - cells_number: number of areas in the layout, page blocks can be places into this areas(cells).
  55. * To define areas in the layout they should be named as '__cell_N__', where N is cell number, starting from 1.
  56. *
  57. *
  58. * 2. Add page blocks to 'sys_pages_blocks' table:
  59. * - object: page object name this block belongs to.
  60. * - cell_id: cell number in page layout to place block to.
  61. * - module: module name this block belongs to.
  62. * - title: block title.
  63. * - designbox_id: design box to use to diplay page block, it is id of the record from 'sys_pages_design_boxes' table.
  64. * - visible_for_levels: bit field with set of member level ids. To use member level id in bit field - the level id minus 1 as power of 2 is used, for example:
  65. * - user level id = 1 -> 2^(1-1) = 1
  66. * - user level id = 2 -> 2^(2-1) = 2
  67. * - user level id = 3 -> 2^(3-1) = 4
  68. * - user level id = 4 -> 2^(4-1) = 8
  69. * - type: block type
  70. * - raw: HTML block, displayed in page builder as HTML textarea.
  71. * - html: HTML block, displayed in page builder as visual editor, like TinyMCE.
  72. * - lang: translatable language string, displayed in page builder as editable language key.
  73. * - image: just an image, displayed in page builder as HTML upload form.
  74. * - rss: RSS block, displayed in page builder as editable URL to RSS resource, along with number of displayed items.
  75. * - menu: menu block, displayed as menu selector.
  76. * - service: to display block content, the provided service method is used.
  77. * - content: depending on 'type' field:
  78. * - raw: HTML string.
  79. * - html: HTML string.
  80. * - lang: language key.
  81. * - image: image id in the storage and alignment (left, center, right) for example: 36\#center
  82. * - rss: URL to RSS with number of displayed items, for example: http://www.example.com/rss#4
  83. * - menu: menu object name.
  84. * - service: serialized array of service call parameters: module - module name, method - service method name, params - array of parameters.
  85. * - deletable: is block deletable from page builder
  86. * - copyable: is block can be copied to any other page from page builder.
  87. * - order: block order in particular cell.
  88. *
  89. * Block design boxes are stored in 'sys_pages_design_boxes' table:
  90. * - id: consistent id, there are the following defines can be used in the code for each system block style:
  91. * - 0 - BX_DB_CONTENT_ONLY: design box with content only - no borders, no background, no caption.
  92. * - 1 - BX_DB_DEF: default design box with content, borders and caption.
  93. * - 2 - BX_DB_EMPTY: just empty design box, without anything.
  94. * - 3 - BX_DB_NO_CAPTION: design box with content, like BX_DB_DEF but without caption.
  95. * - 10 - BX_DB_PADDING_CONTENT_ONLY: design box with content only wrapped with default padding - no borders, no background, no caption; it can be used to just wrap content with default padding.
  96. * - 11 - BX_DB_PADDING_DEF: default design box with content wrapped with default padding, borders and caption.
  97. * - 13 - BX_DB_PADDING_NO_CAPTION: design box with content wrapped with default padding, like BX_DB_DEF but without caption.
  98. * - title: block name which is displayed in studio, describes block styles.
  99. * - template: template name to use to display page block.
  100. *
  101. *
  102. * 3. Display Page.
  103. * Use the following sample code to display page:
  104. * @code
  105. * $oPage = BxDolPage::getObjectInstance('sample'); // 'sample' is 'object' field from 'sys_objects_page' table, it automatically creates instance of default or custom class by object name
  106. * if ($oPage)
  107. * echo $oPage->getCode(); // print page
  108. * @endcode
  109. *
  110. */
  111. class BxDolPage extends BxDolFactory implements iBxDolFactoryObject, iBxDolReplaceable
  112. {
  113. protected $_sObject;
  114. protected $_aObject;
  115. protected $_oQuery;
  116. protected $_aMarkers = array ();
  117. /**
  118. * Constructor
  119. * @param $aObject array of page options
  120. */
  121. protected function __construct($aObject)
  122. {
  123. parent::__construct();
  124. $this->_sObject = $aObject['object'];
  125. $this->_aObject = $aObject;
  126. $this->_oQuery = new BxDolPageQuery($this->_aObject);
  127. }
  128. /**
  129. * Get page object instance by Module name and page URI
  130. * @param $sModule module name
  131. * @param $sURI unique page URI
  132. * @return object instance or false on error
  133. */
  134. static public function getObjectInstanceByModuleAndURI($sModule, $sURI = '', $oTemplate = false)
  135. {
  136. if(empty($sURI) && bx_get('i') !== false)
  137. $sURI = bx_process_input(bx_get('i'));
  138. if(empty($sURI))
  139. return false;
  140. $sObject = BxDolPageQuery::getPageObjectNameByURI($sURI, $sModule);
  141. return $sObject ? self::getObjectInstance($sObject, $oTemplate) : false;
  142. }
  143. /**
  144. * Get page object instance by page URI
  145. * @param $sURI unique page URI
  146. * @return object instance or false on error
  147. */
  148. static public function getObjectInstanceByURI($sURI = '', $oTemplate = false, $bRedirectCheck = false)
  149. {
  150. if(empty($sURI) && bx_get('i') !== false)
  151. $sURI = bx_process_input(bx_get('i'));
  152. if(empty($sURI))
  153. return false;
  154. $sObject = BxDolPageQuery::getPageObjectNameByURI($sURI);
  155. if ($bRedirectCheck && !$sObject && '/' == substr($sURI, -1)) {
  156. header("HTTP/1.1 301 Moved Permanently");
  157. unset($_GET['i']);
  158. header ('Location:' . bx_append_url_params(BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . trim($sURI, '/')), $_GET));
  159. exit;
  160. }
  161. return $sObject ? self::getObjectInstance($sObject, $oTemplate) : false;
  162. }
  163. /**
  164. * Get page object instance by object name
  165. * @param $sObject object name
  166. * @return object instance or false on error
  167. */
  168. static public function getObjectInstance($sObject, $oTemplate = false)
  169. {
  170. if (isset($GLOBALS['bxDolClasses']['BxDolPage!'.$sObject]))
  171. return $GLOBALS['bxDolClasses']['BxDolPage!'.$sObject];
  172. $aObject = BxDolPageQuery::getPageObject($sObject);
  173. if (!$aObject || !is_array($aObject))
  174. return false;
  175. $sClass = 'BxTemplPage';
  176. if (!empty($aObject['override_class_name'])) {
  177. $sClass = $aObject['override_class_name'];
  178. if (!empty($aObject['override_class_file']))
  179. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  180. }
  181. $o = new $sClass($aObject, $oTemplate);
  182. return ($GLOBALS['bxDolClasses']['BxDolPage!'.$sObject] = $o);
  183. }
  184. /**
  185. * Process page triggers.
  186. * Page triggers allow to automatically add page blocks to modules with no different if dependant module was install before or after the module page block belongs to.
  187. * For example module "Notes" adds page blocks to all profiles modules (Persons, Organizations, etc)
  188. * with no difference if persons module was installed before or after "Notes" module was installed.
  189. * @param $sPageTriggerName trigger name to process, usually specified in module installer class - @see BxBaseModGeneralInstaller
  190. * @return always true, always success
  191. */
  192. static public function processPageTrigger ($sPageTriggerName)
  193. {
  194. // get list of active modules
  195. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array(
  196. 'type' => 'modules',
  197. 'active' => 1,
  198. ));
  199. // get list of page block triggers
  200. $aPageBlocks = BxDolPageQuery::getPageTriggers($sPageTriggerName);
  201. // check each page block trigger for all modules
  202. foreach ($aPageBlocks as $aPageBlock) {
  203. foreach ($aModules as $aModule) {
  204. if (!BxDolRequest::serviceExists($aModule['name'], 'get_page_object_for_page_trigger'))
  205. continue;
  206. $mixedPageObject = BxDolService::call($aModule['name'], 'get_page_object_for_page_trigger', array($sPageTriggerName));
  207. if (!$mixedPageObject)
  208. continue;
  209. $aPageBlockRow = $aPageBlock;
  210. if (is_array($mixedPageObject))
  211. $aPageBlockRow = array_merge($aPageBlockRow, $mixedPageObject);
  212. else
  213. $aPageBlockRow['object'] = $mixedPageObject;
  214. BxDolPageQuery::addPageBlockToPage($aPageBlockRow);
  215. }
  216. }
  217. return true;
  218. }
  219. /**
  220. * Process SEO links. It takes request part from SEO link and process it
  221. * to make it work as regular page link
  222. * @param $sRequest request URI with SEO link
  223. * @return true - if page was found and processed correctly, false - if page wasn't found
  224. */
  225. static public function processSeoLink ($sRequest)
  226. {
  227. $a = explode('/', trim($sRequest, '/'));
  228. if (!$a || empty($a[0]))
  229. return false;
  230. $sPageName = BxDolPageQuery::getPageObjectNameByURI($a[0]);
  231. $aPage = $sPageName ? BxDolPageQuery::getPageObject($sPageName) : false;
  232. if (!$aPage)
  233. return false;
  234. if (!empty($a[1])) { // page with params
  235. $r = BxDolPageQuery::getSeoLink($aPage['module'], $a[0], ['uri' => $a[1]]);
  236. if ($r)
  237. $_GET[$r['param_name']] = $_REQUEST[$r['param_name']] = $r['param_value'];
  238. }
  239. $_REQUEST['i'] = $_GET['i'] = $a[0];
  240. $oPage = BxDolPage::getObjectInstanceByURI($a[0], false, true);
  241. $oPage->displayPage();
  242. return true;
  243. }
  244. /**
  245. * Transform regular link to SEO link. It takes regular list as param and return SEO link.
  246. * @param $sLink regular link
  247. * @param $sPrefix prefix to add to the final URL, usually BX_DOL_URL_ROOT
  248. * @param $aParams additional GET params
  249. * @return SEO link string on success, false if transform failed
  250. */
  251. static public function transformSeoLink ($sLink, $sPrefix, $aParams = array())
  252. {
  253. if (0 !== strncmp('page.php', $sLink, 8)) // only page.php links are supported
  254. return false;
  255. $sQuery = parse_url($sLink, PHP_URL_QUERY);
  256. if (false === $sQuery)
  257. return false;
  258. parse_str($sQuery, $aQueryParams);
  259. $sPageUri = !empty($aQueryParams['i']) ? $aQueryParams['i'] : false;
  260. unset($aQueryParams['i']);
  261. if (!$sPageUri) // page URI wasn't found
  262. return false;
  263. $aSeoParams = array('id', 'profile_id'); // supported SEO params which are transformed to SEO strings
  264. $sSeoParamName = '';
  265. $sSeoParamValue = '';
  266. foreach ($aSeoParams as $k) {
  267. if (empty($k) || empty($aQueryParams[$k]))
  268. continue;
  269. $sSeoParamName = $k;
  270. $sSeoParamValue = $aQueryParams[$k];
  271. unset($aQueryParams[$k]);
  272. break; // only 1 SEO param will be transformed
  273. }
  274. $sSeoPageUri = $sPageUri;
  275. if ($sSeoParamName && $sSeoParamValue) { // process page with SEO param
  276. $sPageName = BxDolPageQuery::getPageObjectNameByURI($sPageUri);
  277. $aPage = $sPageName ? BxDolPageQuery::getPageObject($sPageName) : false;
  278. if ($aPage) {
  279. $sModule = $aPage['module'];
  280. if ('id' == $sSeoParamName) {
  281. $oContentInfo = BxDolContentInfo::getObjectInstance($sModule);
  282. $sSeoTitle = $oContentInfo ? $oContentInfo->getContentTitle($sSeoParamValue) : '';
  283. if (!$sSeoTitle)
  284. $sSeoTitle = base_convert(substr(md5($sSeoParamValue), -8), 16, 36);
  285. }
  286. elseif ('profile_id' == $sSeoParamName) {
  287. $oProfile = BxDolProfile::getInstance($sSeoParamValue);
  288. $sSeoTitle = $oProfile ? $oProfile->getDisplayName() : '';
  289. }
  290. $r = BxDolPageQuery::getSeoLink($sModule, $sPageUri, ['param_value' => $sSeoParamValue]);
  291. if (!$r) {
  292. $sUri = uriGenerate ($sSeoTitle, 'sys_seo_links', 'uri', '-', '-', ['module' => $sModule, 'page_uri' => $sPageUri]);
  293. BxDolPageQuery::insertSeoLink($sModule, $sPageUri, $sSeoParamName, $sSeoParamValue, $sUri);
  294. }
  295. elseif ($r['param_name'] == $sSeoParamName) {
  296. $sUri = $r['uri'];
  297. }
  298. else {
  299. $sUri = false;
  300. }
  301. if ($sUri)
  302. $sSeoPageUri .= '/' . $sUri;
  303. else
  304. $sSeoPageUri = false;
  305. }
  306. }
  307. if (!$sSeoPageUri)
  308. return false;
  309. return $sPrefix . bx_append_url_params($sSeoPageUri, array_merge($aQueryParams, $aParams));
  310. }
  311. /**
  312. * Display complete page
  313. */
  314. public function displayPage ($oTemplate = null)
  315. {
  316. if (!$oTemplate)
  317. $oTemplate = BxDolTemplate::getInstance();
  318. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  319. $oTemplate->setPageType ($this->getType());
  320. $oTemplate->setPageInjections ($this->getInjections());
  321. $oTemplate->setPageContent ('page_main_code', $this->getCode());
  322. $oTemplate->getPageCode();
  323. }
  324. public function getId ()
  325. {
  326. return (int)$this->_aObject['id'];
  327. }
  328. public function getType ()
  329. {
  330. return (int)$this->_aObject['type_id'];
  331. }
  332. public function getModule ()
  333. {
  334. return $this->_aObject['module'];
  335. }
  336. public function getSubMenu ()
  337. {
  338. return $this->_aObject['submenu'];
  339. }
  340. public function getName ()
  341. {
  342. return $this->_sObject;
  343. }
  344. public function getInjections()
  345. {
  346. $aResult = array();
  347. foreach($this->_aObject as $sKey => $sValue)
  348. if(strpos($sKey, 'inj_') === 0)
  349. $aResult[substr($sKey, 4)] = $sValue;
  350. return $aResult;
  351. }
  352. /**
  353. * Add replace markers. Markers are replaced in raw, html, lang blocks and page title, description, keywords and block titles.
  354. * @param $a array of markers as key => value
  355. * @return true on success or false on error
  356. */
  357. public function addMarkers ($a)
  358. {
  359. if (empty($a) || !is_array($a))
  360. return false;
  361. $this->_aMarkers = array_merge ($this->_aMarkers, $a);
  362. return true;
  363. }
  364. public function isVisiblePage ()
  365. {
  366. return $this->_isVisiblePage($this->_aObject);
  367. }
  368. public function isEditAllowed ()
  369. {
  370. return false;
  371. }
  372. public function isDeleteAllowed ()
  373. {
  374. return false;
  375. }
  376. /**
  377. * Replace provided markers in a string
  378. * @param $mixed string or array to replace markers in
  379. * @return string where all occured markers are replaced
  380. */
  381. protected function _replaceMarkers ($mixed, $aAdditionalMarkers = array())
  382. {
  383. return bx_replace_markers($mixed, array_merge($this->_aMarkers, $aAdditionalMarkers));
  384. }
  385. /**
  386. * Check if page block is visible.
  387. */
  388. protected function _isVisibleBlock ($a)
  389. {
  390. return BxDolAcl::getInstance()->isMemberLevelInSet($a['visible_for_levels']);
  391. }
  392. /**
  393. * Check if page is visible.
  394. */
  395. protected function _isVisiblePage ($a)
  396. {
  397. return isAdmin() || BxDolAcl::getInstance()->isMemberLevelInSet($a['visible_for_levels']);
  398. }
  399. }
  400. /** @} */