BxDolTemplate.php 78 KB

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