BxDolContentFilter.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolContentFilter extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected $_oDb;
  12. protected $_sDataList;
  13. protected $_iDefaultValue;
  14. protected $_iViewerId;
  15. protected function __construct()
  16. {
  17. parent::__construct();
  18. $this->_oDb = BxDolDb::getInstance();
  19. $this->_sDataList = 'sys_content_filter';
  20. $this->_iDefaultValue = 1; //--- Means G (ID = 1) - content available to everybody
  21. $this->_iViewerId = bx_get_logged_profile_id();
  22. }
  23. public function __clone()
  24. {
  25. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  26. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  27. }
  28. public static function getInstance()
  29. {
  30. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  31. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolContentFilter();
  32. return $GLOBALS['bxDolClasses'][__CLASS__];
  33. }
  34. public function isEnabled()
  35. {
  36. return getParam('sys_cf_enable') == 'on';
  37. }
  38. public function isEnabledForComments()
  39. {
  40. return getParam('sys_cf_enable_comments') == 'on';
  41. }
  42. public function getProhibited()
  43. {
  44. $sValues = getParam('sys_cf_prohibited');
  45. if(!$sValues)
  46. return [];
  47. $aValues = explode(',', $sValues);
  48. if(!$aValues)
  49. return [];
  50. return $aValues;
  51. }
  52. public function getDefaultValue()
  53. {
  54. return $this->_iDefaultValue;
  55. }
  56. public function getInput($aInput, $iProfileId = 0)
  57. {
  58. return $this->_getInput('content', $aInput, $iProfileId);
  59. }
  60. public function getInputForComments($aInput, $iProfileId = 0)
  61. {
  62. return $this->_getInput('comments', $aInput, $iProfileId);
  63. }
  64. protected function _getInput($sType, $aInput, $iProfileId = 0)
  65. {
  66. if(!$this->{'isEnabled' . ($sType == 'comments' ? 'ForComments' : '')}())
  67. return array_merge($aInput, [
  68. 'type' => 'hidden',
  69. 'value' => 1
  70. ]);
  71. if(!$aInput['values'])
  72. $aInput['values'] = BxDolFormQuery::getDataItems($this->_sDataList);
  73. if(!$iProfileId)
  74. $iProfileId = bx_get_logged_profile_id();
  75. $aProfileInfo = BxDolProfileQuery::getInstance()->getInfoById($iProfileId);
  76. if(empty($aProfileInfo) || !isset($aProfileInfo['cfu_items']))
  77. return $aInput;
  78. $aCfuValues = [];
  79. foreach($aInput['values'] as $iValue => $sTitle)
  80. if((1 << ($iValue - 1)) & (int)$aProfileInfo['cfu_items'])
  81. $aCfuValues[$iValue] = $sTitle;
  82. $aInput['values'] = $aCfuValues;
  83. return $aInput;
  84. }
  85. public function isAllowed($iValue, $iViewerId = 0)
  86. {
  87. if(!$this->isAllowedBySetting($iValue))
  88. return false;
  89. if(!$this->isAllowedByViewer($iValue, $iViewerId))
  90. return false;
  91. return true;
  92. }
  93. public function isAllowedBySetting($iValue)
  94. {
  95. $aValues = $this->getProhibited();
  96. if(!$aValues)
  97. return true;
  98. return !in_array($iValue, $aValues);
  99. }
  100. public function isAllowedByViewer($iValue, $iViewerId = 0)
  101. {
  102. if(!$iViewerId)
  103. $iViewerId = $this->_iViewerId;
  104. $aViewerInfo = BxDolProfileQuery::getInstance()->getInfoById($iViewerId);
  105. if(!$aViewerInfo)
  106. return false;
  107. return (1 << ($iValue - 1)) & $aViewerInfo['cfw_value'];
  108. }
  109. public function getSQLParts($sContentTable, $sContentField, $iViewerId = 0)
  110. {
  111. $sResult = '';
  112. $aSQLParts = $this->getSettingSQLParts($sContentTable, $sContentField);
  113. if(!empty($aSQLParts['where']))
  114. $sResult .= $aSQLParts['where'];
  115. $aSQLParts = $this->getViewerSQLParts($sContentTable, $sContentField, $iViewerId);
  116. if(!empty($aSQLParts['where']))
  117. $sResult .= $aSQLParts['where'];
  118. return $sResult;
  119. }
  120. public function getSettingSQLParts($sContentTable, $sContentField)
  121. {
  122. $aValues = $this->getProhibited();
  123. if(!$aValues)
  124. return [];
  125. return [
  126. 'where' => " AND `" . $sContentTable . "`.`" . $sContentField . "` NOT IN (" . $this->_oDb->implode_escape($aValues) . ")"
  127. ];
  128. }
  129. public function getViewerSQLParts($sContentTable, $sContentField, $iViewerId = 0)
  130. {
  131. if(!$iViewerId)
  132. $iViewerId = $this->_iViewerId;
  133. $aViewerInfo = BxDolProfileQuery::getInstance()->getInfoById($iViewerId);
  134. if(empty($aViewerInfo) || !is_array($aViewerInfo))
  135. return [];
  136. return [
  137. 'where' => " AND 1 << (`" . $sContentTable . "`.`" . $sContentField . "` - 1) & " . $aViewerInfo['cfw_value']
  138. ];
  139. }
  140. public function getConditions($sContentTable, $sContentField, $iViewerId = 0)
  141. {
  142. $aResult = [];
  143. $aConditions = $this->getSettingConditions($sContentTable, $sContentField);
  144. if(!empty($aConditions) && is_array($aConditions))
  145. $aResult['cf_setting'] = $aConditions;
  146. $aConditions = $this->getViewerConditions($sContentTable, $sContentField, $iViewerId);
  147. if(!empty($aConditions) && is_array($aConditions))
  148. $aResult['cf_viewer'] = $aConditions;
  149. return $aResult;
  150. }
  151. public function getSettingConditions($sContentTable, $sContentField)
  152. {
  153. $aValues = $this->getProhibited();
  154. if(!$aValues)
  155. return [];
  156. return [
  157. 'value' => $aValues,
  158. 'field' => $sContentField,
  159. 'operator' => 'not in',
  160. 'table' => $sContentTable
  161. ];
  162. }
  163. public function getViewerConditions($sContentTable, $sContentField, $iViewerId = 0)
  164. {
  165. if(!$iViewerId)
  166. $iViewerId = $this->_iViewerId;
  167. $aViewerInfo = BxDolProfileQuery::getInstance()->getInfoById($iViewerId);
  168. if(empty($aViewerInfo) || !is_array($aViewerInfo))
  169. return;
  170. return [
  171. 'value' => $aViewerInfo['cfw_value'],
  172. 'field' => $sContentField,
  173. 'operator' => 'in_set',
  174. 'table' => $sContentTable
  175. ];
  176. }
  177. public function updateValuesByProfile($aProfile)
  178. {
  179. if(!is_array($aProfile))
  180. $aProfile = BxDolProfileQuery::getInstance()->getProfiles(['type' => 'id', 'id' => (int)$aProfile]);
  181. if(!BxDolRequest::serviceExists($aProfile['type'], 'get_info'))
  182. return;
  183. $aFilters = BxDolFormQuery::getDataItems($this->_sDataList, true, BX_DATA_VALUES_ALL);
  184. $aProfileInfo = bx_srv($aProfile['type'], 'get_info', [$aProfile['content_id'], false]);
  185. $iCfwValue = (int)$aProfile['cfw_value'];
  186. $bUpdateCfwValue = false;
  187. $iCfwItems = $iCfuItems = 0;
  188. $bUpdateCfwItems = $bUpdateCfuItems = false;
  189. foreach($aFilters as $aFilter) {
  190. $iFilter = (int)$aFilter['Value'];
  191. if(!empty($aFilter['Data'])) {
  192. $aData = unserialize($aFilter['Data']);
  193. if(empty($aData) || !is_array($aData))
  194. continue;
  195. }
  196. else
  197. $aData = [
  198. 'is_allowed_watch' => ['module' => 'system', 'method' => 'is_allowed_cfilter', 'params' => ['watch'], 'class' => 'BaseServiceProfiles'],
  199. 'is_allowed_use' => ['module' => 'system', 'method' => 'is_allowed_cfilter', 'params' => ['use'], 'class' => 'BaseServiceProfiles'],
  200. ];
  201. if(!empty($aData['is_allowed_watch'])) {
  202. $aData['is_allowed_watch']['params'] = array_merge($aData['is_allowed_watch']['params'], [$iFilter, $aProfileInfo]);
  203. $iWatch = call_user_func_array('bx_srv', $aData['is_allowed_watch']);
  204. if($iWatch === false)
  205. continue;
  206. if($iWatch == 0) {
  207. $iCfwValue &= ~ (1 << ($iFilter - 1));
  208. $bUpdateCfwValue = true;
  209. }
  210. $iCfwItems |= $iWatch;
  211. $bUpdateCfwItems = true;
  212. }
  213. if(!empty($aData['is_allowed_use']) && (int)$aProfile['cfu_locked'] == 0) {
  214. $aData['is_allowed_use']['params'] = array_merge($aData['is_allowed_use']['params'], [$aFilter['Value'], $aProfileInfo]);
  215. $iUse = call_user_func_array('bx_srv', $aData['is_allowed_use']);
  216. if($iUse === false)
  217. continue;
  218. $iCfuItems |= $iUse;
  219. $bUpdateCfuItems = true;
  220. }
  221. }
  222. $oProfileQuery = BxDolProfileQuery::getInstance();
  223. if($bUpdateCfwValue)
  224. $oProfileQuery->changeCfwValue($aProfile['id'], $iCfwValue);
  225. if($bUpdateCfwItems)
  226. $oProfileQuery->changeCfwItems($aProfile['id'], $iCfwItems);
  227. if($bUpdateCfuItems)
  228. $oProfileQuery->changeCfuItems($aProfile['id'], $iCfuItems);
  229. }
  230. }
  231. /** @} */