BxDolTemplate.php 84 KB

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