BxDolTemplate.php 88 KB

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