BxDolTemplate.php 103 KB

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