BxDolTemplate.php 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. ini_set('pcre.backtrack_limit', 1000000);
  10. define('BX_DOL_TEMPLATE_INJECTIONS_CACHE', 'sys_injections.inc');
  11. define('BX_DOL_TEMPLATE_CHECK_IN_BOTH', 'both');
  12. define('BX_DOL_TEMPLATE_CHECK_IN_BASE', 'base');
  13. define('BX_DOL_TEMPLATE_CHECK_IN_TMPL', 'tmpl');
  14. define('BX_DOL_COLOR_BG', 'bg');
  15. define('BX_DOL_COLOR_FT', 'ft');
  16. define('BX_DOL_PAGE_WIDTH', '1024px');
  17. /**
  18. * Page display levels.
  19. * Note. Both levels may refer to the same HTML templates.
  20. *
  21. * 'System' level. It uses page's NameIndex and
  22. * is mainly used for pages which aren't registered
  23. * in 'sys_objects_page' table. Also it's used
  24. * in 'Injections' engine.
  25. */
  26. define('BX_PAGE_DEFAULT', 0); ///< default, regular page
  27. define('BX_PAGE_CLEAR', 2); ///< clear page, without any headers and footers
  28. define('BX_PAGE_EMBED', 22); ///< page used for embeds
  29. define('BX_PAGE_POPUP', 44); ///< popup page, without any headers and footers
  30. define('BX_PAGE_TRANSITION', 150); ///< transition page with redirect to display some msg, like 'please wait', without headers footers
  31. /**
  32. * 'Builder based' level. It uses page's Type
  33. * from BxDolPage class and is used for pages
  34. * which are registered in 'sys_objects_page' table.
  35. * Changeable in Studio -> Pages Builder -> Settings.
  36. */
  37. define('BX_PAGE_TYPE_DEFAULT', 1); ///< default, regular page
  38. define('BX_PAGE_TYPE_DEFAULT_WO_HF', 2); ///< clear page, without any headers and footers
  39. /**
  40. * Template engine.
  41. *
  42. * An object of the class allows to:
  43. * 1. Manage HTML templates.
  44. * 2. Get URL/path for any template image/icon.
  45. * 3. Attach CSS/JavaScript files to the output.
  46. * 4. Add some content to any template key using Injection engine.
  47. *
  48. *
  49. * Avalable constructions.
  50. * 1. &lt;bx_include_auto:template_name.html /&gt; - the content of the file would be inserted. File would be taken from current template if it existes there, and from base directory otherwise.
  51. * 2. &lt;bx_include_base:template_name.html /&gt; - the content of the file would be inserted. File would be taken from base directory.
  52. * 3. &lt;bx_include_tmpl:template_name.html /&gt; - the content of the file would be inserted. File would be taken from tmpl_xxx directory.
  53. * 4. &lt;bx_url_root /&gt; - the value of BX_DOL_URL_ROOT variable will be inserted.
  54. * 5. &lt;bx_url_admin /&gt; - the value of BX_DOL_URL_ADMIN variable will be inserted.
  55. * 6. &lt;bx_text:_language_key /&gt; - _language_key will be translated using language file(function _t()) and inserted.
  56. &lt;bx_text_js:_language_key /&gt; - _language_key will be translated using language file(function _t()) and inserted, use it to insert text into js string.
  57. &lt;bx_text_attribute:_language_key /&gt; - _language_key will be translated using language file(function _t()) and inserted, use it to insert text into html attribute.
  58. * 7. &lt;bx_image_url:image_file_name /&gt; - image with 'image_file_name' file name will be searched in the images folder of current template.
  59. * 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.
  60. * 8. &lt;bx_icon_url:icon_file_name /&gt; - the same with &lt;bx_image_url:image_file_name /&gt;, but icons will be searched in the images/icons/ folders.
  61. * 9. &lt;bx_injection:injection_name /&gt; - will be replaced with injections registered with the page and injection_name in the `sys_injections`/`sys_injections_admin`/ tables.
  62. * 10. &lt;bx_if:tag_name&gt;some_HTML&lt;/bx_if:tag_name&gt; - will be replaced with provided content if the condition is true, and with empty string otherwise.
  63. * 11. &lt;bx_repeat:cycle_name&gt;some_HTML&lt;/bx_repeat:cycle_name&gt; - an inner HTML content will be repeated in accordance with received data.
  64. *
  65. *
  66. * Related classes:
  67. * BxDolTemplateAdmin - for processing admin templates.
  68. * Template classes in modules - for processing modiles' templates.
  69. *
  70. *
  71. * Global variables:
  72. * oSysTemplate - is used for template processing in user part.
  73. * oAdmTemplate - is used for template processing in admin part.
  74. *
  75. *
  76. * Add injection:
  77. * 1. Register it in the `sys_injections` table or `sys_injections_admin` table for admin panel.
  78. * 2. Clear injections cache(sys_injections.inc and sys_injections_admin.inc in cache folder).
  79. *
  80. *
  81. * Predefined template keys to add injections:
  82. * 1. injection_head - add injections in the &lt;head&gt; tag.
  83. * 2. injection_body - add ingection(attribute) in the &lt;body&gt; tag.
  84. * 3. injection_header - add injection inside the &lt;body&gt; tag at the very beginning.
  85. * 4. injection_logo_before - add injection at the left of the main logo(inside logo's DIV).
  86. * 5. injection_logo_after - add injection at the right of the main logo(inside logo's DIV).
  87. * 6. injection_between_logo_top_menu - add injection between logo and top menu.
  88. * 7. injection_top_menu_before - add injection at the left of the top menu(inside top menu's DIV).
  89. * 8. injection_top_menu_after - add injection at the right of the top menu(inside top menu's DIV).
  90. * 13. injection_content_before - add injection just before main content(inside content's DIV).
  91. * 14. injection_content_after - add injection just after main content(inside content's DIV).
  92. * 15. injection_between_content_footer - add injection between content and footer.
  93. * 16. injection_footer_before - add injection at the left of the footer(inside footer's DIV).
  94. * 17. injection_footer_after - add injection at the right of the footer(inside footer's DIV).
  95. * 18. injection_footer - add injection inside the &lt;body&gt; tag at the very end.
  96. *
  97. *
  98. * Example of usage:
  99. * @code
  100. * $oSysTemplate = BxDolTemplate::getInstance();
  101. *
  102. * $oSysTemplate->addCss(array('test1.css', 'test2.css'));
  103. * $oSysTemplate->addJs(array('test1.js', 'test2.js'));
  104. * $oSysTemplate->parseHtmlByName('messageBox.html', array(
  105. * 'id' => $iId,
  106. * 'msgText' => $sText,
  107. * 'bx_if:timer' => array(
  108. * 'condition' => $iTimer > 0,
  109. * 'content' => array(
  110. * 'id' => $iId,
  111. * 'time' => 1000 * $iTimer,
  112. * 'on_close' => $sOnClose,
  113. * )
  114. * ),
  115. * 'bx_if:timer' => array(
  116. * array(
  117. * 'name' => $sName,
  118. * 'title' => $sTitle
  119. * ),
  120. * array(
  121. * 'name' => $sName,
  122. * 'title' => $sTitle
  123. * )
  124. * )
  125. * ));
  126. * @endcode
  127. *
  128. *
  129. * Memberships/ACL:
  130. * Doesn't depend on user's membership.
  131. *
  132. *
  133. * Alerts:
  134. * no alerts available
  135. *
  136. */
  137. class BxDolTemplate extends BxDolFactory implements iBxDolSingleton
  138. {
  139. protected static $_sColorClassPrefix = 'col-';
  140. protected static $_sColorClassPrefixBg = 'bg-col-';
  141. protected static $_aColors = array(
  142. 'red1' => array(216, 9, 96),
  143. 'red1-dark' => array(194, 7, 86),
  144. 'red2' => array(231, 68, 30),
  145. 'red2-dark' => array(207, 60, 25),
  146. 'red3' => array(243, 143, 0),
  147. 'red3-dark' => array(218, 128, 0),
  148. 'green1' => array(96, 174, 0),
  149. 'green1-dark' => array(86, 156, 0),
  150. 'green2' => array(209, 211, 0),
  151. 'green2-dark' => array(186, 188, 0),
  152. 'green3' => array(48, 116, 36),
  153. 'green3-dark' => array(43, 104, 32),
  154. 'blue1' => array(10, 61, 143),
  155. 'blue1-dark' => array(9, 54, 128),
  156. 'blue2' => array(0, 164, 165),
  157. 'blue2-dark' => array(0, 146, 148),
  158. 'blue3' => array(0, 160, 206),
  159. 'blue3-dark' => array(0, 143, 184),
  160. 'gray' => array(97, 97, 97),
  161. 'gray-dark' => array(87, 87, 87)
  162. );
  163. /**
  164. * Main fields
  165. */
  166. protected $_sName;
  167. protected $_sPrefix;
  168. protected $_sRootPath;
  169. protected $_sRootUrl;
  170. protected $_sSubPath;
  171. protected $_sInjectionsTable;
  172. protected $_sInjectionsCache;
  173. protected $_sCode;
  174. protected $_sCodeKey;
  175. protected $_iMix;
  176. protected $_sMixKey;
  177. protected $_sKeyWrapperHtml;
  178. protected $_sFolderHtml;
  179. protected $_sFolderCss;
  180. protected $_sFolderImages;
  181. protected $_sFolderIcons;
  182. protected $_aTemplates;
  183. protected $_aLocations;
  184. protected $_aLocationsJs;
  185. /**
  186. * Cache related fields
  187. */
  188. protected $_bCacheEnable;
  189. protected $_sCacheFolderUrl;
  190. protected $_sCachePublicFolderUrl;
  191. protected $_sCachePublicFolderPath;
  192. protected $_sCacheFilePrefix;
  193. protected $_aCacheExceptions;
  194. protected $_bImagesInline;
  195. protected $_iImagesMaxSize;
  196. protected $_bCssLess;
  197. protected $_bCssCache;
  198. protected $_bCssMinify;
  199. protected $_bCssArchive;
  200. protected $_sCssLessPrefix;
  201. protected $_sCssCachePrefix;
  202. protected $_bJsLess;
  203. protected $_bJsCache;
  204. protected $_bJsMinify;
  205. protected $_bJsArchive;
  206. protected $_sJsCachePrefix;
  207. protected $aPage;
  208. protected $aPageContent;
  209. protected $aPageSnapshot = array();
  210. protected $_oTemplateConfig;
  211. protected $_oTemplateFunctions;
  212. /**
  213. * Constructor
  214. */
  215. protected function __construct($sRootPath = BX_DIRECTORY_PATH_ROOT, $sRootUrl = BX_DOL_URL_ROOT)
  216. {
  217. if(isset($GLOBALS['bxDolClasses'][get_class($this)]))
  218. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  219. parent::__construct();
  220. $this->_sPrefix = 'BxDolTemplate';
  221. $this->_sRootPath = $sRootPath;
  222. $this->_sRootUrl = $sRootUrl;
  223. $this->_sInjectionsTable = 'sys_injections';
  224. $this->_sInjectionsCache = BX_DOL_TEMPLATE_INJECTIONS_CACHE;
  225. $this->_sCodeKey = BX_DOL_TEMPLATE_CODE_KEY;
  226. $this->_sMixKey = BX_DOL_TEMPLATE_MIX_KEY;
  227. list(
  228. $this->_sCode,
  229. $this->_sName,
  230. $this->_sSubPath
  231. ) = self::retrieveCode($this->_sCodeKey, $this->_sMixKey, $this->_sRootPath);
  232. $this->_iMix = 0;
  233. if(is_array($this->_sCode))
  234. list($this->_sCode, $this->_iMix) = $this->_sCode;
  235. if(!$this->_sSubPath)
  236. $this->_sSubPath = 'boonex/' . BX_DOL_TEMPLATE_DEFAULT_CODE . '/';
  237. if(!file_exists(BX_DIRECTORY_PATH_MODULES . $this->_sSubPath)) // just for 8.0.0-A6 upgrade
  238. $this->_sSubPath = 'boonex/uni/';
  239. if(isset($_GET[$this->_sCodeKey])) {
  240. if(BxDolPermalinks::getInstance()->redirectIfNecessary(array($this->_sCodeKey)))
  241. exit;
  242. }
  243. $this->_sKeyWrapperHtml = '__';
  244. $this->_sFolderHtml = '';
  245. $this->_sFolderCss = 'css/';
  246. $this->_sFolderImages = 'images/';
  247. $this->_sFolderIcons = 'images/icons/';
  248. $this->_aTemplates = array('html_tags', 'menu_item_addon', 'menu_item_addon_small', 'menu_item_addon_middle');
  249. $this->addLocation('system', $this->_sRootPath, $this->_sRootUrl);
  250. $this->addLocationJs('system_inc_js', BX_DIRECTORY_PATH_INC . 'js/' , BX_DOL_URL_ROOT . 'inc/js/');
  251. $this->addLocationJs('system_inc_js_classes', BX_DIRECTORY_PATH_INC . 'js/classes/' , BX_DOL_URL_ROOT . 'inc/js/classes/');
  252. $this->addLocationJs('system_plugins_public', BX_DIRECTORY_PATH_PLUGINS_PUBLIC, BX_DOL_URL_PLUGINS_PUBLIC);
  253. $this->_bCacheEnable = !defined('BX_DOL_CRON_EXECUTE') && getParam('sys_template_cache_enable') == 'on';
  254. $this->_sCacheFolderUrl = '';
  255. $this->_sCachePublicFolderUrl = BX_DOL_URL_CACHE_PUBLIC;
  256. $this->_sCachePublicFolderPath = BX_DIRECTORY_PATH_CACHE_PUBLIC;
  257. $this->_sCacheFilePrefix = "bx_templ_";
  258. $this->_aCacheExceptions = ['menu_icon.html'];
  259. $this->_bImagesInline = getParam('sys_template_cache_image_enable') == 'on';
  260. $this->_iImagesMaxSize = (int)getParam('sys_template_cache_image_max_size') * 1024;
  261. $bArchive = getParam('sys_template_cache_compress_enable') == 'on';
  262. $this->_bCssLess = true; //--- Less cannot be disabled for CSS.
  263. $this->_bCssCache = !defined('BX_DOL_CRON_EXECUTE') && getParam('sys_template_cache_css_enable') == 'on';
  264. $this->_bCssMinify = $this->_bCssCache && getParam('sys_template_cache_minify_css_enable') == 'on';
  265. $this->_bCssArchive = $this->_bCssCache && $bArchive;
  266. $this->_sCssLessPrefix = $this->_sCacheFilePrefix . 'less_';
  267. $this->_sCssCachePrefix = $this->_sCacheFilePrefix . 'css_';
  268. $this->_bJsLess = false; //--- Less language isn't available for JS at all.
  269. $this->_bJsCache = !defined('BX_DOL_CRON_EXECUTE') && getParam('sys_template_cache_js_enable') == 'on';
  270. $this->_bJsMinify = $this->_bJsCache && getParam('sys_template_cache_minify_js_enable') == 'on';
  271. $this->_bJsArchive = $this->_bJsCache && $bArchive;
  272. $this->_sJsCachePrefix = $this->_sCacheFilePrefix . 'js_';
  273. $this->aPage = array();
  274. $this->aPageContent = array();
  275. }
  276. /**
  277. * Prevent cloning the instance
  278. */
  279. public function __clone()
  280. {
  281. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  282. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  283. }
  284. /**
  285. * Get singleton instance of the class
  286. */
  287. public static function getInstance()
  288. {
  289. if(!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  290. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolTemplate();
  291. $GLOBALS['bxDolClasses'][__CLASS__]->init();
  292. }
  293. return $GLOBALS['bxDolClasses'][__CLASS__];
  294. }
  295. /**
  296. * Retrieve template code and check whether it's associated with active template or not.
  297. *
  298. * @param string $sCodeKey template's code key.
  299. * @param string $sMixKey template's mix key.
  300. * @param string $sRootPath path to root directory.
  301. */
  302. public static function retrieveCode($sCodeKey = BX_DOL_TEMPLATE_CODE_KEY, $sMixKey = BX_DOL_TEMPLATE_MIX_KEY, $sRootPath = BX_DIRECTORY_PATH_ROOT)
  303. {
  304. $oDb = BxDolDb::getInstance();
  305. $fCheckCode = function($sCode, $bSetCookie) use($sCodeKey, $sRootPath) {
  306. if(empty($sCode) || !preg_match('/^[A-Za-z0-9_-]+$/', $sCode))
  307. return false;
  308. $aModule = BxDolModuleQuery::getInstance()->getModuleByUri($sCode);
  309. if(empty($aModule) || !is_array($aModule) || (int)$aModule['enabled'] != 1 || !file_exists($sRootPath . 'modules/' . $aModule['path'] . 'data/template/'))
  310. return false;
  311. $oConfig = new BxDolModuleConfig($aModule);
  312. $aResult = array(
  313. $oConfig->getUri(), //--- Template module's URI is used as template Code.
  314. $oConfig->getName(),
  315. $oConfig->getDirectory()
  316. );
  317. if(!$bSetCookie || bx_get('preview'))
  318. return $aResult;
  319. $aUrl = parse_url(BX_DOL_URL_ROOT);
  320. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  321. setcookie($sCodeKey, $sCode, time() + 60*60*24*365, $sPath);
  322. return $aResult;
  323. };
  324. $fCheckMix = function($aResult, $iMix, $bSetCookie) use($sMixKey, $sRootPath, $oDb) {
  325. list($sCode, $sName) = $aResult;
  326. if(empty($sName) || empty($iMix))
  327. return false;
  328. $aMix = $oDb->getParamsMix($iMix);
  329. if(empty($aMix) || !is_array($aMix) || $aMix['type'] != $sName)
  330. return false;
  331. if(!$bSetCookie)
  332. return $iMix;
  333. $aUrl = parse_url(BX_DOL_URL_ROOT);
  334. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  335. setcookie($sMixKey, $iMix, time() + 60*60*24*365, $sPath);
  336. return $iMix;
  337. };
  338. $sCode = getParam('template');
  339. if(empty($sCode))
  340. $sCode = BX_DOL_TEMPLATE_DEFAULT_CODE;
  341. $aResult = $fCheckCode($sCode, false);
  342. //--- Check selected template in COOKIE(the lowest priority) ---//
  343. $sCode = !empty($_COOKIE[$sCodeKey]) ? $_COOKIE[$sCodeKey] : '';
  344. $aResultCheck = $fCheckCode($sCode, false);
  345. if($aResultCheck !== false)
  346. $aResult = $aResultCheck;
  347. //--- Check selected template in GET(the highest priority) ---//
  348. $sCode = !empty($_GET[$sCodeKey]) ? $_GET[$sCodeKey] : '';
  349. $aResultCheck = $fCheckCode($sCode, true);
  350. if($aResultCheck !== false)
  351. $aResult = $aResultCheck;
  352. if($aResult === false)
  353. return $aResult;
  354. if(!is_array($aResult[0]))
  355. $aResult[0] = array($aResult[0]);
  356. $iMixDefault = !empty($aResult[1]) ? (int)getParam($aResult[1] . '_default_mix') : 0;
  357. //--- Check selected mix in COOKIE(the lowest priority) ---//
  358. $iMix = !empty($_COOKIE[$sMixKey]) ? (int)$_COOKIE[$sMixKey] : 0;
  359. $iResultCheck = $fCheckMix($aResult, $iMix, false);
  360. if($iResultCheck !== false) {
  361. $aMix = $oDb->getParamsMix($iMix);
  362. if((int)$aMix['published'] == 0 && $iMix != $iMixDefault) {
  363. $aUrl = parse_url(BX_DOL_URL_ROOT);
  364. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  365. setcookie($sMixKey, '', time() - 96 * 3600, $sPath);
  366. unset($_COOKIE[$sMixKey]);
  367. }
  368. else
  369. $aResult[0][1] = $iResultCheck;
  370. }
  371. //--- Check selected mix in GET(the highest priority) ---//
  372. $iMix = !empty($_GET[$sMixKey]) ? (int)$_GET[$sMixKey] : 0;
  373. $iResultCheck = $fCheckMix($aResult, $iMix, true);
  374. if($iResultCheck !== false)
  375. $aResult[0][1] = $iResultCheck;
  376. //--- Get default mix for currently selected template ---//
  377. if(empty($aResult[0][1]) && !empty($iMixDefault)) {
  378. $iResultCheck = $fCheckMix($aResult, $iMixDefault, false);
  379. if($iResultCheck !== false)
  380. $aResult[0][1] = $iResultCheck;
  381. }
  382. if(is_array($aResult[0]) && count($aResult[0]) == 1)
  383. $aResult[0] = $aResult[0][0];
  384. return $aResult;
  385. }
  386. public function getIncludedUrls($sType)
  387. {
  388. if (!isset($this->aPage[$sType]))
  389. return array();
  390. $a = array();
  391. foreach ($this->aPage[$sType] as $r)
  392. $a[] = $r['url'];
  393. return $a;
  394. }
  395. /**
  396. * Remember current state of aPage variable with all css, js, etc
  397. */
  398. public function collectingStart()
  399. {
  400. $this->aPageSnapshot = $this->aPage;
  401. }
  402. public function collectingInject($aCss, $aJs)
  403. {
  404. $a = array('css' => 'aCss', 'js' => 'aJs');
  405. foreach ($a as $s => $sVar) {
  406. if (empty($$sVar))
  407. continue;
  408. $sKey = $s . '_compiled';
  409. foreach ($$sVar as $r)
  410. $this->aPage[$sKey][] = $r;
  411. }
  412. }
  413. /**
  414. * Get difference for non-system css and js files from previously remembered state as ready HTML code,
  415. * additionally filter out css and js from $aExcludeCss and $aExcludeJs arrays
  416. */
  417. public function collectingEndGetCode($aExcludeCss = array(), $aExcludeJs = array(), $sFormat = 'html')
  418. {
  419. $aPageSave = $this->aPage; // save current state to restore later
  420. // filter funcs
  421. $fFilterCss = function ($a) use ($aExcludeCss) {
  422. if (in_array($a['url'], $aExcludeCss))
  423. return false;
  424. if (isset($this->aPageSnapshot['css_compiled']))
  425. foreach ($this->aPageSnapshot['css_compiled'] as $r)
  426. if ($r['url'] == $a['url'])
  427. return false;
  428. return true;
  429. };
  430. $fFilterJs = function ($a) use ($aExcludeJs) {
  431. if (in_array($a['url'], $aExcludeJs))
  432. return false;
  433. if (isset($this->aPageSnapshot['js_compiled']))
  434. foreach ($this->aPageSnapshot['js_compiled'] as $r)
  435. if ($r['url'] == $a['url'])
  436. return false;
  437. return true;
  438. };
  439. // diff aPageSnapshot and aPage and output only newly added css/js
  440. $this->aPage['css_compiled'] = array_filter($this->aPage['css_compiled'], $fFilterCss);
  441. $this->aPage['js_compiled'] = array_filter($this->aPage['js_compiled'], $fFilterJs);
  442. // return js/css
  443. $mixedRet = '';
  444. if ('html' == $sFormat) {
  445. $mixedRet .= $this->includeFiles('css');
  446. $mixedRet .= $this->includeFiles('js');
  447. }
  448. else {
  449. $mixedRet = array(
  450. 'css' => $this->aPage['css_compiled'],
  451. 'js' => $this->aPage['js_compiled'],
  452. );
  453. }
  454. // restore original state
  455. $this->aPageSnapshot = array();
  456. $this->aPage = $aPageSave;
  457. return $mixedRet;
  458. }
  459. public function getClassName()
  460. {
  461. return get_class($this);
  462. }
  463. public static function getColorPalette()
  464. {
  465. $aResult = self::$_aColors;
  466. bx_alert('system', 'get_color_palette', 0, false, array(
  467. 'override_result' => &$aResult
  468. ));
  469. if($aResult != self::$_aColors) {
  470. $oTemplate = self::getInstance();
  471. foreach($aResult as $sName => $aRgb) {
  472. $sRgb = 'rgb(' . trim(implode(', ', $aRgb)) . ') !important';
  473. $oTemplate->addCssStyle('.' . self::$_sColorClassPrefix . $sName, array(
  474. 'color' => $sRgb
  475. ));
  476. $oTemplate->addCssStyle('.' . self::$_sColorClassPrefixBg . $sName, array(
  477. 'background-color' => $sRgb
  478. ));
  479. }
  480. }
  481. return $aResult;
  482. }
  483. public static function getColorCode($mixedName = false, $fOpacity = false)
  484. {
  485. $aPalette = self::getColorPalette();
  486. $aClasses = array_keys($aPalette);
  487. if($mixedName === false || (is_string($mixedName) && !is_numeric($mixedName) && !in_array($mixedName, $aClasses)))
  488. $mixedName = $aClasses[rand(0, count($aClasses) - 1)];
  489. else if(is_numeric($mixedName))
  490. $mixedName = $aClasses[(int)$mixedName % count($aClasses)];
  491. $aColor = $aPalette[$mixedName];
  492. if($fOpacity !== false && is_numeric($fOpacity))
  493. $aColor[] = $fOpacity;
  494. return $aColor;
  495. }
  496. public static function getColorClass($sType = BX_DOL_COLOR_FT, $sName = '')
  497. {
  498. $aClasses = array_keys(self::getColorPalette());
  499. if(empty($sName) || !in_array($sName, $aClasses))
  500. $sName = $aClasses[rand(0, count($aClasses) - 1)];
  501. $sPrefix = '';
  502. switch ($sType) {
  503. case BX_DOL_COLOR_FT:
  504. $sPrefix = self::$_sColorClassPrefix;
  505. break;
  506. case BX_DOL_COLOR_BG:
  507. $sPrefix = self::$_sColorClassPrefixBg;
  508. break;
  509. }
  510. return $sPrefix . $sName;
  511. }
  512. /**
  513. * Load templates.
  514. */
  515. function loadTemplates()
  516. {
  517. $aResult = array();
  518. foreach($this->_aTemplates as $sName)
  519. $aResult[$sName] = $this->getHtml($sName . '.html');
  520. $this->_aTemplates = $aResult;
  521. }
  522. /**
  523. * Initialize template engine.
  524. * Note. The method is executed with the system, you shouldn't execute it in your subclasses.
  525. */
  526. function init()
  527. {
  528. $this->loadTemplates();
  529. //--- Load page elements related static variables ---//
  530. $this->aPage = array(
  531. 'name_index' => BX_PAGE_DEFAULT,
  532. 'type' => BX_PAGE_TYPE_DEFAULT,
  533. 'header' => '',
  534. 'header_text' => '',
  535. 'keywords' => array(),
  536. 'location' => array(),
  537. 'description' => '',
  538. 'robots' => '',
  539. 'base' => ['href' => BX_DOL_URL_ROOT],
  540. 'css_name' => array(),
  541. 'css_compiled' => array(),
  542. 'css_system' => array(),
  543. 'css_async' => array(),
  544. 'js_name' => array(),
  545. 'js_compiled' => array(),
  546. 'js_system' => array(),
  547. 'js_options' => array(),
  548. 'js_translations' => array(),
  549. 'js_images' => array(),
  550. 'injections' => array()
  551. );
  552. //--- Load default CSS, JS, etc ---//
  553. BxDolPreloader::getInstance()->perform($this);
  554. //--- Load injection's cache ---//
  555. if (getParam('sys_db_cache_enable')) {
  556. $oDb = BxDolDb::getInstance();
  557. $oCache = $oDb->getDbCacheObject();
  558. $sCacheKey = $oDb->genDbCacheKey($this->_sInjectionsCache);
  559. $aInjections = $oCache->getData($sCacheKey);
  560. if ($aInjections === null) {
  561. $aInjections = $this->getInjectionsData();
  562. $oCache->setData ($sCacheKey, $aInjections);
  563. }
  564. }
  565. else
  566. $aInjections = $this->getInjectionsData();
  567. $this->aPage['injections'] = $aInjections;
  568. bx_import('BxTemplConfig'); // TODO: for some reason autoloader isn't working here....
  569. $this->_oTemplateConfig = BxTemplConfig::getInstance();
  570. bx_import('BxTemplFunctions');
  571. $this->_oTemplateFunctions = BxTemplFunctions::getInstance($this);
  572. }
  573. protected function getInjectionsData ()
  574. {
  575. $oDb = BxDolDb::getInstance();
  576. $aInjections = $oDb->getAll("SELECT `page_index`, `name`, `key`, `type`, `data`, `replace` FROM `" . $this->_sInjectionsTable . "` WHERE `active`='1'");
  577. if (!$aInjections)
  578. return array();
  579. foreach ($aInjections as $aInjection)
  580. $aInjections['page_' . $aInjection['page_index']][$aInjection['key']][] = $aInjection;
  581. return $aInjections;
  582. }
  583. /**
  584. * Set page name index
  585. * @param int $i name index
  586. */
  587. function setPageNameIndex($i)
  588. {
  589. $this->aPage['name_index'] = $i;
  590. }
  591. /**
  592. * Get page name index
  593. * @return int $i name index
  594. */
  595. function getPageNameIndex()
  596. {
  597. return isset($this->aPage['name_index']) ? (int)$this->aPage['name_index'] : 0;
  598. }
  599. /**
  600. * Set page type
  601. * @param int $i page type
  602. */
  603. function setPageType($a)
  604. {
  605. $this->aPage['type'] = $a;
  606. }
  607. /**
  608. * Get page type
  609. * @return int $i page type
  610. */
  611. function getPageType()
  612. {
  613. return isset($this->aPage['type']) ? (int)$this->aPage['type'] : BX_PAGE_TYPE_DEFAULT;
  614. }
  615. /**
  616. * Set page header
  617. * @param string $s page header
  618. */
  619. function setPageHeader($s)
  620. {
  621. $this->aPage['header'] = $s;
  622. }
  623. /**
  624. * Get page header
  625. * @return string $s page header
  626. */
  627. function getPageHeader()
  628. {
  629. return $this->aPage['header'];
  630. }
  631. /**
  632. * Set page params. Available page params are: name_index, header
  633. * @param array $a page params
  634. */
  635. function setPageParams($a)
  636. {
  637. if (!empty($this->aPage))
  638. $this->aPage = array_merge($this->aPage, $a);
  639. else
  640. $this->aPage = $a;
  641. }
  642. /**
  643. * Get page params.
  644. * @return array $a page params
  645. */
  646. function getPageParams()
  647. {
  648. return $this->aPage;
  649. }
  650. /**
  651. * Set page description.
  652. *
  653. * @param string $sDescription necessary page description.
  654. */
  655. function setPageDescription($sDescription)
  656. {
  657. $this->aPage['description'] = $sDescription;
  658. }
  659. /**
  660. * Set page meta robots.
  661. *
  662. * @param string $s page meta robots.
  663. */
  664. function setPageMetaRobots($s)
  665. {
  666. $this->aPage['robots'] = $s;
  667. }
  668. /**
  669. * Set page injections.
  670. *
  671. * @param array $aInjections name => value injections.
  672. */
  673. function setPageInjections($aInjections)
  674. {
  675. if(empty($aInjections) || !is_array($aInjections))
  676. return;
  677. foreach($aInjections as $sName => $sValue)
  678. $this->addInjection('injection_' . $sName, 'text', $sValue);
  679. }
  680. /**
  681. * Set page content for some variable.
  682. * @param string $sVar name of content variable
  683. * @param string $sContent content for $sVar variable
  684. * @param int $iIndex optional page index, default is index which was set before with @see setPageNameIndex function, or 0
  685. */
  686. function setPageContent($sVar, $sContent, $iIndex = false)
  687. {
  688. $i = false !== $iIndex ? $iIndex : $this->getPageNameIndex();
  689. $this->aPageContent[$i][$sVar] = $sContent;
  690. }
  691. /**
  692. * Get page content for some variable.
  693. * @param string $sVar name of content variable
  694. * @param int $iIndex optional page index, default is index which was set before with @see setPageNameIndex function, or 0
  695. * @return string page content for some variable or for the whole page.
  696. */
  697. function getPageContent($sVar = false, $iIndex = false)
  698. {
  699. $i = false !== $iIndex ? $iIndex : $this->getPageNameIndex();
  700. return false !== $sVar ? $this->aPageContent[$i][$sVar] : $this->aPageContent[$i];
  701. }
  702. /**
  703. * Get currently active template name.
  704. *
  705. * @return string template's name.
  706. */
  707. function getName()
  708. {
  709. return $this->_sName;
  710. }
  711. /**
  712. * Get currently active template code.
  713. *
  714. * @return string template's code.
  715. */
  716. function getCode()
  717. {
  718. return $this->_sCode;
  719. }
  720. /**
  721. * Get embed code.
  722. *
  723. * @return string embed's code.
  724. */
  725. function getEmbed($sContent)
  726. {
  727. header('Content-Security-Policy: frame-ancestors ' . getParam('sys_csp_frame_ancestors')) ;
  728. if ($sContent == ''){
  729. $this->displayPageNotFound('', BX_PAGE_EMBED);
  730. exit;
  731. }
  732. $this->addJs(['inc/js/|embed.js']);
  733. $this->addCss(['embed.css']);
  734. $this->aPage['base']['target'] = '_blank';
  735. $this->setPageNameIndex (BX_PAGE_EMBED);
  736. $this->setPageContent('page_main_code', '<div class="bx-embed">' . $sContent . '</div>');
  737. $this->getPageCode();
  738. }
  739. /**
  740. * Get code key.
  741. *
  742. * @return string template's code key.
  743. */
  744. function getCodeKey()
  745. {
  746. return $this->_sCodeKey;
  747. }
  748. /**
  749. * Get currently active template mix.
  750. *
  751. * @return integer template's mix.
  752. */
  753. function getMix()
  754. {
  755. return $this->_iMix;
  756. }
  757. /**
  758. * Get currently active template path.
  759. *
  760. * @return string template's path.
  761. */
  762. function getPath()
  763. {
  764. return $this->_sSubPath;
  765. }
  766. /**
  767. * Set page title.
  768. * @deprecated use setPageHeader
  769. *
  770. * @param string $sTitle necessary page title.
  771. */
  772. function setPageTitle($sTitle)
  773. {
  774. $this->setPageHeader($sTitle);
  775. }
  776. /**
  777. * Set page's main box title.
  778. * @deprecated use setPageParams
  779. *
  780. * @param string $sTitle necessary page's main box title.
  781. */
  782. function setPageMainBoxTitle($sTitle)
  783. {
  784. $this->setPageParams(array('header_text' => $sTitle));
  785. }
  786. /**
  787. * Check whether location exists or not.
  788. * @param string $sKey - location's unique key.
  789. */
  790. function isLocation($sKey)
  791. {
  792. return isset($this->_aLocations[$sKey]);
  793. }
  794. /**
  795. * Get a lis of all added locations.
  796. * @return array with locations.
  797. */
  798. function getLocations()
  799. {
  800. return $this->_aLocations;
  801. }
  802. /**
  803. * Add location in array of locations.
  804. * Note. Location is the path/url to folder where 'templates' folder is stored.
  805. *
  806. * @param string $sKey - location's unique key.
  807. * @param string $sLocationPath - location's path. For modules: '[path_to_script]/modules/[vendor_name]/[module_name]/'
  808. * @param string $sLocationUrl - location's url. For modules: '[url_to_script]/modules/[vendor_name]/[module_name]/'
  809. */
  810. function addLocation($sKey, $sLocationPath, $sLocationUrl)
  811. {
  812. $this->_aLocations[$sKey] = array(
  813. 'path' => $sLocationPath,
  814. 'url' => $sLocationUrl,
  815. );
  816. return $sKey;
  817. }
  818. /**
  819. * Add dynamic location.
  820. *
  821. * @param string $sLocationPath - location's path. For modules: '[path_to_script]/modules/[vendor_name]/[module_name]/'
  822. * @param string $sLocationUrl - location's url. For modules: '[url_to_script]/modules/[vendor_name]/[module_name]/'
  823. * @return location key. Is needed to remove the location.
  824. */
  825. function addDynamicLocation($sLocationPath, $sLocationUrl)
  826. {
  827. $sLocationKey = time() . mt_rand();
  828. $this->addLocation($sLocationKey, $sLocationPath, $sLocationUrl);
  829. return $sLocationKey;
  830. }
  831. /**
  832. * Remove location from array of locations.
  833. * Note. Location is the path/url to folder where templates are stored.
  834. *
  835. * @param string $sKey - location's unique key.
  836. */
  837. function removeLocation($sKey)
  838. {
  839. if(isset($this->_aLocations[$sKey]))
  840. unset($this->_aLocations[$sKey]);
  841. }
  842. /**
  843. * Check whether JS location exists or not.
  844. * @param string $sKey - JS location's unique key.
  845. */
  846. function isLocationJs($sKey)
  847. {
  848. return isset($this->_aLocationsJs[$sKey]);
  849. }
  850. /**
  851. * Add JS location in array of JS locations.
  852. * Note. Location is the path/url to folder where JS files are stored.
  853. *
  854. * @param string $sKey - location's unique key.
  855. * @param string $sLocationPath - location's path. For modules: '[path_to_script]/modules/[vendor_name]/[module_name]/js/'
  856. * @param string $sLocationUrl - location's url. For modules: '[url_to_script]/modules/[vendor_name]/[module_name]/js/'
  857. */
  858. function addLocationJs($sKey, $sLocationPath, $sLocationUrl)
  859. {
  860. $this->_aLocationsJs[$sKey] = array(
  861. 'path' => $sLocationPath,
  862. 'url' => $sLocationUrl
  863. );
  864. return $sKey;
  865. }
  866. /**
  867. * Add dynamic JS location.
  868. *
  869. * @param string $sLocationPath - location's path. For modules: '[path_to_script]/modules/[vendor_name]/[module_name]/'
  870. * @param string $sLocationUrl - location's url. For modules: '[url_to_script]/modules/[vendor_name]/[module_name]/'
  871. * @return location key. Is needed to remove the location.
  872. */
  873. function addDynamicLocationJs($sLocationPath, $sLocationUrl)
  874. {
  875. $sLocationKey = time() . mt_rand();
  876. $this->addLocationJs($sLocationKey, $sLocationPath, $sLocationUrl);
  877. return $sLocationKey;
  878. }
  879. /**
  880. * Remove JS location from array of locations.
  881. * Note. Location is the path/url to folder where templates are stored.
  882. *
  883. * @param string $sKey - JS location's unique key.
  884. */
  885. function removeLocationJs($sKey)
  886. {
  887. if(isset($this->_aLocationsJs[$sKey]))
  888. unset($this->_aLocationsJs[$sKey]);
  889. }
  890. /**
  891. * Add Option in JS output.
  892. *
  893. * @param mixed $mixedName option's name or an array of options' names.
  894. */
  895. function addJsOption($mixedName)
  896. {
  897. if(is_string($mixedName))
  898. $mixedName = array($mixedName);
  899. foreach($mixedName as $sName)
  900. $this->aPage['js_options'][$sName] = getParam($sName);
  901. }
  902. /**
  903. * Add language translation for key in JS output.
  904. *
  905. * @param mixed $mixedKey language key or an array of keys.
  906. */
  907. function addJsTranslation($mixedKey, $bDynamic = false)
  908. {
  909. if(is_string($mixedKey))
  910. $mixedKey = array($mixedKey);
  911. foreach($mixedKey as $sKey)
  912. $this->aPage['js_translations'][$sKey] = _t($sKey, '{0}', '{1}');
  913. return $bDynamic ? $this->_processJsTranslations() : '';
  914. }
  915. /**
  916. * Add image in JS output.
  917. *
  918. * @param array $aImages an array of image descriptors.
  919. * The descriptor is a key/value pear in the array of descriptors.
  920. */
  921. function addJsImage($aImages)
  922. {
  923. if(!is_array($aImages))
  924. return;
  925. foreach($aImages as $sKey => $sFile) {
  926. $sUrl = $this->getImageUrl($sFile);
  927. if(empty($sUrl))
  928. continue;
  929. $this->aPage['js_images'][$sKey] = $sUrl;
  930. }
  931. }
  932. /**
  933. * Add icon in JS output.
  934. *
  935. * @param array $aIcons an array of icons descriptors.
  936. * The descriptor is a key/value pear in the array of descriptors.
  937. */
  938. function addJsIcon($aIcons)
  939. {
  940. if(!is_array($aIcons))
  941. return;
  942. foreach($aIcons as $sKey => $sFile) {
  943. $sUrl = $this->getIconUrl($sFile);
  944. if(empty($sUrl))
  945. continue;
  946. $this->aPage['js_images'][$sKey] = $sUrl;
  947. }
  948. }
  949. /**
  950. * Add CSS style.
  951. *
  952. * @param string $sName CSS class name.
  953. * @param string $sContent CSS class styles.
  954. */
  955. function addCssStyle($sName, $sContent)
  956. {
  957. $this->aPage['css_styles'][$sName] = $sContent;
  958. }
  959. /**
  960. * Set page keywords.
  961. *
  962. * @param mixed $mixedKeywords necessary page keywords(string - single keyword, array - an array of keywords).
  963. * @param string $sDevider - string devider.
  964. */
  965. function addPageKeywords($mixedKeywords, $sDevider = ',')
  966. {
  967. if(is_string($mixedKeywords))
  968. $mixedKeywords = strpos($mixedKeywords, $sDevider) !== false ? explode($sDevider, $mixedKeywords) : array($mixedKeywords);
  969. foreach($mixedKeywords as $iKey => $sValue)
  970. $mixedKeywords[$iKey] = trim($sValue);
  971. $this->aPage['keywords'] = isset($this->aPage['keywords']) && is_array($this->aPage['keywords']) ? array_merge($this->aPage['keywords'], $mixedKeywords) : $mixedKeywords;
  972. }
  973. /**
  974. * Set page locatoin coordinates.
  975. *
  976. * @param $fLat latitude
  977. * @param $fLng longitude
  978. */
  979. function addPageMetaLocation($fLat, $fLng, $sCountryCode)
  980. {
  981. $this->aPage['location'] = array('lat' => $fLat, 'lng' => $fLng, 'country' => $sCountryCode);
  982. }
  983. /**
  984. * Set page meta image.
  985. *
  986. * @param $sImageUrl meta image url
  987. */
  988. function addPageMetaImage($sImageUrl)
  989. {
  990. $this->aPage['image'] = $sImageUrl;
  991. }
  992. /**
  993. * Set page rss link.
  994. *
  995. * @param $sTitle - rss feed title
  996. * @param $sUrl - rss feed URL
  997. */
  998. function addPageRssLink($sTitle, $sUrl)
  999. {
  1000. if (!isset($this->aPage['rss']))
  1001. $this->aPage['rss'] = array('title' => $sTitle, 'url' => $sUrl);
  1002. else
  1003. $this->aPage['rss'] = false;
  1004. }
  1005. /**
  1006. * Returns page meta info, like meta keyword, meta description, location, etc
  1007. */
  1008. function getMetaInfo()
  1009. {
  1010. $sRet = '';
  1011. // general meta tags
  1012. if (!empty($this->aPage['keywords']) && is_array($this->aPage['keywords']))
  1013. $sRet .= '<meta name="keywords" content="' . bx_html_attribute(implode(',', $this->aPage['keywords'])) . '" />';
  1014. $bDescription = !empty($this->aPage['description']) && is_string($this->aPage['description']);
  1015. if ($bDescription)
  1016. $sRet .= '<meta name="description" content="' . bx_html_attribute($this->aPage['description']) . '" />';
  1017. // location
  1018. if (!empty($this->aPage['location']) && isset($this->aPage['location']['lat']) && isset($this->aPage['location']['lng']) && isset($this->aPage['location']['country']))
  1019. $sRet .= '
  1020. <meta name="ICBM" content="' . $this->aPage['location']['lat'] . ';' . $this->aPage['location']['lng'] . '" />
  1021. <meta name="geo.position" content="' . $this->aPage['location']['lat'] . ';' . $this->aPage['location']['lng'] . '" />
  1022. <meta name="geo.region" content="' . bx_html_attribute($this->aPage['location']['country']) . '" />';
  1023. // set cover image as meta[image] value
  1024. if (empty($this->aPage['image']) && ($oPage = BxDolPage::getObjectInstanceByURI()) !== false){
  1025. $aCover = $oPage->getPageCoverImage();
  1026. if ($aCover){
  1027. $oCover = BxDolCover::getInstance($this);
  1028. $this->aPage['image'] = $oCover->getCoverImageUrl($aCover);
  1029. }
  1030. }
  1031. // facebook / twitter
  1032. $bPageImage = !empty($this->aPage['image']);
  1033. $sRet .= '<meta name="twitter:card" content="' . ($bPageImage ? 'summary_large_image' : 'summary') . '" />';
  1034. if ($bPageImage)
  1035. $sRet .= '<meta property="og:image" content="' . $this->aPage['image'] . '" />';
  1036. $sRet .= '
  1037. <meta property="og:title" content="' . (isset($this->aPage['header']) ? bx_html_attribute(strip_tags($this->aPage['header'])) : '') . '" />
  1038. <meta property="og:description" content="' . ($bDescription ? bx_html_attribute($this->aPage['description']) : '') . '" />';
  1039. // Smart App Banner
  1040. if (getParam('smart_app_banner') && false === strpos($_SERVER['HTTP_USER_AGENT'], 'UNAMobileApp')) {
  1041. if ($sAppIdIOS = getParam('smart_app_banner_ios_app_id'))
  1042. $sRet .= '<meta name="apple-itunes-app" content="app-id=' . $sAppIdIOS . '" />';
  1043. }
  1044. // RSS
  1045. $oFunctions = BxTemplFunctions::getInstance();
  1046. $sRet .= $oFunctions->getManifests();
  1047. $sRet .= $oFunctions->getMetaIcons();
  1048. if (!empty($this->aPage['rss']) && !empty($this->aPage['rss']['url']))
  1049. $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'] . '" />';
  1050. $sRet .= "<link rel=\"alternate\" type=\"application/json+oembed\" href=\"" . BX_DOL_URL_ROOT ."em.php?url=" . urlencode($_SERVER["REQUEST_URI"]) . "&format=json\" title=\"". (isset($this->aPage['header']) ? bx_html_attribute(strip_tags($this->aPage['header'])) : '') . "\" />";
  1051. $sRet .= "<link rel=\"canonical\" href=\"" . BX_DOL_URL_ROOT . substr($_SERVER["REQUEST_URI"], 1) . "\" />";
  1052. return $sRet;
  1053. }
  1054. /**
  1055. * Get template, which was loaded earlier.
  1056. * @see method this->loadTemplates and field this->_aTemplates
  1057. *
  1058. * @param string $sName - template name.
  1059. * @return string template's content.
  1060. */
  1061. public function getTemplate($sName)
  1062. {
  1063. return $this->_aTemplates[$sName];
  1064. }
  1065. /**
  1066. * Get template functions object.
  1067. * @see BxBaseFunctions
  1068. */
  1069. public function getTemplateFunctions()
  1070. {
  1071. return $this->_oTemplateFunctions;
  1072. }
  1073. /**
  1074. * Get image MIME type.
  1075. *
  1076. * @param string $sExtension - image file's extension.
  1077. * @return string with MIME type.
  1078. */
  1079. function getImageMimeType($sExtension)
  1080. {
  1081. $sExtension = strtolower($sExtension);
  1082. $sResult = '';
  1083. switch($sExtension) {
  1084. case 'svg':
  1085. $sResult = 'svg+xml';
  1086. break;
  1087. default:
  1088. $sResult = $sExtension;
  1089. }
  1090. return 'data:image/' . $sResult;
  1091. }
  1092. /**
  1093. * Get icon template in dependence of a value, provided in $mixedId.
  1094. *
  1095. * @param mixed $mixedId numeric id from Storage, string with template's file name or string with font icon.
  1096. */
  1097. public function getIcon($mixedId, $aParams = array())
  1098. {
  1099. return $this->_getImage('icon', $mixedId, $aParams);
  1100. }
  1101. /**
  1102. * Get image template in dependence of a value, provided in $mixedId.
  1103. *
  1104. * @param mixed $mixedId numeric id from Storage, string with template's file name or string with font icon.
  1105. */
  1106. public function getImage($mixedId, $aParams = array())
  1107. {
  1108. return $this->_getImage('image', $mixedId, $aParams);
  1109. }
  1110. protected function _getImage($sType, $mixedId, $aParams = array())
  1111. {
  1112. $sUrl = "";
  1113. $aType2Method = array('image' => 'getImageUrl', 'icon' => 'getIconUrl');
  1114. //--- Check in System Storage.
  1115. if(is_numeric($mixedId) && (int)$mixedId > 0) {
  1116. $sStorage = BX_DOL_STORAGE_OBJ_IMAGES;
  1117. if(!empty($aParams['storage'])) {
  1118. $sStorage = $aParams['storage'];
  1119. unset($aParams['storage']);
  1120. }
  1121. if(($sResult = BxDolStorage::getObjectInstance($sStorage)->getFileUrlById((int)$mixedId)) !== false)
  1122. $sUrl = $sResult;
  1123. }
  1124. //--- Check in template folders.
  1125. if($sUrl == "" && is_string($mixedId) && strpos($mixedId, '.') !== false)
  1126. $sUrl = $this->{$aType2Method[$sType]}($mixedId);
  1127. if($sUrl != "")
  1128. return $this->parseImage($sUrl, array(
  1129. 'class' => isset($aParams['class']) && !empty($aParams['class']) ? $aParams['class'] : '',
  1130. 'alt' => isset($aParams['alt']) && !empty($aParams['alt']) ? $aParams['alt'] : ''
  1131. ));
  1132. //--- Use iconic font.
  1133. return $this->parseIcon($mixedId, $aParams);
  1134. }
  1135. /**
  1136. * Get full URL for the icon.
  1137. *
  1138. * @param string $sName icon's file name.
  1139. * @param string $sCheckIn where the content would be searched(base, template, both)
  1140. * @return string full URL.
  1141. */
  1142. function getIconUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1143. {
  1144. $sContent = "";
  1145. if(($sContent = $this->_getInlineData('icon', $sName, $sCheckIn)) !== false)
  1146. return $sContent;
  1147. return $this->_getAbsoluteLocation('url', $this->_sFolderIcons, $sName, $sCheckIn);
  1148. }
  1149. /**
  1150. * Get absolute Path for the icon.
  1151. *
  1152. * @param string $sName - icon's file name.
  1153. * @param string $sCheckIn where the content would be searched(base, template, both)
  1154. * @return string absolute path.
  1155. */
  1156. function getIconPath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1157. {
  1158. return $this->_getAbsoluteLocation('path', $this->_sFolderIcons, $sName, $sCheckIn);
  1159. }
  1160. /**
  1161. * Get full URL for the image.
  1162. *
  1163. * @param string $sName - images's file name.
  1164. * @param string $sCheckIn where the content would be searched(base, template, both)
  1165. * @return string full URL.
  1166. */
  1167. function getImageUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1168. {
  1169. $sContent = "";
  1170. if(($sContent = $this->_getInlineData('image', $sName, $sCheckIn)) !== false)
  1171. return $sContent;
  1172. return $this->_getAbsoluteLocation('url', $this->_sFolderImages, $sName, $sCheckIn);
  1173. }
  1174. /**
  1175. * Get absolute Path for the image.
  1176. *
  1177. * @param string $sName - image's file name.
  1178. * @param string $sCheckIn where the content would be searched(base, template, both)
  1179. * @return string absolute path.
  1180. */
  1181. function getImagePath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1182. {
  1183. return $this->_getAbsoluteLocation('path', $this->_sFolderImages, $sName, $sCheckIn);
  1184. }
  1185. /**
  1186. * Get full URL of CSS file.
  1187. *
  1188. * @param string $sName - CSS file name.
  1189. * @param string $sCheckIn where the content would be searched(base, template, both)
  1190. * @return string full URL.
  1191. */
  1192. function getCssUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1193. {
  1194. return $this->_getAbsoluteLocation('url', $this->_sFolderCss, $sName, $sCheckIn);
  1195. }
  1196. /**
  1197. * Get full Path of CSS file.
  1198. *
  1199. * @param string $sName - CSS file name.
  1200. * @param string $sCheckIn where the content would be searched(base, template, both)
  1201. * @return string full URL.
  1202. */
  1203. function getCssPath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1204. {
  1205. return $this->_getAbsoluteLocation('path', $this->_sFolderCss, $sName, $sCheckIn);
  1206. }
  1207. /**
  1208. * Get full URL of JS file.
  1209. *
  1210. * @param string $sName - JS file name.
  1211. * @return string full URL.
  1212. */
  1213. function getJsUrl($sName)
  1214. {
  1215. return $this->_getAbsoluteLocationJs('url', $sName);
  1216. }
  1217. /**
  1218. * Get full Path of JS file.
  1219. *
  1220. * @param string $sName - JS file name.
  1221. * @return string full URL.
  1222. */
  1223. function getJsPath($sName)
  1224. {
  1225. return $this->_getAbsoluteLocationJs('path', $sName);
  1226. }
  1227. /**
  1228. * Get full URL of Template (HTML) file.
  1229. *
  1230. * @param string $sName - Template file name.
  1231. * @return string full URL.
  1232. */
  1233. function getTemplateUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1234. {
  1235. return $this->_getAbsoluteLocation('url', $this->_sFolderHtml, $sName, $sCheckIn);
  1236. }
  1237. /**
  1238. * Get full Path of JS file.
  1239. *
  1240. * @param string $sName - JS file name.
  1241. * @return string full URL.
  1242. */
  1243. function getTemplatePath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1244. {
  1245. return $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn);
  1246. }
  1247. /**
  1248. * Get menu.
  1249. * @param $s menu object name
  1250. * @return html or empty string
  1251. */
  1252. function getMenu ($s)
  1253. {
  1254. $oMenu = BxDolMenu::getObjectInstance($s);
  1255. if($s == 'sys_site_submenu'){
  1256. $oPage = BxDolPage::getObjectInstanceByURI();
  1257. if ($oPage && $oPage->getSubMenu() == 'disabled'){
  1258. return;
  1259. }
  1260. }
  1261. return $oMenu ? $oMenu->getCode () : '';
  1262. }
  1263. /**
  1264. * Check whether HTML file exists or not.
  1265. *
  1266. * @param string $sName - HTML file name.
  1267. * @param string $sCheckIn where the content would be searched(base, template, both)
  1268. * @return boolean result of operation.
  1269. */
  1270. function isHtml($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1271. {
  1272. return $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn) != '';
  1273. }
  1274. /**
  1275. * Get content of HTML file.
  1276. *
  1277. * @param string $sName - HTML file name.
  1278. * @param string $sCheckIn where the content would be searched(base, template, both)
  1279. * @return string full content of the file and false on failure.
  1280. */
  1281. function getHtml($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1282. {
  1283. $sAbsolutePath = $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn);
  1284. return !empty($sAbsolutePath) ? trim(file_get_contents($sAbsolutePath)) : false;
  1285. }
  1286. /**
  1287. * Parse HTML template. Search for the template with accordance to it's file name.
  1288. *
  1289. * @see allows to use cache.
  1290. *
  1291. * @param string $sName - HTML file name.
  1292. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1293. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(left, right) otherwise).
  1294. * @param string $sCheckIn where the content would be searched(base, template, both)
  1295. * @return string the result of operation.
  1296. */
  1297. function parseHtmlByName($sName, $aVariables, $mixedKeyWrapperHtml = null, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1298. {
  1299. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginTemplate($sName, $sRand = time().rand());
  1300. if (($sContent = $this->getCached($sName, $aVariables, $mixedKeyWrapperHtml, $sCheckIn)) !== false) {
  1301. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endTemplate($sName, $sRand, $sContent, true);
  1302. return $sContent;
  1303. }
  1304. $sRet = '';
  1305. if (($sContent = $this->getHtml($sName, $sCheckIn)) !== false)
  1306. $sRet = $this->_parseContent($sContent, $aVariables, $mixedKeyWrapperHtml);
  1307. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endTemplate($sName, $sRand, $sRet, false);
  1308. return $sRet;
  1309. }
  1310. /**
  1311. * Parse HTML template.
  1312. *
  1313. * @see Doesn't allow to use cache.
  1314. *
  1315. * @param string $sContent - HTML file content.
  1316. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1317. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(left, right) otherwise).
  1318. * @return string the result of operation.
  1319. */
  1320. function parseHtmlByContent($sContent, $aVariables, $mixedKeyWrapperHtml = null)
  1321. {
  1322. if(empty($sContent))
  1323. return "";
  1324. return $this->_parseContent($sContent, $aVariables, $mixedKeyWrapperHtml);
  1325. }
  1326. /**
  1327. * Parse earlier loaded HTML template.
  1328. *
  1329. * @see Doesn't allow to use cache.
  1330. *
  1331. * @param string $sName - template name.
  1332. * @param array $aVariables - key/value pairs. Key should be the same as template's key, excluding prefix and postfix.
  1333. * @return string the result of operation.
  1334. * @see $this->_aTemplates
  1335. */
  1336. function parseHtmlByTemplateName($sName, $aVariables, $mixedKeyWrapperHtml = null)
  1337. {
  1338. if(!isset($this->_aTemplates[$sName]) || empty($this->_aTemplates[$sName]))
  1339. return "";
  1340. return $this->_parseContent($this->_aTemplates[$sName], $aVariables, $mixedKeyWrapperHtml);
  1341. }
  1342. /**
  1343. * Parse page HTML template. Search for the page's template with accordance to it's file name.
  1344. *
  1345. * @see allows to use cache.
  1346. *
  1347. * @param string $sName - HTML file name.
  1348. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1349. * @return string the result of operation.
  1350. */
  1351. function parsePageByName($sName, $aVariables)
  1352. {
  1353. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPage($sName);
  1354. $sContent = $this->parseHtmlByName($sName, $aVariables, $this->_sKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BOTH);
  1355. if(empty($sContent)) {
  1356. $aType = BxDolPageQuery::getPageType($this->getPageType());
  1357. if(!empty($aType) && is_array($aType))
  1358. $sContent = $this->parseHtmlByName($aType['template'], $aVariables, $this->_sKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BOTH);
  1359. }
  1360. if(empty($sContent))
  1361. $sContent = $this->parseHtmlByName('default.html', $aVariables, $this->_sKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BOTH);
  1362. //---Process injection at the very last ---//
  1363. $oTemplate = &$this;
  1364. $sContent = preg_replace_callback("'<bx_injection:([^\s]+) />'s", function($aMatches) use($oTemplate) {
  1365. return $oTemplate->processInjection($oTemplate->getPageNameIndex(), $aMatches[1]);
  1366. }, $sContent);
  1367. //--- Add CSS and JS at the very last ---//
  1368. if(strpos($sContent, '<bx_include_css_styles />') !== false) {
  1369. $aStyles = array(
  1370. 'display' => 'none !important'
  1371. );
  1372. if(isLogged())
  1373. $this->addCssStyle('.bx-hide-when-logged-in', $aStyles);
  1374. else
  1375. $this->addCssStyle('.bx-hide-when-logged-out', $aStyles);
  1376. $sContent = str_replace('<bx_include_css_styles />', $this->includeCssStyles(), $sContent);
  1377. }
  1378. if(strpos($sContent , '<bx_include_css_system />') !== false) {
  1379. $sContent = str_replace('<bx_include_css_system />', $this->includeFiles('css', true), $sContent);
  1380. }
  1381. if(strpos($sContent , '<bx_include_css />') !== false) {
  1382. if (!empty($this->aPage['css_name']))
  1383. $this->addCss($this->aPage['css_name']);
  1384. $sContent = str_replace('<bx_include_css />', $this->includeFiles('css'), $sContent);
  1385. }
  1386. if(strpos($sContent , '<bx_include_js_system />') !== false) {
  1387. $sContent = str_replace('<bx_include_js_system />', $this->includeFiles('js', true), $sContent);
  1388. }
  1389. if(strpos($sContent , '<bx_include_js />') !== false) {
  1390. if (!empty($this->aPage['js_name']))
  1391. $this->addJs($this->aPage['js_name']);
  1392. $sContent = str_replace('<bx_include_js />', $this->includeFiles('js') . $this->includeCssAsync(), $sContent);
  1393. }
  1394. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPage($sContent);
  1395. return $sContent;
  1396. }
  1397. /**
  1398. * Parse system keys.
  1399. *
  1400. * @param string $sKey key
  1401. * @return string value associated with the key.
  1402. */
  1403. function parseSystemKey($sKey, $mixedKeyWrapperHtml = null, $bProcessInjection = true)
  1404. {
  1405. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  1406. $sRet = '';
  1407. switch( $sKey ) {
  1408. case 'page_charset':
  1409. $sRet = 'UTF-8';
  1410. break;
  1411. case 'page_robots':
  1412. if(!empty($this->aPage['robots']) && is_string($this->aPage['robots']))
  1413. $sRet = '<meta name="robots" content="' . bx_html_attribute($this->aPage['robots']) . '" />';
  1414. break;
  1415. case 'meta_info':
  1416. $sRet = $this->getMetaInfo();
  1417. break;
  1418. case 'page_header':
  1419. if(isset($this->aPage['header']))
  1420. $sRet = bx_process_output(strip_tags($this->aPage['header']));
  1421. break;
  1422. case 'page_header_text':
  1423. if(isset($this->aPage['header_text']))
  1424. $sRet = bx_process_output($this->aPage['header_text']);
  1425. break;
  1426. case 'page_width':
  1427. if (false === strpos($this->_oTemplateConfig->aLessConfig['bx-page-width'], 'px'))
  1428. $sRet = BX_DOL_PAGE_WIDTH;
  1429. else
  1430. $sRet = $this->_oTemplateConfig->aLessConfig['bx-page-width'];
  1431. break;
  1432. case 'system_injection_head':
  1433. $sRet = $this->_oTemplateFunctions->getInjectionHead();
  1434. break;
  1435. case 'system_injection_header':
  1436. $sRet = $this->_oTemplateFunctions->getInjectionHeader();
  1437. break;
  1438. case 'system_injection_footer':
  1439. $sRet = $this->_oTemplateFunctions->getInjectionFooter();
  1440. break;
  1441. case 'lang':
  1442. $sRet = bx_lang_name();
  1443. break;
  1444. case 'lang_direction':
  1445. $sRet = bx_lang_direction();
  1446. break;
  1447. case 'lang_country':
  1448. if (!($sRet = BxDolLanguages::getInstance()->getLangCountryCode()))
  1449. $sRet = bx_lang_name();
  1450. break;
  1451. case 'main_logo':
  1452. $sRet = BxTemplFunctions::getInstance()->getMainLogo();
  1453. break;
  1454. case 'informer':
  1455. $oInformer = BxDolInformer::getInstance($this);
  1456. $sRet = $oInformer ? $oInformer->display() : '';
  1457. break;
  1458. case 'cover':
  1459. $oCover = BxDolCover::getInstance($this);
  1460. if(!$oCover)
  1461. break;
  1462. $bCover = true;
  1463. $oPage = BxDolPage::getObjectInstanceByURI();
  1464. if($oPage !== false) {
  1465. $bCover = $oPage->isPageCover();
  1466. if($bCover && !$oCover->isCover()) {
  1467. $aCover = $oPage->getPageCoverImage();
  1468. if(!empty($aCover))
  1469. $oCover->setCoverImageUrl($aCover);
  1470. }
  1471. }
  1472. $sRet = $bCover ? $oCover->display() : $oCover->displayEmpty();
  1473. break;
  1474. case 'dol_images':
  1475. $sRet = $this->_processJsImages();
  1476. break;
  1477. case 'dol_lang':
  1478. $sRet = $this->_processJsTranslations();
  1479. break;
  1480. case 'dol_options':
  1481. $sRet = $this->_processJsOptions();
  1482. break;
  1483. case 'copyright':
  1484. $sRet = _t( '_copyright', date('Y') ) . getVersionComment();
  1485. break;
  1486. case 'copyright_attr':
  1487. $sRet = bx_html_attribute(_t('_copyright', date('Y')));
  1488. break;
  1489. case 'extra_js':
  1490. $sRet = empty($this->aPage['extra_js']) ? '' : $this->aPage['extra_js'];
  1491. break;
  1492. case 'is_profile_page':
  1493. $sRet = (defined('BX_PROFILE_PAGE')) ? 'true' : 'false';
  1494. break;
  1495. case 'system_js_requred':
  1496. $sRet = _t('_sys_javascript_requred');
  1497. break;
  1498. case 'included_css':
  1499. $sRet = json_encode($this->getIncludedUrls('css_compiled'));
  1500. break;
  1501. case 'included_js':
  1502. $sRet = json_encode($this->getIncludedUrls('js_compiled'));
  1503. break;
  1504. case 'base':
  1505. $sRet = bx_convert_array2attrs($this->aPage['base']);
  1506. break;
  1507. default:
  1508. $sRet = ($sTemplAdd = BxTemplFunctions::getInstance()->TemplPageAddComponent($sKey)) !== false ? $sTemplAdd : $aKeyWrappers['left'] . $sKey . $aKeyWrappers['right'];
  1509. }
  1510. if($bProcessInjection)
  1511. $sRet = $this->processInjection($this->getPageNameIndex(), $sKey, $sRet);
  1512. return $sRet;
  1513. }
  1514. /**
  1515. * Parse tag <A>
  1516. *
  1517. * @param string $sLink link URL
  1518. * @param string $sContent link content
  1519. * @param array $aAttrs an array of key => value pairs
  1520. */
  1521. function parseLink($sLink, $sContent, $aAttrs = array())
  1522. {
  1523. $sAttrs = '';
  1524. foreach($aAttrs as $sKey => $sValue)
  1525. $sAttrs .= ' ' . $sKey . '="' . bx_html_attribute($sValue) . '"';
  1526. return '<a href="' . $sLink . '"' . $sAttrs . '>' . $sContent . '</a>';
  1527. }
  1528. /**
  1529. * Parse tag <A> using provided HTML template
  1530. *
  1531. * @param string $sName template's file name
  1532. * @param string $sLink link URL
  1533. * @param string $sContent link content
  1534. * @param array $aAttrs an array of key => value pairs
  1535. */
  1536. function parseLinkByName($sName, $sLink, $sContent, $aAttrs = array())
  1537. {
  1538. $sAttrs = '';
  1539. foreach($aAttrs as $sKey => $sValue)
  1540. $sAttrs .= ' ' . $sKey . '="' . bx_html_attribute($sValue) . '"';
  1541. return $this->parseHtmlByName($sName, array(
  1542. 'href' => $sLink,
  1543. 'attrs' => $sAttrs,
  1544. 'content' => $sContent
  1545. ));
  1546. }
  1547. /**
  1548. * Parse tag <BUTTON>
  1549. *
  1550. * @param string $sContent link content
  1551. * @param array $aAttrs an array of key => value pairs
  1552. */
  1553. function parseButton($sContent, $aAttrs = array())
  1554. {
  1555. $sAttrs = '';
  1556. foreach($aAttrs as $sKey => $sValue)
  1557. $sAttrs .= ' ' . $sKey . '="' . bx_html_attribute($sValue) . '"';
  1558. return '<button' . $sAttrs . '>' . $sContent . '</button>';
  1559. }
  1560. /**
  1561. * Parse tag <IMG>
  1562. *
  1563. * @param string $sLink URL to image source
  1564. * @param array $aAttrs an array of key => value pairs
  1565. */
  1566. function parseImage($sLink, $aAttrs = array())
  1567. {
  1568. $sAttrs = '';
  1569. foreach($aAttrs as $sKey => $sValue)
  1570. $sAttrs .= ' ' . $sKey . '="' . bx_html_attribute($sValue) . '"';
  1571. return '<img src="' . $sLink . '"' . $sAttrs . ' />';
  1572. }
  1573. /**
  1574. * Parse font based icon in <I> tag
  1575. *
  1576. * @param string $sName font icon name
  1577. * @param array $aAttrs an array of key => value pairs
  1578. */
  1579. function parseIcon($sName, $aAttrs = array())
  1580. {
  1581. $aIcons = BxTemplFunctions::getInstance()->getIcon($sName, $aAttrs);
  1582. if ($aIcons[0] != '')
  1583. $aIcons[0] = '';
  1584. return implode($aIcons);
  1585. }
  1586. function getCacheFilePrefix($sType)
  1587. {
  1588. $sResult = '';
  1589. switch($sType) {
  1590. case 'template':
  1591. $sResult = $this->_sCacheFilePrefix;
  1592. break;
  1593. case 'less':
  1594. $sResult = $this->_sCssLessPrefix;
  1595. break;
  1596. case 'css':
  1597. $sResult = $this->_sCssCachePrefix;
  1598. break;
  1599. case 'js':
  1600. $sResult = $this->_sJsCachePrefix;
  1601. break;
  1602. }
  1603. return $sResult;
  1604. }
  1605. /**
  1606. * Get cache object for templates
  1607. * @return cache class instance
  1608. */
  1609. function getTemplatesCacheObject ()
  1610. {
  1611. $sCacheEngine = getParam('sys_template_cache_engine');
  1612. $oCacheEngine = bx_instance('BxDolCache' . $sCacheEngine);
  1613. if(!$oCacheEngine->isAvailable())
  1614. $oCacheEngine = bx_instance('BxDolCacheFileHtml');
  1615. return $oCacheEngine;
  1616. }
  1617. /**
  1618. * Get template from cache if it's enabled.
  1619. *
  1620. * @param string $sName template name
  1621. * @param string $aVariables key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1622. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1623. * @param string $sCheckIn where the content would be searched(base, template, both)
  1624. * @param boolean $bEvaluate need to evaluate the template or not.
  1625. * @return string result of operation or false on failure.
  1626. */
  1627. function getCached($sName, &$aVariables, $mixedKeyWrapperHtml = null, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH, $bEvaluate = true)
  1628. {
  1629. // initialization
  1630. if(!$this->_bCacheEnable)
  1631. return false;
  1632. if (in_array($sName, $this->_aCacheExceptions))
  1633. return false;
  1634. $sAbsolutePath = $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn);
  1635. if(empty($sAbsolutePath))
  1636. return false;
  1637. $oCacheEngine = $this->getTemplatesCacheObject ();
  1638. $isFileBasedEngine = $bEvaluate && method_exists($oCacheEngine, 'getDataFilePath');
  1639. // try to get cached content
  1640. $sCacheVariableName = "a";
  1641. $sCacheKey = $this->_getCacheFileName('html', $sAbsolutePath) . '.php';
  1642. if ($isFileBasedEngine)
  1643. $sCacheContent = $oCacheEngine->getDataFilePath($sCacheKey);
  1644. else
  1645. $sCacheContent = $oCacheEngine->getData($sCacheKey);
  1646. // recreate cache if it is empty
  1647. if ($sCacheContent === null && ($sContent = file_get_contents($sAbsolutePath)) !== false && ($sContent = $this->_compileContent($sContent, "\$" . $sCacheVariableName, 1, $aVariables, $mixedKeyWrapperHtml)) !== false) {
  1648. if (false === $oCacheEngine->setData($sCacheKey, trim($sContent)))
  1649. return false;
  1650. if ($isFileBasedEngine)
  1651. $sCacheContent = $oCacheEngine->getDataFilePath($sCacheKey);
  1652. else
  1653. $sCacheContent = $sContent;
  1654. }
  1655. if ($sCacheContent === null)
  1656. return false;
  1657. // return simple cache content
  1658. if(!$bEvaluate)
  1659. return $sCacheContent;
  1660. // return evaluated cache content
  1661. ob_start();
  1662. $$sCacheVariableName = &$aVariables;
  1663. if ($isFileBasedEngine)
  1664. include($sCacheContent);
  1665. else
  1666. eval('?'.'>' . $sCacheContent);
  1667. $sContent = ob_get_clean();
  1668. return $sContent;
  1669. }
  1670. /**
  1671. * Add JS file(s) to global output.
  1672. *
  1673. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1674. * @param boolean $bDynamic in the dynamic mode JS file(s) are not included to global output, but are returned from the function directly.
  1675. * @return boolean/string result of operation.
  1676. */
  1677. function addJs($mixedFiles, $bDynamic = false)
  1678. {
  1679. return $this->_processFiles('js', 'add', $mixedFiles, $bDynamic);
  1680. }
  1681. function addJsPreloaded($aFiles, $sCallback = false, $sCondition = false, $sConditionElseCallback = false)
  1682. {
  1683. if(!$aFiles)
  1684. return '';
  1685. if(!is_array($aFiles))
  1686. $aFiles = [$aFiles];
  1687. $sMaskLoad = "bx_get_scripts(%s);";
  1688. $sMaskLoadWithCallback = "bx_get_scripts(%s, function() {%s});";
  1689. $sMaskCondition = "if(%s) {%s}";
  1690. $sMaskConditionWithElse = "if(%s) {%s} else {setTimeout(function() {%s}, 10);}";
  1691. $iRev = $this->_getRevision();
  1692. $aFilesLocated = [];
  1693. foreach($aFiles as $sFile) {
  1694. $mixedFile = $this->_locateFile('js', $sFile);
  1695. if($mixedFile === false)
  1696. continue;
  1697. list($sUrl) = $mixedFile;
  1698. $aFilesLocated[] = bx_append_url_params($sUrl, ['rev' => $iRev]);
  1699. }
  1700. $sFilesLocated = json_encode($aFilesLocated);
  1701. if($sCallback !== false)
  1702. $sResult = sprintf($sMaskLoadWithCallback, $sFilesLocated, $sCallback);
  1703. else
  1704. $sResult = sprintf($sMaskLoad, $sFilesLocated);
  1705. if($sCondition === false)
  1706. return $sResult;
  1707. if($sConditionElseCallback !== false)
  1708. $sResult = sprintf($sMaskConditionWithElse, $sCondition, $sResult, $sConditionElseCallback);
  1709. else
  1710. $sResult = sprintf($sMaskCondition, $sCondition, $sResult);
  1711. return $sResult;
  1712. }
  1713. function addJsPreloadedWrapped($aFiles, $sCallback = false, $sCondition = false, $sConditionElseCallback = false)
  1714. {
  1715. $sCode = $this->addJsPreloaded($aFiles, $sCallback, $sCondition, $sConditionElseCallback);
  1716. if(!$sCode)
  1717. return '';
  1718. return $this->_wrapInTagJsCode($sCode);
  1719. }
  1720. function addJsCodeOnLoad($sCallback)
  1721. {
  1722. $sMaskLoad = "$(document).ready(function() {%s});";
  1723. return sprintf($sMaskLoad, $sCallback);
  1724. }
  1725. function addJsCodeOnLoadWrapped($sCallback)
  1726. {
  1727. $sCode = $this->addJsCodeOnLoad($sCallback);
  1728. return $this->_wrapInTagJsCode($sCode);
  1729. }
  1730. /**
  1731. * get added js files
  1732. */
  1733. function getJs()
  1734. {
  1735. return $this->aPage['js_compiled'];
  1736. }
  1737. /**
  1738. * Add System JS file(s) to global output.
  1739. * System JS files are the files which are attached to all pages. They will be cached separately from the others.
  1740. *
  1741. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1742. * @param boolean $bDynamic in the dynamic mode JS file(s) are not included to global output, but are returned from the function directly.
  1743. * @return boolean/string result of operation.
  1744. */
  1745. function addJsSystem($mixedFiles)
  1746. {
  1747. return $this->_processFiles('js', 'add', $mixedFiles, false, true);
  1748. }
  1749. /**
  1750. * Delete JS file(s) from global output.
  1751. *
  1752. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1753. * @return boolean result of operation.
  1754. */
  1755. function deleteJs($mixedFiles)
  1756. {
  1757. return $this->_processFiles('js', 'delete', $mixedFiles);
  1758. }
  1759. /**
  1760. * Delete System JS file(s) from global output.
  1761. *
  1762. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1763. * @return boolean result of operation.
  1764. */
  1765. function deleteJsSystem($mixedFiles)
  1766. {
  1767. return $this->_processFiles('js', 'delete', $mixedFiles, false, true);
  1768. }
  1769. /**
  1770. * Compile JS files in one file.
  1771. *
  1772. * @param string $sAbsolutePath CSS file absolute path(full URL for external CSS/JS files).
  1773. * @param array $aIncluded an array of already included JS files.
  1774. * @return string result of operation.
  1775. */
  1776. function _compileJs($sAbsolutePath, &$aIncluded)
  1777. {
  1778. if(isset($aIncluded[$sAbsolutePath]))
  1779. return '';
  1780. $bExternal = strpos($sAbsolutePath, "http://") !== false || strpos($sAbsolutePath, "https://") !== false;
  1781. if($bExternal) {
  1782. $sPath = $sAbsolutePath;
  1783. $sName = '';
  1784. $sContent = bx_file_get_contents($sAbsolutePath);
  1785. } else {
  1786. $aFileInfo = pathinfo($sAbsolutePath);
  1787. $sPath = $aFileInfo['dirname'] . DIRECTORY_SEPARATOR;
  1788. $sName = $aFileInfo['basename'];
  1789. $sContent = file_get_contents($sPath . $sName);
  1790. }
  1791. if(empty($sContent))
  1792. return '';
  1793. $sUrl = bx_ltrim_str($sPath, realpath(BX_DIRECTORY_PATH_ROOT), BX_DOL_URL_ROOT);
  1794. $sUrl = str_replace(DIRECTORY_SEPARATOR, '/', $sUrl);
  1795. $sContent = "\r\n/*--- BEGIN: " . $sUrl . $sName . "---*/\r\n" . $sContent . ";\r\n/*--- END: " . $sUrl . $sName . "---*/\r\n";
  1796. $sContent = str_replace(array("\n\r", "\r\n", "\r"), "\n", $sContent);
  1797. $aIncluded[$sAbsolutePath] = 1;
  1798. return preg_replace(
  1799. array(
  1800. "'<bx_url_root />'",
  1801. "'\r\n'"
  1802. ),
  1803. array(
  1804. BX_DOL_URL_ROOT,
  1805. "\n"
  1806. ),
  1807. $sContent
  1808. );
  1809. }
  1810. /**
  1811. * Minify JS
  1812. *
  1813. * @param string $s JS string to minify
  1814. * @return string minified JS string.
  1815. */
  1816. function _minifyJs($s)
  1817. {
  1818. // since each JS file is minified separately, it has to be in own scope
  1819. return "\n {\n" . BxDolMinify::getInstance()->minifyJs($s) . "\n }\n";
  1820. }
  1821. /**
  1822. * Wrap an URL to JS file into JS tag.
  1823. *
  1824. * @param string $sFile - URL to JS file.
  1825. * @return string the result of operation.
  1826. */
  1827. function _wrapInTagJs($sFile)
  1828. {
  1829. return "<script language=\"javascript\" src=\"" . $sFile . "\"></script>";
  1830. }
  1831. /**
  1832. * Wrap JS code into JS tag.
  1833. *
  1834. * @param string $sCode - JS code.
  1835. * @return string the result of operation.
  1836. */
  1837. function _wrapInTagJsCode($sCode)
  1838. {
  1839. return "<script language=\"javascript\">\n" . $sCode . "\n</script>";
  1840. }
  1841. /**
  1842. * Add CSS file(s) to global output.
  1843. *
  1844. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1845. * @param boolean $bDynamic in the dynamic mode CSS file(s) are not included to global output, but are returned from the function directly.
  1846. * @return boolean/string result of operation
  1847. */
  1848. function addCss($mixedFiles, $bDynamic = false)
  1849. {
  1850. if($bDynamic)
  1851. return $this->addCssPreloadedWrapped($mixedFiles);
  1852. else
  1853. return $this->_processFiles('css', 'add', $mixedFiles, $bDynamic);
  1854. }
  1855. function addCssPreloaded($aFiles)
  1856. {
  1857. if(!$aFiles)
  1858. return '';
  1859. if(!is_array($aFiles))
  1860. $aFiles = [$aFiles];
  1861. $sMaskLoad = "bx_get_style(%s);";
  1862. $iRev = $this->_getRevision();
  1863. $aFilesLocated = [];
  1864. foreach($aFiles as $sFile) {
  1865. $mixedFile = $this->_locateFile('css', $sFile);
  1866. if($mixedFile === false)
  1867. continue;
  1868. list($sUrl) = $mixedFile;
  1869. $aFilesLocated[] = bx_append_url_params($sUrl, ['rev' => $iRev]);
  1870. }
  1871. $sFilesLocated = json_encode($aFilesLocated);
  1872. return sprintf($sMaskLoad, $sFilesLocated);
  1873. }
  1874. function addCssPreloadedWrapped($aFiles)
  1875. {
  1876. $sCode = $this->addCssPreloaded($aFiles);
  1877. if(!$sCode)
  1878. return '';
  1879. return $this->_wrapInTagJsCode($sCode);
  1880. }
  1881. /**
  1882. * get added css files
  1883. */
  1884. function getCss()
  1885. {
  1886. return $this->aPage['css_compiled'];
  1887. }
  1888. /**
  1889. * Add additional heavy css file (not very necessary) to load asynchronously for desktop browsers only
  1890. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1891. */
  1892. function addCssAsync($mixedFiles)
  1893. {
  1894. if (!is_array($mixedFiles))
  1895. $mixedFiles = array($mixedFiles);
  1896. foreach ($mixedFiles as $sFile)
  1897. $this->aPage['css_async'][] = $this->_getAbsoluteLocationCss('url', $sFile);
  1898. $this->addJs('loadCSS.js');
  1899. }
  1900. /**
  1901. * Return script tag with special code to load async css.
  1902. * This tag is added after js files list
  1903. */
  1904. function includeCssAsync ()
  1905. {
  1906. if (empty($this->aPage['css_async']))
  1907. return '';
  1908. $this->aPage['css_async'] = array_unique($this->aPage['css_async']);
  1909. $sList = '';
  1910. foreach ($this->aPage['css_async'] as $sUrl)
  1911. $sList .= 'loadCSS("' . $sUrl . '", document.getElementById("bx_css_async"));';
  1912. // don't load css for mobile devices
  1913. return '
  1914. <script id="bx_css_async">
  1915. if(!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
  1916. ' . $sList . '
  1917. }
  1918. </script>
  1919. ';
  1920. }
  1921. /**
  1922. * Add System CSS file(s) to global output.
  1923. * System CSS files are the files which are attached to all pages. They will be cached separately from the others.
  1924. *
  1925. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1926. * @return boolean/string result of operation
  1927. */
  1928. function addCssSystem($mixedFiles)
  1929. {
  1930. return $this->_processFiles('css', 'add', $mixedFiles, false, true);
  1931. }
  1932. /**
  1933. * Delete CSS file(s) from global output.
  1934. *
  1935. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1936. * @return boolean result of operation.
  1937. */
  1938. function deleteCss($mixedFiles)
  1939. {
  1940. return $this->_processFiles('css', 'delete', $mixedFiles);
  1941. }
  1942. /**
  1943. * Delete System CSS file(s) from global output.
  1944. *
  1945. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1946. * @return boolean result of operation.
  1947. */
  1948. function deleteCssSystem($mixedFiles)
  1949. {
  1950. return $this->_processFiles('css', 'delete', $mixedFiles, false, true);
  1951. }
  1952. /**
  1953. * Compile CSS files' structure(@see \@import css_file_path) in one file.
  1954. *
  1955. * @param string $sAbsolutePath CSS file absolute path(full URL for external CSS/JS files).
  1956. * @param array $aIncluded an array of already included CSS files.
  1957. * @return string result of operation.
  1958. */
  1959. function _compileCss($sAbsolutePath, &$aIncluded)
  1960. {
  1961. if(isset($aIncluded[$sAbsolutePath]))
  1962. return '';
  1963. $bExternal = strpos($sAbsolutePath, "http://") !== false || strpos($sAbsolutePath, "https://") !== false;
  1964. if($bExternal) {
  1965. $sPath = $sAbsolutePath;
  1966. $sName = '';
  1967. $aAPUrl = parse_url($sAbsolutePath);
  1968. if(!empty($aAPUrl['path'])) {
  1969. $aAPPath = pathinfo($aAPUrl['path']);
  1970. if(!empty($aAPPath['basename'])) {
  1971. $sPath = bx_rtrim_str($sAbsolutePath, $aAPPath['basename']);
  1972. $sName = $aAPPath['basename'];
  1973. }
  1974. }
  1975. $sContent = bx_file_get_contents($sAbsolutePath);
  1976. } else {
  1977. $aFileInfo = pathinfo($sAbsolutePath);
  1978. $sPath = $aFileInfo['dirname'] . DIRECTORY_SEPARATOR;
  1979. $sName = $aFileInfo['basename'];
  1980. $sContent = file_get_contents($sPath . $sName);
  1981. }
  1982. if(empty($sContent))
  1983. return '';
  1984. $sUrl = bx_ltrim_str($sPath, realpath(BX_DIRECTORY_PATH_ROOT), BX_DOL_URL_ROOT);
  1985. $sUrl = str_replace(DIRECTORY_SEPARATOR, '/', $sUrl);
  1986. $sContent = "\r\n/*--- BEGIN: " . $sUrl . $sName . "---*/\r\n" . $sContent . "\r\n/*--- END: " . $sUrl . $sName . "---*/\r\n";
  1987. $aIncluded[$sAbsolutePath] = 1;
  1988. $sContent = str_replace(array("\n\r", "\r\n", "\r"), "\n", $sContent);
  1989. if($bExternal) {
  1990. $sContent = preg_replace(
  1991. array(
  1992. "'@import\s+url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*\)\s*;'",
  1993. "'url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/\?\#_=-]+)\s*[\'|\"]*\s*\)'"
  1994. ),
  1995. array(
  1996. "",
  1997. "url(" . $sPath . "\\1)"
  1998. ),
  1999. $sContent
  2000. );
  2001. }
  2002. else {
  2003. try {
  2004. $oTemplate = &$this;
  2005. /* Match URL based imports like the following:
  2006. * @import 'http://[domain]/modules/[vendor]/[module]/template/css/view.css';
  2007. * Is mainly needed for CSS files which are gotten from LESS compiler.
  2008. */
  2009. $sContent = preg_replace_callback(
  2010. "'@import\s+[\'|\"]*\s*" . str_replace("/", "\/", BX_DOL_URL_ROOT) . "([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*;'", function ($aMatches) use($oTemplate, $sPath, &$aIncluded) {
  2011. return $oTemplate->_compileCss(realpath(BX_DIRECTORY_PATH_ROOT . $aMatches[1]), $aIncluded);
  2012. }, $sContent);
  2013. /* Match relative path based imports like the following:
  2014. * @import url(../../../../../../base/profile/template/css/main.css);
  2015. * Is mainly needed for default CSS files.
  2016. */
  2017. $sContent = preg_replace_callback(
  2018. "'@import\s+url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*\)\s*;'", function ($aMatches) use($oTemplate, $sPath, &$aIncluded) {
  2019. return $oTemplate->_compileCss(realpath($sPath . dirname($aMatches[1])) . DIRECTORY_SEPARATOR . basename($aMatches[1]), $aIncluded);
  2020. }, $sContent);
  2021. $sContent = preg_replace_callback(
  2022. "'url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/\?\#_=-]+)\s*[\'|\"]*\s*\)'", function ($aMatches) use($oTemplate, $sPath) {
  2023. $sFile = basename($aMatches[1]);
  2024. $sDirectory = dirname($aMatches[1]);
  2025. $sRootPath = realpath(BX_DIRECTORY_PATH_ROOT) . '/';
  2026. $sAbsolutePath = realpath(addslashes($sPath) . $sDirectory) . DIRECTORY_SEPARATOR . $sFile;
  2027. $sRootPath = str_replace(DIRECTORY_SEPARATOR, '/', $sRootPath);
  2028. $sAbsolutePath = str_replace(DIRECTORY_SEPARATOR, '/', $sAbsolutePath);
  2029. return 'url(' . bx_ltrim_str($sAbsolutePath, $sRootPath, BX_DOL_URL_ROOT) . ')';
  2030. }, $sContent);
  2031. }
  2032. catch(Exception $oException) {
  2033. return '';
  2034. }
  2035. }
  2036. return $sContent;
  2037. }
  2038. /**
  2039. * Less CSS
  2040. *
  2041. * @param mixed $mixed CSS string to process with Less compiler or an array with CSS file's Path and URL.
  2042. * @return mixed string or an array with CSS file's Path and URL.
  2043. */
  2044. function _lessCss($mixed)
  2045. {
  2046. if(is_array($mixed) && isset($mixed['url']) && isset($mixed['path'])) {
  2047. $sPathFile = realpath($mixed['path']);
  2048. $aInfoFile = pathinfo($sPathFile);
  2049. if (!isset($aInfoFile['extension']) || $aInfoFile['extension'] != 'less')
  2050. return $mixed;
  2051. $aFiles = array($mixed['path'] => $mixed['url']);
  2052. $aOptions = array('cache_dir' => $this->_sCachePublicFolderPath, 'prefix' => $this->_sCssLessPrefix);
  2053. $sFile = Less_Cache::Get($aFiles, $aOptions, $this->_oTemplateConfig->aLessConfig);
  2054. return array('url' => $this->_sCachePublicFolderUrl . $sFile, 'path' => $this->_sCachePublicFolderPath . $sFile);
  2055. }
  2056. $oLess = new Less_Parser();
  2057. $oLess->ModifyVars($this->_oTemplateConfig->aLessConfig);
  2058. $oLess->parse($mixed);
  2059. return $oLess->getCss();
  2060. }
  2061. /**
  2062. * Minify CSS
  2063. *
  2064. * @param string $s CSS string to minify
  2065. * @return string minified CSS string.
  2066. */
  2067. function _minifyCss($s)
  2068. {
  2069. return BxDolMinify::getInstance()->minifyCss($s);
  2070. }
  2071. /**
  2072. * Wrap an URL to CSS file into CSS tag.
  2073. *
  2074. * @param string $sFile - URL to CSS file.
  2075. * @return string the result of operation.
  2076. */
  2077. function _wrapInTagCss($sFile)
  2078. {
  2079. if (!$sFile)
  2080. return '';
  2081. return "<link href=\"" . $sFile . "\" rel=\"stylesheet\" type=\"text/css\" />";
  2082. }
  2083. /**
  2084. * Wrap CSS code into CSS tag.
  2085. *
  2086. * @param string $sCode - CSS code.
  2087. * @return string the result of operation.
  2088. */
  2089. function _wrapInTagCssCode($sCode)
  2090. {
  2091. return "<style>" . $sCode . "</style>";
  2092. }
  2093. /**
  2094. * Include CSS style(s) in the page's head section.
  2095. */
  2096. function includeCssStyles()
  2097. {
  2098. $sResult = "";
  2099. if(empty($this->aPage['css_styles']) || !is_array($this->aPage['css_styles']))
  2100. return $sResult;
  2101. foreach($this->aPage['css_styles'] as $sName => $aContent) {
  2102. $sContent = "";
  2103. if(!empty($aContent) && is_array($aContent))
  2104. foreach($aContent as $sStyleName => $sStyleValue)
  2105. $sContent .= "\t" . $sStyleName . ": " . $sStyleValue . ";\r\n";
  2106. $sResult .= $sName . " {\r\n" . $sContent . "}\r\n";
  2107. }
  2108. return !empty($sResult) ? $this->_wrapInTagCssCode($sResult) : '';
  2109. }
  2110. /**
  2111. * Include CSS/JS file(s) attached to the page in its head section.
  2112. * @see the method is system and would be called automatically.
  2113. *
  2114. * @param string $sType the type of file('js' or 'css')
  2115. * @return string the result CSS code.
  2116. */
  2117. function includeFiles($sType, $bSystem = false)
  2118. {
  2119. $sUpcaseType = ucfirst($sType);
  2120. $sArrayKey = $sType . ($bSystem ? '_system' : '_compiled');
  2121. $aFiles = isset($this->aPage[$sArrayKey]) ? $this->aPage[$sArrayKey] : array();
  2122. if(empty($aFiles) || !is_array($aFiles))
  2123. return "";
  2124. if(!$this->{'_b' . $sUpcaseType . 'Cache'})
  2125. return $this->_includeFiles($sType, $aFiles);
  2126. //--- If cache already exists, return it ---//
  2127. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  2128. $sMethodCompile = '_compile' . $sUpcaseType;
  2129. $sMethodLess = '_less' . $sUpcaseType;
  2130. $sMethodMinify = '_minify' . $sUpcaseType;
  2131. ksort($aFiles);
  2132. $sName = "";
  2133. foreach($aFiles as $aFile)
  2134. $sName .= $aFile['url'];
  2135. $sName = $this->_getCacheFileName($sType, $sName);
  2136. $sCacheAbsoluteUrl = $this->_sCachePublicFolderUrl . $sName . '.' . $sType;
  2137. $sCacheAbsolutePath = $this->_sCachePublicFolderPath . $sName . '.' . $sType;
  2138. if(file_exists($sCacheAbsolutePath)) {
  2139. if($this->{'_b' . $sUpcaseType . 'Archive'})
  2140. $sCacheAbsoluteUrl = $this->_getLoaderUrl($sType, $sName);
  2141. return $this->$sMethodWrap($sCacheAbsoluteUrl);
  2142. }
  2143. //--- Collect all attached CSS/JS in one file ---//
  2144. $sResult = "";
  2145. $aIncluded = array();
  2146. foreach($aFiles as $aFile) {
  2147. if($this->{'_b' . $sUpcaseType . 'Less'})
  2148. $aFile = $this->$sMethodLess($aFile);
  2149. if(($sContent = $this->$sMethodCompile($aFile['path'], $aIncluded)) === false)
  2150. continue;
  2151. if(!preg_match('/[\.-]min.(js|css)$/i', $aFile['path']) && $this->{'_b' . $sUpcaseType . 'Minify'}) // don't minify minified files
  2152. $sContent = $this->$sMethodMinify($sContent);
  2153. $sResult .= $sContent;
  2154. }
  2155. $mixedWriteResult = false;
  2156. if(!empty($sResult) && ($rHandler = fopen($sCacheAbsolutePath, 'w')) !== false) {
  2157. $mixedWriteResult = fwrite($rHandler, $sResult);
  2158. fclose($rHandler);
  2159. @chmod ($sCacheAbsolutePath, BX_DOL_FILE_RIGHTS);
  2160. }
  2161. if($mixedWriteResult === false)
  2162. return $this->_includeFiles($sType, $aFiles);
  2163. if($this->{'_b' . $sUpcaseType . 'Archive'})
  2164. $sCacheAbsoluteUrl = $this->_getLoaderUrl($sType, $sName);
  2165. return $this->$sMethodWrap($sCacheAbsoluteUrl);
  2166. }
  2167. /**
  2168. * Include CSS/JS files without caching.
  2169. *
  2170. * @param string $sType the file type (css or js)
  2171. * @param array $aFiles CSS/JS files to be added to the page.
  2172. * @return string result of operation.
  2173. */
  2174. function _includeFiles($sType, &$aFiles)
  2175. {
  2176. $iRev = $this->_getRevision();
  2177. $sUpcaseType = ucfirst($sType);
  2178. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  2179. $sMethodLess = '_less' . $sUpcaseType;
  2180. $sResult = "";
  2181. foreach($aFiles as $aFile) {
  2182. if($this->{'_b' . $sUpcaseType . 'Less'})
  2183. $aFile = $this->$sMethodLess($aFile);
  2184. $sFileUrl = $aFile['url'];
  2185. if(!$this->{'_b' . $sUpcaseType . 'Cache'})
  2186. $sFileUrl = bx_append_url_params($sFileUrl, array(
  2187. 'rev' => $iRev
  2188. ));
  2189. $sResult .= $this->$sMethodWrap($sFileUrl);
  2190. }
  2191. return $sResult;
  2192. }
  2193. /**
  2194. * Insert/Delete CSS file from output stack.
  2195. *
  2196. * @param string $sType the file type (css or js)
  2197. * @param string $sAction add/delete
  2198. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  2199. * @return boolean result of operation.
  2200. */
  2201. function _processFiles($sType, $sAction, $mixedFiles, $bDynamic = false, $bSystem = false)
  2202. {
  2203. if(empty($mixedFiles))
  2204. return $bDynamic ? "" : false;
  2205. if(is_string($mixedFiles))
  2206. $mixedFiles = array($mixedFiles);
  2207. $iRev = $this->_getRevision();
  2208. $sUpcaseType = ucfirst($sType);
  2209. $sMethodLocate = '_getAbsoluteLocation' . $sUpcaseType;
  2210. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  2211. $sResult = '';
  2212. foreach($mixedFiles as $sFile) {
  2213. $mixedFile = $this->_locateFile($sType, $sFile);
  2214. if($mixedFile === false)
  2215. continue;
  2216. list($sUrl, $sPath) = $mixedFile;
  2217. $sArrayKey = $sType . ($bSystem ? '_system' : '_compiled');
  2218. switch($sAction) {
  2219. case 'add':
  2220. if($bDynamic)
  2221. $sResult .= $this->$sMethodWrap(bx_append_url_params($sUrl, array(
  2222. 'rev' => $iRev
  2223. )));
  2224. else {
  2225. $bFound = false;
  2226. $aSearchIn = $bSystem ? $this->aPage[$sArrayKey] : array_merge($this->aPage[$sType . '_system'], $this->aPage[$sArrayKey]);
  2227. foreach($aSearchIn as $iKey => $aValue)
  2228. if($aValue['url'] == $sUrl && $aValue['path'] == $sPath) {
  2229. $bFound = true;
  2230. break;
  2231. }
  2232. if(!$bFound)
  2233. $this->aPage[$sArrayKey][] = array('url' => $sUrl, 'path' => $sPath);
  2234. }
  2235. break;
  2236. case 'delete':
  2237. if(!$bDynamic)
  2238. foreach($this->aPage[$sArrayKey] as $iKey => $aValue)
  2239. if($aValue['url'] == $sUrl) {
  2240. unset($this->aPage[$sArrayKey][$iKey]);
  2241. break;
  2242. }
  2243. break;
  2244. }
  2245. }
  2246. return $bDynamic ? $sResult : true;
  2247. }
  2248. function _locateFile($sType, $sFile)
  2249. {
  2250. //--- Process 3d Party CSS/JS file ---//
  2251. if(strpos($sFile, "http://") !== false || strpos($sFile, "https://") !== false) {
  2252. $sUrl = $sFile;
  2253. $sPath = $sFile;
  2254. }
  2255. //--- Process Custom CSS/JS file ---//
  2256. else if(strpos($sFile, "|") !== false) {
  2257. $sFile = implode('', explode("|", $sFile));
  2258. $sFile = bx_ltrim_str($sFile, BX_DIRECTORY_PATH_ROOT);
  2259. $sUrl = BX_DOL_URL_ROOT . $sFile;
  2260. $sPath = realpath(BX_DIRECTORY_PATH_ROOT . $sFile);
  2261. }
  2262. //--- Process Common CSS/JS file(check in default locations) ---//
  2263. else {
  2264. $sMethodLocate = '_getAbsoluteLocation' . ucfirst($sType);
  2265. $sUrl = $this->$sMethodLocate('url', $sFile);
  2266. $sPath = $this->$sMethodLocate('path', $sFile);
  2267. }
  2268. return !empty($sUrl) && !empty($sPath) ? [$sUrl, $sPath] : false;
  2269. }
  2270. /**
  2271. * Parse content.
  2272. *
  2273. * @param string $sContent - HTML file's content.
  2274. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  2275. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  2276. * @return string the result of operation.
  2277. */
  2278. function _parseContent($sContent, $aVariables, $mixedKeyWrapperHtml = null)
  2279. {
  2280. $aKeysSrc = array_keys($aVariables);
  2281. $aValuesSrc = array_values($aVariables);
  2282. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  2283. $sKeyIf = 'bx_if:';
  2284. $sKeyRepeat = 'bx_repeat:';
  2285. $iCountKeys = count($aKeysSrc);
  2286. $aKeys = $aValues = array();
  2287. //--- Parse simple keys ---//
  2288. for ($i = 0; $i < $iCountKeys; $i++) {
  2289. if (strncmp($aKeysSrc[$i], $sKeyRepeat, 10) === 0 || strncmp($aKeysSrc[$i], $sKeyIf, 6) === 0)
  2290. continue;
  2291. $aKeys[] = "'" . $aKeyWrappers['left'] . $aKeysSrc[$i] . $aKeyWrappers['right'] . "'s";
  2292. $aValues[] = str_replace('$', '\\$', str_replace('\\', '\\\\', $aValuesSrc[$i]));
  2293. }
  2294. //--- Parse keys with constructions ---//
  2295. for ($i = 0; $i < $iCountKeys; $i++) {
  2296. if (strncmp($aKeysSrc[$i], $sKeyRepeat, 10) === 0) {
  2297. $sKey = "'<" . $aKeysSrc[$i] . ">(.*)<\/" . $aKeysSrc[$i] . ">'s";
  2298. $aMatches = array();
  2299. preg_match($sKey, $sContent, $aMatches);
  2300. $sValue = '';
  2301. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  2302. if(is_array($aValuesSrc[$i]))
  2303. foreach($aValuesSrc[$i] as $aValue)
  2304. $sValue .= $this->parseHtmlByContent($aMatches[1], $aValue, $mixedKeyWrapperHtml);
  2305. else if(is_string($aValuesSrc[$i]))
  2306. $sValue = $aValuesSrc[$i];
  2307. }
  2308. }
  2309. else if (strncmp($aKeysSrc[$i], $sKeyIf, 6) === 0) {
  2310. $sKey = "'<" . $aKeysSrc[$i] . ">(.*)<\/" . $aKeysSrc[$i] . ">'s";
  2311. $aMatches = array();
  2312. preg_match($sKey, $sContent, $aMatches);
  2313. $sValue = '';
  2314. if(isset($aMatches[1]) && !empty($aMatches[1]))
  2315. if(is_array($aValuesSrc[$i]) && isset($aValuesSrc[$i]['content']) && isset($aValuesSrc[$i]['condition']) && $aValuesSrc[$i]['condition'])
  2316. $sValue .= $this->parseHtmlByContent($aMatches[1], $aValuesSrc[$i]['content'], $mixedKeyWrapperHtml);
  2317. }
  2318. else
  2319. continue;
  2320. $aKeys[] = $sKey;
  2321. $aValues[] = str_replace('$', '\\$', str_replace('\\', '\\\\', $sValue));
  2322. }
  2323. try {
  2324. $oTemplate = &$this;
  2325. $aCallbackPatterns = array(
  2326. "'<bx_include_auto:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BOTH,
  2327. "'<bx_include_auto_mod_general:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_general'),
  2328. "'<bx_include_auto_mod_profile:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_profile'),
  2329. "'<bx_include_auto_mod_group:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_group'),
  2330. "'<bx_include_auto_mod_text:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_text'),
  2331. "'<bx_include_base:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BASE,
  2332. "'<bx_include_base_mod_general:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_general'),
  2333. "'<bx_include_base_mod_profile:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_profile'),
  2334. "'<bx_include_base_mod_group:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_group'),
  2335. "'<bx_include_base_mod_text:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_text'),
  2336. "'<bx_include_tmpl:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_TMPL,
  2337. "'<bx_include_tmpl_mod_general:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_general'),
  2338. "'<bx_include_tmpl_mod_profile:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_profile'),
  2339. "'<bx_include_tmpl_mod_group:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_group'),
  2340. "'<bx_include_tmpl_mod_text:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_text')
  2341. );
  2342. foreach($aCallbackPatterns as $sPattern => $sCheckIn)
  2343. $sContent = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $aVariables, $mixedKeyWrapperHtml, $sCheckIn) {
  2344. return $oTemplate->parseHtmlByName($aMatches[1], $aVariables, $mixedKeyWrapperHtml, $sCheckIn);
  2345. }, $sContent);
  2346. $sContent = $this->_parseContentKeys($sContent, array(
  2347. "'<bx_menu:([^\s]+) \/>'s" => 'get_menu',
  2348. ));
  2349. }
  2350. catch(Exception $oException) {
  2351. return '';
  2352. }
  2353. $aKeys = array_merge($aKeys, array(
  2354. "'<bx_url_root />'",
  2355. "'<bx_url_studio />'",
  2356. ));
  2357. $aValues = array_merge($aValues, array(
  2358. BX_DOL_URL_ROOT,
  2359. BX_DOL_URL_STUDIO,
  2360. ));
  2361. //--- Parse Predefined Keys ---//
  2362. $sContent = preg_replace($aKeys, $aValues, $sContent);
  2363. //--- Parse System Keys ---//
  2364. $sContent = preg_replace_callback("'" . $aKeyWrappers['left'] . "([a-zA-Z0-9_-]+)" . $aKeyWrappers['right'] . "'", function($aMatches) use($oTemplate, $mixedKeyWrapperHtml) {
  2365. return $oTemplate->parseSystemKey($aMatches[1], $mixedKeyWrapperHtml);
  2366. }, $sContent);
  2367. return $sContent;
  2368. }
  2369. /**
  2370. * Compile content
  2371. *
  2372. * @param string $sContent template.
  2373. * @param string $aVarName variable name to be saved in the output file.
  2374. * @param integer $iVarDepth depth is used to process nesting, for example, in cycles.
  2375. * @param array $aVarValues values to be compiled in.
  2376. * @param mixed $mixedKeyWrapperHtml key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  2377. * @return string the result of operation.
  2378. */
  2379. function _compileContent($sContent, $aVarName, $iVarDepth, $aVarValues, $mixedKeyWrapperHtml = null)
  2380. {
  2381. $aKeys = array_keys($aVarValues);
  2382. $aValues = array_values($aVarValues);
  2383. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  2384. for($i = 0; $i < count($aKeys); $i++) {
  2385. if(strpos($aKeys[$i], 'bx_repeat:') === 0) {
  2386. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  2387. $aMatches = array();
  2388. preg_match($sKey, $sContent, $aMatches);
  2389. $sValue = '';
  2390. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  2391. if(empty($aValues[$i]) || !is_array($aValues[$i]))
  2392. return false;
  2393. $sIndex = "\$" . str_repeat("i", $iVarDepth);
  2394. $sValue .= "<?php if(is_array(" . $aVarName . "['" . $aKeys[$i] . "'])) for(" . $sIndex . "=0; " . $sIndex . "<count(" . $aVarName . "['" . $aKeys[$i] . "']); " . $sIndex . "++){ ?>";
  2395. if(($sInnerValue = $this->_compileContent($aMatches[1], $aVarName . "['" . $aKeys[$i] . "'][" . $sIndex . "]", $iVarDepth + 1, current($aValues[$i]), $mixedKeyWrapperHtml)) === false)
  2396. return false;
  2397. $sValue .= $sInnerValue;
  2398. $sValue .= "<?php } else if(is_string(" . $aVarName . "['" . $aKeys[$i] . "'])) echo " . $aVarName . "['" . $aKeys[$i] . "']; ?>";
  2399. }
  2400. } else if(strpos($aKeys[$i], 'bx_if:') === 0) {
  2401. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  2402. $aMatches = array();
  2403. preg_match($sKey, $sContent, $aMatches);
  2404. $sValue = '';
  2405. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  2406. if(!is_array($aValues[$i]) || !isset($aValues[$i]['content']) || empty($aValues[$i]['content']) || !is_array($aValues[$i]['content']))
  2407. return false;
  2408. $sValue .= "<?php if(" . $aVarName . "['" . $aKeys[$i] . "']['condition']){ ?>";
  2409. if(($sInnerValue = $this->_compileContent($aMatches[1], $aVarName . "['" . $aKeys[$i] . "']['content']", $iVarDepth, $aValues[$i]['content'], $mixedKeyWrapperHtml)) === false)
  2410. return false;
  2411. $sValue .= $sInnerValue;
  2412. $sValue .= "<?php } ?>";
  2413. }
  2414. } else {
  2415. $sKey = "'" . $aKeyWrappers['left'] . $aKeys[$i] . $aKeyWrappers['right'] . "'s";
  2416. $sValue = "<?php echo " . $aVarName . "['" . $aKeys[$i] . "'];?>";
  2417. }
  2418. $aKeys[$i] = $sKey;
  2419. $aValues[$i] = $sValue;
  2420. }
  2421. try {
  2422. $oTemplate = &$this;
  2423. $aCallbackPatterns = array(
  2424. "'<bx_include_auto:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BOTH,
  2425. "'<bx_include_auto_mod_general:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_general'),
  2426. "'<bx_include_auto_mod_profile:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_profile'),
  2427. "'<bx_include_auto_mod_group:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_group'),
  2428. "'<bx_include_auto_mod_text:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_text'),
  2429. "'<bx_include_base:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BASE,
  2430. "'<bx_include_base_mod_general:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_general'),
  2431. "'<bx_include_base_mod_profile:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_profile'),
  2432. "'<bx_include_base_mod_group:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_group'),
  2433. "'<bx_include_base_mod_text:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_text'),
  2434. "'<bx_include_tmpl:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_TMPL,
  2435. "'<bx_include_tmpl_mod_general:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_general'),
  2436. "'<bx_include_tmpl_mod_profile:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_profile'),
  2437. "'<bx_include_tmpl_mod_group:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_group'),
  2438. "'<bx_include_tmpl_mod_text:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_text')
  2439. );
  2440. foreach($aCallbackPatterns as $sPattern => $sCheckIn)
  2441. $sContent = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $aVarValues, $mixedKeyWrapperHtml, $sCheckIn) {
  2442. $mixedResult = $oTemplate->getCached($aMatches[1], $aVarValues, $mixedKeyWrapperHtml, $sCheckIn, false);
  2443. if($mixedResult === false)
  2444. throw new Exception('Unable to create cache file.');
  2445. return $mixedResult;
  2446. }, $sContent);
  2447. $sContent = $this->_parseContentKeys($sContent);
  2448. }
  2449. catch(Exception $oException) {
  2450. return false;
  2451. }
  2452. $aKeys = array_merge($aKeys, array(
  2453. "'<bx_menu:([^\s]+) \/>'s",
  2454. "'<bx_url_root />'",
  2455. "'<bx_url_studio />'"
  2456. ));
  2457. $aValues = array_merge($aValues, array(
  2458. "<?php echo \$this->getMenu('\\1'); ?>",
  2459. BX_DOL_URL_ROOT,
  2460. BX_DOL_URL_STUDIO
  2461. ));
  2462. //--- Parse Predefined Keys ---//
  2463. $sContent = preg_replace($aKeys, $aValues, $sContent);
  2464. //--- Parse System Keys ---//
  2465. $sContent = preg_replace( "'" . $aKeyWrappers['left'] . "([a-zA-Z0-9_-]+)" . $aKeyWrappers['right'] . "'", "<?php echo \$this->parseSystemKey('\\1', \$mixedKeyWrapperHtml);?>", $sContent);
  2466. return $sContent;
  2467. }
  2468. protected function _parseContentKeys($sContent, $aCallbackPatterns = array())
  2469. {
  2470. $oTemplate = &$this;
  2471. $aCallbackPatterns = array_merge($aCallbackPatterns, array(
  2472. "'<bx_image_url:([^\s]+) \/>'s" => "get_image_url",
  2473. "'<bx_icon_url:([^\s]+) \/>'s" => "get_icon_url",
  2474. "'<bx_text:([_\{\}\w\d\s]+[^\s]{1}) \/>'s" => "get_text",
  2475. "'<bx_text_js:([^\s]+) \/>'s" => "get_text_js",
  2476. "'<bx_text_attribute:([^\s]+) \/>'s" => "get_text_attribute",
  2477. ));
  2478. foreach($aCallbackPatterns as $sPattern => $sAction)
  2479. $sContent = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $sAction) {
  2480. $sResult = '';
  2481. switch($sAction) {
  2482. case 'get_image_url':
  2483. $sResult = $oTemplate->getImageUrl($aMatches[1]);
  2484. break;
  2485. case 'get_icon_url':
  2486. $sResult = $oTemplate->getIconUrl($aMatches[1]);
  2487. break;
  2488. case 'get_text':
  2489. $sResult = _t($aMatches[1]);
  2490. break;
  2491. case 'get_text_js':
  2492. $sResult = bx_js_string(_t($aMatches[1]));
  2493. break;
  2494. case 'get_text_attribute':
  2495. $sResult = bx_html_attribute(_t($aMatches[1]));
  2496. break;
  2497. case 'get_injection':
  2498. $sResult = $oTemplate->processInjection($oTemplate->getPageNameIndex(), $aMatches[1]);
  2499. break;
  2500. case 'get_menu':
  2501. $sResult = $oTemplate->getMenu($aMatches[1]);
  2502. break;
  2503. }
  2504. return $sResult;
  2505. }, $sContent);
  2506. return $sContent;
  2507. }
  2508. /**
  2509. * Get absolute location of some template's part.
  2510. *
  2511. * @param string $sType - result type. Available values 'url' and 'path'.
  2512. * @param string $sFolder - folders to be searched in. @see $_sFolderHtml, $_sFolderCss, $_sFolderImages and $_sFolderIcons
  2513. * @param string $sName - requested part name.
  2514. * @param string $sCheckIn where the content would be searched(base, template, both)
  2515. * @return string absolute location (path/url) of the part.
  2516. */
  2517. function _getAbsoluteLocation($sType, $sFolder, $sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  2518. {
  2519. $sDirectory = $this->getPath();
  2520. if($sType == 'path') {
  2521. $sDivider = DIRECTORY_SEPARATOR;
  2522. $sRoot = BX_DIRECTORY_PATH_ROOT;
  2523. } else if($sType == 'url') {
  2524. $sDivider = '/';
  2525. $sRoot = BX_DOL_URL_ROOT;
  2526. }
  2527. if(strpos($sName,'|') !== false) {
  2528. $aParts = explode('|', $sName);
  2529. $sName = $aParts[1];
  2530. if(strpos($aParts[0],'@') !== false) {
  2531. $aLocationParts = explode('@', $aParts[0]);
  2532. $sLocationKey = $this->addLocation($aLocationParts[0], BX_DIRECTORY_PATH_ROOT . $aLocationParts[1], BX_DOL_URL_ROOT . $aLocationParts[1]);
  2533. }
  2534. }
  2535. $sResult = '';
  2536. $aLocations = array_reverse($this->_aLocations, true);
  2537. //--- Check it Template.
  2538. $bInSub = false;
  2539. $aCheckIn = [BX_DOL_TEMPLATE_CHECK_IN_BOTH, BX_DOL_TEMPLATE_CHECK_IN_TMPL];
  2540. if(in_array($sCheckIn, $aCheckIn) || $bInSub = (isset($sCheckIn['in'], $sCheckIn['sub']) && in_array($sCheckIn['in'], $aCheckIn)))
  2541. foreach($aLocations as $sKey => $aLocation)
  2542. if((!$bInSub || $sCheckIn['sub'] == $sKey) && extFileExists(BX_DIRECTORY_PATH_MODULES . $this->getPath(). 'data' . DIRECTORY_SEPARATOR . BX_DOL_TEMPLATE_FOLDER_ROOT . DIRECTORY_SEPARATOR . $sKey . DIRECTORY_SEPARATOR . $sFolder . $sName)) {
  2543. $sResult = $sRoot . 'modules' . $sDivider . $sDirectory. 'data' . $sDivider . BX_DOL_TEMPLATE_FOLDER_ROOT . $sDivider . $sKey . $sDivider . $sFolder . $sName;
  2544. break;
  2545. }
  2546. //--- Check it Base.
  2547. $bInSub = false;
  2548. $aCheckIn = [BX_DOL_TEMPLATE_CHECK_IN_BOTH, BX_DOL_TEMPLATE_CHECK_IN_BASE];
  2549. if(empty($sResult) && (in_array($sCheckIn, $aCheckIn) || $bInSub = (isset($sCheckIn['in'], $sCheckIn['sub']) && in_array($sCheckIn['in'], $aCheckIn))))
  2550. foreach($aLocations as $sKey => $aLocation)
  2551. if((!$bInSub || $sCheckIn['sub'] == $sKey) && extFileExists($aLocation['path'] . BX_DOL_TEMPLATE_FOLDER_ROOT . DIRECTORY_SEPARATOR . $sFolder . $sName)) {
  2552. $sResult = $aLocation[$sType] . BX_DOL_TEMPLATE_FOLDER_ROOT . $sDivider . $sFolder . $sName;
  2553. break;
  2554. }
  2555. /**
  2556. * try to find from received path
  2557. */
  2558. if(!$sResult && @is_file(BX_DIRECTORY_PATH_ROOT . $aParts[0] . DIRECTORY_SEPARATOR . $aParts[1])) {
  2559. $sResult = $sRoot . $aParts[0] . $sDivider . $aParts[1];
  2560. }
  2561. if(isset($sLocationKey))
  2562. $this->removeLocation($sLocationKey);
  2563. return $sType == 'path' && !empty($sResult) ? realpath($sResult) : $sResult;
  2564. }
  2565. /**
  2566. * Get absolute location of some template's part.
  2567. *
  2568. * @param string $sType result type. Available values 'url' and 'path'.
  2569. * @param string $sName requested part name.
  2570. * @return string absolute location (path/url) of the part.
  2571. */
  2572. function _getAbsoluteLocationJs($sType, $sName)
  2573. {
  2574. $sResult = '';
  2575. $aLocations = array_reverse($this->_aLocationsJs, true);
  2576. foreach($aLocations as $sKey => $aLocation) {
  2577. if(extFileExists($aLocation['path'] . $sName))
  2578. $sResult = $aLocation[$sType] . $sName;
  2579. else
  2580. continue;
  2581. break;
  2582. }
  2583. return $sType == 'path' && !empty($sResult) ? realpath($sResult) : $sResult;
  2584. }
  2585. function _getAbsoluteLocationCss($sType, $sName)
  2586. {
  2587. $sNameLess = str_replace('.css', '.less', $sName);
  2588. $sResult = $this->_getAbsoluteLocation($sType, $this->_sFolderCss, $sNameLess);
  2589. if(!empty($sResult))
  2590. return $sResult;
  2591. return $this->_getAbsoluteLocation($sType, $this->_sFolderCss, $sName);
  2592. }
  2593. /**
  2594. * Get inline data for Images and Icons.
  2595. *
  2596. * @param string $sType image/icon
  2597. * @param string $sName file name
  2598. * @param string $sCheckIn where the content would be searched(base, template, both)
  2599. * @return unknown
  2600. */
  2601. function _getInlineData($sType, $sName, $sCheckIn)
  2602. {
  2603. switch($sType) {
  2604. case 'image':
  2605. $sFolder = $this->_sFolderImages;
  2606. break;
  2607. case 'icon':
  2608. $sFolder = $this->_sFolderIcons;
  2609. break;
  2610. }
  2611. $sPath = $this->_getAbsoluteLocation('path', $sFolder, $sName, $sCheckIn);
  2612. $iFileSize = 0;
  2613. if($this->_bImagesInline && ($iFileSize = filesize($sPath)) !== false && $iFileSize < $this->_iImagesMaxSize) {
  2614. $aFileInfo = pathinfo($sPath);
  2615. return $this->getImageMimeType($aFileInfo['extension']) . ";base64," . base64_encode(file_get_contents($sPath));
  2616. }
  2617. return false;
  2618. }
  2619. /**
  2620. * Get file name where the template would be cached.
  2621. *
  2622. * @param string $sAbsolutePath template's real path.
  2623. * @return string the result of operation.
  2624. */
  2625. function _getCacheFileName($sType, $sAbsolutePath)
  2626. {
  2627. $sResult = bx_site_hash($sAbsolutePath);
  2628. switch($sType) {
  2629. case 'html':
  2630. $sResult = $this->_sCacheFilePrefix . bx_lang_name() . '_' . $this->_sCode . '_' . $sResult;
  2631. break;
  2632. case 'css':
  2633. $sResult = $this->_sCssCachePrefix . (!empty($this->_iMix) ? $this->_iMix . '_' : '') . $sResult;
  2634. break;
  2635. case 'js':
  2636. $sResult = $this->_sJsCachePrefix . $sResult;
  2637. break;
  2638. }
  2639. return $sResult;
  2640. }
  2641. /**
  2642. * Get template key wrappers(left, right)
  2643. *
  2644. * @param mixed $mixedKeyWrapperHtml key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  2645. * @return array result of operation.
  2646. */
  2647. function _getKeyWrappers($mixedKeyWrapperHtml)
  2648. {
  2649. $aResult = array();
  2650. if(!empty($mixedKeyWrapperHtml) && is_string($mixedKeyWrapperHtml))
  2651. $aResult = array('left' => $mixedKeyWrapperHtml, 'right' => $mixedKeyWrapperHtml);
  2652. else if(!empty($mixedKeyWrapperHtml) && is_array($mixedKeyWrapperHtml))
  2653. $aResult = array('left' => $mixedKeyWrapperHtml[0], 'right' => $mixedKeyWrapperHtml[1]);
  2654. else
  2655. $aResult = array('left' => $this->_sKeyWrapperHtml, 'right' => $this->_sKeyWrapperHtml);
  2656. return $aResult;
  2657. }
  2658. /**
  2659. * Process all added language translations and return them as a string.
  2660. *
  2661. * @return string with JS code.
  2662. */
  2663. function _processJsTranslations()
  2664. {
  2665. $sReturn = '';
  2666. if(isset($this->aPage['js_translations']) && is_array($this->aPage['js_translations'])) {
  2667. foreach($this->aPage['js_translations'] as $sKey => $sString)
  2668. $sReturn .= "'" . bx_js_string($sKey) . "': '" . bx_js_string($sString) . "',";
  2669. $sReturn = substr($sReturn, 0, -1);
  2670. }
  2671. return '
  2672. <script language="javascript">
  2673. if (\'undefined\' === typeof(aDolLang))
  2674. var aDolLang = {' . $sReturn . '};
  2675. else
  2676. $.extend(aDolLang, {' . $sReturn . '});
  2677. </script>';
  2678. }
  2679. /**
  2680. * Process all added options and return them as a string.
  2681. *
  2682. * @return string with JS code.
  2683. */
  2684. function _processJsOptions()
  2685. {
  2686. $sReturn = '';
  2687. if(isset($this->aPage['js_options']) && is_array($this->aPage['js_options'])) {
  2688. foreach($this->aPage['js_options'] as $sName => $mixedValue)
  2689. $sReturn .= "'" . bx_js_string($sName) . "': '" . bx_js_string($mixedValue) . "',";
  2690. $sReturn = substr($sReturn, 0, -1);
  2691. }
  2692. return '<script language="javascript">var aDolOptions = {' . $sReturn . '};</script>';
  2693. }
  2694. /**
  2695. * Process all added images and return them as a string.
  2696. *
  2697. * @return string with JS code.
  2698. */
  2699. function _processJsImages()
  2700. {
  2701. $sReturn = '';
  2702. if(isset($this->aPage['js_images']) && is_array($this->aPage['js_images'])) {
  2703. foreach($this->aPage['js_images'] as $sKey => $sUrl)
  2704. $sReturn .= "'" . bx_js_string($sKey) . "': '" . bx_js_string($sUrl) . "',";
  2705. $sReturn = substr($sReturn, 0, -1);
  2706. }
  2707. return '<script language="javascript">var aDolImages = {' . $sReturn . '};</script>';
  2708. }
  2709. /**
  2710. * Get Gzip loader URL.
  2711. *
  2712. * @param $sType content type CSS/JS
  2713. * @param $sName file name.
  2714. * @return string with URL
  2715. */
  2716. function _getLoaderUrl($sType, $sName)
  2717. {
  2718. return BX_DOL_URL_ROOT . 'gzip_loader.php?file=' . $sName . '.' . $sType;
  2719. }
  2720. /**
  2721. * Get current revision number.
  2722. *
  2723. * @return integer number
  2724. */
  2725. function _getRevision()
  2726. {
  2727. return (int)getParam('sys_revision');
  2728. }
  2729. /**
  2730. *
  2731. * Functions to display pages with errors, messages and so on.
  2732. *
  2733. */
  2734. function displayAccessDenied ($sMsg = '', $iPage = BX_PAGE_DEFAULT, $iDesignBox = BX_DB_PADDING_DEF)
  2735. {
  2736. bx_import('BxDolLanguages');
  2737. header('HTTP/1.0 403 Forbidden');
  2738. header('Status: 403 Forbidden');
  2739. $this->displayMsg($sMsg ? $sMsg : _t('_Access denied'), false, $iPage, $iDesignBox);
  2740. }
  2741. function displayNoData ($sMsg = '', $iPage = BX_PAGE_DEFAULT, $iDesignBox = BX_DB_PADDING_DEF)
  2742. {
  2743. bx_import('BxDolLanguages');
  2744. header('HTTP/1.0 204 No Content');
  2745. header('Status: 204 No Content');
  2746. $this->displayMsg($sMsg ? $sMsg : _t('_Empty'), false, $iPage, $iDesignBox);
  2747. }
  2748. function displayErrorOccured ($sMsg = '', $iPage = BX_PAGE_DEFAULT, $iDesignBox = BX_DB_PADDING_DEF)
  2749. {
  2750. bx_import('BxDolLanguages');
  2751. header('HTTP/1.0 500 Internal Server Error');
  2752. header('Status: 500 Internal Server Error');
  2753. $this->displayMsg($sMsg ? $sMsg : _t('_error occured'), false, $iPage, $iDesignBox);
  2754. }
  2755. function displayPageNotFound ($sMsg = '', $iPage = BX_PAGE_DEFAULT, $iDesignBox = BX_DB_PADDING_DEF)
  2756. {
  2757. bx_import('BxDolLanguages');
  2758. header('HTTP/1.0 404 Not Found');
  2759. header('Status: 404 Not Found');
  2760. $this->displayMsg($sMsg ? $sMsg : _t('_sys_request_page_not_found_cpt'), false, $iPage, $iDesignBox);
  2761. }
  2762. function displayMsg ($s, $bTranslate = false, $iPage = BX_PAGE_DEFAULT, $iDesignBox = BX_DB_PADDING_DEF)
  2763. {
  2764. $sError = '_Error';
  2765. $bArray = is_array($s);
  2766. $sTitle = $bArray ? $s['title'] : ($bTranslate ? $sError : _t($sError));
  2767. $sContent = $bArray ? $s['content'] : $s;
  2768. if($bTranslate) {
  2769. $sTitle = _t($sTitle);
  2770. $sContent = _t($sContent);
  2771. }
  2772. $sContent = MsgBox($sContent);
  2773. if($iPage == BX_PAGE_DEFAULT)
  2774. $sContent = DesignBoxContent($sTitle, $sContent, $iDesignBox);
  2775. $oTemplate = BxDolTemplate::getInstance();
  2776. $oTemplate->setPageNameIndex ($iPage);
  2777. $oTemplate->setPageHeader ($sTitle);
  2778. $oTemplate->setPageContent ('page_main_code', $sContent);
  2779. $oTemplate->getPageCode();
  2780. exit;
  2781. }
  2782. /**
  2783. * * * * Static methods for work with template injections * * *
  2784. *
  2785. * Static method is used to add/replace the content of some key in the template.
  2786. * It's usefull when you don't want to modify existing template but need to add some data to existing template key.
  2787. *
  2788. * @param integer $iPageIndex - page index where injections would processed. Use 0 if you want it to be done on all the pages.
  2789. * @param string $sKey - template key.
  2790. * @param string $sValue - the data to be added.
  2791. * @return string the result of operation.
  2792. */
  2793. function processInjection($iPageIndex, $sKey, $sValue = "")
  2794. {
  2795. if($iPageIndex != 0 && isset($this->aPage['injections']['page_0'][$sKey]) && isset($this->aPage['injections']['page_' . $iPageIndex][$sKey]))
  2796. $aSelection = @array_merge($this->aPage['injections']['page_0'][$sKey], $this->aPage['injections']['page_' . $iPageIndex][$sKey]);
  2797. else if(isset($this->aPage['injections']['page_0'][$sKey]))
  2798. $aSelection = $this->aPage['injections']['page_0'][$sKey];
  2799. else if(isset($this->aPage['injections']['page_' . $iPageIndex][$sKey]))
  2800. $aSelection = $this->aPage['injections']['page_' . $iPageIndex][$sKey];
  2801. else
  2802. $aSelection = array();
  2803. if(is_array($aSelection))
  2804. foreach($aSelection as $aInjection) {
  2805. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginInjection($sRand = time().rand());
  2806. $sInjData = '';
  2807. switch($aInjection['type']) {
  2808. case 'text':
  2809. $sInjData = $aInjection['data'];
  2810. break;
  2811. case 'service':
  2812. if(BxDolService::isSerializedService($aInjection['data']))
  2813. $sInjData = BxDolService::callSerialized($aInjection['data']);
  2814. break;
  2815. }
  2816. if((int)$aInjection['replace'] == 1)
  2817. $sValue = $sInjData;
  2818. else
  2819. $sValue .= $sInjData;
  2820. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endInjection($sRand, $aInjection);
  2821. }
  2822. return $sValue != '__' . $sKey . '__' ? str_replace('__' . $sKey . '__', '', $sValue) : $sValue;
  2823. }
  2824. /**
  2825. * Static method to add ingection available on the current page only.
  2826. *
  2827. * @param string $sKey - template's key.
  2828. * @param string $sType - injection type(text, php).
  2829. * @param string $sData - the data to be added.
  2830. * @param integer $iReplace - replace already existed data or not.
  2831. */
  2832. function addInjection($sKey, $sType, $sData, $iReplace = 0)
  2833. {
  2834. $this->aPage['injections']['page_0'][$sKey][] = array(
  2835. 'page_index' => 0,
  2836. 'key' => $sKey,
  2837. 'type' => $sType,
  2838. 'data' => $sData,
  2839. 'replace' => $iReplace
  2840. );
  2841. }
  2842. function getPageCode($oTemplate = null)
  2843. {
  2844. if (empty($oTemplate))
  2845. $oTemplate = $this;
  2846. $oZ = new BxDolAlerts('system', 'design_before_output', 0, 0, array('page' => &$this->aPage, 'page_content' => &$this->aPageContent));
  2847. $oZ->alert();
  2848. header( 'Content-type: text/html; charset=utf-8' );
  2849. header( 'X-Frame-Options: sameorigin' );
  2850. echo $oTemplate->parsePageByName('page_' . $oTemplate->getPageNameIndex() . '.html', $oTemplate->getPageContent());
  2851. }
  2852. }
  2853. /** @} */