BxDolTemplate.php 101 KB

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