BxBasePage.php 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. require_once(BX_DIRECTORY_PATH_INC . 'design.inc.php');
  10. /**
  11. * Page representation.
  12. * @see BxDolPage
  13. */
  14. class BxBasePage extends BxDolPage
  15. {
  16. protected $_oTemplate;
  17. protected $_sStorage; //--- Storage object for page's images like custom cover, HTML block attachments, etc.
  18. protected $_oPageCacheObject = null;
  19. protected $_oBlockCacheObject = null;
  20. protected $_sJsClassName = '';
  21. protected $_sJsObjectName = '';
  22. protected $_aHtmlIds = [];
  23. protected $_bStickyColumns = false;
  24. protected $_bSubPage = false;
  25. public function __construct ($aObject, $oTemplate)
  26. {
  27. parent::__construct ($aObject);
  28. if ($oTemplate)
  29. $this->_oTemplate = $oTemplate;
  30. else
  31. $this->_oTemplate = BxDolTemplate::getInstance();
  32. $this->_sStorage = 'sys_images';
  33. $this->_sJsClassName = 'BxDolPage';
  34. $this->_sJsObjectName = 'oBxDolPage';
  35. $this->_bStickyColumns = isset($this->_aObject['sticky_columns']) && $this->_aObject['sticky_columns'] == 1;
  36. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $this->_sObject);
  37. $this->_aHtmlIds = array(
  38. 'help_popup' => $sHtmlId . '-help-popup-',
  39. );
  40. $this->addMarkers(array('site_title' => getParam('site_title')));
  41. if (isLogged() && $oProfile = BxDolProfile::getInstance()) {
  42. $this->addMarkers([
  43. 'logged_profile_id' => $oProfile->id(),
  44. 'logged_profile_cid' => $oProfile->getContentId(),
  45. 'logged_account_id' => $oProfile->getAccountId(),
  46. 'logged_profile_module' => $oProfile->getModule(),
  47. 'logged_display_name' => $oProfile->getDisplayName(),
  48. 'logged_profile_url' => $oProfile->getUrl(),
  49. ]);
  50. }
  51. if (isset($GLOBALS['bxReplaceableMarkersPages'])) {
  52. $this->addMarkers($GLOBALS['bxReplaceableMarkersPages']);
  53. }
  54. }
  55. public function performActionGetHelp ()
  56. {
  57. $iBlockId = (int)bx_get('block_id');
  58. if(empty($iBlockId))
  59. return;
  60. $aBlock = $this->_oQuery->getPageBlock($iBlockId);
  61. if(empty($aBlock) || !is_array($aBlock) || empty($aBlock['help']))
  62. return;
  63. echo $this->_oTemplate->parseHtmlByName('designbox_help_popup.html', array(
  64. 'content' => _t($aBlock['help'])
  65. ));
  66. }
  67. public function performActionGetShare ()
  68. {
  69. $aEmbedData = BxDolPage::getEmbedData(bx_get('url'));
  70. if(!isset($aEmbedData['url']))
  71. return;
  72. $sMenu = '';
  73. if(getParam('sys_a2a_enable') != 'on') {
  74. if(($oMenu = BxDolMenu::getObjectInstance('sys_social_sharing')) !== false) {
  75. $oMenu->addMarkers([
  76. 'url' => $aEmbedData['url'],
  77. 'img_url_encoded' => '',
  78. 'title_encoded' => ''
  79. ]);
  80. $sMenu = $oMenu->getCode();
  81. }
  82. }
  83. else
  84. $sMenu = getParam('sys_a2a_code');
  85. echo PopupBox('sys_share_popup', _t('_sys_txt_share_popup_header'), $this->_oTemplate->parseHtmlByName('designbox_share_popup.html', [
  86. 'url' => bx_process_output($aEmbedData['url']),
  87. 'menu' => $sMenu,
  88. 'code' => htmlspecialchars($aEmbedData['html']),
  89. ]), true);
  90. }
  91. public function performActionEmbed ()
  92. {
  93. $aCover = $this->getPageCoverImage();
  94. $sTitle = $this->_getPageTitle();
  95. $this->_oTemplate->getEmbed($this->_oTemplate->parseHtmlByName('embed_card.html', [
  96. 'title' => $sTitle,
  97. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  98. 'bx_if:thumb' => [
  99. 'condition' => $this->isPageCover() && count($aCover) > 0,
  100. 'content' => [
  101. 'title' => $sTitle,
  102. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  103. 'background' => BxDolCover::getInstance(BxDolTemplate::getInstance())->getCoverImageUrl($aCover)
  104. ],
  105. ],
  106. 'bx_if:no_thumb' => [
  107. 'condition' => count($aCover) == 0 || !$this->isPageCover(),
  108. 'content' => [
  109. 'title' => $sTitle,
  110. 'url' => BxDolPermalinks::getInstance()->permalink($this->_aObject['url']),
  111. ]
  112. ]
  113. ]
  114. ));
  115. }
  116. public function performActionCreativeSave()
  117. {
  118. if(!$this->isAllowedCreativeManage())
  119. return echoJson(['code' => 1]);
  120. $iId = 0;
  121. $sContent = '';
  122. if(($iId = bx_get('b')) === false || ($sContent = bx_get('c')) === false)
  123. return echoJson(['code' => 2]);
  124. $iId = bx_process_input($iId, BX_DATA_INT);
  125. $sContent = bx_process_input($sContent, BX_DATA_HTML);
  126. if(!$this->_oQuery->setPageBlockContent($iId, $sContent))
  127. return echoJson(['code' => 3]);
  128. return echoJson(['code' => 0]);
  129. }
  130. public function isAllowedCreativeManage()
  131. {
  132. return isAdmin();
  133. }
  134. /**
  135. * Very similar to BxBasePage::getCode
  136. * but adds css and js files which are needed for the corect page display
  137. */
  138. public function getCodeDynamic ()
  139. {
  140. // TODO: remake to use use collect* template methods
  141. $oTemplate = BxDolTemplate::getInstance();
  142. // get js&css before the page code is generated
  143. $aCssBefore = $oTemplate->getCss();
  144. $aJsBefore = $oTemplate->getJs();
  145. // generate page code
  146. $sContent = $this->getCode();
  147. // get js&css after the page code is generated
  148. $aCssAfter = $oTemplate->getCss();
  149. $aJsAfter = $oTemplate->getJs();
  150. // compare files which were added before and after page code is generated
  151. $f = function ($a1, $a2) {
  152. return strcasecmp($a1['url'], $a2['url']);
  153. };
  154. $aCssNew = array_udiff($aCssAfter, $aCssBefore, $f);
  155. $aJsNew = array_udiff($aJsAfter, $aJsBefore, $f);
  156. // add newly added js&css files in static mode
  157. $sCss = $sJs = '';
  158. foreach ($aCssNew as $a)
  159. $sCss .= $oTemplate->addCss($a['url'], true);
  160. foreach ($aJsNew as $a)
  161. $sJs .= $oTemplate->addJs($a['url'], true);
  162. return $sJs . $sCss . $sContent;
  163. }
  164. public function getIncludes ()
  165. {
  166. $oTemplate = BxDolTemplate::getInstance();
  167. // generate page code
  168. $this->getCode();
  169. // get js&css after the page code is generated
  170. $aCss = $oTemplate->getCss();
  171. $aJs = $oTemplate->getJs();
  172. // add js&css files in static mode
  173. $sResult = '';
  174. foreach ($aCss as $a)
  175. $sResult .= $oTemplate->addCss($a['url'], true);
  176. foreach ($aJs as $a)
  177. $sResult .= $oTemplate->addJs($a['url'], true);
  178. // add js translations
  179. $sResult .= $oTemplate->getJsTranslation(true);
  180. return $sResult;
  181. }
  182. /**
  183. * Get page code with automatic caching, adding necessary css/js files and system template vars.
  184. * @return string.
  185. */
  186. public function getCode ()
  187. {
  188. if (bx_get('dynamic') && ($iBlockId = (int)bx_get('pageBlock'))) {
  189. if (self::isLockedFromUnauthenticated($this->_aObject['uri']) || !$this->_isVisiblePage($this->_aObject)) {
  190. header('HTTP/1.0 403 Forbidden');
  191. exit;
  192. }
  193. /**
  194. * @hooks
  195. * @hookdef hook-system-page_output_block 'system', 'page_output_block' - hook with page block data to be output
  196. * - $unit_name - equals `system`
  197. * - $action - equals `page_output_block`
  198. * - $object_id - not used
  199. * - $sender_id - not used
  200. * - $extra_params - array of additional params with the following array keys:
  201. * - `page_name` - [string] page object name
  202. * - `page_object` - [object] an instance of page class, @see BxDolPage
  203. * - `page_query` - [object] an instance of page related query class
  204. * - `block_id` - [int] block id
  205. * @hook @ref hook-system-page_output_block
  206. */
  207. bx_alert('system', 'page_output_block', 0, false, [
  208. 'page_name' => $this->_sObject,
  209. 'page_object' => $this,
  210. 'page_query' => $this->_oQuery,
  211. 'block_id' => (int)$iBlockId,
  212. ]);
  213. header( 'Content-type:text/html;charset=utf-8' );
  214. BxDolTemplate::getInstance()->collectingStart();
  215. $s = $this->_getBlockOnlyCode($iBlockId);
  216. $sCss = bx_get('includedCss');
  217. $sJs = bx_get('includedJs');
  218. echo BxDolTemplate::getInstance()->collectingEndGetCode($sCss ? @json_decode($sCss) : [], $sJs ? @json_decode($sJs) : []);
  219. echo $s;
  220. exit;
  221. }
  222. if (($mixedAvailable = $this->_isAvailablePage($this->_aObject)) !== true)
  223. return $this->_getPageNotFoundMsg($mixedAvailable);
  224. if (($mixedVisible = $this->_isVisiblePage($this->_aObject)) !== true)
  225. return $this->_getPageAccessDeniedMsg($mixedVisible);
  226. $this->_addJsCss();
  227. if (!$this->_bSubPage) {
  228. $this->_addSysTemplateVars();
  229. $this->_selectMenu();
  230. $this->_setSubmenu(array());
  231. }
  232. if (bx_is_dbg() || !getParam('sys_page_cache_enable') || !$this->_aObject['cache_lifetime'] || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  233. $sPageCode = $this->_getPageCode();
  234. }
  235. else {
  236. $oCache = $this->_getPageCacheObject();
  237. $sKey = $this->_getPageCacheKey();
  238. $sKeyCssJs = 'cssjs_' . $sKey;
  239. $mixedRet = $oCache->getData($sKey, $this->_aObject['cache_lifetime']);
  240. $aRetCssJs = $oCache->getData($sKeyCssJs, $this->_aObject['cache_lifetime']);
  241. if ($mixedRet !== null && $aRetCssJs !== null) {
  242. BxDolTemplate::getInstance()->collectingStart();
  243. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  244. $sPageCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  245. $sPageCode .= $mixedRet;
  246. } else {
  247. BxDolTemplate::getInstance()->collectingStart();
  248. $sPageCode = $this->_getPageCode();
  249. $aPageCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  250. $oCache->setData($sKey, $sPageCode, $this->_aObject['cache_lifetime']);
  251. $oCache->setData($sKeyCssJs, $aPageCssJs, $this->_aObject['cache_lifetime']);
  252. }
  253. }
  254. /**
  255. * @hooks
  256. * @hookdef hook-system-page_output 'system', 'page_output' - hook with page data to be output
  257. * - $unit_name - equals `system`
  258. * - $action - equals `page_output`
  259. * - $object_id - not used
  260. * - $sender_id - not used
  261. * - $extra_params - array of additional params with the following array keys:
  262. * - `page_name` - [string] page object name
  263. * - `page_object` - [object] an instance of page class, @see BxDolPage
  264. * - `page_query` - [object] an instance of page related query class
  265. * - `page_code` - [string] by ref, final page code to be output, can be overridden in hook processing
  266. * - `sub_page` - [boolean] if the page is used as subpage of some parent page
  267. * @hook @ref hook-system-page_output
  268. */
  269. bx_alert('system', 'page_output', 0, false, [
  270. 'page_name' => $this->_sObject,
  271. 'page_object' => $this,
  272. 'page_query' => $this->_oQuery,
  273. 'page_code' => &$sPageCode,
  274. 'page_code_ref' => &$sPageCode,
  275. 'sub_page' => $this->_bSubPage,
  276. ]);
  277. if (!$this->_bSubPage)
  278. $sPageCode .= $this->getJsScript();
  279. return $sPageCode;
  280. }
  281. public function getJsClassName()
  282. {
  283. return $this->_sJsClassName;
  284. }
  285. public function getJsObjectName()
  286. {
  287. return $this->_sJsObjectName;
  288. }
  289. function _wrapInTagJsCode($sCode)
  290. {
  291. return "<script language=\"javascript\">\n<!--\n" . $sCode . "\n-->\n</script>";
  292. }
  293. public function getJsScript()
  294. {
  295. $sJsObjName = $this->getJsObjectName();
  296. $sJsObjClass = $this->getJsClassName();
  297. $sCode = "if(window['" . $sJsObjName . "'] == undefined) var " . $sJsObjName . " = new " . $sJsObjClass . "(" . json_encode(array(
  298. 'sObjName' => $sJsObjName,
  299. 'sObject' => $this->_sObject,
  300. 'sRootUrl' => BX_DOL_URL_ROOT,
  301. 'aHtmlIds' => $this->_aHtmlIds,
  302. 'isStickyColumns' => $this->_bStickyColumns
  303. )) . ");";
  304. return $this->_wrapInTagJsCode($sCode);
  305. }
  306. /**
  307. * Is page cover enabled.
  308. * @return string
  309. */
  310. public function isPageCover()
  311. {
  312. $bResult = false;
  313. switch((int)$this->_aObject['cover']) {
  314. case 1: //--- Enabled for all
  315. $bResult = true;
  316. break;
  317. case 2: //--- Enabled for visitors only
  318. $bResult = !isLogged();
  319. break;
  320. case 3: //--- Enabled for members only
  321. $bResult = isLogged();
  322. break;
  323. }
  324. return $bResult;
  325. }
  326. public function setSubPage($b = true)
  327. {
  328. $this->_bSubPage = $b;
  329. }
  330. public function setPageCover($bCover = true)
  331. {
  332. $this->_aObject['cover'] = (bool)$bCover;
  333. }
  334. public function getPageCoverImage($bTranscoder = true)
  335. {
  336. $iId = (int)$this->_aObject['cover_image'];
  337. if(empty($iId)) {
  338. $iId = (int)getParam('sys_site_cover_common');
  339. if(empty($iId))
  340. return array();
  341. }
  342. $aResult = array(
  343. 'id' => $iId
  344. );
  345. if($bTranscoder)
  346. $aResult['transcoder'] = BX_DOL_TRANSCODER_OBJ_COVER;
  347. else
  348. $aResult['object'] = $this->_sStorage;
  349. return $aResult;
  350. }
  351. public function getPageCoverParams()
  352. {
  353. $aParams = [
  354. 'title' => $this->_getPageTitle(),
  355. 'actions' => '',
  356. 'bx_if:image' => [
  357. 'condition' => false,
  358. 'content' => []
  359. ],
  360. 'bx_if:icon' => [
  361. 'condition' => false,
  362. 'content' => []
  363. ],
  364. ];
  365. if(($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  366. $aCoverParams = $oMenuSubmenu->getPageCoverParams();
  367. if(!empty($aCoverParams) && is_array($aCoverParams))
  368. $aParams = $aCoverParams;
  369. }
  370. if(!empty($this->_aObject['cover_title'])) {
  371. $sCoverTitle = _t($this->_aObject['cover_title']);
  372. if($sCoverTitle && strcmp($sCoverTitle, $this->_aObject['cover_title']) != 0)
  373. $aParams['title'] = $sCoverTitle;
  374. }
  375. return $aParams;
  376. }
  377. /**
  378. * Get block title.
  379. * @return string
  380. */
  381. public function getBlockTitle ($aBlock)
  382. {
  383. return $this->_replaceMarkers(_t($aBlock['title']), array('block_id' => $aBlock['id']));
  384. }
  385. /**
  386. * Get block description.
  387. * @return string
  388. */
  389. public function getBlockDescription ($aBlock)
  390. {
  391. return $this->_replaceMarkers(_t($aBlock['description']), ['block_id' => $aBlock['id']]);
  392. }
  393. /**
  394. * Get block icon.
  395. * @return string
  396. */
  397. public function getBlockIcon ($aBlock)
  398. {
  399. return $aBlock['icon'] ?? '';
  400. }
  401. /**
  402. * Get help control if help is available for the block.
  403. * @return string
  404. */
  405. public function getBlockHelp ($aBlock)
  406. {
  407. if(empty($aBlock['help']) || empty(strip_tags(_t($aBlock['help']))))
  408. return '';
  409. return $this->_oTemplate->parseHtmlByName('designbox_help.html', array(
  410. 'js_object' => $this->_sJsObjectName,
  411. 'block_id' => $aBlock['id']
  412. ));
  413. }
  414. /**
  415. * Get code to load block asyncroniously
  416. * @param $aBlock block code
  417. * @param $iAsync if greater than 0 the it defines loading indicator for the block
  418. * @return HTML code string
  419. */
  420. public function getBlockAsyncCode($aBlock, $iAsync)
  421. {
  422. $aContentPlaceholder = $this->_oQuery->getPageBlockContentPlaceholder($iAsync);
  423. if (!$aContentPlaceholder)
  424. return _t('_sys_txt_error_occured');
  425. $oTemplate = $this->_oTemplate;
  426. if ('system' != $aContentPlaceholder['module']) {
  427. $oModule = BxDolModule::getInstance($aContentPlaceholder['module']);
  428. if (!$oModule)
  429. return _t('_sys_txt_error_occured');
  430. $oTemplate = $oModule->_oTemplate;
  431. }
  432. return $oTemplate->parseHtmlByName($aContentPlaceholder['template'], $aBlock);
  433. }
  434. /**
  435. * Get page array with all cells and blocks
  436. */
  437. public function getPageAPI ($aBlocks = [])
  438. {
  439. define('BX_API_PAGE', true);
  440. $aGetParams = bx_get('params');
  441. $bGetParams = $aGetParams !== false;
  442. $sQueryString = '';
  443. if($bGetParams && !empty($aGetParams[2]) && ($_aGetParams = json_decode($aGetParams[2], true)) && is_array($_aGetParams)) {
  444. $sQueryString = http_build_query($_aGetParams);
  445. parse_str($sQueryString, $aQueryString);
  446. $_GET = array_merge($_GET, $aQueryString);
  447. }
  448. $bLogged = isLogged();
  449. $bIsAvailable = $this->_isAvailablePage($this->_aObject);
  450. $bIsVisible = $this->_isVisiblePage($this->_aObject);
  451. //TODO: If page isn't Available or isn't Visible then we should exit with related Error Codes immediately.
  452. $sMetaTitle = $this->_getPageMetaTitle();
  453. $sName = $this->_getPageTitle();
  454. $sUri = $this->_aObject['uri'];
  455. $sUrl = ($bGetParams && !empty($aGetParams[0]) ? $aGetParams[0] : $sUri) . ($sQueryString != '' ? '?' . $sQueryString : '');
  456. $sModule = $this->getModule();
  457. $aElements = $this->getPageBlocksAPI($aBlocks);
  458. if(!$bLogged && !in_array($sUri, ['home', 'login', 'create-account']) && ($sAutoProfile = getParam('sys_account_default_profile_type'))) {
  459. $sRedirect = getParam($sAutoProfile . '_redirect_aadd');
  460. if($sRedirect == 'last') {
  461. $aAutoProfileConfig = bx_srv($sAutoProfile, 'module_config');
  462. if($sUri != ($aAutoProfileConfig['URI_ADD_ENTRY'] ?? ''))
  463. BxDolSession::getInstance()->setValue('custom-referrer', $sUrl);
  464. }
  465. }
  466. $a = [
  467. 'id' => $this->_aObject['id'],
  468. 'title' => $sMetaTitle ? $sMetaTitle : $sName,
  469. 'name' => $sName,
  470. 'description' => $this->_getPageMetaDesc(),
  471. 'keywords' => $this->_getPageMetaKeywords(),
  472. 'image' => '',
  473. 'uri' => $sUri,
  474. 'url' => $sUrl,
  475. 'author' => $this->_aObject['author'],
  476. 'added' => $this->_aObject['added'],
  477. 'module' => $sModule,
  478. 'type' => $this->getType (),
  479. 'layout' => !$bLogged && $sUri == 'home' && ($sReplacement = getParam('sys_api_root_page_guest')) == 'splash' ? $sReplacement : str_replace('.html', '', $this->_aObject['template']),
  480. 'layout_parsed' => !is_numeric(trim(current(array_keys($aElements)), 'cell_')),
  481. 'cover_block' => '',
  482. 'cover' => $this->_aObject['cover'],
  483. 'menu_top' => '',
  484. 'menu' => '',
  485. 'menu_bottom' => '',
  486. 'menu_add' => '',
  487. 'config' => $this->_aObject['config_api'],
  488. 'elements' => $aElements
  489. ];
  490. $sMenuTop = getParam('sys_api_menu_top');
  491. if (!empty($sMenuTop) && ($oMenuTop = BxDolMenu::getObjectInstance($sMenuTop)) !== false)
  492. $a['menu_top'] = $oMenuTop->getCodeAPI();
  493. if (($oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu')) !== false) {
  494. if(($sSubmenu = $this->getSubMenu()) !== '')
  495. $oMenuSubmenu->setObjectSubmenu($sSubmenu, []);
  496. $a['menu'] = $oMenuSubmenu->getCodeAPI();
  497. if($sModule && $sModule != 'system' && (bx_srv('system', 'is_module_content', [$sModule]) || bx_srv('system', 'is_module_context', [$sModule])) && ($oModule = BxDolModule::getInstance($sModule))) {
  498. $CNF = &$oModule->_oConfig->CNF;
  499. $aMenuAdd = [
  500. 'name' => $oModule->getName(),
  501. 'title' => BxDolModule::getTitle($oModule->_oConfig->getUri()),
  502. 'icon' => !empty($CNF['ICON']) ? $CNF['ICON'] : '',
  503. ];
  504. $sUrlAdd = '';
  505. if(($sKey = 'URI_ADD_ENTRY') && !empty($CNF[$sKey]))
  506. $sUrlAdd = $CNF[$sKey];
  507. if(!$sUrlAdd && ($sKey = 'URI_EDIT_ENTRY') && !empty($CNF[$sKey]))
  508. $sUrlAdd = str_replace('edit-', 'create-', $CNF[$sKey]);
  509. if($sUrlAdd)
  510. $aMenuAdd['add_url'] = $sUrlAdd;
  511. $a['menu'] = array_merge($a['menu'], $aMenuAdd);
  512. }
  513. }
  514. /**
  515. * Profile/Context view page.
  516. */
  517. if(isset($this->_aProfileInfo)) {
  518. $oProfileModule = BxDolModule::getInstance($this->_aProfileInfo['type']);
  519. if($oProfileModule !== null)
  520. $aProfileContentInfo = $oProfileModule->_oDb->getContentInfoById($this->_aProfileInfo['content_id']);
  521. if(!empty($aProfileContentInfo) && is_array($aProfileContentInfo)) {
  522. $CNF = &$oProfileModule->_oConfig->CNF;
  523. /**
  524. * Process page cover with related menus and lists.
  525. */
  526. if(BxDolCover::getInstance($this)->isCover()) {
  527. $aProfileInfo = BxDolProfile::getData($this->_aProfileInfo['id'], [
  528. 'get_avatar' => 'getAvatarBig',
  529. 'with_info' => true
  530. ]);
  531. $oProfile = BxDolProfile::getInstance($this->_aProfileInfo['id']);
  532. $aProfileInfo['online'] = $oProfile->isOnline();
  533. unset($aProfileInfo['info']['profile_email']);
  534. unset($aProfileInfo['info']['profile_last_active']);
  535. unset($aProfileInfo['info']['profile_ip']);
  536. $a['cover_block'] = [
  537. 'profile' => $aProfileInfo,
  538. 'actions_menu' => '',
  539. 'meta_menu' => '',
  540. 'badges' => $oProfile->getBadges(),
  541. 'cover' => $oProfileModule->serviceGetCover($this->_aProfileInfo['content_id']),
  542. 'allow_edit' => $oProfileModule->checkAllowedChangeCover($aProfileContentInfo) === CHECK_ACTION_RESULT_ALLOWED
  543. ];
  544. if(!empty($CNF['OBJECT_MENU_VIEW_ENTRY_META']) && ($oMetaMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_VIEW_ENTRY_META'])) !== false)
  545. $a['cover_block']['meta_menu'] = $oMetaMenu->getCodeAPI();
  546. if(!empty($CNF['OBJECT_CONNECTIONS']) && ($oConnection = BxDolConnection::getObjectInstance($CNF['OBJECT_CONNECTIONS'])) !== false)
  547. $a['cover_block']['members_list'] = $oConnection->getConnectedListAPI($this->_aProfileInfo['id'], false, BX_CONNECTIONS_CONTENT_TYPE_INITIATORS, 10);
  548. if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL']) && ($oActionMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_ALL'])) !== false)
  549. $a['cover_block']['actions_menu'] = $oActionMenu->getCodeAPI();
  550. }
  551. /**
  552. * Process page mange menu.
  553. */
  554. if(($oMenuManage = BxDolMenu::getObjectInstance('sys_site_manage')) !== false) {
  555. if(!empty($CNF['FIELD_ID']))
  556. $oMenuManage->setContentId($aProfileContentInfo[$CNF['FIELD_ID']]);
  557. $aObjectManage = [];
  558. if(!empty($CNF['OBJECT_MENU_MANAGE_VIEW_ENTRY']))
  559. $aObjectManage = [$CNF['OBJECT_MENU_MANAGE_VIEW_ENTRY']];
  560. else if(!empty($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE']))
  561. $aObjectManage = [$CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE'], true];
  562. call_user_func_array([$oMenuManage, 'setObjectManage'], $aObjectManage);
  563. $aMenuManage = $oMenuManage->getCodeAPI();
  564. if(!empty($aMenuManage) && is_array($aMenuManage)) {
  565. if(isset($a['cover_block'], $a['cover_block']['actions_menu']) && is_array($a['cover_block']['actions_menu']))
  566. $a['cover_block']['actions_menu']['items'] = array_merge($a['cover_block']['actions_menu']['items'], $aMenuManage['items']);
  567. else
  568. $a['cover_block']['actions_menu'] = $aMenuManage;
  569. }
  570. }
  571. }
  572. }
  573. if($bLogged && ($o = BxDolProfile::getInstance()) !== false) {
  574. $iLogged = $o->id();
  575. $a['user'] = BxDolProfile::getDataForPage($o);
  576. if(($sContextSwitcher = getParam('sys_api_context_switcher')) && ($sContextConnection = getParam('sys_api_context_connection'))) {
  577. $aCpIds = bx_srv($sContextSwitcher, 'get_participating_profiles', [$iLogged, ($sContextConnection != 'participants' ? $sContextConnection : false)]);
  578. $aContexts = [];
  579. foreach($aCpIds as $iCpId) {
  580. $aProfileData = BxDolProfile::getData($iCpId);
  581. if($aProfileData['display_name'] === false && $aProfileData['url'] === false )
  582. continue;
  583. $aContexts[] = $aProfileData;
  584. }
  585. $aInvitedTo = [];
  586. if(($aInvitations = bx_srv($sContextSwitcher, 'invitations', [$iLogged, true])) && is_array($aInvitations))
  587. foreach($aInvitations as $aInvitation)
  588. $aInvitedTo[] = BxDolProfile::getData($aInvitation['group_profile_id']);
  589. $aCurrent = [];
  590. if(($iId = bx_get('id')) !== false) {
  591. $aInfo = bx_srv($sModule, 'get_info', [(int)$iId, false]);
  592. if($aInfo && (($iValue = (int)($aInfo['allow_view_to'] ?? 0)) || ($iValue = (int)($aInfo['object_privacy_view'] ?? 0)))) {
  593. $mixedContext = false;
  594. if($iValue < 0) {
  595. $aContext = BxDolProfileQuery::getInstance()->getInfoById(abs($iValue));
  596. if($aContext && $aContext['type'] == $sContextSwitcher)
  597. $mixedContext = (int)$aContext['id'];
  598. }
  599. else if($sModule == $sContextSwitcher)
  600. $mixedContext = BxDolProfile::getInstanceByContentAndType((int)$aInfo['id'], $sModule);
  601. if($mixedContext !== false)
  602. $aCurrent = BxDolProfile::getData($mixedContext);
  603. }
  604. }
  605. else if(($iContextPid = bx_get('profile_id')) !== false && ($oContext = BxDolProfile::getInstance($iContextPid)) !== false)
  606. $aCurrent = BxDolProfile::getData($oContext);
  607. $_sSample = '_' . $sContextSwitcher . '_txt_sample_single';
  608. $sSample = _t($_sSample);
  609. if(strcmp($_sSample, $sSample) == 0)
  610. $sSample = _t('_Context');
  611. $a['context'] = [
  612. 'list' => $aContexts,
  613. 'invitations' => $aInvitedTo,
  614. 'current' => $aCurrent,
  615. 'links' => []
  616. ];
  617. if(bx_srv($sContextSwitcher, 'check_allowed', ['add']) === CHECK_ACTION_RESULT_ALLOWED)
  618. $a['context']['links'][] = [
  619. 'title' => _t('_api_create_context', $sSample),
  620. 'icon' => 'Plus',
  621. 'url' => bx_api_get_relative_url(bx_srv($sContextSwitcher, 'get_link_add'))
  622. ];
  623. }
  624. }
  625. if($bIsAvailable !== true)
  626. $a['page_status'] = 404;
  627. else if($bIsVisible !== true)
  628. $a['page_status'] = 403;
  629. return array_merge($a, [
  630. 'version' => ($sVersion = bx_get_ver()) && $sVersion == BX_DOL_VERSION ? $sVersion : 'invalid',
  631. // Interface sounds settings ride along with every page, so the apps never wait on them to play a sound.
  632. 'sounds' => BxDolSounds::getInstance()->getConfig(),
  633. 'ts' => time()
  634. ]);
  635. }
  636. public function getPageContentAPI($aBlocks = [])
  637. {
  638. $sModule = $this->getModule();
  639. $sName = $this->_getPageTitle();
  640. $sMetaTitle = $this->_getPageMetaTitle();
  641. return [
  642. 'module' => $sModule,
  643. 'title' => $sMetaTitle ? $sMetaTitle : $sName,
  644. 'elements' => $this->getPageBlocksAPI($aBlocks),
  645. ];
  646. }
  647. public function getPageBlocksAPI($aBlocks = [])
  648. {
  649. $aHiddenOn = [
  650. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'phone',
  651. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'tablet',
  652. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'desktop',
  653. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'mobile-app'
  654. ];
  655. $aFieldsUnset = ['object', 'cell_id', 'title_system', 'class', 'submenu', 'tabs', 'async', 'visible_for_levels', 'type', 'text', 'text_updated', 'cache_lifetime', 'active', 'active_api', 'copyable', 'deletable', 'order'];
  656. $bBlocks = !empty($aBlocks) && is_array($aBlocks);
  657. $aCells = $this->_oQuery->getPageBlocks(true);
  658. foreach($aCells as $sKey => &$aCell) {
  659. foreach($aCell as $i => &$aBlock) {
  660. if(!$this->_isVisibleBlock($aBlock)) {
  661. unset($aCells[$sKey][$i]);
  662. continue;
  663. }
  664. $this->processPageBlock($aCells[$sKey][$i], true);
  665. $aBlock = $aCells[$sKey][$i];
  666. $sSource = '';
  667. if($aBlock['type'] == 'service') {
  668. $aContent = @unserialize($aBlock['content']);
  669. if(isset($aContent['module'], $aContent['method']))
  670. $sSource = $aContent['module'] . ':' . $aContent['method'];
  671. }
  672. else if($aBlock['type'] == 'custom')
  673. $sSource = $aBlock['content'];
  674. else
  675. $sSource = 'system:block_' . $aBlock['id'];
  676. if($bBlocks && !in_array($sSource, $aBlocks)) {
  677. unset($aCells[$sKey][$i]);
  678. continue;
  679. }
  680. if(($sK = 'config_api') && !empty($aBlock[$sK])) {
  681. $aConfigApi = json_decode($aBlock[$sK], true);
  682. if(!empty($aConfigApi) && is_array($aConfigApi))
  683. $aBlock[$sK] = $aConfigApi;
  684. }
  685. if(($sK = 'help') && !empty($aBlock[$sK]))
  686. $aBlock[$sK] = _t($aBlock[$sK]);
  687. $mBlock = $aBlock['content'];
  688. if(($sFunc = '_getBlock' . bx_gen_method_name($aBlock['type'])) && method_exists($this, $sFunc))
  689. $mBlock = $this->$sFunc($aBlock);
  690. if(empty($mBlock)) {
  691. unset($aCells[$sKey][$i]);
  692. continue;
  693. }
  694. $sIcon = '';
  695. if(($sIcon = $this->getBlockIcon($aBlock))) {
  696. list($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_oTemplate->getTemplateFunctions()->getIcon($sIcon);
  697. if($sIcon)
  698. $sIcon = BxDolIconset::getObjectInstance()->getIcon($sIcon);
  699. else if($sIconHtml)
  700. $sIcon = $sIconHtml;
  701. }
  702. $aCells[$sKey][$i] = array_merge($aCells[$sKey][$i], [
  703. 'title' => isset($mBlock['title']) ? $mBlock['title'] : $this->getBlockTitle($aBlock),
  704. 'description' => isset($mBlock['description']) ? $mBlock['description'] : $this->getBlockDescription($aBlock),
  705. 'icon' => $sIcon,
  706. 'content' => isset($mBlock['content']) ? $mBlock['content'] : $mBlock,
  707. 'menu' => isset($mBlock['menu']) ? $mBlock['menu'] : '',
  708. 'source' => $sSource
  709. ]);
  710. if(($sK = 'hidden_on') && !empty($aBlock[$sK])) {
  711. $aHoResults = [];
  712. foreach($aHiddenOn as $iHiddenOn => $sHiddenOn)
  713. if((int)$aBlock[$sK] & $iHiddenOn)
  714. $aHoResults[] = $sHiddenOn;
  715. $aCells[$sKey][$i][$sK] = $aHoResults;
  716. }
  717. else
  718. $aCells[$sKey][$i][$sK] = false;
  719. $aCells[$sKey][$i] = array_diff_key($aCells[$sKey][$i], array_flip($aFieldsUnset));
  720. }
  721. }
  722. return array_map('array_values', $aCells);
  723. }
  724. /**
  725. * Get page code vars
  726. * @return string
  727. */
  728. protected function _getPageCodeVars ()
  729. {
  730. $aHiddenOn = array(
  731. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  732. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  733. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  734. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  735. );
  736. $aVars = array (
  737. 'page_id' => 'bx-page-' . $this->_aObject['uri'],
  738. 'bx_if:show_layout_row_dump' => array(
  739. 'condition' => false,
  740. 'content' => array()
  741. )
  742. );
  743. $aBlocks = $this->_oQuery->getPageBlocks();
  744. foreach ($aBlocks as $sKey => $aCell) {
  745. $sCell = '';
  746. foreach ($aCell as $aBlock) {
  747. $this->processPageBlock($aBlock, false);
  748. $sContentWithBox = $this->_getBlockCodeWithCache($aBlock, $aBlock['async']);
  749. $sClassAdd = '';
  750. if(!empty($aBlock['class']))
  751. $sClassAdd .= ' ' . $aBlock['class'];
  752. if(!empty($aBlock['hidden_on']))
  753. foreach($aHiddenOn as $iHiddenOn => $sHiddenOnClass)
  754. if((int)$aBlock['hidden_on'] & $iHiddenOn)
  755. $sClassAdd .= ' ' . $sHiddenOnClass;
  756. if ($sContentWithBox)
  757. $sCell .= $this->_oTemplate->parseHtmlByName('designbox_container.html', array(
  758. 'class_add' => $sClassAdd,
  759. 'bx_if:show_html_id' => array(
  760. 'condition' => true,
  761. 'content' => array(
  762. 'html_id' => 'bx-page-block-' . $aBlock['id']
  763. ),
  764. ),
  765. 'content' => $sContentWithBox
  766. ));
  767. }
  768. $aVars[$sKey] = $sCell;
  769. }
  770. return $aVars;
  771. }
  772. /**
  773. * Process block values, especially if someting need to be overrided
  774. */
  775. protected function processPageBlock(&$aBlock, $bApi = false)
  776. {
  777. }
  778. /**
  779. * Get page code only.
  780. * @return string
  781. */
  782. protected function _getPageCode ()
  783. {
  784. $aVars = $this->_getPageCodeVars ();
  785. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  786. }
  787. /**
  788. * Get one block code only.
  789. * @return string
  790. */
  791. protected function _getBlockOnlyCode ($iBlockId)
  792. {
  793. if (!($aBlock = $this->_oQuery->getPageBlock((int)$iBlockId)))
  794. return '';
  795. return $this->_getBlockCodeWithCache($aBlock, 0);
  796. }
  797. protected function _getBlockCodeWithCache(&$aBlock, $iAsync = 0)
  798. {
  799. if (bx_is_dbg() || !getParam('sys_pb_cache_enable') || !$aBlock['cache_lifetime'] || $iAsync || (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST')) {
  800. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  801. }
  802. else {
  803. $oCache = $this->_getBlockCacheObject();
  804. $sKey = $this->_getBlockCacheKey(false, $aBlock);
  805. $sKeyCssJs = 'cssjs_' . $sKey;
  806. $mixedRet = $oCache->getData($sKey, $aBlock['cache_lifetime']);
  807. $aRetCssJs = $oCache->getData($sKeyCssJs, $aBlock['cache_lifetime']);
  808. if ($mixedRet !== null && $aRetCssJs !== null) {
  809. BxDolTemplate::getInstance()->collectingStart();
  810. BxDolTemplate::getInstance()->collectingInject($aRetCssJs['css'], $aRetCssJs['js']);
  811. $sBlockCode = BxDolTemplate::getInstance()->collectingEndGetCode();
  812. $sBlockCode .= $mixedRet;
  813. } else {
  814. BxDolTemplate::getInstance()->collectingStart();
  815. $sBlockCode = $this->_getBlockCode($aBlock, $iAsync);
  816. $aBlockCssJs = BxDolTemplate::getInstance()->collectingEndGetCode(array(), array(), 'array');
  817. $oCache->setData($sKey, $sBlockCode, $aBlock['cache_lifetime']);
  818. $oCache->setData($sKeyCssJs, $aBlockCssJs, $aBlock['cache_lifetime']);
  819. }
  820. }
  821. return $sBlockCode;
  822. }
  823. /**
  824. * Get block code.
  825. * @return string
  826. */
  827. protected function _getBlockCode(&$aBlock, $iAsync = 0)
  828. {
  829. $sContentWithBox = '';
  830. $oFunctions = $this->_oTemplate->getTemplateFunctions();
  831. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPageBlock(_t($aBlock['title']), $aBlock['id']);
  832. $aDbNoTitle = [BX_DB_CONTENT_ONLY, BX_DB_PADDING_CONTENT_ONLY, BX_DB_NO_CAPTION, BX_DB_PADDING_NO_CAPTION];
  833. $sTitle = $this->getBlockTitle($aBlock);
  834. $sDescription = $this->getBlockDescription($aBlock);
  835. $sIcon = $this->getBlockIcon($aBlock);
  836. $sHelp = $this->getBlockHelp($aBlock);
  837. $sFunc = '_getBlock' . ucfirst($aBlock['type']);
  838. $bBlockVisible = $this->_isVisibleBlock($aBlock);
  839. if ($iAsync && $bBlockVisible) {
  840. $iDesignboxId = $aBlock['designbox_id'];
  841. $sHelpTitle = $sHelpContent = '';
  842. if(!in_array($iDesignboxId, $aDbNoTitle))
  843. $sHelpTitle = $sHelp;
  844. else
  845. $sHelpContent = $sHelp;
  846. $sContent = $this->getBlockAsyncCode($aBlock, $iAsync);
  847. $aParams = [
  848. ($sBlTitle = $sTitle . $sHelpTitle) && ($sDescription || $sIcon) ? [$sBlTitle, $sDescription, $sIcon] : $sBlTitle,
  849. $sContent . $sHelpContent,
  850. $iDesignboxId
  851. ];
  852. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  853. }
  854. elseif ($bBlockVisible && method_exists($this, $sFunc)) {
  855. $mixedContent = $this->$sFunc($aBlock);
  856. if(!empty($aBlock['content_empty'])) {
  857. $sEmpty = _t($aBlock['content_empty']);
  858. $sRegExp = '/(<[A-Za-z0-9]+\b.[^<]*[\pPi\pPf]bx\-msg\-box\b.*[\pPi\pPf][^>]*>)(' . _t('_Empty') . ')(<\/[A-Za-z0-9]+>)/su';
  859. if(is_string($mixedContent))
  860. $mixedContent = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent);
  861. else if(is_array($mixedContent) && !empty($mixedContent['content']))
  862. $mixedContent['content'] = preg_replace($sRegExp, "\${1}" . $sEmpty . "\${3}", $mixedContent['content']);
  863. }
  864. $this->_oQuery->setReadOnlyMode(true);
  865. if(is_array($mixedContent) && !empty($mixedContent['content'])) {
  866. $iDesignboxId = isset($mixedContent['designbox_id']) ? $mixedContent['designbox_id'] : $aBlock['designbox_id'];
  867. if(!empty($mixedContent['markers']) && is_array($mixedContent['markers'])) {
  868. $this->addMarkers($mixedContent['markers']);
  869. $sTitle = $this->getBlockTitle($aBlock);
  870. $sDescription = $this->getBlockDescription($aBlock);
  871. }
  872. $sHelpTitle = $sHelpContent = '';
  873. if(!in_array($iDesignboxId, $aDbNoTitle))
  874. $sHelpTitle = $sHelp;
  875. else
  876. $sHelpContent = $sHelp;
  877. $aParams = [
  878. ($sBlTitle = ($mixedContent['title'] ?? $sTitle) . $sHelpTitle) && ($sDescription || $sIcon) ? [$sBlTitle, $sDescription, $sIcon] : $sBlTitle,
  879. $mixedContent['content'] . $sHelpContent,
  880. $iDesignboxId
  881. ];
  882. $mixedMenu = false;
  883. if(isset($mixedContent['menu']))
  884. $mixedMenu = $mixedContent['menu'];
  885. else if(!empty($aBlock['submenu']))
  886. $mixedMenu = $aBlock['submenu'];
  887. $aParams[] = $mixedMenu;
  888. if(isset($mixedContent['buttons']))
  889. $aParams[] = $mixedContent['buttons'];
  890. else if($mixedMenu)
  891. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  892. if(isset($mixedContent['class']))
  893. $aBlock['class'] = $mixedContent['class'];
  894. }
  895. else if(is_string($mixedContent) && !empty($mixedContent)) {
  896. $iDesignboxId = $aBlock['designbox_id'];
  897. $sHelpTitle = $sHelpContent = '';
  898. if(!in_array($iDesignboxId, $aDbNoTitle))
  899. $sHelpTitle = $sHelp;
  900. else
  901. $sHelpContent = $sHelp;
  902. $aParams = array(
  903. ($sBlTitle = $sTitle . $sHelpTitle) && ($sDescription || $sIcon) ? [$sBlTitle, $sDescription, $sIcon] : $sBlTitle,
  904. $mixedContent . $sHelpContent,
  905. $iDesignboxId
  906. );
  907. $mixedMenu = !empty($aBlock['submenu']) ? $aBlock['submenu'] : false;
  908. $aParams[] = $mixedMenu;
  909. if($mixedMenu)
  910. $aParams[] = (int)$aBlock['tabs'] == 1 ? true : array();
  911. }
  912. if(!empty($aParams))
  913. $sContentWithBox = call_user_func_array(array($oFunctions, 'designBoxContent'), $aParams);
  914. }
  915. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPageBlock($aBlock['id'], $sContentWithBox ? false : true, false );
  916. return $sContentWithBox;
  917. }
  918. /**
  919. * Add necessary js and css files.
  920. */
  921. protected function _addJsCss()
  922. {
  923. $this->_oTemplate->addJs(array('BxDolPage.js', 'theia-sticky-sidebar/theia-sticky-sidebar.umd.js', 'theia-sticky-sidebar/ResizeSensor.min.js'));
  924. $this->_oTemplate->addCss('page_layouts.css');
  925. }
  926. /**
  927. * Set system template variables, like page title, meta description, meta keywords and meta robots.
  928. */
  929. protected function _addSysTemplateVars ()
  930. {
  931. $oTemplate = BxDolTemplate::getInstance();
  932. $sPageTitle = $this->_getPageTitle();
  933. if ($sPageTitle)
  934. $oTemplate->setPageHeader ($sPageTitle);
  935. $sMetaTitle = $this->_getPageMetaTitle();
  936. if ($sMetaTitle)
  937. $oTemplate->setPageMetaTitle ($sMetaTitle);
  938. $sMetaDesc = $this->_getPageMetaDesc();
  939. if ($sMetaDesc)
  940. $oTemplate->setPageDescription ($sMetaDesc);
  941. $sMetaRobots = $this->_getPageMetaRobots();
  942. if ($sMetaRobots)
  943. $oTemplate->setPageMetaRobots ($sMetaRobots);
  944. $sMetaImage = $this->_getPageMetaImage();
  945. if ($sMetaImage)
  946. $oTemplate->addPageMetaImage($sMetaImage);
  947. $sMetaKeywords = $this->_getPageMetaKeywords();
  948. if ($sMetaKeywords)
  949. $oTemplate->addPageKeywords ($sMetaKeywords);
  950. }
  951. /**
  952. * Select menu from page properties.
  953. */
  954. protected function _selectMenu ()
  955. {
  956. BxDolMenu::setSelectedGlobal ($this->_aObject['module'], $this->_aObject['uri']);
  957. }
  958. /**
  959. * Set page submenu if it's specified
  960. */
  961. protected function _setSubmenu ($aParams)
  962. {
  963. if(empty($this->_aObject['submenu']) || $this->_aObject['submenu'] == 'disabled')
  964. return;
  965. $oMenuSubmenu = BxDolMenu::getObjectInstance('sys_site_submenu');
  966. if(!$oMenuSubmenu)
  967. return;
  968. $oMenuSubmenu->setObjectSubmenu($this->_aObject['submenu'], $aParams);
  969. }
  970. /**
  971. * Get content for 'raw' block type.
  972. * @return string
  973. */
  974. protected function _getBlockRaw ($aBlock)
  975. {
  976. if (bx_is_api()){
  977. return [bx_api_get_block('raw', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  978. }
  979. $s = '<div class="bx-page-raw-container bx-def-vanilla-html max-w-none">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  980. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  981. $s = bx_process_macros($s);
  982. return $s;
  983. }
  984. /**
  985. * Get content for 'custom' block type.
  986. * @return string
  987. */
  988. protected function _getBlockCustom ($aBlock)
  989. {
  990. if (bx_is_api()){
  991. return [bx_api_get_block('custom', ['title' => _t($aBlock['title']), 'content' => BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array())])];
  992. }
  993. $s = '<div class="bx-page-custom-container">' . BxDolTemplate::getInstance()->parseHtmlByContent($aBlock['content'], array()) . '</div>';
  994. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  995. $s = bx_process_macros($s);
  996. return $s;
  997. }
  998. /**
  999. * Get content for 'html' block type.
  1000. * @return string
  1001. */
  1002. protected function _getBlockHtml ($aBlock)
  1003. {
  1004. if (bx_is_api()){
  1005. return [bx_api_get_block('html', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  1006. }
  1007. $s = '<div class="bx-page-html-container bx-def-vanilla-html max-w-none">' . $aBlock['content'] . '</div>';
  1008. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1009. $s = bx_process_macros($s);
  1010. return $s;
  1011. }
  1012. /**
  1013. * Get content for 'creative' block type.
  1014. * @return string
  1015. */
  1016. protected function _getBlockCreative ($aBlock)
  1017. {
  1018. if(bx_is_api())
  1019. return [bx_api_get_block('creative', ['title' => _t($aBlock['title']), 'content' => $aBlock['content']])];
  1020. $s = $this->_replaceMarkers($aBlock['content'], ['block_id' => $aBlock['id']]);
  1021. $s = bx_process_macros($s);
  1022. $bCanManage = $this->isAllowedCreativeManage();
  1023. if($bCanManage) {
  1024. $this->_oTemplate->addCss([
  1025. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'grapesjs/|grapes.min.css',
  1026. ]);
  1027. $this->_oTemplate->addJs([
  1028. 'grapesjs/grapes.min.js',
  1029. 'grapesjs/grapesjs-blocks-basic.js',
  1030. 'grapesjs/grapesjs-style-bg.js',
  1031. 'grapesjs/grapesjs-preset-webpage.min.js',
  1032. ]);
  1033. }
  1034. return $this->_oTemplate->parseHtmlByName('block_creative.html', [
  1035. 'block_id' => $aBlock['id'],
  1036. 'content' => $s,
  1037. 'bx_if:show_controls' => [
  1038. 'condition' => $bCanManage,
  1039. 'content' => [
  1040. 'onclick_edit' => $this->_sJsObjectName . '.creativeEdit(this, ' . $aBlock['id'] . ')',
  1041. 'onclick_save' => $this->_sJsObjectName . '.creativeSave(this, ' . $aBlock['id'] . ')'
  1042. ]
  1043. ]
  1044. ]);
  1045. }
  1046. /**
  1047. * Get content for 'bento_grid' block type.
  1048. * @return string
  1049. */
  1050. protected function _getBlockBentoGrid ($aBlock)
  1051. {
  1052. if(bx_is_api()) {
  1053. $iBlockId = (int)$aBlock['id'];
  1054. $iContentId = 0;
  1055. if(($mContentId = bx_get('id')) !== false)
  1056. $iContentId = (int)$mContentId;
  1057. $sContentModule = '';
  1058. if(!empty($this->_aObject['module']) && !in_array($this->_aObject['module'], ['system', 'custom']))
  1059. $sContentModule = $this->_aObject['module'];
  1060. $bIsAllowedEdit = false;
  1061. if(!empty($sContentModule) && bx_is_srv($sContentModule, 'check_allowed_with_content'))
  1062. $bIsAllowedEdit = bx_srv($sContentModule, 'check_allowed_with_content', ['edit', $iContentId]) === CHECK_ACTION_RESULT_ALLOWED;
  1063. else
  1064. $bIsAllowedEdit = isAdmin();
  1065. return [bx_api_get_block('bento_grid', [
  1066. 'title' => _t($aBlock['title']),
  1067. 'content' => self::getPageBlockData($iBlockId, $iContentId, $sContentModule)
  1068. ], ['ext' => [
  1069. 'block_id' => $iBlockId,
  1070. 'content_id' => $iContentId,
  1071. 'content_module' => $sContentModule,
  1072. 'is_allowed_edit' => $bIsAllowedEdit
  1073. ]])];
  1074. }
  1075. /**
  1076. * Isn't supported for now.
  1077. */
  1078. return '';
  1079. }
  1080. /**
  1081. * Get content for 'wiki' block type.
  1082. * @return string
  1083. */
  1084. protected function _getBlockWiki ($aBlock)
  1085. {
  1086. $oWiki = BxDolWiki::getObjectInstance($this->_aObject['module']);
  1087. if (!$oWiki) {
  1088. $sContent = ($sMsg = _t('_sys_wiki_error_missing_wiki_object', $this->_aObject['module'])) && $this->_bIsApi ? ['content' => $sMsg] : $sMsg;
  1089. }
  1090. else {
  1091. $sContent = $oWiki->getBlockContent($aBlock['id'], false, ($iRevision = bx_get($aBlock['id'] . 'rev')) !== false ? (int)$iRevision : false);
  1092. }
  1093. if($this->_bIsApi)
  1094. return [bx_api_get_block('html', array_merge([
  1095. 'title' => _t($aBlock['title']),
  1096. 'content' => ''
  1097. ], $sContent))];
  1098. $s = '<div id="bx-page-wiki-container-' . $aBlock['id'] . '" class="bx-page-wiki-container markdown-body bx-def-vanilla-html">' . $sContent . '</div>';
  1099. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1100. $s = bx_process_macros($s);
  1101. return $s;
  1102. }
  1103. /**
  1104. * Get content for 'lang' block type.
  1105. * @return string
  1106. */
  1107. protected function _getBlockLang ($aBlock)
  1108. {
  1109. if (bx_is_api()){
  1110. return [bx_api_get_block('lang', ['title' => _t($aBlock['title']), 'content' => bx_process_macros(_t(trim($aBlock['content'])))])];
  1111. }
  1112. $s = '<div class="bx-page-lang-container bx-def-vanilla-html max-w-none">' . _t(trim($aBlock['content'])) . '</div>';
  1113. $s = $this->_replaceMarkers($s, array('block_id' => $aBlock['id']));
  1114. $s = bx_process_macros($s);
  1115. return $s;
  1116. }
  1117. /**
  1118. * Get content for 'image' block type.
  1119. * @return string
  1120. */
  1121. protected function _getBlockImage ($aBlock)
  1122. {
  1123. if (empty($aBlock['content']))
  1124. return false;
  1125. list($iFileId, $sAlign ) = explode('#', $aBlock['content']);
  1126. $iFileId = (int)$iFileId;
  1127. if (!$iFileId)
  1128. return false;
  1129. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  1130. if (!$oStorage)
  1131. return false;
  1132. $sUrl = $oStorage->getFileUrlById($iFileId);
  1133. if (!$sUrl)
  1134. return false;
  1135. $sStyleAdd = '';
  1136. $sClassAdd = 'bx-page-image-container';
  1137. $aAlign = ['left' => 'start', 'center' => 'center', 'right' => 'end'];
  1138. if(array_key_exists($sAlign, $aAlign)) {
  1139. $sClassAdd .= ' flex justify-' . $aAlign[$sAlign];
  1140. $sStyleAdd = 'style="text-align:' . $sAlign . '"';
  1141. }
  1142. return '<div class="' . $sClassAdd . '" ' . $sStyleAdd . '><img src="' . $sUrl . '" /></div>';
  1143. }
  1144. /**
  1145. * Get content for 'rss' block type.
  1146. * @return string
  1147. */
  1148. protected function _getBlockRss ($aBlock)
  1149. {
  1150. if (empty($aBlock['content']))
  1151. return false;
  1152. list( $sUrl, $iNum ) = explode('#', $aBlock['content']);
  1153. $iNum = (int)$iNum;
  1154. return BxDolRss::getObjectInstance('sys_page_block')->getHolder($aBlock['id'], $iNum);
  1155. }
  1156. /**
  1157. * Get content for 'menu' block type.
  1158. * @return string
  1159. */
  1160. protected function _getBlockMenu ($aBlock)
  1161. {
  1162. $bIsApi = bx_is_api();
  1163. $oMenu = BxTemplMenu::getObjectInstance($aBlock['content']);
  1164. if(!$oMenu)
  1165. return $bIsApi ? [] : '';
  1166. if($bIsApi)
  1167. return [bx_api_get_block('menu' . ($this->_sObject == 'sys_sub_wiki_pages_list' ? '_wiki' : ''), ['title' => _t($aBlock['title']), 'content' => $oMenu->getCodeAPI()])];
  1168. return $oMenu ? $oMenu->getCode() : '';
  1169. }
  1170. /**
  1171. * Get content for 'service' block type.
  1172. * @return string
  1173. */
  1174. protected function _getBlockService ($aBlock)
  1175. {
  1176. self::setBlockProcessing($aBlock);
  1177. $sContent = $aBlock['content'];
  1178. // get_mockup_block blocks are stored with empty params, so BxDolService's memory cache would
  1179. // hand every such block on the page the same tree; key the call by block id instead.
  1180. // Read and rewritten through the service-call codec: new blocks are stored as JSON, and
  1181. // old serialized ones must never be unserialized with objects allowed.
  1182. $aCall = BxDolService::decodeServiceCall($sContent);
  1183. if (is_array($aCall) && ($aCall['method'] ?? '') === 'get_mockup_block')
  1184. $sContent = BxDolService::getSerializedService($aCall['module'], $aCall['method'], [(int)$aBlock['id']], $aCall['class'] ?? '');
  1185. $aResult = BxDolService::callSerialized($sContent, array_merge($this->_aMarkers, [
  1186. 'block_id' => $aBlock['id']
  1187. ]));
  1188. self::unsetBlockProcessing();
  1189. return $aResult;
  1190. }
  1191. /**
  1192. * Get page title.
  1193. * @return string
  1194. */
  1195. protected function _getPageTitle()
  1196. {
  1197. return $this->_replaceMarkers(_t($this->_aObject['title']));
  1198. }
  1199. /**
  1200. * Get page meta description.
  1201. * @return string
  1202. */
  1203. protected function _getPageMetaDesc()
  1204. {
  1205. return $this->_replaceMarkers(_t($this->_aObject['meta_description']));
  1206. }
  1207. /**
  1208. * Get page meta title.
  1209. * @return string
  1210. */
  1211. protected function _getPageMetaTitle()
  1212. {
  1213. return $this->_replaceMarkers(_t($this->_aObject['meta_title']));
  1214. }
  1215. /**
  1216. * Get page meta image.
  1217. * @return string
  1218. */
  1219. protected function _getPageMetaImage()
  1220. {
  1221. return '';
  1222. }
  1223. /**
  1224. * Get page meta keywords.
  1225. * @return string
  1226. */
  1227. protected function _getPageMetaKeywords()
  1228. {
  1229. return $this->_replaceMarkers(_t($this->_aObject['meta_keywords']));
  1230. }
  1231. /**
  1232. * Get page meta robots.
  1233. * @return string
  1234. */
  1235. protected function _getPageMetaRobots()
  1236. {
  1237. return _t($this->_aObject['meta_robots']);
  1238. }
  1239. /**
  1240. * Get page not found message.
  1241. * @return string
  1242. */
  1243. protected function _getPageNotFoundMsg ($mixedMsg = false)
  1244. {
  1245. $this->_oTemplate->displayPageNotFound();
  1246. exit;
  1247. }
  1248. /**
  1249. * Get access denied message.
  1250. * @return string
  1251. */
  1252. protected function _getPageAccessDeniedMsg ($mixedMsg = false)
  1253. {
  1254. return MsgBox($mixedMsg !== false ? $mixedMsg : _t('_Access denied'));
  1255. }
  1256. /**
  1257. * Get page cache object.
  1258. * @return cache object instance
  1259. */
  1260. protected function _getPageCacheObject ()
  1261. {
  1262. return $this->_getCacheObject ();
  1263. }
  1264. /**
  1265. * Get block cache object.
  1266. * @return cache object instance
  1267. */
  1268. protected function _getBlockCacheObject ()
  1269. {
  1270. return $this->_getCacheObject ('Block', 'pb');
  1271. }
  1272. protected function _getCacheObject ($sSuffixObj = 'Page', $sSuffixParam = 'page')
  1273. {
  1274. $sObj = '_o' . $sSuffixObj . 'CacheObject';
  1275. $sParam = 'sys_' . $sSuffixParam . '_cache_engine';
  1276. if ($this->{$sObj} != null) {
  1277. return $this->{$sObj};
  1278. } else {
  1279. $sEngine = getParam($sParam);
  1280. $this->{$sObj} = bx_instance ('BxDolCache' . $sEngine);
  1281. if (!$this->{$sObj}->isAvailable())
  1282. $this->{$sObj} = bx_instance ('BxDolCacheFile');
  1283. return $this->{$sObj};
  1284. }
  1285. }
  1286. /**
  1287. * Get page cache key.
  1288. * @param $isPrefixOnly return key prefix only.
  1289. * @return string
  1290. */
  1291. protected function _getPageCacheKey ($isPrefixOnly = false)
  1292. {
  1293. $s = 'page_' . $this->_aObject['object'] . '_';
  1294. if ($isPrefixOnly)
  1295. return $s;
  1296. $s .= $this->_getPageCacheParams ();
  1297. $s .= bx_site_hash() . '.php';
  1298. return $s;
  1299. }
  1300. /**
  1301. * Get block cache key.
  1302. * @param $isPrefixOnly return key prefix only.
  1303. * @return string
  1304. */
  1305. protected function _getBlockCacheKey ($isPrefixOnly = false, $aBlock = array())
  1306. {
  1307. $s = $this->_getPageCacheKey(true) . 'block_';
  1308. if ($isPrefixOnly)
  1309. return $s;
  1310. $s .= ($aBlock ? $aBlock['id'] : 0) . '_' . $this->_getPageCacheParams ();
  1311. $s .= bx_site_hash() . '.php';
  1312. return $s;
  1313. }
  1314. /**
  1315. * Additional cache key. In the case of dynamic page.
  1316. * For example - profile page, where each profile has own page.
  1317. * @return string
  1318. */
  1319. protected function _getPageCacheParams ()
  1320. {
  1321. return md5(serialize($_GET) . bx_lang_name());
  1322. }
  1323. /**
  1324. * Clean page cache.
  1325. * @param $isDelAllWithPagePrefix delete cache by prefix, it can be used for dynamic pages, like profile view, where for each profile separate cache is generated.
  1326. * @return string
  1327. */
  1328. protected function cleanCache ($isDelAllWithPagePrefix = false)
  1329. {
  1330. $a = ['Page', 'Block'];
  1331. $bRet = false;
  1332. foreach ($a as $s) {
  1333. $oCache = $this->{'_get' . $s . 'CacheObject'} ();
  1334. $sKey = $this->{'_get'. $s . 'CacheKey'} ($isDelAllWithPagePrefix);
  1335. if ($isDelAllWithPagePrefix)
  1336. $bRet = $bRet && $oCache->removeAllByPrefix($sKey);
  1337. else
  1338. $bRet = $bRet && $oCache->delData($sKey);
  1339. }
  1340. return $bRet;
  1341. }
  1342. }
  1343. /** @} */