33namespace Cleantalk \Common ;
44
55use Cleantalk \ApbctWP \Variables \Cookie ;
6+ use Cleantalk \ApbctWP \Variables \Server ;
67use Cleantalk \Common \Firewall \FirewallModule ;
78use Cleantalk \ApbctWP \Variables \Get ;
89
@@ -26,6 +27,11 @@ class Firewall
2627{
2728 public $ ip_array = array ();
2829
30+ /**
31+ * @var array example array ( 'ua', 'ua_id', 'ua_status')
32+ */
33+ public $ user_agent_data = array ();
34+
2935 // Database
3036 protected $ db ;
3137
@@ -64,6 +70,7 @@ public function __construct($db)
6470 $ this ->db = $ db ;
6571 $ this ->debug = (bool )Get::get ('debug ' );
6672 $ this ->ip_array = $ this ->ipGet ();
73+ $ this ->user_agent_data = $ this ->getUserAgentData ();
6774 }
6875
6976 /**
@@ -81,6 +88,42 @@ public function ipGet($ips_input = 'real', $v4_only = true)
8188 return ! empty ($ result ) ? array ('real ' => $ result ) : array ();
8289 }
8390
91+ public function getUserAgentData ()
92+ {
93+ $ server_ua = Server::getString ('HTTP_USER_AGENT ' );
94+ $ ua_table = defined ('APBCT_TBL_AC_UA_BL ' ) ? APBCT_TBL_AC_UA_BL : null ;
95+
96+ if ( $ ua_table ) {
97+ $ ua_bl_query = "SELECT * FROM $ ua_table ORDER BY `ua_status` DESC; " ;
98+ $ ua_bl_results = $ this ->db ->fetchAll ($ ua_bl_query );
99+ foreach ( $ ua_bl_results as $ ua_bl_result ) {
100+ if ( ! empty ($ ua_bl_result ['ua_template ' ]) ) {
101+ $ pattern = '% ' . str_replace (array ('" ' , '% ' ), array ('' , '\% ' ), $ ua_bl_result ['ua_template ' ]) . '%i ' ;
102+ $ match = preg_match ($ pattern , $ server_ua );
103+
104+ if ( $ match === false && in_array (preg_last_error (), array (PREG_BACKTRACK_LIMIT_ERROR , PREG_RECURSION_LIMIT_ERROR ), true ) ) {
105+ continue ;
106+ }
107+
108+ if ( $ match === 1 ) {
109+ $ ua_id = TT ::getArrayValueAsString ($ ua_bl_result , 'id ' );
110+
111+ return array (
112+ 'ua ' => $ server_ua ,
113+ 'ua_id ' => $ ua_id ,
114+ 'ua_status ' => TT ::getArrayValueAsString ($ ua_bl_result , 'ua_status ' ),
115+ );
116+ }
117+ }
118+ }
119+ }
120+ return array (
121+ 'ua ' => $ server_ua ,
122+ 'ua_id ' => null ,
123+ 'ua_status ' => null ,
124+ );
125+ }
126+
84127 /**
85128 * Loads the FireWall module to the array.
86129 * For inner usage only.
@@ -90,11 +133,12 @@ public function ipGet($ips_input = 'real', $v4_only = true)
90133 */
91134 public function loadFwModule (FirewallModule $ module )
92135 {
93- if ( ! in_array ($ module , $ this ->fw_modules )) {
136+ if ( ! in_array ($ module , $ this ->fw_modules ) ) {
94137 $ module ->setDb ($ this ->db );
95138 $ module ->ipAppendAdditional ($ this ->ip_array );
96139 $ this ->fw_modules [$ module ->module_name ] = $ module ;
97140 $ module ->setIpArray ($ this ->ip_array );
141+ $ module ->setUserAgentData ($ this ->user_agent_data );
98142 }
99143 }
100144
@@ -120,8 +164,11 @@ public function run()
120164 $ results [$ module ->module_name ] = $ module_results ;
121165 }
122166
123- if ($ this ->isWhitelisted ($ results )) {
124- // Break protection logic if it whitelisted or trusted network.
167+ if (
168+ $ this ->isWhitelisted ($ results ) &&
169+ ( ! isset ($ this ->user_agent_data ['ua_status ' ]) || (int ) $ this ->user_agent_data ['ua_status ' ] !== 0 )
170+ ) {
171+ // Break protection logic if it whitelisted, or trusted network, or user-agent not blocked.
125172 break ;
126173 }
127174 }
0 commit comments