BxDolTemplate.php 102 KB

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