BxDolTemplate.php 118 KB

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