BxDolTemplate.php 76 KB

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