BxDolTemplate.php 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. ini_set('pcre.backtrack_limit', 1000000);
  10. define('BX_DOL_TEMPLATE_INJECTIONS_CACHE', 'sys_injections.inc');
  11. define('BX_DOL_TEMPLATE_CHECK_IN_BOTH', 'both');
  12. define('BX_DOL_TEMPLATE_CHECK_IN_BASE', 'base');
  13. define('BX_DOL_TEMPLATE_CHECK_IN_TMPL', 'tmpl');
  14. define('BX_DOL_COLOR_BG', 'bg');
  15. define('BX_DOL_COLOR_FT', 'ft');
  16. define('BX_DOL_PAGE_WIDTH', '1024px');
  17. /**
  18. * Page display levels.
  19. * Note. Both levels may refer to the same HTML templates.
  20. *
  21. * 'System' level. It uses page's NameIndex and
  22. * is mainly used for pages which aren't registered
  23. * in 'sys_objects_page' table. Also it's used
  24. * in 'Injections' engine.
  25. */
  26. define('BX_PAGE_DEFAULT', 0); ///< default, regular page
  27. define('BX_PAGE_CLEAR', 2); ///< clear page, without any headers and footers
  28. define('BX_PAGE_EMBED', 22); ///< page used for embeds
  29. define('BX_PAGE_POPUP', 44); ///< popup page, without any headers and footers
  30. define('BX_PAGE_TRANSITION', 150); ///< transition page with redirect to display some msg, like 'please wait', without headers footers
  31. /**
  32. * 'Builder based' level. It uses page's Type
  33. * from BxDolPage class and is used for pages
  34. * which are registered in 'sys_objects_page' table.
  35. * Changeable in Studio -> Pages Builder -> Settings.
  36. */
  37. define('BX_PAGE_TYPE_DEFAULT', 1); ///< default, regular page
  38. define('BX_PAGE_TYPE_DEFAULT_WO_HF', 2); ///< clear page, without any headers and footers
  39. /**
  40. * Template engine.
  41. *
  42. * An object of the class allows to:
  43. * 1. Manage HTML templates.
  44. * 2. Get URL/path for any template image/icon.
  45. * 3. Attach CSS/JavaScript files to the output.
  46. * 4. Add some content to any template key using Injection engine.
  47. *
  48. *
  49. * Avalable constructions.
  50. * 1. &lt;bx_include_auto:template_name.html /&gt; - the content of the file would be inserted. File would be taken from current template if it existes there, and from base directory otherwise.
  51. * 2. &lt;bx_include_base:template_name.html /&gt; - the content of the file would be inserted. File would be taken from base directory.
  52. * 3. &lt;bx_include_tmpl:template_name.html /&gt; - the content of the file would be inserted. File would be taken from tmpl_xxx directory.
  53. * 4. &lt;bx_url_root /&gt; - the value of BX_DOL_URL_ROOT variable will be inserted.
  54. * 5. &lt;bx_url_admin /&gt; - the value of BX_DOL_URL_ADMIN variable will be inserted.
  55. * 6. &lt;bx_text:_language_key /&gt; - _language_key will be translated using language file(function _t()) and inserted.
  56. &lt;bx_text_js:_language_key /&gt; - _language_key will be translated using language file(function _t()) and inserted, use it to insert text into js string.
  57. &lt;bx_text_attribute:_language_key /&gt; - _language_key will be translated using language file(function _t()) and inserted, use it to insert text into html attribute.
  58. * 7. &lt;bx_image_url:image_file_name /&gt; - image with 'image_file_name' file name will be searched in the images folder of current template.
  59. * 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.
  60. * 8. &lt;bx_icon_url:icon_file_name /&gt; - the same with &lt;bx_image_url:image_file_name /&gt;, but icons will be searched in the images/icons/ folders.
  61. * 9. &lt;bx_injection:injection_name /&gt; - will be replaced with injections registered with the page and injection_name in the `sys_injections`/`sys_injections_admin`/ tables.
  62. * 10. &lt;bx_if:tag_name&gt;some_HTML&lt;/bx_if:tag_name&gt; - will be replaced with provided content if the condition is true, and with empty string otherwise.
  63. * 11. &lt;bx_repeat:cycle_name&gt;some_HTML&lt;/bx_repeat:cycle_name&gt; - an inner HTML content will be repeated in accordance with received data.
  64. *
  65. *
  66. * Related classes:
  67. * BxDolTemplateAdmin - for processing admin templates.
  68. * Template classes in modules - for processing modiles' templates.
  69. *
  70. *
  71. * Global variables:
  72. * oSysTemplate - is used for template processing in user part.
  73. * oAdmTemplate - is used for template processing in admin part.
  74. *
  75. *
  76. * Add injection:
  77. * 1. Register it in the `sys_injections` table or `sys_injections_admin` table for admin panel.
  78. * 2. Clear injections cache(sys_injections.inc and sys_injections_admin.inc in cache folder).
  79. *
  80. *
  81. * Predefined template keys to add injections:
  82. * 1. injection_head - add injections in the &lt;head&gt; tag.
  83. * 2. injection_body - add ingection(attribute) in the &lt;body&gt; tag.
  84. * 3. injection_header - add injection inside the &lt;body&gt; tag at the very beginning.
  85. * 4. injection_logo_before - add injection at the left of the main logo(inside logo's DIV).
  86. * 5. injection_logo_after - add injection at the right of the main logo(inside logo's DIV).
  87. * 6. injection_between_logo_top_menu - add injection between logo and top menu.
  88. * 7. injection_top_menu_before - add injection at the left of the top menu(inside top menu's DIV).
  89. * 8. injection_top_menu_after - add injection at the right of the top menu(inside top menu's DIV).
  90. * 13. injection_content_before - add injection just before main content(inside content's DIV).
  91. * 14. injection_content_after - add injection just after main content(inside content's DIV).
  92. * 15. injection_between_content_footer - add injection between content and footer.
  93. * 16. injection_footer_before - add injection at the left of the footer(inside footer's DIV).
  94. * 17. injection_footer_after - add injection at the right of the footer(inside footer's DIV).
  95. * 18. injection_footer - add injection inside the &lt;body&gt; tag at the very end.
  96. *
  97. *
  98. * Example of usage:
  99. * @code
  100. * $oSysTemplate = BxDolTemplate::getInstance();
  101. *
  102. * $oSysTemplate->addCss(array('test1.css', 'test2.css'));
  103. * $oSysTemplate->addJs(array('test1.js', 'test2.js'));
  104. * $oSysTemplate->parseHtmlByName('messageBox.html', array(
  105. * 'id' => $iId,
  106. * 'msgText' => $sText,
  107. * 'bx_if:timer' => array(
  108. * 'condition' => $iTimer > 0,
  109. * 'content' => array(
  110. * 'id' => $iId,
  111. * 'time' => 1000 * $iTimer,
  112. * 'on_close' => $sOnClose,
  113. * )
  114. * ),
  115. * 'bx_if:timer' => array(
  116. * array(
  117. * 'name' => $sName,
  118. * 'title' => $sTitle
  119. * ),
  120. * array(
  121. * 'name' => $sName,
  122. * 'title' => $sTitle
  123. * )
  124. * )
  125. * ));
  126. * @endcode
  127. *
  128. *
  129. * Memberships/ACL:
  130. * Doesn't depend on user's membership.
  131. *
  132. *
  133. * Alerts:
  134. * no alerts available
  135. *
  136. */
  137. class BxDolTemplate extends BxDolFactory implements iBxDolSingleton
  138. {
  139. protected static $_sColorClassPrefix = 'col-';
  140. protected static $_sColorClassPrefixBg = 'bg-col-';
  141. protected static $_aColors = array(
  142. 'red1' => array(216, 9, 96),
  143. 'red1-dark' => array(194, 7, 86),
  144. 'red2' => array(231, 68, 30),
  145. 'red2-dark' => array(207, 60, 25),
  146. 'red3' => array(243, 143, 0),
  147. 'red3-dark' => array(218, 128, 0),
  148. 'green1' => array(96, 174, 0),
  149. 'green1-dark' => array(86, 156, 0),
  150. 'green2' => array(209, 211, 0),
  151. 'green2-dark' => array(186, 188, 0),
  152. 'green3' => array(48, 116, 36),
  153. 'green3-dark' => array(43, 104, 32),
  154. 'blue1' => array(10, 61, 143),
  155. 'blue1-dark' => array(9, 54, 128),
  156. 'blue2' => array(0, 164, 165),
  157. 'blue2-dark' => array(0, 146, 148),
  158. 'blue3' => array(0, 160, 206),
  159. 'blue3-dark' => array(0, 143, 184),
  160. 'gray' => array(97, 97, 97),
  161. 'gray-dark' => array(87, 87, 87)
  162. );
  163. /**
  164. * Main fields
  165. */
  166. protected $_sName;
  167. protected $_sPrefix;
  168. protected $_sRootPath;
  169. protected $_sRootUrl;
  170. protected $_sSubPath;
  171. protected $_sInjectionsTable;
  172. protected $_sInjectionsCache;
  173. protected $_sCode;
  174. protected $_sCodeKey;
  175. protected $_iMix;
  176. protected $_sMixKey;
  177. protected $_sKeyWrapperHtml;
  178. protected $_sFolderHtml;
  179. protected $_sFolderCss;
  180. protected $_sFolderImages;
  181. protected $_sFolderIcons;
  182. protected $_aTemplates;
  183. protected $_aLocations;
  184. protected $_aLocationsJs;
  185. /**
  186. * Cache related fields
  187. */
  188. protected $_bCacheEnable;
  189. protected $_sCacheFolderUrl;
  190. protected $_sCachePublicFolderUrl;
  191. protected $_sCachePublicFolderPath;
  192. protected $_sCacheFilePrefix;
  193. protected $_aCacheExceptions;
  194. protected $_bImagesInline;
  195. protected $_iImagesMaxSize;
  196. protected $_bCssLess;
  197. protected $_bCssCache;
  198. protected $_bCssMinify;
  199. protected $_bCssArchive;
  200. protected $_sCssLessPrefix;
  201. protected $_sCssCachePrefix;
  202. protected $_bJsLess;
  203. protected $_bJsCache;
  204. protected $_bJsMinify;
  205. protected $_bJsArchive;
  206. protected $_sJsCachePrefix;
  207. protected $aPage;
  208. protected $aPageContent;
  209. protected $aPageSnapshot = array();
  210. protected $_oTemplateConfig;
  211. protected $_oTemplateFunctions;
  212. /**
  213. * Constructor
  214. */
  215. protected function __construct($sRootPath = BX_DIRECTORY_PATH_ROOT, $sRootUrl = BX_DOL_URL_ROOT)
  216. {
  217. if(isset($GLOBALS['bxDolClasses'][get_class($this)]))
  218. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  219. parent::__construct();
  220. $this->_sPrefix = 'BxDolTemplate';
  221. $this->_sRootPath = $sRootPath;
  222. $this->_sRootUrl = $sRootUrl;
  223. $this->_sInjectionsTable = 'sys_injections';
  224. $this->_sInjectionsCache = BX_DOL_TEMPLATE_INJECTIONS_CACHE;
  225. $this->_sCodeKey = BX_DOL_TEMPLATE_CODE_KEY;
  226. $this->_sMixKey = BX_DOL_TEMPLATE_MIX_KEY;
  227. list(
  228. $this->_sCode,
  229. $this->_sName,
  230. $this->_sSubPath
  231. ) = self::retrieveCode($this->_sCodeKey, $this->_sMixKey, $this->_sRootPath);
  232. $this->_iMix = 0;
  233. if(is_array($this->_sCode))
  234. list($this->_sCode, $this->_iMix) = $this->_sCode;
  235. if(!$this->_sSubPath)
  236. $this->_sSubPath = 'boonex/' . BX_DOL_TEMPLATE_DEFAULT_CODE . '/';
  237. if(!file_exists(BX_DIRECTORY_PATH_MODULES . $this->_sSubPath)) // just for 8.0.0-A6 upgrade
  238. $this->_sSubPath = 'boonex/uni/';
  239. if(isset($_GET[$this->_sCodeKey])) {
  240. if(BxDolPermalinks::getInstance()->redirectIfNecessary(array($this->_sCodeKey)))
  241. exit;
  242. }
  243. $this->_sKeyWrapperHtml = '__';
  244. $this->_sFolderHtml = '';
  245. $this->_sFolderCss = 'css/';
  246. $this->_sFolderImages = 'images/';
  247. $this->_sFolderIcons = 'images/icons/';
  248. $this->_aTemplates = array('html_tags', 'menu_item_addon', 'menu_item_addon_small', 'menu_item_addon_middle');
  249. $this->addLocation('system', $this->_sRootPath, $this->_sRootUrl);
  250. $this->addLocationJs('system_inc_js', BX_DIRECTORY_PATH_INC . 'js/' , BX_DOL_URL_ROOT . 'inc/js/');
  251. $this->addLocationJs('system_inc_js_classes', BX_DIRECTORY_PATH_INC . 'js/classes/' , BX_DOL_URL_ROOT . 'inc/js/classes/');
  252. $this->addLocationJs('system_plugins_public', BX_DIRECTORY_PATH_PLUGINS_PUBLIC, BX_DOL_URL_PLUGINS_PUBLIC);
  253. $this->_bCacheEnable = !defined('BX_DOL_CRON_EXECUTE') && getParam('sys_template_cache_enable') == 'on';
  254. $this->_sCacheFolderUrl = '';
  255. $this->_sCachePublicFolderUrl = BX_DOL_URL_CACHE_PUBLIC;
  256. $this->_sCachePublicFolderPath = BX_DIRECTORY_PATH_CACHE_PUBLIC;
  257. $this->_sCacheFilePrefix = "bx_templ_";
  258. $this->_aCacheExceptions = ['menu_icon.html'];
  259. $this->_bImagesInline = getParam('sys_template_cache_image_enable') == 'on';
  260. $this->_iImagesMaxSize = (int)getParam('sys_template_cache_image_max_size') * 1024;
  261. $bArchive = getParam('sys_template_cache_compress_enable') == 'on';
  262. $this->_bCssLess = true; //--- Less cannot be disabled for CSS.
  263. $this->_bCssCache = !defined('BX_DOL_CRON_EXECUTE') && getParam('sys_template_cache_css_enable') == 'on';
  264. $this->_bCssMinify = $this->_bCssCache && getParam('sys_template_cache_minify_css_enable') == 'on';
  265. $this->_bCssArchive = $this->_bCssCache && $bArchive;
  266. $this->_sCssLessPrefix = $this->_sCacheFilePrefix . 'less_';
  267. $this->_sCssCachePrefix = $this->_sCacheFilePrefix . 'css_';
  268. $this->_bJsLess = false; //--- Less language isn't available for JS at all.
  269. $this->_bJsCache = !defined('BX_DOL_CRON_EXECUTE') && getParam('sys_template_cache_js_enable') == 'on';
  270. $this->_bJsMinify = $this->_bJsCache && getParam('sys_template_cache_minify_js_enable') == 'on';
  271. $this->_bJsArchive = $this->_bJsCache && $bArchive;
  272. $this->_sJsCachePrefix = $this->_sCacheFilePrefix . 'js_';
  273. $this->aPage = array();
  274. $this->aPageContent = array();
  275. }
  276. /**
  277. * Prevent cloning the instance
  278. */
  279. public function __clone()
  280. {
  281. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  282. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  283. }
  284. /**
  285. * Get singleton instance of the class
  286. */
  287. public static function getInstance()
  288. {
  289. if(!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  290. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolTemplate();
  291. $GLOBALS['bxDolClasses'][__CLASS__]->init();
  292. }
  293. return $GLOBALS['bxDolClasses'][__CLASS__];
  294. }
  295. /**
  296. * Retrieve template code and check whether it's associated with active template or not.
  297. *
  298. * @param string $sCodeKey template's code key.
  299. * @param string $sMixKey template's mix key.
  300. * @param string $sRootPath path to root directory.
  301. */
  302. public static function retrieveCode($sCodeKey = BX_DOL_TEMPLATE_CODE_KEY, $sMixKey = BX_DOL_TEMPLATE_MIX_KEY, $sRootPath = BX_DIRECTORY_PATH_ROOT)
  303. {
  304. $oDb = BxDolDb::getInstance();
  305. $fCheckCode = function($sCode, $bSetCookie) use($sCodeKey, $sRootPath) {
  306. if(empty($sCode) || !preg_match('/^[A-Za-z0-9_-]+$/', $sCode))
  307. return false;
  308. $aModule = BxDolModuleQuery::getInstance()->getModuleByUri($sCode);
  309. if(empty($aModule) || !is_array($aModule) || (int)$aModule['enabled'] != 1 || !file_exists($sRootPath . 'modules/' . $aModule['path'] . 'data/template/'))
  310. return false;
  311. $oConfig = new BxDolModuleConfig($aModule);
  312. $aResult = array(
  313. $oConfig->getUri(), //--- Template module's URI is used as template Code.
  314. $oConfig->getName(),
  315. $oConfig->getDirectory()
  316. );
  317. if(!$bSetCookie || bx_get('preview'))
  318. return $aResult;
  319. $aUrl = parse_url(BX_DOL_URL_ROOT);
  320. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  321. setcookie($sCodeKey, $sCode, time() + 60*60*24*365, $sPath);
  322. return $aResult;
  323. };
  324. $fCheckMix = function($aResult, $iMix, $bSetCookie) use($sMixKey, $sRootPath, $oDb) {
  325. list($sCode, $sName) = $aResult;
  326. if(empty($sName) || empty($iMix))
  327. return false;
  328. $aMix = $oDb->getParamsMix($iMix);
  329. if(empty($aMix) || !is_array($aMix) || $aMix['type'] != $sName)
  330. return false;
  331. if(!$bSetCookie)
  332. return $iMix;
  333. $aUrl = parse_url(BX_DOL_URL_ROOT);
  334. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  335. setcookie($sMixKey, $iMix, time() + 60*60*24*365, $sPath);
  336. return $iMix;
  337. };
  338. $sCode = getParam('template');
  339. if(empty($sCode))
  340. $sCode = BX_DOL_TEMPLATE_DEFAULT_CODE;
  341. $aResult = $fCheckCode($sCode, false);
  342. //--- Check selected template in COOKIE(the lowest priority) ---//
  343. $sCode = !empty($_COOKIE[$sCodeKey]) ? $_COOKIE[$sCodeKey] : '';
  344. $aResultCheck = $fCheckCode($sCode, false);
  345. if($aResultCheck !== false)
  346. $aResult = $aResultCheck;
  347. //--- Check selected template in GET(the highest priority) ---//
  348. $sCode = !empty($_GET[$sCodeKey]) ? $_GET[$sCodeKey] : '';
  349. $aResultCheck = $fCheckCode($sCode, true);
  350. if($aResultCheck !== false)
  351. $aResult = $aResultCheck;
  352. if($aResult === false)
  353. return $aResult;
  354. if(!is_array($aResult[0]))
  355. $aResult[0] = array($aResult[0]);
  356. $iMixDefault = !empty($aResult[1]) ? (int)getParam($aResult[1] . '_default_mix') : 0;
  357. //--- Check selected mix in COOKIE(the lowest priority) ---//
  358. $iMix = !empty($_COOKIE[$sMixKey]) ? (int)$_COOKIE[$sMixKey] : 0;
  359. $iResultCheck = $fCheckMix($aResult, $iMix, false);
  360. if($iResultCheck !== false) {
  361. $aMix = $oDb->getParamsMix($iMix);
  362. if((int)$aMix['published'] == 0 && $iMix != $iMixDefault) {
  363. $aUrl = parse_url(BX_DOL_URL_ROOT);
  364. $sPath = isset($aUrl['path']) && !empty($aUrl['path']) ? $aUrl['path'] : '/';
  365. setcookie($sMixKey, '', time() - 96 * 3600, $sPath);
  366. unset($_COOKIE[$sMixKey]);
  367. }
  368. else
  369. $aResult[0][1] = $iResultCheck;
  370. }
  371. //--- Check selected mix in GET(the highest priority) ---//
  372. $iMix = !empty($_GET[$sMixKey]) ? (int)$_GET[$sMixKey] : 0;
  373. $iResultCheck = $fCheckMix($aResult, $iMix, true);
  374. if($iResultCheck !== false)
  375. $aResult[0][1] = $iResultCheck;
  376. //--- Get default mix for currently selected template ---//
  377. if(empty($aResult[0][1]) && !empty($iMixDefault)) {
  378. $iResultCheck = $fCheckMix($aResult, $iMixDefault, false);
  379. if($iResultCheck !== false)
  380. $aResult[0][1] = $iResultCheck;
  381. }
  382. if(is_array($aResult[0]) && count($aResult[0]) == 1)
  383. $aResult[0] = $aResult[0][0];
  384. return $aResult;
  385. }
  386. public function getIncludedUrls($sType)
  387. {
  388. if (!isset($this->aPage[$sType]))
  389. return array();
  390. $a = array();
  391. foreach ($this->aPage[$sType] as $r)
  392. $a[] = $r['url'];
  393. return $a;
  394. }
  395. /**
  396. * Remember current state of aPage variable with all css, js, etc
  397. */
  398. public function collectingStart()
  399. {
  400. $this->aPageSnapshot = $this->aPage;
  401. }
  402. public function collectingInject($aCss, $aJs)
  403. {
  404. $a = array('css' => 'aCss', 'js' => 'aJs');
  405. foreach ($a as $s => $sVar) {
  406. if (empty($$sVar))
  407. continue;
  408. $sKey = $s . '_compiled';
  409. foreach ($$sVar as $r)
  410. $this->aPage[$sKey][] = $r;
  411. }
  412. }
  413. /**
  414. * Get difference for non-system css and js files from previously remembered state as ready HTML code,
  415. * additionally filter out css and js from $aExcludeCss and $aExcludeJs arrays
  416. */
  417. public function collectingEndGetCode($aExcludeCss = array(), $aExcludeJs = array(), $sFormat = 'html')
  418. {
  419. $aPageSave = $this->aPage; // save current state to restore later
  420. // filter funcs
  421. $fFilterCss = function ($a) use ($aExcludeCss) {
  422. if (in_array($a['url'], $aExcludeCss))
  423. return false;
  424. if (isset($this->aPageSnapshot['css_compiled']))
  425. foreach ($this->aPageSnapshot['css_compiled'] as $r)
  426. if ($r['url'] == $a['url'])
  427. return false;
  428. return true;
  429. };
  430. $fFilterJs = function ($a) use ($aExcludeJs) {
  431. if (in_array($a['url'], $aExcludeJs))
  432. return false;
  433. if (isset($this->aPageSnapshot['js_compiled']))
  434. foreach ($this->aPageSnapshot['js_compiled'] as $r)
  435. if ($r['url'] == $a['url'])
  436. return false;
  437. return true;
  438. };
  439. // diff aPageSnapshot and aPage and output only newly added css/js
  440. $this->aPage['css_compiled'] = array_filter($this->aPage['css_compiled'], $fFilterCss);
  441. $this->aPage['js_compiled'] = array_filter($this->aPage['js_compiled'], $fFilterJs);
  442. // return js/css
  443. $mixedRet = '';
  444. if ('html' == $sFormat) {
  445. $mixedRet .= $this->includeFiles('css');
  446. $mixedRet .= $this->includeFiles('js');
  447. }
  448. else {
  449. $mixedRet = array(
  450. 'css' => $this->aPage['css_compiled'],
  451. 'js' => $this->aPage['js_compiled'],
  452. );
  453. }
  454. // restore original state
  455. $this->aPageSnapshot = array();
  456. $this->aPage = $aPageSave;
  457. return $mixedRet;
  458. }
  459. public function getClassName()
  460. {
  461. return get_class($this);
  462. }
  463. public static function getColorPalette()
  464. {
  465. $aResult = self::$_aColors;
  466. bx_alert('system', 'get_color_palette', 0, false, array(
  467. 'override_result' => &$aResult
  468. ));
  469. if($aResult != self::$_aColors) {
  470. $oTemplate = self::getInstance();
  471. foreach($aResult as $sName => $aRgb) {
  472. $sRgb = 'rgb(' . trim(implode(', ', $aRgb)) . ') !important';
  473. $oTemplate->addCssStyle('.' . self::$_sColorClassPrefix . $sName, array(
  474. 'color' => $sRgb
  475. ));
  476. $oTemplate->addCssStyle('.' . self::$_sColorClassPrefixBg . $sName, array(
  477. 'background-color' => $sRgb
  478. ));
  479. }
  480. }
  481. return $aResult;
  482. }
  483. public static function getColorCode($mixedName = false, $fOpacity = false)
  484. {
  485. $aPalette = self::getColorPalette();
  486. $aClasses = array_keys($aPalette);
  487. if($mixedName === false || (is_string($mixedName) && !is_numeric($mixedName) && !in_array($mixedName, $aClasses)))
  488. $mixedName = $aClasses[rand(0, count($aClasses) - 1)];
  489. else if(is_numeric($mixedName))
  490. $mixedName = $aClasses[(int)$mixedName % count($aClasses)];
  491. $aColor = $aPalette[$mixedName];
  492. if($fOpacity !== false && is_numeric($fOpacity))
  493. $aColor[] = $fOpacity;
  494. return $aColor;
  495. }
  496. public static function getColorClass($sType = BX_DOL_COLOR_FT, $sName = '')
  497. {
  498. $aClasses = array_keys(self::getColorPalette());
  499. if(empty($sName) || !in_array($sName, $aClasses))
  500. $sName = $aClasses[rand(0, count($aClasses) - 1)];
  501. $sPrefix = '';
  502. switch ($sType) {
  503. case BX_DOL_COLOR_FT:
  504. $sPrefix = self::$_sColorClassPrefix;
  505. break;
  506. case BX_DOL_COLOR_BG:
  507. $sPrefix = self::$_sColorClassPrefixBg;
  508. break;
  509. }
  510. return $sPrefix . $sName;
  511. }
  512. /**
  513. * Load templates.
  514. */
  515. function loadTemplates()
  516. {
  517. $aResult = array();
  518. foreach($this->_aTemplates as $sName)
  519. $aResult[$sName] = $this->getHtml($sName . '.html');
  520. $this->_aTemplates = $aResult;
  521. }
  522. /**
  523. * Initialize template engine.
  524. * Note. The method is executed with the system, you shouldn't execute it in your subclasses.
  525. */
  526. function init()
  527. {
  528. $this->loadTemplates();
  529. //--- Load page elements related static variables ---//
  530. $this->aPage = array(
  531. 'name_index' => BX_PAGE_DEFAULT,
  532. 'type' => BX_PAGE_TYPE_DEFAULT,
  533. 'header' => '',
  534. 'header_text' => '',
  535. 'keywords' => array(),
  536. 'location' => array(),
  537. 'description' => '',
  538. 'robots' => '',
  539. 'css_name' => array(),
  540. 'css_compiled' => array(),
  541. 'css_system' => array(),
  542. 'css_async' => array(),
  543. 'js_name' => array(),
  544. 'js_compiled' => array(),
  545. 'js_system' => array(),
  546. 'js_options' => array(),
  547. 'js_translations' => array(),
  548. 'js_images' => array(),
  549. 'injections' => array()
  550. );
  551. //--- Load default CSS, JS, etc ---//
  552. BxDolPreloader::getInstance()->perform($this);
  553. //--- Load injection's cache ---//
  554. if (getParam('sys_db_cache_enable')) {
  555. $oDb = BxDolDb::getInstance();
  556. $oCache = $oDb->getDbCacheObject();
  557. $sCacheKey = $oDb->genDbCacheKey($this->_sInjectionsCache);
  558. $aInjections = $oCache->getData($sCacheKey);
  559. if ($aInjections === null) {
  560. $aInjections = $this->getInjectionsData();
  561. $oCache->setData ($sCacheKey, $aInjections);
  562. }
  563. }
  564. else
  565. $aInjections = $this->getInjectionsData();
  566. $this->aPage['injections'] = $aInjections;
  567. bx_import('BxTemplConfig'); // TODO: for some reason autoloader isn't working here....
  568. $this->_oTemplateConfig = BxTemplConfig::getInstance();
  569. bx_import('BxTemplFunctions');
  570. $this->_oTemplateFunctions = BxTemplFunctions::getInstance($this);
  571. }
  572. protected function getInjectionsData ()
  573. {
  574. $oDb = BxDolDb::getInstance();
  575. $aInjections = $oDb->getAll("SELECT `page_index`, `name`, `key`, `type`, `data`, `replace` FROM `" . $this->_sInjectionsTable . "` WHERE `active`='1'");
  576. if (!$aInjections)
  577. return array();
  578. foreach ($aInjections as $aInjection)
  579. $aInjections['page_' . $aInjection['page_index']][$aInjection['key']][] = $aInjection;
  580. return $aInjections;
  581. }
  582. /**
  583. * Set page name index
  584. * @param int $i name index
  585. */
  586. function setPageNameIndex($i)
  587. {
  588. $this->aPage['name_index'] = $i;
  589. }
  590. /**
  591. * Get page name index
  592. * @return int $i name index
  593. */
  594. function getPageNameIndex()
  595. {
  596. return isset($this->aPage['name_index']) ? (int)$this->aPage['name_index'] : 0;
  597. }
  598. /**
  599. * Set page type
  600. * @param int $i page type
  601. */
  602. function setPageType($a)
  603. {
  604. $this->aPage['type'] = $a;
  605. }
  606. /**
  607. * Get page type
  608. * @return int $i page type
  609. */
  610. function getPageType()
  611. {
  612. return isset($this->aPage['type']) ? (int)$this->aPage['type'] : BX_PAGE_TYPE_DEFAULT;
  613. }
  614. /**
  615. * Set page header
  616. * @param string $s page header
  617. */
  618. function setPageHeader($s)
  619. {
  620. $this->aPage['header'] = $s;
  621. }
  622. /**
  623. * Get page header
  624. * @return string $s page header
  625. */
  626. function getPageHeader()
  627. {
  628. return $this->aPage['header'];
  629. }
  630. /**
  631. * Set page params. Available page params are: name_index, header
  632. * @param array $a page params
  633. */
  634. function setPageParams($a)
  635. {
  636. if (!empty($this->aPage))
  637. $this->aPage = array_merge($this->aPage, $a);
  638. else
  639. $this->aPage = $a;
  640. }
  641. /**
  642. * Get page params.
  643. * @return array $a page params
  644. */
  645. function getPageParams()
  646. {
  647. return $this->aPage;
  648. }
  649. /**
  650. * Set page description.
  651. *
  652. * @param string $sDescription necessary page description.
  653. */
  654. function setPageDescription($sDescription)
  655. {
  656. $this->aPage['description'] = $sDescription;
  657. }
  658. /**
  659. * Set page meta robots.
  660. *
  661. * @param string $s page meta robots.
  662. */
  663. function setPageMetaRobots($s)
  664. {
  665. $this->aPage['robots'] = $s;
  666. }
  667. /**
  668. * Set page injections.
  669. *
  670. * @param array $aInjections name => value injections.
  671. */
  672. function setPageInjections($aInjections)
  673. {
  674. if(empty($aInjections) || !is_array($aInjections))
  675. return;
  676. foreach($aInjections as $sName => $sValue)
  677. $this->addInjection('injection_' . $sName, 'text', $sValue);
  678. }
  679. /**
  680. * Set page content for some variable.
  681. * @param string $sVar name of content variable
  682. * @param string $sContent content for $sVar variable
  683. * @param int $iIndex optional page index, default is index which was set before with @see setPageNameIndex function, or 0
  684. */
  685. function setPageContent($sVar, $sContent, $iIndex = false)
  686. {
  687. $i = false !== $iIndex ? $iIndex : $this->getPageNameIndex();
  688. $this->aPageContent[$i][$sVar] = $sContent;
  689. }
  690. /**
  691. * Get page content for some variable.
  692. * @param string $sVar name of content variable
  693. * @param int $iIndex optional page index, default is index which was set before with @see setPageNameIndex function, or 0
  694. * @return string page content for some variable or for the whole page.
  695. */
  696. function getPageContent($sVar = false, $iIndex = false)
  697. {
  698. $i = false !== $iIndex ? $iIndex : $this->getPageNameIndex();
  699. return false !== $sVar ? $this->aPageContent[$i][$sVar] : $this->aPageContent[$i];
  700. }
  701. /**
  702. * Get currently active template name.
  703. *
  704. * @return string template's name.
  705. */
  706. function getName()
  707. {
  708. return $this->_sName;
  709. }
  710. /**
  711. * Get currently active template code.
  712. *
  713. * @return string template's code.
  714. */
  715. function getCode()
  716. {
  717. return $this->_sCode;
  718. }
  719. /**
  720. * Get code key.
  721. *
  722. * @return string template's code key.
  723. */
  724. function getCodeKey()
  725. {
  726. return $this->_sCodeKey;
  727. }
  728. /**
  729. * Get currently active template mix.
  730. *
  731. * @return integer template's mix.
  732. */
  733. function getMix()
  734. {
  735. return $this->_iMix;
  736. }
  737. /**
  738. * Get currently active template path.
  739. *
  740. * @return string template's path.
  741. */
  742. function getPath()
  743. {
  744. return $this->_sSubPath;
  745. }
  746. /**
  747. * Set page title.
  748. * @deprecated use setPageHeader
  749. *
  750. * @param string $sTitle necessary page title.
  751. */
  752. function setPageTitle($sTitle)
  753. {
  754. $this->setPageHeader($sTitle);
  755. }
  756. /**
  757. * Set page's main box title.
  758. * @deprecated use setPageParams
  759. *
  760. * @param string $sTitle necessary page's main box title.
  761. */
  762. function setPageMainBoxTitle($sTitle)
  763. {
  764. $this->setPageParams(array('header_text' => $sTitle));
  765. }
  766. /**
  767. * Check whether location exists or not.
  768. * @param string $sKey - location's unique key.
  769. */
  770. function isLocation($sKey)
  771. {
  772. return isset($this->_aLocations[$sKey]);
  773. }
  774. /**
  775. * Get a lis of all added locations.
  776. * @return array with locations.
  777. */
  778. function getLocations()
  779. {
  780. return $this->_aLocations;
  781. }
  782. /**
  783. * Add location in array of locations.
  784. * Note. Location is the path/url to folder where 'templates' folder is stored.
  785. *
  786. * @param string $sKey - location's unique key.
  787. * @param string $sLocationPath - location's path. For modules: '[path_to_script]/modules/[vendor_name]/[module_name]/'
  788. * @param string $sLocationUrl - location's url. For modules: '[url_to_script]/modules/[vendor_name]/[module_name]/'
  789. */
  790. function addLocation($sKey, $sLocationPath, $sLocationUrl)
  791. {
  792. $this->_aLocations[$sKey] = array(
  793. 'path' => $sLocationPath,
  794. 'url' => $sLocationUrl,
  795. );
  796. return $sKey;
  797. }
  798. /**
  799. * Add dynamic location.
  800. *
  801. * @param string $sLocationPath - location's path. For modules: '[path_to_script]/modules/[vendor_name]/[module_name]/'
  802. * @param string $sLocationUrl - location's url. For modules: '[url_to_script]/modules/[vendor_name]/[module_name]/'
  803. * @return location key. Is needed to remove the location.
  804. */
  805. function addDynamicLocation($sLocationPath, $sLocationUrl)
  806. {
  807. $sLocationKey = time() . mt_rand();
  808. $this->addLocation($sLocationKey, $sLocationPath, $sLocationUrl);
  809. return $sLocationKey;
  810. }
  811. /**
  812. * Remove location from array of locations.
  813. * Note. Location is the path/url to folder where templates are stored.
  814. *
  815. * @param string $sKey - location's unique key.
  816. */
  817. function removeLocation($sKey)
  818. {
  819. if(isset($this->_aLocations[$sKey]))
  820. unset($this->_aLocations[$sKey]);
  821. }
  822. /**
  823. * Check whether JS location exists or not.
  824. * @param string $sKey - JS location's unique key.
  825. */
  826. function isLocationJs($sKey)
  827. {
  828. return isset($this->_aLocationsJs[$sKey]);
  829. }
  830. /**
  831. * Add JS location in array of JS locations.
  832. * Note. Location is the path/url to folder where JS files are stored.
  833. *
  834. * @param string $sKey - location's unique key.
  835. * @param string $sLocationPath - location's path. For modules: '[path_to_script]/modules/[vendor_name]/[module_name]/js/'
  836. * @param string $sLocationUrl - location's url. For modules: '[url_to_script]/modules/[vendor_name]/[module_name]/js/'
  837. */
  838. function addLocationJs($sKey, $sLocationPath, $sLocationUrl)
  839. {
  840. $this->_aLocationsJs[$sKey] = array(
  841. 'path' => $sLocationPath,
  842. 'url' => $sLocationUrl
  843. );
  844. return $sKey;
  845. }
  846. /**
  847. * Add dynamic JS location.
  848. *
  849. * @param string $sLocationPath - location's path. For modules: '[path_to_script]/modules/[vendor_name]/[module_name]/'
  850. * @param string $sLocationUrl - location's url. For modules: '[url_to_script]/modules/[vendor_name]/[module_name]/'
  851. * @return location key. Is needed to remove the location.
  852. */
  853. function addDynamicLocationJs($sLocationPath, $sLocationUrl)
  854. {
  855. $sLocationKey = time() . mt_rand();
  856. $this->addLocationJs($sLocationKey, $sLocationPath, $sLocationUrl);
  857. return $sLocationKey;
  858. }
  859. /**
  860. * Remove JS location from array of locations.
  861. * Note. Location is the path/url to folder where templates are stored.
  862. *
  863. * @param string $sKey - JS location's unique key.
  864. */
  865. function removeLocationJs($sKey)
  866. {
  867. if(isset($this->_aLocationsJs[$sKey]))
  868. unset($this->_aLocationsJs[$sKey]);
  869. }
  870. /**
  871. * Add Option in JS output.
  872. *
  873. * @param mixed $mixedName option's name or an array of options' names.
  874. */
  875. function addJsOption($mixedName)
  876. {
  877. if(is_string($mixedName))
  878. $mixedName = array($mixedName);
  879. foreach($mixedName as $sName)
  880. $this->aPage['js_options'][$sName] = getParam($sName);
  881. }
  882. /**
  883. * Add language translation for key in JS output.
  884. *
  885. * @param mixed $mixedKey language key or an array of keys.
  886. */
  887. function addJsTranslation($mixedKey, $bDynamic = false)
  888. {
  889. if(is_string($mixedKey))
  890. $mixedKey = array($mixedKey);
  891. foreach($mixedKey as $sKey)
  892. $this->aPage['js_translations'][$sKey] = _t($sKey, '{0}', '{1}');
  893. return $bDynamic ? $this->_processJsTranslations() : '';
  894. }
  895. /**
  896. * Add image in JS output.
  897. *
  898. * @param array $aImages an array of image descriptors.
  899. * The descriptor is a key/value pear in the array of descriptors.
  900. */
  901. function addJsImage($aImages)
  902. {
  903. if(!is_array($aImages))
  904. return;
  905. foreach($aImages as $sKey => $sFile) {
  906. $sUrl = $this->getImageUrl($sFile);
  907. if(empty($sUrl))
  908. continue;
  909. $this->aPage['js_images'][$sKey] = $sUrl;
  910. }
  911. }
  912. /**
  913. * Add icon in JS output.
  914. *
  915. * @param array $aIcons an array of icons descriptors.
  916. * The descriptor is a key/value pear in the array of descriptors.
  917. */
  918. function addJsIcon($aIcons)
  919. {
  920. if(!is_array($aIcons))
  921. return;
  922. foreach($aIcons as $sKey => $sFile) {
  923. $sUrl = $this->getIconUrl($sFile);
  924. if(empty($sUrl))
  925. continue;
  926. $this->aPage['js_images'][$sKey] = $sUrl;
  927. }
  928. }
  929. /**
  930. * Add CSS style.
  931. *
  932. * @param string $sName CSS class name.
  933. * @param string $sContent CSS class styles.
  934. */
  935. function addCssStyle($sName, $sContent)
  936. {
  937. $this->aPage['css_styles'][$sName] = $sContent;
  938. }
  939. /**
  940. * Set page keywords.
  941. *
  942. * @param mixed $mixedKeywords necessary page keywords(string - single keyword, array - an array of keywords).
  943. * @param string $sDevider - string devider.
  944. */
  945. function addPageKeywords($mixedKeywords, $sDevider = ',')
  946. {
  947. if(is_string($mixedKeywords))
  948. $mixedKeywords = strpos($mixedKeywords, $sDevider) !== false ? explode($sDevider, $mixedKeywords) : array($mixedKeywords);
  949. foreach($mixedKeywords as $iKey => $sValue)
  950. $mixedKeywords[$iKey] = trim($sValue);
  951. $this->aPage['keywords'] = isset($this->aPage['keywords']) && is_array($this->aPage['keywords']) ? array_merge($this->aPage['keywords'], $mixedKeywords) : $mixedKeywords;
  952. }
  953. /**
  954. * Set page locatoin coordinates.
  955. *
  956. * @param $fLat latitude
  957. * @param $fLng longitude
  958. */
  959. function addPageMetaLocation($fLat, $fLng, $sCountryCode)
  960. {
  961. $this->aPage['location'] = array('lat' => $fLat, 'lng' => $fLng, 'country' => $sCountryCode);
  962. }
  963. /**
  964. * Set page meta image.
  965. *
  966. * @param $sImageUrl meta image url
  967. */
  968. function addPageMetaImage($sImageUrl)
  969. {
  970. $this->aPage['image'] = $sImageUrl;
  971. }
  972. /**
  973. * Set page rss link.
  974. *
  975. * @param $sTitle - rss feed title
  976. * @param $sUrl - rss feed URL
  977. */
  978. function addPageRssLink($sTitle, $sUrl)
  979. {
  980. if (!isset($this->aPage['rss']))
  981. $this->aPage['rss'] = array('title' => $sTitle, 'url' => $sUrl);
  982. else
  983. $this->aPage['rss'] = false;
  984. }
  985. /**
  986. * Returns page meta info, like meta keyword, meta description, location, etc
  987. */
  988. function getMetaInfo()
  989. {
  990. $sRet = '';
  991. // general meta tags
  992. if (!empty($this->aPage['keywords']) && is_array($this->aPage['keywords']))
  993. $sRet .= '<meta name="keywords" content="' . bx_html_attribute(implode(',', $this->aPage['keywords'])) . '" />';
  994. $bDescription = !empty($this->aPage['description']) && is_string($this->aPage['description']);
  995. if ($bDescription)
  996. $sRet .= '<meta name="description" content="' . bx_html_attribute($this->aPage['description']) . '" />';
  997. // location
  998. if (!empty($this->aPage['location']) && isset($this->aPage['location']['lat']) && isset($this->aPage['location']['lng']) && isset($this->aPage['location']['country']))
  999. $sRet .= '
  1000. <meta name="ICBM" content="' . $this->aPage['location']['lat'] . ';' . $this->aPage['location']['lng'] . '" />
  1001. <meta name="geo.position" content="' . $this->aPage['location']['lat'] . ';' . $this->aPage['location']['lng'] . '" />
  1002. <meta name="geo.region" content="' . bx_html_attribute($this->aPage['location']['country']) . '" />';
  1003. // set cover image as meta[image] value
  1004. if (empty($this->aPage['image']) && ($oPage = BxDolPage::getObjectInstanceByURI()) !== false){
  1005. $aCover = $oPage->getPageCoverImage();
  1006. if ($aCover){
  1007. $oCover = BxDolCover::getInstance($this);
  1008. $this->aPage['image'] = $oCover->getCoverImageUrl($aCover);
  1009. }
  1010. }
  1011. // facebook / twitter
  1012. $bPageImage = !empty($this->aPage['image']);
  1013. $sRet .= '<meta name="twitter:card" content="' . ($bPageImage ? 'summary_large_image' : 'summary') . '" />';
  1014. if ($bPageImage)
  1015. $sRet .= '<meta property="og:image" content="' . $this->aPage['image'] . '" />';
  1016. $sRet .= '
  1017. <meta property="og:title" content="' . (isset($this->aPage['header']) ? bx_html_attribute(strip_tags($this->aPage['header'])) : '') . '" />
  1018. <meta property="og:description" content="' . ($bDescription ? bx_html_attribute($this->aPage['description']) : '') . '" />';
  1019. // Smart App Banner
  1020. if (getParam('smart_app_banner') && false === strpos($_SERVER['HTTP_USER_AGENT'], 'UNAMobileApp')) {
  1021. if ($sAppIdIOS = getParam('smart_app_banner_ios_app_id'))
  1022. $sRet .= '<meta name="apple-itunes-app" content="app-id=' . $sAppIdIOS . '" />';
  1023. }
  1024. // RSS
  1025. $oFunctions = BxTemplFunctions::getInstance();
  1026. $sRet .= $oFunctions->getManifests();
  1027. $sRet .= $oFunctions->getMetaIcons();
  1028. if (!empty($this->aPage['rss']) && !empty($this->aPage['rss']['url']))
  1029. $sRet .= '<link rel="alternate" type="application/rss+xml" title="' . bx_html_attribute($this->aPage['rss']['title'], BX_ESCAPE_STR_QUOTE) . '" href="' . $this->aPage['rss']['url'] . '" />';
  1030. return $sRet;
  1031. }
  1032. /**
  1033. * Get template, which was loaded earlier.
  1034. * @see method this->loadTemplates and field this->_aTemplates
  1035. *
  1036. * @param string $sName - template name.
  1037. * @return string template's content.
  1038. */
  1039. public function getTemplate($sName)
  1040. {
  1041. return $this->_aTemplates[$sName];
  1042. }
  1043. /**
  1044. * Get template functions object.
  1045. * @see BxBaseFunctions
  1046. */
  1047. public function getTemplateFunctions()
  1048. {
  1049. return $this->_oTemplateFunctions;
  1050. }
  1051. /**
  1052. * Get image MIME type.
  1053. *
  1054. * @param string $sExtension - image file's extension.
  1055. * @return string with MIME type.
  1056. */
  1057. function getImageMimeType($sExtension)
  1058. {
  1059. $sExtension = strtolower($sExtension);
  1060. $sResult = '';
  1061. switch($sExtension) {
  1062. case 'svg':
  1063. $sResult = 'svg+xml';
  1064. break;
  1065. default:
  1066. $sResult = $sExtension;
  1067. }
  1068. return 'data:image/' . $sResult;
  1069. }
  1070. /**
  1071. * Get icon template in dependence of a value, provided in $mixedId.
  1072. *
  1073. * @param mixed $mixedId numeric id from Storage, string with template's file name or string with font icon.
  1074. */
  1075. public function getIcon($mixedId, $aParams = array())
  1076. {
  1077. return $this->_getImage('icon', $mixedId, $aParams);
  1078. }
  1079. /**
  1080. * Get image template in dependence of a value, provided in $mixedId.
  1081. *
  1082. * @param mixed $mixedId numeric id from Storage, string with template's file name or string with font icon.
  1083. */
  1084. public function getImage($mixedId, $aParams = array())
  1085. {
  1086. return $this->_getImage('image', $mixedId, $aParams);
  1087. }
  1088. protected function _getImage($sType, $mixedId, $aParams = array())
  1089. {
  1090. $sUrl = "";
  1091. $aType2Method = array('image' => 'getImageUrl', 'icon' => 'getIconUrl');
  1092. //--- Check in System Storage.
  1093. if(is_numeric($mixedId) && (int)$mixedId > 0) {
  1094. $sStorage = BX_DOL_STORAGE_OBJ_IMAGES;
  1095. if(!empty($aParams['storage'])) {
  1096. $sStorage = $aParams['storage'];
  1097. unset($aParams['storage']);
  1098. }
  1099. if(($sResult = BxDolStorage::getObjectInstance($sStorage)->getFileUrlById((int)$mixedId)) !== false)
  1100. $sUrl = $sResult;
  1101. }
  1102. //--- Check in template folders.
  1103. if($sUrl == "" && is_string($mixedId) && strpos($mixedId, '.') !== false)
  1104. $sUrl = $this->{$aType2Method[$sType]}($mixedId);
  1105. if($sUrl != "")
  1106. return $this->parseImage($sUrl, array(
  1107. 'class' => isset($aParams['class']) && !empty($aParams['class']) ? $aParams['class'] : '',
  1108. 'alt' => isset($aParams['alt']) && !empty($aParams['alt']) ? $aParams['alt'] : ''
  1109. ));
  1110. //--- Use iconic font.
  1111. return $this->parseIcon($mixedId, $aParams);
  1112. }
  1113. /**
  1114. * Get full URL for the icon.
  1115. *
  1116. * @param string $sName icon's file name.
  1117. * @param string $sCheckIn where the content would be searched(base, template, both)
  1118. * @return string full URL.
  1119. */
  1120. function getIconUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1121. {
  1122. $sContent = "";
  1123. if(($sContent = $this->_getInlineData('icon', $sName, $sCheckIn)) !== false)
  1124. return $sContent;
  1125. return $this->_getAbsoluteLocation('url', $this->_sFolderIcons, $sName, $sCheckIn);
  1126. }
  1127. /**
  1128. * Get absolute Path for the icon.
  1129. *
  1130. * @param string $sName - icon's file name.
  1131. * @param string $sCheckIn where the content would be searched(base, template, both)
  1132. * @return string absolute path.
  1133. */
  1134. function getIconPath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1135. {
  1136. return $this->_getAbsoluteLocation('path', $this->_sFolderIcons, $sName, $sCheckIn);
  1137. }
  1138. /**
  1139. * Get full URL for the image.
  1140. *
  1141. * @param string $sName - images's file name.
  1142. * @param string $sCheckIn where the content would be searched(base, template, both)
  1143. * @return string full URL.
  1144. */
  1145. function getImageUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1146. {
  1147. $sContent = "";
  1148. if(($sContent = $this->_getInlineData('image', $sName, $sCheckIn)) !== false)
  1149. return $sContent;
  1150. return $this->_getAbsoluteLocation('url', $this->_sFolderImages, $sName, $sCheckIn);
  1151. }
  1152. /**
  1153. * Get absolute Path for the image.
  1154. *
  1155. * @param string $sName - image's file name.
  1156. * @param string $sCheckIn where the content would be searched(base, template, both)
  1157. * @return string absolute path.
  1158. */
  1159. function getImagePath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1160. {
  1161. return $this->_getAbsoluteLocation('path', $this->_sFolderImages, $sName, $sCheckIn);
  1162. }
  1163. /**
  1164. * Get full URL of CSS file.
  1165. *
  1166. * @param string $sName - CSS file name.
  1167. * @param string $sCheckIn where the content would be searched(base, template, both)
  1168. * @return string full URL.
  1169. */
  1170. function getCssUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1171. {
  1172. return $this->_getAbsoluteLocation('url', $this->_sFolderCss, $sName, $sCheckIn);
  1173. }
  1174. /**
  1175. * Get full Path of CSS file.
  1176. *
  1177. * @param string $sName - CSS file name.
  1178. * @param string $sCheckIn where the content would be searched(base, template, both)
  1179. * @return string full URL.
  1180. */
  1181. function getCssPath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1182. {
  1183. return $this->_getAbsoluteLocation('path', $this->_sFolderCss, $sName, $sCheckIn);
  1184. }
  1185. /**
  1186. * Get full URL of JS file.
  1187. *
  1188. * @param string $sName - JS file name.
  1189. * @return string full URL.
  1190. */
  1191. function getJsUrl($sName)
  1192. {
  1193. return $this->_getAbsoluteLocationJs('url', $sName);
  1194. }
  1195. /**
  1196. * Get full Path of JS file.
  1197. *
  1198. * @param string $sName - JS file name.
  1199. * @return string full URL.
  1200. */
  1201. function getJsPath($sName)
  1202. {
  1203. return $this->_getAbsoluteLocationJs('path', $sName);
  1204. }
  1205. /**
  1206. * Get full URL of Template (HTML) file.
  1207. *
  1208. * @param string $sName - Template file name.
  1209. * @return string full URL.
  1210. */
  1211. function getTemplateUrl($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1212. {
  1213. return $this->_getAbsoluteLocation('url', $this->_sFolderHtml, $sName, $sCheckIn);
  1214. }
  1215. /**
  1216. * Get full Path of JS file.
  1217. *
  1218. * @param string $sName - JS file name.
  1219. * @return string full URL.
  1220. */
  1221. function getTemplatePath($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1222. {
  1223. return $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn);
  1224. }
  1225. /**
  1226. * Get menu.
  1227. * @param $s menu object name
  1228. * @return html or empty string
  1229. */
  1230. function getMenu ($s)
  1231. {
  1232. $oMenu = BxDolMenu::getObjectInstance($s);
  1233. if($s == 'sys_site_submenu'){
  1234. $oPage = BxDolPage::getObjectInstanceByURI();
  1235. if ($oPage && $oPage->getSubMenu() == 'disabled'){
  1236. return;
  1237. }
  1238. }
  1239. return $oMenu ? $oMenu->getCode () : '';
  1240. }
  1241. /**
  1242. * Check whether HTML file exists or not.
  1243. *
  1244. * @param string $sName - HTML file name.
  1245. * @param string $sCheckIn where the content would be searched(base, template, both)
  1246. * @return boolean result of operation.
  1247. */
  1248. function isHtml($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1249. {
  1250. return $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn) != '';
  1251. }
  1252. /**
  1253. * Get content of HTML file.
  1254. *
  1255. * @param string $sName - HTML file name.
  1256. * @param string $sCheckIn where the content would be searched(base, template, both)
  1257. * @return string full content of the file and false on failure.
  1258. */
  1259. function getHtml($sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1260. {
  1261. $sAbsolutePath = $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn);
  1262. return !empty($sAbsolutePath) ? trim(file_get_contents($sAbsolutePath)) : false;
  1263. }
  1264. /**
  1265. * Parse HTML template. Search for the template with accordance to it's file name.
  1266. *
  1267. * @see allows to use cache.
  1268. *
  1269. * @param string $sName - HTML file name.
  1270. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1271. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(left, right) otherwise).
  1272. * @param string $sCheckIn where the content would be searched(base, template, both)
  1273. * @return string the result of operation.
  1274. */
  1275. function parseHtmlByName($sName, $aVariables, $mixedKeyWrapperHtml = null, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  1276. {
  1277. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginTemplate($sName, $sRand = time().rand());
  1278. if (($sContent = $this->getCached($sName, $aVariables, $mixedKeyWrapperHtml, $sCheckIn)) !== false) {
  1279. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endTemplate($sName, $sRand, $sContent, true);
  1280. return $sContent;
  1281. }
  1282. $sRet = '';
  1283. if (($sContent = $this->getHtml($sName, $sCheckIn)) !== false)
  1284. $sRet = $this->_parseContent($sContent, $aVariables, $mixedKeyWrapperHtml);
  1285. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endTemplate($sName, $sRand, $sRet, false);
  1286. return $sRet;
  1287. }
  1288. /**
  1289. * Parse HTML template.
  1290. *
  1291. * @see Doesn't allow to use cache.
  1292. *
  1293. * @param string $sContent - HTML file content.
  1294. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1295. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(left, right) otherwise).
  1296. * @return string the result of operation.
  1297. */
  1298. function parseHtmlByContent($sContent, $aVariables, $mixedKeyWrapperHtml = null)
  1299. {
  1300. if(empty($sContent))
  1301. return "";
  1302. return $this->_parseContent($sContent, $aVariables, $mixedKeyWrapperHtml);
  1303. }
  1304. /**
  1305. * Parse earlier loaded HTML template.
  1306. *
  1307. * @see Doesn't allow to use cache.
  1308. *
  1309. * @param string $sName - template name.
  1310. * @param array $aVariables - key/value pairs. Key should be the same as template's key, excluding prefix and postfix.
  1311. * @return string the result of operation.
  1312. * @see $this->_aTemplates
  1313. */
  1314. function parseHtmlByTemplateName($sName, $aVariables, $mixedKeyWrapperHtml = null)
  1315. {
  1316. if(!isset($this->_aTemplates[$sName]) || empty($this->_aTemplates[$sName]))
  1317. return "";
  1318. return $this->_parseContent($this->_aTemplates[$sName], $aVariables, $mixedKeyWrapperHtml);
  1319. }
  1320. /**
  1321. * Parse page HTML template. Search for the page's template with accordance to it's file name.
  1322. *
  1323. * @see allows to use cache.
  1324. *
  1325. * @param string $sName - HTML file name.
  1326. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1327. * @return string the result of operation.
  1328. */
  1329. function parsePageByName($sName, $aVariables)
  1330. {
  1331. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginPage($sName);
  1332. $sContent = $this->parseHtmlByName($sName, $aVariables, $this->_sKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BOTH);
  1333. if(empty($sContent)) {
  1334. $aType = BxDolPageQuery::getPageType($this->getPageType());
  1335. if(!empty($aType) && is_array($aType))
  1336. $sContent = $this->parseHtmlByName($aType['template'], $aVariables, $this->_sKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BOTH);
  1337. }
  1338. if(empty($sContent))
  1339. $sContent = $this->parseHtmlByName('default.html', $aVariables, $this->_sKeyWrapperHtml, BX_DOL_TEMPLATE_CHECK_IN_BOTH);
  1340. //--- Add CSS and JS at the very last ---//
  1341. if(strpos($sContent, '<bx_include_css_styles />') !== false) {
  1342. $aStyles = array(
  1343. 'display' => 'none !important'
  1344. );
  1345. if(isLogged())
  1346. $this->addCssStyle('.bx-hide-when-logged-in', $aStyles);
  1347. else
  1348. $this->addCssStyle('.bx-hide-when-logged-out', $aStyles);
  1349. $sContent = str_replace('<bx_include_css_styles />', $this->includeCssStyles(), $sContent);
  1350. }
  1351. if(strpos($sContent , '<bx_include_css_system />') !== false) {
  1352. $sContent = str_replace('<bx_include_css_system />', $this->includeFiles('css', true), $sContent);
  1353. }
  1354. if(strpos($sContent , '<bx_include_css />') !== false) {
  1355. if (!empty($this->aPage['css_name']))
  1356. $this->addCss($this->aPage['css_name']);
  1357. $sContent = str_replace('<bx_include_css />', $this->includeFiles('css'), $sContent);
  1358. }
  1359. if(strpos($sContent , '<bx_include_js_system />') !== false) {
  1360. $sContent = str_replace('<bx_include_js_system />', $this->includeFiles('js', true), $sContent);
  1361. }
  1362. if(strpos($sContent , '<bx_include_js />') !== false) {
  1363. if (!empty($this->aPage['js_name']))
  1364. $this->addJs($this->aPage['js_name']);
  1365. $sContent = str_replace('<bx_include_js />', $this->includeFiles('js') . $this->includeCssAsync(), $sContent);
  1366. }
  1367. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endPage($sContent);
  1368. return $sContent;
  1369. }
  1370. /**
  1371. * Parse system keys.
  1372. *
  1373. * @param string $sKey key
  1374. * @return string value associated with the key.
  1375. */
  1376. function parseSystemKey($sKey, $mixedKeyWrapperHtml = null, $bProcessInjection = true)
  1377. {
  1378. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  1379. $sRet = '';
  1380. switch( $sKey ) {
  1381. case 'page_charset':
  1382. $sRet = 'UTF-8';
  1383. break;
  1384. case 'page_robots':
  1385. if(!empty($this->aPage['robots']) && is_string($this->aPage['robots']))
  1386. $sRet = '<meta name="robots" content="' . bx_html_attribute($this->aPage['robots']) . '" />';
  1387. break;
  1388. case 'meta_info':
  1389. $sRet = $this->getMetaInfo();
  1390. break;
  1391. case 'page_header':
  1392. if(isset($this->aPage['header']))
  1393. $sRet = bx_process_output(strip_tags($this->aPage['header']));
  1394. break;
  1395. case 'page_header_text':
  1396. if(isset($this->aPage['header_text']))
  1397. $sRet = bx_process_output($this->aPage['header_text']);
  1398. break;
  1399. case 'page_width':
  1400. if (false === strpos($this->_oTemplateConfig->aLessConfig['bx-page-width'], 'px'))
  1401. $sRet = BX_DOL_PAGE_WIDTH;
  1402. else
  1403. $sRet = $this->_oTemplateConfig->aLessConfig['bx-page-width'];
  1404. break;
  1405. case 'system_injection_head':
  1406. $sRet = $this->_oTemplateFunctions->getInjectionHead();
  1407. break;
  1408. case 'system_injection_header':
  1409. $sRet = $this->_oTemplateFunctions->getInjectionHeader();
  1410. break;
  1411. case 'system_injection_footer':
  1412. $sRet = $this->_oTemplateFunctions->getInjectionFooter();
  1413. break;
  1414. case 'lang':
  1415. $sRet = bx_lang_name();
  1416. break;
  1417. case 'lang_direction':
  1418. $sRet = bx_lang_direction();
  1419. break;
  1420. case 'lang_country':
  1421. if (!($sRet = BxDolLanguages::getInstance()->getLangCountryCode()))
  1422. $sRet = bx_lang_name();
  1423. break;
  1424. case 'main_logo':
  1425. $sRet = BxTemplFunctions::getInstance()->getMainLogo();
  1426. break;
  1427. case 'informer':
  1428. $oInformer = BxDolInformer::getInstance($this);
  1429. $sRet = $oInformer ? $oInformer->display() : '';
  1430. break;
  1431. case 'cover':
  1432. $oCover = BxDolCover::getInstance($this);
  1433. if(!$oCover)
  1434. break;
  1435. $bCover = true;
  1436. $oPage = BxDolPage::getObjectInstanceByURI();
  1437. if($oPage !== false) {
  1438. $bCover = $oPage->isPageCover();
  1439. if($bCover && !$oCover->isCover()) {
  1440. $aCover = $oPage->getPageCoverImage();
  1441. if(!empty($aCover))
  1442. $oCover->setCoverImageUrl($aCover);
  1443. }
  1444. }
  1445. $sRet = $bCover ? $oCover->display() : $oCover->displayEmpty();
  1446. break;
  1447. case 'dol_images':
  1448. $sRet = $this->_processJsImages();
  1449. break;
  1450. case 'dol_lang':
  1451. $sRet = $this->_processJsTranslations();
  1452. break;
  1453. case 'dol_options':
  1454. $sRet = $this->_processJsOptions();
  1455. break;
  1456. case 'copyright':
  1457. $sRet = _t( '_copyright', date('Y') ) . getVersionComment();
  1458. break;
  1459. case 'copyright_attr':
  1460. $sRet = bx_html_attribute(_t('_copyright', date('Y')));
  1461. break;
  1462. case 'extra_js':
  1463. $sRet = empty($this->aPage['extra_js']) ? '' : $this->aPage['extra_js'];
  1464. break;
  1465. case 'is_profile_page':
  1466. $sRet = (defined('BX_PROFILE_PAGE')) ? 'true' : 'false';
  1467. break;
  1468. case 'system_js_requred':
  1469. $sRet = _t('_sys_javascript_requred');
  1470. break;
  1471. case 'included_css':
  1472. $sRet = json_encode($this->getIncludedUrls('css_compiled'));
  1473. break;
  1474. case 'included_js':
  1475. $sRet = json_encode($this->getIncludedUrls('js_compiled'));
  1476. break;
  1477. default:
  1478. $sRet = ($sTemplAdd = BxTemplFunctions::getInstance()->TemplPageAddComponent($sKey)) !== false ? $sTemplAdd : $aKeyWrappers['left'] . $sKey . $aKeyWrappers['right'];
  1479. }
  1480. if($bProcessInjection)
  1481. $sRet = $this->processInjection($this->getPageNameIndex(), $sKey, $sRet);
  1482. return $sRet;
  1483. }
  1484. /**
  1485. * Parse tag <A>
  1486. *
  1487. * @param string $sLink link URL
  1488. * @param string $sContent link content
  1489. * @param array $aAttrs an array of key => value pairs
  1490. */
  1491. function parseLink($sLink, $sContent, $aAttrs = array())
  1492. {
  1493. $sAttrs = '';
  1494. foreach($aAttrs as $sKey => $sValue)
  1495. $sAttrs .= ' ' . $sKey . '="' . bx_html_attribute($sValue) . '"';
  1496. return '<a href="' . $sLink . '"' . $sAttrs . '>' . $sContent . '</a>';
  1497. }
  1498. /**
  1499. * Parse tag <A> using provided HTML template
  1500. *
  1501. * @param string $sName template's file name
  1502. * @param string $sLink link URL
  1503. * @param string $sContent link content
  1504. * @param array $aAttrs an array of key => value pairs
  1505. */
  1506. function parseLinkByName($sName, $sLink, $sContent, $aAttrs = array())
  1507. {
  1508. $sAttrs = '';
  1509. foreach($aAttrs as $sKey => $sValue)
  1510. $sAttrs .= ' ' . $sKey . '="' . bx_html_attribute($sValue) . '"';
  1511. return $this->parseHtmlByName($sName, array(
  1512. 'href' => $sLink,
  1513. 'attrs' => $sAttrs,
  1514. 'content' => $sContent
  1515. ));
  1516. }
  1517. /**
  1518. * Parse tag <BUTTON>
  1519. *
  1520. * @param string $sContent link content
  1521. * @param array $aAttrs an array of key => value pairs
  1522. */
  1523. function parseButton($sContent, $aAttrs = array())
  1524. {
  1525. $sAttrs = '';
  1526. foreach($aAttrs as $sKey => $sValue)
  1527. $sAttrs .= ' ' . $sKey . '="' . bx_html_attribute($sValue) . '"';
  1528. return '<button' . $sAttrs . '>' . $sContent . '</button>';
  1529. }
  1530. /**
  1531. * Parse tag <IMG>
  1532. *
  1533. * @param string $sLink URL to image source
  1534. * @param array $aAttrs an array of key => value pairs
  1535. */
  1536. function parseImage($sLink, $aAttrs = array())
  1537. {
  1538. $sAttrs = '';
  1539. foreach($aAttrs as $sKey => $sValue)
  1540. $sAttrs .= ' ' . $sKey . '="' . bx_html_attribute($sValue) . '"';
  1541. return '<img src="' . $sLink . '"' . $sAttrs . ' />';
  1542. }
  1543. /**
  1544. * Parse font based icon in <I> tag
  1545. *
  1546. * @param string $sName font icon name
  1547. * @param array $aAttrs an array of key => value pairs
  1548. */
  1549. function parseIcon($sName, $aAttrs = array())
  1550. {
  1551. $aIcons = BxTemplFunctions::getInstance()->getIcon($sName, $aAttrs);
  1552. if ($aIcons[0] != '')
  1553. $aIcons[0] = '';
  1554. return implode($aIcons);
  1555. }
  1556. function getCacheFilePrefix($sType)
  1557. {
  1558. $sResult = '';
  1559. switch($sType) {
  1560. case 'template':
  1561. $sResult = $this->_sCacheFilePrefix;
  1562. break;
  1563. case 'less':
  1564. $sResult = $this->_sCssLessPrefix;
  1565. break;
  1566. case 'css':
  1567. $sResult = $this->_sCssCachePrefix;
  1568. break;
  1569. case 'js':
  1570. $sResult = $this->_sJsCachePrefix;
  1571. break;
  1572. }
  1573. return $sResult;
  1574. }
  1575. /**
  1576. * Get cache object for templates
  1577. * @return cache class instance
  1578. */
  1579. function getTemplatesCacheObject ()
  1580. {
  1581. $sCacheEngine = getParam('sys_template_cache_engine');
  1582. $oCacheEngine = bx_instance('BxDolCache' . $sCacheEngine);
  1583. if(!$oCacheEngine->isAvailable())
  1584. $oCacheEngine = bx_instance('BxDolCacheFileHtml');
  1585. return $oCacheEngine;
  1586. }
  1587. /**
  1588. * Get template from cache if it's enabled.
  1589. *
  1590. * @param string $sName template name
  1591. * @param string $aVariables key/value pairs. key should be the same as template's key, but without prefix and postfix.
  1592. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  1593. * @param string $sCheckIn where the content would be searched(base, template, both)
  1594. * @param boolean $bEvaluate need to evaluate the template or not.
  1595. * @return string result of operation or false on failure.
  1596. */
  1597. function getCached($sName, &$aVariables, $mixedKeyWrapperHtml = null, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH, $bEvaluate = true)
  1598. {
  1599. // initialization
  1600. if(!$this->_bCacheEnable)
  1601. return false;
  1602. if (in_array($sName, $this->_aCacheExceptions))
  1603. return false;
  1604. $sAbsolutePath = $this->_getAbsoluteLocation('path', $this->_sFolderHtml, $sName, $sCheckIn);
  1605. if(empty($sAbsolutePath))
  1606. return false;
  1607. $oCacheEngine = $this->getTemplatesCacheObject ();
  1608. $isFileBasedEngine = $bEvaluate && method_exists($oCacheEngine, 'getDataFilePath');
  1609. // try to get cached content
  1610. $sCacheVariableName = "a";
  1611. $sCacheKey = $this->_getCacheFileName('html', $sAbsolutePath) . '.php';
  1612. if ($isFileBasedEngine)
  1613. $sCacheContent = $oCacheEngine->getDataFilePath($sCacheKey);
  1614. else
  1615. $sCacheContent = $oCacheEngine->getData($sCacheKey);
  1616. // recreate cache if it is empty
  1617. if ($sCacheContent === null && ($sContent = file_get_contents($sAbsolutePath)) !== false && ($sContent = $this->_compileContent($sContent, "\$" . $sCacheVariableName, 1, $aVariables, $mixedKeyWrapperHtml)) !== false) {
  1618. if (false === $oCacheEngine->setData($sCacheKey, trim($sContent)))
  1619. return false;
  1620. if ($isFileBasedEngine)
  1621. $sCacheContent = $oCacheEngine->getDataFilePath($sCacheKey);
  1622. else
  1623. $sCacheContent = $sContent;
  1624. }
  1625. if ($sCacheContent === null)
  1626. return false;
  1627. // return simple cache content
  1628. if(!$bEvaluate)
  1629. return $sCacheContent;
  1630. // return evaluated cache content
  1631. ob_start();
  1632. $$sCacheVariableName = &$aVariables;
  1633. if ($isFileBasedEngine)
  1634. include($sCacheContent);
  1635. else
  1636. eval('?'.'>' . $sCacheContent);
  1637. $sContent = ob_get_clean();
  1638. return $sContent;
  1639. }
  1640. /**
  1641. * Add JS file(s) to global output.
  1642. *
  1643. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1644. * @param boolean $bDynamic in the dynamic mode JS file(s) are not included to global output, but are returned from the function directly.
  1645. * @return boolean/string result of operation.
  1646. */
  1647. function addJs($mixedFiles, $bDynamic = false)
  1648. {
  1649. return $this->_processFiles('js', 'add', $mixedFiles, $bDynamic);
  1650. }
  1651. /**
  1652. * get added js files
  1653. */
  1654. function getJs()
  1655. {
  1656. return $this->aPage['js_compiled'];
  1657. }
  1658. /**
  1659. * Add System JS file(s) to global output.
  1660. * System JS files are the files which are attached to all pages. They will be cached separately from the others.
  1661. *
  1662. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1663. * @param boolean $bDynamic in the dynamic mode JS file(s) are not included to global output, but are returned from the function directly.
  1664. * @return boolean/string result of operation.
  1665. */
  1666. function addJsSystem($mixedFiles)
  1667. {
  1668. return $this->_processFiles('js', 'add', $mixedFiles, false, true);
  1669. }
  1670. /**
  1671. * Delete JS file(s) from global output.
  1672. *
  1673. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1674. * @return boolean result of operation.
  1675. */
  1676. function deleteJs($mixedFiles)
  1677. {
  1678. return $this->_processFiles('js', 'delete', $mixedFiles);
  1679. }
  1680. /**
  1681. * Delete System JS file(s) from global output.
  1682. *
  1683. * @param mixed $mixedFiles string value represents a single JS file name. An array - array of JS file names.
  1684. * @return boolean result of operation.
  1685. */
  1686. function deleteJsSystem($mixedFiles)
  1687. {
  1688. return $this->_processFiles('js', 'delete', $mixedFiles, false, true);
  1689. }
  1690. /**
  1691. * Compile JS files in one file.
  1692. *
  1693. * @param string $sAbsolutePath CSS file absolute path(full URL for external CSS/JS files).
  1694. * @param array $aIncluded an array of already included JS files.
  1695. * @return string result of operation.
  1696. */
  1697. function _compileJs($sAbsolutePath, &$aIncluded)
  1698. {
  1699. if(isset($aIncluded[$sAbsolutePath]))
  1700. return '';
  1701. $bExternal = strpos($sAbsolutePath, "http://") !== false || strpos($sAbsolutePath, "https://") !== false;
  1702. if($bExternal) {
  1703. $sPath = $sAbsolutePath;
  1704. $sName = '';
  1705. $sContent = bx_file_get_contents($sAbsolutePath);
  1706. } else {
  1707. $aFileInfo = pathinfo($sAbsolutePath);
  1708. $sPath = $aFileInfo['dirname'] . DIRECTORY_SEPARATOR;
  1709. $sName = $aFileInfo['basename'];
  1710. $sContent = file_get_contents($sPath . $sName);
  1711. }
  1712. if(empty($sContent))
  1713. return '';
  1714. $sUrl = bx_ltrim_str($sPath, realpath(BX_DIRECTORY_PATH_ROOT), BX_DOL_URL_ROOT);
  1715. $sUrl = str_replace(DIRECTORY_SEPARATOR, '/', $sUrl);
  1716. $sContent = "\r\n/*--- BEGIN: " . $sUrl . $sName . "---*/\r\n" . $sContent . ";\r\n/*--- END: " . $sUrl . $sName . "---*/\r\n";
  1717. $sContent = str_replace(array("\n\r", "\r\n", "\r"), "\n", $sContent);
  1718. $aIncluded[$sAbsolutePath] = 1;
  1719. return preg_replace(
  1720. array(
  1721. "'<bx_url_root />'",
  1722. "'\r\n'"
  1723. ),
  1724. array(
  1725. BX_DOL_URL_ROOT,
  1726. "\n"
  1727. ),
  1728. $sContent
  1729. );
  1730. }
  1731. /**
  1732. * Minify JS
  1733. *
  1734. * @param string $s JS string to minify
  1735. * @return string minified JS string.
  1736. */
  1737. function _minifyJs($s)
  1738. {
  1739. // since each JS file is minified separately, it has to be in own scope
  1740. return "\n {\n" . BxDolMinify::getInstance()->minifyJs($s) . "\n }\n";
  1741. }
  1742. /**
  1743. * Wrap an URL to JS file into JS tag.
  1744. *
  1745. * @param string $sFile - URL to JS file.
  1746. * @return string the result of operation.
  1747. */
  1748. function _wrapInTagJs($sFile)
  1749. {
  1750. return "<script language=\"javascript\" src=\"" . $sFile . "\"></script>";
  1751. }
  1752. /**
  1753. * Wrap JS code into JS tag.
  1754. *
  1755. * @param string $sCode - JS code.
  1756. * @return string the result of operation.
  1757. */
  1758. function _wrapInTagJsCode($sCode)
  1759. {
  1760. return "<script language=\"javascript\">\n<!--\n" . $sCode . "\n-->\n</script>";
  1761. }
  1762. /**
  1763. * Add CSS file(s) to global output.
  1764. *
  1765. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1766. * @param boolean $bDynamic in the dynamic mode CSS file(s) are not included to global output, but are returned from the function directly.
  1767. * @return boolean/string result of operation
  1768. */
  1769. function addCss($mixedFiles, $bDynamic = false)
  1770. {
  1771. return $this->_processFiles('css', 'add', $mixedFiles, $bDynamic);
  1772. }
  1773. /**
  1774. * get added css files
  1775. */
  1776. function getCss()
  1777. {
  1778. return $this->aPage['css_compiled'];
  1779. }
  1780. /**
  1781. * Add additional heavy css file (not very necessary) to load asynchronously for desktop browsers only
  1782. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1783. */
  1784. function addCssAsync($mixedFiles)
  1785. {
  1786. if (!is_array($mixedFiles))
  1787. $mixedFiles = array($mixedFiles);
  1788. foreach ($mixedFiles as $sFile)
  1789. $this->aPage['css_async'][] = $this->_getAbsoluteLocationCss('url', $sFile);
  1790. $this->addJs('loadCSS.js');
  1791. }
  1792. /**
  1793. * Return script tag with special code to load async css.
  1794. * This tag is added after js files list
  1795. */
  1796. function includeCssAsync ()
  1797. {
  1798. if (empty($this->aPage['css_async']))
  1799. return '';
  1800. $this->aPage['css_async'] = array_unique($this->aPage['css_async']);
  1801. $sList = '';
  1802. foreach ($this->aPage['css_async'] as $sUrl)
  1803. $sList .= 'loadCSS("' . $sUrl . '", document.getElementById("bx_css_async"));';
  1804. // don't load css for mobile devices
  1805. return '
  1806. <script id="bx_css_async">
  1807. if(!/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
  1808. ' . $sList . '
  1809. }
  1810. </script>
  1811. ';
  1812. }
  1813. /**
  1814. * Add System CSS file(s) to global output.
  1815. * System CSS files are the files which are attached to all pages. They will be cached separately from the others.
  1816. *
  1817. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1818. * @return boolean/string result of operation
  1819. */
  1820. function addCssSystem($mixedFiles)
  1821. {
  1822. return $this->_processFiles('css', 'add', $mixedFiles, false, true);
  1823. }
  1824. /**
  1825. * Delete CSS file(s) from global output.
  1826. *
  1827. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1828. * @return boolean result of operation.
  1829. */
  1830. function deleteCss($mixedFiles)
  1831. {
  1832. return $this->_processFiles('css', 'delete', $mixedFiles);
  1833. }
  1834. /**
  1835. * Delete System CSS file(s) from global output.
  1836. *
  1837. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  1838. * @return boolean result of operation.
  1839. */
  1840. function deleteCssSystem($mixedFiles)
  1841. {
  1842. return $this->_processFiles('css', 'delete', $mixedFiles, false, true);
  1843. }
  1844. /**
  1845. * Compile CSS files' structure(@see \@import css_file_path) in one file.
  1846. *
  1847. * @param string $sAbsolutePath CSS file absolute path(full URL for external CSS/JS files).
  1848. * @param array $aIncluded an array of already included CSS files.
  1849. * @return string result of operation.
  1850. */
  1851. function _compileCss($sAbsolutePath, &$aIncluded)
  1852. {
  1853. if(isset($aIncluded[$sAbsolutePath]))
  1854. return '';
  1855. $bExternal = strpos($sAbsolutePath, "http://") !== false || strpos($sAbsolutePath, "https://") !== false;
  1856. if($bExternal) {
  1857. $sPath = $sAbsolutePath;
  1858. $sName = '';
  1859. $aAPUrl = parse_url($sAbsolutePath);
  1860. if(!empty($aAPUrl['path'])) {
  1861. $aAPPath = pathinfo($aAPUrl['path']);
  1862. if(!empty($aAPPath['basename'])) {
  1863. $sPath = bx_rtrim_str($sAbsolutePath, $aAPPath['basename']);
  1864. $sName = $aAPPath['basename'];
  1865. }
  1866. }
  1867. $sContent = bx_file_get_contents($sAbsolutePath);
  1868. } else {
  1869. $aFileInfo = pathinfo($sAbsolutePath);
  1870. $sPath = $aFileInfo['dirname'] . DIRECTORY_SEPARATOR;
  1871. $sName = $aFileInfo['basename'];
  1872. $sContent = file_get_contents($sPath . $sName);
  1873. }
  1874. if(empty($sContent))
  1875. return '';
  1876. $sUrl = bx_ltrim_str($sPath, realpath(BX_DIRECTORY_PATH_ROOT), BX_DOL_URL_ROOT);
  1877. $sUrl = str_replace(DIRECTORY_SEPARATOR, '/', $sUrl);
  1878. $sContent = "\r\n/*--- BEGIN: " . $sUrl . $sName . "---*/\r\n" . $sContent . "\r\n/*--- END: " . $sUrl . $sName . "---*/\r\n";
  1879. $aIncluded[$sAbsolutePath] = 1;
  1880. $sContent = str_replace(array("\n\r", "\r\n", "\r"), "\n", $sContent);
  1881. if($bExternal) {
  1882. $sContent = preg_replace(
  1883. array(
  1884. "'@import\s+url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*\)\s*;'",
  1885. "'url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/\?\#_=-]+)\s*[\'|\"]*\s*\)'"
  1886. ),
  1887. array(
  1888. "",
  1889. "url(" . $sPath . "\\1)"
  1890. ),
  1891. $sContent
  1892. );
  1893. }
  1894. else {
  1895. try {
  1896. $oTemplate = &$this;
  1897. /* Match URL based imports like the following:
  1898. * @import 'http://[domain]/modules/[vendor]/[module]/template/css/view.css';
  1899. * Is mainly needed for CSS files which are gotten from LESS compiler.
  1900. */
  1901. $sContent = preg_replace_callback(
  1902. "'@import\s+[\'|\"]*\s*" . str_replace("/", "\/", BX_DOL_URL_ROOT) . "([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*;'", function ($aMatches) use($oTemplate, $sPath, &$aIncluded) {
  1903. return $oTemplate->_compileCss(realpath(BX_DIRECTORY_PATH_ROOT . $aMatches[1]), $aIncluded);
  1904. }, $sContent);
  1905. /* Match relative path based imports like the following:
  1906. * @import url(../../../../../../base/profile/template/css/main.css);
  1907. * Is mainly needed for default CSS files.
  1908. */
  1909. $sContent = preg_replace_callback(
  1910. "'@import\s+url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/_-]+)\s*[\'|\"]*\s*\)\s*;'", function ($aMatches) use($oTemplate, $sPath, &$aIncluded) {
  1911. return $oTemplate->_compileCss(realpath($sPath . dirname($aMatches[1])) . DIRECTORY_SEPARATOR . basename($aMatches[1]), $aIncluded);
  1912. }, $sContent);
  1913. $sContent = preg_replace_callback(
  1914. "'url\s*\(\s*[\'|\"]*\s*([a-zA-Z0-9\.\/\?\#_=-]+)\s*[\'|\"]*\s*\)'", function ($aMatches) use($oTemplate, $sPath) {
  1915. $sFile = basename($aMatches[1]);
  1916. $sDirectory = dirname($aMatches[1]);
  1917. $sRootPath = realpath(BX_DIRECTORY_PATH_ROOT) . '/';
  1918. $sAbsolutePath = realpath(addslashes($sPath) . $sDirectory) . DIRECTORY_SEPARATOR . $sFile;
  1919. $sRootPath = str_replace(DIRECTORY_SEPARATOR, '/', $sRootPath);
  1920. $sAbsolutePath = str_replace(DIRECTORY_SEPARATOR, '/', $sAbsolutePath);
  1921. return 'url(' . bx_ltrim_str($sAbsolutePath, $sRootPath, BX_DOL_URL_ROOT) . ')';
  1922. }, $sContent);
  1923. }
  1924. catch(Exception $oException) {
  1925. return '';
  1926. }
  1927. }
  1928. return $sContent;
  1929. }
  1930. /**
  1931. * Less CSS
  1932. *
  1933. * @param mixed $mixed CSS string to process with Less compiler or an array with CSS file's Path and URL.
  1934. * @return mixed string or an array with CSS file's Path and URL.
  1935. */
  1936. function _lessCss($mixed)
  1937. {
  1938. if(is_array($mixed) && isset($mixed['url']) && isset($mixed['path'])) {
  1939. $sPathFile = realpath($mixed['path']);
  1940. $aInfoFile = pathinfo($sPathFile);
  1941. if (!isset($aInfoFile['extension']) || $aInfoFile['extension'] != 'less')
  1942. return $mixed;
  1943. $aFiles = array($mixed['path'] => $mixed['url']);
  1944. $aOptions = array('cache_dir' => $this->_sCachePublicFolderPath, 'prefix' => $this->_sCssLessPrefix);
  1945. $sFile = Less_Cache::Get($aFiles, $aOptions, $this->_oTemplateConfig->aLessConfig);
  1946. return array('url' => $this->_sCachePublicFolderUrl . $sFile, 'path' => $this->_sCachePublicFolderPath . $sFile);
  1947. }
  1948. $oLess = new Less_Parser();
  1949. $oLess->ModifyVars($this->_oTemplateConfig->aLessConfig);
  1950. $oLess->parse($mixed);
  1951. return $oLess->getCss();
  1952. }
  1953. /**
  1954. * Minify CSS
  1955. *
  1956. * @param string $s CSS string to minify
  1957. * @return string minified CSS string.
  1958. */
  1959. function _minifyCss($s)
  1960. {
  1961. return BxDolMinify::getInstance()->minifyCss($s);
  1962. }
  1963. /**
  1964. * Wrap an URL to CSS file into CSS tag.
  1965. *
  1966. * @param string $sFile - URL to CSS file.
  1967. * @return string the result of operation.
  1968. */
  1969. function _wrapInTagCss($sFile)
  1970. {
  1971. if (!$sFile)
  1972. return '';
  1973. return "<link href=\"" . $sFile . "\" rel=\"stylesheet\" type=\"text/css\" />";
  1974. }
  1975. /**
  1976. * Wrap CSS code into CSS tag.
  1977. *
  1978. * @param string $sCode - CSS code.
  1979. * @return string the result of operation.
  1980. */
  1981. function _wrapInTagCssCode($sCode)
  1982. {
  1983. return "<style>" . $sCode . "</style>";
  1984. }
  1985. /**
  1986. * Include CSS style(s) in the page's head section.
  1987. */
  1988. function includeCssStyles()
  1989. {
  1990. $sResult = "";
  1991. if(empty($this->aPage['css_styles']) || !is_array($this->aPage['css_styles']))
  1992. return $sResult;
  1993. foreach($this->aPage['css_styles'] as $sName => $aContent) {
  1994. $sContent = "";
  1995. if(!empty($aContent) && is_array($aContent))
  1996. foreach($aContent as $sStyleName => $sStyleValue)
  1997. $sContent .= "\t" . $sStyleName . ": " . $sStyleValue . ";\r\n";
  1998. $sResult .= $sName . " {\r\n" . $sContent . "}\r\n";
  1999. }
  2000. return !empty($sResult) ? $this->_wrapInTagCssCode($sResult) : '';
  2001. }
  2002. /**
  2003. * Include CSS/JS file(s) attached to the page in its head section.
  2004. * @see the method is system and would be called automatically.
  2005. *
  2006. * @param string $sType the type of file('js' or 'css')
  2007. * @return string the result CSS code.
  2008. */
  2009. function includeFiles($sType, $bSystem = false)
  2010. {
  2011. $sUpcaseType = ucfirst($sType);
  2012. $sArrayKey = $sType . ($bSystem ? '_system' : '_compiled');
  2013. $aFiles = isset($this->aPage[$sArrayKey]) ? $this->aPage[$sArrayKey] : array();
  2014. if(empty($aFiles) || !is_array($aFiles))
  2015. return "";
  2016. if(!$this->{'_b' . $sUpcaseType . 'Cache'})
  2017. return $this->_includeFiles($sType, $aFiles);
  2018. //--- If cache already exists, return it ---//
  2019. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  2020. $sMethodCompile = '_compile' . $sUpcaseType;
  2021. $sMethodLess = '_less' . $sUpcaseType;
  2022. $sMethodMinify = '_minify' . $sUpcaseType;
  2023. ksort($aFiles);
  2024. $sName = "";
  2025. foreach($aFiles as $aFile)
  2026. $sName .= $aFile['url'];
  2027. $sName = $this->_getCacheFileName($sType, $sName);
  2028. $sCacheAbsoluteUrl = $this->_sCachePublicFolderUrl . $sName . '.' . $sType;
  2029. $sCacheAbsolutePath = $this->_sCachePublicFolderPath . $sName . '.' . $sType;
  2030. if(file_exists($sCacheAbsolutePath)) {
  2031. if($this->{'_b' . $sUpcaseType . 'Archive'})
  2032. $sCacheAbsoluteUrl = $this->_getLoaderUrl($sType, $sName);
  2033. return $this->$sMethodWrap($sCacheAbsoluteUrl);
  2034. }
  2035. //--- Collect all attached CSS/JS in one file ---//
  2036. $sResult = "";
  2037. $aIncluded = array();
  2038. foreach($aFiles as $aFile) {
  2039. if($this->{'_b' . $sUpcaseType . 'Less'})
  2040. $aFile = $this->$sMethodLess($aFile);
  2041. if(($sContent = $this->$sMethodCompile($aFile['path'], $aIncluded)) === false)
  2042. continue;
  2043. if(!preg_match('/[\.-]min.(js|css)$/i', $aFile['path']) && $this->{'_b' . $sUpcaseType . 'Minify'}) // don't minify minified files
  2044. $sContent = $this->$sMethodMinify($sContent);
  2045. $sResult .= $sContent;
  2046. }
  2047. $mixedWriteResult = false;
  2048. if(!empty($sResult) && ($rHandler = fopen($sCacheAbsolutePath, 'w')) !== false) {
  2049. $mixedWriteResult = fwrite($rHandler, $sResult);
  2050. fclose($rHandler);
  2051. @chmod ($sCacheAbsolutePath, BX_DOL_FILE_RIGHTS);
  2052. }
  2053. if($mixedWriteResult === false)
  2054. return $this->_includeFiles($sType, $aFiles);
  2055. if($this->{'_b' . $sUpcaseType . 'Archive'})
  2056. $sCacheAbsoluteUrl = $this->_getLoaderUrl($sType, $sName);
  2057. return $this->$sMethodWrap($sCacheAbsoluteUrl);
  2058. }
  2059. /**
  2060. * Include CSS/JS files without caching.
  2061. *
  2062. * @param string $sType the file type (css or js)
  2063. * @param array $aFiles CSS/JS files to be added to the page.
  2064. * @return string result of operation.
  2065. */
  2066. function _includeFiles($sType, &$aFiles)
  2067. {
  2068. $iRev = (int)getParam('sys_revision');
  2069. $sUpcaseType = ucfirst($sType);
  2070. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  2071. $sMethodLess = '_less' . $sUpcaseType;
  2072. $sResult = "";
  2073. foreach($aFiles as $aFile) {
  2074. if($this->{'_b' . $sUpcaseType . 'Less'})
  2075. $aFile = $this->$sMethodLess($aFile);
  2076. $sFileUrl = $aFile['url'];
  2077. if(!$this->{'_b' . $sUpcaseType . 'Cache'})
  2078. $sFileUrl = bx_append_url_params($sFileUrl, array(
  2079. 'rev' => $iRev
  2080. ));
  2081. $sResult .= $this->$sMethodWrap($sFileUrl);
  2082. }
  2083. return $sResult;
  2084. }
  2085. /**
  2086. * Insert/Delete CSS file from output stack.
  2087. *
  2088. * @param string $sType the file type (css or js)
  2089. * @param string $sAction add/delete
  2090. * @param mixed $mixedFiles string value represents a single CSS file name. An array - array of CSS file names.
  2091. * @return boolean result of operation.
  2092. */
  2093. function _processFiles($sType, $sAction, $mixedFiles, $bDynamic = false, $bSystem = false)
  2094. {
  2095. if(empty($mixedFiles))
  2096. return $bDynamic ? "" : false;
  2097. if(is_string($mixedFiles))
  2098. $mixedFiles = array($mixedFiles);
  2099. $iRev = (int)getParam('sys_revision');
  2100. $sUpcaseType = ucfirst($sType);
  2101. $sMethodLocate = '_getAbsoluteLocation' . $sUpcaseType;
  2102. $sMethodWrap = '_wrapInTag' . $sUpcaseType;
  2103. $sResult = '';
  2104. foreach($mixedFiles as $sFile) {
  2105. //--- Process 3d Party CSS/JS file ---//
  2106. if(strpos($sFile, "http://") !== false || strpos($sFile, "https://") !== false) {
  2107. $sUrl = $sFile;
  2108. $sPath = $sFile;
  2109. }
  2110. //--- Process Custom CSS/JS file ---//
  2111. else if(strpos($sFile, "|") !== false) {
  2112. $sFile = implode('', explode("|", $sFile));
  2113. $sFile = bx_ltrim_str($sFile, BX_DIRECTORY_PATH_ROOT);
  2114. $sUrl = BX_DOL_URL_ROOT . $sFile;
  2115. $sPath = realpath(BX_DIRECTORY_PATH_ROOT . $sFile);
  2116. }
  2117. //--- Process Common CSS/JS file(check in default locations) ---//
  2118. else {
  2119. $sUrl = $this->$sMethodLocate('url', $sFile);
  2120. $sPath = $this->$sMethodLocate('path', $sFile);
  2121. }
  2122. if(empty($sPath) || empty($sUrl))
  2123. continue;
  2124. $sArrayKey = $sType . ($bSystem ? '_system' : '_compiled');
  2125. switch($sAction) {
  2126. case 'add':
  2127. if($bDynamic)
  2128. $sResult .= $this->$sMethodWrap(bx_append_url_params($sUrl, array(
  2129. 'rev' => $iRev
  2130. )));
  2131. else {
  2132. $bFound = false;
  2133. $aSearchIn = $bSystem ? $this->aPage[$sArrayKey] : array_merge($this->aPage[$sType . '_system'], $this->aPage[$sArrayKey]);
  2134. foreach($aSearchIn as $iKey => $aValue)
  2135. if($aValue['url'] == $sUrl && $aValue['path'] == $sPath) {
  2136. $bFound = true;
  2137. break;
  2138. }
  2139. if(!$bFound)
  2140. $this->aPage[$sArrayKey][] = array('url' => $sUrl, 'path' => $sPath);
  2141. }
  2142. break;
  2143. case 'delete':
  2144. if(!$bDynamic)
  2145. foreach($this->aPage[$sArrayKey] as $iKey => $aValue)
  2146. if($aValue['url'] == $sUrl) {
  2147. unset($this->aPage[$sArrayKey][$iKey]);
  2148. break;
  2149. }
  2150. break;
  2151. }
  2152. }
  2153. return $bDynamic ? $sResult : true;
  2154. }
  2155. /**
  2156. * Parse content.
  2157. *
  2158. * @param string $sContent - HTML file's content.
  2159. * @param array $aVariables - key/value pairs. key should be the same as template's key, but without prefix and postfix.
  2160. * @param mixed $mixedKeyWrapperHtml - key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  2161. * @return string the result of operation.
  2162. */
  2163. function _parseContent($sContent, $aVariables, $mixedKeyWrapperHtml = null)
  2164. {
  2165. $aKeysSrc = array_keys($aVariables);
  2166. $aValuesSrc = array_values($aVariables);
  2167. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  2168. $sKeyIf = 'bx_if:';
  2169. $sKeyRepeat = 'bx_repeat:';
  2170. $iCountKeys = count($aKeysSrc);
  2171. $aKeys = $aValues = array();
  2172. //--- Parse simple keys ---//
  2173. for ($i = 0; $i < $iCountKeys; $i++) {
  2174. if (strncmp($aKeysSrc[$i], $sKeyRepeat, 10) === 0 || strncmp($aKeysSrc[$i], $sKeyIf, 6) === 0)
  2175. continue;
  2176. $aKeys[] = "'" . $aKeyWrappers['left'] . $aKeysSrc[$i] . $aKeyWrappers['right'] . "'s";
  2177. $aValues[] = str_replace('$', '\\$', str_replace('\\', '\\\\', $aValuesSrc[$i]));
  2178. }
  2179. //--- Parse keys with constructions ---//
  2180. for ($i = 0; $i < $iCountKeys; $i++) {
  2181. if (strncmp($aKeysSrc[$i], $sKeyRepeat, 10) === 0) {
  2182. $sKey = "'<" . $aKeysSrc[$i] . ">(.*)<\/" . $aKeysSrc[$i] . ">'s";
  2183. $aMatches = array();
  2184. preg_match($sKey, $sContent, $aMatches);
  2185. $sValue = '';
  2186. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  2187. if(is_array($aValuesSrc[$i]))
  2188. foreach($aValuesSrc[$i] as $aValue)
  2189. $sValue .= $this->parseHtmlByContent($aMatches[1], $aValue, $mixedKeyWrapperHtml);
  2190. else if(is_string($aValuesSrc[$i]))
  2191. $sValue = $aValuesSrc[$i];
  2192. }
  2193. }
  2194. else if (strncmp($aKeysSrc[$i], $sKeyIf, 6) === 0) {
  2195. $sKey = "'<" . $aKeysSrc[$i] . ">(.*)<\/" . $aKeysSrc[$i] . ">'s";
  2196. $aMatches = array();
  2197. preg_match($sKey, $sContent, $aMatches);
  2198. $sValue = '';
  2199. if(isset($aMatches[1]) && !empty($aMatches[1]))
  2200. if(is_array($aValuesSrc[$i]) && isset($aValuesSrc[$i]['content']) && isset($aValuesSrc[$i]['condition']) && $aValuesSrc[$i]['condition'])
  2201. $sValue .= $this->parseHtmlByContent($aMatches[1], $aValuesSrc[$i]['content'], $mixedKeyWrapperHtml);
  2202. }
  2203. else
  2204. continue;
  2205. $aKeys[] = $sKey;
  2206. $aValues[] = str_replace('$', '\\$', str_replace('\\', '\\\\', $sValue));
  2207. }
  2208. try {
  2209. $oTemplate = &$this;
  2210. $aCallbackPatterns = array(
  2211. "'<bx_include_auto:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BOTH,
  2212. "'<bx_include_auto_mod_general:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_general'),
  2213. "'<bx_include_auto_mod_profile:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_profile'),
  2214. "'<bx_include_auto_mod_group:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_group'),
  2215. "'<bx_include_auto_mod_text:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_text'),
  2216. "'<bx_include_base:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BASE,
  2217. "'<bx_include_base_mod_general:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_general'),
  2218. "'<bx_include_base_mod_profile:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_profile'),
  2219. "'<bx_include_base_mod_group:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_group'),
  2220. "'<bx_include_base_mod_text:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_text'),
  2221. "'<bx_include_tmpl:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_TMPL,
  2222. "'<bx_include_tmpl_mod_general:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_general'),
  2223. "'<bx_include_tmpl_mod_profile:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_profile'),
  2224. "'<bx_include_tmpl_mod_group:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_group'),
  2225. "'<bx_include_tmpl_mod_text:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_text')
  2226. );
  2227. foreach($aCallbackPatterns as $sPattern => $sCheckIn)
  2228. $sContent = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $aVariables, $mixedKeyWrapperHtml, $sCheckIn) {
  2229. return $oTemplate->parseHtmlByName($aMatches[1], $aVariables, $mixedKeyWrapperHtml, $sCheckIn);
  2230. }, $sContent);
  2231. $sContent = $this->_parseContentKeys($sContent, array(
  2232. "'<bx_injection:([^\s]+) />'s" => 'get_injection',
  2233. "'<bx_menu:([^\s]+) \/>'s" => 'get_menu',
  2234. ));
  2235. }
  2236. catch(Exception $oException) {
  2237. return '';
  2238. }
  2239. $aKeys = array_merge($aKeys, array(
  2240. "'<bx_url_root />'",
  2241. "'<bx_url_studio />'",
  2242. ));
  2243. $aValues = array_merge($aValues, array(
  2244. BX_DOL_URL_ROOT,
  2245. BX_DOL_URL_STUDIO,
  2246. ));
  2247. //--- Parse Predefined Keys ---//
  2248. $sContent = preg_replace($aKeys, $aValues, $sContent);
  2249. //--- Parse System Keys ---//
  2250. $sContent = preg_replace_callback("'" . $aKeyWrappers['left'] . "([a-zA-Z0-9_-]+)" . $aKeyWrappers['right'] . "'", function($aMatches) use($oTemplate, $mixedKeyWrapperHtml) {
  2251. return $oTemplate->parseSystemKey($aMatches[1], $mixedKeyWrapperHtml);
  2252. }, $sContent);
  2253. return $sContent;
  2254. }
  2255. /**
  2256. * Compile content
  2257. *
  2258. * @param string $sContent template.
  2259. * @param string $aVarName variable name to be saved in the output file.
  2260. * @param integer $iVarDepth depth is used to process nesting, for example, in cycles.
  2261. * @param array $aVarValues values to be compiled in.
  2262. * @param mixed $mixedKeyWrapperHtml key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  2263. * @return string the result of operation.
  2264. */
  2265. function _compileContent($sContent, $aVarName, $iVarDepth, $aVarValues, $mixedKeyWrapperHtml = null)
  2266. {
  2267. $aKeys = array_keys($aVarValues);
  2268. $aValues = array_values($aVarValues);
  2269. $aKeyWrappers = $this->_getKeyWrappers($mixedKeyWrapperHtml);
  2270. for($i = 0; $i < count($aKeys); $i++) {
  2271. if(strpos($aKeys[$i], 'bx_repeat:') === 0) {
  2272. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  2273. $aMatches = array();
  2274. preg_match($sKey, $sContent, $aMatches);
  2275. $sValue = '';
  2276. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  2277. if(empty($aValues[$i]) || !is_array($aValues[$i]))
  2278. return false;
  2279. $sIndex = "\$" . str_repeat("i", $iVarDepth);
  2280. $sValue .= "<?php if(is_array(" . $aVarName . "['" . $aKeys[$i] . "'])) for(" . $sIndex . "=0; " . $sIndex . "<count(" . $aVarName . "['" . $aKeys[$i] . "']); " . $sIndex . "++){ ?>";
  2281. if(($sInnerValue = $this->_compileContent($aMatches[1], $aVarName . "['" . $aKeys[$i] . "'][" . $sIndex . "]", $iVarDepth + 1, current($aValues[$i]), $mixedKeyWrapperHtml)) === false)
  2282. return false;
  2283. $sValue .= $sInnerValue;
  2284. $sValue .= "<?php } else if(is_string(" . $aVarName . "['" . $aKeys[$i] . "'])) echo " . $aVarName . "['" . $aKeys[$i] . "']; ?>";
  2285. }
  2286. } else if(strpos($aKeys[$i], 'bx_if:') === 0) {
  2287. $sKey = "'<" . $aKeys[$i] . ">(.*)<\/" . $aKeys[$i] . ">'s";
  2288. $aMatches = array();
  2289. preg_match($sKey, $sContent, $aMatches);
  2290. $sValue = '';
  2291. if(isset($aMatches[1]) && !empty($aMatches[1])) {
  2292. if(!is_array($aValues[$i]) || !isset($aValues[$i]['content']) || empty($aValues[$i]['content']) || !is_array($aValues[$i]['content']))
  2293. return false;
  2294. $sValue .= "<?php if(" . $aVarName . "['" . $aKeys[$i] . "']['condition']){ ?>";
  2295. if(($sInnerValue = $this->_compileContent($aMatches[1], $aVarName . "['" . $aKeys[$i] . "']['content']", $iVarDepth, $aValues[$i]['content'], $mixedKeyWrapperHtml)) === false)
  2296. return false;
  2297. $sValue .= $sInnerValue;
  2298. $sValue .= "<?php } ?>";
  2299. }
  2300. } else {
  2301. $sKey = "'" . $aKeyWrappers['left'] . $aKeys[$i] . $aKeyWrappers['right'] . "'s";
  2302. $sValue = "<?php echo " . $aVarName . "['" . $aKeys[$i] . "'];?>";
  2303. }
  2304. $aKeys[$i] = $sKey;
  2305. $aValues[$i] = $sValue;
  2306. }
  2307. try {
  2308. $oTemplate = &$this;
  2309. $aCallbackPatterns = array(
  2310. "'<bx_include_auto:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BOTH,
  2311. "'<bx_include_auto_mod_general:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_general'),
  2312. "'<bx_include_auto_mod_profile:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_profile'),
  2313. "'<bx_include_auto_mod_group:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_group'),
  2314. "'<bx_include_auto_mod_text:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BOTH, 'sub' => 'mod_text'),
  2315. "'<bx_include_base:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_BASE,
  2316. "'<bx_include_base_mod_general:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_general'),
  2317. "'<bx_include_base_mod_profile:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_profile'),
  2318. "'<bx_include_base_mod_group:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_group'),
  2319. "'<bx_include_base_mod_text:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_BASE, 'sub' => 'mod_text'),
  2320. "'<bx_include_tmpl:([^\s]+) \/>'s" => BX_DOL_TEMPLATE_CHECK_IN_TMPL,
  2321. "'<bx_include_tmpl_mod_general:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_general'),
  2322. "'<bx_include_tmpl_mod_profile:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_profile'),
  2323. "'<bx_include_tmpl_mod_group:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_group'),
  2324. "'<bx_include_tmpl_mod_text:([^\s]+) \/>'s" => array('in' => BX_DOL_TEMPLATE_CHECK_IN_TMPL, 'sub' => 'mod_text')
  2325. );
  2326. foreach($aCallbackPatterns as $sPattern => $sCheckIn)
  2327. $sContent = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $aVarValues, $mixedKeyWrapperHtml, $sCheckIn) {
  2328. $mixedResult = $oTemplate->getCached($aMatches[1], $aVarValues, $mixedKeyWrapperHtml, $sCheckIn, false);
  2329. if($mixedResult === false)
  2330. throw new Exception('Unable to create cache file.');
  2331. return $mixedResult;
  2332. }, $sContent);
  2333. $sContent = $this->_parseContentKeys($sContent);
  2334. }
  2335. catch(Exception $oException) {
  2336. return false;
  2337. }
  2338. $aKeys = array_merge($aKeys, array(
  2339. "'<bx_injection:([^\s]+) />'s",
  2340. "'<bx_menu:([^\s]+) \/>'s",
  2341. "'<bx_url_root />'",
  2342. "'<bx_url_studio />'"
  2343. ));
  2344. $aValues = array_merge($aValues, array(
  2345. "<?php echo \$this->processInjection(\$this->aPage['name_index'], '\\1'); ?>",
  2346. "<?php echo \$this->getMenu('\\1'); ?>",
  2347. BX_DOL_URL_ROOT,
  2348. BX_DOL_URL_STUDIO
  2349. ));
  2350. //--- Parse Predefined Keys ---//
  2351. $sContent = preg_replace($aKeys, $aValues, $sContent);
  2352. //--- Parse System Keys ---//
  2353. $sContent = preg_replace( "'" . $aKeyWrappers['left'] . "([a-zA-Z0-9_-]+)" . $aKeyWrappers['right'] . "'", "<?php echo \$this->parseSystemKey('\\1', \$mixedKeyWrapperHtml);?>", $sContent);
  2354. return $sContent;
  2355. }
  2356. protected function _parseContentKeys($sContent, $aCallbackPatterns = array())
  2357. {
  2358. $oTemplate = &$this;
  2359. $aCallbackPatterns = array_merge($aCallbackPatterns, array(
  2360. "'<bx_image_url:([^\s]+) \/>'s" => "get_image_url",
  2361. "'<bx_icon_url:([^\s]+) \/>'s" => "get_icon_url",
  2362. "'<bx_text:([_\{\}\w\d\s]+[^\s]{1}) \/>'s" => "get_text",
  2363. "'<bx_text_js:([^\s]+) \/>'s" => "get_text_js",
  2364. "'<bx_text_attribute:([^\s]+) \/>'s" => "get_text_attribute",
  2365. ));
  2366. foreach($aCallbackPatterns as $sPattern => $sAction)
  2367. $sContent = preg_replace_callback($sPattern, function($aMatches) use($oTemplate, $sAction) {
  2368. $sResult = '';
  2369. switch($sAction) {
  2370. case 'get_image_url':
  2371. $sResult = $oTemplate->getImageUrl($aMatches[1]);
  2372. break;
  2373. case 'get_icon_url':
  2374. $sResult = $oTemplate->getIconUrl($aMatches[1]);
  2375. break;
  2376. case 'get_text':
  2377. $sResult = _t($aMatches[1]);
  2378. break;
  2379. case 'get_text_js':
  2380. $sResult = bx_js_string(_t($aMatches[1]));
  2381. break;
  2382. case 'get_text_attribute':
  2383. $sResult = bx_html_attribute(_t($aMatches[1]));
  2384. break;
  2385. case 'get_injection':
  2386. $sResult = $oTemplate->processInjection($oTemplate->getPageNameIndex(), $aMatches[1]);
  2387. break;
  2388. case 'get_menu':
  2389. $sResult = $oTemplate->getMenu($aMatches[1]);
  2390. break;
  2391. }
  2392. return $sResult;
  2393. }, $sContent);
  2394. return $sContent;
  2395. }
  2396. /**
  2397. * Get absolute location of some template's part.
  2398. *
  2399. * @param string $sType - result type. Available values 'url' and 'path'.
  2400. * @param string $sFolder - folders to be searched in. @see $_sFolderHtml, $_sFolderCss, $_sFolderImages and $_sFolderIcons
  2401. * @param string $sName - requested part name.
  2402. * @param string $sCheckIn where the content would be searched(base, template, both)
  2403. * @return string absolute location (path/url) of the part.
  2404. */
  2405. function _getAbsoluteLocation($sType, $sFolder, $sName, $sCheckIn = BX_DOL_TEMPLATE_CHECK_IN_BOTH)
  2406. {
  2407. $sDirectory = $this->getPath();
  2408. if($sType == 'path') {
  2409. $sDivider = DIRECTORY_SEPARATOR;
  2410. $sRoot = BX_DIRECTORY_PATH_ROOT;
  2411. } else if($sType == 'url') {
  2412. $sDivider = '/';
  2413. $sRoot = BX_DOL_URL_ROOT;
  2414. }
  2415. if(strpos($sName,'|') !== false) {
  2416. $aParts = explode('|', $sName);
  2417. $sName = $aParts[1];
  2418. if(strpos($aParts[0],'@') !== false) {
  2419. $aLocationParts = explode('@', $aParts[0]);
  2420. $sLocationKey = $this->addLocation($aLocationParts[0], BX_DIRECTORY_PATH_ROOT . $aLocationParts[1], BX_DOL_URL_ROOT . $aLocationParts[1]);
  2421. }
  2422. }
  2423. $sResult = '';
  2424. $aLocations = array_reverse($this->_aLocations, true);
  2425. //--- Check it Template.
  2426. $bInSub = false;
  2427. $aCheckIn = [BX_DOL_TEMPLATE_CHECK_IN_BOTH, BX_DOL_TEMPLATE_CHECK_IN_TMPL];
  2428. if(in_array($sCheckIn, $aCheckIn) || $bInSub = (isset($sCheckIn['in'], $sCheckIn['sub']) && in_array($sCheckIn['in'], $aCheckIn)))
  2429. foreach($aLocations as $sKey => $aLocation)
  2430. if((!$bInSub || $sCheckIn['sub'] == $sKey) && extFileExists(BX_DIRECTORY_PATH_MODULES . $this->getPath(). 'data' . DIRECTORY_SEPARATOR . BX_DOL_TEMPLATE_FOLDER_ROOT . DIRECTORY_SEPARATOR . $sKey . DIRECTORY_SEPARATOR . $sFolder . $sName)) {
  2431. $sResult = $sRoot . 'modules' . $sDivider . $sDirectory. 'data' . $sDivider . BX_DOL_TEMPLATE_FOLDER_ROOT . $sDivider . $sKey . $sDivider . $sFolder . $sName;
  2432. break;
  2433. }
  2434. //--- Check it Base.
  2435. $bInSub = false;
  2436. $aCheckIn = [BX_DOL_TEMPLATE_CHECK_IN_BOTH, BX_DOL_TEMPLATE_CHECK_IN_BASE];
  2437. if(empty($sResult) && (in_array($sCheckIn, $aCheckIn) || $bInSub = (isset($sCheckIn['in'], $sCheckIn['sub']) && in_array($sCheckIn['in'], $aCheckIn))))
  2438. foreach($aLocations as $sKey => $aLocation)
  2439. if((!$bInSub || $sCheckIn['sub'] == $sKey) && extFileExists($aLocation['path'] . BX_DOL_TEMPLATE_FOLDER_ROOT . DIRECTORY_SEPARATOR . $sFolder . $sName)) {
  2440. $sResult = $aLocation[$sType] . BX_DOL_TEMPLATE_FOLDER_ROOT . $sDivider . $sFolder . $sName;
  2441. break;
  2442. }
  2443. /**
  2444. * try to find from received path
  2445. */
  2446. if(!$sResult && @is_file(BX_DIRECTORY_PATH_ROOT . $aParts[0] . DIRECTORY_SEPARATOR . $aParts[1])) {
  2447. $sResult = $sRoot . $aParts[0] . $sDivider . $aParts[1];
  2448. }
  2449. if(isset($sLocationKey))
  2450. $this->removeLocation($sLocationKey);
  2451. return $sType == 'path' && !empty($sResult) ? realpath($sResult) : $sResult;
  2452. }
  2453. /**
  2454. * Get absolute location of some template's part.
  2455. *
  2456. * @param string $sType result type. Available values 'url' and 'path'.
  2457. * @param string $sName requested part name.
  2458. * @return string absolute location (path/url) of the part.
  2459. */
  2460. function _getAbsoluteLocationJs($sType, $sName)
  2461. {
  2462. $sResult = '';
  2463. $aLocations = array_reverse($this->_aLocationsJs, true);
  2464. foreach($aLocations as $sKey => $aLocation) {
  2465. if(extFileExists($aLocation['path'] . $sName))
  2466. $sResult = $aLocation[$sType] . $sName;
  2467. else
  2468. continue;
  2469. break;
  2470. }
  2471. return $sType == 'path' && !empty($sResult) ? realpath($sResult) : $sResult;
  2472. }
  2473. function _getAbsoluteLocationCss($sType, $sName)
  2474. {
  2475. $sNameLess = str_replace('.css', '.less', $sName);
  2476. $sResult = $this->_getAbsoluteLocation($sType, $this->_sFolderCss, $sNameLess);
  2477. if(!empty($sResult))
  2478. return $sResult;
  2479. return $this->_getAbsoluteLocation($sType, $this->_sFolderCss, $sName);
  2480. }
  2481. /**
  2482. * Get inline data for Images and Icons.
  2483. *
  2484. * @param string $sType image/icon
  2485. * @param string $sName file name
  2486. * @param string $sCheckIn where the content would be searched(base, template, both)
  2487. * @return unknown
  2488. */
  2489. function _getInlineData($sType, $sName, $sCheckIn)
  2490. {
  2491. switch($sType) {
  2492. case 'image':
  2493. $sFolder = $this->_sFolderImages;
  2494. break;
  2495. case 'icon':
  2496. $sFolder = $this->_sFolderIcons;
  2497. break;
  2498. }
  2499. $sPath = $this->_getAbsoluteLocation('path', $sFolder, $sName, $sCheckIn);
  2500. $iFileSize = 0;
  2501. if($this->_bImagesInline && ($iFileSize = filesize($sPath)) !== false && $iFileSize < $this->_iImagesMaxSize) {
  2502. $aFileInfo = pathinfo($sPath);
  2503. return $this->getImageMimeType($aFileInfo['extension']) . ";base64," . base64_encode(file_get_contents($sPath));
  2504. }
  2505. return false;
  2506. }
  2507. /**
  2508. * Get file name where the template would be cached.
  2509. *
  2510. * @param string $sAbsolutePath template's real path.
  2511. * @return string the result of operation.
  2512. */
  2513. function _getCacheFileName($sType, $sAbsolutePath)
  2514. {
  2515. $sResult = bx_site_hash($sAbsolutePath);
  2516. switch($sType) {
  2517. case 'html':
  2518. $sResult = $this->_sCacheFilePrefix . bx_lang_name() . '_' . $this->_sCode . '_' . $sResult;
  2519. break;
  2520. case 'css':
  2521. $sResult = $this->_sCssCachePrefix . (!empty($this->_iMix) ? $this->_iMix . '_' : '') . $sResult;
  2522. break;
  2523. case 'js':
  2524. $sResult = $this->_sJsCachePrefix . $sResult;
  2525. break;
  2526. }
  2527. return $sResult;
  2528. }
  2529. /**
  2530. * Get template key wrappers(left, right)
  2531. *
  2532. * @param mixed $mixedKeyWrapperHtml key wrapper(string value if left and right parts are the same, array(0 => left, 1 => right) otherwise).
  2533. * @return array result of operation.
  2534. */
  2535. function _getKeyWrappers($mixedKeyWrapperHtml)
  2536. {
  2537. $aResult = array();
  2538. if(!empty($mixedKeyWrapperHtml) && is_string($mixedKeyWrapperHtml))
  2539. $aResult = array('left' => $mixedKeyWrapperHtml, 'right' => $mixedKeyWrapperHtml);
  2540. else if(!empty($mixedKeyWrapperHtml) && is_array($mixedKeyWrapperHtml))
  2541. $aResult = array('left' => $mixedKeyWrapperHtml[0], 'right' => $mixedKeyWrapperHtml[1]);
  2542. else
  2543. $aResult = array('left' => $this->_sKeyWrapperHtml, 'right' => $this->_sKeyWrapperHtml);
  2544. return $aResult;
  2545. }
  2546. /**
  2547. * Process all added language translations and return them as a string.
  2548. *
  2549. * @return string with JS code.
  2550. */
  2551. function _processJsTranslations()
  2552. {
  2553. $sReturn = '';
  2554. if(isset($this->aPage['js_translations']) && is_array($this->aPage['js_translations'])) {
  2555. foreach($this->aPage['js_translations'] as $sKey => $sString)
  2556. $sReturn .= "'" . bx_js_string($sKey) . "': '" . bx_js_string($sString) . "',";
  2557. $sReturn = substr($sReturn, 0, -1);
  2558. }
  2559. return '<script language="javascript">var aDolLang = {' . $sReturn . '};</script>';
  2560. }
  2561. /**
  2562. * Process all added options and return them as a string.
  2563. *
  2564. * @return string with JS code.
  2565. */
  2566. function _processJsOptions()
  2567. {
  2568. $sReturn = '';
  2569. if(isset($this->aPage['js_options']) && is_array($this->aPage['js_options'])) {
  2570. foreach($this->aPage['js_options'] as $sName => $mixedValue)
  2571. $sReturn .= "'" . bx_js_string($sName) . "': '" . bx_js_string($mixedValue) . "',";
  2572. $sReturn = substr($sReturn, 0, -1);
  2573. }
  2574. return '<script language="javascript">var aDolOptions = {' . $sReturn . '};</script>';
  2575. }
  2576. /**
  2577. * Process all added images and return them as a string.
  2578. *
  2579. * @return string with JS code.
  2580. */
  2581. function _processJsImages()
  2582. {
  2583. $sReturn = '';
  2584. if(isset($this->aPage['js_images']) && is_array($this->aPage['js_images'])) {
  2585. foreach($this->aPage['js_images'] as $sKey => $sUrl)
  2586. $sReturn .= "'" . bx_js_string($sKey) . "': '" . bx_js_string($sUrl) . "',";
  2587. $sReturn = substr($sReturn, 0, -1);
  2588. }
  2589. return '<script language="javascript">var aDolImages = {' . $sReturn . '};</script>';
  2590. }
  2591. /**
  2592. * Get Gzip loader URL.
  2593. *
  2594. * @param $sType content type CSS/JS
  2595. * @param $sName file name.
  2596. * @return string with URL
  2597. */
  2598. function _getLoaderUrl($sType, $sName)
  2599. {
  2600. return BX_DOL_URL_ROOT . 'gzip_loader.php?file=' . $sName . '.' . $sType;
  2601. }
  2602. /**
  2603. *
  2604. * Functions to display pages with errors, messages and so on.
  2605. *
  2606. */
  2607. function displayAccessDenied ($sMsg = '', $iPage = BX_PAGE_DEFAULT, $iDesignBox = BX_DB_PADDING_DEF)
  2608. {
  2609. bx_import('BxDolLanguages');
  2610. header('HTTP/1.0 403 Forbidden');
  2611. header('Status: 403 Forbidden');
  2612. $this->displayMsg($sMsg ? $sMsg : _t('_Access denied'), false, $iPage, $iDesignBox);
  2613. }
  2614. function displayNoData ($sMsg = '', $iPage = BX_PAGE_DEFAULT, $iDesignBox = BX_DB_PADDING_DEF)
  2615. {
  2616. bx_import('BxDolLanguages');
  2617. header('HTTP/1.0 204 No Content');
  2618. header('Status: 204 No Content');
  2619. $this->displayMsg($sMsg ? $sMsg : _t('_Empty'), false, $iPage, $iDesignBox);
  2620. }
  2621. function displayErrorOccured ($sMsg = '', $iPage = BX_PAGE_DEFAULT, $iDesignBox = BX_DB_PADDING_DEF)
  2622. {
  2623. bx_import('BxDolLanguages');
  2624. header('HTTP/1.0 500 Internal Server Error');
  2625. header('Status: 500 Internal Server Error');
  2626. $this->displayMsg($sMsg ? $sMsg : _t('_error occured'), false, $iPage, $iDesignBox);
  2627. }
  2628. function displayPageNotFound ($sMsg = '', $iPage = BX_PAGE_DEFAULT, $iDesignBox = BX_DB_PADDING_DEF)
  2629. {
  2630. bx_import('BxDolLanguages');
  2631. header('HTTP/1.0 404 Not Found');
  2632. header('Status: 404 Not Found');
  2633. $this->displayMsg($sMsg ? $sMsg : _t('_sys_request_page_not_found_cpt'), false, $iPage, $iDesignBox);
  2634. }
  2635. function displayMsg ($s, $bTranslate = false, $iPage = BX_PAGE_DEFAULT, $iDesignBox = BX_DB_PADDING_DEF)
  2636. {
  2637. $sError = '_Error';
  2638. $bArray = is_array($s);
  2639. $sTitle = $bArray ? $s['title'] : ($bTranslate ? $sError : _t($sError));
  2640. $sContent = $bArray ? $s['content'] : $s;
  2641. if($bTranslate) {
  2642. $sTitle = _t($sTitle);
  2643. $sContent = _t($sContent);
  2644. }
  2645. $sContent = MsgBox($sContent);
  2646. if($iPage == BX_PAGE_DEFAULT)
  2647. $sContent = DesignBoxContent($sTitle, $sContent, $iDesignBox);
  2648. $oTemplate = BxDolTemplate::getInstance();
  2649. $oTemplate->setPageNameIndex ($iPage);
  2650. $oTemplate->setPageHeader ($sTitle);
  2651. $oTemplate->setPageContent ('page_main_code', $sContent);
  2652. $oTemplate->getPageCode();
  2653. exit;
  2654. }
  2655. /**
  2656. * * * * Static methods for work with template injections * * *
  2657. *
  2658. * Static method is used to add/replace the content of some key in the template.
  2659. * It's usefull when you don't want to modify existing template but need to add some data to existing template key.
  2660. *
  2661. * @param integer $iPageIndex - page index where injections would processed. Use 0 if you want it to be done on all the pages.
  2662. * @param string $sKey - template key.
  2663. * @param string $sValue - the data to be added.
  2664. * @return string the result of operation.
  2665. */
  2666. function processInjection($iPageIndex, $sKey, $sValue = "")
  2667. {
  2668. if($iPageIndex != 0 && isset($this->aPage['injections']['page_0'][$sKey]) && isset($this->aPage['injections']['page_' . $iPageIndex][$sKey]))
  2669. $aSelection = @array_merge($this->aPage['injections']['page_0'][$sKey], $this->aPage['injections']['page_' . $iPageIndex][$sKey]);
  2670. else if(isset($this->aPage['injections']['page_0'][$sKey]))
  2671. $aSelection = $this->aPage['injections']['page_0'][$sKey];
  2672. else if(isset($this->aPage['injections']['page_' . $iPageIndex][$sKey]))
  2673. $aSelection = $this->aPage['injections']['page_' . $iPageIndex][$sKey];
  2674. else
  2675. $aSelection = array();
  2676. if(is_array($aSelection))
  2677. foreach($aSelection as $aInjection) {
  2678. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginInjection($sRand = time().rand());
  2679. $sInjData = '';
  2680. switch($aInjection['type']) {
  2681. case 'text':
  2682. $sInjData = $aInjection['data'];
  2683. break;
  2684. case 'service':
  2685. if(BxDolService::isSerializedService($aInjection['data']))
  2686. $sInjData = BxDolService::callSerialized($aInjection['data']);
  2687. break;
  2688. }
  2689. if((int)$aInjection['replace'] == 1)
  2690. $sValue = $sInjData;
  2691. else
  2692. $sValue .= $sInjData;
  2693. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endInjection($sRand, $aInjection);
  2694. }
  2695. return $sValue != '__' . $sKey . '__' ? str_replace('__' . $sKey . '__', '', $sValue) : $sValue;
  2696. }
  2697. /**
  2698. * Static method to add ingection available on the current page only.
  2699. *
  2700. * @param string $sKey - template's key.
  2701. * @param string $sType - injection type(text, php).
  2702. * @param string $sData - the data to be added.
  2703. * @param integer $iReplace - replace already existed data or not.
  2704. */
  2705. function addInjection($sKey, $sType, $sData, $iReplace = 0)
  2706. {
  2707. $this->aPage['injections']['page_0'][$sKey][] = array(
  2708. 'page_index' => 0,
  2709. 'key' => $sKey,
  2710. 'type' => $sType,
  2711. 'data' => $sData,
  2712. 'replace' => $iReplace
  2713. );
  2714. }
  2715. function getPageCode($oTemplate = null)
  2716. {
  2717. if (empty($oTemplate))
  2718. $oTemplate = $this;
  2719. $oZ = new BxDolAlerts('system', 'design_before_output', 0, 0, array('page' => &$this->aPage, 'page_content' => &$this->aPageContent));
  2720. $oZ->alert();
  2721. header( 'Content-type: text/html; charset=utf-8' );
  2722. header( 'X-Frame-Options: sameorigin' );
  2723. echo $oTemplate->parsePageByName('page_' . $oTemplate->getPageNameIndex() . '.html', $oTemplate->getPageContent());
  2724. }
  2725. }
  2726. /** @} */