Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions cleantalk/antispam/acp/main_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Cleantalk\Common\Api\Api;
use Cleantalk\Custom\Errors;
use cleantalk\antispam\model\SuperGlobals;

class main_module
{
Expand Down Expand Up @@ -421,6 +422,8 @@ function sfw_update($access_key = null)
require_once($autoload_path);
}

$super_globals_were_disabled = SuperGlobals::areDisabled($request);

try {
$api_key = $access_key ?: $config['cleantalk_antispam_apikey'];

Expand All @@ -434,15 +437,15 @@ function sfw_update($access_key = null)

$result = $firewall->getUpdater()->update();

$request->disable_super_globals();
SuperGlobals::restore($request, $super_globals_were_disabled);

if (empty($result['error'])) {
$config->set('cleantalk_antispam_sfw_update_last_gc', time());
}

return $result;
} catch (\Exception $e) {
$request->disable_super_globals();
SuperGlobals::restore($request, $super_globals_were_disabled);
error_log('CleanTalk SFW update error: ' . $e->getMessage());
return array('error' => $e->getMessage());
}
Expand All @@ -457,6 +460,8 @@ function sfw_send_logs($access_key)
require_once($autoload_path);
}

$super_globals_were_disabled = SuperGlobals::areDisabled($request);

try {
$request->enable_super_globals();

Expand All @@ -467,15 +472,15 @@ function sfw_send_logs($access_key)

$result = $firewall->sendLogs();

$request->disable_super_globals();
SuperGlobals::restore($request, $super_globals_were_disabled);

if (!isset($result['error'])) {
$config->set('cleantalk_antispam_sfw_logs_send_last_gc', time());
}

return $result;
} catch (\Exception $e) {
$request->disable_super_globals();
SuperGlobals::restore($request, $super_globals_were_disabled);
error_log('CleanTalk SFW send logs error: ' . $e->getMessage());
return array('error' => $e->getMessage());
}
Expand Down
18 changes: 12 additions & 6 deletions cleantalk/antispam/event/main_listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use phpbb\user;
use phpbb\db\driver\driver_interface;
use cleantalk\antispam\model\main_model;
use cleantalk\antispam\model\SuperGlobals;
use phpbb\symfony_request;

/**
Expand Down Expand Up @@ -132,6 +133,8 @@ public function sfw_check($event)
return;
}

$super_globals_were_disabled = SuperGlobals::areDisabled($this->request);

try {
$api_key = $this->config['cleantalk_antispam_apikey'];

Expand All @@ -155,12 +158,13 @@ public function sfw_check($event)

$firewall->run();

$this->request->disable_super_globals();
SuperGlobals::restore($this->request, $super_globals_were_disabled);
} catch (\Exception $e) {
$this->request->disable_super_globals();
SuperGlobals::restore($this->request, $super_globals_were_disabled);
error_log('CleanTalk SFW error: ' . $e->getMessage());
}
}

/**
* Fills tamplate variable by generated JS-code with unique hash
*
Expand Down Expand Up @@ -372,6 +376,8 @@ public function ccf_check($event)
// Remote calls via Common library
$api_key = $this->config['cleantalk_antispam_apikey'];
if ($api_key) {
$super_globals_were_disabled = SuperGlobals::areDisabled($this->request);

try {
// Enable superglobals BEFORE RC check — RemoteCalls::check() reads $_GET/$_REQUEST
$this->request->enable_super_globals();
Expand All @@ -384,17 +390,17 @@ public function ccf_check($event)
$remote_calls = new $rc_class($api_key, new $storage_handler_class());
try {
$rc_result = $remote_calls->process();
$this->request->disable_super_globals();
SuperGlobals::restore($this->request, $super_globals_were_disabled);
die($rc_result);
} catch (\Cleantalk\Common\RemoteCalls\Exceptions\RemoteCallsException $exception) {
$this->request->disable_super_globals();
SuperGlobals::restore($this->request, $super_globals_were_disabled);
error_log('CleanTalk RC error: ' . $exception->getMessage());
die('FAIL ' . json_encode(array('error' => $exception->getMessage())));
}
}
$this->request->disable_super_globals();
SuperGlobals::restore($this->request, $super_globals_were_disabled);
} catch (\Exception $e) {
$this->request->disable_super_globals();
SuperGlobals::restore($this->request, $super_globals_were_disabled);
error_log('CleanTalk RC init error: ' . $e->getMessage());
}
}
Expand Down
46 changes: 46 additions & 0 deletions cleantalk/antispam/model/SuperGlobals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
*
* @package phpBB Extension - Antispam by CleanTalk
* @author Сleantalk team (welcome@cleantalk.org)
* @copyright (C) 2014 СleanTalk team (https://cleantalk.org)
* @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
*
*/

namespace cleantalk\antispam\model;

/**
* Keeps the phpBB superglobals state while CleanTalk Common libraries need them enabled.
*/
class SuperGlobals
{
/**
* Current state of phpBB superglobals.
* Another integration (phpBB embedded into Joomla, etc.) may have enabled them intentionally.
* Returns false for an unusable request object, so nothing gets changed afterwards.
*
* @param \phpbb\request\request $request
* @return bool
*/
static public function areDisabled($request)
{
return is_object($request)
&& method_exists($request, 'super_globals_disabled')
&& $request->super_globals_disabled();
}

/**
* Restores the superglobals state captured before CleanTalk processing
*
* @param \phpbb\request\request $request
* @param bool $were_disabled
*/
static public function restore($request, $were_disabled)
{
if ($were_disabled && is_object($request) && method_exists($request, 'disable_super_globals'))
{
$request->disable_super_globals();
Comment thread
AntonV1211 marked this conversation as resolved.
}
}
}