BxBaseConfig.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. $this->_aConfig['aLessConfig']['bx-root-class-name'] = BxDolTemplate::getInstance()->getCssClassName();
  62. }
  63. public static function getInstance()
  64. {
  65. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  66. $GLOBALS['bxDolClasses'][__CLASS__] = new BxTemplConfig();
  67. return $GLOBALS['bxDolClasses'][__CLASS__];
  68. }
  69. public function __get($sName)
  70. {
  71. if (array_key_exists($sName, $this->_aConfig))
  72. return $this->_aConfig[$sName];
  73. trigger_error('Undefined property "' . $sName . '" in ' . get_class($this), E_USER_ERROR);
  74. return null;
  75. }
  76. public function __isset($sName)
  77. {
  78. return isset($this->_aConfig[$sName]);
  79. }
  80. protected function setPageWidth($sParamName)
  81. {
  82. if(!class_exists('BxDolDb') || !BxDolDb::getInstance() || empty($sParamName))
  83. return;
  84. $mixedWidth = getParam($sParamName);
  85. if(is_numeric($mixedWidth))
  86. $mixedWidth .= 'px';
  87. $this->_aConfig['aLessConfig']['bx-page-width'] = $mixedWidth;
  88. }
  89. protected function _setValue($sKey, $sDefault = '')
  90. {
  91. $sValue = trim(getParam($sKey));
  92. if(!$this->_isModule || empty($sValue))
  93. $sValue = $sDefault;
  94. return $sValue;
  95. }
  96. protected function _setSize($sKey, $sDefault = '', $sPattern = '')
  97. {
  98. if(empty($sDefault))
  99. $sDefault = '0px';
  100. if(empty($sPattern))
  101. $sPattern = "/([0-9\.]+\s*((px)|(rem)|(vh))){1}/";
  102. $sValue = trim(getParam($sKey));
  103. if(!$this->_isModule || empty($sValue) || !preg_match($sPattern, $sValue))
  104. $sValue = $sDefault;
  105. return $sValue;
  106. }
  107. protected function _setSizePx($sKey, $sDefault = '')
  108. {
  109. return $this->_setSize($sKey, $sDefault, "/([0-9\.]+\s*(px)){1}/");
  110. }
  111. protected function _setSizeDivided($sValue)
  112. {
  113. $aEmpty = array(0, 0, 0, 0);
  114. $aValues = explode(' ', $sValue);
  115. if(empty($aValues) || !is_array($aValues))
  116. return $aEmpty;
  117. $aResult = $aEmpty;
  118. switch(count($aValues)) {
  119. case 1:
  120. $aResult = array($aValues[0], $aValues[0], $aValues[0], $aValues[0]);
  121. break;
  122. case 2:
  123. $aResult = array($aValues[0], $aValues[1], $aValues[0], $aValues[1]);
  124. break;
  125. case 3:
  126. $aResult = array($aValues[0], $aValues[1], $aValues[2], $aValues[1]);
  127. break;
  128. case 4:
  129. $aResult = array($aValues[0], $aValues[1], $aValues[2], $aValues[3]);
  130. break;
  131. }
  132. return $aResult;
  133. }
  134. protected function _setInnerSizeDivided($sValue, $aReductor = array('px' => 1, 'rem' => 0.0625))
  135. {
  136. $aEmpty = array(0, 0, 0, 0);
  137. $aValues = explode(' ', $sValue);
  138. if(empty($aValues) || !is_array($aValues))
  139. return $aEmpty;
  140. $aResult = $aEmpty;
  141. switch(count($aValues)) {
  142. case 1:
  143. $sValue0 = $this->_decreaseValue($aValues[0], $aReductor);
  144. $aResult = array($sValue0, $sValue0, $sValue0, $sValue0);
  145. break;
  146. case 2:
  147. $sValue0 = $this->_decreaseValue($aValues[0], $aReductor);
  148. $sValue1 = $this->_decreaseValue($aValues[1], $aReductor);
  149. $aResult = array($sValue0, $sValue1, $sValue0, $sValue1);
  150. break;
  151. case 3:
  152. $sValue0 = $this->_decreaseValue($aValues[0], $aReductor);
  153. $sValue1 = $this->_decreaseValue($aValues[1], $aReductor);
  154. $sValue2 = $this->_decreaseValue($aValues[2], $aReductor);
  155. $aResult = array($sValue0, $sValue1, $sValue2, $sValue1);
  156. break;
  157. case 4:
  158. $sValue0 = $this->_decreaseValue($aValues[0], $aReductor);
  159. $sValue1 = $this->_decreaseValue($aValues[1], $aReductor);
  160. $sValue2 = $this->_decreaseValue($aValues[2], $aReductor);
  161. $sValue3 = $this->_decreaseValue($aValues[3], $aReductor);
  162. $aResult = array($sValue0, $sValue1, $sValue2, $sValue3);
  163. break;
  164. }
  165. return $aResult;
  166. }
  167. protected function _decreaseValue($sValue, $aReductor = array('px' => 1, 'rem' => 0.0625))
  168. {
  169. $sPattern = "/(([0-9\.]+)\s*((px)|(rem))){1}/";
  170. $aMatches = array();
  171. if(!preg_match($sPattern, $sValue, $aMatches))
  172. return $sValue;
  173. if(!isset($aMatches[2]) || empty($aMatches[4]))
  174. return $sValue;
  175. if(!isset($aReductor[$aMatches[4]]))
  176. return $sValue;
  177. return ($aMatches[2] - $aReductor[$aMatches[4]]) . $aMatches[4];
  178. }
  179. protected function _getColorFromRgba($sKey, $sDefault = '')
  180. {
  181. if(empty($sDefault))
  182. $sDefault = '51, 51, 51';
  183. $sPattern = "/rgba?\s*\(\s*(([0-9]{1,3}\s*,?\s*){3})\s*([0-9\.]+)?\s*\)/";
  184. $aMatch = array();
  185. $sValue = trim(getParam($sKey));
  186. if(!$this->_isModule || empty($sValue) || !preg_match($sPattern, $sValue, $aMatch) || empty($aMatch[1]))
  187. $sValue = $sDefault;
  188. else
  189. $sValue = trim($aMatch[1], ', ');
  190. return $sValue;
  191. }
  192. protected function _setMargin($sKey, $sDefault = '')
  193. {
  194. if(empty($sDefault))
  195. $sDefault = '0px';
  196. $sPattern = "/([0-9\.]+\s*((px)|(rem))\s*){1,4}/";
  197. $sValue = trim(getParam($sKey));
  198. if(!$this->_isModule || empty($sValue) || ($sValue != 'inherit' && !preg_match($sPattern, $sValue)))
  199. $sValue = $sDefault;
  200. return $sValue;
  201. }
  202. protected function _setColorRgb($sKey, $sDefault = '')
  203. {
  204. if(empty($sDefault))
  205. $sDefault = 'rgb(51, 51, 51)';
  206. $sPattern = "/rgb\s*\(\s*[0-9]{1,3}\s*,\s*[0-9]{1,3}\s*,\s*[0-9]{1,3}\s*\)/";
  207. $sValue = trim(getParam($sKey));
  208. if(!$this->_isModule || empty($sValue) || !preg_match($sPattern, $sValue))
  209. $sValue = $sDefault;
  210. return $sValue;
  211. }
  212. protected function _setColorRgba($sKey, $sDefault = '')
  213. {
  214. if(empty($sDefault))
  215. $sDefault = 'rgba(51, 51, 51, 1.0)';
  216. $sPattern = "/rgba?\s*\(\s*([0-9]{1,3}\s*,?\s*){3}\s*([0-9\.]+)?\s*\)/";
  217. $sValue = trim(getParam($sKey));
  218. if(!$this->_isModule || empty($sValue) || !preg_match($sPattern, $sValue))
  219. $sValue = $sDefault;
  220. return $sValue;
  221. }
  222. protected function _setColorRgbaCustom($sKey, $sOpacity, $sDefault = '')
  223. {
  224. $sDefaultColor = $sDefault;
  225. if(strpos($sDefaultColor, 'rgba') !== false)
  226. $sDefaultColor = $this->_getColorFromRgba($sDefault);
  227. return "rgba(" . $this->_getColorFromRgba($sKey, $sDefaultColor) . ", " . $sOpacity . ")";
  228. }
  229. protected function _setGradientLeft($sKey, $sDefault = '')
  230. {
  231. $sDefaultColor = $sDefault;
  232. if(strpos($sDefaultColor, 'rgba') !== false)
  233. $sDefaultColor = $this->_getColorFromRgba($sDefault);
  234. $sValue = $this->_getColorFromRgba($sKey, $sDefaultColor);
  235. return "linear-gradient(to right, rgba(" . $sValue . ", 1) 0%, rgba(" . $sValue . ", 0) 100%)";
  236. }
  237. protected function _setGradientRight($sKey, $sDefault = '')
  238. {
  239. $sDefaultColor = $sDefault;
  240. if(strpos($sDefaultColor, 'rgba') !== false)
  241. $sDefaultColor = $this->_getColorFromRgba($sDefault);
  242. $sValue = $this->_getColorFromRgba($sKey, $sDefaultColor);
  243. return "linear-gradient(to right, rgba(" . $sValue . ", 0) 0%, rgba(" . $sValue . ", 1) 100%)";
  244. }
  245. /**
  246. * Can be removed in on of the next versions.
  247. *
  248. * @deprecated since 10.0.0-B2
  249. * @see BxBaseConfig::_setGradientLeft
  250. */
  251. protected function _setGradientMenuPageLeft($sKey, $sDefault = '')
  252. {
  253. return $this->_setGradientLeft($sKey, $sDefault);
  254. }
  255. /**
  256. * Can be removed in on of the next versions.
  257. *
  258. * @deprecated since 10.0.0-B2
  259. * @see BxBaseConfig::_setGradientRight
  260. */
  261. protected function _setGradientMenuPageRight($sKey, $sDefault = '')
  262. {
  263. return $this->_setGradientRight($sKey, $sDefault);
  264. }
  265. protected function _setBgUrl($sKey, $oStorage = null)
  266. {
  267. if(empty($oStorage))
  268. $oStorage = BxDolStorage::getObjectInstance('sys_images_custom');
  269. $iImageId = (int)getParam($sKey);
  270. if(empty($iImageId))
  271. return "none";
  272. $sImageUrl = $oStorage->getFileUrlById($iImageId);
  273. if(empty($sImageUrl))
  274. return "none";
  275. return "url('" . $sImageUrl . "')";
  276. }
  277. protected function _setBgRepeat($sKey, $sDefault = '')
  278. {
  279. if(empty($sDefault))
  280. $sDefault = 'repeat';
  281. $sValue = trim(getParam($sKey));
  282. if(!$this->_isModule || empty($sValue) || !in_array($sValue, array('no-repeat', 'repeat', 'repeat-x', 'repeat-y')))
  283. $sValue = $sDefault;
  284. return $sValue;
  285. }
  286. protected function _setBgAttachment($sKey, $sDefault = '')
  287. {
  288. if(empty($sDefault))
  289. $sDefault = 'scroll';
  290. $sValue = trim(getParam($sKey));
  291. if(!$this->_isModule || empty($sValue) || !in_array($sValue, array('fixed', 'scroll', 'local')))
  292. $sValue = $sDefault;
  293. return $sValue;
  294. }
  295. protected function _setBgSize($sKey, $sDefault = '')
  296. {
  297. if(empty($sDefault))
  298. $sDefault = 'auto';
  299. $sValue = trim(getParam($sKey));
  300. if(!$this->_isModule || empty($sValue) || !in_array($sValue, array('auto', 'cover', 'contain')))
  301. $sValue = $sDefault;
  302. return $sValue;
  303. }
  304. protected function _setShadow($sKey, $sDefault = '')
  305. {
  306. if(empty($sDefault))
  307. $sDefault = '0px 1px 3px 0px rgba(0, 0, 0, 0.25)';
  308. $sPattern = "/((-?[0-9]+\s*px\s*){4}\s*rgba\s*\(\s*([0-9]{1,3}\s*,\s*){3}\s*[0-9\.]+\s*\)\s*,?\s*)+/";
  309. $sValue = trim(getParam($sKey));
  310. if(!$this->_isModule || empty($sValue) || $sValue == 'none')
  311. $sValue = 'none';
  312. else if(!preg_match($sPattern, $sValue))
  313. $sValue = $sDefault;
  314. return $sValue;
  315. }
  316. protected function _setShadowCustom($iX, $iY, $iBlur, $iSpread, $sKeyColor, $fOpacity, $sDefault = '')
  317. {
  318. return $iX . 'px ' . $iY . 'px ' . $iBlur . 'px ' . $iSpread . 'px ' . $this->_setColorRgbaCustom($sKeyColor, 0.08, 'rgba(0, 0, 0, 0.25)');
  319. }
  320. protected function _setShadowFont($sKey, $sDefault = '')
  321. {
  322. if(empty($sDefault))
  323. $sDefault = '0px 1px 3px rgba(0, 0, 0, 0.25)';
  324. $sPattern = "/(-?[0-9]+\s*px\s*){3}\s*rgba\s*\(\s*([0-9]{1,3}\s*,\s*){3}\s*[0-9\.]+\s*\)/";
  325. $sValue = trim(getParam($sKey));
  326. if(!$this->_isModule || empty($sValue) || $sValue == 'none')
  327. $sValue = 'none';
  328. else if(!preg_match($sPattern, $sValue))
  329. $sValue = $sDefault;
  330. return $sValue;
  331. }
  332. protected function _setAlign($sKey, $sDefault = '')
  333. {
  334. if(empty($sDefault))
  335. $sDefault = 'left';
  336. $sValue = trim(getParam($sKey));
  337. if(!$this->_isModule || empty($sValue) || !in_array($sValue, array('left', 'center', 'right')))
  338. $sValue = $sDefault;
  339. return $sValue;
  340. }
  341. protected function _setAlignFlex($sKey, $sDefault = '')
  342. {
  343. $sValue = $this->_setAlign($sKey);
  344. $aToFlex = array(
  345. 'left' => 'flex-start',
  346. 'center' => 'center',
  347. 'right' => 'flex-end'
  348. );
  349. if(empty($sDefault))
  350. $sDefault = 'flex-start';
  351. return isset($aToFlex[$sValue]) ? $aToFlex[$sValue] : $sDefault;
  352. }
  353. }
  354. /** @} */