BxBaseConfig.php 12 KB

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