BxDolTemplate.php 82 KB

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