BxBaseConfig.php 14 KB

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