BxBaseConfig.php 14 KB

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