BxDolTemplate.php 103 KB

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