BxDolTemplate.php 119 KB

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