BxDolTemplate.php 73 KB

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