BxDolTemplate.php 78 KB

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