BxDolTemplate.php 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. ini_set('pcre.backtrack_limit', 1000000);
  10. define('BX_DOL_TEMPLATE_DEFAULT_CODE', 'uni');
  11. define('BX_DOL_TEMPLATE_FOLDER_ROOT', 'template');
  12. define('BX_DOL_TEMPLATE_INJECTIONS_CACHE', 'sys_injections.inc');
  13. define('BX_DOL_TEMPLATE_CHECK_IN_BOTH', 'both');
  14. define('BX_DOL_TEMPLATE_CHECK_IN_BASE', 'base');
  15. define('BX_DOL_TEMPLATE_CHECK_IN_TMPL', 'tmpl');
  16. define('BX_PAGE_DEFAULT', 0); ///< default, regular page
  17. define('BX_PAGE_CLEAR', 2); ///< clear page, without any headers and footers
  18. define('BX_PAGE_POPUP', 44); ///< popup page, without any headers and footers
  19. define('BX_PAGE_TRANSITION', 150); ///< transition page with redirect to display some msg, like 'please wait', without headers footers
  20. /**
  21. * Template engine.
  22. *
  23. * An object of the class allows to:
  24. * 1. Manage HTML templates.
  25. * 2. Get URL/path for any template image/icon.
  26. * 3. Attach CSS/JavaScript files to the output.
  27. * 4. Add some content to any template key using Injection engine.
  28. *
  29. *
  30. * Avalable constructions.
  31. * 1. <bx_include_auto:template_name.html /> - the content of the file would be inserted. File would be taken from current template if it existes there, and from base directory otherwise.
  32. * 2. <bx_include_base:template_name.html /> - the content of the file would be inserted. File would be taken from base directory.
  33. * 3. <bx_include_tmpl:template_name.html /> - the content of the file would be inserted. File would be taken from tmpl_xxx directory.
  34. * 4. <bx_url_root /> - the value of BX_DOL_URL_ROOT variable will be inserted.
  35. * 5. <bx_url_admin /> - the value of BX_DOL_URL_ADMIN variable will be inserted.
  36. * 6. <bx_text:_language_key /> - _language_key will be translated using language file(function _t()) and inserted.
  37. <bx_text_js:_language_key /> - _language_key will be translated using language file(function _t()) and inserted, use it to insert text into js string.
  38. <bx_text_attribute:_language_key /> - _language_key will be translated using language file(function _t()) and inserted, use it to insert text into html attribute.
  39. * 7. <bx_image_url:image_file_name /> - image with 'image_file_name' file name will be searched in the images folder of current template.
  40. * 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.
  41. * 8. <bx_icon_url:icon_file_name /> - the same with <bx_image_url:image_file_name />, but icons will be searched in the images/icons/ folders.
  42. * 9. <bx_injection:injection_name /> - will be replaced with injections registered with the page and injection_name in the `sys_injections`/`sys_injections_admin`/ tables.
  43. * 10. <bx_if:tag_name>some_HTML</bx_if:tag_name> - will be replaced with provided content if the condition is true, and with empty string otherwise.
  44. * 11. <bx_repeat:cycle_name>some_HTML</bx_repeat:cycle_name> - an inner HTML content will be repeated in accordance with received data.
  45. *
  46. *
  47. * Related classes:
  48. * BxDolTemplateAdmin - for processing admin templates.
  49. * Template classes in modules - for processing modiles' templates.
  50. *
  51. *
  52. * Global variables:
  53. * oSysTemplate - is used for template processing in user part.
  54. * oAdmTemplate - is used for template processing in admin part.
  55. *
  56. *
  57. * Add injection:
  58. * 1. Register it in the `sys_injections` table or `sys_injections_admin` table for admin panel.
  59. * 2. Clear injections cache(sys_injections.inc and sys_injections_admin.inc in cache folder).
  60. *
  61. *
  62. * Predefined template keys to add injections:
  63. * 1. injection_head - add injections in the <head> tag.
  64. * 2. injection_body - add ingection(attribute) in the <body> tag.
  65. * 3. injection_header - add injection inside the <body> tag at the very beginning.
  66. * 4. injection_logo_before - add injection at the left of the main logo(inside logo's DIV).
  67. * 5. injection_logo_after - add injection at the right of the main logo(inside logo's DIV).
  68. * 6. injection_between_logo_top_menu - add injection between logo and top menu.
  69. * 7. injection_top_menu_before - add injection at the left of the top menu(inside top menu's DIV).
  70. * 8. injection_top_menu_after - add injection at the right of the top menu(inside top menu's DIV).
  71. * 13. injection_content_before - add injection just before main content(inside content's DIV).
  72. * 14. injection_content_after - add injection just after main content(inside content's DIV).
  73. * 15. injection_between_content_footer - add injection between content and footer.
  74. * 16. injection_footer_before - add injection at the left of the footer(inside footer's DIV).
  75. * 17. injection_footer_after - add injection at the right of the footer(inside footer's DIV).
  76. * 18. injection_footer - add injection inside the <body> tag at the very end.
  77. *
  78. *
  79. * Example of usage:
  80. * @code
  81. * $oSysTemplate = BxDolTemplate::getInstance();
  82. *
  83. * $oSysTemplate->addCss(array('test1.css', 'test2.css'));
  84. * $oSysTemplate->addJs(array('test1.js', 'test2.js'));
  85. * $oSysTemplate->parseHtmlByName('messageBox.html', array(
  86. * 'id' => $iId,
  87. * 'msgText' => $sText,
  88. * 'bx_if:timer' => array(
  89. * 'condition' => $iTimer > 0,
  90. * 'content' => array(
  91. * 'id' => $iId,
  92. * 'time' => 1000 * $iTimer,
  93. * 'on_close' => $sOnClose,
  94. * )
  95. * ),
  96. * 'bx_if:timer' => array(
  97. * array(
  98. * 'name' => $sName,
  99. * 'title' => $sTitle
  100. * ),
  101. * array(
  102. * 'name' => $sName,
  103. * 'title' => $sTitle
  104. * )
  105. * )
  106. * ));
  107. * @endcode
  108. *
  109. *
  110. * Memberships/ACL:
  111. * Doesn't depend on user's membership.
  112. *
  113. *
  114. * Alerts:
  115. * no alerts available
  116. *
  117. */
  118. class BxDolTemplate extends BxDol implements iBxDolSingleton {
  119. /**
  120. * Main fields
  121. */
  122. protected $_sPrefix;
  123. protected $_sRootPath;
  124. protected $_sRootUrl;
  125. protected $_sSubPath;
  126. protected $_sInjectionsTable;
  127. protected $_sInjectionsCache;
  128. protected $_sCode;
  129. protected $_sCodeKey;
  130. protected $_sKeyWrapperHtml;
  131. protected $_sFolderHtml;
  132. protected $_sFolderCss;
  133. protected $_sFolderImages;
  134. protected $_sFolderIcons;
  135. protected $_aTemplates;
  136. protected $_aLocations;
  137. protected $_aLocationsJs;
  138. /**
  139. * Cache related fields
  140. */
  141. protected $_bCacheEnable;
  142. protected $_sCacheFolderUrl;
  143. protected $_sCachePublicFolderUrl;
  144. protected $_sCachePublicFolderPath;
  145. protected $_sCacheFilePrefix;
  146. protected $_bImagesInline;
  147. protected $_iImagesMaxSize;
  148. protected $_bCssCache;
  149. protected $_bCssArchive;
  150. protected $_sCssCachePrefix;
  151. protected $_bJsCache;
  152. protected $_bJsArchive;
  153. protected $_sJsCachePrefix;
  154. /**
  155. * Less related fields
  156. */
  157. protected $_bLessEnable;
  158. protected $_sLessCachePrefix;
  159. /**
  160. * Minify related fields
  161. */
  162. protected $_bMinifyEnable;
  163. protected $aPage;
  164. protected $aPageContent;
  165. protected $_oConfigTemplate;
  166. /**
  167. * Constructor
  168. */
  169. protected function __construct($sRootPath = BX_DIRECTORY_PATH_ROOT, $sRootUrl = BX_DOL_URL_ROOT) {
  170. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  171. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  172. parent::__construct();
  173. $this->_sPrefix = 'BxDolTemplate';
  174. $this->_sRootPath = $sRootPath;
  175. $this->_sRootUrl = $sRootUrl;
  176. $this->_sInjectionsTable = 'sys_injections';
  177. $this->_sInjectionsCache = BX_DOL_TEMPLATE_INJECTIONS_CACHE;
  178. $this->_sCodeKey = 'skin';
  179. $sCode = getParam('template');
  180. if(empty($sCode))
  181. $sCode = BX_DOL_TEMPLATE_DEFAULT_CODE;
  182. $this->_checkCode($sCode, false);
  183. //--- Check selected template in COOKIE(the lowest priority) ---//
  184. $sCode = !empty($_COOKIE[$this->_sCodeKey]) ? $_COOKIE[$this->_sCodeKey] : '';
  185. $this->_checkCode($sCode, false);
  186. //--- Check selected template in GET(the highest priority) ---//
  187. $sCode = !empty($_GET[$this->_sCodeKey]) ? $_GET[$this->_sCodeKey] : '';
  188. $this->_checkCode($sCode, true);
  189. if(isset($_GET[$this->_sCodeKey])) {
  190. bx_import('BxDolPermalinks');
  191. if(BxDolPermalinks::getInstance()->redirectIfNecessary(array($this->_sCodeKey)))
  192. exit;
  193. }
  194. $this->_sKeyWrapperHtml = '__';
  195. $this->_sFolderHtml = '';
  196. $this->_sFolderCss = 'css/';
  197. $this->_sFolderImages = 'images/';
  198. $this->_sFolderIcons = 'images/icons/';
  199. $this->_aTemplates = array();
  200. $this->addLocation('system', $this->_sRootPath, $this->_sRootUrl);
  201. $this->addLocationJs('system_inc_js', BX_DIRECTORY_PATH_INC . 'js/' , BX_DOL_URL_ROOT . 'inc/js/');
  202. $this->addLocationJs('system_inc_js_classes', BX_DIRECTORY_PATH_INC . 'js/classes/' , BX_DOL_URL_ROOT . 'inc/js/classes/');
  203. $this->addLocationJs('system_plugins_public', BX_DIRECTORY_PATH_PLUGINS_PUBLIC, BX_DOL_URL_PLUGINS_PUBLIC);
  204. $this->_bCacheEnable = !defined('BX_DOL_CRON_EXECUTE') && getParam('sys_template_cache_enable') == 'on';
  205. $this->_sCacheFolderUrl = '';
  206. $this->_sCachePublicFolderUrl = BX_DOL_URL_CACHE_PUBLIC;
  207. $this->_sCachePublicFolderPath = BX_DIRECTORY_PATH_CACHE_PUBLIC;
  208. $this->_sCacheFilePrefix = "bx_templ_";
  209. $this->_bImagesInline = getParam('sys_template_cache_image_enable') == 'on';
  210. $this->_iImagesMaxSize = (int)getParam('sys_template_cache_image_max_size') * 1024;
  211. $bArchive = getParam('sys_template_cache_compress_enable') == 'on';
  212. $this->_bCssCache = !defined('BX_DOL_CRON_EXECUTE') && getParam('sys_template_cache_css_enable') == 'on';
  213. $this->_bCssArchive = $this->_bCssCache && $bArchive;
  214. $this->_sCssCachePrefix = $this->_sCacheFilePrefix . 'css_';
  215. $this->_bJsCache = !defined('BX_DOL_CRON_EXECUTE') && getParam('sys_template_cache_js_enable') == 'on';
  216. $this->_bJsArchive = $this->_bJsCache && $bArchive;
  217. $this->_sJsCachePrefix = $this->_sCacheFilePrefix . 'js_';
  218. $this->_bLessEnable = true;
  219. $this->_sLessCachePrefix = $this->_sCacheFilePrefix . 'less_';
  220. $this->_bMinifyEnable = true;
  221. $this->aPage = array();
  222. $this->aPageContent = array();
  223. }
  224. /**
  225. * Prevent cloning the instance
  226. */
  227. public function __clone() {
  228. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  229. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  230. }
  231. /**
  232. * Get singleton instance of the class
  233. */
  234. public static function getInstance() {
  235. if(!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  236. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolTemplate();
  237. $GLOBALS['bxDolClasses'][__CLASS__]->init();
  238. }
  239. return $GLOBALS['bxDolClasses'][__CLASS__];
  240. }
  241. /**
  242. * Load templates.
  243. */
  244. function loadTemplates() {
  245. $aResult = array();
  246. foreach($this->_aTemplates as $sName)
  247. $aResult[$sName] = $this->getHtml($sName . '.html');
  248. $this->_aTemplates = $aResult;
  249. }
  250. /**
  251. * Initialize template engine.
  252. * Note. The method is executed with the system, you shouldn't execute it in your subclasses.
  253. */
  254. function init() {
  255. //--- Load page elements related static variables ---//
  256. $this->aPage = array(
  257. 'name_index' => BX_PAGE_DEFAULT,
  258. 'header' => '',
  259. 'header_text' => '',
  260. 'keywords' => array(),
  261. 'description' => '',
  262. 'robots' => '',
  263. 'css_name' => array(),
  264. 'css_compiled' => array(),
  265. 'css_system' => array(),
  266. 'js_name' => array(),
  267. 'js_compiled' => array(),
  268. 'js_system' => array(),
  269. 'js_options' => array(),
  270. 'js_translations' => array(),
  271. 'js_images' => array(),
  272. 'injections' => array()
  273. );
  274. //--- Load default CSS ---//
  275. $this->addCssSystem(array(
  276. 'common.css',
  277. 'default.less',
  278. 'general.css',
  279. 'icons.css',
  280. 'colors.css',
  281. 'forms.css',
  282. 'media-desktop.css',
  283. 'media-tablet.css',
  284. 'media-phone.css',
  285. 'media-print.css',
  286. ));
  287. //--- Load default JS ---//
  288. $this->addJsSystem(array(
  289. 'jquery/jquery.min.js',
  290. 'jquery/jquery-migrate.min.js',
  291. 'jquery-ui/jquery.ui.position.min.js',
  292. 'spin.min.js',
  293. 'jquery.easing.js',
  294. 'jquery.cookie.min.js',
  295. 'moment-with-langs.js',
  296. 'functions.js',
  297. 'jquery.webForms.js',
  298. 'jquery.dolPopup.js',
  299. ));
  300. //--- Load default JS ---//
  301. bx_import('BxDolLanguages');
  302. $this->addJsTranslation(array(
  303. '_are you sure?',
  304. '_error occured',
  305. '_sys_loading',
  306. ));
  307. //--- Load injection's cache ---//
  308. if (getParam('sys_db_cache_enable')) {
  309. $oDb = BxDolDb::getInstance();
  310. $oCache = $oDb->getDbCacheObject();
  311. $sCacheKey = $oDb->genDbCacheKey($this->_sInjectionsCache);
  312. $aInjections = $oCache->getData($sCacheKey);
  313. if ($aInjections === null) {
  314. $aInjections = $this->getInjectionsData();
  315. $oCache->setData ($sCacheKey, $aInjections);
  316. }
  317. } else {
  318. $aInjections = $this->getInjectionsData();
  319. }
  320. $this->aPage['injections'] = $aInjections;
  321. bx_import('BxTemplConfig');
  322. $this->_oConfigTemplate = BxTemplConfig::getInstance();
  323. }
  324. protected function getInjectionsData () {
  325. $oDb = BxDolDb::getInstance();
  326. $aInjections = $oDb->getAll("SELECT `page_index`, `name`, `key`, `type`, `data`, `replace` FROM `" . $this->_sInjectionsTable . "` WHERE `active`='1'");
  327. if (!$aInjections)
  328. return array();
  329. foreach ($aInjections as $aInjection)
  330. $aInjections['page_' . $aInjection['page_index']][$aInjection['key']][] = $aInjection;
  331. return $aInjections;
  332. }
  333. /**
  334. * Set page name index
  335. * @param int $i name index
  336. */
  337. function setPageNameIndex($i) {
  338. $this->aPage['name_index'] = $i;
  339. }
  340. /**
  341. * Get page name index
  342. * @return int $i name index
  343. */
  344. function getPageNameIndex() {
  345. return isset($this->aPage['name_index']) ? (int)$this->aPage['name_index'] : 0;
  346. }
  347. /**
  348. * Set page header
  349. * @param string $s page header
  350. */
  351. function setPageHeader($s) {
  352. $this->aPage['header'] = $s;
  353. }
  354. /**
  355. * Get page header
  356. * @return string $s page header
  357. */
  358. function getPageHeader() {
  359. return $this->aPage['header'];
  360. }
  361. /**
  362. * Set page params. Available page params are: name_index, header
  363. * @param array $a page params
  364. */
  365. function setPageParams($a) {
  366. if (!empty($this->aPage))
  367. $this->aPage = array_merge($this->aPage, $a);
  368. else
  369. $this->aPage = $a;
  370. }
  371. /**
  372. * Get page params.
  373. * @return array $a page params
  374. */
  375. function getPageParams() {
  376. return $this->aPage;
  377. }
  378. /**
  379. * Set page description.
  380. *
  381. * @param string $sDescription necessary page description.
  382. */
  383. function setPageDescription($sDescription) {
  384. $this->aPage['description'] = $sDescription;
  385. }
  386. /**
  387. * Set page meta robots.
  388. *
  389. * @param string $s page meta robots.
  390. */
  391. function setPageMetaRobots($s) {
  392. $this->aPage['robots'] = $s;
  393. }
  394. /**
  395. * Set page content for some variable.
  396. * @param string $sVar name of content variable
  397. * @param string $sContent content for $sVar variable
  398. * @param int $iIndex optional page index, default is index which was set before with @see setPageNameIndex function, or 0
  399. */
  400. function setPageContent($sVar, $sContent, $iIndex = false) {
  401. $i = false !== $iIndex ? $iIndex : $this->getPageNameIndex();
  402. $this->aPageContent[$i][$sVar] = $sContent;
  403. }
  404. /**
  405. * Get page content for some variable.
  406. * @param string $sVar name of content variable
  407. * @param int $iIndex optional page index, default is index which was set before with @see setPageNameIndex function, or 0
  408. * @return string page content for some variable or for the whole page.
  409. */
  410. function getPageContent($sVar = false, $iIndex = false) {
  411. $i = false !== $iIndex ? $iIndex : $this->getPageNameIndex();
  412. return false !== $sVar ? $this->aPageContent[$i][$sVar] : $this->aPageContent[$i];
  413. }
  414. /**
  415. * Get currently active template code.
  416. *
  417. * @return string template's code.
  418. */
  419. function getCode() {
  420. return $this->_sCode;
  421. }
  422. /**
  423. * Check whether code is associated with active template.
  424. *
  425. * @param string $sCode template's unique URI.
  426. * @param boolean $bSetCookie save code in COOKIE or not.
  427. */
  428. protected function _checkCode($sCode, $bSetCookie) {
  429. if(empty($sCode) || !preg_match('/^[A-Za-z0-9_-]+$/', $sCode))
  430. return;
  431. bx_import('BxDolModuleQuery');
  432. $aModule = BxDolModuleQuery::getInstance()->getModuleByUri($sCode);
  433. if(empty($aModule) || !is_array($aModule) || (int)$aModule['enabled'] != 1 || !file_exists($this->_sRootPath . 'modules/' . $aModule['path'] . 'data/template/'))
  434. return;
  435. bx_import('BxDolModuleConfig');
  436. $oConfig = new BxDolModuleConfig($aModule);
  437. $this->_sCode = $oConfig->getUri();
  438. $this->_sSubPath = $oConfig->getDirectory();
  439. if(!$bSetCookie || bx_get('preview'))
  440. return;
  441. $aUrl = parse_url(BX_DOL_URL_ROOT);
  442. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  443. setcookie( $this->_sCodeKey, $this->_sCode, time() + 60*60*24*365, $sPath);
  444. }
  445. /**
  446. * Get currently active template path.
  447. *
  448. * @return string template's path.
  449. */
  450. function getPath() {
  451. return $this->_sSubPath;
  452. }
  453. /**
  454. * Set page title.
  455. * @deprecated use setPageHeader
  456. *
  457. * @param string $sTitle necessary page title.
  458. */
  459. function setPageTitle($sTitle) {
  460. $this->setPageHeader($sTitle);
  461. }
  462. /**
  463. * Set page's main box title.
  464. * @deprecated use setPageParams
  465. *
  466. * @param string $sTitle necessary page's main box title.
  467. */
  468. function setPageMainBoxTitle($sTitle) {
  469. $this->setPageParams(array('header_text' => $sTitle));
  470. }
  471. /**
  472. * Add location in array of locations.
  473. * Note. Location is the path/url to folder where 'templates' folder is stored.
  474. *
  475. * @param string $sKey - location's unique key.
  476. * @param string $sLocationPath - location's path. For modules: '[path_to_dolphin]/modules/[vendor_name]/[module_name]/'
  477. * @param string $sLocationUrl - location's url. For modules: '[url_to_dolphin]/modules/[vendor_name]/[module_name]/'
  478. */
  479. function addLocation($sKey, $sLocationPath, $sLocationUrl) {
  480. $this->_aLocations[$sKey] = array(
  481. 'path' => $sLocationPath,
  482. 'url' => $sLocationUrl,
  483. );
  484. return $sKey;
  485. }
  486. /**
  487. * Add dynamic location.
  488. *
  489. * @todo SHOULD be removed.
  490. *
  491. * @param string $sLocationPath - location's path. For modules: '[path_to_dolphin]/modules/[vendor_name]/[module_name]/'
  492. * @param string $sLocationUrl - location's url. For modules: '[url_to_dolphin]/modules/[vendor_name]/[module_name]/'
  493. * @return location key. Is needed to remove the location.
  494. */
  495. function addDynamicLocation($sLocationPath, $sLocationUrl) {
  496. $sLocationKey = time();
  497. $this->addLocation($sLocationKey, $sLocationPath, $sLocationUrl);
  498. return $sLocationKey;
  499. }
  500. /**
  501. * Remove location from array of locations.
  502. * Note. Location is the path/url to folder where templates are stored.
  503. *
  504. * @param string $sKey - location's unique key.
  505. */
  506. function removeLocation($sKey) {
  507. if(isset($this->_aLocations[$sKey]))
  508. unset($this->_aLocations[$sKey]);
  509. }
  510. /**
  511. * Add JS location in array of JS locations.
  512. * Note. Location is the path/url to folder where JS files are stored.
  513. *
  514. * @param string $sKey - location's unique key.
  515. * @param string $sLocationPath - location's path. For modules: '[path_to_dolphin]/modules/[vendor_name]/[module_name]/js/'
  516. * @param string $sLocationUrl - location's url. For modules: '[url_to_dolphin]/modules/[vendor_name]/[module_name]/js/'
  517. */
  518. function addLocationJs($sKey, $sLocationPath, $sLocationUrl) {
  519. $this->_aLocationsJs[$sKey] = array(
  520. 'path' => $sLocationPath,
  521. 'url' => $sLocationUrl
  522. );
  523. }
  524. /**
  525. * Add dynamic JS location.
  526. *
  527. * @param string $sLocationPath - location's path. For modules: '[path_to_dolphin]/modules/[vendor_name]/[module_name]/'
  528. * @param string $sLocationUrl - location's url. For modules: '[url_to_dolphin]/modules/[vendor_name]/[module_name]/'
  529. * @return location key. Is needed to remove the location.
  530. */
  531. function addDynamicLocationJs($sLocationPath, $sLocationUrl) {
  532. $sLocationKey = time();
  533. $this->addLocationJs($sLocationKey, $sLocationPath, $sLocationUrl);
  534. return $sLocationKey;
  535. }
  536. /**
  537. * Remove JS location from array of locations.
  538. * Note. Location is the path/url to folder where templates are stored.
  539. *
  540. * @param string $sKey - JS location's unique key.
  541. */
  542. function removeLocationJs($sKey) {
  543. if(isset($this->_aLocationsJs[$sKey]))
  544. unset($this->_aLocationsJs[$sKey]);
  545. }
  546. /**
  547. * Add Option in JS output.
  548. *
  549. * @param mixed $mixedName option's name or an array of options' names.
  550. */
  551. function addJsOption($mixedName) {
  552. if(is_string($mixedName))
  553. $mixedName = array($mixedName);
  554. foreach($mixedName as $sName)
  555. $this->aPage['js_options'][$sName] = getParam($sName);
  556. }
  557. /**
  558. * Add language translation for key in JS output.
  559. *
  560. * @param mixed $mixedKey language key or an array of keys.
  561. */
  562. function addJsTranslation($mixedKey) {
  563. if(is_string($mixedKey))
  564. $mixedKey = array($mixedKey);
  565. foreach($mixedKey as $sKey)
  566. $this->aPage['js_translations'][$sKey] = _t($sKey, '{0}', '{1}');
  567. }
  568. /**
  569. * Add image in JS output.
  570. *
  571. * @param array $aImages an array of image descriptors.
  572. * The descriptor is a key/value pear in the array of descriptors.
  573. */
  574. function addJsImage($aImages) {
  575. if(!is_array($aImages))
  576. return;
  577. foreach($aImages as $sKey => $sFile) {
  578. $sUrl = $this->getImageUrl($sFile);
  579. if(empty($sUrl))
  580. continue;
  581. $this->aPage['js_images'][$sKey] = $sUrl;
  582. }
  583. }
  584. /**
  585. * Add icon in JS output.
  586. *
  587. * @param array $aIcons an array of icons descriptors.
  588. * The descriptor is a key/value pear in the array of descriptors.
  589. */
  590. function addJsIcon($aIcons) {
  591. if(!is_array($aIcons))
  592. return;
  593. foreach($aIcons as $sKey => $sFile) {
  594. $sUrl = $this->getIconUrl($sFile);
  595. if(empty($sUrl))
  596. continue;
  597. $this->aPage['js_images'][$sKey] = $sUrl;
  598. }
  599. }
  600. /**
  601. * Set page keywords.
  602. *
  603. * @param mixed $mixedKeywords necessary page keywords(string - single keyword, array - an array of keywords).
  604. * @param string $sDevider - string devider.
  605. */
  606. function addPageKeywords($mixedKeywords, $sDevider = ',') {
  607. if(is_string($mixedKeywords))
  608. $mixedKeywords = strpos($mixedKeywords, $sDevider) !== false ? explode($sDevider, $mixedKeywords) : array($mixedKeywords);
  609. foreach($mixedKeywords as $iKey => $sValue)
  610. $mixedKeywords[$iKey] = trim($sValue);
  611. $this->aPage['keywords'] = isset($this->aPage['keywords']) && is_array($this->aPage['keywords']) ? array_merge($this->aPage['keywords'], $mixedKeywords) : $mixedKeywords;
  612. }
  613. /**
  614. * Get template, which was loaded earlier.
  615. * @see method this->loadTemplates and field this->_aTemplates
  616. *
  617. * @param string $sName - template name.
  618. * @return string template's content.
  619. */
  620. function getTemplate($sName) {
  621. return $this->_aTemplates[$sName];
  622. }
  623. /**
  624. * Get full URL for the icon.
  625. *
  626. * @param string $sName icon's file name.
  627. * @param string $sCheckIn where the content would be searched(base, template, both)
  628. * @return string full URL.
  629. */
  630. function getIconUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH) {
  631. $sContent = "";
  632. if(($sContent = $this->_getInlineData('icon', $sName, $sCheckIn)) !== false)
  633. return $sContent;
  634. return $this->_getAbsoluteLocation('url', $this->_sFolderIcons, $sName, $sCheckIn);
  635. }
  636. /**
  637. * Get absolute Path for the icon.
  638. *
  639. * @param string $sName - icon's file name.
  640. * @param string $sCheckIn where the content would be searched(base, template, both)
  641. * @return string absolute path.
  642. */
  643. function getIconPath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH) {
  644. return $this->_getAbsoluteLocation('path', $this->_sFolderIcons, $sName, $sCheckIn);
  645. }
  646. /**
  647. * Get full URL for the image.
  648. *
  649. * @param string $sName - images's file name.
  650. * @param string $sCheckIn where the content would be searched(base, template, both)
  651. * @return string full URL.
  652. */
  653. function getImageUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH) {
  654. $sContent = "";
  655. if(($sContent = $this->_getInlineData('image', $sName, $sCheckIn)) !== false)
  656. return $sContent;
  657. return $this->_getAbsoluteLocation('url', $this->_sFolderImages, $sName, $sCheckIn);
  658. }
  659. /**
  660. * Get absolute Path for the image.
  661. *
  662. * @param string $sName - image's file name.
  663. * @param string $sCheckIn where the content would be searched(base, template, both)
  664. * @return string absolute path.
  665. */
  666. function getImagePath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH) {
  667. return $this->_getAbsoluteLocation('path', $this->_sFolderImages, $sName, $sCheckIn);
  668. }
  669. /**
  670. * Get full URL of CSS file.
  671. *
  672. * @param string $sName - CSS file name.
  673. * @param string $sCheckIn where the content would be searched(base, template, both)
  674. * @return string full URL.
  675. */
  676. function getCssUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH) {
  677. return $this->_getAbsoluteLocation('url', $this->_sFolderCss, $sName, $sCheckIn);
  678. }
  679. /**
  680. * Get full Path of CSS file.
  681. *
  682. * @param string $sName - CSS file name.
  683. * @param string $sCheckIn where the content would be searched(base, template, both)
  684. * @return string full URL.
  685. */
  686. function getCssPath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH) {
  687. return $this->_getAbsoluteLocation('path', $this->_sFolderCss, $sName, $sCheckIn);
  688. }
  689. /**
  690. * Get menu.
  691. * @param $s menu object name
  692. * @return html or empty string
  693. */
  694. function getMenu ($s) {
  695. bx_import('BxDolMenu');
  696. $oMenu = BxDolMenu::getObjectInstance($s);
  697. return $oMenu ? $oMenu->getCode () : '';
  698. }
  699. /**
  700. * Get content of HTML file.
  701. *
  702. * @param string $sName - HTML file name.
  703. * @param string $sCheckIn where the content would be searched(base, template, both)
  704. * @return string full content of the file and false on failure.
  705. */
  706. function getHtml($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH) {
  707. $sAbsolutePath = $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn);
  708. return !empty($sAbsolutePath) ? file_get_contents($sAbsolutePath) : false;
  709. }
  710. /**
  711. * Parse HTML template. Search for the template with accordance to it's file name.
  712. *
  713. * @see allows to use cache.
  714. *
  715. * @param string $sName - HTML file name.
  716. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  717. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(left, right) otherwise).
  718. * @param string $sCheckIn where the content would be searched(base, template, both)
  719. * @return string the result of operation.
  720. */
  721. function parseHtmlByName($sName, $aVariables, $mixedKeyWrapperHtml = null, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH) {
  722. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginTemplate($sName, $sRand = time().rand());
  723. if (($sContent = $this->getCached($sName, $aVariables, $mixedKeyWrapperHtml, $sCheckIn)) !== false) {
  724. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endTemplate($sName, $sRand, $sRet, true);
  725. return $sContent;
  726. }
  727. $sRet = '';
  728. if (($sContent = $this->getHtml($sName, $sCheckIn)) !== false)
  729. $sRet = $this->_parseContent($sContent, $aVariables, $mixedKeyWrapperHtml);
  730. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endTemplate($sName, $sRand, $sRet, false);
  731. return $sRet;
  732. }
  733. /**
  734. * Parse HTML template.
  735. *
  736. * @see Doesn't allow to use cache.
  737. *
  738. * @param string $sContent - HTML file content.
  739. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  740. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(left, right) otherwise).
  741. * @return string the result of operation.
  742. */
  743. function parseHtmlByContent($sContent, $aVariables, $mixedKeyWrapperHtml = null) {
  744. if(empty($sContent))
  745. return "";
  746. return $this->_parseContent($sContent, $aVariables, $mixedKeyWrapperHtml);
  747. }
  748. /**
  749. * Parse earlier loaded HTML template.
  750. *
  751. * @see Doesn't allow to use cache.
  752. *
  753. * @param string $sName - template name.
  754. * @param array $aVariables - key/value pairs. Key should be the same as template's key, excluding prefix and postfix.
  755. * @return string the result of operation.
  756. * @see $this->_aTemplates
  757. */
  758. function parseHtmlByTemplateName($sName, $aVariables, $mixedKeyWrapperHtml = null) {
  759. if(!isset($this->_aTemplates[$sName]) || empty($this->_aTemplates[$sName]))
  760. return "";
  761. return $this->_parseContent($this->_aTemplates[$sName], $aVariables, $mixedKeyWrapperHtml);
  762. }
  763. /**
  764. * Parse page HTML template. Search for the page's template with accordance to it's file name.
  765. *
  766. * @see allows to use cache.
  767. *
  768. * @param string $sName - HTML file name.
  769. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  770. * @return string the result of operation.
  771. */
  772. function parsePageByName($sName, $aVariables) {
  773. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPage($sName);
  774. $sContent = $this->parseHtmlByName($sName, $aVariables, $this->_sKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BOTH);
  775. if(empty($sContent))
  776. $sContent = $this->parseHtmlByName('default.html', $aVariables, $this->_sKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BOTH);
  777. //--- Add CSS and JS at the very last ---//
  778. if(strpos($sContent , '<bx_include_css />') !== false) {
  779. if (!empty($this->aPage['css_name']))
  780. $this->addCss($this->aPage['css_name']);
  781. $sContent = str_replace('<bx_include_css />', $this->includeFiles('css', true) . $this->includeFiles('css'), $sContent);
  782. }
  783. if(strpos($sContent , '<bx_include_js />') !== false) {
  784. if (!empty($this->aPage['js_name']))
  785. $this->addJs($this->aPage['js_name']);
  786. $sContent = str_replace('<bx_include_js />', $this->includeFiles('js', true) . $this->includeFiles('js'), $sContent);
  787. }
  788. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPage($sContent);
  789. return $sContent;
  790. }
  791. /**
  792. * Parse system keys.
  793. *
  794. * @param string $sKey key
  795. * @return string value associated with the key.
  796. */
  797. function parseSystemKey($sKey, $mixedKeyWrapperHtml = null, $bProcessInjection = true) {
  798. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  799. $sRet = '';
  800. switch( $sKey ) {
  801. case 'page_charset':
  802. $sRet = 'UTF-8';
  803. break;
  804. case 'page_robots':
  805. if(!empty($this->aPage['robots']) && is_string($this->aPage['robots']))
  806. $sRet = '<meta name="robots" content="' . bx_html_attribute($this->aPage['robots']) . '" />';
  807. break;
  808. case 'page_keywords':
  809. if(!empty($this->aPage['keywords']) && is_array($this->aPage['keywords']))
  810. $sRet = '<meta name="keywords" content="' . bx_html_attribute(implode(',', $this->aPage['keywords'])) . '" />';
  811. break;
  812. case 'page_description':
  813. if(!empty($this->aPage['description']) && is_string($this->aPage['description']))
  814. $sRet = '<meta name="description" content="' . bx_html_attribute($this->aPage['description']) . '" />';
  815. break;
  816. case 'page_header':
  817. if(isset($this->aPage['header']))
  818. $sRet = bx_process_output(strip_tags($this->aPage['header']));
  819. break;
  820. case 'page_header_text':
  821. if(isset($this->aPage['header_text']))
  822. $sRet = bx_process_output($this->aPage['header_text']);
  823. break;
  824. case 'page_icons':
  825. bx_import('BxTemplFunctions');
  826. $sRet = BxTemplFunctions::getInstance()->getMetaIcons();
  827. break;
  828. case 'popup_loading':
  829. $sContent = $this->parsePageByName('popup_loading.html', array());
  830. bx_import('BxTemplFunctions');
  831. $sRet = BxTemplFunctions::getInstance()->transBox('bx-popup-loading', $sContent, true);
  832. break;
  833. case 'lang':
  834. $sRet = bx_lang_name();
  835. break;
  836. case 'main_logo':
  837. bx_import('BxTemplFunctions');
  838. $sRet = BxTemplFunctions::getInstance()->getMainLogo();
  839. break;
  840. case 'informer':
  841. bx_import('BxDolInformer');
  842. $oInformer = BxDolInformer::getInstance($this);
  843. $sRet = $oInformer ? $oInformer->display() : '';
  844. break;
  845. case 'switch_skin_block':
  846. $sRet = getParam("enable_template") ? templates_select_txt() : '';
  847. break;
  848. case 'dol_images':
  849. $sRet = $this->_processJsImages();
  850. break;
  851. case 'dol_lang':
  852. $sRet = $this->_processJsTranslations();
  853. break;
  854. case 'dol_options':
  855. $sRet = $this->_processJsOptions();
  856. break;
  857. case 'bottom_text':
  858. $sRet = _t( '_bottom_text', date('Y') );
  859. break;
  860. case 'copyright':
  861. $sRet = _t( '_copyright', date('Y') ) . getVersionComment();
  862. break;
  863. case 'extra_js':
  864. $sRet = empty($this->aPage['extra_js']) ? '' : $this->aPage['extra_js'];
  865. break;
  866. case 'is_profile_page':
  867. $sRet = (defined('BX_PROFILE_PAGE')) ? 'true' : 'false';
  868. break;
  869. default:
  870. bx_import('BxTemplFunctions');
  871. $sRet = ($sTemplAdd = BxTemplFunctions::getInstance()->TemplPageAddComponent($sKey)) !== false ? $sTemplAdd : $aKeyWrappers['left'] . $sKey . $aKeyWrappers['right'];
  872. }
  873. if($bProcessInjection)
  874. $sRet = $this->processInjection($this->getPageNameIndex(), $sKey, $sRet);
  875. return $sRet;
  876. }
  877. /**
  878. * Get cache object for templates
  879. * @return cache class instance
  880. */
  881. function getTemplatesCacheObject () {
  882. $sCacheEngine = getParam('sys_template_cache_engine');
  883. $oCacheEngine = bx_instance('BxDolCache' . $sCacheEngine);
  884. if(!$oCacheEngine->isAvailable())
  885. $oCacheEngine = bx_instance('BxDolCacheFileHtml');
  886. return $oCacheEngine;
  887. }
  888. /**
  889. * Get template from cache if it's enabled.
  890. *
  891. * @param string $sName template name
  892. * @param string $aVariables key/value pairs. key should be the same as template's key, but without prefix and postfix.
  893. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  894. * @param string $sCheckIn where the content would be searched(base, template, both)
  895. * @param boolean $bEvaluate need to evaluate the template or not.
  896. * @return string result of operation or false on failure.
  897. */
  898. function getCached($sName, &$aVariables, $mixedKeyWrapperHtml = null, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH, $bEvaluate = true) {
  899. // initialization
  900. if(!$this->_bCacheEnable)
  901. return false;
  902. $sAbsolutePath = $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn);
  903. if(empty($sAbsolutePath))
  904. return false;
  905. $oCacheEngine = $this->getTemplatesCacheObject ();
  906. $isFileBasedEngine = $bEvaluate && method_exists($oCacheEngine, 'getDataFilePath');
  907. // try to get cached content
  908. $sCacheVariableName = "a";
  909. $sCacheKey = $this->_getCacheFileName('html', $sAbsolutePath) . '.php';
  910. if ($isFileBasedEngine)
  911. $sCacheContent = $oCacheEngine->getDataFilePath($sCacheKey);
  912. else
  913. $sCacheContent = $oCacheEngine->getData($sCacheKey);
  914. // recreate cache if it is empty
  915. if ($sCacheContent === null && ($sContent = file_get_contents($sAbsolutePath)) !== false && ($sContent = $this->_compileContent($sContent, "\$" . $sCacheVariableName, 1, $aVariables, $mixedKeyWrapperHtml)) !== false) {
  916. if (false === $oCacheEngine->setData($sCacheKey, $sContent))
  917. return false;
  918. if ($isFileBasedEngine)
  919. $sCacheContent = $oCacheEngine->getDataFilePath($sCacheKey);
  920. else
  921. $sCacheContent = $sContent;
  922. }
  923. if ($sCacheContent === null)
  924. return false;
  925. // return simple cache content
  926. if(!$bEvaluate)
  927. return $sCacheContent;
  928. // return evaluated cache content
  929. ob_start();
  930. $$sCacheVariableName = &$aVariables;
  931. if ($isFileBasedEngine)
  932. include($sCacheContent);
  933. else
  934. eval('?'.'>' . $sCacheContent);
  935. $sContent = ob_get_clean();
  936. return $sContent;
  937. }
  938. /**
  939. * Add JS file(s) to global output.
  940. *
  941. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  942. * @param boolean $bDynamic in the dynamic mode JS file(s) are not included to global output, but are returned from the function directly.
  943. * @return boolean/string result of operation.
  944. */
  945. function addJs($mixedFiles, $bDynamic = false) {
  946. return $this->_processFiles('js', 'add', $mixedFiles, $bDynamic);
  947. }
  948. /**
  949. * Add System JS file(s) to global output.
  950. * System JS files are the files which are attached to all pages. They will be cached separately from the others.
  951. *
  952. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  953. * @param boolean $bDynamic in the dynamic mode JS file(s) are not included to global output, but are returned from the function directly.
  954. * @return boolean/string result of operation.
  955. */
  956. function addJsSystem($mixedFiles) {
  957. return $this->_processFiles('js', 'add', $mixedFiles, false, true);
  958. }
  959. /**
  960. * Delete JS file(s) from global output.
  961. *
  962. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  963. * @return boolean result of operation.
  964. */
  965. function deleteJs($mixedFiles) {
  966. return $this->_processFiles('js', 'delete', $mixedFiles);
  967. }
  968. /**
  969. * Delete System JS file(s) from global output.
  970. *
  971. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  972. * @return boolean result of operation.
  973. */
  974. function deleteJsSystem($mixedFiles) {
  975. return $this->_processFiles('js', 'delete', $mixedFiles, false, true);
  976. }
  977. /**
  978. * Compile JS files in one file.
  979. *
  980. * @param string $sAbsolutePath CSS file absolute path(full URL for external CSS/JS files).
  981. * @param array $aIncluded an array of already included JS files.
  982. * @return string result of operation.
  983. */
  984. function _compileJs($sAbsolutePath, &$aIncluded) {
  985. if(isset($aIncluded[$sAbsolutePath]))
  986. return '';
  987. $bExternal = strpos($sAbsolutePath, "http://") !== false || strpos($sAbsolutePath, "https://") !== false;
  988. if($bExternal) {
  989. $sPath = $sAbsolutePath;
  990. $sName = '';
  991. $sContent = bx_file_get_contents($sAbsolutePath);
  992. }
  993. else {
  994. $aFileInfo = pathinfo($sAbsolutePath);
  995. $sPath = $aFileInfo['dirname'] . DIRECTORY_SEPARATOR;
  996. $sName = $aFileInfo['basename'];
  997. $sContent = file_get_contents($sPath . $sName);
  998. }
  999. if(empty($sContent))
  1000. return '';
  1001. $sUrl = bx_ltrim_str($sPath, realpath(BX_DIRECTORY_PATH_ROOT), BX_DOL_URL_ROOT);
  1002. $sUrl = str_replace(DIRECTORY_SEPARATOR, '/', $sPath);
  1003. $sContent = "\r\n/*--- BEGIN: " . $sUrl . $sName . "---*/\r\n" . $sContent . ";\r\n/*--- END: " . $sUrl . $sName . "---*/\r\n";
  1004. $sContent = str_replace(array("\n\r", "\r\n", "\r"), "\n", $sContent);
  1005. $aIncluded[$sAbsolutePath] = 1;
  1006. return preg_replace(
  1007. array(
  1008. "'<bx_url_root />'",
  1009. "'\r\n'"
  1010. ),
  1011. array(
  1012. BX_DOL_URL_ROOT,
  1013. "\n"
  1014. ),
  1015. $sContent
  1016. );
  1017. }
  1018. /**
  1019. * Wrap an URL to JS file into JS tag.
  1020. *
  1021. * @param string $sFile - URL to JS file.
  1022. * @return string the result of operation.
  1023. */
  1024. function _wrapInTagJs($sFile) {
  1025. return "<script language=\"javascript\" type=\"text/javascript\" src=\"" . $sFile . "\"></script>";
  1026. }
  1027. /**
  1028. * Wrap JS code into JS tag.
  1029. *
  1030. * @param string $sCode - JS code.
  1031. * @return string the result of operation.
  1032. */
  1033. function _wrapInTagJsCode($sCode) {
  1034. return "<script language=\"javascript\" type=\"text/javascript\">\n<!--\n" . $sCode . "\n-->\n</script>";
  1035. }
  1036. /**
  1037. * Add CSS file(s) to global output.
  1038. *
  1039. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1040. * @param boolean $bDynamic in the dynamic mode CSS file(s) are not included to global output, but are returned from the function directly.
  1041. * @return boolean/string result of operation
  1042. */
  1043. function addCss($mixedFiles, $bDynamic = false) {
  1044. return $this->_processFiles('css', 'add', $mixedFiles, $bDynamic);
  1045. }
  1046. /**
  1047. * Add System CSS file(s) to global output.
  1048. * System CSS files are the files which are attached to all pages. They will be cached separately from the others.
  1049. *
  1050. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1051. * @return boolean/string result of operation
  1052. */
  1053. function addCssSystem($mixedFiles) {
  1054. return $this->_processFiles('css', 'add', $mixedFiles, false, true);
  1055. }
  1056. /**
  1057. * Delete CSS file(s) from global output.
  1058. *
  1059. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1060. * @return boolean result of operation.
  1061. */
  1062. function deleteCss($mixedFiles){
  1063. return $this->_processFiles('css', 'delete', $mixedFiles);
  1064. }
  1065. /**
  1066. * Delete System CSS file(s) from global output.
  1067. *
  1068. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1069. * @return boolean result of operation.
  1070. */
  1071. function deleteCssSystem($mixedFiles){
  1072. return $this->_processFiles('css', 'delete', $mixedFiles, false, true);
  1073. }
  1074. /**
  1075. * Compile CSS files' structure(@see @import css_file_path) in one file.
  1076. *
  1077. * @param string $sAbsolutePath CSS file absolute path(full URL for external CSS/JS files).
  1078. * @param array $aIncluded an array of already included CSS files.
  1079. * @return string result of operation.
  1080. */
  1081. function _compileCss($sAbsolutePath, &$aIncluded) {
  1082. if(isset($aIncluded[$sAbsolutePath]))
  1083. return '';
  1084. $bExternal = strpos($sAbsolutePath, "http://") !== false || strpos($sAbsolutePath, "https://") !== false;
  1085. if($bExternal) {
  1086. $sPath = $sAbsolutePath;
  1087. $sName = '';
  1088. $sContent = bx_file_get_contents($sAbsolutePath);
  1089. }
  1090. else {
  1091. $aFileInfo = pathinfo($sAbsolutePath);
  1092. $sPath = $aFileInfo['dirname'] . DIRECTORY_SEPARATOR;
  1093. $sName = $aFileInfo['basename'];
  1094. $sContent = file_get_contents($sPath . $sName);
  1095. }
  1096. if(empty($sContent))
  1097. return '';
  1098. $sUrl = bx_ltrim_str($sPath, realpath(BX_DIRECTORY_PATH_ROOT), BX_DOL_URL_ROOT);
  1099. $sUrl = str_replace(DIRECTORY_SEPARATOR, '/', $sPath);
  1100. $sContent = "\r\n/*--- BEGIN: " . $sUrl . $sName . "---*/\r\n" . $sContent . "\r\n/*--- END: " . $sUrl . $sName . "---*/\r\n";
  1101. $aIncluded[$sAbsolutePath] = 1;
  1102. $sContent = str_replace(array("\n\r", "\r\n", "\r"), "\n", $sContent);
  1103. if($bExternal) {
  1104. $sContent = preg_replace(
  1105. array(
  1106. "'@import\s+url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*\)\s*;'e",
  1107. "'url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/\?\#_=-]+)\s*[\'|\"]*\s*\)'e"
  1108. ),
  1109. array(
  1110. "",
  1111. "'url(' . \$sPath . '\\1)'"
  1112. ),
  1113. $sContent
  1114. );
  1115. }
  1116. else {
  1117. $sContent = preg_replace(
  1118. "'@import\s+url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*\)\s*;'e",
  1119. "\$this->_compileCss(realpath(\$sPath . dirname('\\1')) . DIRECTORY_SEPARATOR . basename('\\1'), \$aIncluded)",
  1120. $sContent
  1121. );
  1122. $sContent = preg_replace_callback(
  1123. "'url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/\?\#_=-]+)\s*[\'|\"]*\s*\)'",
  1124. create_function('$aMatches', 'return BxDolTemplate::_callbackParseUrl("' . addslashes($sPath) . '", $aMatches);'),
  1125. $sContent
  1126. );
  1127. }
  1128. return $sContent;
  1129. }
  1130. /**
  1131. * Less CSS
  1132. *
  1133. * @param mixed $mixed CSS string to process with Less compiler or an array with CSS file's Path and URL.
  1134. * @param boolean $bContent whether $mixed has CSS content or file path.
  1135. * @return string minified CSS string.
  1136. */
  1137. function _lessCss($mixed) {
  1138. require_once(BX_DIRECTORY_PATH_PLUGINS . 'lessphp/lessc.inc.php');
  1139. $oLess = new lessc();
  1140. $oLess->setVariables($this->_oConfigTemplate->aLessConfig);
  1141. if(is_array($mixed) && isset($mixed['url']) && isset($mixed['path'])) {
  1142. $sPathFile = realpath($mixed['path']);
  1143. $aInfoFile = pathinfo($sPathFile);
  1144. if (!isset($aInfoFile['extension']) || $aInfoFile['extension'] != 'less')
  1145. return $mixed;
  1146. $sPathRoot = realpath(BX_DIRECTORY_PATH_ROOT);
  1147. $sFile = $this->_sLessCachePrefix . trim(str_replace(array('.' . $aInfoFile['extension'], DIRECTORY_SEPARATOR), array('', '_'), bx_ltrim_str($sPathFile, $sPathRoot)), '_') . '.css';
  1148. $oLess->checkedCompile($mixed['path'], $this->_sCachePublicFolderPath . $sFile);
  1149. return array('url' => $this->_sCachePublicFolderUrl . $sFile, 'path' => $this->_sCachePublicFolderPath . $sFile);
  1150. }
  1151. return $oLess->compile($mixed);
  1152. }
  1153. /**
  1154. * Minify CSS
  1155. *
  1156. * @param string $s CSS string to minify
  1157. * @return string minified CSS string.
  1158. */
  1159. function _minifyCss($s) {
  1160. require_once(BX_DIRECTORY_PATH_PLUGINS . 'minify/lib/Minify/CSS/Compressor.php');
  1161. return Minify_CSS_Compressor::process($s);
  1162. }
  1163. /**
  1164. * Static callback function for CSS compiler.
  1165. *
  1166. * @param string $sPath CSS file absolute path.
  1167. * @param array $aMatches matched parts of image's URL.
  1168. * @return string converted image's URL.
  1169. */
  1170. public static function _callbackParseUrl($sPath, $aMatches) {
  1171. $sFile = basename($aMatches[1]);
  1172. $sDirectory = dirname($aMatches[1]);
  1173. $sRootPath = realpath(BX_DIRECTORY_PATH_ROOT);
  1174. $sAbsolutePath = realpath($sPath . $sDirectory) . DIRECTORY_SEPARATOR . $sFile;
  1175. $sRootPath = str_replace(DIRECTORY_SEPARATOR, '/', $sRootPath);
  1176. $sAbsolutePath = str_replace(DIRECTORY_SEPARATOR, '/', $sAbsolutePath);
  1177. return 'url(' . bx_ltrim_str($sAbsolutePath, $sRootPath, BX_DOL_URL_ROOT) . ')';
  1178. }
  1179. /**
  1180. * Wrap an URL to CSS file into CSS tag.
  1181. *
  1182. * @param string $sFile - URL to CSS file.
  1183. * @return string the result of operation.
  1184. */
  1185. function _wrapInTagCss($sFile) {
  1186. if (!$sFile)
  1187. return '';
  1188. return "<link href=\"" . $sFile . "\" rel=\"stylesheet\" type=\"text/css\" />";
  1189. }
  1190. /**
  1191. * Wrap CSS code into CSS tag.
  1192. *
  1193. * @param string $sCode - CSS code.
  1194. * @return string the result of operation.
  1195. */
  1196. function _wrapInTagCssCode($sCode) {
  1197. return "<link rel=\"stylesheet\" type=\"text/css\">" . $sCode . "</link>";
  1198. }
  1199. /**
  1200. * Include CSS/JS file(s) attached to the page in its head section.
  1201. * @see the method is system and would be called automatically.
  1202. *
  1203. * @param string $sType the type of file('js' or 'css')
  1204. * @return string the result CSS code.
  1205. */
  1206. function includeFiles($sType, $bSystem = false) {
  1207. $sUpcaseType = ucfirst($sType);
  1208. $sArrayKey = $sType . ($bSystem ? '_system' : '_compiled');
  1209. $aFiles = isset($this->aPage[$sArrayKey]) ? $this->aPage[$sArrayKey] : array();
  1210. if(empty($aFiles) || !is_array($aFiles))
  1211. return "";
  1212. if(!$this->{'_b' . $sUpcaseType . 'Cache'})
  1213. return $this->_includeFiles($sType, $aFiles);
  1214. //--- If cache already exists, return it ---//
  1215. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  1216. $sMethodCompile = '_compile' . $sUpcaseType;
  1217. $sMethodLess = '_less' . $sUpcaseType;
  1218. $sMethodMinify = '_minify' . $sUpcaseType;
  1219. ksort($aFiles);
  1220. $sName = "";
  1221. foreach($aFiles as $aFile)
  1222. $sName .= $aFile['url'];
  1223. $sName = $this->_getCacheFileName($sType, $sName);
  1224. $sCacheAbsoluteUrl = $this->_sCachePublicFolderUrl . $sName . '.' . $sType;
  1225. $sCacheAbsolutePath = $this->_sCachePublicFolderPath . $sName . '.' . $sType;
  1226. if(file_exists($sCacheAbsolutePath)) {
  1227. if($this->{'_b' . $sUpcaseType . 'Archive'})
  1228. $sCacheAbsoluteUrl = $this->_getLoaderUrl($sType, $sName);
  1229. return $this->$sMethodWrap($sCacheAbsoluteUrl);
  1230. }
  1231. //--- Collect all attached CSS/JS in one file ---//
  1232. $sResult = "";
  1233. $aIncluded = array();
  1234. foreach($aFiles as $aFile)
  1235. if(($sContent = $this->$sMethodCompile($aFile['path'], $aIncluded)) !== false)
  1236. $sResult .= $sContent;
  1237. if ($this->_bLessEnable && method_exists($this, $sMethodLess))
  1238. $sResult = $this->$sMethodLess($sResult);
  1239. if ($this->_bMinifyEnable && method_exists($this, $sMethodMinify))
  1240. $sResult = $this->$sMethodMinify($sResult);
  1241. $mixedWriteResult = false;
  1242. if(!empty($sResult) && ($rHandler = fopen($sCacheAbsolutePath, 'w')) !== false) {
  1243. $mixedWriteResult = fwrite($rHandler, $sResult);
  1244. fclose($rHandler);
  1245. @chmod ($sCacheAbsolutePath, 0666);
  1246. }
  1247. if($mixedWriteResult === false)
  1248. return $this->_includeFiles($sType, $aFiles);
  1249. if($this->{'_b' . $sUpcaseType . 'Archive'})
  1250. $sCacheAbsoluteUrl = $this->_getLoaderUrl($sType, $sName);
  1251. return $this->$sMethodWrap($sCacheAbsoluteUrl);
  1252. }
  1253. /**
  1254. * Include CSS/JS files without caching.
  1255. *
  1256. * @param string $sType the file type (css or js)
  1257. * @param array $aFiles CSS/JS files to be added to the page.
  1258. * @return string result of operation.
  1259. */
  1260. function _includeFiles($sType, &$aFiles) {
  1261. $sUpcaseType = ucfirst($sType);
  1262. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  1263. $sMethodLess = '_less' . $sUpcaseType;
  1264. $sResult = "";
  1265. foreach($aFiles as $aFile) {
  1266. if($this->_bLessEnable && method_exists($this, $sMethodLess))
  1267. $aFile = $this->$sMethodLess($aFile, false);
  1268. $sResult .= $this->$sMethodWrap($aFile['url']);
  1269. }
  1270. return $sResult;
  1271. }
  1272. /**
  1273. * Insert/Delete CSS file from output stack.
  1274. *
  1275. * @param string $sType the file type (css or js)
  1276. * @param string $sAction add/delete
  1277. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1278. * @return boolean result of operation.
  1279. */
  1280. function _processFiles($sType, $sAction, $mixedFiles, $bDynamic = false, $bSystem = false) {
  1281. if(empty($mixedFiles))
  1282. return $bDynamic ? "" : false;
  1283. if(is_string($mixedFiles))
  1284. $mixedFiles = array($mixedFiles);
  1285. $sUpcaseType = ucfirst($sType);
  1286. $sMethodLocate = '_getAbsoluteLocation' . $sUpcaseType;
  1287. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  1288. $sResult = '';
  1289. foreach($mixedFiles as $sFile) {
  1290. //--- Process 3d Party CSS/JS file ---//
  1291. if(strpos($sFile, "http://") !== false || strpos($sFile, "https://") !== false) {
  1292. $sUrl = $sFile;
  1293. $sPath = $sFile;
  1294. }
  1295. //--- Process Custom CSS/JS file ---//
  1296. else if(strpos($sFile, "|") !== false) {
  1297. $sFile = implode('', explode("|", $sFile));
  1298. $sUrl = BX_DOL_URL_ROOT . $sFile;
  1299. $sPath = realpath(BX_DIRECTORY_PATH_ROOT . $sFile);
  1300. }
  1301. //--- Process Common CSS/JS file(check in default locations) ---//
  1302. else {
  1303. $sUrl = $this->$sMethodLocate('url', $sFile);
  1304. $sPath = $this->$sMethodLocate('path', $sFile);
  1305. }
  1306. if(empty($sPath) || empty($sUrl))
  1307. continue;
  1308. $sArrayKey = $sType . ($bSystem ? '_system' : '_compiled');
  1309. switch($sAction) {
  1310. case 'add':
  1311. if($bDynamic)
  1312. $sResult .= $this->$sMethodWrap($sUrl);
  1313. else {
  1314. $bFound = false;
  1315. foreach($this->aPage[$sArrayKey] as $iKey => $aValue)
  1316. if($aValue['url'] == $sUrl && $aValue['path'] == $sPath) {
  1317. $bFound = true;
  1318. break;
  1319. }
  1320. if(!$bFound)
  1321. $this->aPage[$sArrayKey][] = array('url' => $sUrl, 'path' => $sPath);
  1322. }
  1323. break;
  1324. case 'delete':
  1325. if(!$bDynamic)
  1326. foreach($this->aPage[$sArrayKey] as $iKey => $aValue)
  1327. if($aValue['url'] == $sUrl) {
  1328. unset($this->aPage[$sArrayKey][$iKey]);
  1329. break;
  1330. }
  1331. break;
  1332. }
  1333. }
  1334. return $bDynamic ? $sResult : true;
  1335. }
  1336. /**
  1337. * Parse content.
  1338. *
  1339. * @param string $sContent - HTML file's content.
  1340. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1341. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1342. * @return string the result of operation.
  1343. */
  1344. function _parseContent($sContent, $aVariables, $mixedKeyWrapperHtml = null) {
  1345. $aKeys = array_keys($aVariables);
  1346. $aValues = array_values($aVariables);
  1347. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  1348. $iCountKeys = count($aKeys);
  1349. for ($i = 0; $i < $iCountKeys; $i++) {
  1350. if (strncmp($aKeys[$i], 'bx_repeat:', 10) === 0) {
  1351. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  1352. $aMatches = array();
  1353. preg_match($sKey, $sContent, $aMatches);
  1354. $sValue = '';
  1355. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  1356. if(is_array($aValues[$i]))
  1357. foreach($aValues[$i] as $aValue)
  1358. $sValue .= $this->parseHtmlByContent($aMatches[1], $aValue, $mixedKeyWrapperHtml);
  1359. else if(is_string($aValues[$i]))
  1360. $sValue = $aValues[$i];
  1361. }
  1362. }
  1363. else if (strncmp($aKeys[$i], 'bx_if:', 6) === 0) {
  1364. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  1365. $aMatches = array();
  1366. preg_match($sKey, $sContent, $aMatches);
  1367. $sValue = '';
  1368. if(isset($aMatches[1]) && !empty($aMatches[1]))
  1369. if(is_array($aValues[$i]) && isset($aValues[$i]['content']) && isset($aValues[$i]['condition']) && $aValues[$i]['condition'])
  1370. $sValue .= $this->parseHtmlByContent($aMatches[1], $aValues[$i]['content'], $mixedKeyWrapperHtml);
  1371. }
  1372. else {
  1373. $sKey = "'" . $aKeyWrappers['left'] . $aKeys[$i] . $aKeyWrappers['right'] . "'s";
  1374. $sValue = str_replace('$', '\\$', $aValues[$i]);
  1375. }
  1376. $aKeys[$i] = $sKey;
  1377. $aValues[$i] = $sValue;
  1378. }
  1379. $aKeys = array_merge($aKeys, array(
  1380. "'<bx_include_auto:([^\s]+) \/>'se",
  1381. "'<bx_include_tmpl:([^\s]+) \/>'se",
  1382. "'<bx_include_base:([^\s]+) \/>'se",
  1383. "'<bx_injection:([^\s]+) />'se",
  1384. "'<bx_image_url:([^\s]+) \/>'se",
  1385. "'<bx_icon_url:([^\s]+) \/>'se",
  1386. "'<bx_text:([_\{\}\w\d\s]+[^\s]{1}) \/>'se",
  1387. "'<bx_text_js:([^\s]+) \/>'se",
  1388. "'<bx_text_attribute:([^\s]+) \/>'se",
  1389. "'<bx_menu:([^\s]+) \/>'se",
  1390. "'<bx_url_root />'",
  1391. "'<bx_url_studio />'",
  1392. ));
  1393. $aValues = array_merge($aValues, array(
  1394. "\$this->parseHtmlByName('\\1', \$aVariables, \$mixedKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BOTH)",
  1395. "\$this->parseHtmlByName('\\1', \$aVariables, \$mixedKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_TMPL)",
  1396. "\$this->parseHtmlByName('\\1', \$aVariables, \$mixedKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BASE)",
  1397. "\$this->processInjection(\$this->aPage['name_index'], '\\1')",
  1398. "\$this->getImageUrl('\\1')",
  1399. "\$this->getIconUrl('\\1')",
  1400. "_t('\\1')",
  1401. "bx_js_string(_t('\\1'))",
  1402. "bx_html_attribute(_t('\\1'))",
  1403. "\$this->getMenu('\\1')",
  1404. BX_DOL_URL_ROOT,
  1405. BX_DOL_URL_STUDIO,
  1406. ));
  1407. //--- Parse Predefined Keys ---//
  1408. $sContent = preg_replace($aKeys, $aValues, $sContent);
  1409. //--- Parse System Keys ---//
  1410. $sContent = preg_replace( "'" . $aKeyWrappers['left'] . "([a-zA-Z0-9_-]+)" . $aKeyWrappers['right'] . "'e", "\$this->parseSystemKey('\\1', \$mixedKeyWrapperHtml)", $sContent);
  1411. return $sContent;
  1412. }
  1413. /**
  1414. * Compile content
  1415. *
  1416. * @param string $sContent template.
  1417. * @param string $aVarName variable name to be saved in the output file.
  1418. * @param integer $iVarDepth depth is used to process nesting, for example, in cycles.
  1419. * @param array $aVarValues values to be compiled in.
  1420. * @param mixed $mixedKeyWrapperHtml key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1421. * @return string the result of operation.
  1422. */
  1423. function _compileContent($sContent, $aVarName, $iVarDepth, $aVarValues, $mixedKeyWrapperHtml = null) {
  1424. $aKeys = array_keys($aVarValues);
  1425. $aValues = array_values($aVarValues);
  1426. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  1427. for($i = 0; $i < count($aKeys); $i++) {
  1428. if(strpos($aKeys[$i], 'bx_repeat:') === 0) {
  1429. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  1430. $aMatches = array();
  1431. preg_match($sKey, $sContent, $aMatches);
  1432. $sValue = '';
  1433. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  1434. if(empty($aValues[$i]) || !is_array($aValues[$i]))
  1435. return false;
  1436. $sIndex = "\$" . str_repeat("i", $iVarDepth);
  1437. $sValue .= "<?php if(is_array(" . $aVarName . "['" . $aKeys[$i] . "'])) for(" . $sIndex . "=0; " . $sIndex . "<count(" . $aVarName . "['" . $aKeys[$i] . "']); " . $sIndex . "++){ ?>";
  1438. if(($sInnerValue = $this->_compileContent($aMatches[1], $aVarName . "['" . $aKeys[$i] . "'][" . $sIndex . "]", $iVarDepth + 1, current($aValues[$i]), $mixedKeyWrapperHtml)) === false)
  1439. return false;
  1440. $sValue .= $sInnerValue;
  1441. $sValue .= "<?php } else if(is_string(" . $aVarName . "['" . $aKeys[$i] . "'])) echo " . $aVarName . "['" . $aKeys[$i] . "']; ?>";
  1442. }
  1443. }
  1444. else if(strpos($aKeys[$i], 'bx_if:') === 0) {
  1445. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  1446. $aMatches = array();
  1447. preg_match($sKey, $sContent, $aMatches);
  1448. $sValue = '';
  1449. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  1450. if(!is_array($aValues[$i]) || !isset($aValues[$i]['content']) || empty($aValues[$i]['content']) || !is_array($aValues[$i]['content']))
  1451. return false;
  1452. $sValue .= "<?php if(" . $aVarName . "['" . $aKeys[$i] . "']['condition']){ ?>";
  1453. if(($sInnerValue = $this->_compileContent($aMatches[1], $aVarName . "['" . $aKeys[$i] . "']['content']", $iVarDepth, $aValues[$i]['content'], $mixedKeyWrapperHtml)) === false)
  1454. return false;
  1455. $sValue .= $sInnerValue;
  1456. $sValue .= "<?php } ?>";
  1457. }
  1458. }
  1459. else {
  1460. $sKey = "'" . $aKeyWrappers['left'] . $aKeys[$i] . $aKeyWrappers['right'] . "'s";
  1461. $sValue = "<?php echo " . $aVarName . "['" . $aKeys[$i] . "'];?>";
  1462. }
  1463. $aKeys[$i] = $sKey;
  1464. $aValues[$i] = $sValue;
  1465. }
  1466. $aKeys = array_merge($aKeys, array(
  1467. "'<bx_include_auto:([^\s]+) \/>'se",
  1468. "'<bx_include_base:([^\s]+) \/>'se",
  1469. "'<bx_include_tmpl:([^\s]+) \/>'se",
  1470. "'<bx_injection:([^\s]+) />'s",
  1471. "'<bx_image_url:([^\s]+) \/>'se",
  1472. "'<bx_icon_url:([^\s]+) \/>'se",
  1473. "'<bx_text:([_\{\}\w\d\s]+[^\s]{1}) \/>'se",
  1474. "'<bx_text_js:([^\s]+) \/>'se",
  1475. "'<bx_text_attribute:([^\s]+) \/>'se",
  1476. "'<bx_menu:([^\s]+) \/>'se",
  1477. "'<bx_url_root />'",
  1478. "'<bx_url_studio />'"
  1479. ));
  1480. $aValues = array_merge($aValues, array(
  1481. "\$this->getCached('\\1', \$aVarValues, \$mixedKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BOTH, false)",
  1482. "\$this->getCached('\\1', \$aVarValues, \$mixedKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BASE, false)",
  1483. "\$this->getCached('\\1', \$aVarValues, \$mixedKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_TMPL, false)",
  1484. "<?php echo \$this->processInjection(\$this->aPage['name_index'], '\\1'); ?>",
  1485. "\$this->getImageUrl('\\1')",
  1486. "\$this->getIconUrl('\\1')",
  1487. "_t('\\1')",
  1488. "bx_js_string(_t('\\1'))",
  1489. "bx_html_attribute(_t('\\1'))",
  1490. "\$this->getMenu('\\1')",
  1491. BX_DOL_URL_ROOT,
  1492. BX_DOL_URL_STUDIO
  1493. ));
  1494. //--- Parse Predefined Keys ---//
  1495. $sContent = preg_replace($aKeys, $aValues, $sContent);
  1496. //--- Parse System Keys ---//
  1497. $sContent = preg_replace( "'" . $aKeyWrappers['left'] . "([a-zA-Z0-9_-]+)" . $aKeyWrappers['right'] . "'", "<?php echo \$this->parseSystemKey('\\1', \$mixedKeyWrapperHtml);?>", $sContent);
  1498. return $sContent;
  1499. }
  1500. /**
  1501. * Get absolute location of some template's part.
  1502. *
  1503. * @param string $sType - result type. Available values 'url' and 'path'.
  1504. * @param string $sFolder - folders to be searched in. @see $_sFolderHtml, $_sFolderCss, $_sFolderImages and $_sFolderIcons
  1505. * @param string $sName - requested part name.
  1506. * @param string $sCheckIn where the content would be searched(base, template, both)
  1507. * @return string absolute location (path/url) of the part.
  1508. */
  1509. function _getAbsoluteLocation($sType, $sFolder, $sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH) {
  1510. $sDirectory = $this->getPath();
  1511. if($sType == 'path') {
  1512. $sDivider = DIRECTORY_SEPARATOR;
  1513. $sRoot = BX_DIRECTORY_PATH_ROOT;
  1514. }
  1515. else if($sType == 'url') {
  1516. $sDivider = '/';
  1517. $sRoot = BX_DOL_URL_ROOT;
  1518. }
  1519. if(strpos($sName,'|') !== false) {
  1520. $aParts = explode('|', $sName);
  1521. $sName = $aParts[1];
  1522. if(strpos($aParts[0],'@') !== false) {
  1523. $aLocationParts = explode('@', $aParts[0]);
  1524. $sLocationKey = $this->addLocation($aLocationParts[0], BX_DIRECTORY_PATH_ROOT . $aLocationParts[1], BX_DOL_URL_ROOT . $aLocationParts[1]);
  1525. }
  1526. }
  1527. $sResult = '';
  1528. $aLocations = array_reverse($this->_aLocations, true);
  1529. foreach($aLocations as $sKey => $aLocation) {
  1530. 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))
  1531. $sResult = $sRoot . 'modules' . $sDivider . $sDirectory. 'data' . $sDivider . 'template' . $sDivider . $sKey . $sDivider . $sFolder . $sName;
  1532. 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))
  1533. $sResult = $aLocation[$sType] . BX_DOL_TEMPLATE_FOLDER_ROOT . $sDivider . $sFolder . $sName;
  1534. else
  1535. continue;
  1536. break;
  1537. }
  1538. /**
  1539. * try to find from received path
  1540. */
  1541. if(!$sResult && @is_file(BX_DIRECTORY_PATH_ROOT . $aParts[0] . DIRECTORY_SEPARATOR . $aParts[1])) {
  1542. $sResult = $sRoot . $aParts[0] . $sDivider . $aParts[1];
  1543. }
  1544. if(isset($sLocationKey))
  1545. $this->removeLocation($sLocationKey);
  1546. return $sType == 'path' && !empty($sResult) ? realpath($sResult) : $sResult;
  1547. }
  1548. /**
  1549. * Get absolute location of some template's part.
  1550. *
  1551. * @param string $sType result type. Available values 'url' and 'path'.
  1552. * @param string $sName requested part name.
  1553. * @return string absolute location (path/url) of the part.
  1554. */
  1555. function _getAbsoluteLocationJs($sType, $sName) {
  1556. $sResult = '';
  1557. $aLocations = array_reverse($this->_aLocationsJs, true);
  1558. foreach($aLocations as $sKey => $aLocation) {
  1559. if(extFileExists($aLocation['path'] . $sName))
  1560. $sResult = $aLocation[$sType] . $sName;
  1561. else
  1562. continue;
  1563. break;
  1564. }
  1565. return $sType == 'path' && !empty($sResult) ? realpath($sResult) : $sResult;
  1566. }
  1567. function _getAbsoluteLocationCss($sType, $sName) {
  1568. return $this->_getAbsoluteLocation($sType, $this->_sFolderCss, $sName);
  1569. }
  1570. /**
  1571. * Get inline data for Images and Icons.
  1572. *
  1573. * @param string $sType image/icon
  1574. * @param string $sName file name
  1575. * @param string $sCheckIn where the content would be searched(base, template, both)
  1576. * @return unknown
  1577. */
  1578. function _getInlineData($sType, $sName, $sCheckIn) {
  1579. switch($sType) {
  1580. case 'image':
  1581. $sFolder = $this->_sFolderImages;
  1582. break;
  1583. case 'icon':
  1584. $sFolder = $this->_sFolderIcons;
  1585. break;
  1586. }
  1587. $sPath = $this->_getAbsoluteLocation('path', $sFolder, $sName, $sCheckIn);
  1588. $iFileSize = 0;
  1589. if($this->_bImagesInline && ($iFileSize = filesize($sPath)) !== false && $iFileSize < $this->_iImagesMaxSize) {
  1590. $aFileInfo = pathinfo($sPath);
  1591. return "data:image/" . strtolower($aFileInfo['extension']) . ";base64," . base64_encode(file_get_contents($sPath));
  1592. }
  1593. return false;
  1594. }
  1595. /**
  1596. * Get file name where the template would be cached.
  1597. *
  1598. * @param string $sAbsolutePath template's real path.
  1599. * @return string the result of operation.
  1600. */
  1601. function _getCacheFileName($sType, $sAbsolutePath) {
  1602. $sResult = bx_site_hash($sAbsolutePath);
  1603. switch($sType) {
  1604. case 'html':
  1605. $sResult = $this->_sCacheFilePrefix . bx_lang_name() . '_' . $this->_sCode . '_' . $sResult;
  1606. break;
  1607. case 'css':
  1608. $sResult = $this->_sCssCachePrefix . $sResult;
  1609. break;
  1610. case 'js':
  1611. $sResult = $this->_sJsCachePrefix . $sResult;
  1612. break;
  1613. }
  1614. return $sResult;
  1615. }
  1616. /**
  1617. * Get template key wrappers(left, right)
  1618. *
  1619. * @param mixed $mixedKeyWrapperHtml key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1620. * @return array result of operation.
  1621. */
  1622. function _getKeyWrappers($mixedKeyWrapperHtml) {
  1623. $aResult = array();
  1624. if(!empty($mixedKeyWrapperHtml) && is_string($mixedKeyWrapperHtml))
  1625. $aResult = array('left' => $mixedKeyWrapperHtml, 'right' => $mixedKeyWrapperHtml);
  1626. else if(!empty($mixedKeyWrapperHtml) && is_array($mixedKeyWrapperHtml))
  1627. $aResult = array('left' => $mixedKeyWrapperHtml[0], 'right' => $mixedKeyWrapperHtml[1]);
  1628. else
  1629. $aResult = array('left' => $this->_sKeyWrapperHtml, 'right' => $this->_sKeyWrapperHtml);
  1630. return $aResult;
  1631. }
  1632. /**
  1633. * Process all added language translations and return them as a string.
  1634. *
  1635. * @return string with JS code.
  1636. */
  1637. function _processJsTranslations() {
  1638. $sReturn = '';
  1639. if(isset($this->aPage['js_translations']) && is_array($this->aPage['js_translations'])) {
  1640. foreach($this->aPage['js_translations'] as $sKey => $sString)
  1641. $sReturn .= "'" . bx_js_string($sKey) . "': '" . bx_js_string($sString) . "',";
  1642. $sReturn = substr($sReturn, 0, -1);
  1643. }
  1644. return '<script type="text/javascript" language="javascript">var aDolLang = {' . $sReturn . '};</script>';
  1645. }
  1646. /**
  1647. * Process all added options and return them as a string.
  1648. *
  1649. * @return string with JS code.
  1650. */
  1651. function _processJsOptions() {
  1652. $sReturn = '';
  1653. if(isset($this->aPage['js_options']) && is_array($this->aPage['js_options'])) {
  1654. foreach($this->aPage['js_options'] as $sName => $mixedValue)
  1655. $sReturn .= "'" . bx_js_string($sName) . "': '" . bx_js_string($mixedValue) . "',";
  1656. $sReturn = substr($sReturn, 0, -1);
  1657. }
  1658. return '<script type="text/javascript" language="javascript">var aDolOptions = {' . $sReturn . '};</script>';
  1659. }
  1660. /**
  1661. * Process all added images and return them as a string.
  1662. *
  1663. * @return string with JS code.
  1664. */
  1665. function _processJsImages() {
  1666. $sReturn = '';
  1667. if(isset($this->aPage['js_images']) && is_array($this->aPage['js_images'])) {
  1668. foreach($this->aPage['js_images'] as $sKey => $sUrl)
  1669. $sReturn .= "'" . bx_js_string($sKey) . "': '" . bx_js_string($sUrl) . "',";
  1670. $sReturn = substr($sReturn, 0, -1);
  1671. }
  1672. return '<script type="text/javascript" language="javascript">var aDolImages = {' . $sReturn . '};</script>';
  1673. }
  1674. /**
  1675. * Get Gzip loader URL.
  1676. *
  1677. * @param $sType content type CSS/JS
  1678. * @param $sName file name.
  1679. * @return string with URL
  1680. */
  1681. function _getLoaderUrl($sType, $sName) {
  1682. return BX_DOL_URL_ROOT . 'gzip_loader.php?file=' . $sName . '.' . $sType;
  1683. }
  1684. /**
  1685. *
  1686. * Functions to display pages with errors, messages and so on.
  1687. *
  1688. */
  1689. function displayAccessDenied ($sMsg = '') {
  1690. header("HTTP/1.0 403 Forbidden");
  1691. $this->displayMsg($sMsg ? $sMsg : _t('_Access denied'));
  1692. }
  1693. function displayNoData ($sMsg = '') {
  1694. header("HTTP/1.0 204 No Content");
  1695. $this->displayMsg($sMsg ? $sMsg : _t('_Empty'));
  1696. }
  1697. function displayErrorOccured ($sMsg = '') {
  1698. header("HTTP/1.0 500 Internal Server Error");
  1699. $this->displayMsg($sMsg ? $sMsg : _t('_error occured'));
  1700. }
  1701. function displayPageNotFound ($sMsg = '') {
  1702. header("HTTP/1.0 404 Not Found");
  1703. $this->displayMsg($sMsg ? $sMsg : _t('_sys_request_page_not_found_cpt'));
  1704. }
  1705. function displayMsg ($s, $bTranslate = false) {
  1706. $sTitle = $bTranslate ? _t($s) : $s;
  1707. $oTemplate = BxDolTemplate::getInstance();
  1708. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  1709. $oTemplate->setPageHeader ($sTitle);
  1710. $oTemplate->setPageContent ('page_main_code', DesignBoxContent($sTitle, MsgBox($sTitle), BX_DB_PADDING_DEF));
  1711. $oTemplate->getPageCode();
  1712. exit;
  1713. }
  1714. /**
  1715. * * * * Static methods for work with template injections * * *
  1716. *
  1717. * Static method is used to add/replace the content of some key in the template.
  1718. * It's usefull when you don't want to modify existing template but need to add some data to existing template key.
  1719. *
  1720. * @param integer $iPageIndex - page index where injections would processed. Use 0 if you want it to be done on all the pages.
  1721. * @param string $sKey - template key.
  1722. * @param string $sValue - the data to be added.
  1723. * @return string the result of operation.
  1724. */
  1725. function processInjection($iPageIndex, $sKey, $sValue = "") {
  1726. if($iPageIndex != 0 && isset($this->aPage['injections']['page_0'][$sKey]) && isset($this->aPage['injections']['page_' . $iPageIndex][$sKey]))
  1727. $aSelection = @array_merge($this->aPage['injections']['page_0'][$sKey], $this->aPage['injections']['page_' . $iPageIndex][$sKey]);
  1728. else if(isset($this->aPage['injections']['page_0'][$sKey]))
  1729. $aSelection = $this->aPage['injections']['page_0'][$sKey];
  1730. else if(isset($this->aPage['injections']['page_' . $iPageIndex][$sKey]))
  1731. $aSelection = $this->aPage['injections']['page_' . $iPageIndex][$sKey];
  1732. else
  1733. $aSelection = array();
  1734. if(is_array($aSelection))
  1735. foreach($aSelection as $aInjection) {
  1736. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginInjection($sRand = time().rand());
  1737. switch($aInjection['type']) {
  1738. case 'text':
  1739. $sInjData = $aInjection['data'];
  1740. break;
  1741. case 'php':
  1742. ob_start();
  1743. $sInjData = eval($aInjection['data']);
  1744. if(!empty($sInjData))
  1745. ob_end_clean();
  1746. else
  1747. $sInjData = ob_get_clean();
  1748. break;
  1749. }
  1750. if((int)$aInjection['replace'] == 1)
  1751. $sValue = $sInjData;
  1752. else
  1753. $sValue .= $sInjData;
  1754. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endInjection($sRand, $aInjection);
  1755. }
  1756. return $sValue != '__' . $sKey . '__' ? str_replace('__' . $sKey . '__', '', $sValue) : $sValue;
  1757. }
  1758. /**
  1759. * Static method to add ingection available on the current page only.
  1760. *
  1761. * @param string $sKey - template's key.
  1762. * @param string $sType - injection type(text, php).
  1763. * @param string $sData - the data to be added.
  1764. * @param integer $iReplace - replace already existed data or not.
  1765. */
  1766. function addInjection($sKey, $sType, $sData, $iReplace = 0) {
  1767. $this->aPage['injections']['page_0'][$sKey][] = array(
  1768. 'page_index' => 0,
  1769. 'key' => $sKey,
  1770. 'type' => $sType,
  1771. 'data' => $sData,
  1772. 'replace' => $iReplace
  1773. );
  1774. }
  1775. function getPageCode($oTemplate = null) {
  1776. if (empty($oTemplate))
  1777. $oTemplate = $this;
  1778. header( 'Content-type: text/html; charset=utf-8' );
  1779. header( 'X-Frame-Options: sameorigin' );
  1780. echo $oTemplate->parsePageByName('page_' . $oTemplate->getPageNameIndex() . '.html', $oTemplate->getPageContent());
  1781. }
  1782. }
  1783. /** @} */