Răsfoiți Sursa

Ticket #1430 - Mature-audience content filter (PG, PG13, R)

AntonLV 4 ani în urmă
părinte
comite
266d68a0f3

+ 176 - 0
inc/classes/BxDolContentFilter.php

@@ -0,0 +1,176 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaCore UNA Core
+ * @{
+ */
+
+class BxDolContentFilter extends BxDolFactory implements iBxDolSingleton
+{
+    protected $_oDb;
+
+    protected $_iViewerId;
+
+    protected function __construct()
+    {
+        parent::__construct();
+
+        $this->_oDb = BxDolDb::getInstance();
+
+        $this->_iViewerId = bx_get_logged_profile_id();
+    }
+
+    public function __clone()
+    {
+        if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
+            trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
+    }
+
+    public static function getInstance()
+    {
+        if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
+            $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolContentFilter();
+
+        return $GLOBALS['bxDolClasses'][__CLASS__];
+    }
+
+    public function isEnabled()
+    {
+        return getParam('sys_cf_enable') == 'on';
+    }
+    
+    public function getProhibited()
+    {
+        $sValues = getParam('sys_cf_prohibited');
+        if(!$sValues)
+            return [];
+
+        $aValues = explode(',', $sValues);
+        if(!$aValues)
+            return [];
+
+        return $aValues;
+    }
+
+    public function isAllowed($iValue, $iViewerId = 0)
+    {
+        if(!$this->isAllowedBySetting($iValue))
+            return false;
+
+        if(!$this->isAllowedByViewer($iValue, $iViewerId))
+            return false;
+
+        return true;
+    }
+
+    public function isAllowedBySetting($iValue)
+    {
+        $aValues = $this->getProhibited();
+        if(!$aValues)
+            return true;
+
+        return !in_array($iValue, $aValues);
+    }
+
+    public function isAllowedByViewer($iValue, $iViewerId = 0)
+    {
+        if(!$iViewerId)
+            $iViewerId = $this->_iViewerId;
+
+        $aViewerInfo = BxDolProfileQuery::getInstance()->getInfoById($iViewerId);
+        if(!$aViewerInfo)
+            return false;
+
+        return (1 << ($iValue - 1)) & $aViewerInfo['cfw_value'];
+    }
+
+    public function getSQLParts($sContentTable, $sContentField, $iViewerId = 0)
+    {
+        $sResult = '';
+
+        $aSQLParts = $this->getSettingSQLParts($sContentTable, $sContentField);
+        if(!empty($aSQLParts['where']))
+            $sResult .= $aSQLParts['where'];
+
+        $aSQLParts = $this->getViewerSQLParts($sContentTable, $sContentField, $iViewerId);
+        if(!empty($aSQLParts['where']))
+            $sResult .= $aSQLParts['where'];
+
+        return $sResult;
+    }
+
+    public function getSettingSQLParts($sContentTable, $sContentField)
+    {
+        $aValues = $this->getProhibited();
+        if(!$aValues)
+            return [];
+
+        return [
+            'where' => " AND `" . $sContentTable . "`.`" . $sContentField . "` NOT IN (" . $this->_oDb->implode_escape($aValues) . ")"
+        ];
+    }
+
+    public function getViewerSQLParts($sContentTable, $sContentField, $iViewerId = 0)
+    {
+        if(!$iViewerId)
+            $iViewerId = $this->_iViewerId;
+
+        $aViewerInfo = BxDolProfileQuery::getInstance()->getInfoById($iViewerId);
+        if(empty($aViewerInfo) || !is_array($aViewerInfo))
+            return [];
+
+        return [
+            'where' => " AND 1 << (`" . $sContentTable . "`.`" . $sContentField . "` - 1) & " . $aViewerInfo['cfw_value']
+        ];
+    }
+
+    public function getConditions($sContentTable, $sContentField, $iViewerId = 0)
+    {
+        $aResult = [];
+
+        $aConditions = $this->getSettingConditions($sContentTable, $sContentField);
+        if(!empty($aConditions) && is_array($aConditions))
+            $aResult['cf_setting'] = $aConditions;
+
+        $aConditions = $this->getViewerConditions($sContentTable, $sContentField, $iViewerId);
+        if(!empty($aConditions) && is_array($aConditions))
+            $aResult['cf_viewer'] = $aConditions;
+
+        return $aResult;
+    }
+
+    public function getSettingConditions($sContentTable, $sContentField)
+    {
+        $aValues = $this->getProhibited();
+        if(!$aValues)
+            return [];
+
+        return [
+            'value' => $aValues,
+            'field' => $sContentField,
+            'operator' => 'not in',
+            'table' => $sContentTable
+        ];
+    }
+
+    public function getViewerConditions($sContentTable, $sContentField, $iViewerId = 0)
+    {
+        if(!$iViewerId)
+            $iViewerId = $this->_iViewerId;
+
+        $aViewerInfo = BxDolProfileQuery::getInstance()->getInfoById($iViewerId);
+        if(empty($aViewerInfo) || !is_array($aViewerInfo))
+            return;
+
+        return [
+            'value' => $aViewerInfo['cfw_value'],
+            'field' => $sContentField,
+            'operator' => 'in_set',
+            'table' => $sContentTable
+        ];
+    }
+}
+
+/** @} */

+ 0 - 9
inc/classes/BxDolProfile.php

@@ -603,15 +603,6 @@ class BxDolProfile extends BxDolFactory implements iBxDolProfile
         }
     }
 
