BxDolPage.php 20 KB

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