BxDolTemplate.php 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. ini_set('pcre.backtrack_limit', 1000000);
  10. define('BX_DOL_TEMPLATE_DEFAULT_CODE', 'lagoon');
  11. define('BX_DOL_TEMPLATE_FOLDER_ROOT', 'template');
  12. define('BX_DOL_TEMPLATE_INJECTIONS_CACHE', 'sys_injections.inc');
  13. define('BX_DOL_TEMPLATE_CHECK_IN_BOTH', 'both');
  14. define('BX_DOL_TEMPLATE_CHECK_IN_BASE', 'base');
  15. define('BX_DOL_TEMPLATE_CHECK_IN_TMPL', 'tmpl');
  16. define('BX_PAGE_DEFAULT', 0); ///< default, regular page
  17. define('BX_PAGE_CLEAR', 2); ///< clear page, without any headers and footers
  18. define('BX_PAGE_POPUP', 44); ///< popup page, without any headers and footers
  19. define('BX_PAGE_TRANSITION', 150); ///< transition page with redirect to display some msg, like 'please wait', without headers footers
  20. /**
  21. * Template engine.
  22. *
  23. * An object of the class allows to:
  24. * 1. Manage HTML templates.
  25. * 2. Get URL/path for any template image/icon.
  26. * 3. Attach CSS/JavaScript files to the output.
  27. * 4. Add some content to any template key using Injection engine.
  28. *
  29. *
  30. * Avalable constructions.
  31. * 1. <bx_include_auto:template_name.html /> - the content of the file would be inserted. File would be taken from current template if it existes there, and from base directory otherwise.
  32. * 2. <bx_include_base:template_name.html /> - the content of the file would be inserted. File would be taken from base directory.
  33. * 3. <bx_include_tmpl:template_name.html /> - the content of the file would be inserted. File would be taken from tmpl_xxx directory.
  34. * 4. <bx_url_root /> - the value of BX_DOL_URL_ROOT variable will be inserted.
  35. * 5. <bx_url_admin /> - the value of BX_DOL_URL_ADMIN variable will be inserted.
  36. * 6. <bx_text:_language_key /> - _language_key will be translated using language file(function _t()) and inserted.
  37. <bx_text_js:_language_key /> - _language_key will be translated using language file(function _t()) and inserted, use it to insert text into js string.
  38. <bx_text_attribute:_language_key /> - _language_key will be translated using language file(function _t()) and inserted, use it to insert text into html attribute.
  39. * 7. <bx_image_url:image_file_name /> - image with 'image_file_name' file name will be searched in the images folder of current template.
  40. * If it's not found, then it will be searched in the images folder of base template. On success full URL will be inserted, otherwise an empty string.
  41. * 8. <bx_icon_url:icon_file_name /> - the same with <bx_image_url:image_file_name />, but icons will be searched in the images/icons/ folders.
  42. * 9. <bx_injection:injection_name /> - will be replaced with injections registered with the page and injection_name in the `sys_injections`/`sys_injections_admin`/ tables.
  43. * 10. <bx_if:tag_name>some_HTML</bx_if:tag_name> - will be replaced with provided content if the condition is true, and with empty string otherwise.
  44. * 11. <bx_repeat:cycle_name>some_HTML</bx_repeat:cycle_name> - an inner HTML content will be repeated in accordance with received data.
  45. *
  46. *
  47. * Related classes:
  48. * BxDolTemplateAdmin - for processing admin templates.
  49. * Template classes in modules - for processing modiles' templates.
  50. *
  51. *
  52. * Global variables:
  53. * oSysTemplate - is used for template processing in user part.
  54. * oAdmTemplate - is used for template processing in admin part.
  55. *
  56. *
  57. * Add injection:
  58. * 1. Register it in the `sys_injections` table or `sys_injections_admin` table for admin panel.
  59. * 2. Clear injections cache(sys_injections.inc and sys_injections_admin.inc in cache folder).
  60. *
  61. *
  62. * Predefined template keys to add injections:
  63. * 1. injection_head - add injections in the <head> tag.
  64. * 2. injection_body - add ingection(attribute) in the <body> tag.
  65. * 3. injection_header - add injection inside the <body> tag at the very beginning.
  66. * 4. injection_logo_before - add injection at the left of the main logo(inside logo's DIV).
  67. * 5. injection_logo_after - add injection at the right of the main logo(inside logo's DIV).
  68. * 6. injection_between_logo_top_menu - add injection between logo and top menu.
  69. * 7. injection_top_menu_before - add injection at the left of the top menu(inside top menu's DIV).
  70. * 8. injection_top_menu_after - add injection at the right of the top menu(inside top menu's DIV).
  71. * 13. injection_content_before - add injection just before main content(inside content's DIV).
  72. * 14. injection_content_after - add injection just after main content(inside content's DIV).
  73. * 15. injection_between_content_footer - add injection between content and footer.
  74. * 16. injection_footer_before - add injection at the left of the footer(inside footer's DIV).
  75. * 17. injection_footer_after - add injection at the right of the footer(inside footer's DIV).
  76. * 18. injection_footer - add injection inside the <body> tag at the very end.
  77. *
  78. *
  79. * Example of usage:
  80. * @code
  81. * $oSysTemplate = BxDolTemplate::getInstance();
  82. *
  83. * $oSysTemplate->addCss(array('test1.css', 'test2.css'));
  84. * $oSysTemplate->addJs(array('test1.js', 'test2.js'));
  85. * $oSysTemplate->parseHtmlByName('messageBox.html', array(
  86. * 'id' => $iId,
  87. * 'msgText' => $sText,
  88. * 'bx_if:timer' => array(
  89. * 'condition' => $iTimer > 0,
  90. * 'content' => array(
  91. * 'id' => $iId,
  92. * 'time' => 1000 * $iTimer,
  93. * 'on_close' => $sOnClose,
  94. * )
  95. * ),
  96. * 'bx_if:timer' => array(
  97. * array(
  98. * 'name' => $sName,
  99. * 'title' => $sTitle
  100. * ),
  101. * array(
  102. * 'name' => $sName,
  103. * 'title' => $sTitle
  104. * )
  105. * )
  106. * ));
  107. * @endcode
  108. *
  109. *
  110. * Memberships/ACL:
  111. * Doesn't depend on user's membership.
  112. *
  113. *
  114. * Alerts:
  115. * no alerts available
  116. *
  117. */
  118. class BxDolTemplate extends BxDol implements iBxDolSingleton
  119. {
  120. /**
  121. * Main fields
  122. */
  123. protected $_sPrefix;
  124. protected $_sRootPath;
  125. protected $_sRootUrl;
  126. protected $_sSubPath;
  127. protected $_sInjectionsTable;
  128. protected $_sInjectionsCache;
  129. protected $_sCode;
  130. protected $_sCodeKey;
  131. protected $_sKeyWrapperHtml;
  132. protected $_sFolderHtml;
  133. protected $_sFolderCss;
  134. protected $_sFolderImages;
  135. protected $_sFolderIcons;
  136. protected $_aTemplates;
  137. protected $_aLocations;
  138. protected $_aLocationsJs;
  139. /**
  140. * Cache related fields
  141. */
  142. protected $_bCacheEnable;
  143. protected $_sCacheFolderUrl;
  144. protected $_sCachePublicFolderUrl;
  145. protected $_sCachePublicFolderPath;
  146. protected $_sCacheFilePrefix;
  147. protected $_bImagesInline;
  148. protected $_iImagesMaxSize;
  149. protected $_bCssCache;
  150. protected $_bCssMinify;
  151. protected $_bCssArchive;
  152. protected $_sCssCachePrefix;
  153. protected $_bJsCache;
  154. protected $_bJsMinify;
  155. protected $_bJsArchive;
  156. protected $_sJsCachePrefix;
  157. /**
  158. * Less related fields
  159. */
  160. protected $_bLessEnable;
  161. protected $_sLessCachePrefix;
  162. protected $aPage;
  163. protected $aPageContent;
  164. protected $_oConfigTemplate;
  165. /**
  166. * Constructor
  167. */
  168. protected function __construct($sRootPath = BX_DIRECTORY_PATH_ROOT, $sRootUrl = BX_DOL_URL_ROOT)
  169. {
  170. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  171. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  172. parent::__construct();
  173. $this->_sPrefix = 'BxDolTemplate';
  174. $this->_sRootPath = $sRootPath;
  175. $this->_sRootUrl = $sRootUrl;
  176. $this->_sInjectionsTable = 'sys_injections';
  177. $this->_sInjectionsCache = BX_DOL_TEMPLATE_INJECTIONS_CACHE;
  178. $this->_sCodeKey = 'skin';
  179. $sCode = getParam('template');
  180. if(empty($sCode))
  181. $sCode = BX_DOL_TEMPLATE_DEFAULT_CODE;
  182. $this->_checkCode($sCode, false);
  183. //--- Check selected template in COOKIE(the lowest priority) ---//
  184. $sCode = !empty($_COOKIE[$this->_sCodeKey]) ? $_COOKIE[$this->_sCodeKey] : '';
  185. $this->_checkCode($sCode, false);
  186. //--- Check selected template in GET(the highest priority) ---//
  187. $sCode = !empty($_GET[$this->_sCodeKey]) ? $_GET[$this->_sCodeKey] : '';
  188. $this->_checkCode($sCode, true);
  189. if (!$this->_sSubPath)
  190. $this->_sSubPath = 'boonex/' . BX_DOL_TEMPLATE_DEFAULT_CODE . '/';
  191. if (!file_exists(BX_DIRECTORY_PATH_MODULES . $this->_sSubPath)) // just for 8.0.0-A6 upgrade
  192. $this->_sSubPath = 'boonex/uni/';
  193. if(isset($_GET[$this->_sCodeKey])) {
  194. if(BxDolPermalinks::getInstance()->redirectIfNecessary(array($this->_sCodeKey)))
  195. exit;
  196. }
  197. $this->_sKeyWrapperHtml = '__';
  198. $this->_sFolderHtml = '';
  199. $this->_sFolderCss = 'css/';
  200. $this->_sFolderImages = 'images/';
  201. $this->_sFolderIcons = 'images/icons/';
  202. $this->_aTemplates = array();
  203. $this->addLocation('system', $this->_sRootPath, $this->_sRootUrl);
  204. $this->addLocationJs('system_inc_js', BX_DIRECTORY_PATH_INC . 'js/' , BX_DOL_URL_ROOT . 'inc/js/');
  205. $this->addLocationJs('system_inc_js_classes', BX_DIRECTORY_PATH_INC . 'js/classes/' , BX_DOL_URL_ROOT . 'inc/js/classes/');
  206. $this->addLocationJs('system_plugins_public', BX_DIRECTORY_PATH_PLUGINS_PUBLIC, BX_DOL_URL_PLUGINS_PUBLIC);
  207. $this->_bCacheEnable = !defined('BX_DOL_CRON_EXECUTE') && getParam('sys_template_cache_enable') == 'on';
  208. $this->_sCacheFolderUrl = '';
  209. $this->_sCachePublicFolderUrl = BX_DOL_URL_CACHE_PUBLIC;
  210. $this->_sCachePublicFolderPath = BX_DIRECTORY_PATH_CACHE_PUBLIC;
  211. $this->_sCacheFilePrefix = "bx_templ_";
  212. $this->_bImagesInline = getParam('sys_template_cache_image_enable') == 'on';
  213. $this->_iImagesMaxSize = (int)getParam('sys_template_cache_image_max_size') * 1024;
  214. $bArchive = getParam('sys_template_cache_compress_enable') == 'on';
  215. $this->_bCssCache = !defined('BX_DOL_CRON_EXECUTE') && getParam('sys_template_cache_css_enable') == 'on';
  216. $this->_bCssMinify = $this->_bCssCache && getParam('sys_template_cache_minify_css_enable') == 'on';
  217. $this->_bCssArchive = $this->_bCssCache && $bArchive;
  218. $this->_sCssCachePrefix = $this->_sCacheFilePrefix . 'css_';
  219. $this->_bJsCache = !defined('BX_DOL_CRON_EXECUTE') && getParam('sys_template_cache_js_enable') == 'on';
  220. $this->_bJsMinify = $this->_bJsCache && getParam('sys_template_cache_minify_js_enable') == 'on';
  221. $this->_bJsArchive = $this->_bJsCache && $bArchive;
  222. $this->_sJsCachePrefix = $this->_sCacheFilePrefix . 'js_';
  223. $this->_bLessEnable = true;
  224. $this->_sLessCachePrefix = $this->_sCacheFilePrefix . 'less_';
  225. $this->aPage = array();
  226. $this->aPageContent = array();
  227. }
  228. /**
  229. * Prevent cloning the instance
  230. */
  231. public function __clone()
  232. {
  233. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  234. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  235. }
  236. /**
  237. * Get singleton instance of the class
  238. */
  239. public static function getInstance()
  240. {
  241. if(!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  242. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolTemplate();
  243. $GLOBALS['bxDolClasses'][__CLASS__]->init();
  244. }
  245. return $GLOBALS['bxDolClasses'][__CLASS__];
  246. }
  247. /**
  248. * Load templates.
  249. */
  250. function loadTemplates()
  251. {
  252. $aResult = array();
  253. foreach($this->_aTemplates as $sName)
  254. $aResult[$sName] = $this->getHtml($sName . '.html');
  255. $this->_aTemplates = $aResult;
  256. }
  257. /**
  258. * Initialize template engine.
  259. * Note. The method is executed with the system, you shouldn't execute it in your subclasses.
  260. */
  261. function init()
  262. {
  263. //--- Load page elements related static variables ---//
  264. $this->aPage = array(
  265. 'name_index' => BX_PAGE_DEFAULT,
  266. 'header' => '',
  267. 'header_text' => '',
  268. 'keywords' => array(),
  269. 'location' => array(),
  270. 'description' => '',
  271. 'robots' => '',
  272. 'css_name' => array(),
  273. 'css_compiled' => array(),
  274. 'css_system' => array(),
  275. 'js_name' => array(),
  276. 'js_compiled' => array(),
  277. 'js_system' => array(),
  278. 'js_options' => array(),
  279. 'js_translations' => array(),
  280. 'js_images' => array(),
  281. 'injections' => array()
  282. );
  283. //--- Load default CSS ---//
  284. $this->addCssSystem(array(
  285. 'common.css',
  286. 'default.less',
  287. 'general.css',
  288. 'icons.css',
  289. 'colors.css',
  290. 'forms.css',
  291. 'media-desktop.css',
  292. 'media-tablet.css',
  293. 'media-phone.css',
  294. 'media-print.css',
  295. BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'marka/|marka.min.css',
  296. ));
  297. //--- Load default JS ---//
  298. $this->addJsSystem(array(
  299. 'jquery/jquery.min.js',
  300. 'jquery/jquery-migrate.min.js',
  301. 'jquery-ui/jquery.ui.position.min.js',
  302. 'spin.min.js',
  303. 'jquery.easing.js',
  304. 'jquery.cookie.min.js',
  305. 'moment-with-locales.min.js',
  306. 'functions.js',
  307. 'jquery.webForms.js',
  308. 'jquery.dolPopup.js',
  309. 'marka/marka.min.js',
  310. 'headroom.min.js',
  311. ));
  312. //--- Load default JS ---//
  313. bx_import('BxDolLanguages');
  314. $this->addJsTranslation(array(
  315. '_are you sure?',
  316. '_error occured',
  317. '_sys_loading',
  318. ));
  319. //--- Load injection's cache ---//
  320. if (getParam('sys_db_cache_enable')) {
  321. $oDb = BxDolDb::getInstance();
  322. $oCache = $oDb->getDbCacheObject();
  323. $sCacheKey = $oDb->genDbCacheKey($this->_sInjectionsCache);
  324. $aInjections = $oCache->getData($sCacheKey);
  325. if ($aInjections === null) {
  326. $aInjections = $this->getInjectionsData();
  327. $oCache->setData ($sCacheKey, $aInjections);
  328. }
  329. } else {
  330. $aInjections = $this->getInjectionsData();
  331. }
  332. $this->aPage['injections'] = $aInjections;
  333. bx_import('BxTemplConfig'); // TODO: for some reason autoloader isn't working here....
  334. $this->_oConfigTemplate = BxTemplConfig::getInstance();
  335. }
  336. protected function getInjectionsData ()
  337. {
  338. $oDb = BxDolDb::getInstance();
  339. $aInjections = $oDb->getAll("SELECT `page_index`, `name`, `key`, `type`, `data`, `replace` FROM `" . $this->_sInjectionsTable . "` WHERE `active`='1'");
  340. if (!$aInjections)
  341. return array();
  342. foreach ($aInjections as $aInjection)
  343. $aInjections['page_' . $aInjection['page_index']][$aInjection['key']][] = $aInjection;
  344. return $aInjections;
  345. }
  346. /**
  347. * Set page name index
  348. * @param int $i name index
  349. */
  350. function setPageNameIndex($i)
  351. {
  352. $this->aPage['name_index'] = $i;
  353. }
  354. /**
  355. * Get page name index
  356. * @return int $i name index
  357. */
  358. function getPageNameIndex()
  359. {
  360. return isset($this->aPage['name_index']) ? (int)$this->aPage['name_index'] : 0;
  361. }
  362. /**
  363. * Set page header
  364. * @param string $s page header
  365. */
  366. function setPageHeader($s)
  367. {
  368. $this->aPage['header'] = $s;
  369. }
  370. /**
  371. * Get page header
  372. * @return string $s page header
  373. */
  374. function getPageHeader()
  375. {
  376. return $this->aPage['header'];
  377. }
  378. /**
  379. * Set page params. Available page params are: name_index, header
  380. * @param array $a page params
  381. */
  382. function setPageParams($a)
  383. {
  384. if (!empty($this->aPage))
  385. $this->aPage = array_merge($this->aPage, $a);
  386. else
  387. $this->aPage = $a;
  388. }
  389. /**
  390. * Get page params.
  391. * @return array $a page params
  392. */
  393. function getPageParams()
  394. {
  395. return $this->aPage;
  396. }
  397. /**
  398. * Set page description.
  399. *
  400. * @param string $sDescription necessary page description.
  401. */
  402. function setPageDescription($sDescription)
  403. {
  404. $this->aPage['description'] = $sDescription;
  405. }
  406. /**
  407. * Set page meta robots.
  408. *
  409. * @param string $s page meta robots.
  410. */
  411. function setPageMetaRobots($s)
  412. {
  413. $this->aPage['robots'] = $s;
  414. }
  415. /**
  416. * Set page content for some variable.
  417. * @param string $sVar name of content variable
  418. * @param string $sContent content for $sVar variable
  419. * @param int $iIndex optional page index, default is index which was set before with @see setPageNameIndex function, or 0
  420. */
  421. function setPageContent($sVar, $sContent, $iIndex = false)
  422. {
  423. $i = false !== $iIndex ? $iIndex : $this->getPageNameIndex();
  424. $this->aPageContent[$i][$sVar] = $sContent;
  425. }
  426. /**
  427. * Get page content for some variable.
  428. * @param string $sVar name of content variable
  429. * @param int $iIndex optional page index, default is index which was set before with @see setPageNameIndex function, or 0
  430. * @return string page content for some variable or for the whole page.
  431. */
  432. function getPageContent($sVar = false, $iIndex = false)
  433. {
  434. $i = false !== $iIndex ? $iIndex : $this->getPageNameIndex();
  435. return false !== $sVar ? $this->aPageContent[$i][$sVar] : $this->aPageContent[$i];
  436. }
  437. /**
  438. * Get currently active template code.
  439. *
  440. * @return string template's code.
  441. */
  442. function getCode()
  443. {
  444. return $this->_sCode;
  445. }
  446. /**
  447. * Check whether code is associated with active template.
  448. *
  449. * @param string $sCode template's unique URI.
  450. * @param boolean $bSetCookie save code in COOKIE or not.
  451. */
  452. protected function _checkCode($sCode, $bSetCookie)
  453. {
  454. if(empty($sCode) || !preg_match('/^[A-Za-z0-9_-]+$/', $sCode))
  455. return;
  456. $aModule = BxDolModuleQuery::getInstance()->getModuleByUri($sCode);
  457. if(empty($aModule) || !is_array($aModule) || (int)$aModule['enabled'] != 1 || !file_exists($this->_sRootPath . 'modules/' . $aModule['path'] . 'data/template/'))
  458. return;
  459. $oConfig = new BxDolModuleConfig($aModule);
  460. $this->_sCode = $oConfig->getUri();
  461. $this->_sSubPath = $oConfig->getDirectory();
  462. if(!$bSetCookie || bx_get('preview'))
  463. return;
  464. $aUrl = parse_url(BX_DOL_URL_ROOT);
  465. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  466. setcookie( $this->_sCodeKey, $this->_sCode, time() + 60*60*24*365, $sPath);
  467. }
  468. /**
  469. * Get currently active template path.
  470. *
  471. * @return string template's path.
  472. */
  473. function getPath()
  474. {
  475. return $this->_sSubPath;
  476. }
  477. /**
  478. * Set page title.
  479. * @deprecated use setPageHeader
  480. *
  481. * @param string $sTitle necessary page title.
  482. */
  483. function setPageTitle($sTitle)
  484. {
  485. $this->setPageHeader($sTitle);
  486. }
  487. /**
  488. * Set page's main box title.
  489. * @deprecated use setPageParams
  490. *
  491. * @param string $sTitle necessary page's main box title.
  492. */
  493. function setPageMainBoxTitle($sTitle)
  494. {
  495. $this->setPageParams(array('header_text' => $sTitle));
  496. }
  497. /**
  498. * Add location in array of locations.
  499. * Note. Location is the path/url to folder where 'templates' folder is stored.
  500. *
  501. * @param string $sKey - location's unique key.
  502. * @param string $sLocationPath - location's path. For modules: '[path_to_script]/modules/[vendor_name]/[module_name]/'
  503. * @param string $sLocationUrl - location's url. For modules: '[url_to_script]/modules/[vendor_name]/[module_name]/'
  504. */
  505. function addLocation($sKey, $sLocationPath, $sLocationUrl)
  506. {
  507. $this->_aLocations[$sKey] = array(
  508. 'path' => $sLocationPath,
  509. 'url' => $sLocationUrl,
  510. );
  511. return $sKey;
  512. }
  513. /**
  514. * Add dynamic location.
  515. *
  516. * @param string $sLocationPath - location's path. For modules: '[path_to_script]/modules/[vendor_name]/[module_name]/'
  517. * @param string $sLocationUrl - location's url. For modules: '[url_to_script]/modules/[vendor_name]/[module_name]/'
  518. * @return location key. Is needed to remove the location.
  519. */
  520. function addDynamicLocation($sLocationPath, $sLocationUrl)
  521. {
  522. $sLocationKey = time();
  523. $this->addLocation($sLocationKey, $sLocationPath, $sLocationUrl);
  524. return $sLocationKey;
  525. }
  526. /**
  527. * Remove location from array of locations.
  528. * Note. Location is the path/url to folder where templates are stored.
  529. *
  530. * @param string $sKey - location's unique key.
  531. */
  532. function removeLocation($sKey)
  533. {
  534. if(isset($this->_aLocations[$sKey]))
  535. unset($this->_aLocations[$sKey]);
  536. }
  537. /**
  538. * Add JS location in array of JS locations.
  539. * Note. Location is the path/url to folder where JS files are stored.
  540. *
  541. * @param string $sKey - location's unique key.
  542. * @param string $sLocationPath - location's path. For modules: '[path_to_script]/modules/[vendor_name]/[module_name]/js/'
  543. * @param string $sLocationUrl - location's url. For modules: '[url_to_script]/modules/[vendor_name]/[module_name]/js/'
  544. */
  545. function addLocationJs($sKey, $sLocationPath, $sLocationUrl)
  546. {
  547. $this->_aLocationsJs[$sKey] = array(
  548. 'path' => $sLocationPath,
  549. 'url' => $sLocationUrl
  550. );
  551. }
  552. /**
  553. * Add dynamic JS location.
  554. *
  555. * @param string $sLocationPath - location's path. For modules: '[path_to_script]/modules/[vendor_name]/[module_name]/'
  556. * @param string $sLocationUrl - location's url. For modules: '[url_to_script]/modules/[vendor_name]/[module_name]/'
  557. * @return location key. Is needed to remove the location.
  558. */
  559. function addDynamicLocationJs($sLocationPath, $sLocationUrl)
  560. {
  561. $sLocationKey = time();
  562. $this->addLocationJs($sLocationKey, $sLocationPath, $sLocationUrl);
  563. return $sLocationKey;
  564. }
  565. /**
  566. * Remove JS location from array of locations.
  567. * Note. Location is the path/url to folder where templates are stored.
  568. *
  569. * @param string $sKey - JS location's unique key.
  570. */
  571. function removeLocationJs($sKey)
  572. {
  573. if(isset($this->_aLocationsJs[$sKey]))
  574. unset($this->_aLocationsJs[$sKey]);
  575. }
  576. /**
  577. * Add Option in JS output.
  578. *
  579. * @param mixed $mixedName option's name or an array of options' names.
  580. */
  581. function addJsOption($mixedName)
  582. {
  583. if(is_string($mixedName))
  584. $mixedName = array($mixedName);
  585. foreach($mixedName as $sName)
  586. $this->aPage['js_options'][$sName] = getParam($sName);
  587. }
  588. /**
  589. * Add language translation for key in JS output.
  590. *
  591. * @param mixed $mixedKey language key or an array of keys.
  592. */
  593. function addJsTranslation($mixedKey)
  594. {
  595. if(is_string($mixedKey))
  596. $mixedKey = array($mixedKey);
  597. foreach($mixedKey as $sKey)
  598. $this->aPage['js_translations'][$sKey] = _t($sKey, '{0}', '{1}');
  599. }
  600. /**
  601. * Add image in JS output.
  602. *
  603. * @param array $aImages an array of image descriptors.
  604. * The descriptor is a key/value pear in the array of descriptors.
  605. */
  606. function addJsImage($aImages)
  607. {
  608. if(!is_array($aImages))
  609. return;
  610. foreach($aImages as $sKey => $sFile) {
  611. $sUrl = $this->getImageUrl($sFile);
  612. if(empty($sUrl))
  613. continue;
  614. $this->aPage['js_images'][$sKey] = $sUrl;
  615. }
  616. }
  617. /**
  618. * Add icon in JS output.
  619. *
  620. * @param array $aIcons an array of icons descriptors.
  621. * The descriptor is a key/value pear in the array of descriptors.
  622. */
  623. function addJsIcon($aIcons)
  624. {
  625. if(!is_array($aIcons))
  626. return;
  627. foreach($aIcons as $sKey => $sFile) {
  628. $sUrl = $this->getIconUrl($sFile);
  629. if(empty($sUrl))
  630. continue;
  631. $this->aPage['js_images'][$sKey] = $sUrl;
  632. }
  633. }
  634. /**
  635. * Set page keywords.
  636. *
  637. * @param mixed $mixedKeywords necessary page keywords(string - single keyword, array - an array of keywords).
  638. * @param string $sDevider - string devider.
  639. */
  640. function addPageKeywords($mixedKeywords, $sDevider = ',')
  641. {
  642. if(is_string($mixedKeywords))
  643. $mixedKeywords = strpos($mixedKeywords, $sDevider) !== false ? explode($sDevider, $mixedKeywords) : array($mixedKeywords);
  644. foreach($mixedKeywords as $iKey => $sValue)
  645. $mixedKeywords[$iKey] = trim($sValue);
  646. $this->aPage['keywords'] = isset($this->aPage['keywords']) && is_array($this->aPage['keywords']) ? array_merge($this->aPage['keywords'], $mixedKeywords) : $mixedKeywords;
  647. }
  648. /**
  649. * Set page locatoin coordinates.
  650. *
  651. * @param $fLat latitude
  652. * @param $fLng longitude
  653. */
  654. function addPageMetaLocation($fLat, $fLng, $sCountryCode)
  655. {
  656. $this->aPage['location'] = array('lat' => $fLat, 'lng' => $fLng, 'country' => $sCountryCode);
  657. }
  658. /**
  659. * Set page meta image.
  660. *
  661. * @param $sImageUrl meta image url
  662. */
  663. function addPageMetaImage($sImageUrl)
  664. {
  665. $this->aPage['image'] = $sImageUrl;
  666. }
  667. /**
  668. * Set page rss link.
  669. *
  670. * @param $sTitle - rss feed title
  671. * @param $sUrl - rss feed URL
  672. */
  673. function addPageRssLink($sTitle, $sUrl)
  674. {
  675. if (!isset($this->aPage['rss']))
  676. $this->aPage['rss'] = array('title' => $sTitle, 'url' => $sUrl);
  677. else
  678. $this->aPage['rss'] = false;
  679. }
  680. /**
  681. * Returns page meta info, like meta keyword, meta description, location, etc
  682. */
  683. function getMetaInfo()
  684. {
  685. $sRet = '';
  686. if (!empty($this->aPage['keywords']) && is_array($this->aPage['keywords']))
  687. $sRet .= '<meta name="keywords" content="' . bx_html_attribute(implode(',', $this->aPage['keywords'])) . '" />';
  688. if (!empty($this->aPage['description']) && is_string($this->aPage['description']))
  689. $sRet .= '<meta name="description" content="' . bx_html_attribute($this->aPage['description']) . '" />';
  690. if (!empty($this->aPage['location']) && isset($this->aPage['location']['lat']) && isset($this->aPage['location']['lng']) && isset($this->aPage['location']['country']))
  691. $sRet .= '
  692. <meta name="ICBM" content="' . $this->aPage['location']['lat'] . ';' . $this->aPage['location']['lng'] . '" />
  693. <meta name="geo.position" content="' . $this->aPage['location']['lat'] . ';' . $this->aPage['location']['lng'] . '" />
  694. <meta name="geo.region" content="' . bx_html_attribute($this->aPage['location']['country']) . '" />';
  695. if (!empty($this->aPage['image']))
  696. $sRet .= '<meta property="og:image" content="' . $this->aPage['image'] . '" />';
  697. else
  698. $sRet .= BxTemplFunctions::getInstance()->getMetaIcons();
  699. if (!empty($this->aPage['rss']) && !empty($this->aPage['rss']['url']))
  700. $sRet .= '<link rel="alternate" type="application/rss+xml" title="' . bx_html_attribute($this->aPage['rss']['title'], BX_ESCAPE_STR_QUOTE) . '" href="' . $this->aPage['rss']['url'] . '" />';
  701. return $sRet;
  702. }
  703. /**
  704. * Get template, which was loaded earlier.
  705. * @see method this->loadTemplates and field this->_aTemplates
  706. *
  707. * @param string $sName - template name.
  708. * @return string template's content.
  709. */
  710. function getTemplate($sName)
  711. {
  712. return $this->_aTemplates[$sName];
  713. }
  714. /**
  715. * Get full URL for the icon.
  716. *
  717. * @param string $sName icon's file name.
  718. * @param string $sCheckIn where the content would be searched(base, template, both)
  719. * @return string full URL.
  720. */
  721. function getIconUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  722. {
  723. $sContent = "";
  724. if(($sContent = $this->_getInlineData('icon', $sName, $sCheckIn)) !== false)
  725. return $sContent;
  726. return $this->_getAbsoluteLocation('url', $this->_sFolderIcons, $sName, $sCheckIn);
  727. }
  728. /**
  729. * Get absolute Path for the icon.
  730. *
  731. * @param string $sName - icon's file name.
  732. * @param string $sCheckIn where the content would be searched(base, template, both)
  733. * @return string absolute path.
  734. */
  735. function getIconPath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  736. {
  737. return $this->_getAbsoluteLocation('path', $this->_sFolderIcons, $sName, $sCheckIn);
  738. }
  739. /**
  740. * Get full URL for the image.
  741. *
  742. * @param string $sName - images's file name.
  743. * @param string $sCheckIn where the content would be searched(base, template, both)
  744. * @return string full URL.
  745. */
  746. function getImageUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  747. {
  748. $sContent = "";
  749. if(($sContent = $this->_getInlineData('image', $sName, $sCheckIn)) !== false)
  750. return $sContent;
  751. return $this->_getAbsoluteLocation('url', $this->_sFolderImages, $sName, $sCheckIn);
  752. }
  753. /**
  754. * Get absolute Path for the image.
  755. *
  756. * @param string $sName - image's file name.
  757. * @param string $sCheckIn where the content would be searched(base, template, both)
  758. * @return string absolute path.
  759. */
  760. function getImagePath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  761. {
  762. return $this->_getAbsoluteLocation('path', $this->_sFolderImages, $sName, $sCheckIn);
  763. }
  764. /**
  765. * Get full URL of CSS file.
  766. *
  767. * @param string $sName - CSS file name.
  768. * @param string $sCheckIn where the content would be searched(base, template, both)
  769. * @return string full URL.
  770. */
  771. function getCssUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  772. {
  773. return $this->_getAbsoluteLocation('url', $this->_sFolderCss, $sName, $sCheckIn);
  774. }
  775. /**
  776. * Get full Path of CSS file.
  777. *
  778. * @param string $sName - CSS file name.
  779. * @param string $sCheckIn where the content would be searched(base, template, both)
  780. * @return string full URL.
  781. */
  782. function getCssPath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  783. {
  784. return $this->_getAbsoluteLocation('path', $this->_sFolderCss, $sName, $sCheckIn);
  785. }
  786. /**
  787. * Get menu.
  788. * @param $s menu object name
  789. * @return html or empty string
  790. */
  791. function getMenu ($s)
  792. {
  793. $oMenu = BxDolMenu::getObjectInstance($s);
  794. return $oMenu ? $oMenu->getCode () : '';
  795. }
  796. /**
  797. * Get content of HTML file.
  798. *
  799. * @param string $sName - HTML file name.
  800. * @param string $sCheckIn where the content would be searched(base, template, both)
  801. * @return string full content of the file and false on failure.
  802. */
  803. function getHtml($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  804. {
  805. $sAbsolutePath = $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn);
  806. return !empty($sAbsolutePath) ? trim(file_get_contents($sAbsolutePath)) : false;
  807. }
  808. /**
  809. * Parse HTML template. Search for the template with accordance to it's file name.
  810. *
  811. * @see allows to use cache.
  812. *
  813. * @param string $sName - HTML file name.
  814. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  815. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(left, right) otherwise).
  816. * @param string $sCheckIn where the content would be searched(base, template, both)
  817. * @return string the result of operation.
  818. */
  819. function parseHtmlByName($sName, $aVariables, $mixedKeyWrapperHtml = null, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  820. {
  821. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginTemplate($sName, $sRand = time().rand());
  822. if (($sContent = $this->getCached($sName, $aVariables, $mixedKeyWrapperHtml, $sCheckIn)) !== false) {
  823. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endTemplate($sName, $sRand, $sRet, true);
  824. return $sContent;
  825. }
  826. $sRet = '';
  827. if (($sContent = $this->getHtml($sName, $sCheckIn)) !== false)
  828. $sRet = $this->_parseContent($sContent, $aVariables, $mixedKeyWrapperHtml);
  829. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endTemplate($sName, $sRand, $sRet, false);
  830. return $sRet;
  831. }
  832. /**
  833. * Parse HTML template.
  834. *
  835. * @see Doesn't allow to use cache.
  836. *
  837. * @param string $sContent - HTML file content.
  838. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  839. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(left, right) otherwise).
  840. * @return string the result of operation.
  841. */
  842. function parseHtmlByContent($sContent, $aVariables, $mixedKeyWrapperHtml = null)
  843. {
  844. if(empty($sContent))
  845. return "";
  846. return $this->_parseContent($sContent, $aVariables, $mixedKeyWrapperHtml);
  847. }
  848. /**
  849. * Parse earlier loaded HTML template.
  850. *
  851. * @see Doesn't allow to use cache.
  852. *
  853. * @param string $sName - template name.
  854. * @param array $aVariables - key/value pairs. Key should be the same as template's key, excluding prefix and postfix.
  855. * @return string the result of operation.
  856. * @see $this->_aTemplates
  857. */
  858. function parseHtmlByTemplateName($sName, $aVariables, $mixedKeyWrapperHtml = null)
  859. {
  860. if(!isset($this->_aTemplates[$sName]) || empty($this->_aTemplates[$sName]))
  861. return "";
  862. return $this->_parseContent($this->_aTemplates[$sName], $aVariables, $mixedKeyWrapperHtml);
  863. }
  864. /**
  865. * Parse page HTML template. Search for the page's template with accordance to it's file name.
  866. *
  867. * @see allows to use cache.
  868. *
  869. * @param string $sName - HTML file name.
  870. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  871. * @return string the result of operation.
  872. */
  873. function parsePageByName($sName, $aVariables)
  874. {
  875. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPage($sName);
  876. $sContent = $this->parseHtmlByName($sName, $aVariables, $this->_sKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BOTH);
  877. if(empty($sContent))
  878. $sContent = $this->parseHtmlByName('default.html', $aVariables, $this->_sKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BOTH);
  879. //--- Add CSS and JS at the very last ---//
  880. if(strpos($sContent , '<bx_include_css />') !== false) {
  881. if (!empty($this->aPage['css_name']))
  882. $this->addCss($this->aPage['css_name']);
  883. $sContent = str_replace('<bx_include_css />', $this->includeFiles('css', true) . $this->includeFiles('css'), $sContent);
  884. }
  885. if(strpos($sContent , '<bx_include_js />') !== false) {
  886. if (!empty($this->aPage['js_name']))
  887. $this->addJs($this->aPage['js_name']);
  888. $sContent = str_replace('<bx_include_js />', $this->includeFiles('js', true) . $this->includeFiles('js'), $sContent);
  889. }
  890. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPage($sContent);
  891. return $sContent;
  892. }
  893. /**
  894. * Parse system keys.
  895. *
  896. * @param string $sKey key
  897. * @return string value associated with the key.
  898. */
  899. function parseSystemKey($sKey, $mixedKeyWrapperHtml = null, $bProcessInjection = true)
  900. {
  901. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  902. $sRet = '';
  903. switch( $sKey ) {
  904. case 'page_charset':
  905. $sRet = 'UTF-8';
  906. break;
  907. case 'page_robots':
  908. if(!empty($this->aPage['robots']) && is_string($this->aPage['robots']))
  909. $sRet = '<meta name="robots" content="' . bx_html_attribute($this->aPage['robots']) . '" />';
  910. break;
  911. case 'meta_info':
  912. $sRet = $this->getMetaInfo();
  913. break;
  914. case 'page_header':
  915. if(isset($this->aPage['header']))
  916. $sRet = bx_process_output(strip_tags($this->aPage['header']));
  917. break;
  918. case 'page_header_text':
  919. if(isset($this->aPage['header_text']))
  920. $sRet = bx_process_output($this->aPage['header_text']);
  921. break;
  922. case 'popup_loading':
  923. $s = $this->parsePageByName('popup_loading.html', array());
  924. $sRet = BxTemplFunctions::getInstance()->transBox('bx-popup-loading', $s, true);
  925. $oSearch = new BxTemplSearch();
  926. $oSearch->setLiveSearch(true);
  927. $sRet .= $this->parsePageByName('search.html', array(
  928. 'search_form' => $oSearch->getForm(BX_DB_CONTENT_ONLY),
  929. 'results' => $oSearch->getResultsContainer(),
  930. ));
  931. $sRet .= $this->getMenu ('sys_site');
  932. $sRet .= isLogged() ? $this->getMenu ('sys_add_content') : '';
  933. $sRet .= isLogged() ? $this->getMenu ('sys_account_popup') : '';
  934. break;
  935. case 'lang':
  936. $sRet = bx_lang_name();
  937. break;
  938. case 'main_logo':
  939. $sRet = BxTemplFunctions::getInstance()->getMainLogo();
  940. break;
  941. case 'informer':
  942. $oInformer = BxDolInformer::getInstance($this);
  943. $sRet = $oInformer ? $oInformer->display() : '';
  944. break;
  945. case 'dol_images':
  946. $sRet = $this->_processJsImages();
  947. break;
  948. case 'dol_lang':
  949. $sRet = $this->_processJsTranslations();
  950. break;
  951. case 'dol_options':
  952. $sRet = $this->_processJsOptions();
  953. break;
  954. case 'copyright':
  955. $sRet = _t( '_copyright', date('Y') ) . getVersionComment();
  956. break;
  957. case 'copyright_attr':
  958. $sRet = bx_html_attribute(_t('_copyright', date('Y')));
  959. break;
  960. case 'extra_js':
  961. $sRet = empty($this->aPage['extra_js']) ? '' : $this->aPage['extra_js'];
  962. break;
  963. case 'is_profile_page':
  964. $sRet = (defined('BX_PROFILE_PAGE')) ? 'true' : 'false';
  965. break;
  966. default:
  967. $sRet = ($sTemplAdd = BxTemplFunctions::getInstance()->TemplPageAddComponent($sKey)) !== false ? $sTemplAdd : $aKeyWrappers['left'] . $sKey . $aKeyWrappers['right'];
  968. }
  969. if($bProcessInjection)
  970. $sRet = $this->processInjection($this->getPageNameIndex(), $sKey, $sRet);
  971. return $sRet;
  972. }
  973. function getCacheFilePrefix($sType)
  974. {
  975. $sResult = '';
  976. switch($sType) {
  977. case 'template':
  978. $sResult = $this->_sCacheFilePrefix;
  979. break;
  980. case 'css':
  981. $sResult = $this->_sCssCachePrefix;
  982. break;
  983. case 'js':
  984. $sResult = $this->_sJsCachePrefix;
  985. break;
  986. }
  987. return $sResult;
  988. }
  989. /**
  990. * Get cache object for templates
  991. * @return cache class instance
  992. */
  993. function getTemplatesCacheObject ()
  994. {
  995. $sCacheEngine = getParam('sys_template_cache_engine');
  996. $oCacheEngine = bx_instance('BxDolCache' . $sCacheEngine);
  997. if(!$oCacheEngine->isAvailable())
  998. $oCacheEngine = bx_instance('BxDolCacheFileHtml');
  999. return $oCacheEngine;
  1000. }
  1001. /**
  1002. * Get template from cache if it's enabled.
  1003. *
  1004. * @param string $sName template name
  1005. * @param string $aVariables key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1006. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1007. * @param string $sCheckIn where the content would be searched(base, template, both)
  1008. * @param boolean $bEvaluate need to evaluate the template or not.
  1009. * @return string result of operation or false on failure.
  1010. */
  1011. function getCached($sName, &$aVariables, $mixedKeyWrapperHtml = null, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH, $bEvaluate = true)
  1012. {
  1013. // initialization
  1014. if(!$this->_bCacheEnable)
  1015. return false;
  1016. $sAbsolutePath = $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn);
  1017. if(empty($sAbsolutePath))
  1018. return false;
  1019. $oCacheEngine = $this->getTemplatesCacheObject ();
  1020. $isFileBasedEngine = $bEvaluate && method_exists($oCacheEngine, 'getDataFilePath');
  1021. // try to get cached content
  1022. $sCacheVariableName = "a";
  1023. $sCacheKey = $this->_getCacheFileName('html', $sAbsolutePath) . '.php';
  1024. if ($isFileBasedEngine)
  1025. $sCacheContent = $oCacheEngine->getDataFilePath($sCacheKey);
  1026. else
  1027. $sCacheContent = $oCacheEngine->getData($sCacheKey);
  1028. // recreate cache if it is empty
  1029. if ($sCacheContent === null && ($sContent = file_get_contents($sAbsolutePath)) !== false && ($sContent = $this->_compileContent($sContent, "\$" . $sCacheVariableName, 1, $aVariables, $mixedKeyWrapperHtml)) !== false) {
  1030. if (false === $oCacheEngine->setData($sCacheKey, trim($sContent)))
  1031. return false;
  1032. if ($isFileBasedEngine)
  1033. $sCacheContent = $oCacheEngine->getDataFilePath($sCacheKey);
  1034. else
  1035. $sCacheContent = $sContent;
  1036. }
  1037. if ($sCacheContent === null)
  1038. return false;
  1039. // return simple cache content
  1040. if(!$bEvaluate)
  1041. return $sCacheContent;
  1042. // return evaluated cache content
  1043. ob_start();
  1044. $$sCacheVariableName = &$aVariables;
  1045. if ($isFileBasedEngine)
  1046. include($sCacheContent);
  1047. else
  1048. eval('?'.'>' . $sCacheContent);
  1049. $sContent = ob_get_clean();
  1050. return $sContent;
  1051. }
  1052. /**
  1053. * Add JS file(s) to global output.
  1054. *
  1055. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1056. * @param boolean $bDynamic in the dynamic mode JS file(s) are not included to global output, but are returned from the function directly.
  1057. * @return boolean/string result of operation.
  1058. */
  1059. function addJs($mixedFiles, $bDynamic = false)
  1060. {
  1061. return $this->_processFiles('js', 'add', $mixedFiles, $bDynamic);
  1062. }
  1063. /**
  1064. * Add System JS file(s) to global output.
  1065. * System JS files are the files which are attached to all pages. They will be cached separately from the others.
  1066. *
  1067. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1068. * @param boolean $bDynamic in the dynamic mode JS file(s) are not included to global output, but are returned from the function directly.
  1069. * @return boolean/string result of operation.
  1070. */
  1071. function addJsSystem($mixedFiles)
  1072. {
  1073. return $this->_processFiles('js', 'add', $mixedFiles, false, true);
  1074. }
  1075. /**
  1076. * Delete JS file(s) from global output.
  1077. *
  1078. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1079. * @return boolean result of operation.
  1080. */
  1081. function deleteJs($mixedFiles)
  1082. {
  1083. return $this->_processFiles('js', 'delete', $mixedFiles);
  1084. }
  1085. /**
  1086. * Delete System JS file(s) from global output.
  1087. *
  1088. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1089. * @return boolean result of operation.
  1090. */
  1091. function deleteJsSystem($mixedFiles)
  1092. {
  1093. return $this->_processFiles('js', 'delete', $mixedFiles, false, true);
  1094. }
  1095. /**
  1096. * Compile JS files in one file.
  1097. *
  1098. * @param string $sAbsolutePath CSS file absolute path(full URL for external CSS/JS files).
  1099. * @param array $aIncluded an array of already included JS files.
  1100. * @return string result of operation.
  1101. */
  1102. function _compileJs($sAbsolutePath, &$aIncluded)
  1103. {
  1104. if(isset($aIncluded[$sAbsolutePath]))
  1105. return '';
  1106. $bExternal = strpos($sAbsolutePath, "http://") !== false || strpos($sAbsolutePath, "https://") !== false;
  1107. if($bExternal) {
  1108. $sPath = $sAbsolutePath;
  1109. $sName = '';
  1110. $sContent = bx_file_get_contents($sAbsolutePath);
  1111. } else {
  1112. $aFileInfo = pathinfo($sAbsolutePath);
  1113. $sPath = $aFileInfo['dirname'] . DIRECTORY_SEPARATOR;
  1114. $sName = $aFileInfo['basename'];
  1115. $sContent = file_get_contents($sPath . $sName);
  1116. }
  1117. if(empty($sContent))
  1118. return '';
  1119. $sUrl = bx_ltrim_str($sPath, realpath(BX_DIRECTORY_PATH_ROOT), BX_DOL_URL_ROOT);
  1120. $sUrl = str_replace(DIRECTORY_SEPARATOR, '/', $sUrl);
  1121. $sContent = "\r\n/*--- BEGIN: " . $sUrl . $sName . "---*/\r\n" . $sContent . ";\r\n/*--- END: " . $sUrl . $sName . "---*/\r\n";
  1122. $sContent = str_replace(array("\n\r", "\r\n", "\r"), "\n", $sContent);
  1123. $aIncluded[$sAbsolutePath] = 1;
  1124. return preg_replace(
  1125. array(
  1126. "'<bx_url_root />'",
  1127. "'\r\n'"
  1128. ),
  1129. array(
  1130. BX_DOL_URL_ROOT,
  1131. "\n"
  1132. ),
  1133. $sContent
  1134. );
  1135. }
  1136. /**
  1137. * Minify JS
  1138. *
  1139. * @param string $s JS string to minify
  1140. * @return string minified JS string.
  1141. */
  1142. function _minifyJs($s)
  1143. {
  1144. // since each JS file is minified separately, it has to be in own scope
  1145. return "\n {\n" . BxDolMinify::getInstance()->minifyJs($s) . "\n }\n";
  1146. }
  1147. /**
  1148. * Wrap an URL to JS file into JS tag.
  1149. *
  1150. * @param string $sFile - URL to JS file.
  1151. * @return string the result of operation.
  1152. */
  1153. function _wrapInTagJs($sFile)
  1154. {
  1155. return "<script language=\"javascript\" type=\"text/javascript\" src=\"" . $sFile . "\"></script>";
  1156. }
  1157. /**
  1158. * Wrap JS code into JS tag.
  1159. *
  1160. * @param string $sCode - JS code.
  1161. * @return string the result of operation.
  1162. */
  1163. function _wrapInTagJsCode($sCode)
  1164. {
  1165. return "<script language=\"javascript\" type=\"text/javascript\">\n<!--\n" . $sCode . "\n-->\n</script>";
  1166. }
  1167. /**
  1168. * Add CSS file(s) to global output.
  1169. *
  1170. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1171. * @param boolean $bDynamic in the dynamic mode CSS file(s) are not included to global output, but are returned from the function directly.
  1172. * @return boolean/string result of operation
  1173. */
  1174. function addCss($mixedFiles, $bDynamic = false)
  1175. {
  1176. return $this->_processFiles('css', 'add', $mixedFiles, $bDynamic);
  1177. }
  1178. /**
  1179. * Add System CSS file(s) to global output.
  1180. * System CSS files are the files which are attached to all pages. They will be cached separately from the others.
  1181. *
  1182. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1183. * @return boolean/string result of operation
  1184. */
  1185. function addCssSystem($mixedFiles)
  1186. {
  1187. return $this->_processFiles('css', 'add', $mixedFiles, false, true);
  1188. }
  1189. /**
  1190. * Delete CSS file(s) from global output.
  1191. *
  1192. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1193. * @return boolean result of operation.
  1194. */
  1195. function deleteCss($mixedFiles)
  1196. {
  1197. return $this->_processFiles('css', 'delete', $mixedFiles);
  1198. }
  1199. /**
  1200. * Delete System CSS file(s) from global output.
  1201. *
  1202. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1203. * @return boolean result of operation.
  1204. */
  1205. function deleteCssSystem($mixedFiles)
  1206. {
  1207. return $this->_processFiles('css', 'delete', $mixedFiles, false, true);
  1208. }
  1209. /**
  1210. * Compile CSS files' structure(@see @import css_file_path) in one file.
  1211. *
  1212. * @param string $sAbsolutePath CSS file absolute path(full URL for external CSS/JS files).
  1213. * @param array $aIncluded an array of already included CSS files.
  1214. * @return string result of operation.
  1215. */
  1216. function _compileCss($sAbsolutePath, &$aIncluded)
  1217. {
  1218. if(isset($aIncluded[$sAbsolutePath]))
  1219. return '';
  1220. $bExternal = strpos($sAbsolutePath, "http://") !== false || strpos($sAbsolutePath, "https://") !== false;
  1221. if($bExternal) {
  1222. $sPath = $sAbsolutePath;
  1223. $sName = '';
  1224. $aAPUrl = parse_url($sAbsolutePath);
  1225. if(!empty($aAPUrl['path'])) {
  1226. $aAPPath = pathinfo($aAPUrl['path']);
  1227. if(!empty($aAPPath['basename'])) {
  1228. $sPath = bx_rtrim_str($sAbsolutePath, $aAPPath['basename']);
  1229. $sName = $aAPPath['basename'];
  1230. }
  1231. }
  1232. $sContent = bx_file_get_contents($sAbsolutePath);
  1233. } else {
  1234. $aFileInfo = pathinfo($sAbsolutePath);
  1235. $sPath = $aFileInfo['dirname'] . DIRECTORY_SEPARATOR;
  1236. $sName = $aFileInfo['basename'];
  1237. $sContent = file_get_contents($sPath . $sName);
  1238. }
  1239. if(empty($sContent))
  1240. return '';
  1241. $sUrl = bx_ltrim_str($sPath, realpath(BX_DIRECTORY_PATH_ROOT), BX_DOL_URL_ROOT);
  1242. $sUrl = str_replace(DIRECTORY_SEPARATOR, '/', $sUrl);
  1243. $sContent = "\r\n/*--- BEGIN: " . $sUrl . $sName . "---*/\r\n" . $sContent . "\r\n/*--- END: " . $sUrl . $sName . "---*/\r\n";
  1244. $aIncluded[$sAbsolutePath] = 1;
  1245. $sContent = str_replace(array("\n\r", "\r\n", "\r"), "\n", $sContent);
  1246. if($bExternal) {
  1247. $sContent = preg_replace(
  1248. array(
  1249. "'@import\s+url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*\)\s*;'",
  1250. "'url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/\?\#_=-]+)\s*[\'|\"]*\s*\)'"
  1251. ),
  1252. array(
  1253. "",
  1254. "url(" . $sPath . "\\1)"
  1255. ),
  1256. $sContent
  1257. );
  1258. }
  1259. else {
  1260. try {
  1261. $oTemplate = &$this;
  1262. /* Match URL based imports like the following:
  1263. * @import 'http://[domain]/modules/[vendor]/[module]/template/css/view.css';
  1264. * Is mainly needed for CSS files which are gotten from LESS compiler.
  1265. */
  1266. $sContent = preg_replace_callback(
  1267. "'@import\s+[\'|\"]*\s*" . str_replace("/", "\/", BX_DOL_URL_ROOT) . "([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*;'", function ($aMatches) use($oTemplate, $sPath, $aIncluded) {
  1268. return $oTemplate->_compileCss(realpath(BX_DIRECTORY_PATH_ROOT . $aMatches[1]), $aIncluded);
  1269. }, $sContent);
  1270. /* Match relative path based imports like the following:
  1271. * @import url(../../../../../../base/profile/template/css/main.css);
  1272. * Is mainly needed for default CSS files.
  1273. */
  1274. $sContent = preg_replace_callback(
  1275. "'@import\s+url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*\)\s*;'", function ($aMatches) use($oTemplate, $sPath, $aIncluded) {
  1276. return $oTemplate->_compileCss(realpath($sPath . dirname($aMatches[1])) . DIRECTORY_SEPARATOR . basename($aMatches[1]), $aIncluded);
  1277. }, $sContent);
  1278. $sContent = preg_replace_callback(
  1279. "'url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/\?\#_=-]+)\s*[\'|\"]*\s*\)'", function ($aMatches) use($oTemplate, $sPath) {
  1280. $sFile = basename($aMatches[1]);
  1281. $sDirectory = dirname($aMatches[1]);
  1282. $sRootPath = realpath(BX_DIRECTORY_PATH_ROOT);
  1283. $sAbsolutePath = realpath(addslashes($sPath) . $sDirectory) . DIRECTORY_SEPARATOR . $sFile;
  1284. $sRootPath = str_replace(DIRECTORY_SEPARATOR, '/', $sRootPath);
  1285. $sAbsolutePath = str_replace(DIRECTORY_SEPARATOR, '/', $sAbsolutePath);
  1286. return 'url(' . bx_ltrim_str($sAbsolutePath, $sRootPath, BX_DOL_URL_ROOT) . ')';
  1287. }, $sContent);
  1288. }
  1289. catch(Exception $oException) {
  1290. return '';
  1291. }
  1292. }
  1293. return $sContent;
  1294. }
  1295. /**
  1296. * Less CSS
  1297. *
  1298. * @param mixed $mixed CSS string to process with Less compiler or an array with CSS file's Path and URL.
  1299. * @return mixed string or an array with CSS file's Path and URL.
  1300. */
  1301. function _lessCss($mixed)
  1302. {
  1303. require_once(BX_DIRECTORY_PATH_PLUGINS . 'lessphp/Less.php');
  1304. if(is_array($mixed) && isset($mixed['url']) && isset($mixed['path'])) {
  1305. $sPathFile = realpath($mixed['path']);
  1306. $aInfoFile = pathinfo($sPathFile);
  1307. if (!isset($aInfoFile['extension']) || $aInfoFile['extension'] != 'less')
  1308. return $mixed;
  1309. require_once(BX_DIRECTORY_PATH_PLUGINS . 'lessphp/Cache.php');
  1310. $aFiles = array($mixed['path'] => $mixed['url']);
  1311. $aOptions = array('cache_dir' => $this->_sCachePublicFolderPath);
  1312. $sFile = Less_Cache::Get($aFiles, $aOptions, $this->_oConfigTemplate->aLessConfig);
  1313. return array('url' => $this->_sCachePublicFolderUrl . $sFile, 'path' => $this->_sCachePublicFolderPath . $sFile);
  1314. }
  1315. $oLess = new Less_Parser();
  1316. $oLess->ModifyVars($this->_oConfigTemplate->aLessConfig);
  1317. $oLess->parse($mixed);
  1318. return $oLess->getCss();
  1319. }
  1320. /**
  1321. * Minify CSS
  1322. *
  1323. * @param string $s CSS string to minify
  1324. * @return string minified CSS string.
  1325. */
  1326. function _minifyCss($s)
  1327. {
  1328. return BxDolMinify::getInstance()->minifyCss($s);
  1329. }
  1330. /**
  1331. * Wrap an URL to CSS file into CSS tag.
  1332. *
  1333. * @param string $sFile - URL to CSS file.
  1334. * @return string the result of operation.
  1335. */
  1336. function _wrapInTagCss($sFile)
  1337. {
  1338. if (!$sFile)
  1339. return '';
  1340. return "<link href=\"" . $sFile . "\" rel=\"stylesheet\" type=\"text/css\" />";
  1341. }
  1342. /**
  1343. * Wrap CSS code into CSS tag.
  1344. *
  1345. * @param string $sCode - CSS code.
  1346. * @return string the result of operation.
  1347. */
  1348. function _wrapInTagCssCode($sCode)
  1349. {
  1350. return "<link rel=\"stylesheet\" type=\"text/css\">" . $sCode . "</link>";
  1351. }
  1352. /**
  1353. * Include CSS/JS file(s) attached to the page in its head section.
  1354. * @see the method is system and would be called automatically.
  1355. *
  1356. * @param string $sType the type of file('js' or 'css')
  1357. * @return string the result CSS code.
  1358. */
  1359. function includeFiles($sType, $bSystem = false)
  1360. {
  1361. $sUpcaseType = ucfirst($sType);
  1362. $sArrayKey = $sType . ($bSystem ? '_system' : '_compiled');
  1363. $aFiles = isset($this->aPage[$sArrayKey]) ? $this->aPage[$sArrayKey] : array();
  1364. if(empty($aFiles) || !is_array($aFiles))
  1365. return "";
  1366. if(!$this->{'_b' . $sUpcaseType . 'Cache'})
  1367. return $this->_includeFiles($sType, $aFiles);
  1368. //--- If cache already exists, return it ---//
  1369. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  1370. $sMethodCompile = '_compile' . $sUpcaseType;
  1371. $sMethodLess = '_less' . $sUpcaseType;
  1372. $sMethodMinify = '_minify' . $sUpcaseType;
  1373. ksort($aFiles);
  1374. $sName = "";
  1375. foreach($aFiles as $aFile)
  1376. $sName .= $aFile['url'];
  1377. $sName = $this->_getCacheFileName($sType, $sName);
  1378. $sCacheAbsoluteUrl = $this->_sCachePublicFolderUrl . $sName . '.' . $sType;
  1379. $sCacheAbsolutePath = $this->_sCachePublicFolderPath . $sName . '.' . $sType;
  1380. if(file_exists($sCacheAbsolutePath)) {
  1381. if($this->{'_b' . $sUpcaseType . 'Archive'})
  1382. $sCacheAbsoluteUrl = $this->_getLoaderUrl($sType, $sName);
  1383. return $this->$sMethodWrap($sCacheAbsoluteUrl);
  1384. }
  1385. //--- Collect all attached CSS/JS in one file ---//
  1386. $bLess = $this->_bLessEnable && method_exists($this, $sMethodLess);
  1387. $sResult = "";
  1388. $aIncluded = array();
  1389. foreach($aFiles as $aFile) {
  1390. if($bLess)
  1391. $aFile = $this->$sMethodLess($aFile);
  1392. if(($sContent = $this->$sMethodCompile($aFile['path'], $aIncluded)) === false)
  1393. continue;
  1394. if (!preg_match('/[\.-]min.(js|css)$/i', $aFile['path']) && $this->{'_b' . $sUpcaseType . 'Minify'}) // don't minify minified files
  1395. $sContent = $this->$sMethodMinify($sContent);
  1396. $sResult .= $sContent;
  1397. }
  1398. $mixedWriteResult = false;
  1399. if(!empty($sResult) && ($rHandler = fopen($sCacheAbsolutePath, 'w')) !== false) {
  1400. $mixedWriteResult = fwrite($rHandler, $sResult);
  1401. fclose($rHandler);
  1402. @chmod ($sCacheAbsolutePath, BX_DOL_FILE_RIGHTS);
  1403. }
  1404. if($mixedWriteResult === false)
  1405. return $this->_includeFiles($sType, $aFiles);
  1406. if($this->{'_b' . $sUpcaseType . 'Archive'})
  1407. $sCacheAbsoluteUrl = $this->_getLoaderUrl($sType, $sName);
  1408. return $this->$sMethodWrap($sCacheAbsoluteUrl);
  1409. }
  1410. /**
  1411. * Include CSS/JS files without caching.
  1412. *
  1413. * @param string $sType the file type (css or js)
  1414. * @param array $aFiles CSS/JS files to be added to the page.
  1415. * @return string result of operation.
  1416. */
  1417. function _includeFiles($sType, &$aFiles)
  1418. {
  1419. $sUpcaseType = ucfirst($sType);
  1420. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  1421. $sMethodLess = '_less' . $sUpcaseType;
  1422. $sResult = "";
  1423. foreach($aFiles as $aFile) {
  1424. if($this->_bLessEnable && method_exists($this, $sMethodLess))
  1425. $aFile = $this->$sMethodLess($aFile);
  1426. $sResult .= $this->$sMethodWrap($aFile['url']);
  1427. }
  1428. return $sResult;
  1429. }
  1430. /**
  1431. * Insert/Delete CSS file from output stack.
  1432. *
  1433. * @param string $sType the file type (css or js)
  1434. * @param string $sAction add/delete
  1435. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1436. * @return boolean result of operation.
  1437. */
  1438. function _processFiles($sType, $sAction, $mixedFiles, $bDynamic = false, $bSystem = false)
  1439. {
  1440. if(empty($mixedFiles))
  1441. return $bDynamic ? "" : false;
  1442. if(is_string($mixedFiles))
  1443. $mixedFiles = array($mixedFiles);
  1444. $sUpcaseType = ucfirst($sType);
  1445. $sMethodLocate = '_getAbsoluteLocation' . $sUpcaseType;
  1446. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  1447. $sResult = '';
  1448. foreach($mixedFiles as $sFile) {
  1449. //--- Process 3d Party CSS/JS file ---//
  1450. if(strpos($sFile, "http://") !== false || strpos($sFile, "https://") !== false) {
  1451. $sUrl = $sFile;
  1452. $sPath = $sFile;
  1453. }
  1454. //--- Process Custom CSS/JS file ---//
  1455. else if(strpos($sFile, "|") !== false) {
  1456. $sFile = implode('', explode("|", $sFile));
  1457. $sFile = bx_ltrim_str($sFile, BX_DIRECTORY_PATH_ROOT);
  1458. $sUrl = BX_DOL_URL_ROOT . $sFile;
  1459. $sPath = realpath(BX_DIRECTORY_PATH_ROOT . $sFile);
  1460. }
  1461. //--- Process Common CSS/JS file(check in default locations) ---//
  1462. else {
  1463. $sUrl = $this->$sMethodLocate('url', $sFile);
  1464. $sPath = $this->$sMethodLocate('path', $sFile);
  1465. }
  1466. if(empty($sPath) || empty($sUrl))
  1467. continue;
  1468. $sArrayKey = $sType . ($bSystem ? '_system' : '_compiled');
  1469. switch($sAction) {
  1470. case 'add':
  1471. if($bDynamic)
  1472. $sResult .= $this->$sMethodWrap($sUrl);
  1473. else {
  1474. $bFound = false;
  1475. $aSearchIn = $bSystem ? $this->aPage[$sArrayKey] : array_merge($this->aPage[$sType . '_system'], $this->aPage[$sArrayKey]);
  1476. foreach($aSearchIn as $iKey => $aValue)
  1477. if($aValue['url'] == $sUrl && $aValue['path'] == $sPath) {
  1478. $bFound = true;
  1479. break;
  1480. }
  1481. if(!$bFound)
  1482. $this->aPage[$sArrayKey][] = array('url' => $sUrl, 'path' => $sPath);
  1483. }
  1484. break;
  1485. case 'delete':
  1486. if(!$bDynamic)
  1487. foreach($this->aPage[$sArrayKey] as $iKey => $aValue)
  1488. if($aValue['url'] == $sUrl) {
  1489. unset($this->aPage[$sArrayKey][$iKey]);
  1490. break;
  1491. }
  1492. break;
  1493. }
  1494. }
  1495. return $bDynamic ? $sResult : true;
  1496. }
  1497. /**
  1498. * Parse content.
  1499. *
  1500. * @param string $sContent - HTML file's content.
  1501. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1502. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1503. * @return string the result of operation.
  1504. */
  1505. function _parseContent($sContent, $aVariables, $mixedKeyWrapperHtml = null)
  1506. {
  1507. $aKeys = array_keys($aVariables);
  1508. $aValues = array_values($aVariables);
  1509. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  1510. $iCountKeys = count($aKeys);
  1511. for ($i = 0; $i < $iCountKeys; $i++) {
  1512. if (strncmp($aKeys[$i], 'bx_repeat:', 10) === 0) {
  1513. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  1514. $aMatches = array();
  1515. preg_match($sKey, $sContent, $aMatches);
  1516. $sValue = '';
  1517. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  1518. if(is_array($aValues[$i]))
  1519. foreach($aValues[$i] as $aValue)
  1520. $sValue .= $this->parseHtmlByContent($aMatches[1], $aValue, $mixedKeyWrapperHtml);
  1521. else if(is_string($aValues[$i]))
  1522. $sValue = $aValues[$i];
  1523. }
  1524. } else if (strncmp($aKeys[$i], 'bx_if:', 6) === 0) {
  1525. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  1526. $aMatches = array();
  1527. preg_match($sKey, $sContent, $aMatches);
  1528. $sValue = '';
  1529. if(isset($aMatches[1]) && !empty($aMatches[1]))
  1530. if(is_array($aValues[$i]) && isset($aValues[$i]['content']) && isset($aValues[$i]['condition']) && $aValues[$i]['condition'])
  1531. $sValue .= $this->parseHtmlByContent($aMatches[1], $aValues[$i]['content'], $mixedKeyWrapperHtml);
  1532. } else {
  1533. $sKey = "'" . $aKeyWrappers['left'] . $aKeys[$i] . $aKeyWrappers['right'] . "'s";
  1534. $sValue = str_replace('$', '\\$', $aValues[$i]);
  1535. }
  1536. $aKeys[$i] = $sKey;
  1537. $aValues[$i] = $sValue;
  1538. }
  1539. try {
  1540. $oTemplate = &$this;
  1541. $aCallbackPatterns = array(
  1542. "'<bx_include_auto:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BOTH,
  1543. "'<bx_include_base:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BASE,
  1544. "'<bx_include_tmpl:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_TMPL
  1545. );
  1546. foreach($aCallbackPatterns as $sPattern => $sCheckIn)
  1547. $sContent = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $aVariables, $mixedKeyWrapperHtml, $sCheckIn) {
  1548. return $oTemplate->parseHtmlByName($aMatches[1], $aVariables, $mixedKeyWrapperHtml, $sCheckIn);
  1549. }, $sContent);
  1550. $sContent = $this->_parseContentKeys($sContent, array(
  1551. "'<bx_injection:([^\s]+) />'s" => 'get_injection',
  1552. "'<bx_menu:([^\s]+) \/>'s" => 'get_menu',
  1553. ));
  1554. }
  1555. catch(Exception $oException) {
  1556. return '';
  1557. }
  1558. $aKeys = array_merge($aKeys, array(
  1559. "'<bx_url_root />'",
  1560. "'<bx_url_studio />'",
  1561. ));
  1562. $aValues = array_merge($aValues, array(
  1563. BX_DOL_URL_ROOT,
  1564. BX_DOL_URL_STUDIO,
  1565. ));
  1566. //--- Parse Predefined Keys ---//
  1567. $sContent = preg_replace($aKeys, $aValues, $sContent);
  1568. //--- Parse System Keys ---//
  1569. $sContent = preg_replace_callback("'" . $aKeyWrappers['left'] . "([a-zA-Z0-9_-]+)" . $aKeyWrappers['right'] . "'", function($aMatches) use($oTemplate, $mixedKeyWrapperHtml) {
  1570. return $oTemplate->parseSystemKey($aMatches[1], $mixedKeyWrapperHtml);
  1571. }, $sContent);
  1572. return $sContent;
  1573. }
  1574. /**
  1575. * Compile content
  1576. *
  1577. * @param string $sContent template.
  1578. * @param string $aVarName variable name to be saved in the output file.
  1579. * @param integer $iVarDepth depth is used to process nesting, for example, in cycles.
  1580. * @param array $aVarValues values to be compiled in.
  1581. * @param mixed $mixedKeyWrapperHtml key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1582. * @return string the result of operation.
  1583. */
  1584. function _compileContent($sContent, $aVarName, $iVarDepth, $aVarValues, $mixedKeyWrapperHtml = null)
  1585. {
  1586. $aKeys = array_keys($aVarValues);
  1587. $aValues = array_values($aVarValues);
  1588. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  1589. for($i = 0; $i < count($aKeys); $i++) {
  1590. if(strpos($aKeys[$i], 'bx_repeat:') === 0) {
  1591. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  1592. $aMatches = array();
  1593. preg_match($sKey, $sContent, $aMatches);
  1594. $sValue = '';
  1595. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  1596. if(empty($aValues[$i]) || !is_array($aValues[$i]))
  1597. return false;
  1598. $sIndex = "\$" . str_repeat("i", $iVarDepth);
  1599. $sValue .= "<?php if(is_array(" . $aVarName . "['" . $aKeys[$i] . "'])) for(" . $sIndex . "=0; " . $sIndex . "<count(" . $aVarName . "['" . $aKeys[$i] . "']); " . $sIndex . "++){ ?>";
  1600. if(($sInnerValue = $this->_compileContent($aMatches[1], $aVarName . "['" . $aKeys[$i] . "'][" . $sIndex . "]", $iVarDepth + 1, current($aValues[$i]), $mixedKeyWrapperHtml)) === false)
  1601. return false;
  1602. $sValue .= $sInnerValue;
  1603. $sValue .= "<?php } else if(is_string(" . $aVarName . "['" . $aKeys[$i] . "'])) echo " . $aVarName . "['" . $aKeys[$i] . "']; ?>";
  1604. }
  1605. } else if(strpos($aKeys[$i], 'bx_if:') === 0) {
  1606. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  1607. $aMatches = array();
  1608. preg_match($sKey, $sContent, $aMatches);
  1609. $sValue = '';
  1610. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  1611. if(!is_array($aValues[$i]) || !isset($aValues[$i]['content']) || empty($aValues[$i]['content']) || !is_array($aValues[$i]['content']))
  1612. return false;
  1613. $sValue .= "<?php if(" . $aVarName . "['" . $aKeys[$i] . "']['condition']){ ?>";
  1614. if(($sInnerValue = $this->_compileContent($aMatches[1], $aVarName . "['" . $aKeys[$i] . "']['content']", $iVarDepth, $aValues[$i]['content'], $mixedKeyWrapperHtml)) === false)
  1615. return false;
  1616. $sValue .= $sInnerValue;
  1617. $sValue .= "<?php } ?>";
  1618. }
  1619. } else {
  1620. $sKey = "'" . $aKeyWrappers['left'] . $aKeys[$i] . $aKeyWrappers['right'] . "'s";
  1621. $sValue = "<?php echo " . $aVarName . "['" . $aKeys[$i] . "'];?>";
  1622. }
  1623. $aKeys[$i] = $sKey;
  1624. $aValues[$i] = $sValue;
  1625. }
  1626. try {
  1627. $oTemplate = &$this;
  1628. $aCallbackPatterns = array(
  1629. "'<bx_include_auto:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BOTH,
  1630. "'<bx_include_base:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BASE,
  1631. "'<bx_include_tmpl:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_TMPL
  1632. );
  1633. foreach($aCallbackPatterns as $sPattern => $sCheckIn)
  1634. $sContent = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $aVarValues, $mixedKeyWrapperHtml, $sCheckIn) {
  1635. $mixedResult = $oTemplate->getCached($aMatches[1], $aVarValues, $mixedKeyWrapperHtml, $sCheckIn, false);
  1636. if($mixedResult === false)
  1637. throw new Exception('Unable to create cache file.');
  1638. return $mixedResult;
  1639. }, $sContent);
  1640. $sContent = $this->_parseContentKeys($sContent);
  1641. }
  1642. catch(Exception $oException) {
  1643. return false;
  1644. }
  1645. $aKeys = array_merge($aKeys, array(
  1646. "'<bx_injection:([^\s]+) />'s",
  1647. "'<bx_menu:([^\s]+) \/>'s",
  1648. "'<bx_url_root />'",
  1649. "'<bx_url_studio />'"
  1650. ));
  1651. $aValues = array_merge($aValues, array(
  1652. "<?php echo \$this->processInjection(\$this->aPage['name_index'], '\\1'); ?>",
  1653. "<?php echo \$this->getMenu('\\1'); ?>",
  1654. BX_DOL_URL_ROOT,
  1655. BX_DOL_URL_STUDIO
  1656. ));
  1657. //--- Parse Predefined Keys ---//
  1658. $sContent = preg_replace($aKeys, $aValues, $sContent);
  1659. //--- Parse System Keys ---//
  1660. $sContent = preg_replace( "'" . $aKeyWrappers['left'] . "([a-zA-Z0-9_-]+)" . $aKeyWrappers['right'] . "'", "<?php echo \$this->parseSystemKey('\\1', \$mixedKeyWrapperHtml);?>", $sContent);
  1661. return $sContent;
  1662. }
  1663. protected function _parseContentKeys($sContent, $aCallbackPatterns = array())
  1664. {
  1665. $oTemplate = &$this;
  1666. $aCallbackPatterns = array_merge($aCallbackPatterns, array(
  1667. "'<bx_image_url:([^\s]+) \/>'s" => "get_image_url",
  1668. "'<bx_icon_url:([^\s]+) \/>'s" => "get_icon_url",
  1669. "'<bx_text:([_\{\}\w\d\s]+[^\s]{1}) \/>'s" => "get_text",
  1670. "'<bx_text_js:([^\s]+) \/>'s" => "get_text_js",
  1671. "'<bx_text_attribute:([^\s]+) \/>'s" => "get_text_attribute",
  1672. ));
  1673. foreach($aCallbackPatterns as $sPattern => $sAction)
  1674. $sContent = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $sAction) {
  1675. $sResult = '';
  1676. switch($sAction) {
  1677. case 'get_image_url':
  1678. $sResult = $oTemplate->getImageUrl($aMatches[1]);
  1679. break;
  1680. case 'get_icon_url':
  1681. $sResult = $oTemplate->getIconUrl($aMatches[1]);
  1682. break;
  1683. case 'get_text':
  1684. $sResult = _t($aMatches[1]);
  1685. break;
  1686. case 'get_text_js':
  1687. $sResult = bx_js_string(_t($aMatches[1]));
  1688. break;
  1689. case 'get_text_attribute':
  1690. $sResult = bx_html_attribute(_t($aMatches[1]));
  1691. break;
  1692. case 'get_injection':
  1693. $sResult = $oTemplate->processInjection($oTemplate->getPageNameIndex(), $aMatches[1]);
  1694. break;
  1695. case 'get_menu':
  1696. $sResult = $oTemplate->getMenu($aMatches[1]);
  1697. break;
  1698. }
  1699. return $sResult;
  1700. }, $sContent);
  1701. return $sContent;
  1702. }
  1703. /**
  1704. * Get absolute location of some template's part.
  1705. *
  1706. * @param string $sType - result type. Available values 'url' and 'path'.
  1707. * @param string $sFolder - folders to be searched in. @see $_sFolderHtml, $_sFolderCss, $_sFolderImages and $_sFolderIcons
  1708. * @param string $sName - requested part name.
  1709. * @param string $sCheckIn where the content would be searched(base, template, both)
  1710. * @return string absolute location (path/url) of the part.
  1711. */
  1712. function _getAbsoluteLocation($sType, $sFolder, $sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1713. {
  1714. $sDirectory = $this->getPath();
  1715. if($sType == 'path') {
  1716. $sDivider = DIRECTORY_SEPARATOR;
  1717. $sRoot = BX_DIRECTORY_PATH_ROOT;
  1718. } else if($sType == 'url') {
  1719. $sDivider = '/';
  1720. $sRoot = BX_DOL_URL_ROOT;
  1721. }
  1722. if(strpos($sName,'|') !== false) {
  1723. $aParts = explode('|', $sName);
  1724. $sName = $aParts[1];
  1725. if(strpos($aParts[0],'@') !== false) {
  1726. $aLocationParts = explode('@', $aParts[0]);
  1727. $sLocationKey = $this->addLocation($aLocationParts[0], BX_DIRECTORY_PATH_ROOT . $aLocationParts[1], BX_DOL_URL_ROOT . $aLocationParts[1]);
  1728. }
  1729. }
  1730. $sResult = '';
  1731. $aLocations = array_reverse($this->_aLocations, true);
  1732. foreach($aLocations as $sKey => $aLocation) {
  1733. if(($sCheckIn == BX_DOL_TEMPLATE_CHECK_IN_BOTH || $sCheckIn == BX_DOL_TEMPLATE_CHECK_IN_TMPL) && extFileExists(BX_DIRECTORY_PATH_MODULES . $this->getPath(). 'data' . DIRECTORY_SEPARATOR . BX_DOL_TEMPLATE_FOLDER_ROOT . DIRECTORY_SEPARATOR . $sKey . DIRECTORY_SEPARATOR . $sFolder . $sName))
  1734. $sResult = $sRoot . 'modules' . $sDivider . $sDirectory. 'data' . $sDivider . BX_DOL_TEMPLATE_FOLDER_ROOT . $sDivider . $sKey . $sDivider . $sFolder . $sName;
  1735. else if(($sCheckIn == BX_DOL_TEMPLATE_CHECK_IN_BOTH || $sCheckIn == BX_DOL_TEMPLATE_CHECK_IN_BASE) && extFileExists($aLocation['path'] . BX_DOL_TEMPLATE_FOLDER_ROOT . DIRECTORY_SEPARATOR . $sFolder . $sName))
  1736. $sResult = $aLocation[$sType] . BX_DOL_TEMPLATE_FOLDER_ROOT . $sDivider . $sFolder . $sName;
  1737. else
  1738. continue;
  1739. break;
  1740. }
  1741. /**
  1742. * try to find from received path
  1743. */
  1744. if(!$sResult && @is_file(BX_DIRECTORY_PATH_ROOT . $aParts[0] . DIRECTORY_SEPARATOR . $aParts[1])) {
  1745. $sResult = $sRoot . $aParts[0] . $sDivider . $aParts[1];
  1746. }
  1747. if(isset($sLocationKey))
  1748. $this->removeLocation($sLocationKey);
  1749. return $sType == 'path' && !empty($sResult) ? realpath($sResult) : $sResult;
  1750. }
  1751. /**
  1752. * Get absolute location of some template's part.
  1753. *
  1754. * @param string $sType result type. Available values 'url' and 'path'.
  1755. * @param string $sName requested part name.
  1756. * @return string absolute location (path/url) of the part.
  1757. */
  1758. function _getAbsoluteLocationJs($sType, $sName)
  1759. {
  1760. $sResult = '';
  1761. $aLocations = array_reverse($this->_aLocationsJs, true);
  1762. foreach($aLocations as $sKey => $aLocation) {
  1763. if(extFileExists($aLocation['path'] . $sName))
  1764. $sResult = $aLocation[$sType] . $sName;
  1765. else
  1766. continue;
  1767. break;
  1768. }
  1769. return $sType == 'path' && !empty($sResult) ? realpath($sResult) : $sResult;
  1770. }
  1771. function _getAbsoluteLocationCss($sType, $sName)
  1772. {
  1773. $sNameLess = str_replace('.css', '.less', $sName);
  1774. $sResult = $this->_getAbsoluteLocation($sType, $this->_sFolderCss, $sNameLess);
  1775. if(!empty($sResult))
  1776. return $sResult;
  1777. return $this->_getAbsoluteLocation($sType, $this->_sFolderCss, $sName);
  1778. }
  1779. /**
  1780. * Get inline data for Images and Icons.
  1781. *
  1782. * @param string $sType image/icon
  1783. * @param string $sName file name
  1784. * @param string $sCheckIn where the content would be searched(base, template, both)
  1785. * @return unknown
  1786. */
  1787. function _getInlineData($sType, $sName, $sCheckIn)
  1788. {
  1789. switch($sType) {
  1790. case 'image':
  1791. $sFolder = $this->_sFolderImages;
  1792. break;
  1793. case 'icon':
  1794. $sFolder = $this->_sFolderIcons;
  1795. break;
  1796. }
  1797. $sPath = $this->_getAbsoluteLocation('path', $sFolder, $sName, $sCheckIn);
  1798. $iFileSize = 0;
  1799. if($this->_bImagesInline && ($iFileSize = filesize($sPath)) !== false && $iFileSize < $this->_iImagesMaxSize) {
  1800. $aFileInfo = pathinfo($sPath);
  1801. return "data:image/" . strtolower($aFileInfo['extension']) . ";base64," . base64_encode(file_get_contents($sPath));
  1802. }
  1803. return false;
  1804. }
  1805. /**
  1806. * Get file name where the template would be cached.
  1807. *
  1808. * @param string $sAbsolutePath template's real path.
  1809. * @return string the result of operation.
  1810. */
  1811. function _getCacheFileName($sType, $sAbsolutePath)
  1812. {
  1813. $sResult = bx_site_hash($sAbsolutePath);
  1814. switch($sType) {
  1815. case 'html':
  1816. $sResult = $this->_sCacheFilePrefix . bx_lang_name() . '_' . $this->_sCode . '_' . $sResult;
  1817. break;
  1818. case 'css':
  1819. $sResult = $this->_sCssCachePrefix . $sResult;
  1820. break;
  1821. case 'js':
  1822. $sResult = $this->_sJsCachePrefix . $sResult;
  1823. break;
  1824. }
  1825. return $sResult;
  1826. }
  1827. /**
  1828. * Get template key wrappers(left, right)
  1829. *
  1830. * @param mixed $mixedKeyWrapperHtml key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1831. * @return array result of operation.
  1832. */
  1833. function _getKeyWrappers($mixedKeyWrapperHtml)
  1834. {
  1835. $aResult = array();
  1836. if(!empty($mixedKeyWrapperHtml) && is_string($mixedKeyWrapperHtml))
  1837. $aResult = array('left' => $mixedKeyWrapperHtml, 'right' => $mixedKeyWrapperHtml);
  1838. else if(!empty($mixedKeyWrapperHtml) && is_array($mixedKeyWrapperHtml))
  1839. $aResult = array('left' => $mixedKeyWrapperHtml[0], 'right' => $mixedKeyWrapperHtml[1]);
  1840. else
  1841. $aResult = array('left' => $this->_sKeyWrapperHtml, 'right' => $this->_sKeyWrapperHtml);
  1842. return $aResult;
  1843. }
  1844. /**
  1845. * Process all added language translations and return them as a string.
  1846. *
  1847. * @return string with JS code.
  1848. */
  1849. function _processJsTranslations()
  1850. {
  1851. $sReturn = '';
  1852. if(isset($this->aPage['js_translations']) && is_array($this->aPage['js_translations'])) {
  1853. foreach($this->aPage['js_translations'] as $sKey => $sString)
  1854. $sReturn .= "'" . bx_js_string($sKey) . "': '" . bx_js_string($sString) . "',";
  1855. $sReturn = substr($sReturn, 0, -1);
  1856. }
  1857. return '<script type="text/javascript" language="javascript">var aDolLang = {' . $sReturn . '};</script>';
  1858. }
  1859. /**
  1860. * Process all added options and return them as a string.
  1861. *
  1862. * @return string with JS code.
  1863. */
  1864. function _processJsOptions()
  1865. {
  1866. $sReturn = '';
  1867. if(isset($this->aPage['js_options']) && is_array($this->aPage['js_options'])) {
  1868. foreach($this->aPage['js_options'] as $sName => $mixedValue)
  1869. $sReturn .= "'" . bx_js_string($sName) . "': '" . bx_js_string($mixedValue) . "',";
  1870. $sReturn = substr($sReturn, 0, -1);
  1871. }
  1872. return '<script type="text/javascript" language="javascript">var aDolOptions = {' . $sReturn . '};</script>';
  1873. }
  1874. /**
  1875. * Process all added images and return them as a string.
  1876. *
  1877. * @return string with JS code.
  1878. */
  1879. function _processJsImages()
  1880. {
  1881. $sReturn = '';
  1882. if(isset($this->aPage['js_images']) && is_array($this->aPage['js_images'])) {
  1883. foreach($this->aPage['js_images'] as $sKey => $sUrl)
  1884. $sReturn .= "'" . bx_js_string($sKey) . "': '" . bx_js_string($sUrl) . "',";
  1885. $sReturn = substr($sReturn, 0, -1);
  1886. }
  1887. return '<script type="text/javascript" language="javascript">var aDolImages = {' . $sReturn . '};</script>';
  1888. }
  1889. /**
  1890. * Get Gzip loader URL.
  1891. *
  1892. * @param $sType content type CSS/JS
  1893. * @param $sName file name.
  1894. * @return string with URL
  1895. */
  1896. function _getLoaderUrl($sType, $sName)
  1897. {
  1898. return BX_DOL_URL_ROOT . 'gzip_loader.php?file=' . $sName . '.' . $sType;
  1899. }
  1900. /**
  1901. *
  1902. * Functions to display pages with errors, messages and so on.
  1903. *
  1904. */
  1905. function displayAccessDenied ($sMsg = '')
  1906. {
  1907. bx_import('BxDolLanguages');
  1908. header("HTTP/1.0 403 Forbidden");
  1909. $this->displayMsg($sMsg ? $sMsg : _t('_Access denied'));
  1910. }
  1911. function displayNoData ($sMsg = '')
  1912. {
  1913. bx_import('BxDolLanguages');
  1914. header("HTTP/1.0 204 No Content");
  1915. $this->displayMsg($sMsg ? $sMsg : _t('_Empty'));
  1916. }
  1917. function displayErrorOccured ($sMsg = '')
  1918. {
  1919. bx_import('BxDolLanguages');
  1920. header("HTTP/1.0 500 Internal Server Error");
  1921. $this->displayMsg($sMsg ? $sMsg : _t('_error occured'));
  1922. }
  1923. function displayPageNotFound ($sMsg = '')
  1924. {
  1925. bx_import('BxDolLanguages');
  1926. header("HTTP/1.0 404 Not Found");
  1927. $this->displayMsg($sMsg ? $sMsg : _t('_sys_request_page_not_found_cpt'));
  1928. }
  1929. function displayMsg ($s, $bTranslate = false)
  1930. {
  1931. $sTitle = $bTranslate ? _t($s) : $s;
  1932. $oTemplate = BxDolTemplate::getInstance();
  1933. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  1934. $oTemplate->setPageHeader ($sTitle);
  1935. $oTemplate->setPageContent ('page_main_code', DesignBoxContent($sTitle, MsgBox($sTitle), BX_DB_PADDING_DEF));
  1936. $oTemplate->getPageCode();
  1937. exit;
  1938. }
  1939. /**
  1940. * * * * Static methods for work with template injections * * *
  1941. *
  1942. * Static method is used to add/replace the content of some key in the template.
  1943. * It's usefull when you don't want to modify existing template but need to add some data to existing template key.
  1944. *
  1945. * @param integer $iPageIndex - page index where injections would processed. Use 0 if you want it to be done on all the pages.
  1946. * @param string $sKey - template key.
  1947. * @param string $sValue - the data to be added.
  1948. * @return string the result of operation.
  1949. */
  1950. function processInjection($iPageIndex, $sKey, $sValue = "")
  1951. {
  1952. if($iPageIndex != 0 && isset($this->aPage['injections']['page_0'][$sKey]) && isset($this->aPage['injections']['page_' . $iPageIndex][$sKey]))
  1953. $aSelection = @array_merge($this->aPage['injections']['page_0'][$sKey], $this->aPage['injections']['page_' . $iPageIndex][$sKey]);
  1954. else if(isset($this->aPage['injections']['page_0'][$sKey]))
  1955. $aSelection = $this->aPage['injections']['page_0'][$sKey];
  1956. else if(isset($this->aPage['injections']['page_' . $iPageIndex][$sKey]))
  1957. $aSelection = $this->aPage['injections']['page_' . $iPageIndex][$sKey];
  1958. else
  1959. $aSelection = array();
  1960. if(is_array($aSelection))
  1961. foreach($aSelection as $aInjection) {
  1962. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginInjection($sRand = time().rand());
  1963. $sInjData = '';
  1964. switch($aInjection['type']) {
  1965. case 'text':
  1966. $sInjData = $aInjection['data'];
  1967. break;
  1968. case 'service':
  1969. if(BxDolService::isSerializedService($aInjection['data']))
  1970. $sInjData = BxDolService::callSerialized($aInjection['data']);
  1971. break;
  1972. }
  1973. if((int)$aInjection['replace'] == 1)
  1974. $sValue = $sInjData;
  1975. else
  1976. $sValue .= $sInjData;
  1977. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endInjection($sRand, $aInjection);
  1978. }
  1979. return $sValue != '__' . $sKey . '__' ? str_replace('__' . $sKey . '__', '', $sValue) : $sValue;
  1980. }
  1981. /**
  1982. * Static method to add ingection available on the current page only.
  1983. *
  1984. * @param string $sKey - template's key.
  1985. * @param string $sType - injection type(text, php).
  1986. * @param string $sData - the data to be added.
  1987. * @param integer $iReplace - replace already existed data or not.
  1988. */
  1989. function addInjection($sKey, $sType, $sData, $iReplace = 0)
  1990. {
  1991. $this->aPage['injections']['page_0'][$sKey][] = array(
  1992. 'page_index' => 0,
  1993. 'key' => $sKey,
  1994. 'type' => $sType,
  1995. 'data' => $sData,
  1996. 'replace' => $iReplace
  1997. );
  1998. }
  1999. function getPageCode($oTemplate = null)
  2000. {
  2001. if (empty($oTemplate))
  2002. $oTemplate = $this;
  2003. header( 'Content-type: text/html; charset=utf-8' );
  2004. header( 'X-Frame-Options: sameorigin' );
  2005. echo $oTemplate->parsePageByName('page_' . $oTemplate->getPageNameIndex() . '.html', $oTemplate->getPageContent());
  2006. }
  2007. }
  2008. /** @} */