-    public function checkContentFilter($iValue, $iProfileId = 0)
-    {
-        $aProfileInfo = $this->getInfo($iProfileId);
-        if(!$aProfileInfo)
-            return false;
-
-        return (1 << ($iValue - 1)) & $aProfileInfo['cfw_value'];
-    }
-            
     /**
      * Add permament messages.
      */

+ 3 - 1
install/sql/system.sql

@@ -474,7 +474,9 @@ INSERT INTO `sys_options`(`category_id`, `name`, `caption`, `value`, `type`, `ex
 (@iCategoryId, 'sys_recaptcha_key_private', '_adm_stg_cpt_option_sys_recaptcha_key_private', '', 'digit', '', '', '', 21),
 (@iCategoryId, 'sys_add_nofollow', '_adm_stg_cpt_option_sys_add_nofollow', 'on', 'checkbox', '', '', '', 30),
 (@iCategoryId, 'sys_security_block_content_after_n_reports', '_adm_stg_cpt_option_sys_security_block_content_after_n_reports', '0', 'digit', '', '', '', 35),
-(@iCategoryId, 'sys_cf_prohibited', '_adm_stg_cpt_option_sys_cf_prohibited', '', 'list', 'a:4:{s:6:"module";s:6:"system";s:6:"method";s:25:"get_options_cf_prohibited";s:6:"params";a:0:{}s:5:"class";s:13:"TemplServices";}', '', '', 40);
+
+(@iCategoryId, 'sys_cf_enable', '_adm_stg_cpt_option_sys_cf_enable', 'on', 'checkbox', '', '', '', 40),
+(@iCategoryId, 'sys_cf_prohibited', '_adm_stg_cpt_option_sys_cf_prohibited', '', 'list', 'a:4:{s:6:"module";s:6:"system";s:6:"method";s:25:"get_options_cf_prohibited";s:6:"params";a:0:{}s:5:"class";s:13:"TemplServices";}', '', '', 41);
 
 --
 -- CATEGORY: Site Settings

+ 6 - 25
modules/base/general/classes/BxBaseModGeneralDb.php

@@ -360,9 +360,8 @@ class BxBaseModGeneralDb extends BxDolModuleDb
     protected function _addCustomConditions($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
     {
         $this->_addConditionsForAuthorStatus($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
-        
-        $this->_addConditionsForProhibitedCf($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
-        $this->_addConditionsForViewerCf($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
+
+        $this->_addConditionsForCf($aParams, $aMethod, $sSelectClause, $sJoinClause, $sWhereClause, $sOrderClause, $sLimitClause);
     }
     
     protected function _addConditionsForAuthorStatus($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
@@ -377,35 +376,17 @@ class BxBaseModGeneralDb extends BxDolModuleDb
         $sJoinClause .= " INNER JOIN `sys_profiles` as `p` ON (`p`.`id` = `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_AUTHOR']}` AND `p`.`status` = 'active') ";
     }
 
