BxDolTemplate.php 78 KB

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