BxDolTemplate.php 74 KB

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