-    protected function _addConditionsForProhibitedCf($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
-    {
-        $CNF = &$this->_oConfig->CNF;
-        if(empty($CNF['FIELD_CF']))
-            return;
-
-        $sValues = getParam('sys_cf_prohibited');
-        if(!$sValues)
-            return;
-
-        $aValues = explode(',', $sValues);
-        if(!$aValues)
-            return;
-
-        $sWhereClause .= " AND `" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_CF'] . "` NOT IN (" . $this->implode_escape($aValues) . ")";
-    }
-
-    protected function _addConditionsForViewerCf($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
+    protected function _addConditionsForCf($aParams, &$aMethod, &$sSelectClause, &$sJoinClause, &$sWhereClause, &$sOrderClause, &$sLimitClause)
     {
         $CNF = &$this->_oConfig->CNF;
         if(empty($CNF['FIELD_CF']))
             return;
 
-        $iViewer = bx_get_logged_profile_id();
-        $aViewerInfo = BxDolProfileQuery::getInstance()->getInfoById($iViewer);
-        if(empty($aViewerInfo) || !is_array($aViewerInfo))
+        $oCf = BxDolContentFilter::getInstance();
+        if(!$oCf->isEnabled()) 
             return;
 
-        $sWhereClause .= " AND 1 << (`" . $CNF['TABLE_ENTRIES'] . "`.`" . $CNF['FIELD_CF'] . "` - 1) & " . $aViewerInfo['cfw_value'];
+        $sWhereClause .= $oCf->getSQLParts($CNF['TABLE_ENTRIES'], $CNF['FIELD_CF']);
     }
 
     protected function _getEntriesBySearchIdsOrder($aParams, &$sOrderClause)

+ 2 - 13
modules/base/general/classes/BxBaseModGeneralPageEntry.php

@@ -57,19 +57,8 @@ class BxBaseModGeneralPageEntry extends BxTemplPage
         }
 
         if(!empty($CNF['FIELD_CF'])) {
-            // check global content filter
-            $sValues = getParam('sys_cf_prohibited');
-            if(!empty($sValues)) {
-                $aValues = explode(',', $sValues);
-                if(!empty($aValues) && is_array($aValues) && in_array($this->_aContentInfo[$CNF['FIELD_CF']], $aValues)) {
-                    $this->_oTemplate->displayPageNotFound();
-                    exit;
-                }
-            }
-
-            // check Viewer's content filter
-            $oViewer = BxDolProfile::getInstance();
-            if($oViewer && !$oViewer->checkContentFilter($this->_aContentInfo[$CNF['FIELD_CF']])) {
+            $oCf = BxDolContentFilter::getInstance();
+            if($oCf->isEnabled() && !$oCf->isAllowed($this->_aContentInfo[$CNF['FIELD_CF']])) {
                 $this->_oTemplate->displayPageNotFound();
                 exit;
             }

+ 7 - 31
modules/base/general/classes/BxBaseModGeneralSearchResult.php

@@ -59,8 +59,7 @@ class BxBaseModGeneralSearchResult extends BxTemplSearchResult
     {
         $this->addConditionsForAuthorStatus($CNF);
 
-        $this->addConditionsForProhibitedCf($CNF);
-        $this->addConditionsForViewerCf($CNF);
+        $this->addConditionsForCf($CNF);
     }
 
     protected function addConditionsForAuthorStatus($CNF)
@@ -84,41 +83,18 @@ class BxBaseModGeneralSearchResult extends BxTemplSearchResult
         ];
     }
 
-    protected function addConditionsForProhibitedCf($CNF)
+    protected function addConditionsForCf($CNF)
     {
         if(empty($CNF['FIELD_CF']))
             return;
 
-        $sValues = getParam('sys_cf_prohibited');
-        if(!$sValues)
+        $oCf = BxDolContentFilter::getInstance();
+        if(!$oCf->isEnabled()) 
             return;
 
-        $aValues = explode(',', $sValues);
-        if(!$aValues)
-            return;
-
-        $this->aCurrent['restriction']['prohibited_cf'] = [
-            'value' => $aValues,
-            'field' => $CNF['FIELD_CF'],
-            'operator' => 'not in',
-        ];
-    }
-
-    protected function addConditionsForViewerCf($CNF)
-    {
-        if(empty($CNF['FIELD_CF']) || !isLogged())
-            return;
-
-        $iViewer = bx_get_logged_profile_id();
-        $aViewerInfo = BxDolProfileQuery::getInstance()->getInfoById($iViewer);
-        if(empty($aViewerInfo) || !is_array($aViewerInfo))
-            return;
-
-        $this->aCurrent['restriction']['viewer_cf'] = [
-            'value' => $aViewerInfo['cfw_value'],
-            'field' => $CNF['FIELD_CF'],
-            'operator' => 'in_set',
-        ];
+        $aConditions = $oCf->getConditions($this->aCurrent['table'], $CNF['FIELD_CF']);
+        if(!empty($aConditions) && is_array($aConditions))
+            $this->aCurrent['restriction'] = array_merge($this->aCurrent['restriction'], $aConditions);
     }
 
     /**

+ 10 - 1
modules/base/text/classes/BxBaseModTextModule.php

@@ -171,7 +171,16 @@ class BxBaseModTextModule extends BxBaseModGeneralModule implements iBxDolConten
             'CategoriesMultiListContext' => ''
         ));
     }
-    
+
+    public function serviceManageTools($sType = 'common')
+    {
+        $sResult = parent::serviceManageTools($sType);
+        if(!empty($sResult))
+            $this->_oTemplate->addJsSystem(['modules/base/text/js/|manage_tools.js']);
+
+        return $sResult;
+    }
+
     /**
      * @page service Service Calls
      * @section bx_base_text Base Text

+ 59 - 0
modules/base/text/js/manage_tools.js

@@ -0,0 +1,59 @@
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    Posts Posts
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+function BxBaseModTextManageTools(oOptions) {
+    this._iSearchTimeoutId = false;
+    this._sActionsUrl = oOptions.sActionUrl;
+    this._sObjNameGrid = oOptions.sObjNameGrid;
+    this._sObjName = oOptions.sObjName == undefined ? 'oBxBaseModTextManageTools' : oOptions.sObjName;
+
+    this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'fade' : oOptions.sAnimationEffect;
+    this._iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
+    this._sParamsDivider = oOptions.sParamsDivider == undefined ? '#-#' : oOptions.sParamsDivider;
+
+    this._aHtmlIds = oOptions.aHtmlIds == undefined ? {} : oOptions.aHtmlIds;
+    this._oRequestParams = oOptions.oRequestParams == undefined ? {} : oOptions.oRequestParams;
+}
+
+BxBaseModTextManageTools.prototype.onChangeFilter = function(oFilter) {
+    var $this = this;
+    var oFilter1 = $('#bx-grid-filter1-' + this._sObjNameGrid);
+    var sValueFilter1 = oFilter1.length > 0 ? oFilter1.val() : '';
+
+    var oFilter2 = $('#bx-grid-filter2-' + this._sObjNameGrid);
+    var sValueFilter2 = oFilter2.length > 0 ? oFilter2.val() : '';
+
+    var oSearch = $('#bx-grid-search-' + this._sObjNameGrid);
+    var sValueSearch = oSearch.length > 0 ? oSearch.val() : '';
+    if(sValueSearch == _t('_sys_grid_search'))
+        sValueSearch = '';
+
+    clearTimeout($this._iSearchTimeoutId);
+    $this._iSearchTimeoutId = setTimeout(function () {
+        glGrids[$this._sObjNameGrid].setFilter(sValueFilter1 + $this._sParamsDivider + sValueFilter2 + $this._sParamsDivider + sValueSearch, true);
+    }, 500);
+};
+
+BxBaseModTextManageTools.prototype.onClickSettings = function(sMenuObject, oButton) {
+    if($(oButton).hasClass('bx-btn-disabled'))
+        return false;
+
+    bx_menu_popup(sMenuObject, oButton, {}, {
+        content_id: $(oButton).attr('bx_grid_action_data')
+    });
+};
+
+BxBaseModTextManageTools.prototype.onClickDelete = function(iContentId) {
+    $('.bx-popup-applied:visible').dolPopupHide();
+
+    glGrids[this._sObjNameGrid].actionWithId(iContentId, 'delete', {}, '', false, 1);
+};
+
+/** @} */