BxDolTemplate.php 78 KB

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