BxDolTemplate.php 103 KB

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