Преглед изворни кода

#40 - make translatable select values in settings + serialized service call support in settings extra field

Alex Trofimov пре 12 година
родитељ
комит
0ce0310b53

+ 23 - 4
inc/classes/BxDolService.php

@@ -29,13 +29,15 @@ bx_import('BxDolRequest');
  * no alerts available
  *
  */
-class BxDolService extends BxDol {
-
-    function BxDolService () {
+class BxDolService extends BxDol 
+{
+    function BxDolService () 
+    {
         parent::BxDol();
     }
 
-    public static function call($mixed, $sMethod, $aParams = array(), $sClass = 'Module') {
+    public static function call($mixed, $sMethod, $aParams = array(), $sClass = 'Module') 
+    {
         bx_import('BxDolModuleQuery');
         $oDb = BxDolModuleQuery::getInstance();
 
@@ -47,5 +49,22 @@ class BxDolService extends BxDol {
 
         return empty($aModule) ? '' : BxDolRequest::processAsService($aModule, $sMethod, $aParams, $sClass);
     }
+
+    public static function callSerialized($s, $aMarkers = array(), $sReplaceIn = 'params') 
+    {
+        $a = @unserialize($s);
+        if (false === $a || !is_array($a))
+            return '';
+
+        if (isset($a[$sReplaceIn]))
+            $a[$sReplaceIn] = bx_replace_markers($a[$sReplaceIn], $aMarkers);
+
+        return self::call($a['module'], $a['method'], isset($a['params']) ? $a['params'] : array(), isset($a['class']) ? $a['class'] : 'Module');
+    }
+
+    public static function isSerializedService($s) 
+    {
+        return preg_match('/^a:[\d+]:\{/', $s);
+    }
 }
 

+ 8 - 0
modules/boonex/antispam/classes/BxAntispamDNSBlacklists.php

@@ -112,6 +112,14 @@ class BxAntispamDNSBlacklists extends BxDol
         $oDb->cleanCache('sys_dnsbl_'.BX_DOL_DNSBL_CHAIN_WHITELIST);
     }
 
+    public function getDNSBLConfigValues ()
+    {
+        return array (
+            'block' => _t('_bx_antispam_dnsbl_behaviour_login_block'),
+            'log' => _t('_bx_antispam_dnsbl_behaviour_login_log'),
+        );
+    }
+
     /*************** private function ***************/
 
     private function dnsbl_lookup(&$zones, $key, $querymode)

+ 8 - 0
modules/boonex/antispam/classes/BxAntispamDNSURIBlacklists.php

@@ -137,6 +137,14 @@ class BxAntispamDNSURIBlacklists extends BxDol
         $o->onPositiveDetection (getVisitorIP(false), $sExtraData, 'dnsbluri');
     }
 
+    public function getURIDNSBLConfigValues ()
+    {
+        return array (
+            'block' => _t('_bx_antispam_dnsbl_behaviour_join_block'),
+            'approval' => _t('_bx_antispam_dnsbl_behaviour_join_approval'),
+        );
+    }
+
     /*************** private function ***************/
 
     private function isDbZoneMatch ($iLevel, $sZone) 

+ 19 - 5
modules/boonex/antispam/classes/BxAntispamIP.php

@@ -9,6 +9,10 @@
  * @{
  */
 
+define('BX_ANTISPAM_IP_TABLE_DISABLED', 0);
+define('BX_ANTISPAM_IP_TABLE_ALL_ALLOWED_EXCEPT_LISTED', 1);
+define('BX_ANTISPAM_IP_TABLE_ALL_BLOCKED_EXCEPT_LISTED', 2);
+
 /**
  * Blocking/whitelisting by IP using local database
  */
@@ -31,7 +35,7 @@ class BxAntispamIP extends BxDol
             return true;
 
         $iIPGlobalType = (int)getParam('bx_antispam_ip_list_type');
-        if ($iIPGlobalType != 1 && $iIPGlobalType != 2) // 0 - disabled
+        if (BX_ANTISPAM_IP_TABLE_DISABLED == $iIPGlobalType)
             return false;
 
         if (!$sCurIP)
@@ -52,7 +56,7 @@ class BxAntispamIP extends BxDol
             return false;
 
         $iIPGlobalType = (int)getParam('bx_antispam_ip_list_type');
-        if ($iIPGlobalType != 1 && $iIPGlobalType != 2) // 0 - disabled
+        if (BX_ANTISPAM_IP_TABLE_DISABLED == $iIPGlobalType)
             return false;
 
         if (!$sCurIP)
@@ -64,9 +68,7 @@ class BxAntispamIP extends BxDol
         if ($this->_isIpListed('deny', $sCurIP))
             return true;
 
-        // 1 - all allowed except listed
-        // 2 - all blocked except listed
-        return $iIPGlobalType == 2 ? true : false;
+        return BX_ANTISPAM_IP_TABLE_ALL_BLOCKED_EXCEPT_LISTED == $iIPGlobalType ? true : false;
     }
 
 
@@ -109,6 +111,18 @@ class BxAntispamIP extends BxDol
         return $oDb->getRow($sQuery);
     }
 
+    /**
+     * Get IP table config values
+     */
+    public function getIpTableConfigValues () 
+    {
+        return array (
+            0 => _t('_bx_antispam_ip_table_disabled'),
+            1 => _t('_bx_antispam_ip_table_all_allowed_except_listed'),
+            2 => _t('_bx_antispam_ip_table_all_blocked_except_listed'),
+        );
+    }
+
     /**
      * Clean up expired entries
      * @return number of affected rows

+ 30 - 12
modules/boonex/antispam/classes/BxAntispamModule.php

@@ -13,22 +13,22 @@ bx_import('BxDolModule');
 
 class BxAntispamModule extends BxDolModule 
 {
-    function __construct(&$aModule) 
+    public function __construct(&$aModule) 
     {
         parent::__construct($aModule);
     }
 
-    function serviceIpTable () 
+    public function serviceIpTable () 
     {
         return $this->_grid('bx_antispam_grid_ip_table');
     }
 
-    function serviceDnsblList () 
+    public function serviceDnsblList () 
     {
         return $this->_grid('bx_antispam_grid_dnsbl');
     }
 
-    function serviceBlockLog () 
+    public function serviceBlockLog () 
     {
         return $this->_grid('bx_antispam_grid_block_log');
     }
@@ -47,7 +47,7 @@ class BxAntispamModule extends BxDolModule
      *          BX_SLASHES_NO_ACTION - do not perform any action with slashes
      * @return true if spam detected and content shouln't be recorded, false if content should be processed as usual.
      */
-    function serviceIsSpam ($sContent, $sIp = '', $isStripSlashes = BX_SLASHES_AUTO) 
+    public function serviceIsSpam ($sContent, $sIp = '', $isStripSlashes = BX_SLASHES_AUTO) 
     {
         if (defined('BX_DOL_CRON_EXECUTE') || isAdmin())
             return false;
@@ -110,7 +110,7 @@ class BxAntispamModule extends BxDolModule
      * @param $sIp IP to check, or empty for current IP
      * @return empty string - if join should be allowed, error message - if join should be blocked
      */
-    function serviceCheckLogin ($sIp = '')
+    public function serviceCheckLogin ($sIp = '')
     {
         $bLoginBlock = ('block' == $this->_oConfig->getAntispamOption('dnsbl_behaviour_login'));
         $sErrorMsg = '';
@@ -137,7 +137,7 @@ class BxAntispamModule extends BxDolModule
      * @param $sCurIP IP to check, or empty for current IP
      * @return empty string - if join should be allowed, error message - if join should be blocked
      */
-    function serviceCheckJoin ($sEmail, &$bApproval, $sIp = '') 
+    public function serviceCheckJoin ($sEmail, &$bApproval, $sIp = '') 
     {
         $bJoinBlock = ('block' == $this->_oConfig->getAntispamOption('dnsbl_behaviour_join'));
         $bApproval = false;
@@ -174,7 +174,7 @@ class BxAntispamModule extends BxDolModule
      * @param $sNote [optional] place where checking is performed, for example 'join', 'login'
      * @return true if IP blacklisted and not whiteloisted, or false if under cron execution or if IP isn't blacklisted
      */
-    function serviceIsIpDnsBlacklisted($sCurIP = '', $sNote = '')
+    public function serviceIsIpDnsBlacklisted($sCurIP = '', $sNote = '')
     {
         if (defined('BX_DOL_CRON_EXECUTE'))
             return false;
@@ -198,7 +198,7 @@ class BxAntispamModule extends BxDolModule
     /**
      * @see BxAntispamIP::isIpWhitelisted
      */
-    function serviceIsIpWhitelisted($sIp = '')
+    public function serviceIsIpWhitelisted($sIp = '')
     {
         $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
         return $o->isIpWhitelisted($sIp);
@@ -207,7 +207,7 @@ class BxAntispamModule extends BxDolModule
     /**
      * @see BxAntispamIP::isIpBlocked
      */
-    function serviceIsIpBlocked($sIp = '')
+    public function serviceIsIpBlocked($sIp = '')
     {
         $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
         return $o->isIpBlocked($sIp);
@@ -216,7 +216,7 @@ class BxAntispamModule extends BxDolModule
     /**
      * @see BxAntispamIP::blockIp
      */
-    function serviceBlockIp($mixedIP, $iExpirationInSec = 86400, $sComment = '')
+    public function serviceBlockIp($mixedIP, $iExpirationInSec = 86400, $sComment = '')
     {
         $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
         return $o->blockIp($mixedIP, $iExpirationInSec, $sComment);
@@ -225,12 +225,30 @@ class BxAntispamModule extends BxDolModule
     /**
      * @see BxAntispamIP::pruning
      */
-    function servicePruning()
+    public function servicePruning()
     {
         $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
         return $o->pruning();
     }
 
+    /**
+     * @see BxAntispamIP::pruning
+     */
+    public function serviceConfigValues($s)
+    {
+        switch ($s) {
+            case 'ip_table':
+                $o = bx_instance('BxAntispamIP', array(), $this->_aModule);
+                return $o->getIpTableConfigValues();
+            case 'dnsbl_login':
+                $o = bx_instance('BxAntispamDNSBlacklists', array(), $this->_aModule);
+                return $o->getDNSBLConfigValues();
+            case 'dnsbl_join';
+                $o = bx_instance('BxAntispamDNSURIBlacklists', array(), $this->_aModule);
+                return $o->getURIDNSBLConfigValues();
+        }
+    }
+
     protected function getErrorMessageIpBlocked () 
     {
         bx_import('BxDolLanguages');

+ 8 - 1
modules/boonex/antispam/install/langs/en.xml

@@ -35,6 +35,13 @@ This antispam method scan submitted content for the URLs and check them if any o
 	<string name="_bx_antispam_type_dnsbluri"><![CDATA[URI DNSBL]]></string>
 	<string name="_bx_antispam_type_akismet"><![CDATA[Akismet]]></string>
 	<string name="_bx_antispam_type_stopforumspam"><![CDATA[StopForumSpam]]></string>
+	<string name="_bx_antispam_ip_table_disabled"><![CDATA[Disabled]]></string>
+	<string name="_bx_antispam_ip_table_all_allowed_except_listed"><![CDATA[All allowed except listed]]></string>
+	<string name="_bx_antispam_ip_table_all_blocked_except_listed"><![CDATA[All blocked except listed]]></string>
+	<string name="_bx_antispam_dnsbl_behaviour_login_block"><![CDATA[Block]]></string>
+	<string name="_bx_antispam_dnsbl_behaviour_login_log"><![CDATA[Log]]></string>
+	<string name="_bx_antispam_dnsbl_behaviour_join_block"><![CDATA[Block]]></string>
+	<string name="_bx_antispam_dnsbl_behaviour_join_approval"><![CDATA[Approval]]></string>
 
 	<string name="_bx_antispam_popup_help"><![CDATA[DNSBL help]]></string>
 	<string name="_bx_antispam_popup_dnsbl_recheck"><![CDATA[Recheck IP/URL in DNSBL/URIDNSBL]]></string>
@@ -93,7 +100,7 @@ This antispam method scan submitted content for the URLs and check them if any o
 
 	<string name="_bx_antispam_option_block"><![CDATA[Total block all spam content]]></string>
 	<string name="_bx_antispam_option_report"><![CDATA[Send report to admin if spam content discovered]]></string>
-	<string name="_bx_antispam_option_ip_list_type"><![CDATA[IP table type (1 - all allowed except listed, 2 - all blocked except listed, 0 - disabled)]]></string>
+	<string name="_bx_antispam_option_ip_list_type"><![CDATA[IP table type]]></string>
 	<string name="_bx_antispam_option_dnsbl_enable"><![CDATA[Enable DNS Block Lists]]></string>
 	<string name="_bx_antispam_option_dnsbl_behaviour_login"><![CDATA[User login behaviour if listed in DNS Block Lists]]></string>
 	<string name="_bx_antispam_option_dnsbl_behaviour_join"><![CDATA[User join behaviour if listed in DNS Block Lists]]></string>

+ 3 - 3
modules/boonex/antispam/install/sql/enable.sql

@@ -23,7 +23,7 @@ VALUES (@iTypeId, 'bx_antispam_ip_table', '_bx_antispam_adm_stg_cpt_category_ip_
 SET @iCategoryId = LAST_INSERT_ID();
 
 INSERT INTO `sys_options`(`category_id`, `name`, `caption`, `value`, `type`, `extra`, `check`, `check_error`, `order`) VALUES
-(@iCategoryId, 'bx_antispam_ip_list_type', '_bx_antispam_option_ip_list_type', '0', 'digit', '', '', '', 10);
+(@iCategoryId, 'bx_antispam_ip_list_type', '_bx_antispam_option_ip_list_type', '0', 'select', 'a:3:{s:6:"module";s:11:"bx_antispam";s:6:"method";s:13:"config_values";s:6:"params";a:1:{i:0;s:8:"ip_table";}}', '', '', 10);
 
 
 
@@ -33,8 +33,8 @@ SET @iCategoryId = LAST_INSERT_ID();
 
 INSERT INTO `sys_options`(`category_id`, `name`, `caption`, `value`, `type`, `extra`, `check`, `check_error`, `order`) VALUES
 (@iCategoryId, 'bx_antispam_dnsbl_enable', '_bx_antispam_option_dnsbl_enable', 'on', 'checkbox', '', '', '', 10),
-(@iCategoryId, 'bx_antispam_dnsbl_behaviour_login', '_bx_antispam_option_dnsbl_behaviour_login', 'block', 'select', 'block,log', '', '', 20),
-(@iCategoryId, 'bx_antispam_dnsbl_behaviour_join', '_bx_antispam_option_dnsbl_behaviour_join', 'approval', 'select', 'block,approval', '', '', 30),
+(@iCategoryId, 'bx_antispam_dnsbl_behaviour_login', '_bx_antispam_option_dnsbl_behaviour_login', 'block', 'select', 'a:3:{s:6:"module";s:11:"bx_antispam";s:6:"method";s:13:"config_values";s:6:"params";a:1:{i:0;s:11:"dnsbl_login";}}', '', '', 20),
+(@iCategoryId, 'bx_antispam_dnsbl_behaviour_join', '_bx_antispam_option_dnsbl_behaviour_join', 'approval', 'select', 'a:3:{s:6:"module";s:11:"bx_antispam";s:6:"method";s:13:"config_values";s:6:"params";a:1:{i:0;s:10:"dnsbl_join";}}', '', '', 30),
 (@iCategoryId, 'bx_antispam_uridnsbl_enable', '_bx_antispam_option_uridnsbl_enable', 'on', 'checkbox', '', '', '', 40);
 
 

+ 5 - 2
studio/classes/BxDolStudioSettings.php

@@ -107,7 +107,10 @@ class BxDolStudioSettings extends BxTemplStudioPage {
                 $mixedValue = 0;
                 break;
             case 'select':
-                $aValues = explode(",", $aOption['extra']);
+                if (BxDolService::isSerializedService($aOption['extra']))
+                    $aValues = BxDolService::callSerialized($aOption['extra']);
+                else
+                    $aValues = explode(',', $aOption['extra']);
                 $mixedValue = $aValues[0];
                 break;
             case 'text':
@@ -119,4 +122,4 @@ class BxDolStudioSettings extends BxTemplStudioPage {
         return $mixedValue;
     }
 }
-/** @} */
+/** @} */

+ 7 - 2
studio/template/scripts/BxBaseStudioSettings.php

@@ -200,7 +200,9 @@ class BxBaseStudioSettings extends BxDolStudioSettings {
                     ),
                 );
 
-                if(substr($aItem['extra'], 0, 4) == 'PHP:')
+                if (BxDolService::isSerializedService($aItem['extra']))
+                    $aField['values'] = BxDolService::callSerialized($aItem['extra']);
+                elseif (substr($aItem['extra'], 0, 4) == 'PHP:') // TODO: get rid of PHP: values and replace it with serialized string
                     $aField['values'] = eval(substr($aItem['extra'], 4));
                 else
                     foreach(explode(',', $aItem['extra']) as $sValue)
@@ -218,7 +220,10 @@ class BxBaseStudioSettings extends BxDolStudioSettings {
                         'pass' => 'Xss',
                     ),
                 );
-                if(substr($aItem['extra'], 0, 4) == 'PHP:')
+
+                if (BxDolService::isSerializedService($aItem['extra']))
+                    $aField['values'] = BxDolService::callSerialized($aItem['extra']);
+                elseif (substr($aItem['extra'], 0, 4) == 'PHP:') // TODO: get rid of PHP: values and replace it with serialized string
                     $aField['values'] = eval(substr($aItem['extra'], 4));
                 else
                     foreach(explode(',', $aItem['extra']) as $sValue)

+ 2 - 4
template/scripts/BxBaseMenu.php

@@ -127,10 +127,8 @@ class BxBaseMenu extends BxDolMenu {
     protected function _getMenuAddon ($aMenuItem) {
         if (empty($aMenuItem['addon']))
             return '';
-        $a = @unserialize($aMenuItem['addon']);
-        if (false === $a || !is_array($a))
-            return '';
-        return BxDolService::call($a['module'], $a['method'], isset($a['params']) ? bx_replace_markers($a['params'], $this->_aMarkers) : array(), isset($a['class']) ? $a['class'] : 'Module');
+
+        return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
     }
 
     /**

+ 1 - 4
template/scripts/BxBasePage.php

@@ -266,10 +266,7 @@ class BxBasePage extends BxDolPage {
      * @return string
      */
     protected function _getBlockService ($aBlock) {
-        $a = @unserialize($aBlock['content']);
-        if (false === $a || !is_array($a))
-            return '';
-        return BxDolService::call($a['module'], $a['method'], isset($a['params']) ? $this->_replaceMarkers($a['params']) : array(), isset($a['class']) ? $a['class'] : 'Module');
+        return BxDolService::callSerialized($aBlock['content'], $this->_aMarkers);
     }
 
     /**