BxBaseConfig.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php defined('BX_DOL') or defined('BX_DOL_INSTALL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. class BxBaseConfig extends BxDol implements iBxDolSingleton
  10. {
  11. protected $_aConfig = array (
  12. 'bAllowUnicodeInPreg' => true, ///< allow unicode in regular expressions
  13. 'aLessConfig' => array (
  14. 'bx-root-class-name' => '',
  15. 'bx-page-width' => '1000px',
  16. 'bx-margin' => '16px',
  17. 'bx-margin-sec' => '8px',
  18. 'bx-margin-thd' => '4px',
  19. 'bx-padding' => '16px',
  20. 'bx-padding-sec' => '8px',
  21. 'bx-padding-thd' => '4px',
  22. 'bx-font-family' => 'Helvetica, Arial, sans-serif',
  23. 'bx-size-avatar-big' => '192px',
  24. 'bx-size-avatar' => '96px',
  25. 'bx-size-thumb' => '48px',
  26. 'bx-size-icon' => '32px',
  27. 'bx-size-gallery-img-width' => '300px',
  28. 'bx-size-gallery-img-height' => '200px',
  29. 'bx-color-page' => '#fff',
  30. 'bx-color-block' => '#fff',
  31. 'bx-color-box' => 'rgba(242, 242, 242, 1.0)',
  32. 'bx-color-box-hover' => 'rgba(242, 242, 242, 0.8)',
  33. 'bx-color-label' => '#e8e8e8',
  34. 'bx-color-label-menu' => '#999999',
  35. 'bx-color-sec' => '#f2f2f2',
  36. 'bx-color-hl' => 'rgba(196, 248, 156, 0.2)',
  37. 'bx-color-active' => 'rgba(196, 248, 156, 0.4)',
  38. 'bx-color-disabled' => 'rgba(221, 221, 221, 1.0)',
  39. 'bx-border-width' => '1px',
  40. 'bx-border-type' => 'solid',
  41. 'bx-border-color' => '#d0d0d0',
  42. 'bx-border-color-hr' => 'rgba(232, 232, 232, 0.8)',
  43. 'bx-font-size-default' => '16px',
  44. 'bx-font-size-small' => '12px',
  45. 'bx-font-size-middle' => '14px',
  46. 'bx-font-size-large' => '20px',
  47. 'bx-font-size-h1' => '32px',
  48. 'bx-font-size-h2' => '28px',
  49. 'bx-font-size-h3' => '24px',
  50. 'bx-font-color-default' => '#333',
  51. 'bx-font-color-grayed' => '#999',
  52. 'bx-font-color-contrasted' => '#fff',
  53. 'bx-round-corners-radius' => '3px',
  54. 'bx-round-corners-radius-label' => '4px',
  55. 'bx-round-corners-radius-label-menu' => '12px',
  56. ),
  57. );
  58. function __construct()
  59. {
  60. parent::__construct();
  61. if (class_exists('BxDolTemplate'))
  62. $this->_aConfig['aLessConfig']['bx-root-class-name'] = BxDolTemplate::getInstance()->getCssClassName();
  63. }
  64. public static function getInstance()
  65. {
  66. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  67. $GLOBALS['bxDolClasses'][__CLASS__] = new BxTemplConfig();
  68. return $GLOBALS['bxDolClasses'][__CLASS__];
  69. }
  70. public function __get($sName)
  71. {
  72. if (array_key_exists($sName, $this->_aConfig))
  73. return $this->_aConfig[$sName];
  74. trigger_error('Undefined property "' . $sName . '" in ' . get_class($this), E_USER_ERROR);
  75. return null;
  76. }
  77. public function __isset($sName)
  78. {
  79. return isset($this->_aConfig[$sName]);
  80. }
  81. protected function setPageWidth($sParamName)
  82. {
  83. if(!class_exists('BxDolDb') || !BxDolDb::getInstance() || empty($sParamName))
  84. return;
  85. $mixedWidth = getParam($sParamName);
  86. if(is_numeric($mixedWidth))
  87. $mixedWidth .= 'px';
  88. $this->_aConfig['aLessConfig']['bx-page-width'] = $mixedWidth;
  89. }
  90. protected function _setValue($sKey, $sDefault = '')
  91. {
  92. $sValue = trim(getParam($sKey));
  93. if(!$this->_isModule || empty($sValue))
  94. $sValue = $sDefault;
  95. return $sValue;
  96. }
  97. protected function _setSize($sKey, $sDefault = '', $sPattern = '')
  98. {
  99. if(empty($sDefault))
  100. $sDefault = '0px';
  101. if(empty($sPattern))
  102. $sPattern = "/([0-9\.]+\s*((px)|(rem)|(vh))){1}/";
  103. $sValue = trim(getParam($sKey));
  104. if(!$this->_isModule || empty($sValue) || !preg_match($sPattern, $sValue))
  105. $sValue = $sDefault;
  106. return $sValue;
  107. }
  108. protected function _setSizePx($sKey, $sDefault = '')
  109. {
  110. return $this->_setSize($sKey, $sDefault, "/([0-9\.]+\s*(px)){1}/");
  111. }
  112. protected function _setSizeDivided($sValue)
  113. {
  114. $aEmpty = array(0, 0, 0, 0);
  115. $aValues = explode(' ', $sValue);
  116. if(empty($aValues) || !is_array($aValues))
  117. return $aEmpty;
  118. $aResult = $aEmpty;
  119. switch(count($aValues)) {
  120. case 1:
  121. $aResult = array($aValues[0], $aValues[0], $aValues[0], $aValues[0]);
  122. break;
  123. case 2:
  124. $aResult = array($aValues[0], $aValues[1], $aValues[0], $aValues[1]);
  125. break;
  126. case 3:
  127. $aResult = array($aValues[0], $aValues[1], $aValues[2], $aValues[1]);
  128. break;
  129. case 4:
  130. $aResult = array($aValues[0], $aValues[1], $aValues[2], $aValues[3]);
  131. break;
  132. }
  133. return $aResult;
  134. }
  135. protected function _setInnerSizeDivided($sValue, $aReductor = array('px' => 1, 'rem' => 0.0625))
  136. {
  137. $aEmpty = array(0, 0, 0, 0);
  138. $aValues = explode(' ', $sValue);
  139. if(empty($aValues) || !is_array($aValues))
  140. return $aEmpty;
  141. $aResult = $aEmpty;
  142. switch(count($aValues)) {
  143. case 1:
  144. $sValue0 = $this->_decreaseValue($aValues[0], $aReductor);
  145. $aResult = array($sValue0, $sValue0, $sValue0, $sValue0);
  146. break;
  147. case 2:
  148. $sValue0 = $this->_decreaseValue($aValues[0], $aReductor);
  149. $sValue1 = $this->_decreaseValue($aValues[1], $aReductor);
  150. $aResult = array($sValue0, $sValue1, $sValue0, $sValue1);
  151. break;
  152. case 3:
  153. $sValue0 = $this->_decreaseValue($aValues[0], $aReductor);
  154. $sValue1 = $this->_decreaseValue($aValues[1], $aReductor);
  155. $sValue2 = $this->_decreaseValue($aValues[2], $aReductor);
  156. $aResult = array($sValue0, $sValue1, $sValue2, $sValue1);
  157. break;
  158. case 4:
  159. $sValue0 = $this->_decreaseValue($aValues[0], $aReductor);
  160. $sValue1 = $this->_decreaseValue($aValues[1], $aReductor);
  161. $sValue2 = $this->_decreaseValue($aValues[2], $aReductor);
  162. $sValue3 = $this->_decreaseValue($aValues[3], $aReductor);
  163. $aResult = array($sValue0, $sValue1, $sValue2, $sValue3);
  164. break;
  165. }
  166. return $aResult;
  167. }
  168. protected function _decreaseValue($sValue, $aReductor = array('px' => 1, 'rem' => 0.0625))
  169. {
  170. $sPattern = "/(([0-9\.]+)\s*((px)|(rem))){1}/";
  171. $aMatches = array();
  172. if(!preg_match($sPattern, $sValue, $aMatches))
  173. return $sValue;
  174. if(!isset($aMatches[2]) || empty($aMatches[4]))
  175. return $sValue;
  176. if(!isset($aReductor[$aMatches[4]]))
  177. return $sValue;
  178. return ($aMatches[2] - $aReductor[$aMatches[4]]) . $aMatches[4];
  179. }
  180. protected function _getColorFromRgba($sKey, $sDefault = '')
  181. {
  182. if(empty($sDefault))
  183. $sDefault = '51, 51, 51';
  184. $sPattern = "/rgba?\s*\(\s*(([0-9]{1,3}\s*,?\s*){3})\s*([0-9\.]+)?\s*\)/";
  185. $aMatch = array();
  186. $sValue = trim(getParam($sKey));
  187. if(!$this->_isModule || empty($sValue) || !preg_match($sPattern, $sValue, $aMatch) || empty($aMatch[1]))
  188. $sValue = $sDefault;
  189. else
  190. $sValue = trim($aMatch[1], ', ');
  191. return $sValue;
  192. }
  193. protected function _setMargin($sKey, $sDefault = '')
  194. {
  195. if(empty($sDefault))
  196. $sDefault = '0px';
  197. $sPattern = "/([0-9\.]+\s*((px)|(rem))\s*){1,4}/";
  198. $sValue = trim(getParam($sKey));
  199. if(!$this->_isModule || empty($sValue) || ($sValue != 'inherit' && !preg_match($sPattern, $sValue)))
  200. $sValue = $sDefault;
  201. return $sValue;
  202. }
  203. protected function _setColorRgb($sKey, $sDefault = '')
  204. {
  205. if(empty($sDefault))
  206. $sDefault = 'rgb(51, 51, 51)';
  207. $sPattern = "/rgb\s*\(\s*[0-9]{1,3}\s*,\s*[0-9]{1,3}\s*,\s*[0-9]{1,3}\s*\)/";
  208. $sValue = trim(getParam($sKey));
  209. if(!$this->_isModule || empty($sValue) || !preg_match($sPattern, $sValue))
  210. $sValue = $sDefault;
  211. return $sValue;
  212. }
  213. protected function _setColorRgba($sKey, $sDefault = '')
  214. {
  215. if(empty($sDefault))
  216. $sDefault = 'rgba(51, 51, 51, 1.0)';
  217. $sPattern = "/rgba?\s*\(\s*([0-9]{1,3}\s*,?\s*){3}\s*([0-9\.]+)?\s*\)/";
  218. $sValue = trim(getParam($sKey));
  219. if(!$this->_isModule || empty($sValue) || !preg_match($sPattern, $sValue))
  220. $sValue = $sDefault;
  221. return $sValue;
  222. }
  223. protected function _setColorRgbaCustom($sKey, $sOpacity, $sDefault = '')
  224. {
  225. $sDefaultColor = $sDefault;
  226. if(strpos($sDefaultColor, 'rgba') !== false)
  227. $sDefaultColor = $this->_getColorFromRgba($sDefault);
  228. return "rgba(" . $this->_getColorFromRgba($sKey, $sDefaultColor) . ", " . $sOpacity . ")";
  229. }
  230. protected function _setGradientLeft($sKey, $sDefault = '')
  231. {
  232. $sDefaultColor = $sDefault;
  233. if(strpos($sDefaultColor, 'rgba') !== false)
  234. $sDefaultColor = $this->_getColorFromRgba($sDefault);
  235. $sValue = $this->_getColorFromRgba($sKey, $sDefaultColor);
  236. return "linear-gradient(to right, rgba(" . $sValue . ", 1) 0%, rgba(" . $sValue . ", 0) 100%)";
  237. }
  238. protected function _setGradientRight($sKey, $sDefault = '')
  239. {
  240. $sDefaultColor = $sDefault;
  241. if(strpos($sDefaultColor, 'rgba') !== false)
  242. $sDefaultColor = $this->_getColorFromRgba($sDefault);
  243. $sValue = $this->_getColorFromRgba($sKey, $sDefaultColor);
  244. return "linear-gradient(to right, rgba(" . $sValue . ", 0) 0%, rgba(" . $sValue . ", 1) 100%)";
  245. }
  246. /**
  247. * Can be removed in on of the next versions.
  248. *
  249. * @deprecated since 10.0.0-B2
  250. * @see BxBaseConfig::_setGradientLeft
  251. */
  252. protected function _setGradientMenuPageLeft($sKey, $sDefault = '')
  253. {
  254. return $this->_setGradientLeft($sKey, $sDefault);
  255. }
  256. /**
  257. * Can be removed in on of the next versions.
  258. *
  259. * @deprecated since 10.0.0-B2
  260. * @see BxBaseConfig::_setGradientRight
  261. */
  262. protected function _setGradientMenuPageRight($sKey, $sDefault = '')
  263. {
  264. return $this->_setGradientRight($sKey, $sDefault);
  265. }
  266. protected function _setBgUrl($sKey, $oStorage = null)
  267. {
  268. if(empty($oStorage))
  269. $oStorage = BxDolStorage::getObjectInstance('sys_images_custom');
  270. $iImageId = (int)getParam($sKey);
  271. if(empty($iImageId))
  272. return "none";
  273. $sImageUrl = $oStorage->getFileUrlById($iImageId);
  274. if(empty($sImageUrl))
  275. return "none";
  276. return "url('" . $sImageUrl . "')";
  277. }
  278. protected function _setBgRepeat($sKey, $sDefault = '')
  279. {
  280. if(empty($sDefault))
  281. $sDefault = 'repeat';
  282. $sValue = trim(getParam($sKey));
  283. if(!$this->_isModule || empty($sValue) || !in_array($sValue, array('no-repeat', 'repeat', 'repeat-x', 'repeat-y')))
  284. $sValue = $sDefault;
  285. return $sValue;
  286. }
  287. protected function _setBgAttachment($sKey, $sDefault = '')
  288. {
  289. if(empty($sDefault))
  290. $sDefault = 'scroll';
  291. $sValue = trim(getParam($sKey));
  292. if(!$this->_isModule || empty($sValue) || !in_array($sValue, array('fixed', 'scroll', 'local')))
  293. $sValue = $sDefault;
  294. return $sValue;
  295. }
  296. protected function _setBgSize($sKey, $sDefault = '')
  297. {
  298. if(empty($sDefault))
  299. $sDefault = 'auto';
  300. $sValue = trim(getParam($sKey));
  301. if(!$this->_isModule || empty($sValue) || !in_array($sValue, array('auto', 'cover', 'contain')))
  302. $sValue = $sDefault;
  303. return $sValue;
  304. }
  305. protected function _setShadow($sKey, $sDefault = '')
  306. {
  307. if(empty($sDefault))
  308. $sDefault = '0px 1px 3px 0px rgba(0, 0, 0, 0.25)';
  309. $sPattern = "/((-?[0-9]+\s*px\s*){4}\s*rgba\s*\(\s*([0-9]{1,3}\s*,\s*){3}\s*[0-9\.]+\s*\)\s*,?\s*)+/";
  310. $sValue = trim(getParam($sKey));
  311. if(!$this->_isModule || empty($sValue) || $sValue == 'none')
  312. $sValue = 'none';
  313. else if(!preg_match($sPattern, $sValue))
  314. $sValue = $sDefault;
  315. return $sValue;
  316. }
  317. protected function _setShadowCustom($iX, $iY, $iBlur, $iSpread, $sKeyColor, $fOpacity, $sDefault = '')
  318. {
  319. return $iX . 'px ' . $iY . 'px ' . $iBlur . 'px ' . $iSpread . 'px ' . $this->_setColorRgbaCustom($sKeyColor, 0.08, 'rgba(0, 0, 0, 0.25)');
  320. }
  321. protected function _setShadowFont($sKey, $sDefault = '')
  322. {
  323. if(empty($sDefault))
  324. $sDefault = '0px 1px 3px rgba(0, 0, 0, 0.25)';
  325. $sPattern = "/(-?[0-9]+\s*px\s*){3}\s*rgba\s*\(\s*([0-9]{1,3}\s*,\s*){3}\s*[0-9\.]+\s*\)/";
  326. $sValue = trim(getParam($sKey));
  327. if(!$this->_isModule || empty($sValue) || $sValue == 'none')
  328. $sValue = 'none';
  329. else if(!preg_match($sPattern, $sValue))
  330. $sValue = $sDefault;
  331. return $sValue;
  332. }
  333. protected function _setAlign($sKey, $sDefault = '')
  334. {
  335. if(empty($sDefault))
  336. $sDefault = 'left';
  337. $sValue = trim(getParam($sKey));
  338. if(!$this->_isModule || empty($sValue) || !in_array($sValue, array('left', 'center', 'right')))
  339. $sValue = $sDefault;
  340. return $sValue;
  341. }
  342. protected function _setAlignFlex($sKey, $sDefault = '')
  343. {
  344. $sValue = $this->_setAlign($sKey);
  345. $aToFlex = array(
  346. 'left' => 'flex-start',
  347. 'center' => 'center',
  348. 'right' => 'flex-end'
  349. );
  350. if(empty($sDefault))
  351. $sDefault = 'flex-start';
  352. return isset($aToFlex[$sValue]) ? $aToFlex[$sValue] : $sDefault;
  353. }
  354. }
  355. /** @} */