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