Skip to content

Commit

Permalink
Fixes Joomla 3.1.4 compatibility issue (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
codeling committed Jul 28, 2013
1 parent 9b86f8e commit 9349c5b
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions bfstop.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

class plgSystembfstop extends JPlugin
{
private $db;
private $app;
private $myapp;
private $mydb;
private $logger;

function plgSystembfstop(& $subject, $config)
Expand All @@ -37,7 +37,7 @@ static function endsWith($haystack, $needle)

function getUnblockLink($id)
{
$token = $this->db->getNewUnblockToken($id);
$token = $this->mydb->getNewUnblockToken($id);
$link = 'index.php?option=com_bfstop'.
'&view=tokenunblock'.
'&token='.$token;
Expand All @@ -59,23 +59,23 @@ function block($logEntry, $duration)
}
// if the IP address is blocked we actually shouldn't be here in the first place
// I guess, but just to make sure
if ($this->db->isIPBlocked($logEntry->ipaddress))
if ($this->mydb->isIPBlocked($logEntry->ipaddress))
{
$this->logger->log('IP '.$logEntry->ipaddress.' is already blocked!', JLog::WARNING);
return;
}
$maxBlocksBefore = $this->params->get('maxBlocksBefore');
if ($maxBlocksBefore > 0)
{
$numberOfPrevBlocks = $this->db->getNumberOfPreviousBlocks($logEntry->ipaddress);
$numberOfPrevBlocks = $this->mydb->getNumberOfPreviousBlocks($logEntry->ipaddress);
$this->logger->log('Number of previous blocks for IP='.$logEntry->ipaddress.': '.$numberOfPrevBlocks, JLog::DEBUG);
if ($numberOfPrevBlocks >= $maxBlocksBefore)
{
$this->logger->log('Number of previous blocks exceeds configured maximum, blocking permanently!', JLog::INFO);
$duration = 0;
}
}
$id = $this->db->blockIP($logEntry, $duration);
$id = $this->mydb->blockIP($logEntry, $duration);

$this->logger->log('Inserted IP address '.$logEntry->ipaddress.' into block list', JLog::INFO);
// send email notification to admin
Expand Down Expand Up @@ -106,7 +106,7 @@ function blockIfTooManyAttempts($logEntry)
{
$interval = $this->getBlockInterval();
$maxNumber = (int)$this->params->get('blockNumber', 15);
if ($this->db->getNumberOfFailedLogins(
if ($this->mydb->getNumberOfFailedLogins(
$interval,
$logEntry->ipaddress,
$logEntry->logtime) < $maxNumber) {
Expand All @@ -124,12 +124,12 @@ function getIPAddr()
private function init()
{
$this->logger = new BFStopLogger((int)$this->params->get('logLevel', BFStopLogger::Disabled));
$this->db = new BFStopDBHelper($this->logger);
$this->notifier = new BFStopNotifier($this->logger, $this->db,
$this->mydb = new BFStopDBHelper($this->logger);
$this->notifier = new BFStopNotifier($this->logger, $this->mydb,
(int)$this->params->get( 'emailtype' ),
$this->params->get('emailaddress'),
$this->params->get('userIDs'));
$this->app = JFactory::getApplication();
$this->myapp = JFactory::getApplication();
}

function notifyOfRemainingAttempts($logEntry)
Expand All @@ -141,7 +141,7 @@ function notifyOfRemainingAttempts($logEntry)
return;
}
$allowedAttempts = (int)$this->params->get('blockNumber', 15);
$numberOfFailedLogins = $this->db->getNumberOfFailedLogins(
$numberOfFailedLogins = $this->mydb->getNumberOfFailedLogins(
$this->getBlockInterval(),
$logEntry->ipaddress, $logEntry->logtime);
$attemptsLeft = $allowedAttempts - $numberOfFailedLogins;
Expand All @@ -153,14 +153,14 @@ function notifyOfRemainingAttempts($logEntry)
return;
}
if ($attemptsLeft > 0) {
$this->app->enqueueMessage(JText::sprintf("X_ATTEMPTS_LEFT", $attemptsLeft));
$this->myapp->enqueueMessage(JText::sprintf("X_ATTEMPTS_LEFT", $attemptsLeft));
}
}

public function isEnabledForCurrentOrigin()
{
$enabledFor = (int)$this->params->get('enabledForOrigin', 3);
return ( ($enabledFor & ($this->app->getClientId()+1)) != 0);
return ( ($enabledFor & ($this->myapp->getClientId()+1)) != 0);
}

public function onUserLoginFailure($user, $options=null)
Expand All @@ -183,12 +183,12 @@ public function onUserLoginFailure($user, $options=null)
$logEntry->logtime = date("Y-m-d H:i:s");
$logEntry->error = $user['error_message'];
$logEntry->username = $user['username'];
$logEntry->origin = $this->app->getClientId();
$logEntry->origin = $this->myapp->getClientId();

$this->logger->log('Failed login attempt from IP address '.$logEntry->ipaddress, JLog::DEBUG);

// insert into log:
$this->db->insertFailedLogin($logEntry);
$this->mydb->insertFailedLogin($logEntry);

$this->notifyOfRemainingAttempts($logEntry);

Expand All @@ -210,16 +210,16 @@ public function OnUserLogin($user, $options)
$info->username = $user['username'];
$this->logger->log('Successful login by '.$info->username.
' from IP address '.$info->ipaddress, JLog::DEBUG);
$this->db->successfulLogin($info);
$this->mydb->successfulLogin($info);
}

function isUnblockRequest()
{
$input = $this->app->input;
$input = $this->myapp->input;
$view = $input->getString('view', '');
$token = $input->getString('token', '');
$result = (strcmp($view, "tokenunblock") == 0 &&
$this->db->unblockTokenExists($token));
$this->mydb->unblockTokenExists($token));
if ($result) {
$this->logger->log('Seeing valid unblock token ('.
$token.'), letting the request pass through to com_bfstop',
Expand All @@ -236,10 +236,10 @@ public function onAfterInitialise()
return;
}
$ipaddress = $this->getIPAddr();
if ($this->db->isIPBlocked($ipaddress))
if ($this->mydb->isIPBlocked($ipaddress))
{
$this->logger->log("Blocked IP Address $ipaddress trying to access ".
$this->db->getClientString($this->app->getClientId()),
$this->mydb->getClientString($this->myapp->getClientId()),
JLog::INFO );
if ($this->isUnblockRequest())
{
Expand All @@ -248,7 +248,7 @@ public function onAfterInitialise()
JPlugin::loadLanguage('plg_system_bfstop');
$message = $this->params->get('blockedMessage', JText::_('BLOCKED_IP_MESSAGE'));
echo $message;
$this->app->close();
$this->myapp->close();
}
}
}

3 comments on commit 9349c5b

@dongilbert
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing these two items to protected instead of private will fix the issue as well.

@codeling
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dongilbert Yes, but only for Joomla 3.1.4, correct? I also want my extension to be able to run on Joomla 2.5 without change.
And $db doesn't refer to the plain db connection, but a custom type handling my db stuff...

@dongilbert
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error on 3.1.2+ is due to a new feature that will auto-populate your $app and $db properties in a plugin. When I made that feature and submitted the patch, I didn't take into account that some plugins may already have these properties, and that they could be set to private instead of an accessible visibility. If you set them to protected, it will still work on 2.5, and the fatal error will go away in 3.1.2+.

We're pushing a 3.1.5 release tonight (?) that fixes this by checking visibility on the properties before doing anything.

Ideally, a plugin would set those properties in the constructor. You could do it like this and work in all versions 2.5+

class plgSystemBfstop extends JPlugin
{
    protected $db;
    protected $app;
    protected $logger;

    public function __construct(&$subject, $config = array())
    {
        $this->logger = new BFStopLogger((int)$this->params->get('logLevel', BFStopLogger::Disabled));
        $this->db = new BFStopDBHelper($this->logger);
        $this->app = JFactory::getApplication();

        parent::__construct($subject, $config);
    }
}

With that in place, you're variables will be what you set them as, and not be overwritten, as the feature doesn't touch the variables if they are already set.

Please sign in to comment.