BxDolTemplate.php 121 KB

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