BxDolTemplate.php 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396
  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', 'protean');
  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. if(!$oCover)
  1033. break;
  1034. $bCover = true;
  1035. $oPage = BxDolPage::getObjectInstanceByURI();
  1036. if($oPage !== false) {
  1037. $bCover = $oPage->isPageCover();
  1038. if($bCover && !$oCover->isCover()) {
  1039. $aCover = $oPage->getPageCoverImage();
  1040. if(!empty($aCover))
  1041. $oCover->setCoverImageUrl($aCover);
  1042. }
  1043. }
  1044. $sRet = $bCover ? $oCover->display() : $oCover->displayEmpty();
  1045. break;
  1046. case 'dol_images':
  1047. $sRet = $this->_processJsImages();
  1048. break;
  1049. case 'dol_lang':
  1050. $sRet = $this->_processJsTranslations();
  1051. break;
  1052. case 'dol_options':
  1053. $sRet = $this->_processJsOptions();
  1054. break;
  1055. case 'copyright':
  1056. $sRet = _t( '_copyright', date('Y') ) . getVersionComment();
  1057. break;
  1058. case 'copyright_attr':
  1059. $sRet = bx_html_attribute(_t('_copyright', date('Y')));
  1060. break;
  1061. case 'extra_js':
  1062. $sRet = empty($this->aPage['extra_js']) ? '' : $this->aPage['extra_js'];
  1063. break;
  1064. case 'is_profile_page':
  1065. $sRet = (defined('BX_PROFILE_PAGE')) ? 'true' : 'false';
  1066. break;
  1067. default:
  1068. $sRet = ($sTemplAdd = BxTemplFunctions::getInstance()->TemplPageAddComponent($sKey)) !== false ? $sTemplAdd : $aKeyWrappers['left'] . $sKey . $aKeyWrappers['right'];
  1069. }
  1070. if($bProcessInjection)
  1071. $sRet = $this->processInjection($this->getPageNameIndex(), $sKey, $sRet);
  1072. return $sRet;
  1073. }
  1074. function getCacheFilePrefix($sType)
  1075. {
  1076. $sResult = '';
  1077. switch($sType) {
  1078. case 'template':
  1079. $sResult = $this->_sCacheFilePrefix;
  1080. break;
  1081. case 'css':
  1082. $sResult = $this->_sCssCachePrefix;
  1083. break;
  1084. case 'js':
  1085. $sResult = $this->_sJsCachePrefix;
  1086. break;
  1087. }
  1088. return $sResult;
  1089. }
  1090. /**
  1091. * Get cache object for templates
  1092. * @return cache class instance
  1093. */
  1094. function getTemplatesCacheObject ()
  1095. {
  1096. $sCacheEngine = getParam('sys_template_cache_engine');
  1097. $oCacheEngine = bx_instance('BxDolCache' . $sCacheEngine);
  1098. if(!$oCacheEngine->isAvailable())
  1099. $oCacheEngine = bx_instance('BxDolCacheFileHtml');
  1100. return $oCacheEngine;
  1101. }
  1102. /**
  1103. * Get template from cache if it's enabled.
  1104. *
  1105. * @param string $sName template name
  1106. * @param string $aVariables key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1107. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1108. * @param string $sCheckIn where the content would be searched(base, template, both)
  1109. * @param boolean $bEvaluate need to evaluate the template or not.
  1110. * @return string result of operation or false on failure.
  1111. */
  1112. function getCached($sName, &$aVariables, $mixedKeyWrapperHtml = null, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH, $bEvaluate = true)
  1113. {
  1114. // initialization
  1115. if(!$this->_bCacheEnable)
  1116. return false;
  1117. $sAbsolutePath = $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn);
  1118. if(empty($sAbsolutePath))
  1119. return false;
  1120. $oCacheEngine = $this->getTemplatesCacheObject ();
  1121. $isFileBasedEngine = $bEvaluate && method_exists($oCacheEngine, 'getDataFilePath');
  1122. // try to get cached content
  1123. $sCacheVariableName = "a";
  1124. $sCacheKey = $this->_getCacheFileName('html', $sAbsolutePath) . '.php';
  1125. if ($isFileBasedEngine)
  1126. $sCacheContent = $oCacheEngine->getDataFilePath($sCacheKey);
  1127. else
  1128. $sCacheContent = $oCacheEngine->getData($sCacheKey);
  1129. // recreate cache if it is empty
  1130. if ($sCacheContent === null && ($sContent = file_get_contents($sAbsolutePath)) !== false && ($sContent = $this->_compileContent($sContent, "\$" . $sCacheVariableName, 1, $aVariables, $mixedKeyWrapperHtml)) !== false) {
  1131. if (false === $oCacheEngine->setData($sCacheKey, trim($sContent)))
  1132. return false;
  1133. if ($isFileBasedEngine)
  1134. $sCacheContent = $oCacheEngine->getDataFilePath($sCacheKey);
  1135. else
  1136. $sCacheContent = $sContent;
  1137. }
  1138. if ($sCacheContent === null)
  1139. return false;
  1140. // return simple cache content
  1141. if(!$bEvaluate)
  1142. return $sCacheContent;
  1143. // return evaluated cache content
  1144. ob_start();
  1145. $$sCacheVariableName = &$aVariables;
  1146. if ($isFileBasedEngine)
  1147. include($sCacheContent);
  1148. else
  1149. eval('?'.'>' . $sCacheContent);
  1150. $sContent = ob_get_clean();
  1151. return $sContent;
  1152. }
  1153. /**
  1154. * Add JS file(s) to global output.
  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 addJs($mixedFiles, $bDynamic = false)
  1161. {
  1162. return $this->_processFiles('js', 'add', $mixedFiles, $bDynamic);
  1163. }
  1164. /**
  1165. * Add System JS file(s) to global output.
  1166. * System JS files are the files which are attached to all pages. They will be cached separately from the others.
  1167. *
  1168. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1169. * @param boolean $bDynamic in the dynamic mode JS file(s) are not included to global output, but are returned from the function directly.
  1170. * @return boolean/string result of operation.
  1171. */
  1172. function addJsSystem($mixedFiles)
  1173. {
  1174. return $this->_processFiles('js', 'add', $mixedFiles, false, true);
  1175. }
  1176. /**
  1177. * Delete JS file(s) from global output.
  1178. *
  1179. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1180. * @return boolean result of operation.
  1181. */
  1182. function deleteJs($mixedFiles)
  1183. {
  1184. return $this->_processFiles('js', 'delete', $mixedFiles);
  1185. }
  1186. /**
  1187. * Delete System JS file(s) from global output.
  1188. *
  1189. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1190. * @return boolean result of operation.
  1191. */
  1192. function deleteJsSystem($mixedFiles)
  1193. {
  1194. return $this->_processFiles('js', 'delete', $mixedFiles, false, true);
  1195. }
  1196. /**
  1197. * Compile JS files in one file.
  1198. *
  1199. * @param string $sAbsolutePath CSS file absolute path(full URL for external CSS/JS files).
  1200. * @param array $aIncluded an array of already included JS files.
  1201. * @return string result of operation.
  1202. */
  1203. function _compileJs($sAbsolutePath, &$aIncluded)
  1204. {
  1205. if(isset($aIncluded[$sAbsolutePath]))
  1206. return '';
  1207. $bExternal = strpos($sAbsolutePath, "http://") !== false || strpos($sAbsolutePath, "https://") !== false;
  1208. if($bExternal) {
  1209. $sPath = $sAbsolutePath;
  1210. $sName = '';
  1211. $sContent = bx_file_get_contents($sAbsolutePath);
  1212. } else {
  1213. $aFileInfo = pathinfo($sAbsolutePath);
  1214. $sPath = $aFileInfo['dirname'] . DIRECTORY_SEPARATOR;
  1215. $sName = $aFileInfo['basename'];
  1216. $sContent = file_get_contents($sPath . $sName);
  1217. }
  1218. if(empty($sContent))
  1219. return '';
  1220. $sUrl = bx_ltrim_str($sPath, realpath(BX_DIRECTORY_PATH_ROOT), BX_DOL_URL_ROOT);
  1221. $sUrl = str_replace(DIRECTORY_SEPARATOR, '/', $sUrl);
  1222. $sContent = "\r\n/*--- BEGIN: " . $sUrl . $sName . "---*/\r\n" . $sContent . ";\r\n/*--- END: " . $sUrl . $sName . "---*/\r\n";
  1223. $sContent = str_replace(array("\n\r", "\r\n", "\r"), "\n", $sContent);
  1224. $aIncluded[$sAbsolutePath] = 1;
  1225. return preg_replace(
  1226. array(
  1227. "'<bx_url_root />'",
  1228. "'\r\n'"
  1229. ),
  1230. array(
  1231. BX_DOL_URL_ROOT,
  1232. "\n"
  1233. ),
  1234. $sContent
  1235. );
  1236. }
  1237. /**
  1238. * Minify JS
  1239. *
  1240. * @param string $s JS string to minify
  1241. * @return string minified JS string.
  1242. */
  1243. function _minifyJs($s)
  1244. {
  1245. // since each JS file is minified separately, it has to be in own scope
  1246. return "\n {\n" . BxDolMinify::getInstance()->minifyJs($s) . "\n }\n";
  1247. }
  1248. /**
  1249. * Wrap an URL to JS file into JS tag.
  1250. *
  1251. * @param string $sFile - URL to JS file.
  1252. * @return string the result of operation.
  1253. */
  1254. function _wrapInTagJs($sFile)
  1255. {
  1256. return "<script language=\"javascript\" type=\"text/javascript\" src=\"" . $sFile . "\"></script>";
  1257. }
  1258. /**
  1259. * Wrap JS code into JS tag.
  1260. *
  1261. * @param string $sCode - JS code.
  1262. * @return string the result of operation.
  1263. */
  1264. function _wrapInTagJsCode($sCode)
  1265. {
  1266. return "<script language=\"javascript\" type=\"text/javascript\">\n<!--\n" . $sCode . "\n-->\n</script>";
  1267. }
  1268. /**
  1269. * Add CSS file(s) to global output.
  1270. *
  1271. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1272. * @param boolean $bDynamic in the dynamic mode CSS file(s) are not included to global output, but are returned from the function directly.
  1273. * @return boolean/string result of operation
  1274. */
  1275. function addCss($mixedFiles, $bDynamic = false)
  1276. {
  1277. return $this->_processFiles('css', 'add', $mixedFiles, $bDynamic);
  1278. }
  1279. /**
  1280. * Add System CSS file(s) to global output.
  1281. * System CSS files are the files which are attached to all pages. They will be cached separately from the others.
  1282. *
  1283. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1284. * @return boolean/string result of operation
  1285. */
  1286. function addCssSystem($mixedFiles)
  1287. {
  1288. return $this->_processFiles('css', 'add', $mixedFiles, false, true);
  1289. }
  1290. /**
  1291. * Delete CSS file(s) from global output.
  1292. *
  1293. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1294. * @return boolean result of operation.
  1295. */
  1296. function deleteCss($mixedFiles)
  1297. {
  1298. return $this->_processFiles('css', 'delete', $mixedFiles);
  1299. }
  1300. /**
  1301. * Delete System CSS file(s) from global output.
  1302. *
  1303. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1304. * @return boolean result of operation.
  1305. */
  1306. function deleteCssSystem($mixedFiles)
  1307. {
  1308. return $this->_processFiles('css', 'delete', $mixedFiles, false, true);
  1309. }
  1310. /**
  1311. * Compile CSS files' structure(@see @import css_file_path) in one file.
  1312. *
  1313. * @param string $sAbsolutePath CSS file absolute path(full URL for external CSS/JS files).
  1314. * @param array $aIncluded an array of already included CSS files.
  1315. * @return string result of operation.
  1316. */
  1317. function _compileCss($sAbsolutePath, &$aIncluded)
  1318. {
  1319. if(isset($aIncluded[$sAbsolutePath]))
  1320. return '';
  1321. $bExternal = strpos($sAbsolutePath, "http://") !== false || strpos($sAbsolutePath, "https://") !== false;
  1322. if($bExternal) {
  1323. $sPath = $sAbsolutePath;
  1324. $sName = '';
  1325. $aAPUrl = parse_url($sAbsolutePath);
  1326. if(!empty($aAPUrl['path'])) {
  1327. $aAPPath = pathinfo($aAPUrl['path']);
  1328. if(!empty($aAPPath['basename'])) {
  1329. $sPath = bx_rtrim_str($sAbsolutePath, $aAPPath['basename']);
  1330. $sName = $aAPPath['basename'];
  1331. }
  1332. }
  1333. $sContent = bx_file_get_contents($sAbsolutePath);
  1334. } else {
  1335. $aFileInfo = pathinfo($sAbsolutePath);
  1336. $sPath = $aFileInfo['dirname'] . DIRECTORY_SEPARATOR;
  1337. $sName = $aFileInfo['basename'];
  1338. $sContent = file_get_contents($sPath . $sName);
  1339. }
  1340. if(empty($sContent))
  1341. return '';
  1342. $sUrl = bx_ltrim_str($sPath, realpath(BX_DIRECTORY_PATH_ROOT), BX_DOL_URL_ROOT);
  1343. $sUrl = str_replace(DIRECTORY_SEPARATOR, '/', $sUrl);
  1344. $sContent = "\r\n/*--- BEGIN: " . $sUrl . $sName . "---*/\r\n" . $sContent . "\r\n/*--- END: " . $sUrl . $sName . "---*/\r\n";
  1345. $aIncluded[$sAbsolutePath] = 1;
  1346. $sContent = str_replace(array("\n\r", "\r\n", "\r"), "\n", $sContent);
  1347. if($bExternal) {
  1348. $sContent = preg_replace(
  1349. array(
  1350. "'@import\s+url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*\)\s*;'",
  1351. "'url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/\?\#_=-]+)\s*[\'|\"]*\s*\)'"
  1352. ),
  1353. array(
  1354. "",
  1355. "url(" . $sPath . "\\1)"
  1356. ),
  1357. $sContent
  1358. );
  1359. }
  1360. else {
  1361. try {
  1362. $oTemplate = &$this;
  1363. /* Match URL based imports like the following:
  1364. * @import 'http://[domain]/modules/[vendor]/[module]/template/css/view.css';
  1365. * Is mainly needed for CSS files which are gotten from LESS compiler.
  1366. */
  1367. $sContent = preg_replace_callback(
  1368. "'@import\s+[\'|\"]*\s*" . str_replace("/", "\/", BX_DOL_URL_ROOT) . "([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*;'", function ($aMatches) use($oTemplate, $sPath, $aIncluded) {
  1369. return $oTemplate->_compileCss(realpath(BX_DIRECTORY_PATH_ROOT . $aMatches[1]), $aIncluded);
  1370. }, $sContent);
  1371. /* Match relative path based imports like the following:
  1372. * @import url(../../../../../../base/profile/template/css/main.css);
  1373. * Is mainly needed for default CSS files.
  1374. */
  1375. $sContent = preg_replace_callback(
  1376. "'@import\s+url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*\)\s*;'", function ($aMatches) use($oTemplate, $sPath, $aIncluded) {
  1377. return $oTemplate->_compileCss(realpath($sPath . dirname($aMatches[1])) . DIRECTORY_SEPARATOR . basename($aMatches[1]), $aIncluded);
  1378. }, $sContent);
  1379. $sContent = preg_replace_callback(
  1380. "'url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/\?\#_=-]+)\s*[\'|\"]*\s*\)'", function ($aMatches) use($oTemplate, $sPath) {
  1381. $sFile = basename($aMatches[1]);
  1382. $sDirectory = dirname($aMatches[1]);
  1383. $sRootPath = realpath(BX_DIRECTORY_PATH_ROOT);
  1384. $sAbsolutePath = realpath(addslashes($sPath) . $sDirectory) . DIRECTORY_SEPARATOR . $sFile;
  1385. $sRootPath = str_replace(DIRECTORY_SEPARATOR, '/', $sRootPath);
  1386. $sAbsolutePath = str_replace(DIRECTORY_SEPARATOR, '/', $sAbsolutePath);
  1387. return 'url(' . bx_ltrim_str($sAbsolutePath, $sRootPath, BX_DOL_URL_ROOT) . ')';
  1388. }, $sContent);
  1389. }
  1390. catch(Exception $oException) {
  1391. return '';
  1392. }
  1393. }
  1394. return $sContent;
  1395. }
  1396. /**
  1397. * Less CSS
  1398. *
  1399. * @param mixed $mixed CSS string to process with Less compiler or an array with CSS file's Path and URL.
  1400. * @return mixed string or an array with CSS file's Path and URL.
  1401. */
  1402. function _lessCss($mixed)
  1403. {
  1404. require_once(BX_DIRECTORY_PATH_PLUGINS . 'lessphp/Less.php');
  1405. if(is_array($mixed) && isset($mixed['url']) && isset($mixed['path'])) {
  1406. $sPathFile = realpath($mixed['path']);
  1407. $aInfoFile = pathinfo($sPathFile);
  1408. if (!isset($aInfoFile['extension']) || $aInfoFile['extension'] != 'less')
  1409. return $mixed;
  1410. require_once(BX_DIRECTORY_PATH_PLUGINS . 'lessphp/Cache.php');
  1411. $aFiles = array($mixed['path'] => $mixed['url']);
  1412. $aOptions = array('cache_dir' => $this->_sCachePublicFolderPath);
  1413. $sFile = Less_Cache::Get($aFiles, $aOptions, $this->_oConfigTemplate->aLessConfig);
  1414. return array('url' => $this->_sCachePublicFolderUrl . $sFile, 'path' => $this->_sCachePublicFolderPath . $sFile);
  1415. }
  1416. $oLess = new Less_Parser();
  1417. $oLess->ModifyVars($this->_oConfigTemplate->aLessConfig);
  1418. $oLess->parse($mixed);
  1419. return $oLess->getCss();
  1420. }
  1421. /**
  1422. * Minify CSS
  1423. *
  1424. * @param string $s CSS string to minify
  1425. * @return string minified CSS string.
  1426. */
  1427. function _minifyCss($s)
  1428. {
  1429. return BxDolMinify::getInstance()->minifyCss($s);
  1430. }
  1431. /**
  1432. * Wrap an URL to CSS file into CSS tag.
  1433. *
  1434. * @param string $sFile - URL to CSS file.
  1435. * @return string the result of operation.
  1436. */
  1437. function _wrapInTagCss($sFile)
  1438. {
  1439. if (!$sFile)
  1440. return '';
  1441. return "<link href=\"" . $sFile . "\" rel=\"stylesheet\" type=\"text/css\" />";
  1442. }
  1443. /**
  1444. * Wrap CSS code into CSS tag.
  1445. *
  1446. * @param string $sCode - CSS code.
  1447. * @return string the result of operation.
  1448. */
  1449. function _wrapInTagCssCode($sCode)
  1450. {
  1451. return "<link rel=\"stylesheet\" type=\"text/css\">" . $sCode . "</link>";
  1452. }
  1453. /**
  1454. * Include CSS/JS file(s) attached to the page in its head section.
  1455. * @see the method is system and would be called automatically.
  1456. *
  1457. * @param string $sType the type of file('js' or 'css')
  1458. * @return string the result CSS code.
  1459. */
  1460. function includeFiles($sType, $bSystem = false)
  1461. {
  1462. $sUpcaseType = ucfirst($sType);
  1463. $sArrayKey = $sType . ($bSystem ? '_system' : '_compiled');
  1464. $aFiles = isset($this->aPage[$sArrayKey]) ? $this->aPage[$sArrayKey] : array();
  1465. if(empty($aFiles) || !is_array($aFiles))
  1466. return "";
  1467. if(!$this->{'_b' . $sUpcaseType . 'Cache'})
  1468. return $this->_includeFiles($sType, $aFiles);
  1469. //--- If cache already exists, return it ---//
  1470. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  1471. $sMethodCompile = '_compile' . $sUpcaseType;
  1472. $sMethodLess = '_less' . $sUpcaseType;
  1473. $sMethodMinify = '_minify' . $sUpcaseType;
  1474. ksort($aFiles);
  1475. $sName = "";
  1476. foreach($aFiles as $aFile)
  1477. $sName .= $aFile['url'];
  1478. $sName = $this->_getCacheFileName($sType, $sName);
  1479. $sCacheAbsoluteUrl = $this->_sCachePublicFolderUrl . $sName . '.' . $sType;
  1480. $sCacheAbsolutePath = $this->_sCachePublicFolderPath . $sName . '.' . $sType;
  1481. if(file_exists($sCacheAbsolutePath)) {
  1482. if($this->{'_b' . $sUpcaseType . 'Archive'})
  1483. $sCacheAbsoluteUrl = $this->_getLoaderUrl($sType, $sName);
  1484. return $this->$sMethodWrap($sCacheAbsoluteUrl);
  1485. }
  1486. //--- Collect all attached CSS/JS in one file ---//
  1487. $bLess = $this->_bLessEnable && method_exists($this, $sMethodLess);
  1488. $sResult = "";
  1489. $aIncluded = array();
  1490. foreach($aFiles as $aFile) {
  1491. if($bLess)
  1492. $aFile = $this->$sMethodLess($aFile);
  1493. if(($sContent = $this->$sMethodCompile($aFile['path'], $aIncluded)) === false)
  1494. continue;
  1495. if (!preg_match('/[\.-]min.(js|css)$/i', $aFile['path']) && $this->{'_b' . $sUpcaseType . 'Minify'}) // don't minify minified files
  1496. $sContent = $this->$sMethodMinify($sContent);
  1497. $sResult .= $sContent;
  1498. }
  1499. $mixedWriteResult = false;
  1500. if(!empty($sResult) && ($rHandler = fopen($sCacheAbsolutePath, 'w')) !== false) {
  1501. $mixedWriteResult = fwrite($rHandler, $sResult);
  1502. fclose($rHandler);
  1503. @chmod ($sCacheAbsolutePath, BX_DOL_FILE_RIGHTS);
  1504. }
  1505. if($mixedWriteResult === false)
  1506. return $this->_includeFiles($sType, $aFiles);
  1507. if($this->{'_b' . $sUpcaseType . 'Archive'})
  1508. $sCacheAbsoluteUrl = $this->_getLoaderUrl($sType, $sName);
  1509. return $this->$sMethodWrap($sCacheAbsoluteUrl);
  1510. }
  1511. /**
  1512. * Include CSS/JS files without caching.
  1513. *
  1514. * @param string $sType the file type (css or js)
  1515. * @param array $aFiles CSS/JS files to be added to the page.
  1516. * @return string result of operation.
  1517. */
  1518. function _includeFiles($sType, &$aFiles)
  1519. {
  1520. $sUpcaseType = ucfirst($sType);
  1521. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  1522. $sMethodLess = '_less' . $sUpcaseType;
  1523. $sResult = "";
  1524. foreach($aFiles as $aFile) {
  1525. if($this->_bLessEnable && method_exists($this, $sMethodLess))
  1526. $aFile = $this->$sMethodLess($aFile);
  1527. $sResult .= $this->$sMethodWrap($aFile['url']);
  1528. }
  1529. return $sResult;
  1530. }
  1531. /**
  1532. * Insert/Delete CSS file from output stack.
  1533. *
  1534. * @param string $sType the file type (css or js)
  1535. * @param string $sAction add/delete
  1536. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1537. * @return boolean result of operation.
  1538. */
  1539. function _processFiles($sType, $sAction, $mixedFiles, $bDynamic = false, $bSystem = false)
  1540. {
  1541. if(empty($mixedFiles))
  1542. return $bDynamic ? "" : false;
  1543. if(is_string($mixedFiles))
  1544. $mixedFiles = array($mixedFiles);
  1545. $sUpcaseType = ucfirst($sType);
  1546. $sMethodLocate = '_getAbsoluteLocation' . $sUpcaseType;
  1547. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  1548. $sResult = '';
  1549. foreach($mixedFiles as $sFile) {
  1550. //--- Process 3d Party CSS/JS file ---//
  1551. if(strpos($sFile, "http://") !== false || strpos($sFile, "https://") !== false) {
  1552. $sUrl = $sFile;
  1553. $sPath = $sFile;
  1554. }
  1555. //--- Process Custom CSS/JS file ---//
  1556. else if(strpos($sFile, "|") !== false) {
  1557. $sFile = implode('', explode("|", $sFile));
  1558. $sFile = bx_ltrim_str($sFile, BX_DIRECTORY_PATH_ROOT);
  1559. $sUrl = BX_DOL_URL_ROOT . $sFile;
  1560. $sPath = realpath(BX_DIRECTORY_PATH_ROOT . $sFile);
  1561. }
  1562. //--- Process Common CSS/JS file(check in default locations) ---//
  1563. else {
  1564. $sUrl = $this->$sMethodLocate('url', $sFile);
  1565. $sPath = $this->$sMethodLocate('path', $sFile);
  1566. }
  1567. if(empty($sPath) || empty($sUrl))
  1568. continue;
  1569. $sArrayKey = $sType . ($bSystem ? '_system' : '_compiled');
  1570. switch($sAction) {
  1571. case 'add':
  1572. if($bDynamic)
  1573. $sResult .= $this->$sMethodWrap($sUrl);
  1574. else {
  1575. $bFound = false;
  1576. $aSearchIn = $bSystem ? $this->aPage[$sArrayKey] : array_merge($this->aPage[$sType . '_system'], $this->aPage[$sArrayKey]);
  1577. foreach($aSearchIn as $iKey => $aValue)
  1578. if($aValue['url'] == $sUrl && $aValue['path'] == $sPath) {
  1579. $bFound = true;
  1580. break;
  1581. }
  1582. if(!$bFound)
  1583. $this->aPage[$sArrayKey][] = array('url' => $sUrl, 'path' => $sPath);
  1584. }
  1585. break;
  1586. case 'delete':
  1587. if(!$bDynamic)
  1588. foreach($this->aPage[$sArrayKey] as $iKey => $aValue)
  1589. if($aValue['url'] == $sUrl) {
  1590. unset($this->aPage[$sArrayKey][$iKey]);
  1591. break;
  1592. }
  1593. break;
  1594. }
  1595. }
  1596. return $bDynamic ? $sResult : true;
  1597. }
  1598. /**
  1599. * Parse content.
  1600. *
  1601. * @param string $sContent - HTML file's content.
  1602. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1603. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1604. * @return string the result of operation.
  1605. */
  1606. function _parseContent($sContent, $aVariables, $mixedKeyWrapperHtml = null)
  1607. {
  1608. $aKeys = array_keys($aVariables);
  1609. $aValues = array_values($aVariables);
  1610. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  1611. $iCountKeys = count($aKeys);
  1612. for ($i = 0; $i < $iCountKeys; $i++) {
  1613. if (strncmp($aKeys[$i], 'bx_repeat:', 10) === 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]))
  1620. foreach($aValues[$i] as $aValue)
  1621. $sValue .= $this->parseHtmlByContent($aMatches[1], $aValue, $mixedKeyWrapperHtml);
  1622. else if(is_string($aValues[$i]))
  1623. $sValue = $aValues[$i];
  1624. }
  1625. } else if (strncmp($aKeys[$i], 'bx_if:', 6) === 0) {
  1626. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  1627. $aMatches = array();
  1628. preg_match($sKey, $sContent, $aMatches);
  1629. $sValue = '';
  1630. if(isset($aMatches[1]) && !empty($aMatches[1]))
  1631. if(is_array($aValues[$i]) && isset($aValues[$i]['content']) && isset($aValues[$i]['condition']) && $aValues[$i]['condition'])
  1632. $sValue .= $this->parseHtmlByContent($aMatches[1], $aValues[$i]['content'], $mixedKeyWrapperHtml);
  1633. } else {
  1634. $sKey = "'" . $aKeyWrappers['left'] . $aKeys[$i] . $aKeyWrappers['right'] . "'s";
  1635. $sValue = str_replace('$', '\\$', $aValues[$i]);
  1636. }
  1637. $aKeys[$i] = $sKey;
  1638. $aValues[$i] = $sValue;
  1639. }
  1640. try {
  1641. $oTemplate = &$this;
  1642. $aCallbackPatterns = array(
  1643. "'<bx_include_auto:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BOTH,
  1644. "'<bx_include_base:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BASE,
  1645. "'<bx_include_tmpl:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_TMPL
  1646. );
  1647. foreach($aCallbackPatterns as $sPattern => $sCheckIn)
  1648. $sContent = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $aVariables, $mixedKeyWrapperHtml, $sCheckIn) {
  1649. return $oTemplate->parseHtmlByName($aMatches[1], $aVariables, $mixedKeyWrapperHtml, $sCheckIn);
  1650. }, $sContent);
  1651. $sContent = $this->_parseContentKeys($sContent, array(
  1652. "'<bx_injection:([^\s]+) />'s" => 'get_injection',
  1653. "'<bx_menu:([^\s]+) \/>'s" => 'get_menu',
  1654. ));
  1655. }
  1656. catch(Exception $oException) {
  1657. return '';
  1658. }
  1659. $aKeys = array_merge($aKeys, array(
  1660. "'<bx_url_root />'",
  1661. "'<bx_url_studio />'",
  1662. ));
  1663. $aValues = array_merge($aValues, array(
  1664. BX_DOL_URL_ROOT,
  1665. BX_DOL_URL_STUDIO,
  1666. ));
  1667. //--- Parse Predefined Keys ---//
  1668. $sContent = preg_replace($aKeys, $aValues, $sContent);
  1669. //--- Parse System Keys ---//
  1670. $sContent = preg_replace_callback("'" . $aKeyWrappers['left'] . "([a-zA-Z0-9_-]+)" . $aKeyWrappers['right'] . "'", function($aMatches) use($oTemplate, $mixedKeyWrapperHtml) {
  1671. return $oTemplate->parseSystemKey($aMatches[1], $mixedKeyWrapperHtml);
  1672. }, $sContent);
  1673. return $sContent;
  1674. }
  1675. /**
  1676. * Compile content
  1677. *
  1678. * @param string $sContent template.
  1679. * @param string $aVarName variable name to be saved in the output file.
  1680. * @param integer $iVarDepth depth is used to process nesting, for example, in cycles.
  1681. * @param array $aVarValues values to be compiled in.
  1682. * @param mixed $mixedKeyWrapperHtml key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1683. * @return string the result of operation.
  1684. */
  1685. function _compileContent($sContent, $aVarName, $iVarDepth, $aVarValues, $mixedKeyWrapperHtml = null)
  1686. {
  1687. $aKeys = array_keys($aVarValues);
  1688. $aValues = array_values($aVarValues);
  1689. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  1690. for($i = 0; $i < count($aKeys); $i++) {
  1691. if(strpos($aKeys[$i], 'bx_repeat:') === 0) {
  1692. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  1693. $aMatches = array();
  1694. preg_match($sKey, $sContent, $aMatches);
  1695. $sValue = '';
  1696. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  1697. if(empty($aValues[$i]) || !is_array($aValues[$i]))
  1698. return false;
  1699. $sIndex = "\$" . str_repeat("i", $iVarDepth);
  1700. $sValue .= "<?php if(is_array(" . $aVarName . "['" . $aKeys[$i] . "'])) for(" . $sIndex . "=0; " . $sIndex . "<count(" . $aVarName . "['" . $aKeys[$i] . "']); " . $sIndex . "++){ ?>";
  1701. if(($sInnerValue = $this->_compileContent($aMatches[1], $aVarName . "['" . $aKeys[$i] . "'][" . $sIndex . "]", $iVarDepth + 1, current($aValues[$i]), $mixedKeyWrapperHtml)) === false)
  1702. return false;
  1703. $sValue .= $sInnerValue;
  1704. $sValue .= "<?php } else if(is_string(" . $aVarName . "['" . $aKeys[$i] . "'])) echo " . $aVarName . "['" . $aKeys[$i] . "']; ?>";
  1705. }
  1706. } else if(strpos($aKeys[$i], 'bx_if:') === 0) {
  1707. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  1708. $aMatches = array();
  1709. preg_match($sKey, $sContent, $aMatches);
  1710. $sValue = '';
  1711. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  1712. if(!is_array($aValues[$i]) || !isset($aValues[$i]['content']) || empty($aValues[$i]['content']) || !is_array($aValues[$i]['content']))
  1713. return false;
  1714. $sValue .= "<?php if(" . $aVarName . "['" . $aKeys[$i] . "']['condition']){ ?>";
  1715. if(($sInnerValue = $this->_compileContent($aMatches[1], $aVarName . "['" . $aKeys[$i] . "']['content']", $iVarDepth, $aValues[$i]['content'], $mixedKeyWrapperHtml)) === false)
  1716. return false;
  1717. $sValue .= $sInnerValue;
  1718. $sValue .= "<?php } ?>";
  1719. }
  1720. } else {
  1721. $sKey = "'" . $aKeyWrappers['left'] . $aKeys[$i] . $aKeyWrappers['right'] . "'s";
  1722. $sValue = "<?php echo " . $aVarName . "['" . $aKeys[$i] . "'];?>";
  1723. }
  1724. $aKeys[$i] = $sKey;
  1725. $aValues[$i] = $sValue;
  1726. }
  1727. try {
  1728. $oTemplate = &$this;
  1729. $aCallbackPatterns = array(
  1730. "'<bx_include_auto:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BOTH,
  1731. "'<bx_include_base:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BASE,
  1732. "'<bx_include_tmpl:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_TMPL
  1733. );
  1734. foreach($aCallbackPatterns as $sPattern => $sCheckIn)
  1735. $sContent = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $aVarValues, $mixedKeyWrapperHtml, $sCheckIn) {
  1736. $mixedResult = $oTemplate->getCached($aMatches[1], $aVarValues, $mixedKeyWrapperHtml, $sCheckIn, false);
  1737. if($mixedResult === false)
  1738. throw new Exception('Unable to create cache file.');
  1739. return $mixedResult;
  1740. }, $sContent);
  1741. $sContent = $this->_parseContentKeys($sContent);
  1742. }
  1743. catch(Exception $oException) {
  1744. return false;
  1745. }
  1746. $aKeys = array_merge($aKeys, array(
  1747. "'<bx_injection:([^\s]+) />'s",
  1748. "'<bx_menu:([^\s]+) \/>'s",
  1749. "'<bx_url_root />'",
  1750. "'<bx_url_studio />'"
  1751. ));
  1752. $aValues = array_merge($aValues, array(
  1753. "<?php echo \$this->processInjection(\$this->aPage['name_index'], '\\1'); ?>",
  1754. "<?php echo \$this->getMenu('\\1'); ?>",
  1755. BX_DOL_URL_ROOT,
  1756. BX_DOL_URL_STUDIO
  1757. ));
  1758. //--- Parse Predefined Keys ---//
  1759. $sContent = preg_replace($aKeys, $aValues, $sContent);
  1760. //--- Parse System Keys ---//
  1761. $sContent = preg_replace( "'" . $aKeyWrappers['left'] . "([a-zA-Z0-9_-]+)" . $aKeyWrappers['right'] . "'", "<?php echo \$this->parseSystemKey('\\1', \$mixedKeyWrapperHtml);?>", $sContent);
  1762. return $sContent;
  1763. }
  1764. protected function _parseContentKeys($sContent, $aCallbackPatterns = array())
  1765. {
  1766. $oTemplate = &$this;
  1767. $aCallbackPatterns = array_merge($aCallbackPatterns, array(
  1768. "'<bx_image_url:([^\s]+) \/>'s" => "get_image_url",
  1769. "'<bx_icon_url:([^\s]+) \/>'s" => "get_icon_url",
  1770. "'<bx_text:([_\{\}\w\d\s]+[^\s]{1}) \/>'s" => "get_text",
  1771. "'<bx_text_js:([^\s]+) \/>'s" => "get_text_js",
  1772. "'<bx_text_attribute:([^\s]+) \/>'s" => "get_text_attribute",
  1773. ));
  1774. foreach($aCallbackPatterns as $sPattern => $sAction)
  1775. $sContent = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $sAction) {
  1776. $sResult = '';
  1777. switch($sAction) {
  1778. case 'get_image_url':
  1779. $sResult = $oTemplate->getImageUrl($aMatches[1]);
  1780. break;
  1781. case 'get_icon_url':
  1782. $sResult = $oTemplate->getIconUrl($aMatches[1]);
  1783. break;
  1784. case 'get_text':
  1785. $sResult = _t($aMatches[1]);
  1786. break;
  1787. case 'get_text_js':
  1788. $sResult = bx_js_string(_t($aMatches[1]));
  1789. break;
  1790. case 'get_text_attribute':
  1791. $sResult = bx_html_attribute(_t($aMatches[1]));
  1792. break;
  1793. case 'get_injection':
  1794. $sResult = $oTemplate->processInjection($oTemplate->getPageNameIndex(), $aMatches[1]);
  1795. break;
  1796. case 'get_menu':
  1797. $sResult = $oTemplate->getMenu($aMatches[1]);
  1798. break;
  1799. }
  1800. return $sResult;
  1801. }, $sContent);
  1802. return $sContent;
  1803. }
  1804. /**
  1805. * Get absolute location of some template's part.
  1806. *
  1807. * @param string $sType - result type. Available values 'url' and 'path'.
  1808. * @param string $sFolder - folders to be searched in. @see $_sFolderHtml, $_sFolderCss, $_sFolderImages and $_sFolderIcons
  1809. * @param string $sName - requested part name.
  1810. * @param string $sCheckIn where the content would be searched(base, template, both)
  1811. * @return string absolute location (path/url) of the part.
  1812. */
  1813. function _getAbsoluteLocation($sType, $sFolder, $sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1814. {
  1815. $sDirectory = $this->getPath();
  1816. if($sType == 'path') {
  1817. $sDivider = DIRECTORY_SEPARATOR;
  1818. $sRoot = BX_DIRECTORY_PATH_ROOT;
  1819. } else if($sType == 'url') {
  1820. $sDivider = '/';
  1821. $sRoot = BX_DOL_URL_ROOT;
  1822. }
  1823. if(strpos($sName,'|') !== false) {
  1824. $aParts = explode('|', $sName);
  1825. $sName = $aParts[1];
  1826. if(strpos($aParts[0],'@') !== false) {
  1827. $aLocationParts = explode('@', $aParts[0]);
  1828. $sLocationKey = $this->addLocation($aLocationParts[0], BX_DIRECTORY_PATH_ROOT . $aLocationParts[1], BX_DOL_URL_ROOT . $aLocationParts[1]);
  1829. }
  1830. }
  1831. $sResult = '';
  1832. $aLocations = array_reverse($this->_aLocations, true);
  1833. foreach($aLocations as $sKey => $aLocation) {
  1834. 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))
  1835. $sResult = $sRoot . 'modules' . $sDivider . $sDirectory. 'data' . $sDivider . BX_DOL_TEMPLATE_FOLDER_ROOT . $sDivider . $sKey . $sDivider . $sFolder . $sName;
  1836. 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))
  1837. $sResult = $aLocation[$sType] . BX_DOL_TEMPLATE_FOLDER_ROOT . $sDivider . $sFolder . $sName;
  1838. else
  1839. continue;
  1840. break;
  1841. }
  1842. /**
  1843. * try to find from received path
  1844. */
  1845. if(!$sResult && @is_file(BX_DIRECTORY_PATH_ROOT . $aParts[0] . DIRECTORY_SEPARATOR . $aParts[1])) {
  1846. $sResult = $sRoot . $aParts[0] . $sDivider . $aParts[1];
  1847. }
  1848. if(isset($sLocationKey))
  1849. $this->removeLocation($sLocationKey);
  1850. return $sType == 'path' && !empty($sResult) ? realpath($sResult) : $sResult;
  1851. }
  1852. /**
  1853. * Get absolute location of some template's part.
  1854. *
  1855. * @param string $sType result type. Available values 'url' and 'path'.
  1856. * @param string $sName requested part name.
  1857. * @return string absolute location (path/url) of the part.
  1858. */
  1859. function _getAbsoluteLocationJs($sType, $sName)
  1860. {
  1861. $sResult = '';
  1862. $aLocations = array_reverse($this->_aLocationsJs, true);
  1863. foreach($aLocations as $sKey => $aLocation) {
  1864. if(extFileExists($aLocation['path'] . $sName))
  1865. $sResult = $aLocation[$sType] . $sName;
  1866. else
  1867. continue;
  1868. break;
  1869. }
  1870. return $sType == 'path' && !empty($sResult) ? realpath($sResult) : $sResult;
  1871. }
  1872. function _getAbsoluteLocationCss($sType, $sName)
  1873. {
  1874. $sNameLess = str_replace('.css', '.less', $sName);
  1875. $sResult = $this->_getAbsoluteLocation($sType, $this->_sFolderCss, $sNameLess);
  1876. if(!empty($sResult))
  1877. return $sResult;
  1878. return $this->_getAbsoluteLocation($sType, $this->_sFolderCss, $sName);
  1879. }
  1880. /**
  1881. * Get inline data for Images and Icons.
  1882. *
  1883. * @param string $sType image/icon
  1884. * @param string $sName file name
  1885. * @param string $sCheckIn where the content would be searched(base, template, both)
  1886. * @return unknown
  1887. */
  1888. function _getInlineData($sType, $sName, $sCheckIn)
  1889. {
  1890. switch($sType) {
  1891. case 'image':
  1892. $sFolder = $this->_sFolderImages;
  1893. break;
  1894. case 'icon':
  1895. $sFolder = $this->_sFolderIcons;
  1896. break;
  1897. }
  1898. $sPath = $this->_getAbsoluteLocation('path', $sFolder, $sName, $sCheckIn);
  1899. $iFileSize = 0;
  1900. if($this->_bImagesInline && ($iFileSize = filesize($sPath)) !== false && $iFileSize < $this->_iImagesMaxSize) {
  1901. $aFileInfo = pathinfo($sPath);
  1902. return "data:image/" . strtolower($aFileInfo['extension']) . ";base64," . base64_encode(file_get_contents($sPath));
  1903. }
  1904. return false;
  1905. }
  1906. /**
  1907. * Get file name where the template would be cached.
  1908. *
  1909. * @param string $sAbsolutePath template's real path.
  1910. * @return string the result of operation.
  1911. */
  1912. function _getCacheFileName($sType, $sAbsolutePath)
  1913. {
  1914. $sResult = bx_site_hash($sAbsolutePath);
  1915. switch($sType) {
  1916. case 'html':
  1917. $sResult = $this->_sCacheFilePrefix . bx_lang_name() . '_' . $this->_sCode . '_' . $sResult;
  1918. break;
  1919. case 'css':
  1920. $sResult = $this->_sCssCachePrefix . $sResult;
  1921. break;
  1922. case 'js':
  1923. $sResult = $this->_sJsCachePrefix . $sResult;
  1924. break;
  1925. }
  1926. return $sResult;
  1927. }
  1928. /**
  1929. * Get template key wrappers(left, right)
  1930. *
  1931. * @param mixed $mixedKeyWrapperHtml key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1932. * @return array result of operation.
  1933. */
  1934. function _getKeyWrappers($mixedKeyWrapperHtml)
  1935. {
  1936. $aResult = array();
  1937. if(!empty($mixedKeyWrapperHtml) && is_string($mixedKeyWrapperHtml))
  1938. $aResult = array('left' => $mixedKeyWrapperHtml, 'right' => $mixedKeyWrapperHtml);
  1939. else if(!empty($mixedKeyWrapperHtml) && is_array($mixedKeyWrapperHtml))
  1940. $aResult = array('left' => $mixedKeyWrapperHtml[0], 'right' => $mixedKeyWrapperHtml[1]);
  1941. else
  1942. $aResult = array('left' => $this->_sKeyWrapperHtml, 'right' => $this->_sKeyWrapperHtml);
  1943. return $aResult;
  1944. }
  1945. /**
  1946. * Process all added language translations and return them as a string.
  1947. *
  1948. * @return string with JS code.
  1949. */
  1950. function _processJsTranslations()
  1951. {
  1952. $sReturn = '';
  1953. if(isset($this->aPage['js_translations']) && is_array($this->aPage['js_translations'])) {
  1954. foreach($this->aPage['js_translations'] as $sKey => $sString)
  1955. $sReturn .= "'" . bx_js_string($sKey) . "': '" . bx_js_string($sString) . "',";
  1956. $sReturn = substr($sReturn, 0, -1);
  1957. }
  1958. return '<script type="text/javascript" language="javascript">var aDolLang = {' . $sReturn . '};</script>';
  1959. }
  1960. /**
  1961. * Process all added options and return them as a string.
  1962. *
  1963. * @return string with JS code.
  1964. */
  1965. function _processJsOptions()
  1966. {
  1967. $sReturn = '';
  1968. if(isset($this->aPage['js_options']) && is_array($this->aPage['js_options'])) {
  1969. foreach($this->aPage['js_options'] as $sName => $mixedValue)
  1970. $sReturn .= "'" . bx_js_string($sName) . "': '" . bx_js_string($mixedValue) . "',";
  1971. $sReturn = substr($sReturn, 0, -1);
  1972. }
  1973. return '<script type="text/javascript" language="javascript">var aDolOptions = {' . $sReturn . '};</script>';
  1974. }
  1975. /**
  1976. * Process all added images and return them as a string.
  1977. *
  1978. * @return string with JS code.
  1979. */
  1980. function _processJsImages()
  1981. {
  1982. $sReturn = '';
  1983. if(isset($this->aPage['js_images']) && is_array($this->aPage['js_images'])) {
  1984. foreach($this->aPage['js_images'] as $sKey => $sUrl)
  1985. $sReturn .= "'" . bx_js_string($sKey) . "': '" . bx_js_string($sUrl) . "',";
  1986. $sReturn = substr($sReturn, 0, -1);
  1987. }
  1988. return '<script type="text/javascript" language="javascript">var aDolImages = {' . $sReturn . '};</script>';
  1989. }
  1990. /**
  1991. * Get Gzip loader URL.
  1992. *
  1993. * @param $sType content type CSS/JS
  1994. * @param $sName file name.
  1995. * @return string with URL
  1996. */
  1997. function _getLoaderUrl($sType, $sName)
  1998. {
  1999. return BX_DOL_URL_ROOT . 'gzip_loader.php?file=' . $sName . '.' . $sType;
  2000. }
  2001. /**
  2002. *
  2003. * Functions to display pages with errors, messages and so on.
  2004. *
  2005. */
  2006. function displayAccessDenied ($sMsg = '')
  2007. {
  2008. bx_import('BxDolLanguages');
  2009. header("HTTP/1.0 403 Forbidden");
  2010. $this->displayMsg($sMsg ? $sMsg : _t('_Access denied'));
  2011. }
  2012. function displayNoData ($sMsg = '')
  2013. {
  2014. bx_import('BxDolLanguages');
  2015. header("HTTP/1.0 204 No Content");
  2016. $this->displayMsg($sMsg ? $sMsg : _t('_Empty'));
  2017. }
  2018. function displayErrorOccured ($sMsg = '')
  2019. {
  2020. bx_import('BxDolLanguages');
  2021. header("HTTP/1.0 500 Internal Server Error");
  2022. $this->displayMsg($sMsg ? $sMsg : _t('_error occured'));
  2023. }
  2024. function displayPageNotFound ($sMsg = '')
  2025. {
  2026. bx_import('BxDolLanguages');
  2027. header("HTTP/1.0 404 Not Found");
  2028. $this->displayMsg($sMsg ? $sMsg : _t('_sys_request_page_not_found_cpt'));
  2029. }
  2030. function displayMsg ($s, $bTranslate = false)
  2031. {
  2032. $sTitle = $bTranslate ? _t($s) : $s;
  2033. $oTemplate = BxDolTemplate::getInstance();
  2034. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  2035. $oTemplate->setPageHeader ($sTitle);
  2036. $oTemplate->setPageContent ('page_main_code', DesignBoxContent($sTitle, MsgBox($sTitle), BX_DB_PADDING_DEF));
  2037. $oTemplate->getPageCode();
  2038. exit;
  2039. }
  2040. /**
  2041. * * * * Static methods for work with template injections * * *
  2042. *
  2043. * Static method is used to add/replace the content of some key in the template.
  2044. * It's usefull when you don't want to modify existing template but need to add some data to existing template key.
  2045. *
  2046. * @param integer $iPageIndex - page index where injections would processed. Use 0 if you want it to be done on all the pages.
  2047. * @param string $sKey - template key.
  2048. * @param string $sValue - the data to be added.
  2049. * @return string the result of operation.
  2050. */
  2051. function processInjection($iPageIndex, $sKey, $sValue = "")
  2052. {
  2053. if($iPageIndex != 0 && isset($this->aPage['injections']['page_0'][$sKey]) && isset($this->aPage['injections']['page_' . $iPageIndex][$sKey]))
  2054. $aSelection = @array_merge($this->aPage['injections']['page_0'][$sKey], $this->aPage['injections']['page_' . $iPageIndex][$sKey]);
  2055. else if(isset($this->aPage['injections']['page_0'][$sKey]))
  2056. $aSelection = $this->aPage['injections']['page_0'][$sKey];
  2057. else if(isset($this->aPage['injections']['page_' . $iPageIndex][$sKey]))
  2058. $aSelection = $this->aPage['injections']['page_' . $iPageIndex][$sKey];
  2059. else
  2060. $aSelection = array();
  2061. if(is_array($aSelection))
  2062. foreach($aSelection as $aInjection) {
  2063. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginInjection($sRand = time().rand());
  2064. $sInjData = '';
  2065. switch($aInjection['type']) {
  2066. case 'text':
  2067. $sInjData = $aInjection['data'];
  2068. break;
  2069. case 'service':
  2070. if(BxDolService::isSerializedService($aInjection['data']))
  2071. $sInjData = BxDolService::callSerialized($aInjection['data']);
  2072. break;
  2073. }
  2074. if((int)$aInjection['replace'] == 1)
  2075. $sValue = $sInjData;
  2076. else
  2077. $sValue .= $sInjData;
  2078. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endInjection($sRand, $aInjection);
  2079. }
  2080. return $sValue != '__' . $sKey . '__' ? str_replace('__' . $sKey . '__', '', $sValue) : $sValue;
  2081. }
  2082. /**
  2083. * Static method to add ingection available on the current page only.
  2084. *
  2085. * @param string $sKey - template's key.
  2086. * @param string $sType - injection type(text, php).
  2087. * @param string $sData - the data to be added.
  2088. * @param integer $iReplace - replace already existed data or not.
  2089. */
  2090. function addInjection($sKey, $sType, $sData, $iReplace = 0)
  2091. {
  2092. $this->aPage['injections']['page_0'][$sKey][] = array(
  2093. 'page_index' => 0,
  2094. 'key' => $sKey,
  2095. 'type' => $sType,
  2096. 'data' => $sData,
  2097. 'replace' => $iReplace
  2098. );
  2099. }
  2100. function getPageCode($oTemplate = null)
  2101. {
  2102. if (empty($oTemplate))
  2103. $oTemplate = $this;
  2104. header( 'Content-type: text/html; charset=utf-8' );
  2105. header( 'X-Frame-Options: sameorigin' );
  2106. echo $oTemplate->parsePageByName('page_' . $oTemplate->getPageNameIndex() . '.html', $oTemplate->getPageContent());
  2107. }
  2108. }
  2109. /** @} */