BxDolTemplate.php 97 KB

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